pexpr.pas 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478
  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 getpropaccesslist(propsym,palt_write,propaccesslist) then
  1016. begin
  1017. sym:=propaccesslist.firstsym^.sym;
  1018. case sym.typ of
  1019. procsym :
  1020. begin
  1021. callflags:=[];
  1022. { generate the method call }
  1023. membercall:=maybe_load_methodpointer(st,p1);
  1024. if membercall then
  1025. include(callflags,cnf_member_call);
  1026. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1027. addsymref(sym);
  1028. paras:=nil;
  1029. consume(_ASSIGNMENT);
  1030. { read the expression }
  1031. if propsym.propdef.typ=procvardef then
  1032. getprocvardef:=tprocvardef(propsym.propdef);
  1033. p2:=comp_expr(true,false);
  1034. if assigned(getprocvardef) then
  1035. handle_procvar(getprocvardef,p2);
  1036. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1037. { mark as property, both the tcallnode and the real call block }
  1038. include(p1.flags,nf_isproperty);
  1039. getprocvardef:=nil;
  1040. end;
  1041. fieldvarsym :
  1042. begin
  1043. { generate access code }
  1044. if not handle_staticfield_access(sym,false,p1) then
  1045. propaccesslist_to_node(p1,st,propaccesslist);
  1046. include(p1.flags,nf_isproperty);
  1047. consume(_ASSIGNMENT);
  1048. { read the expression }
  1049. p2:=comp_expr(true,false);
  1050. p1:=cassignmentnode.create(p1,p2);
  1051. end
  1052. else
  1053. begin
  1054. p1:=cerrornode.create;
  1055. Message(parser_e_no_procedure_to_access_property);
  1056. end;
  1057. end;
  1058. end
  1059. else
  1060. begin
  1061. p1:=cerrornode.create;
  1062. Message(parser_e_no_procedure_to_access_property);
  1063. end;
  1064. end
  1065. else
  1066. begin
  1067. if getpropaccesslist(propsym,palt_read,propaccesslist) then
  1068. begin
  1069. sym := propaccesslist.firstsym^.sym;
  1070. case sym.typ of
  1071. fieldvarsym :
  1072. begin
  1073. { generate access code }
  1074. if not handle_staticfield_access(sym,false,p1) then
  1075. propaccesslist_to_node(p1,st,propaccesslist);
  1076. include(p1.flags,nf_isproperty);
  1077. { catch expressions like "(propx):=1;" }
  1078. include(p1.flags,nf_no_lvalue);
  1079. end;
  1080. procsym :
  1081. begin
  1082. callflags:=[];
  1083. { generate the method call }
  1084. membercall:=maybe_load_methodpointer(st,p1);
  1085. if membercall then
  1086. include(callflags,cnf_member_call);
  1087. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1088. paras:=nil;
  1089. include(p1.flags,nf_isproperty);
  1090. include(p1.flags,nf_no_lvalue);
  1091. end
  1092. else
  1093. begin
  1094. p1:=cerrornode.create;
  1095. Message(type_e_mismatch);
  1096. end;
  1097. end;
  1098. end
  1099. else
  1100. begin
  1101. { error, no function to read property }
  1102. p1:=cerrornode.create;
  1103. Message(parser_e_no_procedure_to_access_property);
  1104. end;
  1105. end;
  1106. { release paras if not used }
  1107. if assigned(paras) then
  1108. paras.free;
  1109. end;
  1110. { the ID token has to be consumed before calling this function }
  1111. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  1112. var
  1113. isclassref:boolean;
  1114. begin
  1115. if sym=nil then
  1116. begin
  1117. { pattern is still valid unless
  1118. there is another ID just after the ID of sym }
  1119. Message1(sym_e_id_no_member,orgpattern);
  1120. p1.free;
  1121. p1:=cerrornode.create;
  1122. { try to clean up }
  1123. again:=false;
  1124. end
  1125. else
  1126. begin
  1127. if assigned(p1) then
  1128. begin
  1129. if not assigned(p1.resultdef) then
  1130. do_typecheckpass(p1);
  1131. isclassref:=(p1.resultdef.typ=classrefdef);
  1132. end
  1133. else
  1134. isclassref:=false;
  1135. { we assume, that only procsyms and varsyms are in an object }
  1136. { symbol table, for classes, properties are allowed }
  1137. case sym.typ of
  1138. procsym:
  1139. begin
  1140. do_proc_call(sym,sym.owner,structh,
  1141. (getaddr and not(token in [_CARET,_POINT])),
  1142. again,p1,callflags);
  1143. { we need to know which procedure is called }
  1144. do_typecheckpass(p1);
  1145. { calling using classref? }
  1146. if isclassref and
  1147. (p1.nodetype=calln) and
  1148. assigned(tcallnode(p1).procdefinition) 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. var
  1995. srsym : tsym;
  1996. srsymtable : TSymtable;
  1997. hdef : tdef;
  1998. orgstoredpattern,
  1999. storedpattern : string;
  2000. callflags: tcallnodeflags;
  2001. t : ttoken;
  2002. unit_found : boolean;
  2003. begin
  2004. { allow post fix operators }
  2005. again:=true;
  2006. { first check for identifier }
  2007. if token<>_ID then
  2008. begin
  2009. srsym:=generrorsym;
  2010. srsymtable:=nil;
  2011. consume(_ID);
  2012. end
  2013. else
  2014. begin
  2015. if typeonly then
  2016. searchsym_type(pattern,srsym,srsymtable)
  2017. else
  2018. searchsym(pattern,srsym,srsymtable);
  2019. { handle unit specification like System.Writeln }
  2020. unit_found:=try_consume_unitsym(srsym,srsymtable,t,true);
  2021. storedpattern:=pattern;
  2022. orgstoredpattern:=orgpattern;
  2023. consume(t);
  2024. { named parameter support }
  2025. found_arg_name:=false;
  2026. if not(unit_found) and
  2027. named_args_allowed and
  2028. (token=_ASSIGNMENT) then
  2029. begin
  2030. found_arg_name:=true;
  2031. p1:=cstringconstnode.createstr(storedpattern);
  2032. consume(_ASSIGNMENT);
  2033. exit;
  2034. end;
  2035. { check hints, but only if it isn't a potential generic symbol;
  2036. that is checked in sub_expr if it isn't a generic }
  2037. if assigned(srsym) and
  2038. not (
  2039. (srsym.typ=typesym) and
  2040. (ttypesym(srsym).typedef.typ in [recorddef,objectdef,arraydef,procvardef,undefineddef]) and
  2041. not (sp_generic_para in srsym.symoptions) and
  2042. (token in [_LT, _LSHARPBRACKET])
  2043. ) then
  2044. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2045. { if nothing found give error and return errorsym }
  2046. if not assigned(srsym) or
  2047. { is this a generic dummy symbol? }
  2048. ((srsym.typ=typesym) and
  2049. assigned(ttypesym(srsym).typedef) and
  2050. (ttypesym(srsym).typedef.typ=undefineddef) and
  2051. not (sp_generic_para in srsym.symoptions) and
  2052. not (token in [_LT, _LSHARPBRACKET]) and
  2053. not (
  2054. { in non-Delphi modes the generic class' name without a
  2055. "specialization" or "<T>" may be used to identify the
  2056. current class }
  2057. (sp_generic_dummy in srsym.symoptions) and
  2058. assigned(current_structdef) and
  2059. (df_generic in current_structdef.defoptions) and
  2060. not (m_delphi in current_settings.modeswitches) and
  2061. assigned(get_generic_in_hierarchy_by_name(srsym,current_structdef))
  2062. )) and
  2063. { it could be a rename of a generic para }
  2064. { Note: if this generates false positives we'll need to
  2065. include a "basesym" to tsym to track the original
  2066. symbol }
  2067. not (sp_explicitrename in srsym.symoptions) then
  2068. begin
  2069. identifier_not_found(orgstoredpattern);
  2070. srsym:=generrorsym;
  2071. srsymtable:=nil;
  2072. end;
  2073. end;
  2074. { Access to funcret or need to call the function? }
  2075. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  2076. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  2077. { result(x) is not allowed }
  2078. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  2079. (
  2080. (token=_LKLAMMER) or
  2081. (
  2082. (([m_tp7,m_delphi,m_mac] * current_settings.modeswitches) <> []) and
  2083. (afterassignment or in_args)
  2084. )
  2085. ) then
  2086. begin
  2087. hdef:=tdef(srsym.owner.defowner);
  2088. if assigned(hdef) and
  2089. (hdef.typ=procdef) then
  2090. srsym:=tprocdef(hdef).procsym
  2091. else
  2092. begin
  2093. Message(parser_e_illegal_expression);
  2094. srsym:=generrorsym;
  2095. end;
  2096. srsymtable:=srsym.owner;
  2097. end;
  2098. begin
  2099. case srsym.typ of
  2100. absolutevarsym :
  2101. begin
  2102. if (tabsolutevarsym(srsym).abstyp=tovar) then
  2103. begin
  2104. p1:=nil;
  2105. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  2106. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  2107. include(p1.flags,nf_absolute);
  2108. end
  2109. else
  2110. p1:=cloadnode.create(srsym,srsymtable);
  2111. end;
  2112. staticvarsym,
  2113. localvarsym,
  2114. paravarsym,
  2115. fieldvarsym :
  2116. begin
  2117. { check if we are reading a field of an object/class/ }
  2118. { record. is_member_read() will deal with withsymtables }
  2119. { if needed. }
  2120. p1:=nil;
  2121. if is_member_read(srsym,srsymtable,p1,hdef) then
  2122. begin
  2123. { if the field was originally found in an }
  2124. { objectsymtable, it means it's part of self }
  2125. { if only method from which it was called is }
  2126. { not class static }
  2127. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2128. { if we are accessing a owner procsym from the nested }
  2129. { class we need to call it as a class member }
  2130. if assigned(current_structdef) and
  2131. (((current_structdef<>hdef) and is_owned_by(current_structdef,hdef)) or
  2132. (sp_static in srsym.symoptions)) then
  2133. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2134. else
  2135. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  2136. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  2137. else
  2138. p1:=load_self_node;
  2139. { now, if the field itself is part of an objectsymtab }
  2140. { (it can be even if it was found in a withsymtable, }
  2141. { e.g., "with classinstance do field := 5"), then }
  2142. { let do_member_read handle it }
  2143. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2144. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  2145. else
  2146. { otherwise it's a regular record subscript }
  2147. p1:=csubscriptnode.create(srsym,p1);
  2148. end
  2149. else
  2150. { regular non-field load }
  2151. p1:=cloadnode.create(srsym,srsymtable);
  2152. end;
  2153. syssym :
  2154. begin
  2155. p1:=statement_syssym(tsyssym(srsym).number);
  2156. end;
  2157. typesym :
  2158. begin
  2159. hdef:=ttypesym(srsym).typedef;
  2160. if not assigned(hdef) then
  2161. begin
  2162. again:=false;
  2163. end
  2164. else
  2165. begin
  2166. { We need to know if this unit uses Variants }
  2167. if ((hdef=cvarianttype) or (hdef=colevarianttype)) and
  2168. not(cs_compilesystem in current_settings.moduleswitches) then
  2169. current_module.flags:=current_module.flags or uf_uses_variants;
  2170. p1:=handle_factor_typenode(hdef,getaddr,again,srsym,typeonly);
  2171. end;
  2172. end;
  2173. enumsym :
  2174. begin
  2175. p1:=genenumnode(tenumsym(srsym));
  2176. end;
  2177. constsym :
  2178. begin
  2179. if tconstsym(srsym).consttyp=constresourcestring then
  2180. begin
  2181. p1:=cloadnode.create(srsym,srsymtable);
  2182. do_typecheckpass(p1);
  2183. p1.resultdef:=getansistringdef;
  2184. end
  2185. else
  2186. p1:=genconstsymtree(tconstsym(srsym));
  2187. end;
  2188. procsym :
  2189. begin
  2190. p1:=nil;
  2191. { check if it's a method/class method }
  2192. if is_member_read(srsym,srsymtable,p1,hdef) then
  2193. begin
  2194. { if we are accessing a owner procsym from the nested }
  2195. { class we need to call it as a class member }
  2196. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) and
  2197. assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef) then
  2198. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef));
  2199. { not srsymtable.symtabletype since that can be }
  2200. { withsymtable as well }
  2201. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2202. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  2203. else
  2204. { no procsyms in records (yet) }
  2205. internalerror(2007012006);
  2206. end
  2207. else
  2208. begin
  2209. { regular procedure/function call }
  2210. if not unit_found then
  2211. callflags:=[]
  2212. else
  2213. callflags:=[cnf_unit_specified];
  2214. do_proc_call(srsym,srsymtable,nil,
  2215. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  2216. again,p1,callflags);
  2217. end;
  2218. end;
  2219. propertysym :
  2220. begin
  2221. p1:=nil;
  2222. { property of a class/object? }
  2223. if is_member_read(srsym,srsymtable,p1,hdef) then
  2224. begin
  2225. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2226. { if we are accessing a owner procsym from the nested }
  2227. { class we need to call it as a class member }
  2228. if assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef) then
  2229. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2230. else
  2231. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  2232. { no self node in static class methods }
  2233. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2234. else
  2235. p1:=load_self_node;
  2236. { not srsymtable.symtabletype since that can be }
  2237. { withsymtable as well }
  2238. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2239. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  2240. else
  2241. { no propertysyms in records (yet) }
  2242. internalerror(2009111510);
  2243. end
  2244. else
  2245. { no method pointer }
  2246. begin
  2247. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  2248. end;
  2249. end;
  2250. labelsym :
  2251. begin
  2252. { Support @label }
  2253. if getaddr then
  2254. begin
  2255. if srsym.owner<>current_procinfo.procdef.localst then
  2256. CGMessage(parser_e_label_outside_proc);
  2257. p1:=cloadnode.create(srsym,srsym.owner)
  2258. end
  2259. else
  2260. begin
  2261. consume(_COLON);
  2262. if tlabelsym(srsym).defined then
  2263. Message(sym_e_label_already_defined);
  2264. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  2265. begin
  2266. tlabelsym(srsym).nonlocal:=true;
  2267. exclude(current_procinfo.procdef.procoptions,po_inline);
  2268. end;
  2269. if tlabelsym(srsym).nonlocal and
  2270. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  2271. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  2272. tlabelsym(srsym).defined:=true;
  2273. p1:=clabelnode.create(nil,tlabelsym(srsym));
  2274. tlabelsym(srsym).code:=p1;
  2275. end;
  2276. end;
  2277. errorsym :
  2278. begin
  2279. p1:=cerrornode.create;
  2280. if try_to_consume(_LKLAMMER) then
  2281. begin
  2282. parse_paras(false,false,_RKLAMMER);
  2283. consume(_RKLAMMER);
  2284. end;
  2285. end;
  2286. else
  2287. begin
  2288. p1:=cerrornode.create;
  2289. Message(parser_e_illegal_expression);
  2290. end;
  2291. end; { end case }
  2292. end;
  2293. end;
  2294. {---------------------------------------------
  2295. Factor_Read_Set
  2296. ---------------------------------------------}
  2297. { Read a set between [] }
  2298. function factor_read_set:tnode;
  2299. var
  2300. p1,p2 : tnode;
  2301. lastp,
  2302. buildp : tarrayconstructornode;
  2303. old_allow_array_constructor : boolean;
  2304. begin
  2305. buildp:=nil;
  2306. { be sure that a least one arrayconstructn is used, also for an
  2307. empty [] }
  2308. if token=_RECKKLAMMER then
  2309. buildp:=carrayconstructornode.create(nil,buildp)
  2310. else
  2311. repeat
  2312. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  2313. old_allow_array_constructor:=allow_array_constructor;
  2314. allow_array_constructor:=false;
  2315. p1:=comp_expr(true,false);
  2316. if try_to_consume(_POINTPOINT) then
  2317. begin
  2318. p2:=comp_expr(true,false);
  2319. p1:=carrayconstructorrangenode.create(p1,p2);
  2320. end;
  2321. { insert at the end of the tree, to get the correct order }
  2322. if not assigned(buildp) then
  2323. begin
  2324. buildp:=carrayconstructornode.create(p1,nil);
  2325. lastp:=buildp;
  2326. end
  2327. else
  2328. begin
  2329. lastp.right:=carrayconstructornode.create(p1,nil);
  2330. lastp:=tarrayconstructornode(lastp.right);
  2331. end;
  2332. allow_array_constructor:=old_allow_array_constructor;
  2333. { there could be more elements }
  2334. until not try_to_consume(_COMMA);
  2335. factor_read_set:=buildp;
  2336. end;
  2337. {---------------------------------------------
  2338. Factor (Main)
  2339. ---------------------------------------------}
  2340. var
  2341. l : longint;
  2342. ic : int64;
  2343. qc : qword;
  2344. p1 : tnode;
  2345. code : integer;
  2346. srsym : tsym;
  2347. srsymtable : TSymtable;
  2348. pd : tprocdef;
  2349. hclassdef : tobjectdef;
  2350. d : bestreal;
  2351. cur : currency;
  2352. hs,hsorg : string;
  2353. hdef : tdef;
  2354. filepos : tfileposinfo;
  2355. callflags : tcallnodeflags;
  2356. again,
  2357. updatefpos,
  2358. nodechanged : boolean;
  2359. begin
  2360. { can't keep a copy of p1 and compare pointers afterwards, because
  2361. p1 may be freed and reallocated in the same place! }
  2362. updatefpos:=false;
  2363. p1:=nil;
  2364. filepos:=current_tokenpos;
  2365. again:=false;
  2366. if token=_ID then
  2367. begin
  2368. again:=true;
  2369. { Handle references to self }
  2370. if (idtoken=_SELF) and
  2371. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2372. assigned(current_structdef) then
  2373. begin
  2374. p1:=load_self_node;
  2375. consume(_ID);
  2376. again:=true;
  2377. end
  2378. else
  2379. factor_read_id(p1,again);
  2380. if assigned(p1) then
  2381. begin
  2382. { factor_read_id will set the filepos to after the id,
  2383. and in case of _SELF the filepos will already be the
  2384. same as filepos (so setting it again doesn't hurt). }
  2385. p1.fileinfo:=filepos;
  2386. filepos:=current_tokenpos;
  2387. end;
  2388. { handle post fix operators }
  2389. updatefpos:=postfixoperators(p1,again,getaddr);
  2390. end
  2391. else
  2392. begin
  2393. updatefpos:=true;
  2394. case token of
  2395. _RETURN :
  2396. begin
  2397. consume(_RETURN);
  2398. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2399. p1 := cexitnode.create(comp_expr(true,false))
  2400. else
  2401. p1 := cexitnode.create(nil);
  2402. end;
  2403. _INHERITED :
  2404. begin
  2405. again:=true;
  2406. consume(_INHERITED);
  2407. if assigned(current_procinfo) and
  2408. assigned(current_structdef) and
  2409. ((current_structdef.typ=objectdef) or
  2410. ((target_info.system in systems_jvm) and
  2411. (current_structdef.typ=recorddef)))then
  2412. begin
  2413. { for record helpers in mode Delphi "inherited" is not
  2414. allowed }
  2415. if is_objectpascal_helper(current_structdef) and
  2416. (m_delphi in current_settings.modeswitches) and
  2417. is_record(tobjectdef(current_structdef).extendeddef) then
  2418. Message(parser_e_inherited_not_in_record);
  2419. if (current_structdef.typ=objectdef) then
  2420. begin
  2421. hclassdef:=tobjectdef(current_structdef).childof;
  2422. { Objective-C categories *replace* methods in the class
  2423. they extend, or add methods to it. So calling an
  2424. inherited method always calls the method inherited from
  2425. the parent of the extended class }
  2426. if is_objccategory(current_structdef) then
  2427. hclassdef:=hclassdef.childof;
  2428. end
  2429. else if target_info.system in systems_jvm then
  2430. hclassdef:=java_fpcbaserecordtype
  2431. else
  2432. internalerror(2012012401);
  2433. { if inherited; only then we need the method with
  2434. the same name }
  2435. if token <> _ID then
  2436. begin
  2437. hs:=current_procinfo.procdef.procsym.name;
  2438. hsorg:=current_procinfo.procdef.procsym.realname;
  2439. anon_inherited:=true;
  2440. { For message methods we need to search using the message
  2441. number or string }
  2442. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2443. srdef:=nil;
  2444. if (po_msgint in pd.procoptions) then
  2445. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2446. else
  2447. if (po_msgstr in pd.procoptions) then
  2448. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2449. else
  2450. { helpers have their own ways of dealing with inherited }
  2451. if is_objectpascal_helper(current_structdef) then
  2452. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2453. else
  2454. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,true);
  2455. end
  2456. else
  2457. begin
  2458. hs:=pattern;
  2459. hsorg:=orgpattern;
  2460. consume(_ID);
  2461. anon_inherited:=false;
  2462. { helpers have their own ways of dealing with inherited }
  2463. if is_objectpascal_helper(current_structdef) then
  2464. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2465. else
  2466. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,true);
  2467. end;
  2468. if assigned(srsym) then
  2469. begin
  2470. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2471. { load the procdef from the inherited class and
  2472. not from self }
  2473. case srsym.typ of
  2474. procsym:
  2475. begin
  2476. if is_objectpascal_helper(current_structdef) then
  2477. begin
  2478. { for a helper load the procdef either from the
  2479. extended type, from the parent helper or from
  2480. the extended type of the parent helper
  2481. depending on the def the found symbol belongs
  2482. to }
  2483. if (srsym.Owner.defowner.typ=objectdef) and
  2484. is_objectpascal_helper(tobjectdef(srsym.Owner.defowner)) then
  2485. if current_structdef.is_related(tdef(srsym.Owner.defowner)) and
  2486. assigned(tobjectdef(current_structdef).childof) then
  2487. hdef:=tobjectdef(current_structdef).childof
  2488. else
  2489. hdef:=tobjectdef(srsym.Owner.defowner).extendeddef
  2490. else
  2491. hdef:=tdef(srsym.Owner.defowner);
  2492. end
  2493. else
  2494. hdef:=hclassdef;
  2495. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2496. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2497. hdef:=tclassrefdef.create(hdef);
  2498. p1:=ctypenode.create(hdef);
  2499. { we need to allow helpers here }
  2500. ttypenode(p1).helperallowed:=true;
  2501. end;
  2502. propertysym:
  2503. ;
  2504. else
  2505. begin
  2506. Message(parser_e_methode_id_expected);
  2507. p1:=cerrornode.create;
  2508. end;
  2509. end;
  2510. callflags:=[cnf_inherited];
  2511. include(current_procinfo.flags,pi_has_inherited);
  2512. if anon_inherited then
  2513. include(callflags,cnf_anon_inherited);
  2514. do_member_read(hclassdef,getaddr,srsym,p1,again,callflags);
  2515. end
  2516. else
  2517. begin
  2518. if anon_inherited then
  2519. begin
  2520. { For message methods we need to call DefaultHandler }
  2521. if (po_msgint in pd.procoptions) or
  2522. (po_msgstr in pd.procoptions) then
  2523. begin
  2524. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable,true);
  2525. if not assigned(srsym) or
  2526. (srsym.typ<>procsym) then
  2527. internalerror(200303171);
  2528. p1:=nil;
  2529. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2530. end
  2531. else
  2532. begin
  2533. { we need to ignore the inherited; }
  2534. p1:=cnothingnode.create;
  2535. end;
  2536. end
  2537. else
  2538. begin
  2539. Message1(sym_e_id_no_member,hsorg);
  2540. p1:=cerrornode.create;
  2541. end;
  2542. again:=false;
  2543. end;
  2544. { turn auto inheriting off }
  2545. anon_inherited:=false;
  2546. end
  2547. else
  2548. begin
  2549. { in case of records we use a more clear error message }
  2550. if assigned(current_structdef) and
  2551. (current_structdef.typ=recorddef) then
  2552. Message(parser_e_inherited_not_in_record)
  2553. else
  2554. Message(parser_e_generic_methods_only_in_methods);
  2555. again:=false;
  2556. p1:=cerrornode.create;
  2557. end;
  2558. postfixoperators(p1,again,getaddr);
  2559. end;
  2560. _INTCONST :
  2561. begin
  2562. {Try first wether the value fits in an int64.}
  2563. val(pattern,ic,code);
  2564. if code=0 then
  2565. begin
  2566. consume(_INTCONST);
  2567. int_to_type(ic,hdef);
  2568. p1:=cordconstnode.create(ic,hdef,true);
  2569. end
  2570. else
  2571. begin
  2572. { try qword next }
  2573. val(pattern,qc,code);
  2574. if code=0 then
  2575. begin
  2576. consume(_INTCONST);
  2577. int_to_type(qc,hdef);
  2578. p1:=cordconstnode.create(qc,hdef,true);
  2579. end;
  2580. end;
  2581. if code<>0 then
  2582. begin
  2583. { finally float }
  2584. val(pattern,d,code);
  2585. if code<>0 then
  2586. begin
  2587. Message(parser_e_invalid_integer);
  2588. consume(_INTCONST);
  2589. l:=1;
  2590. p1:=cordconstnode.create(l,sinttype,true);
  2591. end
  2592. else
  2593. begin
  2594. consume(_INTCONST);
  2595. p1:=crealconstnode.create(d,pbestrealtype^);
  2596. end;
  2597. end
  2598. else
  2599. { the necessary range checking has already been done by val }
  2600. tordconstnode(p1).rangecheck:=false;
  2601. end;
  2602. _REALNUMBER :
  2603. begin
  2604. val(pattern,d,code);
  2605. if code<>0 then
  2606. begin
  2607. Message(parser_e_error_in_real);
  2608. d:=1.0;
  2609. end;
  2610. consume(_REALNUMBER);
  2611. {$ifdef FPC_REAL2REAL_FIXED}
  2612. if current_settings.fputype=fpu_none then
  2613. Message(parser_e_unsupported_real);
  2614. if (current_settings.minfpconstprec=s32real) and
  2615. (d = single(d)) then
  2616. p1:=crealconstnode.create(d,s32floattype)
  2617. else if (current_settings.minfpconstprec=s64real) and
  2618. (d = double(d)) then
  2619. p1:=crealconstnode.create(d,s64floattype)
  2620. else
  2621. {$endif FPC_REAL2REAL_FIXED}
  2622. p1:=crealconstnode.create(d,pbestrealtype^);
  2623. {$ifdef FPC_HAS_STR_CURRENCY}
  2624. val(pattern,cur,code);
  2625. if code=0 then
  2626. trealconstnode(p1).value_currency:=cur;
  2627. {$endif FPC_HAS_STR_CURRENCY}
  2628. end;
  2629. _STRING :
  2630. begin
  2631. string_dec(hdef,true);
  2632. { STRING can be also a type cast }
  2633. if try_to_consume(_LKLAMMER) then
  2634. begin
  2635. p1:=comp_expr(true,false);
  2636. consume(_RKLAMMER);
  2637. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2638. { handle postfix operators here e.g. string(a)[10] }
  2639. again:=true;
  2640. postfixoperators(p1,again,getaddr);
  2641. end
  2642. else
  2643. p1:=ctypenode.create(hdef);
  2644. end;
  2645. _FILE :
  2646. begin
  2647. hdef:=cfiletype;
  2648. consume(_FILE);
  2649. { FILE can be also a type cast }
  2650. if try_to_consume(_LKLAMMER) then
  2651. begin
  2652. p1:=comp_expr(true,false);
  2653. consume(_RKLAMMER);
  2654. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2655. { handle postfix operators here e.g. string(a)[10] }
  2656. again:=true;
  2657. postfixoperators(p1,again,getaddr);
  2658. end
  2659. else
  2660. begin
  2661. p1:=ctypenode.create(hdef);
  2662. end;
  2663. end;
  2664. _CSTRING :
  2665. begin
  2666. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2667. consume(_CSTRING);
  2668. if token in postfixoperator_tokens then
  2669. begin
  2670. again:=true;
  2671. postfixoperators(p1,again,getaddr);
  2672. end;
  2673. end;
  2674. _CCHAR :
  2675. begin
  2676. p1:=cordconstnode.create(ord(pattern[1]),cansichartype,true);
  2677. consume(_CCHAR);
  2678. end;
  2679. _CWSTRING:
  2680. begin
  2681. p1:=cstringconstnode.createunistr(patternw);
  2682. consume(_CWSTRING);
  2683. if token in postfixoperator_tokens then
  2684. begin
  2685. again:=true;
  2686. postfixoperators(p1,again,getaddr);
  2687. end;
  2688. end;
  2689. _CWCHAR:
  2690. begin
  2691. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2692. consume(_CWCHAR);
  2693. end;
  2694. _KLAMMERAFFE :
  2695. begin
  2696. consume(_KLAMMERAFFE);
  2697. got_addrn:=true;
  2698. { support both @<x> and @(<x>) }
  2699. if try_to_consume(_LKLAMMER) then
  2700. begin
  2701. p1:=factor(true,false);
  2702. if token in postfixoperator_tokens then
  2703. begin
  2704. again:=true;
  2705. postfixoperators(p1,again,getaddr);
  2706. end
  2707. else
  2708. consume(_RKLAMMER);
  2709. end
  2710. else
  2711. p1:=factor(true,false);
  2712. if token in postfixoperator_tokens then
  2713. begin
  2714. again:=true;
  2715. postfixoperators(p1,again,getaddr);
  2716. end;
  2717. got_addrn:=false;
  2718. p1:=caddrnode.create(p1);
  2719. p1.fileinfo:=filepos;
  2720. if cs_typed_addresses in current_settings.localswitches then
  2721. include(p1.flags,nf_typedaddr);
  2722. { Store the procvar that we are expecting, the
  2723. addrn will use the information to find the correct
  2724. procdef or it will return an error }
  2725. if assigned(getprocvardef) and
  2726. (taddrnode(p1).left.nodetype = loadn) then
  2727. taddrnode(p1).getprocvardef:=getprocvardef;
  2728. end;
  2729. _LKLAMMER :
  2730. begin
  2731. consume(_LKLAMMER);
  2732. p1:=comp_expr(true,false);
  2733. consume(_RKLAMMER);
  2734. { it's not a good solution
  2735. but (a+b)^ makes some problems }
  2736. if token in postfixoperator_tokens then
  2737. begin
  2738. again:=true;
  2739. postfixoperators(p1,again,getaddr);
  2740. end;
  2741. end;
  2742. _LECKKLAMMER :
  2743. begin
  2744. consume(_LECKKLAMMER);
  2745. p1:=factor_read_set;
  2746. consume(_RECKKLAMMER);
  2747. end;
  2748. _PLUS :
  2749. begin
  2750. consume(_PLUS);
  2751. p1:=factor(false,false);
  2752. p1:=cunaryplusnode.create(p1);
  2753. end;
  2754. _MINUS :
  2755. begin
  2756. consume(_MINUS);
  2757. if (token = _INTCONST) and not(m_isolike_unary_minus in current_settings.modeswitches) then
  2758. begin
  2759. { ugly hack, but necessary to be able to parse }
  2760. { -9223372036854775808 as int64 (JM) }
  2761. pattern := '-'+pattern;
  2762. p1:=sub_expr(oppower,false,false,nil);
  2763. { -1 ** 4 should be - (1 ** 4) and not
  2764. (-1) ** 4
  2765. This was the reason of tw0869.pp test failure PM }
  2766. if p1.nodetype=starstarn then
  2767. begin
  2768. if tbinarynode(p1).left.nodetype=ordconstn then
  2769. begin
  2770. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2771. p1:=cunaryminusnode.create(p1);
  2772. end
  2773. else if tbinarynode(p1).left.nodetype=realconstn then
  2774. begin
  2775. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2776. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2777. p1:=cunaryminusnode.create(p1);
  2778. end
  2779. else
  2780. internalerror(20021029);
  2781. end;
  2782. end
  2783. else
  2784. begin
  2785. if m_isolike_unary_minus in current_settings.modeswitches then
  2786. p1:=sub_expr(opmultiply,false,false,nil)
  2787. else
  2788. p1:=sub_expr(oppower,false,false,nil);
  2789. p1:=cunaryminusnode.create(p1);
  2790. end;
  2791. end;
  2792. _OP_NOT :
  2793. begin
  2794. consume(_OP_NOT);
  2795. p1:=factor(false,false);
  2796. p1:=cnotnode.create(p1);
  2797. end;
  2798. _TRUE :
  2799. begin
  2800. consume(_TRUE);
  2801. p1:=cordconstnode.create(1,pasbool8type,false);
  2802. end;
  2803. _FALSE :
  2804. begin
  2805. consume(_FALSE);
  2806. p1:=cordconstnode.create(0,pasbool8type,false);
  2807. end;
  2808. _NIL :
  2809. begin
  2810. consume(_NIL);
  2811. p1:=cnilnode.create;
  2812. { It's really ugly code nil^, but delphi allows it }
  2813. if token in [_CARET] then
  2814. begin
  2815. again:=true;
  2816. postfixoperators(p1,again,getaddr);
  2817. end;
  2818. end;
  2819. _OBJCPROTOCOL:
  2820. begin
  2821. { The @protocol keyword is used in two ways in Objective-C:
  2822. 1) to declare protocols (~ Object Pascal interfaces)
  2823. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2824. of a declared protocol
  2825. This code is for handling the second case. Because of 1),
  2826. we cannot simply use a system unit symbol.
  2827. }
  2828. consume(_OBJCPROTOCOL);
  2829. consume(_LKLAMMER);
  2830. p1:=factor(false,false);
  2831. consume(_RKLAMMER);
  2832. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2833. end;
  2834. else
  2835. begin
  2836. Message(parser_e_illegal_expression);
  2837. p1:=cerrornode.create;
  2838. { recover }
  2839. consume(token);
  2840. end;
  2841. end;
  2842. end;
  2843. { generate error node if no node is created }
  2844. if not assigned(p1) then
  2845. begin
  2846. {$ifdef EXTDEBUG}
  2847. Comment(V_Warning,'factor: p1=nil');
  2848. {$endif}
  2849. p1:=cerrornode.create;
  2850. updatefpos:=true;
  2851. end;
  2852. { get the resultdef for the node }
  2853. if (not assigned(p1.resultdef)) then
  2854. begin
  2855. do_typecheckpass_changed(p1,nodechanged);
  2856. updatefpos:=updatefpos or nodechanged;
  2857. end;
  2858. if assigned(p1) and
  2859. updatefpos then
  2860. p1.fileinfo:=filepos;
  2861. factor:=p1;
  2862. end;
  2863. {$maxfpuregisters default}
  2864. procedure post_comp_expr_gendef(var def: tdef);
  2865. var
  2866. p1 : tnode;
  2867. again : boolean;
  2868. begin
  2869. if not assigned(def) then
  2870. internalerror(2011053001);
  2871. again:=false;
  2872. { handle potential typecasts, etc }
  2873. p1:=handle_factor_typenode(def,false,again,nil,false);
  2874. { parse postfix operators }
  2875. postfixoperators(p1,again,false);
  2876. if assigned(p1) and (p1.nodetype=typen) then
  2877. def:=ttypenode(p1).typedef
  2878. else
  2879. def:=generrordef;
  2880. end;
  2881. {****************************************************************************
  2882. Sub_Expr
  2883. ****************************************************************************}
  2884. const
  2885. { Warning these stay be ordered !! }
  2886. operator_levels:array[Toperator_precedence] of set of NOTOKEN..last_operator=
  2887. ([_LT,_LTE,_GT,_GTE,_EQ,_NE,_OP_IN],
  2888. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2889. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2890. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2891. [_STARSTAR] );
  2892. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;
  2893. {Reads a subexpression while the operators are of the current precedence
  2894. level, or any higher level. Replaces the old term, simpl_expr and
  2895. simpl2_expr.}
  2896. function istypenode(n:tnode):boolean;inline;
  2897. { Checks whether the given node is a type node or a VMT node containing a
  2898. typenode. This is used in the code for inline specializations in the
  2899. _LT branch below }
  2900. begin
  2901. result:=assigned(n) and
  2902. (
  2903. (n.nodetype=typen) or
  2904. (
  2905. (n.nodetype=loadvmtaddrn) and
  2906. (tloadvmtaddrnode(n).left.nodetype=typen)
  2907. )
  2908. );
  2909. end;
  2910. function gettypedef(n:tnode):tdef;inline;
  2911. { This returns the typedef that belongs to the given typenode or
  2912. loadvmtaddrnode. n must not be Nil! }
  2913. begin
  2914. if n.nodetype=typen then
  2915. result:=ttypenode(n).typedef
  2916. else
  2917. result:=ttypenode(tloadvmtaddrnode(n).left).typedef;
  2918. end;
  2919. function getgenericsym(n:tnode;out srsym:tsym):boolean;
  2920. var
  2921. srsymtable : tsymtable;
  2922. begin
  2923. srsym:=nil;
  2924. case n.nodetype of
  2925. typen:
  2926. srsym:=ttypenode(n).typedef.typesym;
  2927. loadvmtaddrn:
  2928. srsym:=ttypenode(tloadvmtaddrnode(n).left).typedef.typesym;
  2929. loadn:
  2930. if not searchsym_with_symoption(tloadnode(n).symtableentry.Name,srsym,srsymtable,sp_generic_dummy) then
  2931. srsym:=nil;
  2932. { TODO : handle const nodes }
  2933. end;
  2934. result:=assigned(srsym);
  2935. end;
  2936. label
  2937. SubExprStart;
  2938. var
  2939. p1,p2 : tnode;
  2940. oldt : Ttoken;
  2941. filepos : tfileposinfo;
  2942. again : boolean;
  2943. gendef,parseddef : tdef;
  2944. gensym : tsym;
  2945. begin
  2946. SubExprStart:
  2947. if pred_level=highest_precedence then
  2948. begin
  2949. if factornode=nil then
  2950. p1:=factor(false,typeonly)
  2951. else
  2952. p1:=factornode;
  2953. end
  2954. else
  2955. p1:=sub_expr(succ(pred_level),true,typeonly,factornode);
  2956. repeat
  2957. if (token in [NOTOKEN..last_operator]) and
  2958. (token in operator_levels[pred_level]) and
  2959. ((token<>_EQ) or accept_equal) then
  2960. begin
  2961. oldt:=token;
  2962. filepos:=current_tokenpos;
  2963. consume(token);
  2964. if pred_level=highest_precedence then
  2965. p2:=factor(false,false)
  2966. else
  2967. p2:=sub_expr(succ(pred_level),true,typeonly,nil);
  2968. case oldt of
  2969. _PLUS :
  2970. p1:=caddnode.create(addn,p1,p2);
  2971. _MINUS :
  2972. p1:=caddnode.create(subn,p1,p2);
  2973. _STAR :
  2974. p1:=caddnode.create(muln,p1,p2);
  2975. _SLASH :
  2976. p1:=caddnode.create(slashn,p1,p2);
  2977. _EQ:
  2978. p1:=caddnode.create(equaln,p1,p2);
  2979. _GT :
  2980. p1:=caddnode.create(gtn,p1,p2);
  2981. _LT :
  2982. begin
  2983. { we need to decice whether we have an inline specialization
  2984. (type nodes to the left and right of "<", mode Delphi and
  2985. ">" or "," following) or a normal "<" comparison }
  2986. { TODO : p1 could be a non type if e.g. a variable with the
  2987. same name is defined in the same unit where the
  2988. generic is defined (though "same unit" is not
  2989. necessarily needed) }
  2990. if getgenericsym(p1,gensym) and
  2991. { Attention: when nested specializations are supported
  2992. p2 could be a loadn if a "<" follows }
  2993. istypenode(p2) and
  2994. (m_delphi in current_settings.modeswitches) and
  2995. { TODO : add _LT, _LSHARPBRACKET for nested specializations }
  2996. (token in [_GT,_RSHARPBRACKET,_COMMA]) then
  2997. begin
  2998. { this is an inline specialization }
  2999. { retrieve the defs of two nodes }
  3000. gendef:=nil;
  3001. parseddef:=gettypedef(p2);
  3002. if parseddef.typesym.typ<>typesym then
  3003. Internalerror(2011051001);
  3004. { check the hints for parseddef }
  3005. check_hints(parseddef.typesym,parseddef.typesym.symoptions,parseddef.typesym.deprecatedmsg);
  3006. { generate the specialization }
  3007. generate_specialization(gendef,false,'',parseddef,gensym.RealName);
  3008. { we don't need the old left and right nodes anymore }
  3009. p1.Free;
  3010. p2.Free;
  3011. { in case of a class or a record the specialized generic
  3012. is always a classrefdef }
  3013. again:=false;
  3014. { handle potential typecasts, etc }
  3015. p1:=handle_factor_typenode(gendef,false,again,nil,false);
  3016. { parse postfix operators }
  3017. if postfixoperators(p1,again,false) then
  3018. if assigned(p1) then
  3019. p1.fileinfo:=filepos
  3020. else
  3021. p1:=cerrornode.create;
  3022. { with p1 now set we are in reality directly behind the
  3023. call to "factor" thus we need to call down to that
  3024. again }
  3025. { This is disabled until specializations on the right
  3026. hand side work as well, because
  3027. "not working expressions" is better than "half working
  3028. expressions" }
  3029. {factornode:=p1;
  3030. goto SubExprStart;}
  3031. end
  3032. else
  3033. begin
  3034. { this is a normal "<" comparison }
  3035. { potential generic types that are followed by a "<" }
  3036. { a) are not checked whether they are an undefined def,
  3037. but not a generic parameter }
  3038. if (p1.nodetype=typen) and
  3039. (ttypenode(p1).typedef.typ=undefineddef) and
  3040. assigned(ttypenode(p1).typedef.typesym) and
  3041. not (sp_generic_para in ttypenode(p1).typedef.typesym.symoptions) then
  3042. begin
  3043. identifier_not_found(ttypenode(p1).typedef.typesym.RealName);
  3044. p1.Free;
  3045. p1:=cerrornode.create;
  3046. end;
  3047. { b) don't have their hints checked }
  3048. if istypenode(p1) then
  3049. begin
  3050. gendef:=gettypedef(p1);
  3051. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3052. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3053. end;
  3054. { Note: the second part of the expression will be needed
  3055. for nested specializations }
  3056. if istypenode(p2) {and
  3057. not (token in [_LT, _LSHARPBRACKET])} then
  3058. begin
  3059. gendef:=gettypedef(p2);
  3060. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3061. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3062. end;
  3063. { create the comparison node for "<" }
  3064. p1:=caddnode.create(ltn,p1,p2)
  3065. end;
  3066. end;
  3067. _GTE :
  3068. p1:=caddnode.create(gten,p1,p2);
  3069. _LTE :
  3070. p1:=caddnode.create(lten,p1,p2);
  3071. _SYMDIF :
  3072. p1:=caddnode.create(symdifn,p1,p2);
  3073. _STARSTAR :
  3074. p1:=caddnode.create(starstarn,p1,p2);
  3075. _OP_AS,
  3076. _OP_IS :
  3077. begin
  3078. if token in [_LT, _LSHARPBRACKET] then
  3079. begin
  3080. { for now we're handling this as a generic declaration;
  3081. there could be cases though (because of operator
  3082. overloading) where this is the wrong decision... }
  3083. { TODO : here the same note as in _LT applies as p2 could
  3084. point to a variable, etc }
  3085. gendef:=gettypedef(p2);
  3086. if gendef.typesym.typ<>typesym then
  3087. Internalerror(2011071401);
  3088. { generate the specialization }
  3089. generate_specialization(gendef,false,'',nil,'');
  3090. { we don't need the old p2 anymore }
  3091. p2.Free;
  3092. again:=false;
  3093. { handle potential typecasts, etc }
  3094. p2:=handle_factor_typenode(gendef,false,again,nil,false);
  3095. { parse postfix operators }
  3096. if postfixoperators(p2,again,false) then
  3097. if assigned(p2) then
  3098. p2.fileinfo:=filepos
  3099. else
  3100. p2:=cerrornode.create;
  3101. { here we don't need to call back down to "factor", thus
  3102. no "goto" }
  3103. end;
  3104. { now generate the "is" or "as" node }
  3105. case oldt of
  3106. _OP_AS:
  3107. p1:=casnode.create(p1,p2);
  3108. _OP_IS:
  3109. p1:=cisnode.create(p1,p2);
  3110. end;
  3111. end;
  3112. _OP_IN :
  3113. p1:=cinnode.create(p1,p2);
  3114. _OP_OR,
  3115. _PIPE {macpas only} :
  3116. begin
  3117. p1:=caddnode.create(orn,p1,p2);
  3118. if (oldt = _PIPE) then
  3119. include(p1.flags,nf_short_bool);
  3120. end;
  3121. _OP_AND,
  3122. _AMPERSAND {macpas only} :
  3123. begin
  3124. p1:=caddnode.create(andn,p1,p2);
  3125. if (oldt = _AMPERSAND) then
  3126. include(p1.flags,nf_short_bool);
  3127. end;
  3128. _OP_DIV :
  3129. p1:=cmoddivnode.create(divn,p1,p2);
  3130. _OP_NOT :
  3131. p1:=cnotnode.create(p1);
  3132. _OP_MOD :
  3133. begin
  3134. p1:=cmoddivnode.create(modn,p1,p2);
  3135. if m_iso in current_settings.modeswitches then
  3136. include(p1.flags,nf_isomod);
  3137. end;
  3138. _OP_SHL :
  3139. p1:=cshlshrnode.create(shln,p1,p2);
  3140. _OP_SHR :
  3141. p1:=cshlshrnode.create(shrn,p1,p2);
  3142. _OP_XOR :
  3143. p1:=caddnode.create(xorn,p1,p2);
  3144. _ASSIGNMENT :
  3145. p1:=cassignmentnode.create(p1,p2);
  3146. _NE :
  3147. p1:=caddnode.create(unequaln,p1,p2);
  3148. end;
  3149. p1.fileinfo:=filepos;
  3150. end
  3151. else
  3152. break;
  3153. until false;
  3154. sub_expr:=p1;
  3155. end;
  3156. function comp_expr(accept_equal,typeonly:boolean):tnode;
  3157. var
  3158. oldafterassignment : boolean;
  3159. p1 : tnode;
  3160. begin
  3161. oldafterassignment:=afterassignment;
  3162. afterassignment:=true;
  3163. p1:=sub_expr(opcompare,accept_equal,typeonly,nil);
  3164. { get the resultdef for this expression }
  3165. if not assigned(p1.resultdef) then
  3166. do_typecheckpass(p1);
  3167. afterassignment:=oldafterassignment;
  3168. comp_expr:=p1;
  3169. end;
  3170. function expr(dotypecheck : boolean) : tnode;
  3171. var
  3172. p1,p2 : tnode;
  3173. filepos : tfileposinfo;
  3174. oldafterassignment,
  3175. updatefpos : boolean;
  3176. begin
  3177. oldafterassignment:=afterassignment;
  3178. p1:=sub_expr(opcompare,true,false,nil);
  3179. { get the resultdef for this expression }
  3180. if not assigned(p1.resultdef) and
  3181. dotypecheck then
  3182. do_typecheckpass(p1);
  3183. filepos:=current_tokenpos;
  3184. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  3185. afterassignment:=true;
  3186. updatefpos:=true;
  3187. case token of
  3188. _POINTPOINT :
  3189. begin
  3190. consume(_POINTPOINT);
  3191. p2:=sub_expr(opcompare,true,false,nil);
  3192. p1:=crangenode.create(p1,p2);
  3193. end;
  3194. _ASSIGNMENT :
  3195. begin
  3196. consume(_ASSIGNMENT);
  3197. if assigned(p1.resultdef) and (p1.resultdef.typ=procvardef) then
  3198. getprocvardef:=tprocvardef(p1.resultdef);
  3199. p2:=sub_expr(opcompare,true,false,nil);
  3200. if assigned(getprocvardef) then
  3201. handle_procvar(getprocvardef,p2);
  3202. getprocvardef:=nil;
  3203. p1:=cassignmentnode.create(p1,p2);
  3204. end;
  3205. _PLUSASN :
  3206. begin
  3207. consume(_PLUSASN);
  3208. p2:=sub_expr(opcompare,true,false,nil);
  3209. p1:=gen_c_style_operator(addn,p1,p2);
  3210. end;
  3211. _MINUSASN :
  3212. begin
  3213. consume(_MINUSASN);
  3214. p2:=sub_expr(opcompare,true,false,nil);
  3215. p1:=gen_c_style_operator(subn,p1,p2);
  3216. end;
  3217. _STARASN :
  3218. begin
  3219. consume(_STARASN );
  3220. p2:=sub_expr(opcompare,true,false,nil);
  3221. p1:=gen_c_style_operator(muln,p1,p2);
  3222. end;
  3223. _SLASHASN :
  3224. begin
  3225. consume(_SLASHASN );
  3226. p2:=sub_expr(opcompare,true,false,nil);
  3227. p1:=gen_c_style_operator(slashn,p1,p2);
  3228. end;
  3229. else
  3230. updatefpos:=false;
  3231. end;
  3232. { get the resultdef for this expression }
  3233. if not assigned(p1.resultdef) and
  3234. dotypecheck then
  3235. do_typecheckpass(p1);
  3236. afterassignment:=oldafterassignment;
  3237. if updatefpos then
  3238. p1.fileinfo:=filepos;
  3239. expr:=p1;
  3240. end;
  3241. function get_intconst:TConstExprInt;
  3242. {Reads an expression, tries to evalute it and check if it is an integer
  3243. constant. Then the constant is returned.}
  3244. var
  3245. p:tnode;
  3246. begin
  3247. result:=0;
  3248. p:=comp_expr(true,false);
  3249. if not codegenerror then
  3250. begin
  3251. if (p.nodetype<>ordconstn) or
  3252. not(is_integer(p.resultdef)) then
  3253. Message(parser_e_illegal_expression)
  3254. else
  3255. result:=tordconstnode(p).value;
  3256. end;
  3257. p.free;
  3258. end;
  3259. function get_stringconst:string;
  3260. {Reads an expression, tries to evaluate it and checks if it is a string
  3261. constant. Then the constant is returned.}
  3262. var
  3263. p:tnode;
  3264. begin
  3265. get_stringconst:='';
  3266. p:=comp_expr(true,false);
  3267. if p.nodetype<>stringconstn then
  3268. begin
  3269. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  3270. get_stringconst:=char(int64(tordconstnode(p).value))
  3271. else
  3272. Message(parser_e_illegal_expression);
  3273. end
  3274. else
  3275. get_stringconst:=strpas(tstringconstnode(p).value_str);
  3276. p.free;
  3277. end;
  3278. end.