pexpr.pas 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985
  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 : boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr : 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(classh:tobjectdef;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 : 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);
  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);
  152. named_args_allowed:=false;
  153. if found_arg_name then
  154. begin
  155. argname:=p1;
  156. p1:=comp_expr(true);
  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);
  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);
  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);
  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);
  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);
  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);
  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);
  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 (l = in_sizeof_x) or
  369. (not((p1.nodetype = vecn) and
  370. is_packed_array(tvecnode(p1).left.resultdef)) and
  371. not((p1.nodetype = subscriptn) and
  372. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  373. begin
  374. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  375. if (l = in_bitsizeof_x) then
  376. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  377. end
  378. else
  379. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  380. { p1 not needed !}
  381. p1.destroy;
  382. end;
  383. end;
  384. in_typeinfo_x,
  385. in_objc_encode_x :
  386. begin
  387. if (l=in_typeinfo_x) or
  388. (m_objectivec1 in current_settings.modeswitches) then
  389. begin
  390. consume(_LKLAMMER);
  391. in_args:=true;
  392. p1:=comp_expr(true);
  393. { When reading a class type it is parsed as loadvmtaddrn,
  394. typeinfo only needs the type so we remove the loadvmtaddrn }
  395. if p1.nodetype=loadvmtaddrn then
  396. begin
  397. p2:=tloadvmtaddrnode(p1).left;
  398. tloadvmtaddrnode(p1).left:=nil;
  399. p1.free;
  400. p1:=p2;
  401. end;
  402. if p1.nodetype=typen then
  403. ttypenode(p1).allowed:=true;
  404. { else
  405. begin
  406. p1.destroy;
  407. p1:=cerrornode.create;
  408. Message(parser_e_illegal_parameter_list);
  409. end;}
  410. consume(_RKLAMMER);
  411. p2:=geninlinenode(l,false,p1);
  412. statement_syssym:=p2;
  413. end
  414. else
  415. begin
  416. Message1(sym_e_id_not_found, orgpattern);
  417. statement_syssym:=cerrornode.create;
  418. end;
  419. end;
  420. in_unaligned_x :
  421. begin
  422. err:=false;
  423. consume(_LKLAMMER);
  424. in_args:=true;
  425. p1:=comp_expr(true);
  426. p2:=ccallparanode.create(p1,nil);
  427. p2:=geninlinenode(in_unaligned_x,false,p2);
  428. consume(_RKLAMMER);
  429. statement_syssym:=p2;
  430. end;
  431. in_assigned_x :
  432. begin
  433. err:=false;
  434. consume(_LKLAMMER);
  435. in_args:=true;
  436. p1:=comp_expr(true);
  437. { When reading a class type it is parsed as loadvmtaddrn,
  438. typeinfo only needs the type so we remove the loadvmtaddrn }
  439. if p1.nodetype=loadvmtaddrn then
  440. begin
  441. p2:=tloadvmtaddrnode(p1).left;
  442. tloadvmtaddrnode(p1).left:=nil;
  443. p1.free;
  444. p1:=p2;
  445. end;
  446. if not codegenerror then
  447. begin
  448. case p1.resultdef.typ of
  449. procdef, { procvar }
  450. pointerdef,
  451. procvardef,
  452. classrefdef : ;
  453. objectdef :
  454. if not is_class_or_interface_or_dispinterface_or_objc(p1.resultdef) then
  455. begin
  456. Message(parser_e_illegal_parameter_list);
  457. err:=true;
  458. end;
  459. arraydef :
  460. if not is_dynamic_array(p1.resultdef) then
  461. begin
  462. Message(parser_e_illegal_parameter_list);
  463. err:=true;
  464. end;
  465. else
  466. begin
  467. Message(parser_e_illegal_parameter_list);
  468. err:=true;
  469. end;
  470. end;
  471. end
  472. else
  473. err:=true;
  474. if not err then
  475. begin
  476. p2:=ccallparanode.create(p1,nil);
  477. p2:=geninlinenode(in_assigned_x,false,p2);
  478. end
  479. else
  480. begin
  481. p1.free;
  482. p2:=cerrornode.create;
  483. end;
  484. consume(_RKLAMMER);
  485. statement_syssym:=p2;
  486. end;
  487. in_addr_x :
  488. begin
  489. consume(_LKLAMMER);
  490. in_args:=true;
  491. p1:=comp_expr(true);
  492. p1:=caddrnode.create(p1);
  493. if cs_typed_addresses in current_settings.localswitches then
  494. include(p1.flags,nf_typedaddr);
  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);
  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);
  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);
  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);
  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);
  545. if try_to_consume(_COMMA) then
  546. p2:=ccallparanode.create(comp_expr(true),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).free;
  561. if try_to_consume(_COMMA) then
  562. comp_expr(true).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);
  571. Consume(_COMMA);
  572. if not(codegenerror) then
  573. p2:=ccallparanode.create(comp_expr(true),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);
  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);
  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);
  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), nil);
  691. consume(_COMMA);
  692. p2 := ccallparanode.create(comp_expr(true),p1);
  693. if try_to_consume(_COMMA) then
  694. p2 := ccallparanode.create(comp_expr(true),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);
  705. consume(_COMMA);
  706. p2:=comp_expr(true);
  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);
  716. consume(_COMMA);
  717. p2:=comp_expr(true);
  718. consume(_COMMA);
  719. paras:=comp_expr(true);
  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);
  728. if try_to_consume(_COMMA) then
  729. p2:=comp_expr(true)
  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);
  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. begin
  776. { We are calling from the static class method which has no self node }
  777. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  778. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef._class))
  779. else
  780. p1:=load_self_node;
  781. { We are calling a member }
  782. maybe_load_methodpointer:=true;
  783. end;
  784. end;
  785. end;
  786. end;
  787. { reads the parameter for a subroutine call }
  788. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tobjectdef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  789. var
  790. membercall,
  791. prevafterassn : boolean;
  792. i : integer;
  793. para,p2 : tnode;
  794. currpara : tparavarsym;
  795. aprocdef : tprocdef;
  796. begin
  797. prevafterassn:=afterassignment;
  798. afterassignment:=false;
  799. membercall:=false;
  800. aprocdef:=nil;
  801. { when it is a call to a member we need to load the
  802. methodpointer first
  803. }
  804. membercall:=maybe_load_methodpointer(st,p1);
  805. { When we are expecting a procvar we also need
  806. to get the address in some cases }
  807. if assigned(getprocvardef) then
  808. begin
  809. if (block_type=bt_const) or
  810. getaddr then
  811. begin
  812. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  813. getaddr:=true;
  814. end
  815. else
  816. if (m_tp_procvar in current_settings.modeswitches) or
  817. (m_mac_procvar in current_settings.modeswitches) then
  818. begin
  819. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  820. if assigned(aprocdef) then
  821. getaddr:=true;
  822. end;
  823. end;
  824. { only need to get the address of the procedure? }
  825. if getaddr then
  826. begin
  827. { Retrieve info which procvar to call. For tp_procvar the
  828. aprocdef is already loaded above so we can reuse it }
  829. if not assigned(aprocdef) and
  830. assigned(getprocvardef) then
  831. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  832. { generate a methodcallnode or proccallnode }
  833. { we shouldn't convert things like @tcollection.load }
  834. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  835. if assigned(p1) then
  836. begin
  837. { for loading methodpointer of an inherited function
  838. we use self as instance and load the address of
  839. the function directly and not through the vmt (PFV) }
  840. if (cnf_inherited in callflags) then
  841. begin
  842. include(p2.flags,nf_inherited);
  843. p1.free;
  844. p1:=load_self_node;
  845. end;
  846. if (p1.nodetype<>typen) then
  847. tloadnode(p2).set_mp(p1)
  848. else
  849. p1.free;
  850. end;
  851. p1:=p2;
  852. { no postfix operators }
  853. again:=false;
  854. end
  855. else
  856. begin
  857. para:=nil;
  858. if anon_inherited then
  859. begin
  860. if not assigned(current_procinfo) then
  861. internalerror(200305054);
  862. for i:=0 to current_procinfo.procdef.paras.count-1 do
  863. begin
  864. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  865. if not(vo_is_hidden_para in currpara.varoptions) then
  866. begin
  867. { inheritance by msgint? }
  868. if assigned(srdef) then
  869. { anonymous inherited via msgid calls only require a var parameter for
  870. both methods, so we need some type casting here }
  871. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  872. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  873. para)
  874. else
  875. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  876. end;
  877. end;
  878. end
  879. else
  880. begin
  881. if try_to_consume(_LKLAMMER) then
  882. begin
  883. para:=parse_paras(false,false,_RKLAMMER);
  884. consume(_RKLAMMER);
  885. end;
  886. end;
  887. { indicate if this call was generated by a member and
  888. no explicit self is used, this is needed to determine
  889. how to handle a destructor call (PFV) }
  890. if membercall then
  891. include(callflags,cnf_member_call);
  892. if assigned(obj) then
  893. begin
  894. if (st.symtabletype<>ObjectSymtable) then
  895. internalerror(200310031);
  896. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  897. end
  898. else
  899. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  900. end;
  901. afterassignment:=prevafterassn;
  902. end;
  903. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  904. var
  905. hp,hp2 : tnode;
  906. hpp : ^tnode;
  907. currprocdef : tprocdef;
  908. begin
  909. if not assigned(pv) then
  910. internalerror(200301121);
  911. if (m_tp_procvar in current_settings.modeswitches) or
  912. (m_mac_procvar in current_settings.modeswitches) then
  913. begin
  914. hp:=p2;
  915. hpp:=@p2;
  916. while assigned(hp) and
  917. (hp.nodetype=typeconvn) do
  918. begin
  919. hp:=ttypeconvnode(hp).left;
  920. { save orignal address of the old tree so we can replace the node }
  921. hpp:=@hp;
  922. end;
  923. if (hp.nodetype=calln) and
  924. { a procvar can't have parameters! }
  925. not assigned(tcallnode(hp).left) then
  926. begin
  927. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  928. if assigned(currprocdef) then
  929. begin
  930. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  931. if (po_methodpointer in pv.procoptions) then
  932. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  933. hp.destroy;
  934. { replace the old callnode with the new loadnode }
  935. hpp^:=hp2;
  936. end;
  937. end;
  938. end;
  939. end;
  940. { the following procedure handles the access to a property symbol }
  941. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  942. var
  943. paras : tnode;
  944. p2 : tnode;
  945. membercall : boolean;
  946. callflags : tcallnodeflags;
  947. propaccesslist : tpropaccesslist;
  948. static_name : shortstring;
  949. sym: tsym;
  950. srsymtable : tsymtable;
  951. statements : tstatementnode;
  952. converted_result_data : ttempcreatenode;
  953. begin
  954. { property parameters? read them only if the property really }
  955. { has parameters }
  956. paras:=nil;
  957. if (ppo_hasparameters in propsym.propoptions) then
  958. begin
  959. if try_to_consume(_LECKKLAMMER) then
  960. begin
  961. paras:=parse_paras(false,false,_RECKKLAMMER);
  962. consume(_RECKKLAMMER);
  963. end;
  964. end;
  965. { indexed property }
  966. if (ppo_indexed in propsym.propoptions) then
  967. begin
  968. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  969. paras:=ccallparanode.create(p2,paras);
  970. end;
  971. { we need only a write property if a := follows }
  972. { if not(afterassignment) and not(in_args) then }
  973. if token=_ASSIGNMENT then
  974. begin
  975. if getpropaccesslist(propsym,palt_write,propaccesslist) then
  976. begin
  977. sym:=propaccesslist.firstsym^.sym;
  978. case sym.typ of
  979. procsym :
  980. begin
  981. callflags:=[];
  982. { generate the method call }
  983. membercall:=maybe_load_methodpointer(st,p1);
  984. if membercall then
  985. include(callflags,cnf_member_call);
  986. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  987. addsymref(sym);
  988. paras:=nil;
  989. consume(_ASSIGNMENT);
  990. { read the expression }
  991. if propsym.propdef.typ=procvardef then
  992. getprocvardef:=tprocvardef(propsym.propdef);
  993. p2:=comp_expr(true);
  994. if assigned(getprocvardef) then
  995. handle_procvar(getprocvardef,p2);
  996. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  997. { mark as property, both the tcallnode and the real call block }
  998. include(p1.flags,nf_isproperty);
  999. getprocvardef:=nil;
  1000. end;
  1001. fieldvarsym :
  1002. begin
  1003. { generate access code }
  1004. if (sp_static in sym.symoptions) then
  1005. begin
  1006. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1007. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1008. if assigned(sym) then
  1009. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1010. p1.free;
  1011. p1:=nil;
  1012. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1013. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1014. end
  1015. else
  1016. propaccesslist_to_node(p1,st,propaccesslist);
  1017. include(p1.flags,nf_isproperty);
  1018. consume(_ASSIGNMENT);
  1019. { read the expression }
  1020. p2:=comp_expr(true);
  1021. p1:=cassignmentnode.create(p1,p2);
  1022. end
  1023. else
  1024. begin
  1025. p1:=cerrornode.create;
  1026. Message(parser_e_no_procedure_to_access_property);
  1027. end;
  1028. end;
  1029. end
  1030. else
  1031. if (ppo_dispid_write in propsym.propoptions) then
  1032. begin
  1033. consume(_ASSIGNMENT);
  1034. p2:=comp_expr(true);
  1035. { concat value parameter too }
  1036. p2:=ccallparanode.create(p2,nil);
  1037. { passing p3 here is only for information purposes }
  1038. p1:=translate_disp_call(p1,p2,p2,'',propsym.dispid,voidtype);
  1039. end
  1040. else
  1041. begin
  1042. p1:=cerrornode.create;
  1043. Message(parser_e_no_procedure_to_access_property);
  1044. end;
  1045. end
  1046. else
  1047. begin
  1048. if getpropaccesslist(propsym,palt_read,propaccesslist) then
  1049. begin
  1050. sym := propaccesslist.firstsym^.sym;
  1051. case sym.typ of
  1052. fieldvarsym :
  1053. begin
  1054. { generate access code }
  1055. if (sp_static in sym.symoptions) then
  1056. begin
  1057. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1058. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1059. if assigned(sym) then
  1060. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1061. p1.free;
  1062. p1:=nil;
  1063. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1064. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1065. end
  1066. else
  1067. propaccesslist_to_node(p1,st,propaccesslist);
  1068. include(p1.flags,nf_isproperty);
  1069. end;
  1070. procsym :
  1071. begin
  1072. callflags:=[];
  1073. { generate the method call }
  1074. membercall:=maybe_load_methodpointer(st,p1);
  1075. if membercall then
  1076. include(callflags,cnf_member_call);
  1077. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1078. paras:=nil;
  1079. include(p1.flags,nf_isproperty);
  1080. end
  1081. else
  1082. begin
  1083. p1:=cerrornode.create;
  1084. Message(type_e_mismatch);
  1085. end;
  1086. end;
  1087. end
  1088. else
  1089. if (ppo_dispid_read in propsym.propoptions) then
  1090. begin
  1091. p2:=internalstatements(statements);
  1092. converted_result_data:=ctempcreatenode.create(propsym.propdef,sizeof(propsym.propdef),tt_persistent,true);
  1093. addstatement(statements,converted_result_data);
  1094. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  1095. ctypeconvnode.create_internal(translate_disp_call(p1,nil,nil,'',propsym.dispid,propsym.propdef),
  1096. propsym.propdef)));
  1097. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  1098. addstatement(statements,ctemprefnode.create(converted_result_data));
  1099. p1:=p2;
  1100. end
  1101. else
  1102. begin
  1103. { error, no function to read property }
  1104. p1:=cerrornode.create;
  1105. Message(parser_e_no_procedure_to_access_property);
  1106. end;
  1107. end;
  1108. { release paras if not used }
  1109. if assigned(paras) then
  1110. paras.free;
  1111. end;
  1112. { the ID token has to be consumed before calling this function }
  1113. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  1114. var
  1115. static_name : string;
  1116. isclassref : boolean;
  1117. srsymtable : TSymtable;
  1118. begin
  1119. if sym=nil then
  1120. begin
  1121. { pattern is still valid unless
  1122. there is another ID just after the ID of sym }
  1123. Message1(sym_e_id_no_member,orgpattern);
  1124. p1.free;
  1125. p1:=cerrornode.create;
  1126. { try to clean up }
  1127. again:=false;
  1128. end
  1129. else
  1130. begin
  1131. if assigned(p1) then
  1132. begin
  1133. if not assigned(p1.resultdef) then
  1134. do_typecheckpass(p1);
  1135. isclassref:=(p1.resultdef.typ=classrefdef);
  1136. end
  1137. else
  1138. isclassref:=false;
  1139. { we assume, that only procsyms and varsyms are in an object }
  1140. { symbol table, for classes, properties are allowed }
  1141. case sym.typ of
  1142. procsym:
  1143. begin
  1144. do_proc_call(sym,sym.owner,classh,
  1145. (getaddr and not(token in [_CARET,_POINT])),
  1146. again,p1,callflags);
  1147. { we need to know which procedure is called }
  1148. do_typecheckpass(p1);
  1149. { calling using classref? }
  1150. if isclassref and
  1151. (p1.nodetype=calln) and
  1152. assigned(tcallnode(p1).procdefinition) and
  1153. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1154. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1155. Message(parser_e_only_class_members_via_class_ref);
  1156. end;
  1157. fieldvarsym:
  1158. begin
  1159. if (sp_static in sym.symoptions) then
  1160. begin
  1161. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1162. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1163. if assigned(sym) then
  1164. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1165. p1.free;
  1166. p1:=nil;
  1167. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1168. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1169. end
  1170. else
  1171. begin
  1172. if isclassref then
  1173. if assigned(p1) and
  1174. (
  1175. is_self_node(p1) or
  1176. (assigned(current_procinfo) and (current_procinfo.procdef.no_self_node) and
  1177. (current_procinfo.procdef._class = classh))) then
  1178. Message(parser_e_only_class_members)
  1179. else
  1180. Message(parser_e_only_class_members_via_class_ref);
  1181. p1:=csubscriptnode.create(sym,p1);
  1182. end;
  1183. end;
  1184. propertysym:
  1185. begin
  1186. if isclassref and not (sp_static in sym.symoptions) then
  1187. Message(parser_e_only_class_members_via_class_ref);
  1188. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1189. end;
  1190. typesym:
  1191. begin
  1192. p1.free;
  1193. if try_to_consume(_LKLAMMER) then
  1194. begin
  1195. p1:=comp_expr(true);
  1196. consume(_RKLAMMER);
  1197. p1:=ctypeconvnode.create_explicit(p1,ttypesym(sym).typedef);
  1198. end
  1199. else
  1200. begin
  1201. p1:=ctypenode.create(ttypesym(sym).typedef);
  1202. if (is_class(ttypesym(sym).typedef) or is_objcclass(ttypesym(sym).typedef)) and
  1203. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1204. p1:=cloadvmtaddrnode.create(p1);
  1205. end;
  1206. end;
  1207. constsym:
  1208. begin
  1209. p1.free;
  1210. p1:=genconstsymtree(tconstsym(sym));
  1211. end;
  1212. staticvarsym:
  1213. begin
  1214. // typed constant is a staticvarsym
  1215. p1.free;
  1216. p1:=cloadnode.create(sym,sym.Owner);
  1217. end
  1218. else
  1219. internalerror(16);
  1220. end;
  1221. end;
  1222. end;
  1223. {****************************************************************************
  1224. Factor
  1225. ****************************************************************************}
  1226. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1227. out memberparentdef: tdef): boolean;
  1228. var
  1229. hdef : tdef;
  1230. begin
  1231. result:=true;
  1232. memberparentdef:=nil;
  1233. case st.symtabletype of
  1234. ObjectSymtable:
  1235. begin
  1236. memberparentdef:=tdef(st.defowner);
  1237. exit;
  1238. end;
  1239. WithSymtable:
  1240. begin
  1241. if assigned(p1) then
  1242. internalerror(2007012002);
  1243. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1244. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1245. if not(hdef.typ in [objectdef,classrefdef]) then
  1246. exit;
  1247. if (hdef.typ=classrefdef) then
  1248. hdef:=tclassrefdef(hdef).pointeddef;
  1249. memberparentdef:=hdef;
  1250. end;
  1251. else
  1252. result:=false;
  1253. end;
  1254. end;
  1255. {$maxfpuregisters 0}
  1256. function factor(getaddr : boolean) : tnode;
  1257. {---------------------------------------------
  1258. Factor_read_id
  1259. ---------------------------------------------}
  1260. procedure factor_read_id(out p1:tnode;var again:boolean);
  1261. var
  1262. srsym : tsym;
  1263. unit_found : boolean;
  1264. srsymtable : TSymtable;
  1265. hdef : tdef;
  1266. orgstoredpattern,
  1267. storedpattern : string;
  1268. t : ttoken;
  1269. begin
  1270. { allow post fix operators }
  1271. again:=true;
  1272. { first check for identifier }
  1273. if token<>_ID then
  1274. begin
  1275. srsym:=generrorsym;
  1276. srsymtable:=nil;
  1277. consume(_ID);
  1278. end
  1279. else
  1280. begin
  1281. searchsym(pattern,srsym,srsymtable);
  1282. { handle unit specification like System.Writeln }
  1283. unit_found:=try_consume_unitsym(srsym,srsymtable,t);
  1284. storedpattern:=pattern;
  1285. orgstoredpattern:=orgpattern;
  1286. consume(t);
  1287. { named parameter support }
  1288. found_arg_name:=false;
  1289. if not(unit_found) and
  1290. named_args_allowed and
  1291. (token=_ASSIGNMENT) then
  1292. begin
  1293. found_arg_name:=true;
  1294. p1:=cstringconstnode.createstr(storedpattern);
  1295. consume(_ASSIGNMENT);
  1296. exit;
  1297. end;
  1298. { if nothing found give error and return errorsym }
  1299. if assigned(srsym) then
  1300. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  1301. else
  1302. begin
  1303. identifier_not_found(orgstoredpattern);
  1304. srsym:=generrorsym;
  1305. srsymtable:=nil;
  1306. end;
  1307. end;
  1308. { Access to funcret or need to call the function? }
  1309. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1310. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1311. { result(x) is not allowed }
  1312. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  1313. (
  1314. (token=_LKLAMMER) or
  1315. (
  1316. (
  1317. (m_tp7 in current_settings.modeswitches) or
  1318. (m_delphi in current_settings.modeswitches)
  1319. ) and
  1320. (afterassignment or in_args)
  1321. )
  1322. ) then
  1323. begin
  1324. hdef:=tdef(srsym.owner.defowner);
  1325. if assigned(hdef) and
  1326. (hdef.typ=procdef) then
  1327. srsym:=tprocdef(hdef).procsym
  1328. else
  1329. begin
  1330. Message(parser_e_illegal_expression);
  1331. srsym:=generrorsym;
  1332. end;
  1333. srsymtable:=srsym.owner;
  1334. end;
  1335. begin
  1336. case srsym.typ of
  1337. absolutevarsym :
  1338. begin
  1339. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1340. begin
  1341. p1:=nil;
  1342. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1343. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1344. include(p1.flags,nf_absolute);
  1345. end
  1346. else
  1347. p1:=cloadnode.create(srsym,srsymtable);
  1348. end;
  1349. staticvarsym,
  1350. localvarsym,
  1351. paravarsym,
  1352. fieldvarsym :
  1353. begin
  1354. { check if we are reading a field of an object/class/ }
  1355. { record. is_member_read() will deal with withsymtables }
  1356. { if needed. }
  1357. p1:=nil;
  1358. if is_member_read(srsym,srsymtable,p1,hdef) then
  1359. begin
  1360. { if the field was originally found in an }
  1361. { objectsymtable, it means it's part of self
  1362. if only method from which it was called is
  1363. not class static }
  1364. if (srsymtable.symtabletype=ObjectSymtable) then
  1365. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  1366. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef._class))
  1367. else
  1368. p1:=load_self_node;
  1369. { now, if the field itself is part of an objectsymtab }
  1370. { (it can be even if it was found in a withsymtable, }
  1371. { e.g., "with classinstance do field := 5"), then }
  1372. { let do_member_read handle it }
  1373. if (srsym.owner.symtabletype=ObjectSymtable) then
  1374. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1375. else
  1376. { otherwise it's a regular record subscript }
  1377. p1:=csubscriptnode.create(srsym,p1);
  1378. end
  1379. else
  1380. { regular non-field load }
  1381. p1:=cloadnode.create(srsym,srsymtable);
  1382. end;
  1383. syssym :
  1384. begin
  1385. p1:=statement_syssym(tsyssym(srsym).number);
  1386. end;
  1387. typesym :
  1388. begin
  1389. hdef:=ttypesym(srsym).typedef;
  1390. if not assigned(hdef) then
  1391. begin
  1392. again:=false;
  1393. end
  1394. else
  1395. begin
  1396. { We need to know if this unit uses Variants }
  1397. if (hdef=cvarianttype) and
  1398. not(cs_compilesystem in current_settings.moduleswitches) then
  1399. current_module.flags:=current_module.flags or uf_uses_variants;
  1400. if try_to_consume(_LKLAMMER) then
  1401. begin
  1402. p1:=comp_expr(true);
  1403. consume(_RKLAMMER);
  1404. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1405. end
  1406. else { not LKLAMMER }
  1407. if (token=_POINT) and
  1408. is_object(hdef) then
  1409. begin
  1410. consume(_POINT);
  1411. if assigned(current_objectdef) and
  1412. not(getaddr) then
  1413. begin
  1414. if current_objectdef.is_related(tobjectdef(hdef)) then
  1415. begin
  1416. p1:=ctypenode.create(hdef);
  1417. { search also in inherited methods }
  1418. searchsym_in_class(tobjectdef(hdef),current_objectdef,pattern,srsym,srsymtable);
  1419. if assigned(srsym) then
  1420. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1421. consume(_ID);
  1422. do_member_read(tobjectdef(hdef),false,srsym,p1,again,[]);
  1423. end
  1424. else
  1425. begin
  1426. Message(parser_e_no_super_class);
  1427. again:=false;
  1428. end;
  1429. end
  1430. else
  1431. begin
  1432. { allows @TObject.Load }
  1433. { also allows static methods and variables }
  1434. p1:=ctypenode.create(hdef);
  1435. { TP allows also @TMenu.Load if Load is only }
  1436. { defined in an anchestor class }
  1437. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1438. if assigned(srsym) then
  1439. begin
  1440. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1441. if not(getaddr) and
  1442. not((sp_static in srsym.symoptions) or (srsym.typ in [constsym,staticvarsym])) then
  1443. Message(sym_e_only_static_in_static)
  1444. else
  1445. begin
  1446. consume(_ID);
  1447. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1448. end;
  1449. end
  1450. else
  1451. Message1(sym_e_id_no_member,orgpattern);
  1452. end;
  1453. end
  1454. else
  1455. begin
  1456. { class reference ? }
  1457. if is_class(hdef) or
  1458. is_objcclass(hdef) then
  1459. begin
  1460. if getaddr and (token=_POINT) then
  1461. begin
  1462. consume(_POINT);
  1463. { allows @Object.Method }
  1464. { also allows static methods and variables }
  1465. p1:=ctypenode.create(hdef);
  1466. { TP allows also @TMenu.Load if Load is only }
  1467. { defined in an anchestor class }
  1468. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1469. if assigned(srsym) then
  1470. begin
  1471. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1472. consume(_ID);
  1473. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1474. end
  1475. else
  1476. begin
  1477. Message1(sym_e_id_no_member,orgpattern);
  1478. consume(_ID);
  1479. end;
  1480. end
  1481. else
  1482. begin
  1483. p1:=ctypenode.create(hdef);
  1484. { For a type block we simply return only
  1485. the type. For all other blocks we return
  1486. a loadvmt node }
  1487. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1488. p1:=cloadvmtaddrnode.create(p1);
  1489. end;
  1490. end
  1491. else
  1492. p1:=ctypenode.create(hdef);
  1493. end;
  1494. end;
  1495. end;
  1496. enumsym :
  1497. begin
  1498. p1:=genenumnode(tenumsym(srsym));
  1499. end;
  1500. constsym :
  1501. begin
  1502. if tconstsym(srsym).consttyp=constresourcestring then
  1503. begin
  1504. p1:=cloadnode.create(srsym,srsymtable);
  1505. do_typecheckpass(p1);
  1506. p1.resultdef:=cansistringtype;
  1507. end
  1508. else
  1509. p1:=genconstsymtree(tconstsym(srsym));
  1510. end;
  1511. procsym :
  1512. begin
  1513. p1:=nil;
  1514. { check if it's a method/class method }
  1515. if is_member_read(srsym,srsymtable,p1,hdef) then
  1516. begin
  1517. { not srsymtable.symtabletype since that can be }
  1518. { withsymtable as well }
  1519. if (srsym.owner.symtabletype=ObjectSymtable) then
  1520. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1521. else
  1522. { no procsyms in records (yet) }
  1523. internalerror(2007012006);
  1524. end
  1525. else
  1526. { regular procedure/function call }
  1527. do_proc_call(srsym,srsymtable,nil,
  1528. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  1529. again,p1,[]);
  1530. end;
  1531. propertysym :
  1532. begin
  1533. p1:=nil;
  1534. { property of a class/object? }
  1535. if is_member_read(srsym,srsymtable,p1,hdef) then
  1536. begin
  1537. if (srsymtable.symtabletype=ObjectSymtable) then
  1538. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  1539. { no self node in static class methods }
  1540. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  1541. else
  1542. p1:=load_self_node;
  1543. { not srsymtable.symtabletype since that can be }
  1544. { withsymtable as well }
  1545. if (srsym.owner.symtabletype=ObjectSymtable) then
  1546. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1547. else
  1548. { no propertysyms in records (yet) }
  1549. internalerror(2009111510);
  1550. end
  1551. else
  1552. { no method pointer }
  1553. begin
  1554. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1555. end;
  1556. end;
  1557. labelsym :
  1558. begin
  1559. { Support @label }
  1560. if getaddr then
  1561. begin
  1562. if srsym.owner<>current_procinfo.procdef.localst then
  1563. CGMessage(parser_e_label_outside_proc);
  1564. p1:=cloadnode.create(srsym,srsym.owner)
  1565. end
  1566. else
  1567. begin
  1568. consume(_COLON);
  1569. if tlabelsym(srsym).defined then
  1570. Message(sym_e_label_already_defined);
  1571. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1572. begin
  1573. tlabelsym(srsym).nonlocal:=true;
  1574. exclude(current_procinfo.procdef.procoptions,po_inline);
  1575. end;
  1576. tlabelsym(srsym).defined:=true;
  1577. p1:=clabelnode.create(nil,tlabelsym(srsym));
  1578. tlabelsym(srsym).code:=p1;
  1579. end;
  1580. end;
  1581. errorsym :
  1582. begin
  1583. p1:=cerrornode.create;
  1584. if try_to_consume(_LKLAMMER) then
  1585. begin
  1586. parse_paras(false,false,_RKLAMMER);
  1587. consume(_RKLAMMER);
  1588. end;
  1589. end;
  1590. else
  1591. begin
  1592. p1:=cerrornode.create;
  1593. Message(parser_e_illegal_expression);
  1594. end;
  1595. end; { end case }
  1596. end;
  1597. end;
  1598. {---------------------------------------------
  1599. Factor_Read_Set
  1600. ---------------------------------------------}
  1601. { Read a set between [] }
  1602. function factor_read_set:tnode;
  1603. var
  1604. p1,p2 : tnode;
  1605. lastp,
  1606. buildp : tarrayconstructornode;
  1607. begin
  1608. buildp:=nil;
  1609. { be sure that a least one arrayconstructn is used, also for an
  1610. empty [] }
  1611. if token=_RECKKLAMMER then
  1612. buildp:=carrayconstructornode.create(nil,buildp)
  1613. else
  1614. repeat
  1615. p1:=comp_expr(true);
  1616. if try_to_consume(_POINTPOINT) then
  1617. begin
  1618. p2:=comp_expr(true);
  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. { there could be more elements }
  1633. until not try_to_consume(_COMMA);
  1634. factor_read_set:=buildp;
  1635. end;
  1636. {---------------------------------------------
  1637. PostFixOperators
  1638. ---------------------------------------------}
  1639. { returns whether or not p1 has been changed }
  1640. function postfixoperators(var p1:tnode;var again:boolean): boolean;
  1641. { tries to avoid syntax errors after invalid qualifiers }
  1642. procedure recoverconsume_postfixops;
  1643. begin
  1644. repeat
  1645. if not try_to_consume(_CARET) then
  1646. if try_to_consume(_POINT) then
  1647. try_to_consume(_ID)
  1648. else if try_to_consume(_LECKKLAMMER) then
  1649. begin
  1650. repeat
  1651. comp_expr(true);
  1652. until not try_to_consume(_COMMA);
  1653. consume(_RECKKLAMMER);
  1654. end
  1655. else if try_to_consume(_LKLAMMER) then
  1656. begin
  1657. repeat
  1658. comp_expr(true);
  1659. until not try_to_consume(_COMMA);
  1660. consume(_RKLAMMER);
  1661. end
  1662. else
  1663. break;
  1664. until false;
  1665. end;
  1666. procedure handle_variantarray;
  1667. var
  1668. p4 : tnode;
  1669. newstatement : tstatementnode;
  1670. tempresultvariant,
  1671. temp : ttempcreatenode;
  1672. paras : tcallparanode;
  1673. newblock : tnode;
  1674. countindices : aint;
  1675. begin
  1676. { create statements with call initialize the arguments and
  1677. call fpc_dynarr_setlength }
  1678. newblock:=internalstatements(newstatement);
  1679. { get temp for array of indicies,
  1680. we set the real size later }
  1681. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1682. addstatement(newstatement,temp);
  1683. countindices:=0;
  1684. repeat
  1685. p4:=comp_expr(true);
  1686. addstatement(newstatement,cassignmentnode.create(
  1687. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1688. inc(countindices);
  1689. until not try_to_consume(_COMMA);
  1690. { set real size }
  1691. temp.size:=countindices*s32inttype.size;
  1692. consume(_RECKKLAMMER);
  1693. { we need only a write access if a := follows }
  1694. if token=_ASSIGNMENT then
  1695. begin
  1696. consume(_ASSIGNMENT);
  1697. p4:=comp_expr(true);
  1698. { create call to fpc_vararray_put }
  1699. paras:=ccallparanode.create(cordconstnode.create
  1700. (countindices,s32inttype,true),
  1701. ccallparanode.create(caddrnode.create_internal
  1702. (ctemprefnode.create(temp)),
  1703. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1704. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1705. ,nil))));
  1706. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1707. addstatement(newstatement,ctempdeletenode.create(temp));
  1708. end
  1709. else
  1710. begin
  1711. { create temp for result }
  1712. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1713. addstatement(newstatement,tempresultvariant);
  1714. { create call to fpc_vararray_get }
  1715. paras:=ccallparanode.create(cordconstnode.create
  1716. (countindices,s32inttype,true),
  1717. ccallparanode.create(caddrnode.create_internal
  1718. (ctemprefnode.create(temp)),
  1719. ccallparanode.create(p1,
  1720. ccallparanode.create(
  1721. ctemprefnode.create(tempresultvariant)
  1722. ,nil))));
  1723. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1724. addstatement(newstatement,ctempdeletenode.create(temp));
  1725. { the last statement should return the value as
  1726. location and type, this is done be referencing the
  1727. temp and converting it first from a persistent temp to
  1728. normal temp }
  1729. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1730. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1731. end;
  1732. p1:=newblock;
  1733. end;
  1734. var
  1735. protsym : tpropertysym;
  1736. p2,p3 : tnode;
  1737. srsym : tsym;
  1738. srsymtable : TSymtable;
  1739. classh : tobjectdef;
  1740. { shouldn't be used that often, so the extra overhead is ok to save
  1741. stack space }
  1742. dispatchstring : ansistring;
  1743. nodechanged : boolean;
  1744. label
  1745. skipreckklammercheck;
  1746. begin
  1747. result:=false;
  1748. again:=true;
  1749. while again do
  1750. begin
  1751. { we need the resultdef }
  1752. do_typecheckpass_changed(p1,nodechanged);
  1753. result:=result or nodechanged;
  1754. if codegenerror then
  1755. begin
  1756. recoverconsume_postfixops;
  1757. exit;
  1758. end;
  1759. { handle token }
  1760. case token of
  1761. _CARET:
  1762. begin
  1763. consume(_CARET);
  1764. { support tp/mac procvar^ if the procvar returns a
  1765. pointer type }
  1766. if ((m_tp_procvar in current_settings.modeswitches) or
  1767. (m_mac_procvar in current_settings.modeswitches)) and
  1768. (p1.resultdef.typ=procvardef) and
  1769. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1770. begin
  1771. p1:=ccallnode.create_procvar(nil,p1);
  1772. typecheckpass(p1);
  1773. end;
  1774. if (p1.resultdef.typ<>pointerdef) then
  1775. begin
  1776. { ^ as binary operator is a problem!!!! (FK) }
  1777. again:=false;
  1778. Message(parser_e_invalid_qualifier);
  1779. recoverconsume_postfixops;
  1780. p1.destroy;
  1781. p1:=cerrornode.create;
  1782. end
  1783. else
  1784. p1:=cderefnode.create(p1);
  1785. end;
  1786. _LECKKLAMMER:
  1787. begin
  1788. if is_class_or_interface_or_object(p1.resultdef) then
  1789. begin
  1790. { default property }
  1791. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1792. if not(assigned(protsym)) then
  1793. begin
  1794. p1.destroy;
  1795. p1:=cerrornode.create;
  1796. again:=false;
  1797. message(parser_e_no_default_property_available);
  1798. end
  1799. else
  1800. begin
  1801. { The property symbol is referenced indirect }
  1802. protsym.IncRefCount;
  1803. handle_propertysym(protsym,protsym.owner,p1);
  1804. end;
  1805. end
  1806. else
  1807. begin
  1808. consume(_LECKKLAMMER);
  1809. repeat
  1810. { in all of the cases below, p1 is changed }
  1811. case p1.resultdef.typ of
  1812. pointerdef:
  1813. begin
  1814. { support delphi autoderef }
  1815. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1816. (m_autoderef in current_settings.modeswitches) then
  1817. p1:=cderefnode.create(p1);
  1818. p2:=comp_expr(true);
  1819. { Support Pbytevar[0..9] which returns array [0..9].}
  1820. if try_to_consume(_POINTPOINT) then
  1821. p2:=crangenode.create(p2,comp_expr(true));
  1822. p1:=cvecnode.create(p1,p2);
  1823. end;
  1824. variantdef:
  1825. begin
  1826. handle_variantarray;
  1827. { the RECKKLAMMER is already read }
  1828. goto skipreckklammercheck;
  1829. end;
  1830. stringdef :
  1831. begin
  1832. p2:=comp_expr(true);
  1833. { Support string[0..9] which returns array [0..9] of char.}
  1834. if try_to_consume(_POINTPOINT) then
  1835. p2:=crangenode.create(p2,comp_expr(true));
  1836. p1:=cvecnode.create(p1,p2);
  1837. end;
  1838. arraydef:
  1839. begin
  1840. p2:=comp_expr(true);
  1841. { support SEG:OFS for go32v2 Mem[] }
  1842. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1843. (p1.nodetype=loadn) and
  1844. assigned(tloadnode(p1).symtableentry) and
  1845. assigned(tloadnode(p1).symtableentry.owner.name) and
  1846. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1847. ((tloadnode(p1).symtableentry.name='MEM') or
  1848. (tloadnode(p1).symtableentry.name='MEMW') or
  1849. (tloadnode(p1).symtableentry.name='MEML')) then
  1850. begin
  1851. if try_to_consume(_COLON) then
  1852. begin
  1853. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1854. p2:=comp_expr(true);
  1855. p2:=caddnode.create(addn,p2,p3);
  1856. if try_to_consume(_POINTPOINT) then
  1857. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1858. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true),p3.getcopy));
  1859. p1:=cvecnode.create(p1,p2);
  1860. include(tvecnode(p1).flags,nf_memseg);
  1861. include(tvecnode(p1).flags,nf_memindex);
  1862. end
  1863. else
  1864. begin
  1865. if try_to_consume(_POINTPOINT) then
  1866. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1867. p2:=crangenode.create(p2,comp_expr(true));
  1868. p1:=cvecnode.create(p1,p2);
  1869. include(tvecnode(p1).flags,nf_memindex);
  1870. end;
  1871. end
  1872. else
  1873. begin
  1874. if try_to_consume(_POINTPOINT) then
  1875. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1876. p2:=crangenode.create(p2,comp_expr(true));
  1877. p1:=cvecnode.create(p1,p2);
  1878. end;
  1879. end;
  1880. else
  1881. begin
  1882. if p1.resultdef.typ<>undefineddef then
  1883. Message(parser_e_invalid_qualifier);
  1884. p1.destroy;
  1885. p1:=cerrornode.create;
  1886. comp_expr(true);
  1887. again:=false;
  1888. end;
  1889. end;
  1890. do_typecheckpass(p1);
  1891. until not try_to_consume(_COMMA);
  1892. consume(_RECKKLAMMER);
  1893. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1894. skipreckklammercheck:
  1895. end;
  1896. end;
  1897. _POINT :
  1898. begin
  1899. consume(_POINT);
  1900. if (p1.resultdef.typ=pointerdef) and
  1901. (m_autoderef in current_settings.modeswitches) and
  1902. { don't auto-deref objc.id, because then the code
  1903. below for supporting id.anyobjcmethod isn't triggered }
  1904. (p1.resultdef<>objc_idtype) then
  1905. begin
  1906. p1:=cderefnode.create(p1);
  1907. do_typecheckpass(p1);
  1908. end;
  1909. { procvar.<something> can never mean anything so always
  1910. try to call it in case it returns a record/object/... }
  1911. maybe_call_procvar(p1,false);
  1912. case p1.resultdef.typ of
  1913. recorddef:
  1914. begin
  1915. if token=_ID then
  1916. begin
  1917. srsym:=tsym(trecorddef(p1.resultdef).symtable.Find(pattern));
  1918. if assigned(srsym) and
  1919. (srsym.typ=fieldvarsym) then
  1920. begin
  1921. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1922. p1:=csubscriptnode.create(srsym,p1)
  1923. end
  1924. else
  1925. begin
  1926. Message1(sym_e_illegal_field,pattern);
  1927. p1.destroy;
  1928. p1:=cerrornode.create;
  1929. end;
  1930. end;
  1931. consume(_ID);
  1932. end;
  1933. enumdef:
  1934. begin
  1935. if token=_ID then
  1936. begin
  1937. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  1938. p1.destroy;
  1939. if assigned(srsym) and (srsym.typ=enumsym) then
  1940. begin
  1941. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1942. p1:=genenumnode(tenumsym(srsym));
  1943. end
  1944. else
  1945. begin
  1946. Message1(sym_e_id_no_member,orgpattern);
  1947. p1:=cerrornode.create;
  1948. end;
  1949. end;
  1950. consume(_ID);
  1951. end;
  1952. variantdef:
  1953. begin
  1954. { dispatch call? }
  1955. if token=_ID then
  1956. begin
  1957. dispatchstring:=orgpattern;
  1958. consume(_ID);
  1959. if try_to_consume(_LKLAMMER) then
  1960. begin
  1961. p2:=parse_paras(false,true,_RKLAMMER);
  1962. consume(_RKLAMMER);
  1963. end
  1964. else
  1965. p2:=nil;
  1966. { property setter? }
  1967. if (token=_ASSIGNMENT) and not(afterassignment) then
  1968. begin
  1969. consume(_ASSIGNMENT);
  1970. { read the expression }
  1971. p3:=comp_expr(true);
  1972. { concat value parameter too }
  1973. p2:=ccallparanode.create(p3,p2);
  1974. { passing p3 here is only for information purposes }
  1975. p1:=translate_disp_call(p1,p2,p3,dispatchstring,0,voidtype);
  1976. end
  1977. else
  1978. { this is only an approximation
  1979. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1980. if afterassignment or in_args or (token<>_SEMICOLON) then
  1981. p1:=translate_disp_call(p1,p2,nil,dispatchstring,0,cvarianttype)
  1982. else
  1983. p1:=translate_disp_call(p1,p2,nil,dispatchstring,0,voidtype);
  1984. end
  1985. else { Error }
  1986. Consume(_ID);
  1987. end;
  1988. classrefdef:
  1989. begin
  1990. if token=_ID then
  1991. begin
  1992. classh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1993. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1994. if assigned(srsym) then
  1995. begin
  1996. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1997. consume(_ID);
  1998. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1999. end
  2000. else
  2001. begin
  2002. Message1(sym_e_id_no_member,orgpattern);
  2003. p1.destroy;
  2004. p1:=cerrornode.create;
  2005. { try to clean up }
  2006. consume(_ID);
  2007. end;
  2008. end
  2009. else { Error }
  2010. Consume(_ID);
  2011. end;
  2012. objectdef:
  2013. begin
  2014. if token=_ID then
  2015. begin
  2016. classh:=tobjectdef(p1.resultdef);
  2017. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  2018. if assigned(srsym) then
  2019. begin
  2020. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2021. consume(_ID);
  2022. do_member_read(classh,getaddr,srsym,p1,again,[]);
  2023. end
  2024. else
  2025. begin
  2026. Message1(sym_e_id_no_member,orgpattern);
  2027. p1.destroy;
  2028. p1:=cerrornode.create;
  2029. { try to clean up }
  2030. consume(_ID);
  2031. end;
  2032. end
  2033. else { Error }
  2034. Consume(_ID);
  2035. end;
  2036. pointerdef:
  2037. begin
  2038. if (p1.resultdef=objc_idtype) then
  2039. begin
  2040. { objc's id type can be used to call any
  2041. Objective-C method of any Objective-C class
  2042. type that's currently in scope }
  2043. if search_objc_method(pattern,srsym,srsymtable) then
  2044. begin
  2045. consume(_ID);
  2046. do_proc_call(srsym,srsymtable,nil,
  2047. (getaddr and not(token in [_CARET,_POINT])),
  2048. again,p1,[cnf_objc_id_call]);
  2049. { we need to know which procedure is called }
  2050. do_typecheckpass(p1);
  2051. end
  2052. else
  2053. begin
  2054. consume(_ID);
  2055. Message(parser_e_methode_id_expected);
  2056. end;
  2057. end
  2058. else
  2059. begin
  2060. Message(parser_e_invalid_qualifier);
  2061. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2062. Message(parser_h_maybe_deref_caret_missing);
  2063. end
  2064. end;
  2065. else
  2066. begin
  2067. if p1.resultdef.typ<>undefineddef then
  2068. Message(parser_e_invalid_qualifier);
  2069. p1.destroy;
  2070. p1:=cerrornode.create;
  2071. { Error }
  2072. consume(_ID);
  2073. end;
  2074. end;
  2075. end;
  2076. else
  2077. begin
  2078. { is this a procedure variable ? }
  2079. if assigned(p1.resultdef) and
  2080. (p1.resultdef.typ=procvardef) then
  2081. begin
  2082. { Typenode for typecasting or expecting a procvar }
  2083. if (p1.nodetype=typen) or
  2084. (
  2085. assigned(getprocvardef) and
  2086. equal_defs(p1.resultdef,getprocvardef)
  2087. ) then
  2088. begin
  2089. if try_to_consume(_LKLAMMER) then
  2090. begin
  2091. p1:=comp_expr(true);
  2092. consume(_RKLAMMER);
  2093. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2094. end
  2095. else
  2096. again:=false
  2097. end
  2098. else
  2099. begin
  2100. if try_to_consume(_LKLAMMER) then
  2101. begin
  2102. p2:=parse_paras(false,false,_RKLAMMER);
  2103. consume(_RKLAMMER);
  2104. p1:=ccallnode.create_procvar(p2,p1);
  2105. { proc():= is never possible }
  2106. if token=_ASSIGNMENT then
  2107. begin
  2108. Message(parser_e_illegal_expression);
  2109. p1.free;
  2110. p1:=cerrornode.create;
  2111. again:=false;
  2112. end;
  2113. end
  2114. else
  2115. again:=false;
  2116. end;
  2117. end
  2118. else
  2119. again:=false;
  2120. end;
  2121. end;
  2122. { we only try again if p1 was changed }
  2123. if again or
  2124. (p1.nodetype=errorn) then
  2125. result:=true;
  2126. end; { while again }
  2127. end;
  2128. {---------------------------------------------
  2129. Factor (Main)
  2130. ---------------------------------------------}
  2131. var
  2132. l : longint;
  2133. ic : int64;
  2134. qc : qword;
  2135. p1 : tnode;
  2136. code : integer;
  2137. srsym : tsym;
  2138. srsymtable : TSymtable;
  2139. pd : tprocdef;
  2140. hclassdef : tobjectdef;
  2141. d : bestreal;
  2142. cur : currency;
  2143. hs,hsorg : string;
  2144. hdef : tdef;
  2145. filepos : tfileposinfo;
  2146. again,
  2147. updatefpos,
  2148. nodechanged : boolean;
  2149. begin
  2150. { can't keep a copy of p1 and compare pointers afterwards, because
  2151. p1 may be freed and reallocated in the same place! }
  2152. updatefpos:=false;
  2153. p1:=nil;
  2154. filepos:=current_tokenpos;
  2155. again:=false;
  2156. if token=_ID then
  2157. begin
  2158. again:=true;
  2159. { Handle references to self }
  2160. if (idtoken=_SELF) and
  2161. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2162. assigned(current_objectdef) then
  2163. begin
  2164. p1:=load_self_node;
  2165. consume(_ID);
  2166. again:=true;
  2167. end
  2168. else
  2169. factor_read_id(p1,again);
  2170. if assigned(p1) then
  2171. begin
  2172. { factor_read_id will set the filepos to after the id,
  2173. and in case of _SELF the filepos will already be the
  2174. same as filepos (so setting it again doesn't hurt). }
  2175. p1.fileinfo:=filepos;
  2176. filepos:=current_tokenpos;
  2177. end;
  2178. { handle post fix operators }
  2179. updatefpos:=postfixoperators(p1,again);
  2180. end
  2181. else
  2182. begin
  2183. updatefpos:=true;
  2184. case token of
  2185. _RETURN :
  2186. begin
  2187. consume(_RETURN);
  2188. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2189. p1 := cexitnode.create(comp_expr(true))
  2190. else
  2191. p1 := cexitnode.create(nil);
  2192. end;
  2193. _INHERITED :
  2194. begin
  2195. again:=true;
  2196. consume(_INHERITED);
  2197. if assigned(current_procinfo) and
  2198. assigned(current_objectdef) then
  2199. begin
  2200. hclassdef:=current_objectdef.childof;
  2201. { Objective-C categories *replace* methods in the class
  2202. they extend, or add methods to it. So calling an
  2203. inherited method always calls the method inherited from
  2204. the parent of the extended class }
  2205. if is_objccategory(current_objectdef) then
  2206. hclassdef:=hclassdef.childof;
  2207. { if inherited; only then we need the method with
  2208. the same name }
  2209. if token in endtokens then
  2210. begin
  2211. hs:=current_procinfo.procdef.procsym.name;
  2212. hsorg:=current_procinfo.procdef.procsym.realname;
  2213. anon_inherited:=true;
  2214. { For message methods we need to search using the message
  2215. number or string }
  2216. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2217. srdef:=nil;
  2218. if (po_msgint in pd.procoptions) then
  2219. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2220. else
  2221. if (po_msgstr in pd.procoptions) then
  2222. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2223. else
  2224. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2225. end
  2226. else
  2227. begin
  2228. hs:=pattern;
  2229. hsorg:=orgpattern;
  2230. consume(_ID);
  2231. anon_inherited:=false;
  2232. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2233. end;
  2234. if assigned(srsym) then
  2235. begin
  2236. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2237. { load the procdef from the inherited class and
  2238. not from self }
  2239. case srsym.typ of
  2240. procsym:
  2241. begin
  2242. hdef:=hclassdef;
  2243. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2244. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2245. hdef:=tclassrefdef.create(hdef);
  2246. p1:=ctypenode.create(hdef);
  2247. end;
  2248. propertysym:
  2249. ;
  2250. else
  2251. begin
  2252. Message(parser_e_methode_id_expected);
  2253. p1:=cerrornode.create;
  2254. end;
  2255. end;
  2256. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2257. end
  2258. else
  2259. begin
  2260. if anon_inherited then
  2261. begin
  2262. { For message methods we need to call DefaultHandler }
  2263. if (po_msgint in pd.procoptions) or
  2264. (po_msgstr in pd.procoptions) then
  2265. begin
  2266. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2267. if not assigned(srsym) or
  2268. (srsym.typ<>procsym) then
  2269. internalerror(200303171);
  2270. p1:=nil;
  2271. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2272. end
  2273. else
  2274. begin
  2275. { we need to ignore the inherited; }
  2276. p1:=cnothingnode.create;
  2277. end;
  2278. end
  2279. else
  2280. begin
  2281. Message1(sym_e_id_no_member,hsorg);
  2282. p1:=cerrornode.create;
  2283. end;
  2284. again:=false;
  2285. end;
  2286. { turn auto inheriting off }
  2287. anon_inherited:=false;
  2288. end
  2289. else
  2290. begin
  2291. Message(parser_e_generic_methods_only_in_methods);
  2292. again:=false;
  2293. p1:=cerrornode.create;
  2294. end;
  2295. postfixoperators(p1,again);
  2296. end;
  2297. _INTCONST :
  2298. begin
  2299. {Try first wether the value fits in an int64.}
  2300. val(pattern,ic,code);
  2301. if code=0 then
  2302. begin
  2303. consume(_INTCONST);
  2304. int_to_type(ic,hdef);
  2305. p1:=cordconstnode.create(ic,hdef,true);
  2306. end
  2307. else
  2308. begin
  2309. { try qword next }
  2310. val(pattern,qc,code);
  2311. if code=0 then
  2312. begin
  2313. consume(_INTCONST);
  2314. int_to_type(qc,hdef);
  2315. p1:=cordconstnode.create(qc,hdef,true);
  2316. end;
  2317. end;
  2318. if code<>0 then
  2319. begin
  2320. { finally float }
  2321. val(pattern,d,code);
  2322. if code<>0 then
  2323. begin
  2324. Message(parser_e_invalid_integer);
  2325. consume(_INTCONST);
  2326. l:=1;
  2327. p1:=cordconstnode.create(l,sinttype,true);
  2328. end
  2329. else
  2330. begin
  2331. consume(_INTCONST);
  2332. p1:=crealconstnode.create(d,pbestrealtype^);
  2333. end;
  2334. end
  2335. else
  2336. { the necessary range checking has already been done by val }
  2337. tordconstnode(p1).rangecheck:=false;
  2338. end;
  2339. _REALNUMBER :
  2340. begin
  2341. val(pattern,d,code);
  2342. if code<>0 then
  2343. begin
  2344. Message(parser_e_error_in_real);
  2345. d:=1.0;
  2346. end;
  2347. consume(_REALNUMBER);
  2348. {$ifdef FPC_REAL2REAL_FIXED}
  2349. if current_settings.fputype=fpu_none then
  2350. Message(parser_e_unsupported_real);
  2351. if (current_settings.minfpconstprec=s32real) and
  2352. (d = single(d)) then
  2353. p1:=crealconstnode.create(d,s32floattype)
  2354. else if (current_settings.minfpconstprec=s64real) and
  2355. (d = double(d)) then
  2356. p1:=crealconstnode.create(d,s64floattype)
  2357. else
  2358. {$endif FPC_REAL2REAL_FIXED}
  2359. p1:=crealconstnode.create(d,pbestrealtype^);
  2360. {$ifdef FPC_HAS_STR_CURRENCY}
  2361. val(pattern,cur,code);
  2362. if code=0 then
  2363. trealconstnode(p1).value_currency:=cur;
  2364. {$endif FPC_HAS_STR_CURRENCY}
  2365. end;
  2366. _STRING :
  2367. begin
  2368. string_dec(hdef,true);
  2369. { STRING can be also a type cast }
  2370. if try_to_consume(_LKLAMMER) then
  2371. begin
  2372. p1:=comp_expr(true);
  2373. consume(_RKLAMMER);
  2374. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2375. { handle postfix operators here e.g. string(a)[10] }
  2376. again:=true;
  2377. postfixoperators(p1,again);
  2378. end
  2379. else
  2380. p1:=ctypenode.create(hdef);
  2381. end;
  2382. _FILE :
  2383. begin
  2384. hdef:=cfiletype;
  2385. consume(_FILE);
  2386. { FILE can be also a type cast }
  2387. if try_to_consume(_LKLAMMER) then
  2388. begin
  2389. p1:=comp_expr(true);
  2390. consume(_RKLAMMER);
  2391. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2392. { handle postfix operators here e.g. string(a)[10] }
  2393. again:=true;
  2394. postfixoperators(p1,again);
  2395. end
  2396. else
  2397. begin
  2398. p1:=ctypenode.create(hdef);
  2399. end;
  2400. end;
  2401. _CSTRING :
  2402. begin
  2403. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2404. consume(_CSTRING);
  2405. end;
  2406. _CCHAR :
  2407. begin
  2408. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2409. consume(_CCHAR);
  2410. end;
  2411. _CWSTRING:
  2412. begin
  2413. p1:=cstringconstnode.createwstr(patternw);
  2414. consume(_CWSTRING);
  2415. end;
  2416. _CWCHAR:
  2417. begin
  2418. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2419. consume(_CWCHAR);
  2420. end;
  2421. _KLAMMERAFFE :
  2422. begin
  2423. consume(_KLAMMERAFFE);
  2424. got_addrn:=true;
  2425. { support both @<x> and @(<x>) }
  2426. if try_to_consume(_LKLAMMER) then
  2427. begin
  2428. p1:=factor(true);
  2429. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2430. begin
  2431. again:=true;
  2432. postfixoperators(p1,again);
  2433. end
  2434. else
  2435. consume(_RKLAMMER);
  2436. end
  2437. else
  2438. p1:=factor(true);
  2439. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2440. begin
  2441. again:=true;
  2442. postfixoperators(p1,again);
  2443. end;
  2444. got_addrn:=false;
  2445. p1:=caddrnode.create(p1);
  2446. p1.fileinfo:=filepos;
  2447. if cs_typed_addresses in current_settings.localswitches then
  2448. include(p1.flags,nf_typedaddr);
  2449. { Store the procvar that we are expecting, the
  2450. addrn will use the information to find the correct
  2451. procdef or it will return an error }
  2452. if assigned(getprocvardef) and
  2453. (taddrnode(p1).left.nodetype = loadn) then
  2454. taddrnode(p1).getprocvardef:=getprocvardef;
  2455. end;
  2456. _LKLAMMER :
  2457. begin
  2458. consume(_LKLAMMER);
  2459. p1:=comp_expr(true);
  2460. consume(_RKLAMMER);
  2461. { it's not a good solution }
  2462. { but (a+b)^ makes some problems }
  2463. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2464. begin
  2465. again:=true;
  2466. postfixoperators(p1,again);
  2467. end;
  2468. end;
  2469. _LECKKLAMMER :
  2470. begin
  2471. consume(_LECKKLAMMER);
  2472. p1:=factor_read_set;
  2473. consume(_RECKKLAMMER);
  2474. end;
  2475. _PLUS :
  2476. begin
  2477. consume(_PLUS);
  2478. p1:=factor(false);
  2479. { we must generate a new node to do 0+<p1> otherwise the + will
  2480. not be checked }
  2481. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2482. end;
  2483. _MINUS :
  2484. begin
  2485. consume(_MINUS);
  2486. if (token = _INTCONST) then
  2487. begin
  2488. { ugly hack, but necessary to be able to parse }
  2489. { -9223372036854775808 as int64 (JM) }
  2490. pattern := '-'+pattern;
  2491. p1:=sub_expr(oppower,false);
  2492. { -1 ** 4 should be - (1 ** 4) and not
  2493. (-1) ** 4
  2494. This was the reason of tw0869.pp test failure PM }
  2495. if p1.nodetype=starstarn then
  2496. begin
  2497. if tbinarynode(p1).left.nodetype=ordconstn then
  2498. begin
  2499. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2500. p1:=cunaryminusnode.create(p1);
  2501. end
  2502. else if tbinarynode(p1).left.nodetype=realconstn then
  2503. begin
  2504. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2505. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2506. p1:=cunaryminusnode.create(p1);
  2507. end
  2508. else
  2509. internalerror(20021029);
  2510. end;
  2511. end
  2512. else
  2513. begin
  2514. p1:=sub_expr(oppower,false);
  2515. p1:=cunaryminusnode.create(p1);
  2516. end;
  2517. end;
  2518. _OP_NOT :
  2519. begin
  2520. consume(_OP_NOT);
  2521. p1:=factor(false);
  2522. p1:=cnotnode.create(p1);
  2523. end;
  2524. _TRUE :
  2525. begin
  2526. consume(_TRUE);
  2527. p1:=cordconstnode.create(1,booltype,false);
  2528. end;
  2529. _FALSE :
  2530. begin
  2531. consume(_FALSE);
  2532. p1:=cordconstnode.create(0,booltype,false);
  2533. end;
  2534. _NIL :
  2535. begin
  2536. consume(_NIL);
  2537. p1:=cnilnode.create;
  2538. { It's really ugly code nil^, but delphi allows it }
  2539. if token in [_CARET] then
  2540. begin
  2541. again:=true;
  2542. postfixoperators(p1,again);
  2543. end;
  2544. end;
  2545. _OBJCPROTOCOL:
  2546. begin
  2547. { The @protocol keyword is used in two ways in Objective-C:
  2548. 1) to declare protocols (~ Object Pascal interfaces)
  2549. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2550. of a declared protocol
  2551. This code is for handling the second case. Because of 1),
  2552. we cannot simply use a system unit symbol.
  2553. }
  2554. consume(_OBJCPROTOCOL);
  2555. consume(_LKLAMMER);
  2556. p1:=factor(false);
  2557. consume(_RKLAMMER);
  2558. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2559. end;
  2560. else
  2561. begin
  2562. Message(parser_e_illegal_expression);
  2563. p1:=cerrornode.create;
  2564. { recover }
  2565. consume(token);
  2566. end;
  2567. end;
  2568. end;
  2569. { generate error node if no node is created }
  2570. if not assigned(p1) then
  2571. begin
  2572. {$ifdef EXTDEBUG}
  2573. Comment(V_Warning,'factor: p1=nil');
  2574. {$endif}
  2575. p1:=cerrornode.create;
  2576. updatefpos:=true;
  2577. end;
  2578. { get the resultdef for the node }
  2579. if (not assigned(p1.resultdef)) then
  2580. begin
  2581. do_typecheckpass_changed(p1,nodechanged);
  2582. updatefpos:=updatefpos or nodechanged;
  2583. end;
  2584. if assigned(p1) and
  2585. updatefpos then
  2586. p1.fileinfo:=filepos;
  2587. factor:=p1;
  2588. end;
  2589. {$maxfpuregisters default}
  2590. {****************************************************************************
  2591. Sub_Expr
  2592. ****************************************************************************}
  2593. const
  2594. { Warning these stay be ordered !! }
  2595. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2596. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2597. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2598. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2599. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2600. [_STARSTAR] );
  2601. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2602. {Reads a subexpression while the operators are of the current precedence
  2603. level, or any higher level. Replaces the old term, simpl_expr and
  2604. simpl2_expr.}
  2605. var
  2606. p1,p2 : tnode;
  2607. oldt : Ttoken;
  2608. filepos : tfileposinfo;
  2609. begin
  2610. if pred_level=highest_precedence then
  2611. p1:=factor(false)
  2612. else
  2613. p1:=sub_expr(succ(pred_level),true);
  2614. repeat
  2615. if (token in operator_levels[pred_level]) and
  2616. ((token<>_EQUAL) or accept_equal) then
  2617. begin
  2618. oldt:=token;
  2619. filepos:=current_tokenpos;
  2620. consume(token);
  2621. if pred_level=highest_precedence then
  2622. p2:=factor(false)
  2623. else
  2624. p2:=sub_expr(succ(pred_level),true);
  2625. case oldt of
  2626. _PLUS :
  2627. p1:=caddnode.create(addn,p1,p2);
  2628. _MINUS :
  2629. p1:=caddnode.create(subn,p1,p2);
  2630. _STAR :
  2631. p1:=caddnode.create(muln,p1,p2);
  2632. _SLASH :
  2633. p1:=caddnode.create(slashn,p1,p2);
  2634. _EQUAL :
  2635. p1:=caddnode.create(equaln,p1,p2);
  2636. _GT :
  2637. p1:=caddnode.create(gtn,p1,p2);
  2638. _LT :
  2639. p1:=caddnode.create(ltn,p1,p2);
  2640. _GTE :
  2641. p1:=caddnode.create(gten,p1,p2);
  2642. _LTE :
  2643. p1:=caddnode.create(lten,p1,p2);
  2644. _SYMDIF :
  2645. p1:=caddnode.create(symdifn,p1,p2);
  2646. _STARSTAR :
  2647. p1:=caddnode.create(starstarn,p1,p2);
  2648. _OP_AS :
  2649. p1:=casnode.create(p1,p2);
  2650. _OP_IN :
  2651. p1:=cinnode.create(p1,p2);
  2652. _OP_IS :
  2653. p1:=cisnode.create(p1,p2);
  2654. _OP_OR,
  2655. _PIPE {macpas only} :
  2656. begin
  2657. p1:=caddnode.create(orn,p1,p2);
  2658. if (oldt = _PIPE) then
  2659. include(p1.flags,nf_short_bool);
  2660. end;
  2661. _OP_AND,
  2662. _AMPERSAND {macpas only} :
  2663. begin
  2664. p1:=caddnode.create(andn,p1,p2);
  2665. if (oldt = _AMPERSAND) then
  2666. include(p1.flags,nf_short_bool);
  2667. end;
  2668. _OP_DIV :
  2669. p1:=cmoddivnode.create(divn,p1,p2);
  2670. _OP_NOT :
  2671. p1:=cnotnode.create(p1);
  2672. _OP_MOD :
  2673. p1:=cmoddivnode.create(modn,p1,p2);
  2674. _OP_SHL :
  2675. p1:=cshlshrnode.create(shln,p1,p2);
  2676. _OP_SHR :
  2677. p1:=cshlshrnode.create(shrn,p1,p2);
  2678. _OP_XOR :
  2679. p1:=caddnode.create(xorn,p1,p2);
  2680. _ASSIGNMENT :
  2681. p1:=cassignmentnode.create(p1,p2);
  2682. _UNEQUAL :
  2683. p1:=caddnode.create(unequaln,p1,p2);
  2684. end;
  2685. p1.fileinfo:=filepos;
  2686. end
  2687. else
  2688. break;
  2689. until false;
  2690. sub_expr:=p1;
  2691. end;
  2692. function comp_expr(accept_equal : boolean):tnode;
  2693. var
  2694. oldafterassignment : boolean;
  2695. p1 : tnode;
  2696. begin
  2697. oldafterassignment:=afterassignment;
  2698. afterassignment:=true;
  2699. p1:=sub_expr(opcompare,accept_equal);
  2700. { get the resultdef for this expression }
  2701. if not assigned(p1.resultdef) then
  2702. do_typecheckpass(p1);
  2703. afterassignment:=oldafterassignment;
  2704. comp_expr:=p1;
  2705. end;
  2706. function expr(dotypecheck : boolean) : tnode;
  2707. var
  2708. p1,p2 : tnode;
  2709. filepos : tfileposinfo;
  2710. oldafterassignment,
  2711. updatefpos : boolean;
  2712. begin
  2713. oldafterassignment:=afterassignment;
  2714. p1:=sub_expr(opcompare,true);
  2715. { get the resultdef for this expression }
  2716. if not assigned(p1.resultdef) and
  2717. dotypecheck then
  2718. do_typecheckpass(p1);
  2719. filepos:=current_tokenpos;
  2720. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2721. afterassignment:=true;
  2722. updatefpos:=true;
  2723. case token of
  2724. _POINTPOINT :
  2725. begin
  2726. consume(_POINTPOINT);
  2727. p2:=sub_expr(opcompare,true);
  2728. p1:=crangenode.create(p1,p2);
  2729. end;
  2730. _ASSIGNMENT :
  2731. begin
  2732. consume(_ASSIGNMENT);
  2733. if (p1.resultdef.typ=procvardef) then
  2734. getprocvardef:=tprocvardef(p1.resultdef);
  2735. p2:=sub_expr(opcompare,true);
  2736. if assigned(getprocvardef) then
  2737. handle_procvar(getprocvardef,p2);
  2738. getprocvardef:=nil;
  2739. p1:=cassignmentnode.create(p1,p2);
  2740. end;
  2741. _PLUSASN :
  2742. begin
  2743. consume(_PLUSASN);
  2744. p2:=sub_expr(opcompare,true);
  2745. p1:=gen_c_style_operator(addn,p1,p2);
  2746. end;
  2747. _MINUSASN :
  2748. begin
  2749. consume(_MINUSASN);
  2750. p2:=sub_expr(opcompare,true);
  2751. p1:=gen_c_style_operator(subn,p1,p2);
  2752. end;
  2753. _STARASN :
  2754. begin
  2755. consume(_STARASN );
  2756. p2:=sub_expr(opcompare,true);
  2757. p1:=gen_c_style_operator(muln,p1,p2);
  2758. end;
  2759. _SLASHASN :
  2760. begin
  2761. consume(_SLASHASN );
  2762. p2:=sub_expr(opcompare,true);
  2763. p1:=gen_c_style_operator(slashn,p1,p2);
  2764. end;
  2765. else
  2766. updatefpos:=false;
  2767. end;
  2768. { get the resultdef for this expression }
  2769. if not assigned(p1.resultdef) and
  2770. dotypecheck then
  2771. do_typecheckpass(p1);
  2772. afterassignment:=oldafterassignment;
  2773. if updatefpos then
  2774. p1.fileinfo:=filepos;
  2775. expr:=p1;
  2776. end;
  2777. function get_intconst:TConstExprInt;
  2778. {Reads an expression, tries to evalute it and check if it is an integer
  2779. constant. Then the constant is returned.}
  2780. var
  2781. p:tnode;
  2782. begin
  2783. result:=0;
  2784. p:=comp_expr(true);
  2785. if not codegenerror then
  2786. begin
  2787. if (p.nodetype<>ordconstn) or
  2788. not(is_integer(p.resultdef)) then
  2789. Message(parser_e_illegal_expression)
  2790. else
  2791. result:=tordconstnode(p).value;
  2792. end;
  2793. p.free;
  2794. end;
  2795. function get_stringconst:string;
  2796. {Reads an expression, tries to evaluate it and checks if it is a string
  2797. constant. Then the constant is returned.}
  2798. var
  2799. p:tnode;
  2800. begin
  2801. get_stringconst:='';
  2802. p:=comp_expr(true);
  2803. if p.nodetype<>stringconstn then
  2804. begin
  2805. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2806. get_stringconst:=char(int64(tordconstnode(p).value))
  2807. else
  2808. Message(parser_e_illegal_expression);
  2809. end
  2810. else
  2811. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2812. p.free;
  2813. end;
  2814. end.