pexpr.pas 98 KB

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