pexpr.pas 133 KB

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