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