pexpr.pas 96 KB

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