pexpr.pas 143 KB

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