pexpr.pas 115 KB

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