pexpr.pas 86 KB

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