pexpr.pas 139 KB

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