pexpr.pas 97 KB

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