pexpr.pas 85 KB

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