pexpr.pas 117 KB

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