pexpr.pas 100 KB

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