pexpr.pas 171 KB

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