pexpr.pas 144 KB

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