pexpr.pas 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631
  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_initialize_x:
  599. begin
  600. statement_syssym:=inline_initialize;
  601. end;
  602. in_finalize_x:
  603. begin
  604. statement_syssym:=inline_finalize;
  605. end;
  606. in_copy_x:
  607. begin
  608. statement_syssym:=inline_copy;
  609. end;
  610. in_concat_x :
  611. begin
  612. consume(_LKLAMMER);
  613. in_args:=true;
  614. { Translate to x:=x+y[+z]. The addnode will do the
  615. type checking }
  616. p2:=nil;
  617. repeat
  618. p1:=comp_expr(true);
  619. if p2<>nil then
  620. p2:=caddnode.create(addn,p2,p1)
  621. else
  622. begin
  623. { Force string type if it isn't yet }
  624. if not(
  625. (p1.resulttype.def.deftype=stringdef) or
  626. is_chararray(p1.resulttype.def) or
  627. is_char(p1.resulttype.def)
  628. ) then
  629. inserttypeconv(p1,cshortstringtype);
  630. p2:=p1;
  631. end;
  632. until not try_to_consume(_COMMA);
  633. consume(_RKLAMMER);
  634. statement_syssym:=p2;
  635. end;
  636. in_read_x,
  637. in_readln_x :
  638. begin
  639. if try_to_consume(_LKLAMMER) then
  640. begin
  641. paras:=parse_paras(false,_RKLAMMER);
  642. consume(_RKLAMMER);
  643. end
  644. else
  645. paras:=nil;
  646. p1:=geninlinenode(l,false,paras);
  647. statement_syssym := p1;
  648. end;
  649. in_setlength_x:
  650. begin
  651. statement_syssym := inline_setlength;
  652. end;
  653. in_length_x:
  654. begin
  655. consume(_LKLAMMER);
  656. in_args:=true;
  657. p1:=comp_expr(true);
  658. p2:=geninlinenode(l,false,p1);
  659. consume(_RKLAMMER);
  660. statement_syssym:=p2;
  661. end;
  662. in_write_x,
  663. in_writeln_x :
  664. begin
  665. if try_to_consume(_LKLAMMER) then
  666. begin
  667. paras:=parse_paras(true,_RKLAMMER);
  668. consume(_RKLAMMER);
  669. end
  670. else
  671. paras:=nil;
  672. p1 := geninlinenode(l,false,paras);
  673. statement_syssym := p1;
  674. end;
  675. in_str_x_string :
  676. begin
  677. consume(_LKLAMMER);
  678. paras:=parse_paras(true,_RKLAMMER);
  679. consume(_RKLAMMER);
  680. p1 := geninlinenode(l,false,paras);
  681. statement_syssym := p1;
  682. end;
  683. in_val_x:
  684. Begin
  685. consume(_LKLAMMER);
  686. in_args := true;
  687. p1:= ccallparanode.create(comp_expr(true), nil);
  688. consume(_COMMA);
  689. p2 := ccallparanode.create(comp_expr(true),p1);
  690. if try_to_consume(_COMMA) then
  691. p2 := ccallparanode.create(comp_expr(true),p2);
  692. consume(_RKLAMMER);
  693. p2 := geninlinenode(l,false,p2);
  694. statement_syssym := p2;
  695. End;
  696. in_include_x_y,
  697. in_exclude_x_y :
  698. begin
  699. consume(_LKLAMMER);
  700. in_args:=true;
  701. p1:=comp_expr(true);
  702. consume(_COMMA);
  703. p2:=comp_expr(true);
  704. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  705. consume(_RKLAMMER);
  706. end;
  707. in_assert_x_y :
  708. begin
  709. consume(_LKLAMMER);
  710. in_args:=true;
  711. p1:=comp_expr(true);
  712. if try_to_consume(_COMMA) then
  713. p2:=comp_expr(true)
  714. else
  715. begin
  716. { then insert an empty string }
  717. p2:=cstringconstnode.createstr('',st_conststring);
  718. end;
  719. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  720. consume(_RKLAMMER);
  721. end;
  722. else
  723. internalerror(15);
  724. end;
  725. in_args:=prev_in_args;
  726. end;
  727. function maybe_load_methodpointer(st:tsymtable;var p1:tnode):boolean;
  728. begin
  729. maybe_load_methodpointer:=false;
  730. if not assigned(p1) then
  731. begin
  732. case st.symtabletype of
  733. withsymtable :
  734. begin
  735. if (st.defowner.deftype=objectdef) then
  736. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  737. end;
  738. objectsymtable :
  739. begin
  740. p1:=load_self_node;
  741. { We are calling a member }
  742. maybe_load_methodpointer:=true;
  743. end;
  744. end;
  745. end;
  746. end;
  747. { reads the parameter for a subroutine call }
  748. procedure do_proc_call(sym:tsym;st:tsymtable;obj:tobjectdef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  749. var
  750. membercall,
  751. prevafterassn : boolean;
  752. i : integer;
  753. para,p2 : tnode;
  754. currpara : tparavarsym;
  755. aprocdef : tprocdef;
  756. begin
  757. prevafterassn:=afterassignment;
  758. afterassignment:=false;
  759. membercall:=false;
  760. aprocdef:=nil;
  761. { when it is a call to a member we need to load the
  762. methodpointer first }
  763. membercall:=maybe_load_methodpointer(st,p1);
  764. { When we are expecting a procvar we also need
  765. to get the address in some cases }
  766. if assigned(getprocvardef) then
  767. begin
  768. if (block_type=bt_const) or
  769. getaddr then
  770. begin
  771. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  772. getaddr:=true;
  773. end
  774. else
  775. if (m_tp_procvar in aktmodeswitches) or
  776. (m_mac_procvar in aktmodeswitches) then
  777. begin
  778. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  779. if assigned(aprocdef) then
  780. getaddr:=true;
  781. end;
  782. end;
  783. { only need to get the address of the procedure? }
  784. if getaddr then
  785. begin
  786. { Retrieve info which procvar to call. For tp_procvar the
  787. aprocdef is already loaded above so we can reuse it }
  788. if not assigned(aprocdef) and
  789. assigned(getprocvardef) then
  790. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  791. { generate a methodcallnode or proccallnode }
  792. { we shouldn't convert things like @tcollection.load }
  793. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  794. if assigned(p1) then
  795. begin
  796. if (p1.nodetype<>typen) then
  797. tloadnode(p2).set_mp(p1)
  798. else
  799. p1.free;
  800. end;
  801. p1:=p2;
  802. { no postfix operators }
  803. again:=false;
  804. end
  805. else
  806. begin
  807. para:=nil;
  808. if anon_inherited then
  809. begin
  810. if not assigned(current_procinfo) then
  811. internalerror(200305054);
  812. for i:=0 to current_procinfo.procdef.paras.count-1 do
  813. begin
  814. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  815. if not(vo_is_hidden_para in currpara.varoptions) then
  816. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  817. end;
  818. end
  819. else
  820. begin
  821. if try_to_consume(_LKLAMMER) then
  822. begin
  823. para:=parse_paras(false,_RKLAMMER);
  824. consume(_RKLAMMER);
  825. end;
  826. end;
  827. { indicate if this call was generated by a member and
  828. no explicit self is used, this is needed to determine
  829. how to handle a destructor call (PFV) }
  830. if membercall then
  831. include(callflags,cnf_member_call);
  832. if assigned(obj) then
  833. begin
  834. if (st.symtabletype<>objectsymtable) then
  835. internalerror(200310031);
  836. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  837. end
  838. else
  839. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  840. end;
  841. afterassignment:=prevafterassn;
  842. end;
  843. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  844. var
  845. hp,hp2 : tnode;
  846. hpp : ^tnode;
  847. currprocdef : tprocdef;
  848. begin
  849. if not assigned(pv) then
  850. internalerror(200301121);
  851. if (m_tp_procvar in aktmodeswitches) or
  852. (m_mac_procvar in aktmodeswitches) then
  853. begin
  854. hp:=p2;
  855. hpp:=@p2;
  856. while assigned(hp) and
  857. (hp.nodetype=typeconvn) do
  858. begin
  859. hp:=ttypeconvnode(hp).left;
  860. { save orignal address of the old tree so we can replace the node }
  861. hpp:=@hp;
  862. end;
  863. if (hp.nodetype=calln) and
  864. { a procvar can't have parameters! }
  865. not assigned(tcallnode(hp).left) then
  866. begin
  867. currprocdef:=tcallnode(hp).symtableprocentry.search_procdef_byprocvardef(pv);
  868. if assigned(currprocdef) then
  869. begin
  870. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  871. if (po_methodpointer in pv.procoptions) then
  872. tloadnode(hp2).set_mp(tcallnode(hp).get_load_methodpointer);
  873. hp.destroy;
  874. { replace the old callnode with the new loadnode }
  875. hpp^:=hp2;
  876. end;
  877. end;
  878. end;
  879. end;
  880. { the following procedure handles the access to a property symbol }
  881. procedure handle_propertysym(sym : tsym;st : tsymtable;var p1 : tnode);
  882. var
  883. paras : tnode;
  884. p2 : tnode;
  885. membercall : boolean;
  886. callflags : tcallnodeflags;
  887. begin
  888. paras:=nil;
  889. { property parameters? read them only if the property really }
  890. { has parameters }
  891. if (ppo_hasparameters in tpropertysym(sym).propoptions) then
  892. begin
  893. if try_to_consume(_LECKKLAMMER) then
  894. begin
  895. paras:=parse_paras(false,_RECKKLAMMER);
  896. consume(_RECKKLAMMER);
  897. end;
  898. end;
  899. { indexed property }
  900. if (ppo_indexed in tpropertysym(sym).propoptions) then
  901. begin
  902. p2:=cordconstnode.create(tpropertysym(sym).index,tpropertysym(sym).indextype,true);
  903. paras:=ccallparanode.create(p2,paras);
  904. end;
  905. { we need only a write property if a := follows }
  906. { if not(afterassignment) and not(in_args) then }
  907. if token=_ASSIGNMENT then
  908. begin
  909. { write property: }
  910. if not tpropertysym(sym).writeaccess.empty then
  911. begin
  912. case tpropertysym(sym).writeaccess.firstsym^.sym.typ of
  913. procsym :
  914. begin
  915. callflags:=[];
  916. { generate the method call }
  917. membercall:=maybe_load_methodpointer(st,p1);
  918. if membercall then
  919. include(callflags,cnf_member_call);
  920. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).writeaccess.firstsym^.sym),st,p1,callflags);
  921. paras:=nil;
  922. consume(_ASSIGNMENT);
  923. { read the expression }
  924. if tpropertysym(sym).proptype.def.deftype=procvardef then
  925. getprocvardef:=tprocvardef(tpropertysym(sym).proptype.def);
  926. p2:=comp_expr(true);
  927. if assigned(getprocvardef) then
  928. handle_procvar(getprocvardef,p2);
  929. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  930. { mark as property, both the tcallnode and the real call block }
  931. include(p1.flags,nf_isproperty);
  932. getprocvardef:=nil;
  933. end;
  934. fieldvarsym :
  935. begin
  936. { generate access code }
  937. symlist_to_node(p1,st,tpropertysym(sym).writeaccess);
  938. include(p1.flags,nf_isproperty);
  939. consume(_ASSIGNMENT);
  940. { read the expression }
  941. p2:=comp_expr(true);
  942. p1:=cassignmentnode.create(p1,p2);
  943. end
  944. else
  945. begin
  946. p1:=cerrornode.create;
  947. Message(parser_e_no_procedure_to_access_property);
  948. end;
  949. end;
  950. end
  951. else
  952. begin
  953. p1:=cerrornode.create;
  954. Message(parser_e_no_procedure_to_access_property);
  955. end;
  956. end
  957. else
  958. begin
  959. { read property: }
  960. if not tpropertysym(sym).readaccess.empty then
  961. begin
  962. case tpropertysym(sym).readaccess.firstsym^.sym.typ of
  963. fieldvarsym :
  964. begin
  965. { generate access code }
  966. symlist_to_node(p1,st,tpropertysym(sym).readaccess);
  967. include(p1.flags,nf_isproperty);
  968. end;
  969. procsym :
  970. begin
  971. callflags:=[];
  972. { generate the method call }
  973. membercall:=maybe_load_methodpointer(st,p1);
  974. if membercall then
  975. include(callflags,cnf_member_call);
  976. p1:=ccallnode.create(paras,tprocsym(tpropertysym(sym).readaccess.firstsym^.sym),st,p1,callflags);
  977. paras:=nil;
  978. include(p1.flags,nf_isproperty);
  979. end
  980. else
  981. begin
  982. p1:=cerrornode.create;
  983. Message(type_e_mismatch);
  984. end;
  985. end;
  986. end
  987. else
  988. begin
  989. { error, no function to read property }
  990. p1:=cerrornode.create;
  991. Message(parser_e_no_procedure_to_access_property);
  992. end;
  993. end;
  994. { release paras if not used }
  995. if assigned(paras) then
  996. paras.free;
  997. end;
  998. { the ID token has to be consumed before calling this function }
  999. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  1000. var
  1001. static_name : string;
  1002. isclassref : boolean;
  1003. srsymtable : tsymtable;
  1004. begin
  1005. if sym=nil then
  1006. begin
  1007. { pattern is still valid unless
  1008. there is another ID just after the ID of sym }
  1009. Message1(sym_e_id_no_member,orgpattern);
  1010. p1.free;
  1011. p1:=cerrornode.create;
  1012. { try to clean up }
  1013. again:=false;
  1014. end
  1015. else
  1016. begin
  1017. if assigned(p1) then
  1018. begin
  1019. if not assigned(p1.resulttype.def) then
  1020. do_resulttypepass(p1);
  1021. isclassref:=(p1.resulttype.def.deftype=classrefdef);
  1022. end
  1023. else
  1024. isclassref:=false;
  1025. { we assume, that only procsyms and varsyms are in an object }
  1026. { symbol table, for classes, properties are allowed }
  1027. case sym.typ of
  1028. procsym:
  1029. begin
  1030. do_proc_call(sym,sym.owner,classh,
  1031. (getaddr and not(token in [_CARET,_POINT])),
  1032. again,p1,callflags);
  1033. { we need to know which procedure is called }
  1034. do_resulttypepass(p1);
  1035. { calling using classref? }
  1036. if isclassref and
  1037. (p1.nodetype=calln) and
  1038. assigned(tcallnode(p1).procdefinition) and
  1039. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1040. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1041. Message(parser_e_only_class_methods_via_class_ref);
  1042. end;
  1043. fieldvarsym:
  1044. begin
  1045. if (sp_static in sym.symoptions) then
  1046. begin
  1047. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1048. searchsym(static_name,sym,srsymtable);
  1049. check_hints(sym);
  1050. p1.free;
  1051. p1:=cloadnode.create(sym,srsymtable);
  1052. end
  1053. else
  1054. begin
  1055. if isclassref then
  1056. Message(parser_e_only_class_methods_via_class_ref);
  1057. p1:=csubscriptnode.create(sym,p1);
  1058. end;
  1059. end;
  1060. propertysym:
  1061. begin
  1062. if isclassref then
  1063. Message(parser_e_only_class_methods_via_class_ref);
  1064. handle_propertysym(sym,sym.owner,p1);
  1065. end;
  1066. else internalerror(16);
  1067. end;
  1068. end;
  1069. end;
  1070. {****************************************************************************
  1071. Factor
  1072. ****************************************************************************}
  1073. {$ifdef fpc}
  1074. {$maxfpuregisters 0}
  1075. {$endif fpc}
  1076. function factor(getaddr : boolean) : tnode;
  1077. {---------------------------------------------
  1078. Factor_read_id
  1079. ---------------------------------------------}
  1080. procedure factor_read_id(var p1:tnode;var again:boolean);
  1081. var
  1082. pc : pchar;
  1083. len : longint;
  1084. srsym : tsym;
  1085. possible_error : boolean;
  1086. srsymtable : tsymtable;
  1087. storesymtablestack : tsymtable;
  1088. htype : ttype;
  1089. static_name : string;
  1090. begin
  1091. { allow post fix operators }
  1092. again:=true;
  1093. consume_sym(srsym,srsymtable);
  1094. { Access to funcret or need to call the function? }
  1095. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1096. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1097. (
  1098. (token=_LKLAMMER) or
  1099. (not(m_fpc in aktmodeswitches) and
  1100. (afterassignment or in_args) and
  1101. not(vo_is_result in tabstractvarsym(srsym).varoptions))
  1102. ) then
  1103. begin
  1104. storesymtablestack:=symtablestack;
  1105. symtablestack:=srsym.owner.next;
  1106. searchsym(srsym.name,srsym,srsymtable);
  1107. if not assigned(srsym) then
  1108. srsym:=generrorsym;
  1109. if (srsym.typ<>procsym) then
  1110. Message(parser_e_illegal_expression);
  1111. symtablestack:=storesymtablestack;
  1112. end;
  1113. begin
  1114. case srsym.typ of
  1115. absolutevarsym :
  1116. begin
  1117. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1118. begin
  1119. p1:=nil;
  1120. symlist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1121. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vartype);
  1122. include(p1.flags,nf_absolute);
  1123. end
  1124. else
  1125. p1:=cloadnode.create(srsym,srsymtable);
  1126. end;
  1127. globalvarsym,
  1128. localvarsym,
  1129. paravarsym,
  1130. fieldvarsym :
  1131. begin
  1132. if (sp_static in srsym.symoptions) then
  1133. begin
  1134. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  1135. searchsym(static_name,srsym,srsymtable);
  1136. check_hints(srsym);
  1137. end
  1138. else
  1139. begin
  1140. { are we in a class method, we check here the
  1141. srsymtable, because a field in another object
  1142. also has objectsymtable. And withsymtable is
  1143. not possible for self in class methods (PFV) }
  1144. if (srsymtable.symtabletype=objectsymtable) and
  1145. assigned(current_procinfo) and
  1146. (po_classmethod in current_procinfo.procdef.procoptions) then
  1147. Message(parser_e_only_class_methods);
  1148. end;
  1149. case srsymtable.symtabletype of
  1150. objectsymtable :
  1151. begin
  1152. p1:=csubscriptnode.create(srsym,load_self_node);
  1153. node_tree_set_filepos(p1,aktfilepos);
  1154. end;
  1155. withsymtable :
  1156. begin
  1157. p1:=csubscriptnode.create(srsym,tnode(twithsymtable(srsymtable).withrefnode).getcopy);
  1158. node_tree_set_filepos(p1,aktfilepos);
  1159. end;
  1160. else
  1161. p1:=cloadnode.create(srsym,srsymtable);
  1162. end;
  1163. end;
  1164. typedconstsym :
  1165. begin
  1166. p1:=cloadnode.create(srsym,srsymtable);
  1167. end;
  1168. syssym :
  1169. begin
  1170. p1:=statement_syssym(tsyssym(srsym).number);
  1171. end;
  1172. typesym :
  1173. begin
  1174. htype.setsym(srsym);
  1175. if not assigned(htype.def) then
  1176. begin
  1177. again:=false;
  1178. end
  1179. else
  1180. begin
  1181. { We need to know if this unit uses Variants }
  1182. if (htype.def=cvarianttype.def) and
  1183. not(cs_compilesystem in aktmoduleswitches) then
  1184. current_module.flags:=current_module.flags or uf_uses_variants;
  1185. if try_to_consume(_LKLAMMER) then
  1186. begin
  1187. p1:=comp_expr(true);
  1188. consume(_RKLAMMER);
  1189. p1:=ctypeconvnode.create_explicit(p1,htype);
  1190. end
  1191. else { not LKLAMMER }
  1192. if (token=_POINT) and
  1193. is_object(htype.def) then
  1194. begin
  1195. consume(_POINT);
  1196. if assigned(current_procinfo) and
  1197. assigned(current_procinfo.procdef._class) and
  1198. not(getaddr) then
  1199. begin
  1200. if current_procinfo.procdef._class.is_related(tobjectdef(htype.def)) then
  1201. begin
  1202. p1:=ctypenode.create(htype);
  1203. { search also in inherited methods }
  1204. srsym:=searchsym_in_class(tobjectdef(htype.def),pattern);
  1205. check_hints(srsym);
  1206. consume(_ID);
  1207. do_member_read(tobjectdef(htype.def),false,srsym,p1,again,[]);
  1208. end
  1209. else
  1210. begin
  1211. Message(parser_e_no_super_class);
  1212. again:=false;
  1213. end;
  1214. end
  1215. else
  1216. begin
  1217. { allows @TObject.Load }
  1218. { also allows static methods and variables }
  1219. p1:=ctypenode.create(htype);
  1220. { TP allows also @TMenu.Load if Load is only }
  1221. { defined in an anchestor class }
  1222. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1223. check_hints(srsym);
  1224. if not assigned(srsym) then
  1225. Message1(sym_e_id_no_member,orgpattern)
  1226. else if not(getaddr) and not(sp_static in srsym.symoptions) then
  1227. Message(sym_e_only_static_in_static)
  1228. else
  1229. begin
  1230. consume(_ID);
  1231. do_member_read(tobjectdef(htype.def),getaddr,srsym,p1,again,[]);
  1232. end;
  1233. end;
  1234. end
  1235. else
  1236. begin
  1237. { class reference ? }
  1238. if is_class(htype.def) then
  1239. begin
  1240. if getaddr and (token=_POINT) then
  1241. begin
  1242. consume(_POINT);
  1243. { allows @Object.Method }
  1244. { also allows static methods and variables }
  1245. p1:=ctypenode.create(htype);
  1246. { TP allows also @TMenu.Load if Load is only }
  1247. { defined in an anchestor class }
  1248. srsym:=search_class_member(tobjectdef(htype.def),pattern);
  1249. check_hints(srsym);
  1250. if not assigned(srsym) then
  1251. Message1(sym_e_id_no_member,orgpattern)
  1252. else
  1253. begin
  1254. consume(_ID);
  1255. do_member_read(tobjectdef(htype.def),getaddr,srsym,p1,again,[]);
  1256. end;
  1257. end
  1258. else
  1259. begin
  1260. p1:=ctypenode.create(htype);
  1261. { For a type block we simply return only
  1262. the type. For all other blocks we return
  1263. a loadvmt node }
  1264. if (block_type<>bt_type) then
  1265. p1:=cloadvmtaddrnode.create(p1);
  1266. end;
  1267. end
  1268. else
  1269. p1:=ctypenode.create(htype);
  1270. end;
  1271. end;
  1272. end;
  1273. enumsym :
  1274. begin
  1275. p1:=genenumnode(tenumsym(srsym));
  1276. end;
  1277. constsym :
  1278. begin
  1279. case tconstsym(srsym).consttyp of
  1280. constord :
  1281. begin
  1282. if tconstsym(srsym).consttype.def=nil then
  1283. internalerror(200403232);
  1284. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).consttype,true);
  1285. end;
  1286. conststring :
  1287. begin
  1288. len:=tconstsym(srsym).value.len;
  1289. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1290. len:=255;
  1291. getmem(pc,len+1);
  1292. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1293. pc[len]:=#0;
  1294. p1:=cstringconstnode.createpchar(pc,len,st_conststring);
  1295. end;
  1296. constwstring :
  1297. p1:=cstringconstnode.createwstr(pcompilerwidestring(tconstsym(srsym).value.valueptr));
  1298. constreal :
  1299. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1300. constset :
  1301. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).consttype);
  1302. constpointer :
  1303. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).consttype);
  1304. constnil :
  1305. p1:=cnilnode.create;
  1306. constresourcestring:
  1307. begin
  1308. p1:=cloadnode.create(srsym,srsymtable);
  1309. do_resulttypepass(p1);
  1310. {$ifdef ansistring_bits}
  1311. case aktansistring_bits of
  1312. sb_16:
  1313. p1.resulttype:=cansistringtype16;
  1314. sb_32:
  1315. p1.resulttype:=cansistringtype32;
  1316. sb_64:
  1317. p1.resulttype:=cansistringtype64;
  1318. end;
  1319. {$else}
  1320. p1.resulttype:=cansistringtype;
  1321. {$endif}
  1322. end;
  1323. constguid :
  1324. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1325. else
  1326. internalerror(200507181);
  1327. end;
  1328. end;
  1329. procsym :
  1330. begin
  1331. { are we in a class method ? }
  1332. possible_error:=(srsymtable.symtabletype<>withsymtable) and
  1333. (srsym.owner.symtabletype=objectsymtable) and
  1334. not(is_interface(tdef(srsym.owner.defowner))) and
  1335. assigned(current_procinfo) and
  1336. (po_classmethod in current_procinfo.procdef.procoptions);
  1337. do_proc_call(srsym,srsymtable,nil,
  1338. (getaddr and not(token in [_CARET,_POINT])),
  1339. again,p1,[]);
  1340. { we need to know which procedure is called }
  1341. if possible_error then
  1342. begin
  1343. do_resulttypepass(p1);
  1344. if (p1.nodetype=calln) and
  1345. assigned(tcallnode(p1).procdefinition) and
  1346. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1347. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1348. Message(parser_e_only_class_methods);
  1349. end;
  1350. end;
  1351. propertysym :
  1352. begin
  1353. { access to property in a method }
  1354. { are we in a class method ? }
  1355. if (srsymtable.symtabletype=objectsymtable) and
  1356. assigned(current_procinfo) and
  1357. (po_classmethod in current_procinfo.procdef.procoptions) then
  1358. Message(parser_e_only_class_methods);
  1359. { no method pointer }
  1360. p1:=nil;
  1361. handle_propertysym(srsym,srsymtable,p1);
  1362. end;
  1363. labelsym :
  1364. begin
  1365. { Support @label }
  1366. if getaddr then
  1367. p1:=cloadnode.create(srsym,srsym.owner)
  1368. else
  1369. begin
  1370. consume(_COLON);
  1371. if tlabelsym(srsym).defined then
  1372. Message(sym_e_label_already_defined);
  1373. tlabelsym(srsym).defined:=true;
  1374. p1:=clabelnode.create(nil);
  1375. tlabelsym(srsym).code:=p1;
  1376. end;
  1377. end;
  1378. errorsym :
  1379. begin
  1380. p1:=cerrornode.create;
  1381. if try_to_consume(_LKLAMMER) then
  1382. begin
  1383. parse_paras(false,_RKLAMMER);
  1384. consume(_RKLAMMER);
  1385. end;
  1386. end;
  1387. else
  1388. begin
  1389. p1:=cerrornode.create;
  1390. Message(parser_e_illegal_expression);
  1391. end;
  1392. end; { end case }
  1393. end;
  1394. end;
  1395. {---------------------------------------------
  1396. Factor_Read_Set
  1397. ---------------------------------------------}
  1398. { Read a set between [] }
  1399. function factor_read_set:tnode;
  1400. var
  1401. p1,p2 : tnode;
  1402. lastp,
  1403. buildp : tarrayconstructornode;
  1404. begin
  1405. buildp:=nil;
  1406. { be sure that a least one arrayconstructn is used, also for an
  1407. empty [] }
  1408. if token=_RECKKLAMMER then
  1409. buildp:=carrayconstructornode.create(nil,buildp)
  1410. else
  1411. repeat
  1412. p1:=comp_expr(true);
  1413. if try_to_consume(_POINTPOINT) then
  1414. begin
  1415. p2:=comp_expr(true);
  1416. p1:=carrayconstructorrangenode.create(p1,p2);
  1417. end;
  1418. { insert at the end of the tree, to get the correct order }
  1419. if not assigned(buildp) then
  1420. begin
  1421. buildp:=carrayconstructornode.create(p1,nil);
  1422. lastp:=buildp;
  1423. end
  1424. else
  1425. begin
  1426. lastp.right:=carrayconstructornode.create(p1,nil);
  1427. lastp:=tarrayconstructornode(lastp.right);
  1428. end;
  1429. { there could be more elements }
  1430. until not try_to_consume(_COMMA);
  1431. factor_read_set:=buildp;
  1432. end;
  1433. {---------------------------------------------
  1434. PostFixOperators
  1435. ---------------------------------------------}
  1436. procedure postfixoperators(var p1:tnode;var again:boolean);
  1437. { tries to avoid syntax errors after invalid qualifiers }
  1438. procedure recoverconsume_postfixops;
  1439. begin
  1440. repeat
  1441. if not try_to_consume(_CARET) then
  1442. if try_to_consume(_POINT) then
  1443. try_to_consume(_ID)
  1444. else if try_to_consume(_LECKKLAMMER) then
  1445. begin
  1446. repeat
  1447. comp_expr(true);
  1448. until not try_to_consume(_COMMA);
  1449. consume(_RECKKLAMMER);
  1450. end
  1451. else
  1452. break;
  1453. until false;
  1454. end;
  1455. procedure handle_variantarray;
  1456. var
  1457. p4 : tnode;
  1458. newstatement : tstatementnode;
  1459. tempresultvariant,
  1460. temp : ttempcreatenode;
  1461. paras : tcallparanode;
  1462. newblock : tnode;
  1463. countindices : aint;
  1464. begin
  1465. { create statements with call initialize the arguments and
  1466. call fpc_dynarr_setlength }
  1467. newblock:=internalstatements(newstatement);
  1468. { get temp for array of indicies,
  1469. we set the real size later }
  1470. temp:=ctempcreatenode.create(sinttype,4,tt_persistent,false);
  1471. addstatement(newstatement,temp);
  1472. countindices:=0;
  1473. repeat
  1474. p4:=comp_expr(true);
  1475. addstatement(newstatement,cassignmentnode.create(
  1476. ctemprefnode.create_offset(temp,countindices*sinttype.def.size),p4));
  1477. inc(countindices);
  1478. until not try_to_consume(_COMMA);
  1479. { set real size }
  1480. temp.size:=countindices*sinttype.def.size;
  1481. consume(_RECKKLAMMER);
  1482. { we need only a write access if a := follows }
  1483. if token=_ASSIGNMENT then
  1484. begin
  1485. consume(_ASSIGNMENT);
  1486. p4:=comp_expr(true);
  1487. { create call to fpc_vararray_put }
  1488. paras:=ccallparanode.create(cordconstnode.create
  1489. (countindices,sinttype,true),
  1490. ccallparanode.create(caddrnode.create_internal
  1491. (ctemprefnode.create(temp)),
  1492. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1493. ccallparanode.create(p1
  1494. ,nil))));
  1495. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1496. addstatement(newstatement,ctempdeletenode.create(temp));
  1497. end
  1498. else
  1499. begin
  1500. { create temp for result }
  1501. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.def.size,tt_persistent,true);
  1502. addstatement(newstatement,tempresultvariant);
  1503. { create call to fpc_vararray_get }
  1504. paras:=ccallparanode.create(cordconstnode.create
  1505. (countindices,sinttype,true),
  1506. ccallparanode.create(caddrnode.create_internal
  1507. (ctemprefnode.create(temp)),
  1508. ccallparanode.create(p1,
  1509. ccallparanode.create(
  1510. ctemprefnode.create(tempresultvariant)
  1511. ,nil))));
  1512. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1513. addstatement(newstatement,ctempdeletenode.create(temp));
  1514. { the last statement should return the value as
  1515. location and type, this is done be referencing the
  1516. temp and converting it first from a persistent temp to
  1517. normal temp }
  1518. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1519. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1520. end;
  1521. p1:=newblock;
  1522. end;
  1523. var
  1524. store_static : boolean;
  1525. protsym : tpropertysym;
  1526. p2,p3 : tnode;
  1527. hsym : tsym;
  1528. classh : tobjectdef;
  1529. label
  1530. skipreckklammercheck;
  1531. begin
  1532. again:=true;
  1533. while again do
  1534. begin
  1535. { we need the resulttype }
  1536. do_resulttypepass(p1);
  1537. if codegenerror then
  1538. begin
  1539. recoverconsume_postfixops;
  1540. exit;
  1541. end;
  1542. { handle token }
  1543. case token of
  1544. _CARET:
  1545. begin
  1546. consume(_CARET);
  1547. if (p1.resulttype.def.deftype<>pointerdef) then
  1548. begin
  1549. { ^ as binary operator is a problem!!!! (FK) }
  1550. again:=false;
  1551. Message(parser_e_invalid_qualifier);
  1552. recoverconsume_postfixops;
  1553. p1.destroy;
  1554. p1:=cerrornode.create;
  1555. end
  1556. else
  1557. begin
  1558. p1:=cderefnode.create(p1);
  1559. end;
  1560. end;
  1561. _LECKKLAMMER:
  1562. begin
  1563. if is_class_or_interface(p1.resulttype.def) then
  1564. begin
  1565. { default property }
  1566. protsym:=search_default_property(tobjectdef(p1.resulttype.def));
  1567. if not(assigned(protsym)) then
  1568. begin
  1569. p1.destroy;
  1570. p1:=cerrornode.create;
  1571. again:=false;
  1572. message(parser_e_no_default_property_available);
  1573. end
  1574. else
  1575. begin
  1576. { The property symbol is referenced indirect }
  1577. inc(protsym.refs);
  1578. handle_propertysym(protsym,protsym.owner,p1);
  1579. end;
  1580. end
  1581. else
  1582. begin
  1583. consume(_LECKKLAMMER);
  1584. repeat
  1585. case p1.resulttype.def.deftype of
  1586. pointerdef:
  1587. begin
  1588. { support delphi autoderef }
  1589. if (tpointerdef(p1.resulttype.def).pointertype.def.deftype=arraydef) and
  1590. (m_autoderef in aktmodeswitches) then
  1591. begin
  1592. p1:=cderefnode.create(p1);
  1593. end;
  1594. p2:=comp_expr(true);
  1595. p1:=cvecnode.create(p1,p2);
  1596. end;
  1597. variantdef:
  1598. begin
  1599. handle_variantarray;
  1600. { the RECKKLAMMER is already read }
  1601. goto skipreckklammercheck;
  1602. end;
  1603. stringdef :
  1604. begin
  1605. p2:=comp_expr(true);
  1606. p1:=cvecnode.create(p1,p2);
  1607. end;
  1608. arraydef :
  1609. begin
  1610. p2:=comp_expr(true);
  1611. { support SEG:OFS for go32v2 Mem[] }
  1612. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1613. (p1.nodetype=loadn) and
  1614. assigned(tloadnode(p1).symtableentry) and
  1615. assigned(tloadnode(p1).symtableentry.owner.name) and
  1616. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1617. ((tloadnode(p1).symtableentry.name='MEM') or
  1618. (tloadnode(p1).symtableentry.name='MEMW') or
  1619. (tloadnode(p1).symtableentry.name='MEML')) then
  1620. begin
  1621. if try_to_consume(_COLON) then
  1622. begin
  1623. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1624. p2:=comp_expr(true);
  1625. p2:=caddnode.create(addn,p2,p3);
  1626. p1:=cvecnode.create(p1,p2);
  1627. include(tvecnode(p1).flags,nf_memseg);
  1628. include(tvecnode(p1).flags,nf_memindex);
  1629. end
  1630. else
  1631. begin
  1632. p1:=cvecnode.create(p1,p2);
  1633. include(tvecnode(p1).flags,nf_memindex);
  1634. end;
  1635. end
  1636. else
  1637. p1:=cvecnode.create(p1,p2);
  1638. end;
  1639. else
  1640. begin
  1641. Message(parser_e_invalid_qualifier);
  1642. p1.destroy;
  1643. p1:=cerrornode.create;
  1644. comp_expr(true);
  1645. again:=false;
  1646. end;
  1647. end;
  1648. do_resulttypepass(p1);
  1649. until not try_to_consume(_COMMA);
  1650. consume(_RECKKLAMMER);
  1651. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1652. skipreckklammercheck:
  1653. end;
  1654. end;
  1655. _POINT :
  1656. begin
  1657. consume(_POINT);
  1658. if (p1.resulttype.def.deftype=pointerdef) and
  1659. (m_autoderef in aktmodeswitches) then
  1660. begin
  1661. p1:=cderefnode.create(p1);
  1662. do_resulttypepass(p1);
  1663. end;
  1664. case p1.resulttype.def.deftype of
  1665. recorddef:
  1666. begin
  1667. if token=_ID then
  1668. begin
  1669. hsym:=tsym(trecorddef(p1.resulttype.def).symtable.search(pattern));
  1670. check_hints(hsym);
  1671. if assigned(hsym) and
  1672. (hsym.typ=fieldvarsym) then
  1673. p1:=csubscriptnode.create(hsym,p1)
  1674. else
  1675. begin
  1676. Message1(sym_e_illegal_field,pattern);
  1677. p1.destroy;
  1678. p1:=cerrornode.create;
  1679. end;
  1680. end;
  1681. consume(_ID);
  1682. end;
  1683. variantdef:
  1684. begin
  1685. end;
  1686. classrefdef:
  1687. begin
  1688. if token=_ID then
  1689. begin
  1690. classh:=tobjectdef(tclassrefdef(p1.resulttype.def).pointertype.def);
  1691. hsym:=searchsym_in_class(classh,pattern);
  1692. check_hints(hsym);
  1693. if hsym=nil then
  1694. begin
  1695. Message1(sym_e_id_no_member,orgpattern);
  1696. p1.destroy;
  1697. p1:=cerrornode.create;
  1698. { try to clean up }
  1699. consume(_ID);
  1700. end
  1701. else
  1702. begin
  1703. consume(_ID);
  1704. do_member_read(classh,getaddr,hsym,p1,again,[]);
  1705. end;
  1706. end
  1707. else { Error }
  1708. Consume(_ID);
  1709. end;
  1710. objectdef:
  1711. begin
  1712. if token=_ID then
  1713. begin
  1714. store_static:=allow_only_static;
  1715. allow_only_static:=false;
  1716. classh:=tobjectdef(p1.resulttype.def);
  1717. hsym:=searchsym_in_class(classh,pattern);
  1718. check_hints(hsym);
  1719. allow_only_static:=store_static;
  1720. if hsym=nil then
  1721. begin
  1722. Message1(sym_e_id_no_member,orgpattern);
  1723. p1.destroy;
  1724. p1:=cerrornode.create;
  1725. { try to clean up }
  1726. consume(_ID);
  1727. end
  1728. else
  1729. begin
  1730. consume(_ID);
  1731. do_member_read(classh,getaddr,hsym,p1,again,[]);
  1732. end;
  1733. end
  1734. else { Error }
  1735. Consume(_ID);
  1736. end;
  1737. pointerdef:
  1738. begin
  1739. Message(parser_e_invalid_qualifier);
  1740. if tpointerdef(p1.resulttype.def).pointertype.def.deftype in [recorddef,objectdef,classrefdef] then
  1741. Message(parser_h_maybe_deref_caret_missing);
  1742. end;
  1743. else
  1744. begin
  1745. Message(parser_e_invalid_qualifier);
  1746. p1.destroy;
  1747. p1:=cerrornode.create;
  1748. { Error }
  1749. consume(_ID);
  1750. end;
  1751. end;
  1752. end;
  1753. else
  1754. begin
  1755. { is this a procedure variable ? }
  1756. if assigned(p1.resulttype.def) and
  1757. (p1.resulttype.def.deftype=procvardef) then
  1758. begin
  1759. if assigned(getprocvardef) and
  1760. equal_defs(p1.resulttype.def,getprocvardef) then
  1761. again:=false
  1762. else
  1763. begin
  1764. if try_to_consume(_LKLAMMER) then
  1765. begin
  1766. p2:=parse_paras(false,_RKLAMMER);
  1767. consume(_RKLAMMER);
  1768. p1:=ccallnode.create_procvar(p2,p1);
  1769. { proc():= is never possible }
  1770. if token=_ASSIGNMENT then
  1771. begin
  1772. Message(parser_e_illegal_expression);
  1773. p1.free;
  1774. p1:=cerrornode.create;
  1775. again:=false;
  1776. end;
  1777. end
  1778. else
  1779. again:=false;
  1780. end;
  1781. end
  1782. else
  1783. again:=false;
  1784. end;
  1785. end;
  1786. end; { while again }
  1787. end;
  1788. {---------------------------------------------
  1789. Factor (Main)
  1790. ---------------------------------------------}
  1791. var
  1792. l : longint;
  1793. ic : int64;
  1794. qc : qword;
  1795. {$ifndef cpu64}
  1796. card : cardinal;
  1797. {$endif cpu64}
  1798. oldp1,
  1799. p1 : tnode;
  1800. code : integer;
  1801. again : boolean;
  1802. sym : tsym;
  1803. pd : tprocdef;
  1804. classh : tobjectdef;
  1805. d : bestreal;
  1806. hs,hsorg : string;
  1807. htype : ttype;
  1808. filepos : tfileposinfo;
  1809. {---------------------------------------------
  1810. Helpers
  1811. ---------------------------------------------}
  1812. procedure check_tokenpos;
  1813. begin
  1814. if (p1<>oldp1) then
  1815. begin
  1816. if assigned(p1) then
  1817. p1.fileinfo:=filepos;
  1818. oldp1:=p1;
  1819. filepos:=akttokenpos;
  1820. end;
  1821. end;
  1822. begin
  1823. oldp1:=nil;
  1824. p1:=nil;
  1825. filepos:=akttokenpos;
  1826. again:=false;
  1827. if token=_ID then
  1828. begin
  1829. again:=true;
  1830. { Handle references to self }
  1831. if (idtoken=_SELF) and
  1832. not(block_type in [bt_const,bt_type]) and
  1833. assigned(current_procinfo) and
  1834. assigned(current_procinfo.procdef._class) then
  1835. begin
  1836. p1:=load_self_node;
  1837. consume(_ID);
  1838. again:=true;
  1839. end
  1840. else
  1841. factor_read_id(p1,again);
  1842. if again then
  1843. begin
  1844. check_tokenpos;
  1845. { handle post fix operators }
  1846. postfixoperators(p1,again);
  1847. end;
  1848. end
  1849. else
  1850. case token of
  1851. _INHERITED :
  1852. begin
  1853. again:=true;
  1854. consume(_INHERITED);
  1855. if assigned(current_procinfo) and
  1856. assigned(current_procinfo.procdef._class) then
  1857. begin
  1858. classh:=current_procinfo.procdef._class.childof;
  1859. { if inherited; only then we need the method with
  1860. the same name }
  1861. if token in endtokens then
  1862. begin
  1863. hs:=current_procinfo.procdef.procsym.name;
  1864. hsorg:=current_procinfo.procdef.procsym.realname;
  1865. anon_inherited:=true;
  1866. { For message methods we need to search using the message
  1867. number or string }
  1868. pd:=tprocsym(current_procinfo.procdef.procsym).first_procdef;
  1869. if (po_msgint in pd.procoptions) then
  1870. sym:=searchsym_in_class_by_msgint(classh,pd.messageinf.i)
  1871. else
  1872. if (po_msgstr in pd.procoptions) then
  1873. sym:=searchsym_in_class_by_msgstr(classh,pd.messageinf.str)
  1874. else
  1875. sym:=searchsym_in_class(classh,hs);
  1876. end
  1877. else
  1878. begin
  1879. hs:=pattern;
  1880. hsorg:=orgpattern;
  1881. consume(_ID);
  1882. anon_inherited:=false;
  1883. sym:=searchsym_in_class(classh,hs);
  1884. end;
  1885. if assigned(sym) then
  1886. begin
  1887. check_hints(sym);
  1888. { load the procdef from the inherited class and
  1889. not from self }
  1890. if sym.typ in [procsym,propertysym] then
  1891. begin
  1892. if (sym.typ = procsym) then
  1893. begin
  1894. htype.setdef(classh);
  1895. if (po_classmethod in current_procinfo.procdef.procoptions) or
  1896. (po_staticmethod in current_procinfo.procdef.procoptions) then
  1897. htype.setdef(tclassrefdef.create(htype));
  1898. p1:=ctypenode.create(htype);
  1899. end;
  1900. end
  1901. else
  1902. begin
  1903. Message(parser_e_methode_id_expected);
  1904. p1:=cerrornode.create;
  1905. end;
  1906. do_member_read(classh,false,sym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  1907. end
  1908. else
  1909. begin
  1910. if anon_inherited then
  1911. begin
  1912. { For message methods we need to call DefaultHandler }
  1913. if (po_msgint in pd.procoptions) or
  1914. (po_msgstr in pd.procoptions) then
  1915. begin
  1916. sym:=searchsym_in_class(classh,'DEFAULTHANDLER');
  1917. if not assigned(sym) or
  1918. (sym.typ<>procsym) then
  1919. internalerror(200303171);
  1920. p1:=nil;
  1921. do_proc_call(sym,sym.owner,classh,false,again,p1,[]);
  1922. end
  1923. else
  1924. begin
  1925. { we need to ignore the inherited; }
  1926. p1:=cnothingnode.create;
  1927. end;
  1928. end
  1929. else
  1930. begin
  1931. Message1(sym_e_id_no_member,hsorg);
  1932. p1:=cerrornode.create;
  1933. end;
  1934. again:=false;
  1935. end;
  1936. { turn auto inheriting off }
  1937. anon_inherited:=false;
  1938. end
  1939. else
  1940. begin
  1941. Message(parser_e_generic_methods_only_in_methods);
  1942. again:=false;
  1943. p1:=cerrornode.create;
  1944. end;
  1945. postfixoperators(p1,again);
  1946. end;
  1947. _INTCONST :
  1948. begin
  1949. {$ifdef cpu64}
  1950. { when already running under 64bit must read int64 constant, because reading
  1951. cardinal first will also succeed (code=0) for values > maxcardinal, because
  1952. range checking is off by default (PFV) }
  1953. val(pattern,ic,code);
  1954. if code=0 then
  1955. begin
  1956. consume(_INTCONST);
  1957. int_to_type(ic,htype);
  1958. p1:=cordconstnode.create(ic,htype,true);
  1959. end
  1960. else
  1961. begin
  1962. { try qword next }
  1963. val(pattern,qc,code);
  1964. if code=0 then
  1965. begin
  1966. consume(_INTCONST);
  1967. htype:=u64inttype;
  1968. p1:=cordconstnode.create(qc,htype,true);
  1969. end;
  1970. end;
  1971. {$else}
  1972. { try cardinal first }
  1973. val(pattern,card,code);
  1974. if code=0 then
  1975. begin
  1976. consume(_INTCONST);
  1977. int_to_type(card,htype);
  1978. p1:=cordconstnode.create(card,htype,true);
  1979. end
  1980. else
  1981. begin
  1982. { then longint }
  1983. val(pattern,l,code);
  1984. if code = 0 then
  1985. begin
  1986. consume(_INTCONST);
  1987. int_to_type(l,htype);
  1988. p1:=cordconstnode.create(l,htype,true);
  1989. end
  1990. else
  1991. begin
  1992. { then int64 }
  1993. val(pattern,ic,code);
  1994. if code=0 then
  1995. begin
  1996. consume(_INTCONST);
  1997. int_to_type(ic,htype);
  1998. p1:=cordconstnode.create(ic,htype,true);
  1999. end
  2000. else
  2001. begin
  2002. { try qword next }
  2003. val(pattern,qc,code);
  2004. if code=0 then
  2005. begin
  2006. consume(_INTCONST);
  2007. htype:=u64inttype;
  2008. p1:=cordconstnode.create(tconstexprint(qc),htype,true);
  2009. end;
  2010. end;
  2011. end;
  2012. end;
  2013. {$endif}
  2014. if code<>0 then
  2015. begin
  2016. { finally float }
  2017. val(pattern,d,code);
  2018. if code<>0 then
  2019. begin
  2020. Message(parser_e_invalid_integer);
  2021. consume(_INTCONST);
  2022. l:=1;
  2023. p1:=cordconstnode.create(l,sinttype,true);
  2024. end
  2025. else
  2026. begin
  2027. consume(_INTCONST);
  2028. p1:=crealconstnode.create(d,pbestrealtype^);
  2029. end;
  2030. end;
  2031. end;
  2032. _REALNUMBER :
  2033. begin
  2034. val(pattern,d,code);
  2035. if code<>0 then
  2036. begin
  2037. Message(parser_e_error_in_real);
  2038. d:=1.0;
  2039. end;
  2040. consume(_REALNUMBER);
  2041. p1:=crealconstnode.create(d,pbestrealtype^);
  2042. end;
  2043. _STRING :
  2044. begin
  2045. string_dec(htype);
  2046. { STRING can be also a type cast }
  2047. if try_to_consume(_LKLAMMER) then
  2048. begin
  2049. p1:=comp_expr(true);
  2050. consume(_RKLAMMER);
  2051. p1:=ctypeconvnode.create_explicit(p1,htype);
  2052. { handle postfix operators here e.g. string(a)[10] }
  2053. again:=true;
  2054. postfixoperators(p1,again);
  2055. end
  2056. else
  2057. p1:=ctypenode.create(htype);
  2058. end;
  2059. _FILE :
  2060. begin
  2061. htype:=cfiletype;
  2062. consume(_FILE);
  2063. { FILE can be also a type cast }
  2064. if try_to_consume(_LKLAMMER) then
  2065. begin
  2066. p1:=comp_expr(true);
  2067. consume(_RKLAMMER);
  2068. p1:=ctypeconvnode.create_explicit(p1,htype);
  2069. { handle postfix operators here e.g. string(a)[10] }
  2070. again:=true;
  2071. postfixoperators(p1,again);
  2072. end
  2073. else
  2074. begin
  2075. p1:=ctypenode.create(htype);
  2076. end;
  2077. end;
  2078. _CSTRING :
  2079. begin
  2080. p1:=cstringconstnode.createstr(pattern,st_conststring);
  2081. consume(_CSTRING);
  2082. end;
  2083. _CCHAR :
  2084. begin
  2085. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2086. consume(_CCHAR);
  2087. end;
  2088. _CWSTRING:
  2089. begin
  2090. p1:=cstringconstnode.createwstr(patternw);
  2091. consume(_CWSTRING);
  2092. end;
  2093. _CWCHAR:
  2094. begin
  2095. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2096. consume(_CWCHAR);
  2097. end;
  2098. _KLAMMERAFFE :
  2099. begin
  2100. consume(_KLAMMERAFFE);
  2101. got_addrn:=true;
  2102. { support both @<x> and @(<x>) }
  2103. if try_to_consume(_LKLAMMER) then
  2104. begin
  2105. p1:=factor(true);
  2106. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2107. begin
  2108. again:=true;
  2109. postfixoperators(p1,again);
  2110. end;
  2111. consume(_RKLAMMER);
  2112. end
  2113. else
  2114. p1:=factor(true);
  2115. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2116. begin
  2117. again:=true;
  2118. postfixoperators(p1,again);
  2119. end;
  2120. got_addrn:=false;
  2121. p1:=caddrnode.create(p1);
  2122. if cs_typed_addresses in aktlocalswitches then
  2123. include(p1.flags,nf_typedaddr);
  2124. { Store the procvar that we are expecting, the
  2125. addrn will use the information to find the correct
  2126. procdef or it will return an error }
  2127. if assigned(getprocvardef) and
  2128. (taddrnode(p1).left.nodetype = loadn) then
  2129. taddrnode(p1).getprocvardef:=getprocvardef;
  2130. end;
  2131. _LKLAMMER :
  2132. begin
  2133. consume(_LKLAMMER);
  2134. p1:=comp_expr(true);
  2135. consume(_RKLAMMER);
  2136. { it's not a good solution }
  2137. { but (a+b)^ makes some problems }
  2138. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2139. begin
  2140. again:=true;
  2141. postfixoperators(p1,again);
  2142. end;
  2143. end;
  2144. _LECKKLAMMER :
  2145. begin
  2146. consume(_LECKKLAMMER);
  2147. p1:=factor_read_set;
  2148. consume(_RECKKLAMMER);
  2149. end;
  2150. _PLUS :
  2151. begin
  2152. consume(_PLUS);
  2153. p1:=factor(false);
  2154. end;
  2155. _MINUS :
  2156. begin
  2157. consume(_MINUS);
  2158. if (token = _INTCONST) then
  2159. begin
  2160. { ugly hack, but necessary to be able to parse }
  2161. { -9223372036854775808 as int64 (JM) }
  2162. pattern := '-'+pattern;
  2163. p1:=sub_expr(oppower,false);
  2164. { -1 ** 4 should be - (1 ** 4) and not
  2165. (-1) ** 4
  2166. This was the reason of tw0869.pp test failure PM }
  2167. if p1.nodetype=starstarn then
  2168. begin
  2169. if tbinarynode(p1).left.nodetype=ordconstn then
  2170. begin
  2171. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2172. p1:=cunaryminusnode.create(p1);
  2173. end
  2174. else if tbinarynode(p1).left.nodetype=realconstn then
  2175. begin
  2176. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2177. p1:=cunaryminusnode.create(p1);
  2178. end
  2179. else
  2180. internalerror(20021029);
  2181. end;
  2182. end
  2183. else
  2184. begin
  2185. p1:=sub_expr(oppower,false);
  2186. p1:=cunaryminusnode.create(p1);
  2187. end;
  2188. end;
  2189. _OP_NOT :
  2190. begin
  2191. consume(_OP_NOT);
  2192. p1:=factor(false);
  2193. p1:=cnotnode.create(p1);
  2194. end;
  2195. _TRUE :
  2196. begin
  2197. consume(_TRUE);
  2198. p1:=cordconstnode.create(1,booltype,false);
  2199. end;
  2200. _FALSE :
  2201. begin
  2202. consume(_FALSE);
  2203. p1:=cordconstnode.create(0,booltype,false);
  2204. end;
  2205. _NIL :
  2206. begin
  2207. consume(_NIL);
  2208. p1:=cnilnode.create;
  2209. { It's really ugly code nil^, but delphi allows it }
  2210. if token in [_CARET] then
  2211. begin
  2212. again:=true;
  2213. postfixoperators(p1,again);
  2214. end;
  2215. end;
  2216. else
  2217. begin
  2218. p1:=cerrornode.create;
  2219. consume(token);
  2220. Message(parser_e_illegal_expression);
  2221. end;
  2222. end;
  2223. { generate error node if no node is created }
  2224. if not assigned(p1) then
  2225. begin
  2226. {$ifdef EXTDEBUG}
  2227. Comment(V_Warning,'factor: p1=nil');
  2228. {$endif}
  2229. p1:=cerrornode.create;
  2230. end;
  2231. { get the resulttype for the node }
  2232. if (not assigned(p1.resulttype.def)) then
  2233. do_resulttypepass(p1);
  2234. factor:=p1;
  2235. check_tokenpos;
  2236. end;
  2237. {$ifdef fpc}
  2238. {$maxfpuregisters default}
  2239. {$endif fpc}
  2240. {****************************************************************************
  2241. Sub_Expr
  2242. ****************************************************************************}
  2243. const
  2244. { Warning these stay be ordered !! }
  2245. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2246. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2247. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2248. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2249. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2250. [_STARSTAR] );
  2251. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2252. {Reads a subexpression while the operators are of the current precedence
  2253. level, or any higher level. Replaces the old term, simpl_expr and
  2254. simpl2_expr.}
  2255. var
  2256. p1,p2 : tnode;
  2257. oldt : Ttoken;
  2258. filepos : tfileposinfo;
  2259. begin
  2260. if pred_level=highest_precedence then
  2261. p1:=factor(false)
  2262. else
  2263. p1:=sub_expr(succ(pred_level),true);
  2264. repeat
  2265. if (token in operator_levels[pred_level]) and
  2266. ((token<>_EQUAL) or accept_equal) then
  2267. begin
  2268. oldt:=token;
  2269. filepos:=akttokenpos;
  2270. consume(token);
  2271. if pred_level=highest_precedence then
  2272. p2:=factor(false)
  2273. else
  2274. p2:=sub_expr(succ(pred_level),true);
  2275. case oldt of
  2276. _PLUS :
  2277. p1:=caddnode.create(addn,p1,p2);
  2278. _MINUS :
  2279. p1:=caddnode.create(subn,p1,p2);
  2280. _STAR :
  2281. p1:=caddnode.create(muln,p1,p2);
  2282. _SLASH :
  2283. p1:=caddnode.create(slashn,p1,p2);
  2284. _EQUAL :
  2285. p1:=caddnode.create(equaln,p1,p2);
  2286. _GT :
  2287. p1:=caddnode.create(gtn,p1,p2);
  2288. _LT :
  2289. p1:=caddnode.create(ltn,p1,p2);
  2290. _GTE :
  2291. p1:=caddnode.create(gten,p1,p2);
  2292. _LTE :
  2293. p1:=caddnode.create(lten,p1,p2);
  2294. _SYMDIF :
  2295. p1:=caddnode.create(symdifn,p1,p2);
  2296. _STARSTAR :
  2297. p1:=caddnode.create(starstarn,p1,p2);
  2298. _OP_AS :
  2299. p1:=casnode.create(p1,p2);
  2300. _OP_IN :
  2301. p1:=cinnode.create(p1,p2);
  2302. _OP_IS :
  2303. p1:=cisnode.create(p1,p2);
  2304. _OP_OR,
  2305. _PIPE {macpas only} :
  2306. p1:=caddnode.create(orn,p1,p2);
  2307. _OP_AND,
  2308. _AMPERSAND {macpas only} :
  2309. p1:=caddnode.create(andn,p1,p2);
  2310. _OP_DIV :
  2311. p1:=cmoddivnode.create(divn,p1,p2);
  2312. _OP_NOT :
  2313. p1:=cnotnode.create(p1);
  2314. _OP_MOD :
  2315. p1:=cmoddivnode.create(modn,p1,p2);
  2316. _OP_SHL :
  2317. p1:=cshlshrnode.create(shln,p1,p2);
  2318. _OP_SHR :
  2319. p1:=cshlshrnode.create(shrn,p1,p2);
  2320. _OP_XOR :
  2321. p1:=caddnode.create(xorn,p1,p2);
  2322. _ASSIGNMENT :
  2323. p1:=cassignmentnode.create(p1,p2);
  2324. _CARET :
  2325. p1:=caddnode.create(caretn,p1,p2);
  2326. _UNEQUAL :
  2327. p1:=caddnode.create(unequaln,p1,p2);
  2328. end;
  2329. p1.fileinfo:=filepos;
  2330. end
  2331. else
  2332. break;
  2333. until false;
  2334. sub_expr:=p1;
  2335. end;
  2336. function comp_expr(accept_equal : boolean):tnode;
  2337. var
  2338. oldafterassignment : boolean;
  2339. p1 : tnode;
  2340. begin
  2341. oldafterassignment:=afterassignment;
  2342. afterassignment:=true;
  2343. p1:=sub_expr(opcompare,accept_equal);
  2344. { get the resulttype for this expression }
  2345. if not assigned(p1.resulttype.def) then
  2346. do_resulttypepass(p1);
  2347. afterassignment:=oldafterassignment;
  2348. comp_expr:=p1;
  2349. end;
  2350. function expr : tnode;
  2351. var
  2352. p1,p2 : tnode;
  2353. oldafterassignment : boolean;
  2354. oldp1 : tnode;
  2355. filepos : tfileposinfo;
  2356. begin
  2357. oldafterassignment:=afterassignment;
  2358. p1:=sub_expr(opcompare,true);
  2359. { get the resulttype for this expression }
  2360. if not assigned(p1.resulttype.def) then
  2361. do_resulttypepass(p1);
  2362. filepos:=akttokenpos;
  2363. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2364. afterassignment:=true;
  2365. oldp1:=p1;
  2366. case token of
  2367. _POINTPOINT :
  2368. begin
  2369. consume(_POINTPOINT);
  2370. p2:=sub_expr(opcompare,true);
  2371. p1:=crangenode.create(p1,p2);
  2372. end;
  2373. _ASSIGNMENT :
  2374. begin
  2375. consume(_ASSIGNMENT);
  2376. if (p1.resulttype.def.deftype=procvardef) then
  2377. getprocvardef:=tprocvardef(p1.resulttype.def);
  2378. p2:=sub_expr(opcompare,true);
  2379. if assigned(getprocvardef) then
  2380. handle_procvar(getprocvardef,p2);
  2381. getprocvardef:=nil;
  2382. p1:=cassignmentnode.create(p1,p2);
  2383. end;
  2384. _PLUSASN :
  2385. begin
  2386. consume(_PLUSASN);
  2387. p2:=sub_expr(opcompare,true);
  2388. p1:=gen_c_style_operator(addn,p1,p2);
  2389. end;
  2390. _MINUSASN :
  2391. begin
  2392. consume(_MINUSASN);
  2393. p2:=sub_expr(opcompare,true);
  2394. p1:=gen_c_style_operator(subn,p1,p2);
  2395. end;
  2396. _STARASN :
  2397. begin
  2398. consume(_STARASN );
  2399. p2:=sub_expr(opcompare,true);
  2400. p1:=gen_c_style_operator(muln,p1,p2);
  2401. end;
  2402. _SLASHASN :
  2403. begin
  2404. consume(_SLASHASN );
  2405. p2:=sub_expr(opcompare,true);
  2406. p1:=gen_c_style_operator(slashn,p1,p2);
  2407. end;
  2408. end;
  2409. { get the resulttype for this expression }
  2410. if not assigned(p1.resulttype.def) then
  2411. do_resulttypepass(p1);
  2412. afterassignment:=oldafterassignment;
  2413. if p1<>oldp1 then
  2414. p1.fileinfo:=filepos;
  2415. expr:=p1;
  2416. end;
  2417. {$ifdef int64funcresok}
  2418. function get_intconst:TConstExprInt;
  2419. {$else int64funcresok}
  2420. function get_intconst:longint;
  2421. {$endif int64funcresok}
  2422. {Reads an expression, tries to evalute it and check if it is an integer
  2423. constant. Then the constant is returned.}
  2424. var
  2425. p:tnode;
  2426. begin
  2427. result:=0;
  2428. p:=comp_expr(true);
  2429. if not codegenerror then
  2430. begin
  2431. if (p.nodetype<>ordconstn) or
  2432. not(is_integer(p.resulttype.def)) then
  2433. Message(parser_e_illegal_expression)
  2434. else
  2435. result:=tordconstnode(p).value;
  2436. end;
  2437. p.free;
  2438. end;
  2439. function get_stringconst:string;
  2440. {Reads an expression, tries to evaluate it and checks if it is a string
  2441. constant. Then the constant is returned.}
  2442. var
  2443. p:tnode;
  2444. begin
  2445. get_stringconst:='';
  2446. p:=comp_expr(true);
  2447. if p.nodetype<>stringconstn then
  2448. begin
  2449. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2450. get_stringconst:=char(tordconstnode(p).value)
  2451. else
  2452. Message(parser_e_illegal_expression);
  2453. end
  2454. else
  2455. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2456. p.free;
  2457. end;
  2458. end.