pexpr.pas 87 KB

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