pexpr.pas 85 KB

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