pexpr.pas 85 KB

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