pexpr.pas 137 KB

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