pexpr.pas 117 KB

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