pexpr.pas 89 KB

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