pexpr.pas 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing of expression for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pexpr;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symdef,symbase,
  22. node,ncal,
  23. tokens,globtype,globals,constexp;
  24. { reads a whole expression }
  25. function expr(dotypecheck:boolean) : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal,typeonly:boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr,typeonly:boolean) : tnode;
  30. procedure string_dec(var def: tdef; allowtypedef: boolean);
  31. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  32. { the ID token has to be consumed before calling this function }
  33. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  34. function get_intconst:TConstExprInt;
  35. function get_stringconst:string;
  36. { Does some postprocessing for a generic type (especially when nested types
  37. of the specialization are used) }
  38. procedure post_comp_expr_gendef(var def: tdef);
  39. implementation
  40. uses
  41. { common }
  42. cutils,
  43. { global }
  44. verbose,
  45. systems,widestr,
  46. { symtable }
  47. symconst,symtable,symsym,defutil,defcmp,
  48. { module }
  49. fmodule,ppu,
  50. { pass 1 }
  51. pass_1,htypechk,
  52. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  53. { parser }
  54. scanner,
  55. pbase,pinline,ptype,pgenutil,
  56. { codegen }
  57. cgbase,procinfo,cpuinfo
  58. ;
  59. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  60. read as - (1**4) and not (-1)**4 PM }
  61. type
  62. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  63. const
  64. highest_precedence = oppower;
  65. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;forward;
  66. const
  67. { true, if the inherited call is anonymous }
  68. anon_inherited : boolean = false;
  69. { last def found, only used by anon. inherited calls to insert proper type casts }
  70. srdef : tdef = nil;
  71. procedure string_dec(var def:tdef; allowtypedef: boolean);
  72. { reads a string type with optional length }
  73. { and returns a pointer to the string }
  74. { definition }
  75. var
  76. p : tnode;
  77. begin
  78. def:=cshortstringtype;
  79. consume(_STRING);
  80. if token=_LECKKLAMMER then
  81. begin
  82. if not(allowtypedef) then
  83. Message(parser_e_no_local_para_def);
  84. consume(_LECKKLAMMER);
  85. p:=comp_expr(true,false);
  86. if not is_constintnode(p) then
  87. begin
  88. Message(parser_e_illegal_expression);
  89. { error recovery }
  90. consume(_RECKKLAMMER);
  91. end
  92. else
  93. begin
  94. if (tordconstnode(p).value<=0) then
  95. begin
  96. Message(parser_e_invalid_string_size);
  97. tordconstnode(p).value:=255;
  98. end;
  99. if tordconstnode(p).value>255 then
  100. begin
  101. { longstring is currently unsupported (CEC)! }
  102. { t:=tstringdef.createlong(tordconstnode(p).value))}
  103. Message(parser_e_invalid_string_size);
  104. tordconstnode(p).value:=255;
  105. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  106. end
  107. else
  108. if tordconstnode(p).value<>255 then
  109. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  110. consume(_RECKKLAMMER);
  111. end;
  112. p.free;
  113. end
  114. else
  115. begin
  116. if cs_ansistrings in current_settings.localswitches then
  117. def:=getansistringdef
  118. else
  119. def:=cshortstringtype;
  120. end;
  121. end;
  122. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  123. var
  124. p1,p2,argname : tnode;
  125. prev_in_args,
  126. old_named_args_allowed,
  127. old_allow_array_constructor : boolean;
  128. begin
  129. if token=end_of_paras then
  130. begin
  131. parse_paras:=nil;
  132. exit;
  133. end;
  134. { save old values }
  135. prev_in_args:=in_args;
  136. old_allow_array_constructor:=allow_array_constructor;
  137. old_named_args_allowed:=named_args_allowed;
  138. { set para parsing values }
  139. in_args:=true;
  140. named_args_allowed:=false;
  141. allow_array_constructor:=true;
  142. p2:=nil;
  143. repeat
  144. if __namedpara then
  145. begin
  146. if token=_COMMA then
  147. begin
  148. { empty parameter }
  149. p2:=ccallparanode.create(cnothingnode.create,p2);
  150. end
  151. else
  152. begin
  153. named_args_allowed:=true;
  154. p1:=comp_expr(true,false);
  155. named_args_allowed:=false;
  156. if found_arg_name then
  157. begin
  158. argname:=p1;
  159. p1:=comp_expr(true,false);
  160. p2:=ccallparanode.create(p1,p2);
  161. tcallparanode(p2).parametername:=argname;
  162. end
  163. else
  164. p2:=ccallparanode.create(p1,p2);
  165. found_arg_name:=false;
  166. end;
  167. end
  168. else
  169. begin
  170. p1:=comp_expr(true,false);
  171. p2:=ccallparanode.create(p1,p2);
  172. end;
  173. { it's for the str(l:5,s); }
  174. if __colon and (token=_COLON) then
  175. begin
  176. consume(_COLON);
  177. p1:=comp_expr(true,false);
  178. p2:=ccallparanode.create(p1,p2);
  179. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  180. if try_to_consume(_COLON) then
  181. begin
  182. p1:=comp_expr(true,false);
  183. p2:=ccallparanode.create(p1,p2);
  184. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  185. end
  186. end;
  187. until not try_to_consume(_COMMA);
  188. allow_array_constructor:=old_allow_array_constructor;
  189. in_args:=prev_in_args;
  190. named_args_allowed:=old_named_args_allowed;
  191. parse_paras:=p2;
  192. end;
  193. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  194. var
  195. hp : tnode;
  196. hdef : tdef;
  197. temp : ttempcreatenode;
  198. newstatement : tstatementnode;
  199. begin
  200. { Properties are not allowed, because the write can
  201. be different from the read }
  202. if (nf_isproperty in p1.flags) then
  203. begin
  204. Message(type_e_variable_id_expected);
  205. { We can continue with the loading,
  206. it'll not create errors. Only the expected
  207. result can be wrong }
  208. end;
  209. hp:=p1;
  210. while assigned(hp) and
  211. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  212. hp:=tunarynode(hp).left;
  213. if not assigned(hp) then
  214. internalerror(200410121);
  215. if (hp.nodetype=calln) then
  216. begin
  217. typecheckpass(p1);
  218. result:=internalstatements(newstatement);
  219. hdef:=tpointerdef.create(p1.resultdef);
  220. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  221. addstatement(newstatement,temp);
  222. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  223. addstatement(newstatement,cassignmentnode.create(
  224. cderefnode.create(ctemprefnode.create(temp)),
  225. caddnode.create(ntyp,
  226. cderefnode.create(ctemprefnode.create(temp)),
  227. p2)));
  228. addstatement(newstatement,ctempdeletenode.create(temp));
  229. end
  230. else
  231. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  232. end;
  233. function statement_syssym(l : byte) : tnode;
  234. var
  235. p1,p2,paras : tnode;
  236. err,
  237. prev_in_args : boolean;
  238. begin
  239. prev_in_args:=in_args;
  240. case l of
  241. in_new_x :
  242. begin
  243. if afterassignment or in_args then
  244. statement_syssym:=new_function
  245. else
  246. statement_syssym:=new_dispose_statement(true);
  247. end;
  248. in_dispose_x :
  249. begin
  250. statement_syssym:=new_dispose_statement(false);
  251. end;
  252. in_ord_x :
  253. begin
  254. consume(_LKLAMMER);
  255. in_args:=true;
  256. p1:=comp_expr(true,false);
  257. consume(_RKLAMMER);
  258. p1:=geninlinenode(in_ord_x,false,p1);
  259. statement_syssym := p1;
  260. end;
  261. in_exit :
  262. begin
  263. if try_to_consume(_LKLAMMER) then
  264. begin
  265. if not (m_mac in current_settings.modeswitches) then
  266. begin
  267. if not(try_to_consume(_RKLAMMER)) then
  268. begin
  269. p1:=comp_expr(true,false);
  270. consume(_RKLAMMER);
  271. if (not assigned(current_procinfo) or
  272. is_void(current_procinfo.procdef.returndef)) then
  273. begin
  274. Message(parser_e_void_function);
  275. { recovery }
  276. p1.free;
  277. p1:=nil;
  278. end;
  279. end
  280. else
  281. p1:=nil;
  282. end
  283. else
  284. begin
  285. if not (current_procinfo.procdef.procsym.name = pattern) then
  286. Message(parser_e_macpas_exit_wrong_param);
  287. consume(_ID);
  288. consume(_RKLAMMER);
  289. p1:=nil;
  290. end
  291. end
  292. else
  293. p1:=nil;
  294. statement_syssym:=cexitnode.create(p1);
  295. end;
  296. in_break :
  297. begin
  298. statement_syssym:=cbreaknode.create
  299. end;
  300. in_continue :
  301. begin
  302. statement_syssym:=ccontinuenode.create
  303. end;
  304. in_leave :
  305. begin
  306. if m_mac in current_settings.modeswitches then
  307. statement_syssym:=cbreaknode.create
  308. else
  309. begin
  310. Message1(sym_e_id_not_found, orgpattern);
  311. statement_syssym:=cerrornode.create;
  312. end;
  313. end;
  314. in_cycle :
  315. begin
  316. if m_mac in current_settings.modeswitches then
  317. statement_syssym:=ccontinuenode.create
  318. else
  319. begin
  320. Message1(sym_e_id_not_found, orgpattern);
  321. statement_syssym:=cerrornode.create;
  322. end;
  323. end;
  324. in_typeof_x :
  325. begin
  326. consume(_LKLAMMER);
  327. in_args:=true;
  328. p1:=comp_expr(true,false);
  329. consume(_RKLAMMER);
  330. if p1.nodetype=typen then
  331. ttypenode(p1).allowed:=true;
  332. { Allow classrefdef, which is required for
  333. Typeof(self) in static class methods }
  334. if not(is_objc_class_or_protocol(p1.resultdef)) and
  335. ((p1.resultdef.typ = objectdef) or
  336. (assigned(current_procinfo) and
  337. ((po_classmethod in current_procinfo.procdef.procoptions) or
  338. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  339. (p1.resultdef.typ=classrefdef))) then
  340. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  341. else
  342. begin
  343. Message(parser_e_class_id_expected);
  344. p1.destroy;
  345. statement_syssym:=cerrornode.create;
  346. end;
  347. end;
  348. in_sizeof_x,
  349. in_bitsizeof_x :
  350. begin
  351. consume(_LKLAMMER);
  352. in_args:=true;
  353. p1:=comp_expr(true,false);
  354. consume(_RKLAMMER);
  355. if ((p1.nodetype<>typen) and
  356. (
  357. (is_object(p1.resultdef) and
  358. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  359. is_open_array(p1.resultdef) or
  360. is_array_of_const(p1.resultdef) or
  361. is_open_string(p1.resultdef)
  362. )) or
  363. { keep the function call if it is a type parameter to avoid arithmetic errors due to constant folding }
  364. (p1.resultdef.typ=undefineddef) then
  365. begin
  366. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  367. { no packed bit support for these things }
  368. if (l = in_bitsizeof_x) then
  369. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  370. end
  371. else
  372. begin
  373. { allow helpers for SizeOf and BitSizeOf }
  374. if p1.nodetype=typen then
  375. ttypenode(p1).helperallowed:=true;
  376. if (p1.resultdef.typ=forwarddef) then
  377. Message1(type_e_type_is_not_completly_defined,tforwarddef(p1.resultdef).tosymname^);
  378. if (l = in_sizeof_x) or
  379. (not((p1.nodetype = vecn) and
  380. is_packed_array(tvecnode(p1).left.resultdef)) and
  381. not((p1.nodetype = subscriptn) and
  382. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  383. begin
  384. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  385. if (l = in_bitsizeof_x) then
  386. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  387. end
  388. else
  389. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  390. { p1 not needed !}
  391. p1.destroy;
  392. end;
  393. end;
  394. in_typeinfo_x,
  395. in_objc_encode_x :
  396. begin
  397. if (l=in_typeinfo_x) or
  398. (m_objectivec1 in current_settings.modeswitches) then
  399. begin
  400. consume(_LKLAMMER);
  401. in_args:=true;
  402. p1:=comp_expr(true,false);
  403. { When reading a class type it is parsed as loadvmtaddrn,
  404. typeinfo only needs the type so we remove the loadvmtaddrn }
  405. if p1.nodetype=loadvmtaddrn then
  406. begin
  407. p2:=tloadvmtaddrnode(p1).left;
  408. tloadvmtaddrnode(p1).left:=nil;
  409. p1.free;
  410. p1:=p2;
  411. end;
  412. if p1.nodetype=typen then
  413. begin
  414. ttypenode(p1).allowed:=true;
  415. { allow helpers for TypeInfo }
  416. if l=in_typeinfo_x then
  417. ttypenode(p1).helperallowed:=true;
  418. end;
  419. { else
  420. begin
  421. p1.destroy;
  422. p1:=cerrornode.create;
  423. Message(parser_e_illegal_parameter_list);
  424. end;}
  425. consume(_RKLAMMER);
  426. p2:=geninlinenode(l,false,p1);
  427. statement_syssym:=p2;
  428. end
  429. else
  430. begin
  431. Message1(sym_e_id_not_found, orgpattern);
  432. statement_syssym:=cerrornode.create;
  433. end;
  434. end;
  435. in_unaligned_x :
  436. begin
  437. err:=false;
  438. consume(_LKLAMMER);
  439. in_args:=true;
  440. p1:=comp_expr(true,false);
  441. p2:=ccallparanode.create(p1,nil);
  442. p2:=geninlinenode(in_unaligned_x,false,p2);
  443. consume(_RKLAMMER);
  444. statement_syssym:=p2;
  445. end;
  446. in_assigned_x :
  447. begin
  448. err:=false;
  449. consume(_LKLAMMER);
  450. in_args:=true;
  451. p1:=comp_expr(true,false);
  452. { When reading a class type it is parsed as loadvmtaddrn,
  453. typeinfo only needs the type so we remove the loadvmtaddrn }
  454. if p1.nodetype=loadvmtaddrn then
  455. begin
  456. p2:=tloadvmtaddrnode(p1).left;
  457. tloadvmtaddrnode(p1).left:=nil;
  458. p1.free;
  459. p1:=p2;
  460. end;
  461. if not codegenerror then
  462. begin
  463. case p1.resultdef.typ of
  464. procdef, { procvar }
  465. pointerdef,
  466. procvardef,
  467. classrefdef : ;
  468. objectdef :
  469. if not is_implicit_pointer_object_type(p1.resultdef) then
  470. begin
  471. Message(parser_e_illegal_parameter_list);
  472. err:=true;
  473. end;
  474. arraydef :
  475. if not is_dynamic_array(p1.resultdef) then
  476. begin
  477. Message(parser_e_illegal_parameter_list);
  478. err:=true;
  479. end;
  480. else
  481. if p1.resultdef.typ<>undefineddef then
  482. begin
  483. Message(parser_e_illegal_parameter_list);
  484. err:=true;
  485. end;
  486. end;
  487. end
  488. else
  489. err:=true;
  490. if not err then
  491. begin
  492. p2:=ccallparanode.create(p1,nil);
  493. p2:=geninlinenode(in_assigned_x,false,p2);
  494. end
  495. else
  496. begin
  497. p1.free;
  498. p2:=cerrornode.create;
  499. end;
  500. consume(_RKLAMMER);
  501. statement_syssym:=p2;
  502. end;
  503. in_addr_x :
  504. begin
  505. consume(_LKLAMMER);
  506. in_args:=true;
  507. p1:=comp_expr(true,false);
  508. p1:=caddrnode.create(p1);
  509. consume(_RKLAMMER);
  510. statement_syssym:=p1;
  511. end;
  512. in_ofs_x :
  513. begin
  514. consume(_LKLAMMER);
  515. in_args:=true;
  516. p1:=comp_expr(true,false);
  517. p1:=caddrnode.create(p1);
  518. do_typecheckpass(p1);
  519. { Ofs() returns a cardinal/qword, not a pointer }
  520. p1.resultdef:=uinttype;
  521. consume(_RKLAMMER);
  522. statement_syssym:=p1;
  523. end;
  524. in_seg_x :
  525. begin
  526. consume(_LKLAMMER);
  527. in_args:=true;
  528. p1:=comp_expr(true,false);
  529. p1:=geninlinenode(in_seg_x,false,p1);
  530. consume(_RKLAMMER);
  531. statement_syssym:=p1;
  532. end;
  533. in_high_x,
  534. in_low_x :
  535. begin
  536. consume(_LKLAMMER);
  537. in_args:=true;
  538. p1:=comp_expr(true,false);
  539. p2:=geninlinenode(l,false,p1);
  540. consume(_RKLAMMER);
  541. statement_syssym:=p2;
  542. end;
  543. in_succ_x,
  544. in_pred_x :
  545. begin
  546. consume(_LKLAMMER);
  547. in_args:=true;
  548. p1:=comp_expr(true,false);
  549. p2:=geninlinenode(l,false,p1);
  550. consume(_RKLAMMER);
  551. statement_syssym:=p2;
  552. end;
  553. in_inc_x,
  554. in_dec_x :
  555. begin
  556. consume(_LKLAMMER);
  557. in_args:=true;
  558. p1:=comp_expr(true,false);
  559. if try_to_consume(_COMMA) then
  560. p2:=ccallparanode.create(comp_expr(true,false),nil)
  561. else
  562. p2:=nil;
  563. p2:=ccallparanode.create(p1,p2);
  564. statement_syssym:=geninlinenode(l,false,p2);
  565. consume(_RKLAMMER);
  566. end;
  567. in_slice_x:
  568. begin
  569. if not(in_args) then
  570. begin
  571. message(parser_e_illegal_slice);
  572. consume(_LKLAMMER);
  573. in_args:=true;
  574. comp_expr(true,false).free;
  575. if try_to_consume(_COMMA) then
  576. comp_expr(true,false).free;
  577. statement_syssym:=cerrornode.create;
  578. consume(_RKLAMMER);
  579. end
  580. else
  581. begin
  582. consume(_LKLAMMER);
  583. in_args:=true;
  584. p1:=comp_expr(true,false);
  585. Consume(_COMMA);
  586. if not(codegenerror) then
  587. p2:=ccallparanode.create(comp_expr(true,false),nil)
  588. else
  589. p2:=cerrornode.create;
  590. p2:=ccallparanode.create(p1,p2);
  591. statement_syssym:=geninlinenode(l,false,p2);
  592. consume(_RKLAMMER);
  593. end;
  594. end;
  595. in_initialize_x:
  596. begin
  597. statement_syssym:=inline_initialize;
  598. end;
  599. in_finalize_x:
  600. begin
  601. statement_syssym:=inline_finalize;
  602. end;
  603. in_copy_x:
  604. begin
  605. statement_syssym:=inline_copy;
  606. end;
  607. in_concat_x :
  608. begin
  609. consume(_LKLAMMER);
  610. in_args:=true;
  611. { Translate to x:=x+y[+z]. The addnode will do the
  612. type checking }
  613. p2:=nil;
  614. repeat
  615. p1:=comp_expr(true,false);
  616. if p2<>nil then
  617. p2:=caddnode.create(addn,p2,p1)
  618. else
  619. begin
  620. { Force string type if it isn't yet }
  621. if not(
  622. (p1.resultdef.typ=stringdef) or
  623. is_chararray(p1.resultdef) or
  624. is_char(p1.resultdef)
  625. ) then
  626. inserttypeconv(p1,cshortstringtype);
  627. p2:=p1;
  628. end;
  629. until not try_to_consume(_COMMA);
  630. consume(_RKLAMMER);
  631. statement_syssym:=p2;
  632. end;
  633. in_read_x,
  634. in_readln_x,
  635. in_readstr_x:
  636. begin
  637. if try_to_consume(_LKLAMMER) then
  638. begin
  639. paras:=parse_paras(false,false,_RKLAMMER);
  640. consume(_RKLAMMER);
  641. end
  642. else
  643. paras:=nil;
  644. p1:=geninlinenode(l,false,paras);
  645. statement_syssym := p1;
  646. end;
  647. in_setlength_x:
  648. begin
  649. statement_syssym := inline_setlength;
  650. end;
  651. in_objc_selector_x:
  652. begin
  653. if (m_objectivec1 in current_settings.modeswitches) then
  654. begin
  655. consume(_LKLAMMER);
  656. in_args:=true;
  657. { don't turn procsyms into calls (getaddr = true) }
  658. p1:=factor(true,false);
  659. p2:=geninlinenode(l,false,p1);
  660. consume(_RKLAMMER);
  661. statement_syssym:=p2;
  662. end
  663. else
  664. begin
  665. Message1(sym_e_id_not_found, orgpattern);
  666. statement_syssym:=cerrornode.create;
  667. end;
  668. end;
  669. in_length_x:
  670. begin
  671. consume(_LKLAMMER);
  672. in_args:=true;
  673. p1:=comp_expr(true,false);
  674. p2:=geninlinenode(l,false,p1);
  675. consume(_RKLAMMER);
  676. statement_syssym:=p2;
  677. end;
  678. in_write_x,
  679. in_writeln_x,
  680. in_writestr_x :
  681. begin
  682. if try_to_consume(_LKLAMMER) then
  683. begin
  684. paras:=parse_paras(true,false,_RKLAMMER);
  685. consume(_RKLAMMER);
  686. end
  687. else
  688. paras:=nil;
  689. p1 := geninlinenode(l,false,paras);
  690. statement_syssym := p1;
  691. end;
  692. in_str_x_string :
  693. begin
  694. consume(_LKLAMMER);
  695. paras:=parse_paras(true,false,_RKLAMMER);
  696. consume(_RKLAMMER);
  697. p1 := geninlinenode(l,false,paras);
  698. statement_syssym := p1;
  699. end;
  700. in_val_x:
  701. Begin
  702. consume(_LKLAMMER);
  703. in_args := true;
  704. p1:= ccallparanode.create(comp_expr(true,false), nil);
  705. consume(_COMMA);
  706. p2 := ccallparanode.create(comp_expr(true,false),p1);
  707. if try_to_consume(_COMMA) then
  708. p2 := ccallparanode.create(comp_expr(true,false),p2);
  709. consume(_RKLAMMER);
  710. p2 := geninlinenode(l,false,p2);
  711. statement_syssym := p2;
  712. End;
  713. in_include_x_y,
  714. in_exclude_x_y :
  715. begin
  716. consume(_LKLAMMER);
  717. in_args:=true;
  718. p1:=comp_expr(true,false);
  719. consume(_COMMA);
  720. p2:=comp_expr(true,false);
  721. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  722. consume(_RKLAMMER);
  723. end;
  724. in_pack_x_y_z,
  725. in_unpack_x_y_z :
  726. begin
  727. consume(_LKLAMMER);
  728. in_args:=true;
  729. p1:=comp_expr(true,false);
  730. consume(_COMMA);
  731. p2:=comp_expr(true,false);
  732. consume(_COMMA);
  733. paras:=comp_expr(true,false);
  734. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  735. consume(_RKLAMMER);
  736. end;
  737. in_assert_x_y :
  738. begin
  739. consume(_LKLAMMER);
  740. in_args:=true;
  741. p1:=comp_expr(true,false);
  742. if try_to_consume(_COMMA) then
  743. p2:=comp_expr(true,false)
  744. else
  745. begin
  746. { then insert an empty string }
  747. p2:=cstringconstnode.createstr('');
  748. end;
  749. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  750. consume(_RKLAMMER);
  751. end;
  752. in_get_frame:
  753. begin
  754. statement_syssym:=geninlinenode(l,false,nil);
  755. end;
  756. (*
  757. in_get_caller_frame:
  758. begin
  759. if try_to_consume(_LKLAMMER) then
  760. begin
  761. {You used to call get_caller_frame as get_caller_frame(get_frame),
  762. however, as a stack frame may not exist, it does more harm than
  763. good, so ignore it.}
  764. in_args:=true;
  765. p1:=comp_expr(true,false);
  766. p1.destroy;
  767. consume(_RKLAMMER);
  768. end;
  769. statement_syssym:=geninlinenode(l,false,nil);
  770. end;
  771. *)
  772. else
  773. internalerror(15);
  774. end;
  775. in_args:=prev_in_args;
  776. end;
  777. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  778. begin
  779. maybe_load_methodpointer:=false;
  780. if not assigned(p1) then
  781. begin
  782. case st.symtabletype of
  783. withsymtable :
  784. begin
  785. if (st.defowner.typ=objectdef) then
  786. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  787. end;
  788. ObjectSymtable,
  789. recordsymtable:
  790. begin
  791. { We are calling from the static class method which has no self node }
  792. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  793. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  794. else
  795. p1:=load_self_node;
  796. { We are calling a member }
  797. maybe_load_methodpointer:=true;
  798. end;
  799. end;
  800. end;
  801. end;
  802. { reads the parameter for a subroutine call }
  803. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tabstractrecorddef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  804. var
  805. membercall,
  806. prevafterassn : boolean;
  807. i : integer;
  808. para,p2 : tnode;
  809. currpara : tparavarsym;
  810. aprocdef : tprocdef;
  811. begin
  812. prevafterassn:=afterassignment;
  813. afterassignment:=false;
  814. membercall:=false;
  815. aprocdef:=nil;
  816. { when it is a call to a member we need to load the
  817. methodpointer first
  818. }
  819. membercall:=maybe_load_methodpointer(st,p1);
  820. { When we are expecting a procvar we also need
  821. to get the address in some cases }
  822. if assigned(getprocvardef) then
  823. begin
  824. if (block_type=bt_const) or
  825. getaddr then
  826. begin
  827. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  828. getaddr:=true;
  829. end
  830. else
  831. if ((m_tp_procvar in current_settings.modeswitches) or
  832. (m_mac_procvar in current_settings.modeswitches)) and
  833. not(token in [_CARET,_POINT,_LKLAMMER]) then
  834. begin
  835. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  836. if assigned(aprocdef) then
  837. getaddr:=true;
  838. end;
  839. end;
  840. { only need to get the address of the procedure? }
  841. if getaddr then
  842. begin
  843. { Retrieve info which procvar to call. For tp_procvar the
  844. aprocdef is already loaded above so we can reuse it }
  845. if not assigned(aprocdef) and
  846. assigned(getprocvardef) then
  847. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  848. { generate a methodcallnode or proccallnode }
  849. { we shouldn't convert things like @tcollection.load }
  850. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  851. if assigned(p1) then
  852. begin
  853. { for loading methodpointer of an inherited function
  854. we use self as instance and load the address of
  855. the function directly and not through the vmt (PFV) }
  856. if (cnf_inherited in callflags) then
  857. begin
  858. include(tloadnode(p2).loadnodeflags,loadnf_inherited);
  859. p1.free;
  860. p1:=load_self_node;
  861. end;
  862. if (p1.nodetype<>typen) then
  863. tloadnode(p2).set_mp(p1)
  864. else
  865. p1.free;
  866. end;
  867. p1:=p2;
  868. { no postfix operators }
  869. again:=false;
  870. end
  871. else
  872. begin
  873. para:=nil;
  874. if anon_inherited then
  875. begin
  876. if not assigned(current_procinfo) then
  877. internalerror(200305054);
  878. for i:=0 to current_procinfo.procdef.paras.count-1 do
  879. begin
  880. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  881. if not(vo_is_hidden_para in currpara.varoptions) then
  882. begin
  883. { inheritance by msgint? }
  884. if assigned(srdef) then
  885. { anonymous inherited via msgid calls only require a var parameter for
  886. both methods, so we need some type casting here }
  887. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  888. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  889. para)
  890. else
  891. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  892. end;
  893. end;
  894. end
  895. else
  896. begin
  897. if try_to_consume(_LKLAMMER) then
  898. begin
  899. para:=parse_paras(false,false,_RKLAMMER);
  900. consume(_RKLAMMER);
  901. end;
  902. end;
  903. { indicate if this call was generated by a member and
  904. no explicit self is used, this is needed to determine
  905. how to handle a destructor call (PFV) }
  906. if membercall then
  907. include(callflags,cnf_member_call);
  908. if assigned(obj) then
  909. begin
  910. if not (st.symtabletype in [ObjectSymtable,recordsymtable]) then
  911. internalerror(200310031);
  912. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  913. end
  914. else
  915. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  916. end;
  917. afterassignment:=prevafterassn;
  918. end;
  919. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  920. var
  921. hp,hp2 : tnode;
  922. hpp : ^tnode;
  923. currprocdef : tprocdef;
  924. begin
  925. if not assigned(pv) then
  926. internalerror(200301121);
  927. if (m_tp_procvar in current_settings.modeswitches) or
  928. (m_mac_procvar in current_settings.modeswitches) then
  929. begin
  930. hp:=p2;
  931. hpp:=@p2;
  932. while assigned(hp) and
  933. (hp.nodetype=typeconvn) do
  934. begin
  935. hp:=ttypeconvnode(hp).left;
  936. { save orignal address of the old tree so we can replace the node }
  937. hpp:=@hp;
  938. end;
  939. if (hp.nodetype=calln) and
  940. { a procvar can't have parameters! }
  941. not assigned(tcallnode(hp).left) then
  942. begin
  943. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  944. if assigned(currprocdef) then
  945. begin
  946. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  947. if (po_methodpointer in pv.procoptions) then
  948. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  949. hp.destroy;
  950. { replace the old callnode with the new loadnode }
  951. hpp^:=hp2;
  952. end;
  953. end;
  954. end;
  955. end;
  956. { checks whether sym is a static field and if so, translates the access
  957. to the appropriate node tree }
  958. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  959. var
  960. static_name: shortstring;
  961. srsymtable: tsymtable;
  962. begin
  963. result:=false;
  964. { generate access code }
  965. if (sp_static in sym.symoptions) then
  966. begin
  967. result:=true;
  968. if not nested then
  969. static_name:=lower(sym.owner.name^)+'_'+sym.name
  970. else
  971. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  972. if sym.owner.defowner.typ=objectdef then
  973. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,true)
  974. else
  975. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  976. if assigned(sym) then
  977. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  978. p1.free;
  979. p1:=nil;
  980. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  981. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  982. end;
  983. end;
  984. { the following procedure handles the access to a property symbol }
  985. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  986. var
  987. paras : tnode;
  988. p2 : tnode;
  989. membercall : boolean;
  990. callflags : tcallnodeflags;
  991. propaccesslist : tpropaccesslist;
  992. sym: tsym;
  993. begin
  994. { property parameters? read them only if the property really }
  995. { has parameters }
  996. paras:=nil;
  997. if (ppo_hasparameters in propsym.propoptions) then
  998. begin
  999. if try_to_consume(_LECKKLAMMER) then
  1000. begin
  1001. paras:=parse_paras(false,false,_RECKKLAMMER);
  1002. consume(_RECKKLAMMER);
  1003. end;
  1004. end;
  1005. { indexed property }
  1006. if (ppo_indexed in propsym.propoptions) then
  1007. begin
  1008. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  1009. paras:=ccallparanode.create(p2,paras);
  1010. end;
  1011. { we need only a write property if a := follows }
  1012. { if not(afterassignment) and not(in_args) then }
  1013. if token=_ASSIGNMENT then
  1014. begin
  1015. if getpropaccesslist(propsym,palt_write,propaccesslist) then
  1016. begin
  1017. sym:=propaccesslist.firstsym^.sym;
  1018. case sym.typ of
  1019. procsym :
  1020. begin
  1021. callflags:=[];
  1022. { generate the method call }
  1023. membercall:=maybe_load_methodpointer(st,p1);
  1024. if membercall then
  1025. include(callflags,cnf_member_call);
  1026. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1027. addsymref(sym);
  1028. paras:=nil;
  1029. consume(_ASSIGNMENT);
  1030. { read the expression }
  1031. if propsym.propdef.typ=procvardef then
  1032. getprocvardef:=tprocvardef(propsym.propdef);
  1033. p2:=comp_expr(true,false);
  1034. if assigned(getprocvardef) then
  1035. handle_procvar(getprocvardef,p2);
  1036. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1037. { mark as property, both the tcallnode and the real call block }
  1038. include(p1.flags,nf_isproperty);
  1039. getprocvardef:=nil;
  1040. end;
  1041. fieldvarsym :
  1042. begin
  1043. { generate access code }
  1044. if not handle_staticfield_access(sym,false,p1) then
  1045. propaccesslist_to_node(p1,st,propaccesslist);
  1046. include(p1.flags,nf_isproperty);
  1047. consume(_ASSIGNMENT);
  1048. { read the expression }
  1049. p2:=comp_expr(true,false);
  1050. p1:=cassignmentnode.create(p1,p2);
  1051. end
  1052. else
  1053. begin
  1054. p1:=cerrornode.create;
  1055. Message(parser_e_no_procedure_to_access_property);
  1056. end;
  1057. end;
  1058. end
  1059. else
  1060. begin
  1061. p1:=cerrornode.create;
  1062. Message(parser_e_no_procedure_to_access_property);
  1063. end;
  1064. end
  1065. else
  1066. begin
  1067. if getpropaccesslist(propsym,palt_read,propaccesslist) then
  1068. begin
  1069. sym := propaccesslist.firstsym^.sym;
  1070. case sym.typ of
  1071. fieldvarsym :
  1072. begin
  1073. { generate access code }
  1074. if not handle_staticfield_access(sym,false,p1) then
  1075. propaccesslist_to_node(p1,st,propaccesslist);
  1076. include(p1.flags,nf_isproperty);
  1077. { catch expressions like "(propx):=1;" }
  1078. include(p1.flags,nf_no_lvalue);
  1079. end;
  1080. procsym :
  1081. begin
  1082. callflags:=[];
  1083. { generate the method call }
  1084. membercall:=maybe_load_methodpointer(st,p1);
  1085. if membercall then
  1086. include(callflags,cnf_member_call);
  1087. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1088. paras:=nil;
  1089. include(p1.flags,nf_isproperty);
  1090. include(p1.flags,nf_no_lvalue);
  1091. end
  1092. else
  1093. begin
  1094. p1:=cerrornode.create;
  1095. Message(type_e_mismatch);
  1096. end;
  1097. end;
  1098. end
  1099. else
  1100. begin
  1101. { error, no function to read property }
  1102. p1:=cerrornode.create;
  1103. Message(parser_e_no_procedure_to_access_property);
  1104. end;
  1105. end;
  1106. { release paras if not used }
  1107. if assigned(paras) then
  1108. paras.free;
  1109. end;
  1110. { the ID token has to be consumed before calling this function }
  1111. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  1112. var
  1113. isclassref:boolean;
  1114. begin
  1115. if sym=nil then
  1116. begin
  1117. { pattern is still valid unless
  1118. there is another ID just after the ID of sym }
  1119. Message1(sym_e_id_no_member,orgpattern);
  1120. p1.free;
  1121. p1:=cerrornode.create;
  1122. { try to clean up }
  1123. again:=false;
  1124. end
  1125. else
  1126. begin
  1127. if assigned(p1) then
  1128. begin
  1129. if not assigned(p1.resultdef) then
  1130. do_typecheckpass(p1);
  1131. isclassref:=(p1.resultdef.typ=classrefdef);
  1132. end
  1133. else
  1134. isclassref:=false;
  1135. { we assume, that only procsyms and varsyms are in an object }
  1136. { symbol table, for classes, properties are allowed }
  1137. case sym.typ of
  1138. procsym:
  1139. begin
  1140. do_proc_call(sym,sym.owner,structh,
  1141. (getaddr and not(token in [_CARET,_POINT])),
  1142. again,p1,callflags);
  1143. { we need to know which procedure is called }
  1144. do_typecheckpass(p1);
  1145. { calling using classref? }
  1146. if isclassref and
  1147. (p1.nodetype=calln) and
  1148. assigned(tcallnode(p1).procdefinition) and
  1149. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1150. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1151. Message(parser_e_only_class_members_via_class_ref);
  1152. end;
  1153. fieldvarsym:
  1154. begin
  1155. if not handle_staticfield_access(sym,true,p1) then
  1156. begin
  1157. if isclassref then
  1158. if assigned(p1) and
  1159. (
  1160. is_self_node(p1) or
  1161. (assigned(current_procinfo) and (current_procinfo.procdef.no_self_node) and
  1162. (current_procinfo.procdef.struct=structh))) then
  1163. Message(parser_e_only_class_members)
  1164. else
  1165. Message(parser_e_only_class_members_via_class_ref);
  1166. p1:=csubscriptnode.create(sym,p1);
  1167. end;
  1168. end;
  1169. propertysym:
  1170. begin
  1171. if isclassref and not (sp_static in sym.symoptions) then
  1172. Message(parser_e_only_class_members_via_class_ref);
  1173. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1174. end;
  1175. typesym:
  1176. begin
  1177. p1.free;
  1178. if try_to_consume(_LKLAMMER) then
  1179. begin
  1180. p1:=comp_expr(true,false);
  1181. consume(_RKLAMMER);
  1182. p1:=ctypeconvnode.create_explicit(p1,ttypesym(sym).typedef);
  1183. end
  1184. else
  1185. begin
  1186. p1:=ctypenode.create(ttypesym(sym).typedef);
  1187. if (is_class(ttypesym(sym).typedef) or is_objcclass(ttypesym(sym).typedef)) and
  1188. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1189. p1:=cloadvmtaddrnode.create(p1);
  1190. end;
  1191. end;
  1192. constsym:
  1193. begin
  1194. p1.free;
  1195. p1:=genconstsymtree(tconstsym(sym));
  1196. end;
  1197. staticvarsym:
  1198. begin
  1199. { typed constant is a staticvarsym
  1200. now they are absolutevarsym }
  1201. p1.free;
  1202. p1:=cloadnode.create(sym,sym.Owner);
  1203. end;
  1204. absolutevarsym:
  1205. begin
  1206. p1.free;
  1207. p1:=nil;
  1208. { typed constants are absolutebarsyms now to handle storage properly }
  1209. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1210. end
  1211. else
  1212. internalerror(16);
  1213. end;
  1214. end;
  1215. end;
  1216. function handle_factor_typenode(hdef:tdef;getaddr:boolean;var again:boolean;sym:tsym;typeonly:boolean):tnode;
  1217. var
  1218. srsym : tsym;
  1219. srsymtable : tsymtable;
  1220. begin
  1221. if sym=nil then
  1222. sym:=hdef.typesym;
  1223. { allow Ordinal(Value) for type declarations since it
  1224. can be an enummeration declaration or a set lke:
  1225. (OrdinalType(const1)..OrdinalType(const2) }
  1226. if (not typeonly or is_ordinal(hdef))and try_to_consume(_LKLAMMER) then
  1227. begin
  1228. result:=comp_expr(true,false);
  1229. consume(_RKLAMMER);
  1230. { type casts to class helpers aren't allowed }
  1231. if is_objectpascal_helper(hdef) then
  1232. Message(parser_e_no_category_as_types)
  1233. { recovery by not creating a conversion node }
  1234. else
  1235. result:=ctypeconvnode.create_explicit(result,hdef);
  1236. end
  1237. else { not LKLAMMER }
  1238. if (token=_POINT) and
  1239. (is_object(hdef) or is_record(hdef)) then
  1240. begin
  1241. consume(_POINT);
  1242. { handles calling methods declared in parent objects
  1243. using "parentobject.methodname()" }
  1244. if assigned(current_structdef) and
  1245. not(getaddr) and
  1246. current_structdef.is_related(hdef) then
  1247. begin
  1248. result:=ctypenode.create(hdef);
  1249. ttypenode(result).typesym:=sym;
  1250. { search also in inherited methods }
  1251. searchsym_in_class(tobjectdef(hdef),tobjectdef(current_structdef),pattern,srsym,srsymtable,true);
  1252. if assigned(srsym) then
  1253. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1254. consume(_ID);
  1255. do_member_read(tabstractrecorddef(hdef),false,srsym,result,again,[]);
  1256. end
  1257. else
  1258. begin
  1259. { handles:
  1260. * @TObject.Load
  1261. * static methods and variables }
  1262. result:=ctypenode.create(hdef);
  1263. ttypenode(result).typesym:=sym;
  1264. { TP allows also @TMenu.Load if Load is only }
  1265. { defined in an anchestor class }
  1266. srsym:=search_struct_member(tabstractrecorddef(hdef),pattern);
  1267. if assigned(srsym) then
  1268. begin
  1269. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1270. consume(_ID);
  1271. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[]);
  1272. end
  1273. else
  1274. Message1(sym_e_id_no_member,orgpattern);
  1275. end;
  1276. end
  1277. else
  1278. begin
  1279. { Normally here would be the check against the usage
  1280. of "TClassHelper.Something", but as that might be
  1281. used inside of system symbols like sizeof and
  1282. typeinfo this check is put into ttypenode.pass_1
  1283. (for "TClassHelper" alone) and tcallnode.pass_1
  1284. (for "TClassHelper.Something") }
  1285. { class reference ? }
  1286. if is_class(hdef) or
  1287. is_objcclass(hdef) then
  1288. begin
  1289. if getaddr and (token=_POINT) then
  1290. begin
  1291. consume(_POINT);
  1292. { allows @Object.Method }
  1293. { also allows static methods and variables }
  1294. result:=ctypenode.create(hdef);
  1295. ttypenode(result).typesym:=sym;
  1296. { TP allows also @TMenu.Load if Load is only }
  1297. { defined in an anchestor class }
  1298. srsym:=search_struct_member(tobjectdef(hdef),pattern);
  1299. if assigned(srsym) then
  1300. begin
  1301. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1302. consume(_ID);
  1303. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[]);
  1304. end
  1305. else
  1306. begin
  1307. Message1(sym_e_id_no_member,orgpattern);
  1308. consume(_ID);
  1309. end;
  1310. end
  1311. else
  1312. begin
  1313. result:=ctypenode.create(hdef);
  1314. ttypenode(result).typesym:=sym;
  1315. { For a type block we simply return only
  1316. the type. For all other blocks we return
  1317. a loadvmt node }
  1318. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1319. result:=cloadvmtaddrnode.create(result);
  1320. end;
  1321. end
  1322. else
  1323. begin
  1324. result:=ctypenode.create(hdef);
  1325. ttypenode(result).typesym:=sym;
  1326. end;
  1327. end;
  1328. end;
  1329. {****************************************************************************
  1330. Factor
  1331. ****************************************************************************}
  1332. {---------------------------------------------
  1333. PostFixOperators
  1334. ---------------------------------------------}
  1335. { returns whether or not p1 has been changed }
  1336. function postfixoperators(var p1:tnode;var again:boolean;getaddr:boolean): boolean;
  1337. { tries to avoid syntax errors after invalid qualifiers }
  1338. procedure recoverconsume_postfixops;
  1339. begin
  1340. repeat
  1341. if not try_to_consume(_CARET) then
  1342. if try_to_consume(_POINT) then
  1343. try_to_consume(_ID)
  1344. else if try_to_consume(_LECKKLAMMER) then
  1345. begin
  1346. repeat
  1347. comp_expr(true,false);
  1348. until not try_to_consume(_COMMA);
  1349. consume(_RECKKLAMMER);
  1350. end
  1351. else if try_to_consume(_LKLAMMER) then
  1352. begin
  1353. repeat
  1354. comp_expr(true,false);
  1355. until not try_to_consume(_COMMA);
  1356. consume(_RKLAMMER);
  1357. end
  1358. else
  1359. break;
  1360. until false;
  1361. end;
  1362. procedure handle_variantarray;
  1363. var
  1364. p4 : tnode;
  1365. newstatement : tstatementnode;
  1366. tempresultvariant,
  1367. temp : ttempcreatenode;
  1368. paras : tcallparanode;
  1369. newblock : tnode;
  1370. countindices : aint;
  1371. begin
  1372. { create statements with call initialize the arguments and
  1373. call fpc_dynarr_setlength }
  1374. newblock:=internalstatements(newstatement);
  1375. { get temp for array of indicies,
  1376. we set the real size later }
  1377. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1378. addstatement(newstatement,temp);
  1379. countindices:=0;
  1380. repeat
  1381. p4:=comp_expr(true,false);
  1382. addstatement(newstatement,cassignmentnode.create(
  1383. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1384. inc(countindices);
  1385. until not try_to_consume(_COMMA);
  1386. { set real size }
  1387. temp.size:=countindices*s32inttype.size;
  1388. consume(_RECKKLAMMER);
  1389. { we need only a write access if a := follows }
  1390. if token=_ASSIGNMENT then
  1391. begin
  1392. consume(_ASSIGNMENT);
  1393. p4:=comp_expr(true,false);
  1394. { create call to fpc_vararray_put }
  1395. paras:=ccallparanode.create(cordconstnode.create
  1396. (countindices,s32inttype,true),
  1397. ccallparanode.create(caddrnode.create_internal
  1398. (ctemprefnode.create(temp)),
  1399. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1400. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1401. ,nil))));
  1402. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1403. addstatement(newstatement,ctempdeletenode.create(temp));
  1404. end
  1405. else
  1406. begin
  1407. { create temp for result }
  1408. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1409. addstatement(newstatement,tempresultvariant);
  1410. { create call to fpc_vararray_get }
  1411. paras:=ccallparanode.create(cordconstnode.create
  1412. (countindices,s32inttype,true),
  1413. ccallparanode.create(caddrnode.create_internal
  1414. (ctemprefnode.create(temp)),
  1415. ccallparanode.create(p1,
  1416. ccallparanode.create(
  1417. ctemprefnode.create(tempresultvariant)
  1418. ,nil))));
  1419. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1420. addstatement(newstatement,ctempdeletenode.create(temp));
  1421. { the last statement should return the value as
  1422. location and type, this is done be referencing the
  1423. temp and converting it first from a persistent temp to
  1424. normal temp }
  1425. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1426. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1427. end;
  1428. p1:=newblock;
  1429. end;
  1430. function parse_array_constructor(arrdef:tarraydef): tnode;
  1431. var
  1432. newstatement,assstatement:tstatementnode;
  1433. arrnode:ttempcreatenode;
  1434. temp2:ttempcreatenode;
  1435. assnode:tnode;
  1436. paracount:integer;
  1437. begin
  1438. result:=internalstatements(newstatement);
  1439. { create temp for result }
  1440. arrnode:=ctempcreatenode.create(arrdef,arrdef.size,tt_persistent,true);
  1441. addstatement(newstatement,arrnode);
  1442. paracount:=0;
  1443. { check arguments and create an assignment calls }
  1444. if try_to_consume(_LKLAMMER) then
  1445. begin
  1446. assnode:=internalstatements(assstatement);
  1447. repeat
  1448. { arr[i] := param_i }
  1449. addstatement(assstatement,
  1450. cassignmentnode.create(
  1451. cvecnode.create(
  1452. ctemprefnode.create(arrnode),
  1453. cordconstnode.create(paracount,arrdef.rangedef,false)),
  1454. comp_expr(true,false)));
  1455. inc(paracount);
  1456. until not try_to_consume(_COMMA);
  1457. consume(_RKLAMMER);
  1458. end
  1459. else
  1460. assnode:=nil;
  1461. { get temp for array of lengths }
  1462. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1463. addstatement(newstatement,temp2);
  1464. { one dimensional }
  1465. addstatement(newstatement,cassignmentnode.create(
  1466. ctemprefnode.create_offset(temp2,0),
  1467. cordconstnode.create
  1468. (paracount,s32inttype,true)));
  1469. { create call to fpc_dynarr_setlength }
  1470. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1471. ccallparanode.create(caddrnode.create_internal
  1472. (ctemprefnode.create(temp2)),
  1473. ccallparanode.create(cordconstnode.create
  1474. (1,s32inttype,true),
  1475. ccallparanode.create(caddrnode.create_internal
  1476. (crttinode.create(tstoreddef(arrdef),initrtti,rdt_normal)),
  1477. ccallparanode.create(
  1478. ctypeconvnode.create_internal(
  1479. ctemprefnode.create(arrnode),voidpointertype),
  1480. nil))))
  1481. ));
  1482. { add assignment statememnts }
  1483. addstatement(newstatement,ctempdeletenode.create(temp2));
  1484. if assigned(assnode) then
  1485. addstatement(newstatement,assnode);
  1486. { the last statement should return the value as
  1487. location and type, this is done be referencing the
  1488. temp and converting it first from a persistent temp to
  1489. normal temp }
  1490. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1491. addstatement(newstatement,ctemprefnode.create(arrnode));
  1492. end;
  1493. var
  1494. protsym : tpropertysym;
  1495. p2,p3 : tnode;
  1496. srsym : tsym;
  1497. srsymtable : TSymtable;
  1498. structh : tabstractrecorddef;
  1499. { shouldn't be used that often, so the extra overhead is ok to save
  1500. stack space }
  1501. dispatchstring : ansistring;
  1502. nodechanged : boolean;
  1503. calltype: tdispcalltype;
  1504. label
  1505. skipreckklammercheck;
  1506. begin
  1507. result:=false;
  1508. again:=true;
  1509. while again do
  1510. begin
  1511. { we need the resultdef }
  1512. do_typecheckpass_changed(p1,nodechanged);
  1513. result:=result or nodechanged;
  1514. if codegenerror then
  1515. begin
  1516. recoverconsume_postfixops;
  1517. exit;
  1518. end;
  1519. { handle token }
  1520. case token of
  1521. _CARET:
  1522. begin
  1523. consume(_CARET);
  1524. { support tp/mac procvar^ if the procvar returns a
  1525. pointer type }
  1526. if ((m_tp_procvar in current_settings.modeswitches) or
  1527. (m_mac_procvar in current_settings.modeswitches)) and
  1528. (p1.resultdef.typ=procvardef) and
  1529. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1530. begin
  1531. p1:=ccallnode.create_procvar(nil,p1);
  1532. typecheckpass(p1);
  1533. end;
  1534. if (p1.resultdef.typ<>pointerdef) then
  1535. begin
  1536. { ^ as binary operator is a problem!!!! (FK) }
  1537. again:=false;
  1538. Message(parser_e_invalid_qualifier);
  1539. recoverconsume_postfixops;
  1540. p1.destroy;
  1541. p1:=cerrornode.create;
  1542. end
  1543. else
  1544. p1:=cderefnode.create(p1);
  1545. end;
  1546. _LECKKLAMMER:
  1547. begin
  1548. if is_class_or_interface_or_object(p1.resultdef) or
  1549. is_dispinterface(p1.resultdef) or is_record(p1.resultdef) then
  1550. begin
  1551. { default property }
  1552. protsym:=search_default_property(tabstractrecorddef(p1.resultdef));
  1553. if not(assigned(protsym)) then
  1554. begin
  1555. p1.destroy;
  1556. p1:=cerrornode.create;
  1557. again:=false;
  1558. message(parser_e_no_default_property_available);
  1559. end
  1560. else
  1561. begin
  1562. { The property symbol is referenced indirect }
  1563. protsym.IncRefCount;
  1564. handle_propertysym(protsym,protsym.owner,p1);
  1565. end;
  1566. end
  1567. else
  1568. begin
  1569. consume(_LECKKLAMMER);
  1570. repeat
  1571. { in all of the cases below, p1 is changed }
  1572. case p1.resultdef.typ of
  1573. pointerdef:
  1574. begin
  1575. { support delphi autoderef }
  1576. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1577. (m_autoderef in current_settings.modeswitches) then
  1578. p1:=cderefnode.create(p1);
  1579. p2:=comp_expr(true,false);
  1580. { Support Pbytevar[0..9] which returns array [0..9].}
  1581. if try_to_consume(_POINTPOINT) then
  1582. p2:=crangenode.create(p2,comp_expr(true,false));
  1583. p1:=cvecnode.create(p1,p2);
  1584. end;
  1585. variantdef:
  1586. begin
  1587. handle_variantarray;
  1588. { the RECKKLAMMER is already read }
  1589. goto skipreckklammercheck;
  1590. end;
  1591. stringdef :
  1592. begin
  1593. p2:=comp_expr(true,false);
  1594. { Support string[0..9] which returns array [0..9] of char.}
  1595. if try_to_consume(_POINTPOINT) then
  1596. p2:=crangenode.create(p2,comp_expr(true,false));
  1597. p1:=cvecnode.create(p1,p2);
  1598. end;
  1599. arraydef:
  1600. begin
  1601. p2:=comp_expr(true,false);
  1602. { support SEG:OFS for go32v2 Mem[] }
  1603. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1604. (p1.nodetype=loadn) and
  1605. assigned(tloadnode(p1).symtableentry) and
  1606. assigned(tloadnode(p1).symtableentry.owner.name) and
  1607. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1608. ((tloadnode(p1).symtableentry.name='MEM') or
  1609. (tloadnode(p1).symtableentry.name='MEMW') or
  1610. (tloadnode(p1).symtableentry.name='MEML')) then
  1611. begin
  1612. if try_to_consume(_COLON) then
  1613. begin
  1614. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1615. p2:=comp_expr(true,false);
  1616. p2:=caddnode.create(addn,p2,p3);
  1617. if try_to_consume(_POINTPOINT) then
  1618. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1619. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1620. p1:=cvecnode.create(p1,p2);
  1621. include(tvecnode(p1).flags,nf_memseg);
  1622. include(tvecnode(p1).flags,nf_memindex);
  1623. end
  1624. else
  1625. begin
  1626. if try_to_consume(_POINTPOINT) then
  1627. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1628. p2:=crangenode.create(p2,comp_expr(true,false));
  1629. p1:=cvecnode.create(p1,p2);
  1630. include(tvecnode(p1).flags,nf_memindex);
  1631. end;
  1632. end
  1633. else
  1634. begin
  1635. if try_to_consume(_POINTPOINT) then
  1636. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1637. p2:=crangenode.create(p2,comp_expr(true,false));
  1638. p1:=cvecnode.create(p1,p2);
  1639. end;
  1640. end;
  1641. else
  1642. begin
  1643. if p1.resultdef.typ<>undefineddef then
  1644. Message(parser_e_invalid_qualifier);
  1645. p1.destroy;
  1646. p1:=cerrornode.create;
  1647. comp_expr(true,false);
  1648. again:=false;
  1649. end;
  1650. end;
  1651. do_typecheckpass(p1);
  1652. until not try_to_consume(_COMMA);
  1653. consume(_RECKKLAMMER);
  1654. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1655. skipreckklammercheck:
  1656. end;
  1657. end;
  1658. _POINT :
  1659. begin
  1660. consume(_POINT);
  1661. if (p1.resultdef.typ=pointerdef) and
  1662. (m_autoderef in current_settings.modeswitches) and
  1663. { don't auto-deref objc.id, because then the code
  1664. below for supporting id.anyobjcmethod isn't triggered }
  1665. (p1.resultdef<>objc_idtype) then
  1666. begin
  1667. p1:=cderefnode.create(p1);
  1668. do_typecheckpass(p1);
  1669. end;
  1670. { procvar.<something> can never mean anything so always
  1671. try to call it in case it returns a record/object/... }
  1672. maybe_call_procvar(p1,false);
  1673. case p1.resultdef.typ of
  1674. recorddef:
  1675. begin
  1676. if token=_ID then
  1677. begin
  1678. structh:=tabstractrecorddef(p1.resultdef);
  1679. searchsym_in_record(structh,pattern,srsym,srsymtable);
  1680. if assigned(srsym) then
  1681. begin
  1682. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1683. consume(_ID);
  1684. do_member_read(structh,getaddr,srsym,p1,again,[]);
  1685. end
  1686. else
  1687. begin
  1688. Message1(sym_e_id_no_member,orgpattern);
  1689. p1.destroy;
  1690. p1:=cerrornode.create;
  1691. { try to clean up }
  1692. consume(_ID);
  1693. end;
  1694. end
  1695. else
  1696. consume(_ID);
  1697. end;
  1698. enumdef:
  1699. begin
  1700. if token=_ID then
  1701. begin
  1702. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  1703. p1.destroy;
  1704. if assigned(srsym) and (srsym.typ=enumsym) then
  1705. begin
  1706. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1707. p1:=genenumnode(tenumsym(srsym));
  1708. end
  1709. else
  1710. begin
  1711. Message1(sym_e_id_no_member,orgpattern);
  1712. p1:=cerrornode.create;
  1713. end;
  1714. end;
  1715. consume(_ID);
  1716. end;
  1717. arraydef:
  1718. begin
  1719. if is_dynamic_array(p1.resultdef) then
  1720. begin
  1721. if token=_ID then
  1722. begin
  1723. if pattern='CREATE' then
  1724. begin
  1725. consume(_ID);
  1726. p2:=parse_array_constructor(tarraydef(p1.resultdef));
  1727. p1.destroy;
  1728. p1:=p2;
  1729. end
  1730. else
  1731. begin
  1732. Message2(scan_f_syn_expected,'CREATE',pattern);
  1733. p1.destroy;
  1734. p1:=cerrornode.create;
  1735. consume(_ID);
  1736. end;
  1737. end;
  1738. end
  1739. else
  1740. begin
  1741. Message(parser_e_invalid_qualifier);
  1742. p1.destroy;
  1743. p1:=cerrornode.create;
  1744. consume(_ID);
  1745. end;
  1746. end;
  1747. variantdef:
  1748. begin
  1749. { dispatch call? }
  1750. { lhs := v.ident[parameters] -> property get
  1751. lhs := v.ident(parameters) -> method call
  1752. v.ident[parameters] := rhs -> property put
  1753. v.ident(parameters) := rhs -> also property put }
  1754. if token=_ID then
  1755. begin
  1756. dispatchstring:=orgpattern;
  1757. consume(_ID);
  1758. calltype:=dct_method;
  1759. if try_to_consume(_LKLAMMER) then
  1760. begin
  1761. p2:=parse_paras(false,true,_RKLAMMER);
  1762. consume(_RKLAMMER);
  1763. end
  1764. else if try_to_consume(_LECKKLAMMER) then
  1765. begin
  1766. p2:=parse_paras(false,true,_RECKKLAMMER);
  1767. consume(_RECKKLAMMER);
  1768. calltype:=dct_propget;
  1769. end
  1770. else
  1771. p2:=nil;
  1772. { property setter? }
  1773. if (token=_ASSIGNMENT) and not(afterassignment) then
  1774. begin
  1775. consume(_ASSIGNMENT);
  1776. { read the expression }
  1777. p3:=comp_expr(true,false);
  1778. { concat value parameter too }
  1779. p2:=ccallparanode.create(p3,p2);
  1780. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  1781. end
  1782. else
  1783. { this is only an approximation
  1784. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1785. if afterassignment or in_args or (token<>_SEMICOLON) then
  1786. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,cvarianttype)
  1787. else
  1788. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,voidtype);
  1789. end
  1790. else { Error }
  1791. Consume(_ID);
  1792. end;
  1793. classrefdef:
  1794. begin
  1795. if token=_ID then
  1796. begin
  1797. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1798. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,true);
  1799. if assigned(srsym) then
  1800. begin
  1801. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1802. consume(_ID);
  1803. do_member_read(structh,getaddr,srsym,p1,again,[]);
  1804. end
  1805. else
  1806. begin
  1807. Message1(sym_e_id_no_member,orgpattern);
  1808. p1.destroy;
  1809. p1:=cerrornode.create;
  1810. { try to clean up }
  1811. consume(_ID);
  1812. end;
  1813. end
  1814. else { Error }
  1815. Consume(_ID);
  1816. end;
  1817. objectdef:
  1818. begin
  1819. if token=_ID then
  1820. begin
  1821. structh:=tobjectdef(p1.resultdef);
  1822. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,true);
  1823. if assigned(srsym) then
  1824. begin
  1825. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1826. consume(_ID);
  1827. do_member_read(structh,getaddr,srsym,p1,again,[]);
  1828. end
  1829. else
  1830. begin
  1831. Message1(sym_e_id_no_member,orgpattern);
  1832. p1.destroy;
  1833. p1:=cerrornode.create;
  1834. { try to clean up }
  1835. consume(_ID);
  1836. end;
  1837. end
  1838. else { Error }
  1839. Consume(_ID);
  1840. end;
  1841. pointerdef:
  1842. begin
  1843. if (p1.resultdef=objc_idtype) then
  1844. begin
  1845. { objc's id type can be used to call any
  1846. Objective-C method of any Objective-C class
  1847. type that's currently in scope }
  1848. if search_objc_method(pattern,srsym,srsymtable) then
  1849. begin
  1850. consume(_ID);
  1851. do_proc_call(srsym,srsymtable,nil,
  1852. (getaddr and not(token in [_CARET,_POINT])),
  1853. again,p1,[cnf_objc_id_call]);
  1854. { we need to know which procedure is called }
  1855. do_typecheckpass(p1);
  1856. end
  1857. else
  1858. begin
  1859. consume(_ID);
  1860. Message(parser_e_methode_id_expected);
  1861. end;
  1862. end
  1863. else
  1864. begin
  1865. Message(parser_e_invalid_qualifier);
  1866. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  1867. Message(parser_h_maybe_deref_caret_missing);
  1868. end
  1869. end;
  1870. else
  1871. begin
  1872. if p1.resultdef.typ<>undefineddef then
  1873. Message(parser_e_invalid_qualifier);
  1874. p1.destroy;
  1875. p1:=cerrornode.create;
  1876. { Error }
  1877. consume(_ID);
  1878. end;
  1879. end;
  1880. end;
  1881. else
  1882. begin
  1883. { is this a procedure variable ? }
  1884. if assigned(p1.resultdef) and
  1885. (p1.resultdef.typ=procvardef) then
  1886. begin
  1887. { Typenode for typecasting or expecting a procvar }
  1888. if (p1.nodetype=typen) or
  1889. (
  1890. assigned(getprocvardef) and
  1891. equal_defs(p1.resultdef,getprocvardef)
  1892. ) then
  1893. begin
  1894. if try_to_consume(_LKLAMMER) then
  1895. begin
  1896. p1:=comp_expr(true,false);
  1897. consume(_RKLAMMER);
  1898. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  1899. end
  1900. else
  1901. again:=false
  1902. end
  1903. else
  1904. begin
  1905. if try_to_consume(_LKLAMMER) then
  1906. begin
  1907. p2:=parse_paras(false,false,_RKLAMMER);
  1908. consume(_RKLAMMER);
  1909. p1:=ccallnode.create_procvar(p2,p1);
  1910. { proc():= is never possible }
  1911. if token=_ASSIGNMENT then
  1912. begin
  1913. Message(parser_e_illegal_expression);
  1914. p1.free;
  1915. p1:=cerrornode.create;
  1916. again:=false;
  1917. end;
  1918. end
  1919. else
  1920. again:=false;
  1921. end;
  1922. end
  1923. else
  1924. again:=false;
  1925. end;
  1926. end;
  1927. { we only try again if p1 was changed }
  1928. if again or
  1929. (p1.nodetype=errorn) then
  1930. result:=true;
  1931. end; { while again }
  1932. end;
  1933. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1934. out memberparentdef: tdef): boolean;
  1935. var
  1936. hdef : tdef;
  1937. begin
  1938. result:=true;
  1939. memberparentdef:=nil;
  1940. case st.symtabletype of
  1941. ObjectSymtable,
  1942. recordsymtable:
  1943. begin
  1944. memberparentdef:=tdef(st.defowner);
  1945. exit;
  1946. end;
  1947. WithSymtable:
  1948. begin
  1949. if assigned(p1) then
  1950. internalerror(2007012002);
  1951. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1952. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1953. if not(hdef.typ in [objectdef,classrefdef]) then
  1954. exit;
  1955. if (hdef.typ=classrefdef) then
  1956. hdef:=tclassrefdef(hdef).pointeddef;
  1957. memberparentdef:=hdef;
  1958. end;
  1959. else
  1960. result:=false;
  1961. end;
  1962. end;
  1963. {$maxfpuregisters 0}
  1964. function factor(getaddr,typeonly:boolean) : tnode;
  1965. {---------------------------------------------
  1966. Factor_read_id
  1967. ---------------------------------------------}
  1968. procedure factor_read_id(out p1:tnode;var again:boolean);
  1969. var
  1970. srsym : tsym;
  1971. srsymtable : TSymtable;
  1972. hdef : tdef;
  1973. orgstoredpattern,
  1974. storedpattern : string;
  1975. callflags: tcallnodeflags;
  1976. t : ttoken;
  1977. unit_found : boolean;
  1978. begin
  1979. { allow post fix operators }
  1980. again:=true;
  1981. { first check for identifier }
  1982. if token<>_ID then
  1983. begin
  1984. srsym:=generrorsym;
  1985. srsymtable:=nil;
  1986. consume(_ID);
  1987. end
  1988. else
  1989. begin
  1990. if typeonly then
  1991. searchsym_type(pattern,srsym,srsymtable)
  1992. else
  1993. searchsym(pattern,srsym,srsymtable);
  1994. { handle unit specification like System.Writeln }
  1995. unit_found:=try_consume_unitsym(srsym,srsymtable,t,true);
  1996. storedpattern:=pattern;
  1997. orgstoredpattern:=orgpattern;
  1998. consume(t);
  1999. { named parameter support }
  2000. found_arg_name:=false;
  2001. if not(unit_found) and
  2002. named_args_allowed and
  2003. (token=_ASSIGNMENT) then
  2004. begin
  2005. found_arg_name:=true;
  2006. p1:=cstringconstnode.createstr(storedpattern);
  2007. consume(_ASSIGNMENT);
  2008. exit;
  2009. end;
  2010. { check hints, but only if it isn't a potential generic symbol;
  2011. that is checked in sub_expr if it isn't a generic }
  2012. if assigned(srsym) and
  2013. not (
  2014. (srsym.typ=typesym) and
  2015. (ttypesym(srsym).typedef.typ in [recorddef,objectdef,arraydef,procvardef,undefineddef]) and
  2016. not (sp_generic_para in srsym.symoptions) and
  2017. (token in [_LT, _LSHARPBRACKET])
  2018. ) then
  2019. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2020. { if nothing found give error and return errorsym }
  2021. if not assigned(srsym) or
  2022. { is this a generic dummy symbol? }
  2023. ((srsym.typ=typesym) and
  2024. assigned(ttypesym(srsym).typedef) and
  2025. (ttypesym(srsym).typedef.typ=undefineddef) and
  2026. not (sp_generic_para in srsym.symoptions) and
  2027. not (token in [_LT, _LSHARPBRACKET]) and
  2028. not (
  2029. { in non-Delphi modes the generic class' name without a
  2030. "specialization" or "<T>" may be used to identify the
  2031. current class }
  2032. (sp_generic_dummy in srsym.symoptions) and
  2033. assigned(current_structdef) and
  2034. (df_generic in current_structdef.defoptions) and
  2035. not (m_delphi in current_settings.modeswitches) and
  2036. (upper(srsym.realname)=copy(current_structdef.objname^,1,pos('$',current_structdef.objname^)-1))
  2037. )) then
  2038. begin
  2039. identifier_not_found(orgstoredpattern);
  2040. srsym:=generrorsym;
  2041. srsymtable:=nil;
  2042. end;
  2043. end;
  2044. { Access to funcret or need to call the function? }
  2045. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  2046. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  2047. { result(x) is not allowed }
  2048. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  2049. (
  2050. (token=_LKLAMMER) or
  2051. (
  2052. (
  2053. (m_tp7 in current_settings.modeswitches) or
  2054. (m_delphi in current_settings.modeswitches)
  2055. ) and
  2056. (afterassignment or in_args)
  2057. )
  2058. ) then
  2059. begin
  2060. hdef:=tdef(srsym.owner.defowner);
  2061. if assigned(hdef) and
  2062. (hdef.typ=procdef) then
  2063. srsym:=tprocdef(hdef).procsym
  2064. else
  2065. begin
  2066. Message(parser_e_illegal_expression);
  2067. srsym:=generrorsym;
  2068. end;
  2069. srsymtable:=srsym.owner;
  2070. end;
  2071. begin
  2072. case srsym.typ of
  2073. absolutevarsym :
  2074. begin
  2075. if (tabsolutevarsym(srsym).abstyp=tovar) then
  2076. begin
  2077. p1:=nil;
  2078. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  2079. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  2080. include(p1.flags,nf_absolute);
  2081. end
  2082. else
  2083. p1:=cloadnode.create(srsym,srsymtable);
  2084. end;
  2085. staticvarsym,
  2086. localvarsym,
  2087. paravarsym,
  2088. fieldvarsym :
  2089. begin
  2090. { check if we are reading a field of an object/class/ }
  2091. { record. is_member_read() will deal with withsymtables }
  2092. { if needed. }
  2093. p1:=nil;
  2094. if is_member_read(srsym,srsymtable,p1,hdef) then
  2095. begin
  2096. { if the field was originally found in an }
  2097. { objectsymtable, it means it's part of self }
  2098. { if only method from which it was called is }
  2099. { not class static }
  2100. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2101. { if we are accessing a owner procsym from the nested }
  2102. { class we need to call it as a class member }
  2103. if assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef) then
  2104. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2105. else
  2106. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  2107. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  2108. else
  2109. p1:=load_self_node;
  2110. { now, if the field itself is part of an objectsymtab }
  2111. { (it can be even if it was found in a withsymtable, }
  2112. { e.g., "with classinstance do field := 5"), then }
  2113. { let do_member_read handle it }
  2114. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2115. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  2116. else
  2117. { otherwise it's a regular record subscript }
  2118. p1:=csubscriptnode.create(srsym,p1);
  2119. end
  2120. else
  2121. { regular non-field load }
  2122. p1:=cloadnode.create(srsym,srsymtable);
  2123. end;
  2124. syssym :
  2125. begin
  2126. p1:=statement_syssym(tsyssym(srsym).number);
  2127. end;
  2128. typesym :
  2129. begin
  2130. hdef:=ttypesym(srsym).typedef;
  2131. if not assigned(hdef) then
  2132. begin
  2133. again:=false;
  2134. end
  2135. else
  2136. begin
  2137. { We need to know if this unit uses Variants }
  2138. if (hdef=cvarianttype) and
  2139. not(cs_compilesystem in current_settings.moduleswitches) then
  2140. current_module.flags:=current_module.flags or uf_uses_variants;
  2141. p1:=handle_factor_typenode(hdef,getaddr,again,srsym,typeonly);
  2142. end;
  2143. end;
  2144. enumsym :
  2145. begin
  2146. p1:=genenumnode(tenumsym(srsym));
  2147. end;
  2148. constsym :
  2149. begin
  2150. if tconstsym(srsym).consttyp=constresourcestring then
  2151. begin
  2152. p1:=cloadnode.create(srsym,srsymtable);
  2153. do_typecheckpass(p1);
  2154. p1.resultdef:=getansistringdef;
  2155. end
  2156. else
  2157. p1:=genconstsymtree(tconstsym(srsym));
  2158. end;
  2159. procsym :
  2160. begin
  2161. p1:=nil;
  2162. { check if it's a method/class method }
  2163. if is_member_read(srsym,srsymtable,p1,hdef) then
  2164. begin
  2165. { if we are accessing a owner procsym from the nested }
  2166. { class we need to call it as a class member }
  2167. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) and
  2168. assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef) then
  2169. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef));
  2170. { not srsymtable.symtabletype since that can be }
  2171. { withsymtable as well }
  2172. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2173. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  2174. else
  2175. { no procsyms in records (yet) }
  2176. internalerror(2007012006);
  2177. end
  2178. else
  2179. begin
  2180. { regular procedure/function call }
  2181. if not unit_found then
  2182. callflags:=[]
  2183. else
  2184. callflags:=[cnf_unit_specified];
  2185. do_proc_call(srsym,srsymtable,nil,
  2186. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  2187. again,p1,callflags);
  2188. end;
  2189. end;
  2190. propertysym :
  2191. begin
  2192. p1:=nil;
  2193. { property of a class/object? }
  2194. if is_member_read(srsym,srsymtable,p1,hdef) then
  2195. begin
  2196. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2197. { if we are accessing a owner procsym from the nested }
  2198. { class we need to call it as a class member }
  2199. if assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef) then
  2200. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2201. else
  2202. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  2203. { no self node in static class methods }
  2204. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2205. else
  2206. p1:=load_self_node;
  2207. { not srsymtable.symtabletype since that can be }
  2208. { withsymtable as well }
  2209. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2210. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  2211. else
  2212. { no propertysyms in records (yet) }
  2213. internalerror(2009111510);
  2214. end
  2215. else
  2216. { no method pointer }
  2217. begin
  2218. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  2219. end;
  2220. end;
  2221. labelsym :
  2222. begin
  2223. { Support @label }
  2224. if getaddr then
  2225. begin
  2226. if srsym.owner<>current_procinfo.procdef.localst then
  2227. CGMessage(parser_e_label_outside_proc);
  2228. p1:=cloadnode.create(srsym,srsym.owner)
  2229. end
  2230. else
  2231. begin
  2232. consume(_COLON);
  2233. if tlabelsym(srsym).defined then
  2234. Message(sym_e_label_already_defined);
  2235. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  2236. begin
  2237. tlabelsym(srsym).nonlocal:=true;
  2238. exclude(current_procinfo.procdef.procoptions,po_inline);
  2239. end;
  2240. if tlabelsym(srsym).nonlocal and
  2241. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  2242. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  2243. tlabelsym(srsym).defined:=true;
  2244. p1:=clabelnode.create(nil,tlabelsym(srsym));
  2245. tlabelsym(srsym).code:=p1;
  2246. end;
  2247. end;
  2248. errorsym :
  2249. begin
  2250. p1:=cerrornode.create;
  2251. if try_to_consume(_LKLAMMER) then
  2252. begin
  2253. parse_paras(false,false,_RKLAMMER);
  2254. consume(_RKLAMMER);
  2255. end;
  2256. end;
  2257. else
  2258. begin
  2259. p1:=cerrornode.create;
  2260. Message(parser_e_illegal_expression);
  2261. end;
  2262. end; { end case }
  2263. end;
  2264. end;
  2265. {---------------------------------------------
  2266. Factor_Read_Set
  2267. ---------------------------------------------}
  2268. { Read a set between [] }
  2269. function factor_read_set:tnode;
  2270. var
  2271. p1,p2 : tnode;
  2272. lastp,
  2273. buildp : tarrayconstructornode;
  2274. old_allow_array_constructor : boolean;
  2275. begin
  2276. buildp:=nil;
  2277. { be sure that a least one arrayconstructn is used, also for an
  2278. empty [] }
  2279. if token=_RECKKLAMMER then
  2280. buildp:=carrayconstructornode.create(nil,buildp)
  2281. else
  2282. repeat
  2283. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  2284. old_allow_array_constructor:=allow_array_constructor;
  2285. allow_array_constructor:=false;
  2286. p1:=comp_expr(true,false);
  2287. if try_to_consume(_POINTPOINT) then
  2288. begin
  2289. p2:=comp_expr(true,false);
  2290. p1:=carrayconstructorrangenode.create(p1,p2);
  2291. end;
  2292. { insert at the end of the tree, to get the correct order }
  2293. if not assigned(buildp) then
  2294. begin
  2295. buildp:=carrayconstructornode.create(p1,nil);
  2296. lastp:=buildp;
  2297. end
  2298. else
  2299. begin
  2300. lastp.right:=carrayconstructornode.create(p1,nil);
  2301. lastp:=tarrayconstructornode(lastp.right);
  2302. end;
  2303. allow_array_constructor:=old_allow_array_constructor;
  2304. { there could be more elements }
  2305. until not try_to_consume(_COMMA);
  2306. factor_read_set:=buildp;
  2307. end;
  2308. {---------------------------------------------
  2309. Factor (Main)
  2310. ---------------------------------------------}
  2311. var
  2312. l : longint;
  2313. ic : int64;
  2314. qc : qword;
  2315. p1 : tnode;
  2316. code : integer;
  2317. srsym : tsym;
  2318. srsymtable : TSymtable;
  2319. pd : tprocdef;
  2320. hclassdef : tobjectdef;
  2321. d : bestreal;
  2322. cur : currency;
  2323. hs,hsorg : string;
  2324. hdef : tdef;
  2325. filepos : tfileposinfo;
  2326. callflags : tcallnodeflags;
  2327. again,
  2328. updatefpos,
  2329. nodechanged : boolean;
  2330. begin
  2331. { can't keep a copy of p1 and compare pointers afterwards, because
  2332. p1 may be freed and reallocated in the same place! }
  2333. updatefpos:=false;
  2334. p1:=nil;
  2335. filepos:=current_tokenpos;
  2336. again:=false;
  2337. if token=_ID then
  2338. begin
  2339. again:=true;
  2340. { Handle references to self }
  2341. if (idtoken=_SELF) and
  2342. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2343. assigned(current_structdef) then
  2344. begin
  2345. p1:=load_self_node;
  2346. consume(_ID);
  2347. again:=true;
  2348. end
  2349. else
  2350. factor_read_id(p1,again);
  2351. if assigned(p1) then
  2352. begin
  2353. { factor_read_id will set the filepos to after the id,
  2354. and in case of _SELF the filepos will already be the
  2355. same as filepos (so setting it again doesn't hurt). }
  2356. p1.fileinfo:=filepos;
  2357. filepos:=current_tokenpos;
  2358. end;
  2359. { handle post fix operators }
  2360. updatefpos:=postfixoperators(p1,again,getaddr);
  2361. end
  2362. else
  2363. begin
  2364. updatefpos:=true;
  2365. case token of
  2366. _RETURN :
  2367. begin
  2368. consume(_RETURN);
  2369. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2370. p1 := cexitnode.create(comp_expr(true,false))
  2371. else
  2372. p1 := cexitnode.create(nil);
  2373. end;
  2374. _INHERITED :
  2375. begin
  2376. again:=true;
  2377. consume(_INHERITED);
  2378. if assigned(current_procinfo) and
  2379. assigned(current_structdef) and
  2380. (current_structdef.typ=objectdef) then
  2381. begin
  2382. { for record helpers in mode Delphi "inherited" is not
  2383. allowed }
  2384. if is_objectpascal_helper(current_structdef) and
  2385. (m_delphi in current_settings.modeswitches) and
  2386. is_record(tobjectdef(current_structdef).extendeddef) then
  2387. Message(parser_e_inherited_not_in_record);
  2388. hclassdef:=tobjectdef(current_structdef).childof;
  2389. { Objective-C categories *replace* methods in the class
  2390. they extend, or add methods to it. So calling an
  2391. inherited method always calls the method inherited from
  2392. the parent of the extended class }
  2393. if is_objccategory(current_structdef) then
  2394. hclassdef:=hclassdef.childof;
  2395. { if inherited; only then we need the method with
  2396. the same name }
  2397. if token <> _ID then
  2398. begin
  2399. hs:=current_procinfo.procdef.procsym.name;
  2400. hsorg:=current_procinfo.procdef.procsym.realname;
  2401. anon_inherited:=true;
  2402. { For message methods we need to search using the message
  2403. number or string }
  2404. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2405. srdef:=nil;
  2406. if (po_msgint in pd.procoptions) then
  2407. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2408. else
  2409. if (po_msgstr in pd.procoptions) then
  2410. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2411. else
  2412. { helpers have their own ways of dealing with inherited }
  2413. if is_objectpascal_helper(current_structdef) then
  2414. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2415. else
  2416. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable,true);
  2417. end
  2418. else
  2419. begin
  2420. hs:=pattern;
  2421. hsorg:=orgpattern;
  2422. consume(_ID);
  2423. anon_inherited:=false;
  2424. { helpers have their own ways of dealing with inherited }
  2425. if is_objectpascal_helper(current_structdef) then
  2426. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2427. else
  2428. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable,true);
  2429. end;
  2430. if assigned(srsym) then
  2431. begin
  2432. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2433. { load the procdef from the inherited class and
  2434. not from self }
  2435. case srsym.typ of
  2436. procsym:
  2437. begin
  2438. if is_objectpascal_helper(current_structdef) then
  2439. begin
  2440. { for a helper load the procdef either from the
  2441. extended type, from the parent helper or from
  2442. the extended type of the parent helper
  2443. depending on the def the found symbol belongs
  2444. to }
  2445. if (srsym.Owner.defowner.typ=objectdef) and
  2446. is_objectpascal_helper(tobjectdef(srsym.Owner.defowner)) then
  2447. if current_structdef.is_related(tdef(srsym.Owner.defowner)) and
  2448. assigned(tobjectdef(current_structdef).childof) then
  2449. hdef:=tobjectdef(current_structdef).childof
  2450. else
  2451. hdef:=tobjectdef(srsym.Owner.defowner).extendeddef
  2452. else
  2453. hdef:=tdef(srsym.Owner.defowner);
  2454. end
  2455. else
  2456. hdef:=hclassdef;
  2457. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2458. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2459. hdef:=tclassrefdef.create(hdef);
  2460. p1:=ctypenode.create(hdef);
  2461. { we need to allow helpers here }
  2462. ttypenode(p1).helperallowed:=true;
  2463. end;
  2464. propertysym:
  2465. ;
  2466. else
  2467. begin
  2468. Message(parser_e_methode_id_expected);
  2469. p1:=cerrornode.create;
  2470. end;
  2471. end;
  2472. callflags:=[cnf_inherited];
  2473. if anon_inherited then
  2474. include(callflags,cnf_anon_inherited);
  2475. do_member_read(hclassdef,getaddr,srsym,p1,again,callflags);
  2476. end
  2477. else
  2478. begin
  2479. if anon_inherited then
  2480. begin
  2481. { For message methods we need to call DefaultHandler }
  2482. if (po_msgint in pd.procoptions) or
  2483. (po_msgstr in pd.procoptions) then
  2484. begin
  2485. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable,true);
  2486. if not assigned(srsym) or
  2487. (srsym.typ<>procsym) then
  2488. internalerror(200303171);
  2489. p1:=nil;
  2490. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2491. end
  2492. else
  2493. begin
  2494. { we need to ignore the inherited; }
  2495. p1:=cnothingnode.create;
  2496. end;
  2497. end
  2498. else
  2499. begin
  2500. Message1(sym_e_id_no_member,hsorg);
  2501. p1:=cerrornode.create;
  2502. end;
  2503. again:=false;
  2504. end;
  2505. { turn auto inheriting off }
  2506. anon_inherited:=false;
  2507. end
  2508. else
  2509. begin
  2510. { in case of records we use a more clear error message }
  2511. if assigned(current_structdef) and
  2512. (current_structdef.typ=recorddef) then
  2513. Message(parser_e_inherited_not_in_record)
  2514. else
  2515. Message(parser_e_generic_methods_only_in_methods);
  2516. again:=false;
  2517. p1:=cerrornode.create;
  2518. end;
  2519. postfixoperators(p1,again,getaddr);
  2520. end;
  2521. _INTCONST :
  2522. begin
  2523. {Try first wether the value fits in an int64.}
  2524. val(pattern,ic,code);
  2525. if code=0 then
  2526. begin
  2527. consume(_INTCONST);
  2528. int_to_type(ic,hdef);
  2529. p1:=cordconstnode.create(ic,hdef,true);
  2530. end
  2531. else
  2532. begin
  2533. { try qword next }
  2534. val(pattern,qc,code);
  2535. if code=0 then
  2536. begin
  2537. consume(_INTCONST);
  2538. int_to_type(qc,hdef);
  2539. p1:=cordconstnode.create(qc,hdef,true);
  2540. end;
  2541. end;
  2542. if code<>0 then
  2543. begin
  2544. { finally float }
  2545. val(pattern,d,code);
  2546. if code<>0 then
  2547. begin
  2548. Message(parser_e_invalid_integer);
  2549. consume(_INTCONST);
  2550. l:=1;
  2551. p1:=cordconstnode.create(l,sinttype,true);
  2552. end
  2553. else
  2554. begin
  2555. consume(_INTCONST);
  2556. p1:=crealconstnode.create(d,pbestrealtype^);
  2557. end;
  2558. end
  2559. else
  2560. { the necessary range checking has already been done by val }
  2561. tordconstnode(p1).rangecheck:=false;
  2562. end;
  2563. _REALNUMBER :
  2564. begin
  2565. val(pattern,d,code);
  2566. if code<>0 then
  2567. begin
  2568. Message(parser_e_error_in_real);
  2569. d:=1.0;
  2570. end;
  2571. consume(_REALNUMBER);
  2572. {$ifdef FPC_REAL2REAL_FIXED}
  2573. if current_settings.fputype=fpu_none then
  2574. Message(parser_e_unsupported_real);
  2575. if (current_settings.minfpconstprec=s32real) and
  2576. (d = single(d)) then
  2577. p1:=crealconstnode.create(d,s32floattype)
  2578. else if (current_settings.minfpconstprec=s64real) and
  2579. (d = double(d)) then
  2580. p1:=crealconstnode.create(d,s64floattype)
  2581. else
  2582. {$endif FPC_REAL2REAL_FIXED}
  2583. p1:=crealconstnode.create(d,pbestrealtype^);
  2584. {$ifdef FPC_HAS_STR_CURRENCY}
  2585. val(pattern,cur,code);
  2586. if code=0 then
  2587. trealconstnode(p1).value_currency:=cur;
  2588. {$endif FPC_HAS_STR_CURRENCY}
  2589. end;
  2590. _STRING :
  2591. begin
  2592. string_dec(hdef,true);
  2593. { STRING can be also a type cast }
  2594. if try_to_consume(_LKLAMMER) then
  2595. begin
  2596. p1:=comp_expr(true,false);
  2597. consume(_RKLAMMER);
  2598. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2599. { handle postfix operators here e.g. string(a)[10] }
  2600. again:=true;
  2601. postfixoperators(p1,again,getaddr);
  2602. end
  2603. else
  2604. p1:=ctypenode.create(hdef);
  2605. end;
  2606. _FILE :
  2607. begin
  2608. hdef:=cfiletype;
  2609. consume(_FILE);
  2610. { FILE can be also a type cast }
  2611. if try_to_consume(_LKLAMMER) then
  2612. begin
  2613. p1:=comp_expr(true,false);
  2614. consume(_RKLAMMER);
  2615. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2616. { handle postfix operators here e.g. string(a)[10] }
  2617. again:=true;
  2618. postfixoperators(p1,again,getaddr);
  2619. end
  2620. else
  2621. begin
  2622. p1:=ctypenode.create(hdef);
  2623. end;
  2624. end;
  2625. _CSTRING :
  2626. begin
  2627. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2628. consume(_CSTRING);
  2629. end;
  2630. _CCHAR :
  2631. begin
  2632. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2633. consume(_CCHAR);
  2634. end;
  2635. _CWSTRING:
  2636. begin
  2637. p1:=cstringconstnode.createwstr(patternw);
  2638. consume(_CWSTRING);
  2639. end;
  2640. _CWCHAR:
  2641. begin
  2642. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2643. consume(_CWCHAR);
  2644. end;
  2645. _KLAMMERAFFE :
  2646. begin
  2647. consume(_KLAMMERAFFE);
  2648. got_addrn:=true;
  2649. { support both @<x> and @(<x>) }
  2650. if try_to_consume(_LKLAMMER) then
  2651. begin
  2652. p1:=factor(true,false);
  2653. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2654. begin
  2655. again:=true;
  2656. postfixoperators(p1,again,getaddr);
  2657. end
  2658. else
  2659. consume(_RKLAMMER);
  2660. end
  2661. else
  2662. p1:=factor(true,false);
  2663. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2664. begin
  2665. again:=true;
  2666. postfixoperators(p1,again,getaddr);
  2667. end;
  2668. got_addrn:=false;
  2669. p1:=caddrnode.create(p1);
  2670. p1.fileinfo:=filepos;
  2671. if cs_typed_addresses in current_settings.localswitches then
  2672. include(p1.flags,nf_typedaddr);
  2673. { Store the procvar that we are expecting, the
  2674. addrn will use the information to find the correct
  2675. procdef or it will return an error }
  2676. if assigned(getprocvardef) and
  2677. (taddrnode(p1).left.nodetype = loadn) then
  2678. taddrnode(p1).getprocvardef:=getprocvardef;
  2679. end;
  2680. _LKLAMMER :
  2681. begin
  2682. consume(_LKLAMMER);
  2683. p1:=comp_expr(true,false);
  2684. consume(_RKLAMMER);
  2685. { it's not a good solution }
  2686. { but (a+b)^ makes some problems }
  2687. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2688. begin
  2689. again:=true;
  2690. postfixoperators(p1,again,getaddr);
  2691. end;
  2692. end;
  2693. _LECKKLAMMER :
  2694. begin
  2695. consume(_LECKKLAMMER);
  2696. p1:=factor_read_set;
  2697. consume(_RECKKLAMMER);
  2698. end;
  2699. _PLUS :
  2700. begin
  2701. consume(_PLUS);
  2702. p1:=factor(false,false);
  2703. p1:=cunaryplusnode.create(p1);
  2704. end;
  2705. _MINUS :
  2706. begin
  2707. consume(_MINUS);
  2708. if (token = _INTCONST) and not(m_isolike_unary_minus in current_settings.modeswitches) then
  2709. begin
  2710. { ugly hack, but necessary to be able to parse }
  2711. { -9223372036854775808 as int64 (JM) }
  2712. pattern := '-'+pattern;
  2713. p1:=sub_expr(oppower,false,false,nil);
  2714. { -1 ** 4 should be - (1 ** 4) and not
  2715. (-1) ** 4
  2716. This was the reason of tw0869.pp test failure PM }
  2717. if p1.nodetype=starstarn then
  2718. begin
  2719. if tbinarynode(p1).left.nodetype=ordconstn then
  2720. begin
  2721. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2722. p1:=cunaryminusnode.create(p1);
  2723. end
  2724. else if tbinarynode(p1).left.nodetype=realconstn then
  2725. begin
  2726. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2727. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2728. p1:=cunaryminusnode.create(p1);
  2729. end
  2730. else
  2731. internalerror(20021029);
  2732. end;
  2733. end
  2734. else
  2735. begin
  2736. if m_isolike_unary_minus in current_settings.modeswitches then
  2737. p1:=sub_expr(opmultiply,false,false,nil)
  2738. else
  2739. p1:=sub_expr(oppower,false,false,nil);
  2740. p1:=cunaryminusnode.create(p1);
  2741. end;
  2742. end;
  2743. _OP_NOT :
  2744. begin
  2745. consume(_OP_NOT);
  2746. p1:=factor(false,false);
  2747. p1:=cnotnode.create(p1);
  2748. end;
  2749. _TRUE :
  2750. begin
  2751. consume(_TRUE);
  2752. p1:=cordconstnode.create(1,pasbool8type,false);
  2753. end;
  2754. _FALSE :
  2755. begin
  2756. consume(_FALSE);
  2757. p1:=cordconstnode.create(0,pasbool8type,false);
  2758. end;
  2759. _NIL :
  2760. begin
  2761. consume(_NIL);
  2762. p1:=cnilnode.create;
  2763. { It's really ugly code nil^, but delphi allows it }
  2764. if token in [_CARET] then
  2765. begin
  2766. again:=true;
  2767. postfixoperators(p1,again,getaddr);
  2768. end;
  2769. end;
  2770. _OBJCPROTOCOL:
  2771. begin
  2772. { The @protocol keyword is used in two ways in Objective-C:
  2773. 1) to declare protocols (~ Object Pascal interfaces)
  2774. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2775. of a declared protocol
  2776. This code is for handling the second case. Because of 1),
  2777. we cannot simply use a system unit symbol.
  2778. }
  2779. consume(_OBJCPROTOCOL);
  2780. consume(_LKLAMMER);
  2781. p1:=factor(false,false);
  2782. consume(_RKLAMMER);
  2783. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2784. end;
  2785. else
  2786. begin
  2787. Message(parser_e_illegal_expression);
  2788. p1:=cerrornode.create;
  2789. { recover }
  2790. consume(token);
  2791. end;
  2792. end;
  2793. end;
  2794. { generate error node if no node is created }
  2795. if not assigned(p1) then
  2796. begin
  2797. {$ifdef EXTDEBUG}
  2798. Comment(V_Warning,'factor: p1=nil');
  2799. {$endif}
  2800. p1:=cerrornode.create;
  2801. updatefpos:=true;
  2802. end;
  2803. { get the resultdef for the node }
  2804. if (not assigned(p1.resultdef)) then
  2805. begin
  2806. do_typecheckpass_changed(p1,nodechanged);
  2807. updatefpos:=updatefpos or nodechanged;
  2808. end;
  2809. if assigned(p1) and
  2810. updatefpos then
  2811. p1.fileinfo:=filepos;
  2812. factor:=p1;
  2813. end;
  2814. {$maxfpuregisters default}
  2815. procedure post_comp_expr_gendef(var def: tdef);
  2816. var
  2817. p1 : tnode;
  2818. again : boolean;
  2819. begin
  2820. if not assigned(def) then
  2821. internalerror(2011053001);
  2822. again:=false;
  2823. { handle potential typecasts, etc }
  2824. p1:=handle_factor_typenode(def,false,again,nil,false);
  2825. { parse postfix operators }
  2826. postfixoperators(p1,again,false);
  2827. if assigned(p1) and (p1.nodetype=typen) then
  2828. def:=ttypenode(p1).typedef
  2829. else
  2830. def:=generrordef;
  2831. end;
  2832. {****************************************************************************
  2833. Sub_Expr
  2834. ****************************************************************************}
  2835. const
  2836. { Warning these stay be ordered !! }
  2837. operator_levels:array[Toperator_precedence] of set of NOTOKEN..last_operator=
  2838. ([_LT,_LTE,_GT,_GTE,_EQ,_NE,_OP_IN],
  2839. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2840. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2841. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2842. [_STARSTAR] );
  2843. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;
  2844. {Reads a subexpression while the operators are of the current precedence
  2845. level, or any higher level. Replaces the old term, simpl_expr and
  2846. simpl2_expr.}
  2847. function istypenode(n:tnode):boolean;inline;
  2848. { Checks whether the given node is a type node or a VMT node containing a
  2849. typenode. This is used in the code for inline specializations in the
  2850. _LT branch below }
  2851. begin
  2852. result:=assigned(n) and
  2853. (
  2854. (n.nodetype=typen) or
  2855. (
  2856. (n.nodetype=loadvmtaddrn) and
  2857. (tloadvmtaddrnode(n).left.nodetype=typen)
  2858. )
  2859. );
  2860. end;
  2861. function gettypedef(n:tnode):tdef;inline;
  2862. { This returns the typedef that belongs to the given typenode or
  2863. loadvmtaddrnode. n must not be Nil! }
  2864. begin
  2865. if n.nodetype=typen then
  2866. result:=ttypenode(n).typedef
  2867. else
  2868. result:=ttypenode(tloadvmtaddrnode(n).left).typedef;
  2869. end;
  2870. function getgenericsym(n:tnode;out srsym:tsym):boolean;
  2871. var
  2872. srsymtable : tsymtable;
  2873. begin
  2874. srsym:=nil;
  2875. case n.nodetype of
  2876. typen:
  2877. srsym:=ttypenode(n).typedef.typesym;
  2878. loadvmtaddrn:
  2879. srsym:=ttypenode(tloadvmtaddrnode(n).left).typedef.typesym;
  2880. loadn:
  2881. if not searchsym_with_symoption(tloadnode(n).symtableentry.Name,srsym,srsymtable,sp_generic_dummy) then
  2882. srsym:=nil;
  2883. { TODO : handle const nodes }
  2884. end;
  2885. result:=assigned(srsym);
  2886. end;
  2887. label
  2888. SubExprStart;
  2889. var
  2890. p1,p2 : tnode;
  2891. oldt : Ttoken;
  2892. filepos : tfileposinfo;
  2893. again : boolean;
  2894. gendef,parseddef : tdef;
  2895. gensym : tsym;
  2896. begin
  2897. SubExprStart:
  2898. if pred_level=highest_precedence then
  2899. begin
  2900. if factornode=nil then
  2901. p1:=factor(false,typeonly)
  2902. else
  2903. p1:=factornode;
  2904. end
  2905. else
  2906. p1:=sub_expr(succ(pred_level),true,typeonly,factornode);
  2907. repeat
  2908. if (token in [NOTOKEN..last_operator]) and
  2909. (token in operator_levels[pred_level]) and
  2910. ((token<>_EQ) or accept_equal) then
  2911. begin
  2912. oldt:=token;
  2913. filepos:=current_tokenpos;
  2914. consume(token);
  2915. if pred_level=highest_precedence then
  2916. p2:=factor(false,false)
  2917. else
  2918. p2:=sub_expr(succ(pred_level),true,typeonly,nil);
  2919. case oldt of
  2920. _PLUS :
  2921. p1:=caddnode.create(addn,p1,p2);
  2922. _MINUS :
  2923. p1:=caddnode.create(subn,p1,p2);
  2924. _STAR :
  2925. p1:=caddnode.create(muln,p1,p2);
  2926. _SLASH :
  2927. p1:=caddnode.create(slashn,p1,p2);
  2928. _EQ:
  2929. p1:=caddnode.create(equaln,p1,p2);
  2930. _GT :
  2931. p1:=caddnode.create(gtn,p1,p2);
  2932. _LT :
  2933. begin
  2934. { we need to decice whether we have an inline specialization
  2935. (type nodes to the left and right of "<", mode Delphi and
  2936. ">" or "," following) or a normal "<" comparison }
  2937. { TODO : p1 could be a non type if e.g. a variable with the
  2938. same name is defined in the same unit where the
  2939. generic is defined (though "same unit" is not
  2940. necessarily needed) }
  2941. if getgenericsym(p1,gensym) and
  2942. { Attention: when nested specializations are supported
  2943. p2 could be a loadn if a "<" follows }
  2944. istypenode(p2) and
  2945. (m_delphi in current_settings.modeswitches) and
  2946. { TODO : add _LT, _LSHARPBRACKET for nested specializations }
  2947. (token in [_GT,_RSHARPBRACKET,_COMMA]) then
  2948. begin
  2949. { this is an inline specialization }
  2950. { retrieve the defs of two nodes }
  2951. gendef:=nil;
  2952. parseddef:=gettypedef(p2);
  2953. if parseddef.typesym.typ<>typesym then
  2954. Internalerror(2011051001);
  2955. { check the hints for parseddef }
  2956. check_hints(parseddef.typesym,parseddef.typesym.symoptions,parseddef.typesym.deprecatedmsg);
  2957. { generate the specialization }
  2958. generate_specialization(gendef,false,'',parseddef,gensym.RealName);
  2959. { we don't need the old left and right nodes anymore }
  2960. p1.Free;
  2961. p2.Free;
  2962. { in case of a class or a record the specialized generic
  2963. is always a classrefdef }
  2964. again:=false;
  2965. { handle potential typecasts, etc }
  2966. p1:=handle_factor_typenode(gendef,false,again,nil,false);
  2967. { parse postfix operators }
  2968. if postfixoperators(p1,again,false) then
  2969. if assigned(p1) then
  2970. p1.fileinfo:=filepos
  2971. else
  2972. p1:=cerrornode.create;
  2973. { with p1 now set we are in reality directly behind the
  2974. call to "factor" thus we need to call down to that
  2975. again }
  2976. { This is disabled until specializations on the right
  2977. hand side work as well, because
  2978. "not working expressions" is better than "half working
  2979. expressions" }
  2980. {factornode:=p1;
  2981. goto SubExprStart;}
  2982. end
  2983. else
  2984. begin
  2985. { this is a normal "<" comparison }
  2986. { potential generic types that are followed by a "<" }
  2987. { a) are not checked whether they are an undefined def,
  2988. but not a generic parameter }
  2989. if (p1.nodetype=typen) and
  2990. (ttypenode(p1).typedef.typ=undefineddef) and
  2991. assigned(ttypenode(p1).typedef.typesym) and
  2992. not (sp_generic_para in ttypenode(p1).typedef.typesym.symoptions) then
  2993. begin
  2994. identifier_not_found(ttypenode(p1).typedef.typesym.RealName);
  2995. p1.Free;
  2996. p1:=cerrornode.create;
  2997. end;
  2998. { b) don't have their hints checked }
  2999. if istypenode(p1) then
  3000. begin
  3001. gendef:=gettypedef(p1);
  3002. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3003. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3004. end;
  3005. { Note: the second part of the expression will be needed
  3006. for nested specializations }
  3007. if istypenode(p2) {and
  3008. not (token in [_LT, _LSHARPBRACKET])} then
  3009. begin
  3010. gendef:=gettypedef(p2);
  3011. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3012. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3013. end;
  3014. { create the comparison node for "<" }
  3015. p1:=caddnode.create(ltn,p1,p2)
  3016. end;
  3017. end;
  3018. _GTE :
  3019. p1:=caddnode.create(gten,p1,p2);
  3020. _LTE :
  3021. p1:=caddnode.create(lten,p1,p2);
  3022. _SYMDIF :
  3023. p1:=caddnode.create(symdifn,p1,p2);
  3024. _STARSTAR :
  3025. p1:=caddnode.create(starstarn,p1,p2);
  3026. _OP_AS,
  3027. _OP_IS :
  3028. begin
  3029. if token in [_LT, _LSHARPBRACKET] then
  3030. begin
  3031. { for now we're handling this as a generic declaration;
  3032. there could be cases though (because of operator
  3033. overloading) where this is the wrong decision... }
  3034. { TODO : here the same note as in _LT applies as p2 could
  3035. point to a variable, etc }
  3036. gendef:=gettypedef(p2);
  3037. if gendef.typesym.typ<>typesym then
  3038. Internalerror(2011071401);
  3039. { generate the specialization }
  3040. generate_specialization(gendef,false,'',nil,'');
  3041. { we don't need the old p2 anymore }
  3042. p2.Free;
  3043. again:=false;
  3044. { handle potential typecasts, etc }
  3045. p2:=handle_factor_typenode(gendef,false,again,nil,false);
  3046. { parse postfix operators }
  3047. if postfixoperators(p2,again,false) then
  3048. if assigned(p2) then
  3049. p2.fileinfo:=filepos
  3050. else
  3051. p2:=cerrornode.create;
  3052. { here we don't need to call back down to "factor", thus
  3053. no "goto" }
  3054. end;
  3055. { now generate the "is" or "as" node }
  3056. case oldt of
  3057. _OP_AS:
  3058. p1:=casnode.create(p1,p2);
  3059. _OP_IS:
  3060. p1:=cisnode.create(p1,p2);
  3061. end;
  3062. end;
  3063. _OP_IN :
  3064. p1:=cinnode.create(p1,p2);
  3065. _OP_OR,
  3066. _PIPE {macpas only} :
  3067. begin
  3068. p1:=caddnode.create(orn,p1,p2);
  3069. if (oldt = _PIPE) then
  3070. include(p1.flags,nf_short_bool);
  3071. end;
  3072. _OP_AND,
  3073. _AMPERSAND {macpas only} :
  3074. begin
  3075. p1:=caddnode.create(andn,p1,p2);
  3076. if (oldt = _AMPERSAND) then
  3077. include(p1.flags,nf_short_bool);
  3078. end;
  3079. _OP_DIV :
  3080. p1:=cmoddivnode.create(divn,p1,p2);
  3081. _OP_NOT :
  3082. p1:=cnotnode.create(p1);
  3083. _OP_MOD :
  3084. begin
  3085. p1:=cmoddivnode.create(modn,p1,p2);
  3086. if m_iso in current_settings.modeswitches then
  3087. include(p1.flags,nf_isomod);
  3088. end;
  3089. _OP_SHL :
  3090. p1:=cshlshrnode.create(shln,p1,p2);
  3091. _OP_SHR :
  3092. p1:=cshlshrnode.create(shrn,p1,p2);
  3093. _OP_XOR :
  3094. p1:=caddnode.create(xorn,p1,p2);
  3095. _ASSIGNMENT :
  3096. p1:=cassignmentnode.create(p1,p2);
  3097. _NE :
  3098. p1:=caddnode.create(unequaln,p1,p2);
  3099. end;
  3100. p1.fileinfo:=filepos;
  3101. end
  3102. else
  3103. break;
  3104. until false;
  3105. sub_expr:=p1;
  3106. end;
  3107. function comp_expr(accept_equal,typeonly:boolean):tnode;
  3108. var
  3109. oldafterassignment : boolean;
  3110. p1 : tnode;
  3111. begin
  3112. oldafterassignment:=afterassignment;
  3113. afterassignment:=true;
  3114. p1:=sub_expr(opcompare,accept_equal,typeonly,nil);
  3115. { get the resultdef for this expression }
  3116. if not assigned(p1.resultdef) then
  3117. do_typecheckpass(p1);
  3118. afterassignment:=oldafterassignment;
  3119. comp_expr:=p1;
  3120. end;
  3121. function expr(dotypecheck : boolean) : tnode;
  3122. var
  3123. p1,p2 : tnode;
  3124. filepos : tfileposinfo;
  3125. oldafterassignment,
  3126. updatefpos : boolean;
  3127. begin
  3128. oldafterassignment:=afterassignment;
  3129. p1:=sub_expr(opcompare,true,false,nil);
  3130. { get the resultdef for this expression }
  3131. if not assigned(p1.resultdef) and
  3132. dotypecheck then
  3133. do_typecheckpass(p1);
  3134. filepos:=current_tokenpos;
  3135. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  3136. afterassignment:=true;
  3137. updatefpos:=true;
  3138. case token of
  3139. _POINTPOINT :
  3140. begin
  3141. consume(_POINTPOINT);
  3142. p2:=sub_expr(opcompare,true,false,nil);
  3143. p1:=crangenode.create(p1,p2);
  3144. end;
  3145. _ASSIGNMENT :
  3146. begin
  3147. consume(_ASSIGNMENT);
  3148. if assigned(p1.resultdef) and (p1.resultdef.typ=procvardef) then
  3149. getprocvardef:=tprocvardef(p1.resultdef);
  3150. p2:=sub_expr(opcompare,true,false,nil);
  3151. if assigned(getprocvardef) then
  3152. handle_procvar(getprocvardef,p2);
  3153. getprocvardef:=nil;
  3154. p1:=cassignmentnode.create(p1,p2);
  3155. end;
  3156. _PLUSASN :
  3157. begin
  3158. consume(_PLUSASN);
  3159. p2:=sub_expr(opcompare,true,false,nil);
  3160. p1:=gen_c_style_operator(addn,p1,p2);
  3161. end;
  3162. _MINUSASN :
  3163. begin
  3164. consume(_MINUSASN);
  3165. p2:=sub_expr(opcompare,true,false,nil);
  3166. p1:=gen_c_style_operator(subn,p1,p2);
  3167. end;
  3168. _STARASN :
  3169. begin
  3170. consume(_STARASN );
  3171. p2:=sub_expr(opcompare,true,false,nil);
  3172. p1:=gen_c_style_operator(muln,p1,p2);
  3173. end;
  3174. _SLASHASN :
  3175. begin
  3176. consume(_SLASHASN );
  3177. p2:=sub_expr(opcompare,true,false,nil);
  3178. p1:=gen_c_style_operator(slashn,p1,p2);
  3179. end;
  3180. else
  3181. updatefpos:=false;
  3182. end;
  3183. { get the resultdef for this expression }
  3184. if not assigned(p1.resultdef) and
  3185. dotypecheck then
  3186. do_typecheckpass(p1);
  3187. afterassignment:=oldafterassignment;
  3188. if updatefpos then
  3189. p1.fileinfo:=filepos;
  3190. expr:=p1;
  3191. end;
  3192. function get_intconst:TConstExprInt;
  3193. {Reads an expression, tries to evalute it and check if it is an integer
  3194. constant. Then the constant is returned.}
  3195. var
  3196. p:tnode;
  3197. begin
  3198. result:=0;
  3199. p:=comp_expr(true,false);
  3200. if not codegenerror then
  3201. begin
  3202. if (p.nodetype<>ordconstn) or
  3203. not(is_integer(p.resultdef)) then
  3204. Message(parser_e_illegal_expression)
  3205. else
  3206. result:=tordconstnode(p).value;
  3207. end;
  3208. p.free;
  3209. end;
  3210. function get_stringconst:string;
  3211. {Reads an expression, tries to evaluate it and checks if it is a string
  3212. constant. Then the constant is returned.}
  3213. var
  3214. p:tnode;
  3215. begin
  3216. get_stringconst:='';
  3217. p:=comp_expr(true,false);
  3218. if p.nodetype<>stringconstn then
  3219. begin
  3220. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  3221. get_stringconst:=char(int64(tordconstnode(p).value))
  3222. else
  3223. Message(parser_e_illegal_expression);
  3224. end
  3225. else
  3226. get_stringconst:=strpas(tstringconstnode(p).value_str);
  3227. p.free;
  3228. end;
  3229. end.