pexpr.pas 149 KB

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