pexpr.pas 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. interface
  21. uses symtable,tree;
  22. { reads a whole expression }
  23. function expr : ptree;
  24. { reads an expression without assignements and .. }
  25. function comp_expr(accept_equal : boolean):Ptree;
  26. { reads a single factor }
  27. function factor(getaddr : boolean) : ptree;
  28. { the ID token has to be consumed before calling this function }
  29. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : ptree;
  30. var pd : pdef;var again : boolean);
  31. function get_intconst:longint;
  32. function get_stringconst:string;
  33. implementation
  34. uses
  35. {$ifdef delphi}
  36. sysutils,
  37. {$else}
  38. strings,
  39. {$endif}
  40. globtype,systems,tokens,
  41. cutils,cobjects,globals,scanner,
  42. symconst,aasm,htypechk,
  43. {$ifdef newcg}
  44. cgbase,
  45. {$else}
  46. hcodegen,
  47. {$endif}
  48. types,verbose,
  49. {$ifndef newcg}
  50. {$ifndef CG11}
  51. tccal,
  52. {$endif}
  53. {$endif newcg}
  54. pass_1,
  55. { parser specific stuff }
  56. pbase,ptype,
  57. { processor specific stuff }
  58. cpubase,cpuinfo;
  59. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  60. read as - (1**4) and not (-1)**4 PM }
  61. type
  62. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  63. const
  64. highest_precedence = oppower;
  65. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;forward;
  66. const
  67. allow_type : boolean = true;
  68. got_addrn : boolean = false;
  69. auto_inherited : boolean = false;
  70. function parse_paras(__colon,in_prop_paras : boolean) : ptree;
  71. var
  72. p1,p2 : ptree;
  73. end_of_paras : ttoken;
  74. begin
  75. if in_prop_paras then
  76. end_of_paras:=_RECKKLAMMER
  77. else
  78. end_of_paras:=_RKLAMMER;
  79. if token=end_of_paras then
  80. begin
  81. parse_paras:=nil;
  82. exit;
  83. end;
  84. p2:=nil;
  85. inc(parsing_para_level);
  86. while true do
  87. begin
  88. p1:=comp_expr(true);
  89. p2:=gencallparanode(p1,p2);
  90. { it's for the str(l:5,s); }
  91. if __colon and (token=_COLON) then
  92. begin
  93. consume(_COLON);
  94. p1:=comp_expr(true);
  95. p2:=gencallparanode(p1,p2);
  96. p2^.is_colon_para:=true;
  97. if token=_COLON then
  98. begin
  99. consume(_COLON);
  100. p1:=comp_expr(true);
  101. p2:=gencallparanode(p1,p2);
  102. p2^.is_colon_para:=true;
  103. end
  104. end;
  105. if token=_COMMA then
  106. consume(_COMMA)
  107. else
  108. break;
  109. end;
  110. dec(parsing_para_level);
  111. parse_paras:=p2;
  112. end;
  113. procedure check_tp_procvar(var p : ptree);
  114. var
  115. p1 : ptree;
  116. begin
  117. if (m_tp_procvar in aktmodeswitches) and
  118. (not got_addrn) and
  119. (not in_args) and
  120. (p^.treetype=loadn) then
  121. begin
  122. { support if procvar then for tp7 and many other expression like this }
  123. do_firstpass(p);
  124. set_varstate(p,false);
  125. { reset varstateset to maybe set used state later web bug769 PM }
  126. unset_varstate(p);
  127. if not(getprocvar) and (p^.resulttype^.deftype=procvardef) then
  128. begin
  129. p1:=gencallnode(nil,nil);
  130. p1^.right:=p;
  131. p1^.resulttype:=pprocvardef(p^.resulttype)^.rettype.def;
  132. firstpass(p1);
  133. p:=p1;
  134. end;
  135. end;
  136. end;
  137. function statement_syssym(l : longint;var pd : pdef) : ptree;
  138. var
  139. p1,p2,paras : ptree;
  140. prev_in_args : boolean;
  141. begin
  142. prev_in_args:=in_args;
  143. case l of
  144. in_ord_x :
  145. begin
  146. consume(_LKLAMMER);
  147. in_args:=true;
  148. p1:=comp_expr(true);
  149. consume(_RKLAMMER);
  150. do_firstpass(p1);
  151. p1:=geninlinenode(in_ord_x,false,p1);
  152. do_firstpass(p1);
  153. statement_syssym := p1;
  154. pd:=p1^.resulttype;
  155. end;
  156. in_break :
  157. begin
  158. statement_syssym:=genzeronode(breakn);
  159. pd:=voiddef;
  160. end;
  161. in_continue :
  162. begin
  163. statement_syssym:=genzeronode(continuen);
  164. pd:=voiddef;
  165. end;
  166. in_typeof_x :
  167. begin
  168. consume(_LKLAMMER);
  169. in_args:=true;
  170. {allow_type:=true;}
  171. p1:=comp_expr(true);
  172. {allow_type:=false;}
  173. consume(_RKLAMMER);
  174. pd:=voidpointerdef;
  175. if p1^.treetype=typen then
  176. begin
  177. if (p1^.typenodetype=nil) then
  178. begin
  179. Message(type_e_mismatch);
  180. statement_syssym:=genzeronode(errorn);
  181. end
  182. else
  183. if p1^.typenodetype^.deftype=objectdef then
  184. begin
  185. { we can use resulttype in pass_2 (PM) }
  186. p1^.resulttype:=p1^.typenodetype;
  187. statement_syssym:=geninlinenode(in_typeof_x,false,p1);
  188. end
  189. else
  190. begin
  191. Message(type_e_mismatch);
  192. disposetree(p1);
  193. statement_syssym:=genzeronode(errorn);
  194. end;
  195. end
  196. else { not a type node }
  197. begin
  198. do_firstpass(p1);
  199. set_varstate(p1,false);
  200. if (p1^.resulttype=nil) then
  201. begin
  202. Message(type_e_mismatch);
  203. disposetree(p1);
  204. statement_syssym:=genzeronode(errorn)
  205. end
  206. else
  207. if p1^.resulttype^.deftype=objectdef then
  208. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  209. else
  210. begin
  211. Message(type_e_mismatch);
  212. statement_syssym:=genzeronode(errorn);
  213. disposetree(p1);
  214. end;
  215. end;
  216. end;
  217. in_sizeof_x :
  218. begin
  219. consume(_LKLAMMER);
  220. in_args:=true;
  221. {allow_type:=true;}
  222. p1:=comp_expr(true);
  223. {allow_type:=false; }
  224. consume(_RKLAMMER);
  225. pd:=s32bitdef;
  226. if p1^.treetype=typen then
  227. begin
  228. statement_syssym:=genordinalconstnode(p1^.typenodetype^.size,pd);
  229. { p1 not needed !}
  230. disposetree(p1);
  231. end
  232. else
  233. begin
  234. do_firstpass(p1);
  235. if ((p1^.resulttype^.deftype=objectdef) and
  236. (oo_has_constructor in pobjectdef(p1^.resulttype)^.objectoptions)) or
  237. is_open_array(p1^.resulttype) or
  238. is_open_string(p1^.resulttype) then
  239. statement_syssym:=geninlinenode(in_sizeof_x,false,p1)
  240. else
  241. begin
  242. statement_syssym:=genordinalconstnode(p1^.resulttype^.size,pd);
  243. { p1 not needed !}
  244. disposetree(p1);
  245. end;
  246. end;
  247. end;
  248. in_typeinfo_x :
  249. begin
  250. consume(_LKLAMMER);
  251. in_args:=true;
  252. {allow_type:=true;}
  253. p1:=comp_expr(true);
  254. do_firstpass(p1);
  255. {allow_type:=false; }
  256. if p1^.treetype<>typen then
  257. begin
  258. disposetree(p1);
  259. p1:=genzeronode(errorn);
  260. Message(parser_e_illegal_parameter_list);
  261. end;
  262. consume(_RKLAMMER);
  263. p2:=gencallparanode(p1,nil);
  264. p2:=geninlinenode(in_typeinfo_x,false,p2);
  265. pd:=voidpointerdef;
  266. statement_syssym:=p2;
  267. end;
  268. in_assigned_x :
  269. begin
  270. consume(_LKLAMMER);
  271. in_args:=true;
  272. p1:=comp_expr(true);
  273. do_firstpass(p1);
  274. if not codegenerror then
  275. begin
  276. case p1^.resulttype^.deftype of
  277. pointerdef,
  278. procvardef,
  279. classrefdef : ;
  280. objectdef :
  281. if not(pobjectdef(p1^.resulttype)^.is_class) then
  282. Message(parser_e_illegal_parameter_list);
  283. else
  284. Message(parser_e_illegal_parameter_list);
  285. end;
  286. end;
  287. p2:=gencallparanode(p1,nil);
  288. p2:=geninlinenode(in_assigned_x,false,p2);
  289. consume(_RKLAMMER);
  290. pd:=booldef;
  291. statement_syssym:=p2;
  292. end;
  293. in_ofs_x :
  294. begin
  295. consume(_LKLAMMER);
  296. in_args:=true;
  297. p1:=comp_expr(true);
  298. p1:=gensinglenode(addrn,p1);
  299. do_firstpass(p1);
  300. { Ofs() returns a longint, not a pointer }
  301. p1^.resulttype:=u32bitdef;
  302. pd:=p1^.resulttype;
  303. consume(_RKLAMMER);
  304. statement_syssym:=p1;
  305. end;
  306. in_addr_x :
  307. begin
  308. consume(_LKLAMMER);
  309. in_args:=true;
  310. p1:=comp_expr(true);
  311. p1:=gensinglenode(addrn,p1);
  312. do_firstpass(p1);
  313. pd:=p1^.resulttype;
  314. consume(_RKLAMMER);
  315. statement_syssym:=p1;
  316. end;
  317. in_seg_x :
  318. begin
  319. consume(_LKLAMMER);
  320. in_args:=true;
  321. p1:=comp_expr(true);
  322. do_firstpass(p1);
  323. set_varstate(p1,false);
  324. if p1^.location.loc<>LOC_REFERENCE then
  325. Message(cg_e_illegal_expression);
  326. p1:=genordinalconstnode(0,s32bitdef);
  327. pd:=s32bitdef;
  328. consume(_RKLAMMER);
  329. statement_syssym:=p1;
  330. end;
  331. in_high_x,
  332. in_low_x :
  333. begin
  334. consume(_LKLAMMER);
  335. in_args:=true;
  336. {allow_type:=true;}
  337. p1:=comp_expr(true);
  338. {allow_type:=false;}
  339. do_firstpass(p1);
  340. if p1^.treetype=typen then
  341. p1^.resulttype:=p1^.typenodetype;
  342. p2:=geninlinenode(l,false,p1);
  343. consume(_RKLAMMER);
  344. pd:=s32bitdef;
  345. statement_syssym:=p2;
  346. end;
  347. in_succ_x,
  348. in_pred_x :
  349. begin
  350. consume(_LKLAMMER);
  351. in_args:=true;
  352. p1:=comp_expr(true);
  353. do_firstpass(p1);
  354. p2:=geninlinenode(l,false,p1);
  355. consume(_RKLAMMER);
  356. pd:=p1^.resulttype;
  357. statement_syssym:=p2;
  358. end;
  359. in_inc_x,
  360. in_dec_x :
  361. begin
  362. consume(_LKLAMMER);
  363. in_args:=true;
  364. p1:=comp_expr(true);
  365. if token=_COMMA then
  366. begin
  367. consume(_COMMA);
  368. p2:=gencallparanode(comp_expr(true),nil);
  369. end
  370. else
  371. p2:=nil;
  372. p2:=gencallparanode(p1,p2);
  373. statement_syssym:=geninlinenode(l,false,p2);
  374. consume(_RKLAMMER);
  375. pd:=voiddef;
  376. end;
  377. in_concat_x :
  378. begin
  379. consume(_LKLAMMER);
  380. in_args:=true;
  381. p2:=nil;
  382. while true do
  383. begin
  384. p1:=comp_expr(true);
  385. do_firstpass(p1);
  386. set_varstate(p1,true);
  387. if not((p1^.resulttype^.deftype=stringdef) or
  388. ((p1^.resulttype^.deftype=orddef) and
  389. (porddef(p1^.resulttype)^.typ=uchar))) then
  390. Message(parser_e_illegal_parameter_list);
  391. if p2<>nil then
  392. p2:=gennode(addn,p2,p1)
  393. else
  394. p2:=p1;
  395. if token=_COMMA then
  396. consume(_COMMA)
  397. else
  398. break;
  399. end;
  400. consume(_RKLAMMER);
  401. pd:=cshortstringdef;
  402. statement_syssym:=p2;
  403. end;
  404. in_read_x,
  405. in_readln_x :
  406. begin
  407. if token=_LKLAMMER then
  408. begin
  409. consume(_LKLAMMER);
  410. in_args:=true;
  411. paras:=parse_paras(false,false);
  412. consume(_RKLAMMER);
  413. end
  414. else
  415. paras:=nil;
  416. pd:=voiddef;
  417. p1:=geninlinenode(l,false,paras);
  418. do_firstpass(p1);
  419. statement_syssym := p1;
  420. end;
  421. in_write_x,
  422. in_writeln_x :
  423. begin
  424. if token=_LKLAMMER then
  425. begin
  426. consume(_LKLAMMER);
  427. in_args:=true;
  428. paras:=parse_paras(true,false);
  429. consume(_RKLAMMER);
  430. end
  431. else
  432. paras:=nil;
  433. pd:=voiddef;
  434. p1 := geninlinenode(l,false,paras);
  435. do_firstpass(p1);
  436. statement_syssym := p1;
  437. end;
  438. in_str_x_string :
  439. begin
  440. consume(_LKLAMMER);
  441. in_args:=true;
  442. paras:=parse_paras(true,false);
  443. consume(_RKLAMMER);
  444. p1 := geninlinenode(l,false,paras);
  445. do_firstpass(p1);
  446. statement_syssym := p1;
  447. pd:=voiddef;
  448. end;
  449. in_val_x:
  450. Begin
  451. consume(_LKLAMMER);
  452. in_args := true;
  453. p1:= gencallparanode(comp_expr(true), nil);
  454. consume(_COMMA);
  455. p2 := gencallparanode(comp_expr(true),p1);
  456. if (token = _COMMA) then
  457. Begin
  458. consume(_COMMA);
  459. p2 := gencallparanode(comp_expr(true),p2)
  460. End;
  461. consume(_RKLAMMER);
  462. p2 := geninlinenode(l,false,p2);
  463. do_firstpass(p2);
  464. statement_syssym := p2;
  465. pd := voiddef;
  466. End;
  467. in_include_x_y,
  468. in_exclude_x_y :
  469. begin
  470. consume(_LKLAMMER);
  471. in_args:=true;
  472. p1:=comp_expr(true);
  473. consume(_COMMA);
  474. p2:=comp_expr(true);
  475. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  476. consume(_RKLAMMER);
  477. pd:=voiddef;
  478. end;
  479. in_assert_x_y :
  480. begin
  481. consume(_LKLAMMER);
  482. in_args:=true;
  483. p1:=comp_expr(true);
  484. if token=_COMMA then
  485. begin
  486. consume(_COMMA);
  487. p2:=comp_expr(true);
  488. end
  489. else
  490. begin
  491. { then insert an empty string }
  492. p2:=genstringconstnode('',st_default);
  493. end;
  494. statement_syssym:=geninlinenode(l,false,gencallparanode(p1,gencallparanode(p2,nil)));
  495. consume(_RKLAMMER);
  496. pd:=voiddef;
  497. end;
  498. else
  499. internalerror(15);
  500. end;
  501. in_args:=prev_in_args;
  502. end;
  503. { reads the parameter for a subroutine call }
  504. procedure do_proc_call(getaddr : boolean;var again : boolean;var p1:Ptree;var pd:Pdef);
  505. var
  506. prev_in_args : boolean;
  507. prevafterassn : boolean;
  508. hs,hs1 : pvarsym;
  509. st : psymtable;
  510. p2 : ptree;
  511. begin
  512. prev_in_args:=in_args;
  513. prevafterassn:=afterassignment;
  514. afterassignment:=false;
  515. { want we only determine the address of }
  516. { a subroutine ? }
  517. if not(getaddr) then
  518. begin
  519. if auto_inherited then
  520. begin
  521. st:=symtablestack;
  522. while assigned(st) and (st^.symtabletype<>parasymtable) do
  523. st:=st^.next;
  524. p2:=nil;
  525. if assigned(st) then
  526. begin
  527. hs:=pvarsym(st^.symindex^.first);
  528. while assigned(hs) do
  529. begin
  530. if hs^.typ<>varsym then
  531. internalerror(54382953);
  532. { if there is a localcopy then use that }
  533. if assigned(hs^.localvarsym) then
  534. hs1:=hs^.localvarsym
  535. else
  536. hs1:=hs;
  537. p2:=gencallparanode(genloadnode(hs1,hs1^.owner),p2);
  538. hs:=pvarsym(hs^.indexnext);
  539. end;
  540. end
  541. else
  542. internalerror(54382954);
  543. p1^.left:=p2;
  544. end
  545. else
  546. begin
  547. if token=_LKLAMMER then
  548. begin
  549. consume(_LKLAMMER);
  550. in_args:=true;
  551. p1^.left:=parse_paras(false,false);
  552. consume(_RKLAMMER);
  553. end
  554. else
  555. p1^.left:=nil;
  556. end;
  557. { do firstpass because we need the }
  558. { result type }
  559. do_firstpass(p1);
  560. {set_var_state is handled inside firstcalln }
  561. end
  562. else
  563. begin
  564. { address operator @: }
  565. p1^.left:=nil;
  566. { forget pd }
  567. pd:=nil;
  568. if (p1^.symtableproc^.symtabletype=withsymtable) and
  569. (p1^.symtableproc^.defowner^.deftype=objectdef) then
  570. begin
  571. p1^.methodpointer:=getcopy(pwithsymtable(p1^.symtableproc)^.withrefnode);
  572. end
  573. else if not(assigned(p1^.methodpointer)) then
  574. begin
  575. { we must provide a method pointer, if it isn't given, }
  576. { it is self }
  577. if assigned(procinfo) then
  578. begin
  579. p1^.methodpointer:=genselfnode(procinfo^._class);
  580. p1^.methodpointer^.resulttype:=procinfo^._class;
  581. end
  582. else
  583. begin
  584. p1^.methodpointer:=genselfnode(nil);
  585. p1^.methodpointer^.resulttype:=nil;
  586. end;
  587. end;
  588. { no postfix operators }
  589. again:=false;
  590. end;
  591. pd:=p1^.resulttype;
  592. in_args:=prev_in_args;
  593. afterassignment:=prevafterassn;
  594. end;
  595. procedure handle_procvar(pv : pprocvardef;var p2 : ptree);
  596. procedure doconv(procvar : pprocvardef;var t : ptree);
  597. var
  598. hp : ptree;
  599. begin
  600. hp:=nil;
  601. if (proc_to_procvar_equal(pprocsym(t^.symtableentry)^.definition,procvar)) then
  602. begin
  603. if (po_methodpointer in procvar^.procoptions) then
  604. hp:=genloadmethodcallnode(pprocsym(t^.symtableprocentry),t^.symtable,getcopy(t^.methodpointer))
  605. else
  606. hp:=genloadcallnode(pprocsym(t^.symtableprocentry),t^.symtable);
  607. end;
  608. if assigned(hp) then
  609. begin
  610. disposetree(t);
  611. t:=hp;
  612. end;
  613. end;
  614. begin
  615. if (p2^.treetype=calln) then
  616. doconv(pv,p2)
  617. else
  618. if (p2^.treetype=typeconvn) and
  619. (p2^.left^.treetype=calln) then
  620. doconv(pv,p2^.left);
  621. end;
  622. { the following procedure handles the access to a property symbol }
  623. procedure handle_propertysym(sym : psym;st : psymtable;var p1 : ptree;
  624. var pd : pdef);
  625. var
  626. paras : ptree;
  627. p2 : ptree;
  628. plist : psymlistitem;
  629. begin
  630. paras:=nil;
  631. { property parameters? read them only if the property really }
  632. { has parameters }
  633. if ppo_hasparameters in ppropertysym(sym)^.propoptions then
  634. begin
  635. if token=_LECKKLAMMER then
  636. begin
  637. consume(_LECKKLAMMER);
  638. paras:=parse_paras(false,true);
  639. consume(_RECKKLAMMER);
  640. end;
  641. { indexed property }
  642. if (ppo_indexed in ppropertysym(sym)^.propoptions) then
  643. begin
  644. p2:=genordinalconstnode(ppropertysym(sym)^.index,ppropertysym(sym)^.indextype.def);
  645. paras:=gencallparanode(p2,paras);
  646. end;
  647. end;
  648. { we need only a write property if a := follows }
  649. { if not(afterassignment) and not(in_args) then }
  650. if token=_ASSIGNMENT then
  651. begin
  652. { write property: }
  653. { no result }
  654. pd:=voiddef;
  655. if not ppropertysym(sym)^.writeaccess^.empty then
  656. begin
  657. case ppropertysym(sym)^.writeaccess^.firstsym^.sym^.typ of
  658. procsym :
  659. begin
  660. { generate the method call }
  661. p1:=genmethodcallnode(pprocsym(ppropertysym(sym)^.writeaccess^.firstsym^.sym),st,p1);
  662. { we know the procedure to call, so
  663. force the usage of that procedure }
  664. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.writeaccess^.def);
  665. p1^.left:=paras;
  666. consume(_ASSIGNMENT);
  667. { read the expression }
  668. getprocvar:=ppropertysym(sym)^.proptype.def^.deftype=procvardef;
  669. p2:=comp_expr(true);
  670. if getprocvar then
  671. handle_procvar(pprocvardef(ppropertysym(sym)^.proptype.def),p2);
  672. p1^.left:=gencallparanode(p2,p1^.left);
  673. p1^.isproperty:=true;
  674. getprocvar:=false;
  675. end;
  676. varsym :
  677. begin
  678. if assigned(paras) then
  679. message(parser_e_no_paras_allowed);
  680. { subscribed access? }
  681. plist:=ppropertysym(sym)^.writeaccess^.firstsym;
  682. while assigned(plist) do
  683. begin
  684. if p1=nil then
  685. p1:=genloadnode(pvarsym(plist^.sym),st)
  686. else
  687. p1:=gensubscriptnode(pvarsym(plist^.sym),p1);
  688. plist:=plist^.next;
  689. end;
  690. p1^.isproperty:=true;
  691. consume(_ASSIGNMENT);
  692. { read the expression }
  693. p2:=comp_expr(true);
  694. p1:=gennode(assignn,p1,p2);
  695. end
  696. else
  697. begin
  698. p1:=genzeronode(errorn);
  699. Message(parser_e_no_procedure_to_access_property);
  700. end;
  701. end;
  702. end
  703. else
  704. begin
  705. p1:=genzeronode(errorn);
  706. Message(parser_e_no_procedure_to_access_property);
  707. end;
  708. end
  709. else
  710. begin
  711. { read property: }
  712. pd:=ppropertysym(sym)^.proptype.def;
  713. if not ppropertysym(sym)^.readaccess^.empty then
  714. begin
  715. case ppropertysym(sym)^.readaccess^.firstsym^.sym^.typ of
  716. varsym :
  717. begin
  718. if assigned(paras) then
  719. message(parser_e_no_paras_allowed);
  720. { subscribed access? }
  721. plist:=ppropertysym(sym)^.readaccess^.firstsym;
  722. while assigned(plist) do
  723. begin
  724. if p1=nil then
  725. p1:=genloadnode(pvarsym(plist^.sym),st)
  726. else
  727. p1:=gensubscriptnode(pvarsym(plist^.sym),p1);
  728. plist:=plist^.next;
  729. end;
  730. p1^.isproperty:=true;
  731. end;
  732. procsym :
  733. begin
  734. { generate the method call }
  735. p1:=genmethodcallnode(pprocsym(ppropertysym(sym)^.readaccess^.firstsym^.sym),st,p1);
  736. { we know the procedure to call, so
  737. force the usage of that procedure }
  738. p1^.procdefinition:=pprocdef(ppropertysym(sym)^.readaccess^.def);
  739. { insert paras }
  740. p1^.left:=paras;
  741. p1^.isproperty:=true;
  742. end
  743. else
  744. begin
  745. p1:=genzeronode(errorn);
  746. Message(type_e_mismatch);
  747. end;
  748. end;
  749. end
  750. else
  751. begin
  752. { error, no function to read property }
  753. p1:=genzeronode(errorn);
  754. Message(parser_e_no_procedure_to_access_property);
  755. end;
  756. end;
  757. end;
  758. { the ID token has to be consumed before calling this function }
  759. procedure do_member_read(getaddr : boolean;const sym : psym;var p1 : ptree;
  760. var pd : pdef;var again : boolean);
  761. var
  762. static_name : string;
  763. isclassref : boolean;
  764. objdef : pobjectdef;
  765. begin
  766. if sym=nil then
  767. begin
  768. { pattern is still valid unless
  769. there is another ID just after the ID of sym }
  770. Message1(sym_e_id_no_member,pattern);
  771. disposetree(p1);
  772. p1:=genzeronode(errorn);
  773. { try to clean up }
  774. pd:=generrordef;
  775. again:=false;
  776. end
  777. else
  778. begin
  779. objdef:=pobjectdef(sym^.owner^.defowner);
  780. isclassref:=(pd^.deftype=classrefdef);
  781. { check protected and private members }
  782. { please leave this code as it is, }
  783. { it has now the same behaviaor as TP/Delphi }
  784. if (sp_private in sym^.symoptions) and
  785. (objdef^.owner^.symtabletype=unitsymtable) then
  786. Message(parser_e_cant_access_private_member);
  787. if (sp_protected in sym^.symoptions) and
  788. (objdef^.owner^.symtabletype=unitsymtable) then
  789. begin
  790. if assigned(aktprocsym^.definition^._class) then
  791. begin
  792. if not aktprocsym^.definition^._class^.is_related(objdef) then
  793. Message(parser_e_cant_access_protected_member);
  794. end
  795. else
  796. Message(parser_e_cant_access_protected_member);
  797. end;
  798. { we assume, that only procsyms and varsyms are in an object }
  799. { symbol table, for classes, properties are allowed }
  800. case sym^.typ of
  801. procsym:
  802. begin
  803. p1:=genmethodcallnode(pprocsym(sym),sym^.owner,p1);
  804. do_proc_call(getaddr or
  805. (getprocvar and
  806. ((block_type=bt_const) or
  807. ((m_tp_procvar in aktmodeswitches) and
  808. proc_to_procvar_equal(pprocsym(sym)^.definition,getprocvardef)
  809. )
  810. )
  811. ),again,p1,pd);
  812. if (block_type=bt_const) and
  813. getprocvar then
  814. handle_procvar(getprocvardef,p1);
  815. { now we know the real method e.g. we can check for a class method }
  816. if isclassref and
  817. assigned(p1^.procdefinition) and
  818. not(po_classmethod in p1^.procdefinition^.procoptions) and
  819. not(p1^.procdefinition^.proctypeoption=potype_constructor) then
  820. Message(parser_e_only_class_methods_via_class_ref);
  821. end;
  822. varsym:
  823. begin
  824. if isclassref then
  825. Message(parser_e_only_class_methods_via_class_ref);
  826. if (sp_static in sym^.symoptions) then
  827. begin
  828. { static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  829. this is wrong for static field in with symtable (PM) }
  830. static_name:=lower(srsym^.owner^.name^)+'_'+sym^.name;
  831. getsym(static_name,true);
  832. disposetree(p1);
  833. p1:=genloadnode(pvarsym(srsym),srsymtable);
  834. end
  835. else
  836. p1:=gensubscriptnode(pvarsym(sym),p1);
  837. pd:=pvarsym(sym)^.vartype.def;
  838. end;
  839. propertysym:
  840. begin
  841. if isclassref then
  842. Message(parser_e_only_class_methods_via_class_ref);
  843. handle_propertysym(sym,srsymtable,p1,pd);
  844. end;
  845. else internalerror(16);
  846. end;
  847. end;
  848. end;
  849. {****************************************************************************
  850. Factor
  851. ****************************************************************************}
  852. {$ifdef fpc}
  853. {$maxfpuregisters 0}
  854. {$endif fpc}
  855. function factor(getaddr : boolean) : ptree;
  856. var
  857. l : longint;
  858. ic : TConstExprInt;
  859. oldp1,
  860. p1,p2,p3 : ptree;
  861. code : integer;
  862. pd,pd2 : pdef;
  863. possible_error,
  864. unit_specific,
  865. again : boolean;
  866. sym : psym;
  867. classh : pobjectdef;
  868. d : bestreal;
  869. hs,
  870. static_name : string;
  871. propsym : ppropertysym;
  872. filepos : tfileposinfo;
  873. {---------------------------------------------
  874. Is_func_ret
  875. ---------------------------------------------}
  876. function is_func_ret(sym : psym) : boolean;
  877. var
  878. p : pprocinfo;
  879. storesymtablestack : psymtable;
  880. begin
  881. is_func_ret:=false;
  882. if not assigned(procinfo) or
  883. ((sym^.typ<>funcretsym) and ((procinfo^.flags and pi_operator)=0)) then
  884. exit;
  885. p:=procinfo;
  886. while assigned(p) do
  887. begin
  888. { is this an access to a function result? Accessing _RESULT is
  889. always allowed and funcretn is generated }
  890. if assigned(p^.funcretsym) and
  891. ((pfuncretsym(sym)=p^.resultfuncretsym) or
  892. ((pfuncretsym(sym)=p^.funcretsym) or
  893. ((pvarsym(sym)=opsym) and ((p^.flags and pi_operator)<>0))) and
  894. (p^.returntype.def<>pdef(voiddef)) and
  895. (token<>_LKLAMMER) and
  896. (not ((m_tp in aktmodeswitches) and (afterassignment or in_args)))
  897. ) then
  898. begin
  899. if ((pvarsym(sym)=opsym) and
  900. ((p^.flags and pi_operator)<>0)) then
  901. inc(opsym^.refs);
  902. p1:=genzeronode(funcretn);
  903. pd:=p^.returntype.def;
  904. p1^.funcretprocinfo:=p;
  905. p1^.rettype.def:=pd;
  906. is_func_ret:=true;
  907. if p^.funcret_state=vs_declared then
  908. begin
  909. p^.funcret_state:=vs_declared_and_first_found;
  910. p1^.is_first_funcret:=true;
  911. end;
  912. exit;
  913. end;
  914. p:=p^.parent;
  915. end;
  916. { we must use the function call }
  917. if (sym^.typ=funcretsym) then
  918. begin
  919. storesymtablestack:=symtablestack;
  920. symtablestack:=srsymtable^.next;
  921. getsym(sym^.name,true);
  922. if srsym^.typ<>procsym then
  923. Message(cg_e_illegal_expression);
  924. symtablestack:=storesymtablestack;
  925. end;
  926. end;
  927. {---------------------------------------------
  928. Factor_read_id
  929. ---------------------------------------------}
  930. procedure factor_read_id;
  931. var
  932. pc : pchar;
  933. len : longint;
  934. begin
  935. { allow post fix operators }
  936. again:=true;
  937. begin
  938. if lastsymknown then
  939. begin
  940. srsym:=lastsrsym;
  941. srsymtable:=lastsrsymtable;
  942. lastsymknown:=false;
  943. end
  944. else
  945. getsym(pattern,true);
  946. consume(_ID);
  947. if not is_func_ret(srsym) then
  948. { else it's a normal symbol }
  949. begin
  950. { is it defined like UNIT.SYMBOL ? }
  951. if srsym^.typ=unitsym then
  952. begin
  953. consume(_POINT);
  954. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  955. unit_specific:=true;
  956. consume(_ID);
  957. end
  958. else
  959. unit_specific:=false;
  960. if not assigned(srsym) then
  961. Begin
  962. p1:=genzeronode(errorn);
  963. { try to clean up }
  964. pd:=generrordef;
  965. end
  966. else
  967. Begin
  968. { check semantics of private }
  969. if (srsym^.typ in [propertysym,procsym,varsym]) and
  970. (srsymtable^.symtabletype=objectsymtable) then
  971. begin
  972. if (sp_private in srsym^.symoptions) and
  973. (pobjectdef(srsym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  974. Message(parser_e_cant_access_private_member);
  975. end;
  976. case srsym^.typ of
  977. absolutesym : begin
  978. p1:=genloadnode(pvarsym(srsym),srsymtable);
  979. pd:=pabsolutesym(srsym)^.vartype.def;
  980. end;
  981. varsym : begin
  982. { are we in a class method ? }
  983. if (srsymtable^.symtabletype=objectsymtable) and
  984. assigned(aktprocsym) and
  985. (po_classmethod in aktprocsym^.definition^.procoptions) then
  986. Message(parser_e_only_class_methods);
  987. if (sp_static in srsym^.symoptions) then
  988. begin
  989. static_name:=lower(srsym^.owner^.name^)+'_'+srsym^.name;
  990. getsym(static_name,true);
  991. end;
  992. p1:=genloadnode(pvarsym(srsym),srsymtable);
  993. if pvarsym(srsym)^.varstate=vs_declared then
  994. begin
  995. p1^.is_first := true;
  996. { set special between first loaded until checked in firstpass }
  997. pvarsym(srsym)^.varstate:=vs_declared_and_first_found;
  998. end;
  999. pd:=pvarsym(srsym)^.vartype.def;
  1000. end;
  1001. typedconstsym : begin
  1002. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  1003. pd:=ptypedconstsym(srsym)^.typedconsttype.def;
  1004. end;
  1005. syssym : p1:=statement_syssym(psyssym(srsym)^.number,pd);
  1006. typesym : begin
  1007. pd:=ptypesym(srsym)^.restype.def;
  1008. if not assigned(pd) then
  1009. begin
  1010. pd:=generrordef;
  1011. again:=false;
  1012. end
  1013. else
  1014. begin
  1015. { if we read a type declaration }
  1016. { we have to return the type and }
  1017. { nothing else }
  1018. if block_type=bt_type then
  1019. begin
  1020. { we don't need sym reference when it's in the
  1021. current unit or system unit, because those
  1022. units are always loaded (PFV) }
  1023. if not(assigned(pd^.owner)) or
  1024. (pd^.owner^.unitid=0) or
  1025. (pd^.owner^.unitid=1) then
  1026. p1:=gentypenode(pd,nil)
  1027. else
  1028. p1:=gentypenode(pd,ptypesym(srsym));
  1029. { here we can also set resulttype !! }
  1030. p1^.resulttype:=pd;
  1031. pd:=voiddef;
  1032. end
  1033. else { not type block }
  1034. begin
  1035. if token=_LKLAMMER then
  1036. begin
  1037. consume(_LKLAMMER);
  1038. p1:=comp_expr(true);
  1039. consume(_RKLAMMER);
  1040. p1:=gentypeconvnode(p1,pd);
  1041. p1^.explizit:=true;
  1042. end
  1043. else { not LKLAMMER}
  1044. if (token=_POINT) and
  1045. (pd^.deftype=objectdef) and
  1046. not(pobjectdef(pd)^.is_class) then
  1047. begin
  1048. consume(_POINT);
  1049. if assigned(procinfo) and
  1050. assigned(procinfo^._class) and
  1051. not(getaddr) then
  1052. begin
  1053. if procinfo^._class^.is_related(pobjectdef(pd)) then
  1054. begin
  1055. p1:=gentypenode(pd,ptypesym(srsym));
  1056. p1^.resulttype:=pd;
  1057. { search also in inherited methods }
  1058. repeat
  1059. srsymtable:=pobjectdef(pd)^.symtable;
  1060. sym:=pvarsym(srsymtable^.search(pattern));
  1061. if assigned(sym) then
  1062. break;
  1063. pd:=pobjectdef(pd)^.childof;
  1064. until not assigned(pd);
  1065. consume(_ID);
  1066. do_member_read(false,sym,p1,pd,again);
  1067. end
  1068. else
  1069. begin
  1070. Message(parser_e_no_super_class);
  1071. pd:=generrordef;
  1072. again:=false;
  1073. end;
  1074. end
  1075. else
  1076. begin
  1077. { allows @TObject.Load }
  1078. { also allows static methods and variables }
  1079. p1:=genzeronode(typen);
  1080. p1^.resulttype:=pd;
  1081. { TP allows also @TMenu.Load if Load is only }
  1082. { defined in an anchestor class }
  1083. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  1084. if not assigned(sym) then
  1085. Message1(sym_e_id_no_member,pattern)
  1086. else if not(getaddr) and not(sp_static in sym^.symoptions) then
  1087. Message(sym_e_only_static_in_static)
  1088. else
  1089. begin
  1090. consume(_ID);
  1091. do_member_read(getaddr,sym,p1,pd,again);
  1092. end;
  1093. end;
  1094. end
  1095. else
  1096. begin
  1097. { class reference ? }
  1098. if (pd^.deftype=objectdef)
  1099. and pobjectdef(pd)^.is_class then
  1100. begin
  1101. p1:=gentypenode(pd,nil);
  1102. p1^.resulttype:=pd;
  1103. pd:=new(pclassrefdef,init(pd));
  1104. p1:=gensinglenode(loadvmtn,p1);
  1105. p1^.resulttype:=pd;
  1106. end
  1107. else
  1108. begin
  1109. { generate a type node }
  1110. { (for typeof etc) }
  1111. if allow_type then
  1112. begin
  1113. p1:=gentypenode(pd,nil);
  1114. { here we must use typenodetype explicitly !! PM
  1115. p1^.resulttype:=pd; }
  1116. pd:=voiddef;
  1117. end
  1118. else
  1119. Message(parser_e_no_type_not_allowed_here);
  1120. end;
  1121. end;
  1122. end;
  1123. end;
  1124. end;
  1125. enumsym : begin
  1126. p1:=genenumnode(penumsym(srsym));
  1127. pd:=p1^.resulttype;
  1128. end;
  1129. constsym : begin
  1130. case pconstsym(srsym)^.consttyp of
  1131. constint :
  1132. { do a very dirty trick to bootstrap this code }
  1133. if (pconstsym(srsym)^.value>=-(int64(2147483647)+int64(1))) and (pconstsym(srsym)^.value<=2147483647) then
  1134. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef)
  1135. else
  1136. p1:=genordinalconstnode(pconstsym(srsym)^.value,cs64bitdef);
  1137. conststring :
  1138. begin
  1139. len:=pconstsym(srsym)^.len;
  1140. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1141. len:=255;
  1142. getmem(pc,len+1);
  1143. move(pchar(tpointerord(pconstsym(srsym)^.value))^,pc^,len);
  1144. pc[len]:=#0;
  1145. p1:=genpcharconstnode(pc,len);
  1146. end;
  1147. constchar :
  1148. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1149. constreal :
  1150. p1:=genrealconstnode(pbestreal(tpointerord(pconstsym(srsym)^.value))^,bestrealdef^);
  1151. constbool :
  1152. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1153. constset :
  1154. p1:=gensetconstnode(pconstset(tpointerord(pconstsym(srsym)^.value)),
  1155. psetdef(pconstsym(srsym)^.consttype.def));
  1156. constord :
  1157. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1158. pconstsym(srsym)^.consttype.def);
  1159. constpointer :
  1160. p1:=genpointerconstnode(pconstsym(srsym)^.value,
  1161. pconstsym(srsym)^.consttype.def);
  1162. constnil :
  1163. p1:=genzeronode(niln);
  1164. constresourcestring:
  1165. begin
  1166. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1167. p1^.resulttype:=cansistringdef;
  1168. end;
  1169. end;
  1170. pd:=p1^.resulttype;
  1171. end;
  1172. procsym : begin
  1173. { are we in a class method ? }
  1174. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1175. assigned(aktprocsym) and
  1176. (po_classmethod in aktprocsym^.definition^.procoptions);
  1177. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1178. p1^.unit_specific:=unit_specific;
  1179. do_proc_call(getaddr or
  1180. (getprocvar and
  1181. ((block_type=bt_const) or
  1182. ((m_tp_procvar in aktmodeswitches) and
  1183. proc_to_procvar_equal(pprocsym(srsym)^.definition,getprocvardef)
  1184. )
  1185. )
  1186. ),again,p1,pd);
  1187. if (block_type=bt_const) and
  1188. getprocvar then
  1189. handle_procvar(getprocvardef,p1);
  1190. if possible_error and
  1191. not(po_classmethod in p1^.procdefinition^.procoptions) then
  1192. Message(parser_e_only_class_methods);
  1193. end;
  1194. propertysym : begin
  1195. { access to property in a method }
  1196. { are we in a class method ? }
  1197. if (srsymtable^.symtabletype=objectsymtable) and
  1198. assigned(aktprocsym) and
  1199. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1200. Message(parser_e_only_class_methods);
  1201. { no method pointer }
  1202. p1:=nil;
  1203. handle_propertysym(srsym,srsymtable,p1,pd);
  1204. end;
  1205. errorsym : begin
  1206. p1:=genzeronode(errorn);
  1207. p1^.resulttype:=generrordef;
  1208. pd:=generrordef;
  1209. if token=_LKLAMMER then
  1210. begin
  1211. consume(_LKLAMMER);
  1212. parse_paras(false,false);
  1213. consume(_RKLAMMER);
  1214. end;
  1215. end;
  1216. else
  1217. begin
  1218. p1:=genzeronode(errorn);
  1219. pd:=generrordef;
  1220. Message(cg_e_illegal_expression);
  1221. end;
  1222. end; { end case }
  1223. end;
  1224. end;
  1225. end;
  1226. end;
  1227. {---------------------------------------------
  1228. Factor_Read_Set
  1229. ---------------------------------------------}
  1230. { Read a set between [] }
  1231. function factor_read_set:ptree;
  1232. var
  1233. p1,
  1234. lastp,
  1235. buildp : ptree;
  1236. begin
  1237. buildp:=nil;
  1238. { be sure that a least one arrayconstructn is used, also for an
  1239. empty [] }
  1240. if token=_RECKKLAMMER then
  1241. buildp:=gennode(arrayconstructn,nil,buildp)
  1242. else
  1243. begin
  1244. while true do
  1245. begin
  1246. p1:=comp_expr(true);
  1247. if token=_POINTPOINT then
  1248. begin
  1249. consume(_POINTPOINT);
  1250. p2:=comp_expr(true);
  1251. p1:=gennode(arrayconstructrangen,p1,p2);
  1252. end;
  1253. { insert at the end of the tree, to get the correct order }
  1254. if not assigned(buildp) then
  1255. begin
  1256. buildp:=gennode(arrayconstructn,p1,nil);
  1257. lastp:=buildp;
  1258. end
  1259. else
  1260. begin
  1261. lastp^.right:=gennode(arrayconstructn,p1,nil);
  1262. lastp:=lastp^.right;
  1263. end;
  1264. { there could be more elements }
  1265. if token=_COMMA then
  1266. consume(_COMMA)
  1267. else
  1268. break;
  1269. end;
  1270. end;
  1271. factor_read_set:=buildp;
  1272. end;
  1273. {---------------------------------------------
  1274. Helpers
  1275. ---------------------------------------------}
  1276. procedure check_tokenpos;
  1277. begin
  1278. if (p1<>oldp1) then
  1279. begin
  1280. if assigned(p1) then
  1281. set_tree_filepos(p1,filepos);
  1282. oldp1:=p1;
  1283. filepos:=tokenpos;
  1284. end;
  1285. end;
  1286. {---------------------------------------------
  1287. PostFixOperators
  1288. ---------------------------------------------}
  1289. procedure postfixoperators;
  1290. var
  1291. store_static : boolean;
  1292. { p1 and p2 must contain valid value_str }
  1293. begin
  1294. check_tokenpos;
  1295. while again do
  1296. begin
  1297. { prevent crashes with unknown types }
  1298. if not assigned(pd) then
  1299. begin
  1300. { try to recover }
  1301. repeat
  1302. case token of
  1303. _CARET:
  1304. consume(_CARET);
  1305. _POINT:
  1306. begin
  1307. consume(_POINT);
  1308. consume(_ID);
  1309. end;
  1310. _LECKKLAMMER:
  1311. begin
  1312. repeat
  1313. consume(token);
  1314. until token in [_RECKKLAMMER,_SEMICOLON];
  1315. end;
  1316. else
  1317. break;
  1318. end;
  1319. until false;
  1320. exit;
  1321. end;
  1322. { handle token }
  1323. case token of
  1324. _CARET:
  1325. begin
  1326. consume(_CARET);
  1327. if (pd^.deftype<>pointerdef) then
  1328. begin
  1329. { ^ as binary operator is a problem!!!! (FK) }
  1330. again:=false;
  1331. Message(cg_e_invalid_qualifier);
  1332. disposetree(p1);
  1333. p1:=genzeronode(errorn);
  1334. end
  1335. else
  1336. begin
  1337. p1:=gensinglenode(derefn,p1);
  1338. pd:=ppointerdef(pd)^.pointertype.def;
  1339. end;
  1340. end;
  1341. _LECKKLAMMER:
  1342. begin
  1343. if (pd^.deftype=objectdef) and pobjectdef(pd)^.is_class then
  1344. begin
  1345. { default property }
  1346. propsym:=search_default_property(pobjectdef(pd));
  1347. if not(assigned(propsym)) then
  1348. begin
  1349. disposetree(p1);
  1350. p1:=genzeronode(errorn);
  1351. again:=false;
  1352. message(parser_e_no_default_property_available);
  1353. end
  1354. else
  1355. handle_propertysym(propsym,propsym^.owner,p1,pd);
  1356. end
  1357. else
  1358. begin
  1359. consume(_LECKKLAMMER);
  1360. repeat
  1361. case pd^.deftype of
  1362. pointerdef:
  1363. begin
  1364. pd:=ppointerdef(pd)^.pointertype.def;
  1365. { support delphi autoderef }
  1366. if (pd^.deftype=arraydef) and
  1367. (m_autoderef in aktmodeswitches) then
  1368. begin
  1369. p1:=gensinglenode(derefn,p1);
  1370. pd:=parraydef(pd)^.elementtype.def;
  1371. end;
  1372. p2:=comp_expr(true);
  1373. p1:=gennode(vecn,p1,p2);
  1374. end;
  1375. stringdef : begin
  1376. p2:=comp_expr(true);
  1377. p1:=gennode(vecn,p1,p2);
  1378. pd:=cchardef
  1379. end;
  1380. arraydef : begin
  1381. p2:=comp_expr(true);
  1382. { support SEG:OFS for go32v2 Mem[] }
  1383. if (target_info.target=target_i386_go32v2) and
  1384. (p1^.treetype=loadn) and
  1385. assigned(p1^.symtableentry) and
  1386. assigned(p1^.symtableentry^.owner^.name) and
  1387. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  1388. ((p1^.symtableentry^.name='MEM') or
  1389. (p1^.symtableentry^.name='MEMW') or
  1390. (p1^.symtableentry^.name='MEML')) then
  1391. begin
  1392. if (token=_COLON) then
  1393. begin
  1394. consume(_COLON);
  1395. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  1396. p2:=comp_expr(true);
  1397. p2:=gennode(addn,p2,p3);
  1398. p1:=gennode(vecn,p1,p2);
  1399. p1^.memseg:=true;
  1400. p1^.memindex:=true;
  1401. end
  1402. else
  1403. begin
  1404. p1:=gennode(vecn,p1,p2);
  1405. p1^.memindex:=true;
  1406. end;
  1407. end
  1408. else
  1409. p1:=gennode(vecn,p1,p2);
  1410. pd:=parraydef(pd)^.elementtype.def;
  1411. end;
  1412. else
  1413. begin
  1414. Message(cg_e_invalid_qualifier);
  1415. disposetree(p1);
  1416. p1:=genzeronode(errorn);
  1417. again:=false;
  1418. end;
  1419. end;
  1420. if token=_COMMA then
  1421. consume(_COMMA)
  1422. else
  1423. break;
  1424. until false;
  1425. consume(_RECKKLAMMER);
  1426. end;
  1427. end;
  1428. _POINT : begin
  1429. consume(_POINT);
  1430. if (pd^.deftype=pointerdef) and
  1431. (m_autoderef in aktmodeswitches) then
  1432. begin
  1433. p1:=gensinglenode(derefn,p1);
  1434. pd:=ppointerdef(pd)^.pointertype.def;
  1435. end;
  1436. case pd^.deftype of
  1437. recorddef:
  1438. begin
  1439. sym:=precorddef(pd)^.symtable^.search(pattern);
  1440. if assigned(sym) and
  1441. (sym^.typ=varsym) then
  1442. begin
  1443. p1:=gensubscriptnode(pvarsym(sym),p1);
  1444. pd:=pvarsym(sym)^.vartype.def;
  1445. end
  1446. else
  1447. begin
  1448. Message1(sym_e_illegal_field,pattern);
  1449. disposetree(p1);
  1450. p1:=genzeronode(errorn);
  1451. end;
  1452. consume(_ID);
  1453. end;
  1454. classrefdef:
  1455. begin
  1456. classh:=pobjectdef(pclassrefdef(pd)^.pointertype.def);
  1457. sym:=nil;
  1458. while assigned(classh) do
  1459. begin
  1460. sym:=classh^.symtable^.search(pattern);
  1461. srsymtable:=classh^.symtable;
  1462. if assigned(sym) then
  1463. break;
  1464. classh:=classh^.childof;
  1465. end;
  1466. if sym=nil then
  1467. begin
  1468. Message1(sym_e_id_no_member,pattern);
  1469. disposetree(p1);
  1470. p1:=genzeronode(errorn);
  1471. { try to clean up }
  1472. pd:=generrordef;
  1473. consume(_ID);
  1474. end
  1475. else
  1476. begin
  1477. consume(_ID);
  1478. do_member_read(getaddr,sym,p1,pd,again);
  1479. end;
  1480. end;
  1481. objectdef:
  1482. begin
  1483. classh:=pobjectdef(pd);
  1484. sym:=nil;
  1485. store_static:=allow_only_static;
  1486. allow_only_static:=false;
  1487. while assigned(classh) do
  1488. begin
  1489. sym:=classh^.symtable^.search(pattern);
  1490. srsymtable:=classh^.symtable;
  1491. if assigned(sym) then
  1492. break;
  1493. classh:=classh^.childof;
  1494. end;
  1495. allow_only_static:=store_static;
  1496. if sym=nil then
  1497. begin
  1498. Message1(sym_e_id_no_member,pattern);
  1499. disposetree(p1);
  1500. p1:=genzeronode(errorn);
  1501. { try to clean up }
  1502. pd:=generrordef;
  1503. consume(_ID);
  1504. end
  1505. else
  1506. begin
  1507. consume(_ID);
  1508. do_member_read(getaddr,sym,p1,pd,again);
  1509. end;
  1510. end;
  1511. pointerdef:
  1512. begin
  1513. Message(cg_e_invalid_qualifier);
  1514. if ppointerdef(pd)^.pointertype.def^.deftype in [recorddef,objectdef,classrefdef] then
  1515. Message(parser_h_maybe_deref_caret_missing);
  1516. end;
  1517. else
  1518. begin
  1519. Message(cg_e_invalid_qualifier);
  1520. disposetree(p1);
  1521. p1:=genzeronode(errorn);
  1522. end;
  1523. end;
  1524. end;
  1525. else
  1526. begin
  1527. { is this a procedure variable ? }
  1528. if assigned(pd) then
  1529. begin
  1530. if (pd^.deftype=procvardef) then
  1531. begin
  1532. if getprocvar and is_equal(pd,getprocvardef) then
  1533. again:=false
  1534. else
  1535. if (token=_LKLAMMER) or
  1536. ((pprocvardef(pd)^.para^.empty) and
  1537. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1538. (not afterassignment) and
  1539. (not in_args)) then
  1540. begin
  1541. { do this in a strange way }
  1542. { it's not a clean solution }
  1543. p2:=p1;
  1544. p1:=gencallnode(nil,nil);
  1545. p1^.right:=p2;
  1546. p1^.unit_specific:=unit_specific;
  1547. p1^.symtableprocentry:=pprocsym(sym);
  1548. if token=_LKLAMMER then
  1549. begin
  1550. consume(_LKLAMMER);
  1551. p1^.left:=parse_paras(false,false);
  1552. consume(_RKLAMMER);
  1553. end;
  1554. pd:=pprocvardef(pd)^.rettype.def;
  1555. { proc():= is never possible }
  1556. if token=_ASSIGNMENT then
  1557. begin
  1558. Message(cg_e_illegal_expression);
  1559. p1:=genzeronode(errorn);
  1560. again:=false;
  1561. end;
  1562. p1^.resulttype:=pd;
  1563. end
  1564. else
  1565. again:=false;
  1566. p1^.resulttype:=pd;
  1567. end
  1568. else
  1569. again:=false;
  1570. end
  1571. else
  1572. again:=false;
  1573. end;
  1574. end;
  1575. check_tokenpos;
  1576. end; { while again }
  1577. end;
  1578. {---------------------------------------------
  1579. Factor (Main)
  1580. ---------------------------------------------}
  1581. begin
  1582. oldp1:=nil;
  1583. p1:=nil;
  1584. filepos:=tokenpos;
  1585. if token=_ID then
  1586. begin
  1587. factor_read_id;
  1588. { handle post fix operators }
  1589. postfixoperators;
  1590. end
  1591. else
  1592. case token of
  1593. _NEW : begin
  1594. consume(_NEW);
  1595. consume(_LKLAMMER);
  1596. {allow_type:=true;}
  1597. p1:=factor(false);
  1598. {allow_type:=false;}
  1599. if p1^.treetype<>typen then
  1600. begin
  1601. Message(type_e_type_id_expected);
  1602. disposetree(p1);
  1603. pd:=generrordef;
  1604. end
  1605. else
  1606. pd:=p1^.typenodetype;
  1607. pd2:=pd;
  1608. if (pd^.deftype<>pointerdef) then
  1609. Message1(type_e_pointer_type_expected,pd^.typename)
  1610. else
  1611. if token=_RKLAMMER then
  1612. begin
  1613. if (ppointerdef(pd)^.pointertype.def^.deftype=objectdef) and
  1614. (oo_has_vmt in pobjectdef(ppointerdef(pd)^.pointertype.def)^.objectoptions) then
  1615. Message(parser_w_use_extended_syntax_for_objects);
  1616. p1:=gensinglenode(newn,nil);
  1617. p1^.resulttype:=pd2;
  1618. consume(_RKLAMMER);
  1619. end
  1620. else
  1621. begin
  1622. disposetree(p1);
  1623. p1:=genzeronode(hnewn);
  1624. p1^.resulttype:=ppointerdef(pd)^.pointertype.def;
  1625. consume(_COMMA);
  1626. afterassignment:=false;
  1627. { determines the current object defintion }
  1628. classh:=pobjectdef(ppointerdef(pd)^.pointertype.def);
  1629. if classh^.deftype<>objectdef then
  1630. Message(parser_e_pointer_to_class_expected)
  1631. else
  1632. begin
  1633. { check for an abstract class }
  1634. if (oo_has_abstract in classh^.objectoptions) then
  1635. Message(sym_e_no_instance_of_abstract_object);
  1636. { search the constructor also in the symbol tables of
  1637. the parents }
  1638. sym:=nil;
  1639. while assigned(classh) do
  1640. begin
  1641. sym:=classh^.symtable^.search(pattern);
  1642. srsymtable:=classh^.symtable;
  1643. if assigned(sym) then
  1644. break;
  1645. classh:=classh^.childof;
  1646. end;
  1647. consume(_ID);
  1648. do_member_read(false,sym,p1,pd,again);
  1649. if (p1^.treetype<>calln) or
  1650. (assigned(p1^.procdefinition) and
  1651. (p1^.procdefinition^.proctypeoption<>potype_constructor)) then
  1652. Message(parser_e_expr_have_to_be_constructor_call);
  1653. end;
  1654. p1:=gensinglenode(newn,p1);
  1655. { set the resulttype }
  1656. p1^.resulttype:=pd2;
  1657. consume(_RKLAMMER);
  1658. end;
  1659. postfixoperators;
  1660. end;
  1661. _SELF : begin
  1662. again:=true;
  1663. consume(_SELF);
  1664. if not assigned(procinfo^._class) then
  1665. begin
  1666. p1:=genzeronode(errorn);
  1667. pd:=generrordef;
  1668. again:=false;
  1669. Message(parser_e_self_not_in_method);
  1670. end
  1671. else
  1672. begin
  1673. if (po_classmethod in aktprocsym^.definition^.procoptions) then
  1674. begin
  1675. { self in class methods is a class reference type }
  1676. pd:=new(pclassrefdef,init(procinfo^._class));
  1677. p1:=genselfnode(pd);
  1678. p1^.resulttype:=pd;
  1679. end
  1680. else
  1681. begin
  1682. p1:=genselfnode(procinfo^._class);
  1683. p1^.resulttype:=procinfo^._class;
  1684. end;
  1685. pd:=p1^.resulttype;
  1686. postfixoperators;
  1687. end;
  1688. end;
  1689. _INHERITED : begin
  1690. again:=true;
  1691. consume(_INHERITED);
  1692. if assigned(procinfo^._class) then
  1693. begin
  1694. { if inherited; only then we need the method with
  1695. the same name }
  1696. if token=_SEMICOLON then
  1697. begin
  1698. hs:=aktprocsym^.name;
  1699. auto_inherited:=true
  1700. end
  1701. else
  1702. begin
  1703. hs:=pattern;
  1704. consume(_ID);
  1705. auto_inherited:=false;
  1706. end;
  1707. classh:=procinfo^._class^.childof;
  1708. while assigned(classh) do
  1709. begin
  1710. srsymtable:=pobjectdef(classh)^.symtable;
  1711. sym:=srsymtable^.search(hs);
  1712. if assigned(sym) then
  1713. begin
  1714. { only for procsyms we need to set the type (PFV) }
  1715. case sym^.typ of
  1716. procsym :
  1717. begin
  1718. p1:=genzeronode(typen);
  1719. p1^.resulttype:=classh;
  1720. pd:=p1^.resulttype;
  1721. end;
  1722. varsym :
  1723. begin
  1724. p1:=nil;
  1725. pd:=pvarsym(sym)^.vartype.def;
  1726. end;
  1727. propertysym :
  1728. begin
  1729. p1:=nil;
  1730. pd:=ppropertysym(sym)^.proptype.def;
  1731. end;
  1732. else
  1733. internalerror(83251763);
  1734. end;
  1735. do_member_read(false,sym,p1,pd,again);
  1736. break;
  1737. end;
  1738. classh:=classh^.childof;
  1739. end;
  1740. if classh=nil then
  1741. begin
  1742. Message1(sym_e_id_no_member,hs);
  1743. again:=false;
  1744. pd:=generrordef;
  1745. p1:=genzeronode(errorn);
  1746. end;
  1747. { turn auto inheriting off }
  1748. auto_inherited:=false;
  1749. end
  1750. else
  1751. begin
  1752. Message(parser_e_generic_methods_only_in_methods);
  1753. again:=false;
  1754. pd:=generrordef;
  1755. p1:=genzeronode(errorn);
  1756. end;
  1757. postfixoperators;
  1758. end;
  1759. _INTCONST : begin
  1760. valint(pattern,l,code);
  1761. if code<>0 then
  1762. begin
  1763. { try int64 if available }
  1764. { if no int64 available longint is tried a second }
  1765. { time which doesn't hurt }
  1766. val(pattern,ic,code);
  1767. if code<>0 then
  1768. begin
  1769. val(pattern,d,code);
  1770. if code<>0 then
  1771. begin
  1772. Message(cg_e_invalid_integer);
  1773. consume(_INTCONST);
  1774. l:=1;
  1775. p1:=genordinalconstnode(l,s32bitdef);
  1776. end
  1777. else
  1778. begin
  1779. consume(_INTCONST);
  1780. p1:=genrealconstnode(d,bestrealdef^);
  1781. end;
  1782. end
  1783. else
  1784. begin
  1785. consume(_INTCONST);
  1786. p1:=genordinalconstnode(ic,cs64bitdef);
  1787. end;
  1788. end
  1789. else
  1790. begin
  1791. consume(_INTCONST);
  1792. p1:=genordinalconstnode(l,s32bitdef)
  1793. end;
  1794. end;
  1795. _REALNUMBER : begin
  1796. val(pattern,d,code);
  1797. if code<>0 then
  1798. begin
  1799. Message(parser_e_error_in_real);
  1800. d:=1.0;
  1801. end;
  1802. consume(_REALNUMBER);
  1803. p1:=genrealconstnode(d,bestrealdef^);
  1804. end;
  1805. _STRING : begin
  1806. pd:=string_dec;
  1807. { STRING can be also a type cast }
  1808. if token=_LKLAMMER then
  1809. begin
  1810. consume(_LKLAMMER);
  1811. p1:=comp_expr(true);
  1812. consume(_RKLAMMER);
  1813. p1:=gentypeconvnode(p1,pd);
  1814. p1^.explizit:=true;
  1815. { handle postfix operators here e.g. string(a)[10] }
  1816. again:=true;
  1817. postfixoperators;
  1818. end
  1819. else
  1820. p1:=gentypenode(pd,nil);
  1821. end;
  1822. _FILE : begin
  1823. pd:=cfiledef;
  1824. consume(_FILE);
  1825. { FILE can be also a type cast }
  1826. if token=_LKLAMMER then
  1827. begin
  1828. consume(_LKLAMMER);
  1829. p1:=comp_expr(true);
  1830. consume(_RKLAMMER);
  1831. p1:=gentypeconvnode(p1,pd);
  1832. p1^.explizit:=true;
  1833. { handle postfix operators here e.g. string(a)[10] }
  1834. again:=true;
  1835. postfixoperators;
  1836. end
  1837. else
  1838. p1:=gentypenode(pd,nil);
  1839. end;
  1840. _CSTRING : begin
  1841. p1:=genstringconstnode(pattern,st_default);
  1842. consume(_CSTRING);
  1843. end;
  1844. _CCHAR : begin
  1845. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1846. consume(_CCHAR);
  1847. end;
  1848. _KLAMMERAFFE : begin
  1849. consume(_KLAMMERAFFE);
  1850. got_addrn:=true;
  1851. { support both @<x> and @(<x>) }
  1852. if token=_LKLAMMER then
  1853. begin
  1854. consume(_LKLAMMER);
  1855. p1:=factor(true);
  1856. consume(_RKLAMMER);
  1857. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1858. begin
  1859. { we need the resulttype }
  1860. { of the expression in pd }
  1861. do_firstpass(p1);
  1862. pd:=p1^.resulttype;
  1863. again:=true;
  1864. postfixoperators;
  1865. end;
  1866. end
  1867. else
  1868. p1:=factor(true);
  1869. got_addrn:=false;
  1870. p1:=gensinglenode(addrn,p1);
  1871. end;
  1872. _LKLAMMER : begin
  1873. consume(_LKLAMMER);
  1874. p1:=comp_expr(true);
  1875. consume(_RKLAMMER);
  1876. { it's not a good solution }
  1877. { but (a+b)^ makes some problems }
  1878. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1879. begin
  1880. { we need the resulttype }
  1881. { of the expression in pd }
  1882. do_firstpass(p1);
  1883. pd:=p1^.resulttype;
  1884. again:=true;
  1885. postfixoperators;
  1886. end;
  1887. end;
  1888. _LECKKLAMMER : begin
  1889. consume(_LECKKLAMMER);
  1890. p1:=factor_read_set;
  1891. consume(_RECKKLAMMER);
  1892. end;
  1893. _PLUS : begin
  1894. consume(_PLUS);
  1895. p1:=factor(false);
  1896. end;
  1897. _MINUS : begin
  1898. consume(_MINUS);
  1899. p1:=sub_expr(oppower,false);
  1900. p1:=gensinglenode(unaryminusn,p1);
  1901. end;
  1902. _OP_NOT : begin
  1903. consume(_OP_NOT);
  1904. p1:=factor(false);
  1905. p1:=gensinglenode(notn,p1);
  1906. end;
  1907. _TRUE : begin
  1908. consume(_TRUE);
  1909. p1:=genordinalconstnode(1,booldef);
  1910. end;
  1911. _FALSE : begin
  1912. consume(_FALSE);
  1913. p1:=genordinalconstnode(0,booldef);
  1914. end;
  1915. _NIL : begin
  1916. consume(_NIL);
  1917. p1:=genzeronode(niln);
  1918. end;
  1919. else
  1920. begin
  1921. p1:=genzeronode(errorn);
  1922. consume(token);
  1923. Message(cg_e_illegal_expression);
  1924. end;
  1925. end;
  1926. { generate error node if no node is created }
  1927. if not assigned(p1) then
  1928. p1:=genzeronode(errorn);
  1929. { tp7 procvar handling, but not if the next token
  1930. will be a := }
  1931. if (m_tp_procvar in aktmodeswitches) and
  1932. (token<>_ASSIGNMENT) then
  1933. check_tp_procvar(p1);
  1934. factor:=p1;
  1935. check_tokenpos;
  1936. end;
  1937. {$ifdef fpc}
  1938. {$maxfpuregisters default}
  1939. {$endif fpc}
  1940. {****************************************************************************
  1941. Sub_Expr
  1942. ****************************************************************************}
  1943. const
  1944. { Warning these stay be ordered !! }
  1945. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1946. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1947. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1948. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1949. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1950. [_STARSTAR] );
  1951. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1952. {Reads a subexpression while the operators are of the current precedence
  1953. level, or any higher level. Replaces the old term, simpl_expr and
  1954. simpl2_expr.}
  1955. var
  1956. low,high,mid : longint;
  1957. p1,p2 : Ptree;
  1958. oldt : Ttoken;
  1959. filepos : tfileposinfo;
  1960. begin
  1961. if pred_level=highest_precedence then
  1962. p1:=factor(false)
  1963. else
  1964. p1:=sub_expr(succ(pred_level),true);
  1965. repeat
  1966. if (token in operator_levels[pred_level]) and
  1967. ((token<>_EQUAL) or accept_equal) then
  1968. begin
  1969. oldt:=token;
  1970. filepos:=tokenpos;
  1971. consume(token);
  1972. if pred_level=highest_precedence then
  1973. p2:=factor(false)
  1974. else
  1975. p2:=sub_expr(succ(pred_level),true);
  1976. low:=1;
  1977. high:=tok2nodes;
  1978. while (low<high) do
  1979. begin
  1980. mid:=(low+high+1) shr 1;
  1981. if oldt<tok2node[mid].tok then
  1982. high:=mid-1
  1983. else
  1984. low:=mid;
  1985. end;
  1986. if tok2node[high].tok=oldt then
  1987. p1:=gennode(tok2node[high].nod,p1,p2)
  1988. else
  1989. p1:=gennode(nothingn,p1,p2);
  1990. set_tree_filepos(p1,filepos);
  1991. end
  1992. else
  1993. break;
  1994. until false;
  1995. sub_expr:=p1;
  1996. end;
  1997. function comp_expr(accept_equal : boolean):Ptree;
  1998. var
  1999. oldafterassignment : boolean;
  2000. p1 : ptree;
  2001. begin
  2002. oldafterassignment:=afterassignment;
  2003. afterassignment:=true;
  2004. p1:=sub_expr(opcompare,accept_equal);
  2005. afterassignment:=oldafterassignment;
  2006. comp_expr:=p1;
  2007. end;
  2008. function expr : ptree;
  2009. var
  2010. p1,p2 : ptree;
  2011. oldafterassignment : boolean;
  2012. oldp1 : ptree;
  2013. filepos : tfileposinfo;
  2014. begin
  2015. oldafterassignment:=afterassignment;
  2016. p1:=sub_expr(opcompare,true);
  2017. filepos:=tokenpos;
  2018. if (m_tp_procvar in aktmodeswitches) and
  2019. (token<>_ASSIGNMENT) then
  2020. check_tp_procvar(p1);
  2021. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2022. afterassignment:=true;
  2023. oldp1:=p1;
  2024. case token of
  2025. _POINTPOINT : begin
  2026. consume(_POINTPOINT);
  2027. p2:=sub_expr(opcompare,true);
  2028. p1:=gennode(rangen,p1,p2);
  2029. end;
  2030. _ASSIGNMENT : begin
  2031. consume(_ASSIGNMENT);
  2032. { avoid a firstpass of a procedure if
  2033. it must be assigned to a procvar }
  2034. { should be recursive for a:=b:=c !!! }
  2035. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  2036. begin
  2037. getprocvar:=true;
  2038. getprocvardef:=pprocvardef(p1^.resulttype);
  2039. end;
  2040. p2:=sub_expr(opcompare,true);
  2041. if getprocvar then
  2042. handle_procvar(getprocvardef,p2);
  2043. getprocvar:=false;
  2044. p1:=gennode(assignn,p1,p2);
  2045. end;
  2046. { this is the code for C like assignements }
  2047. { from an improvement of Peter Schaefer }
  2048. _PLUSASN : begin
  2049. consume(_PLUSASN );
  2050. p2:=sub_expr(opcompare,true);
  2051. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  2052. { was first
  2053. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  2054. but disposetree assumes that we have a real
  2055. *** tree *** }
  2056. end;
  2057. _MINUSASN : begin
  2058. consume(_MINUSASN );
  2059. p2:=sub_expr(opcompare,true);
  2060. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  2061. end;
  2062. _STARASN : begin
  2063. consume(_STARASN );
  2064. p2:=sub_expr(opcompare,true);
  2065. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  2066. end;
  2067. _SLASHASN : begin
  2068. consume(_SLASHASN );
  2069. p2:=sub_expr(opcompare,true);
  2070. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  2071. end;
  2072. end;
  2073. afterassignment:=oldafterassignment;
  2074. if p1<>oldp1 then
  2075. set_tree_filepos(p1,filepos);
  2076. expr:=p1;
  2077. end;
  2078. function get_intconst:longint;
  2079. {Reads an expression, tries to evalute it and check if it is an integer
  2080. constant. Then the constant is returned.}
  2081. var
  2082. p:Ptree;
  2083. begin
  2084. p:=comp_expr(true);
  2085. do_firstpass(p);
  2086. if not codegenerror then
  2087. begin
  2088. if (p^.treetype<>ordconstn) and
  2089. (p^.resulttype^.deftype=orddef) and
  2090. not(Porddef(p^.resulttype)^.typ in [uvoid,uchar,bool8bit,bool16bit,bool32bit]) then
  2091. Message(cg_e_illegal_expression)
  2092. else
  2093. get_intconst:=p^.value;
  2094. end;
  2095. disposetree(p);
  2096. end;
  2097. function get_stringconst:string;
  2098. {Reads an expression, tries to evaluate it and checks if it is a string
  2099. constant. Then the constant is returned.}
  2100. var
  2101. p:Ptree;
  2102. begin
  2103. get_stringconst:='';
  2104. p:=comp_expr(true);
  2105. do_firstpass(p);
  2106. if p^.treetype<>stringconstn then
  2107. begin
  2108. if (p^.treetype=ordconstn) and is_char(p^.resulttype) then
  2109. get_stringconst:=char(p^.value)
  2110. else
  2111. Message(cg_e_illegal_expression);
  2112. end
  2113. else
  2114. get_stringconst:=strpas(p^.value_str);
  2115. disposetree(p);
  2116. end;
  2117. end.
  2118. {
  2119. $Log$
  2120. Revision 1.10 2000-10-01 19:48:25 peter
  2121. * lot of compile updates for cg11
  2122. Revision 1.9 2000/09/24 21:19:50 peter
  2123. * delphi compile fixes
  2124. Revision 1.8 2000/09/24 15:06:22 peter
  2125. * use defines.inc
  2126. Revision 1.7 2000/08/27 16:11:51 peter
  2127. * moved some util functions from globals,cobjects to cutils
  2128. * splitted files into finput,fmodule
  2129. Revision 1.6 2000/08/20 15:12:49 peter
  2130. * auto derefence mode for array pointer (merged)
  2131. Revision 1.5 2000/08/16 18:33:53 peter
  2132. * splitted namedobjectitem.next into indexnext and listnext so it
  2133. can be used in both lists
  2134. * don't allow "word = word" type definitions (merged)
  2135. Revision 1.4 2000/08/16 13:06:06 florian
  2136. + support of 64 bit integer constants
  2137. Revision 1.3 2000/08/04 22:00:52 peter
  2138. * merges from fixes
  2139. Revision 1.2 2000/07/13 11:32:44 michael
  2140. + removed logs
  2141. }