pexpr.pas 121 KB

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