pexpr.pas 86 KB

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