pexpr.pas 98 KB

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