pexpr.pas 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  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. begin
  703. if sym=nil then
  704. begin
  705. { pattern is still valid unless
  706. there is another ID just after the ID of sym }
  707. Message1(sym_e_id_no_member,pattern);
  708. disposetree(p1);
  709. p1:=genzeronode(errorn);
  710. { try to clean up }
  711. pd:=generrordef;
  712. again:=false;
  713. end
  714. else
  715. begin
  716. isclassref:=pd^.deftype=classrefdef;
  717. { check protected and private members }
  718. { please leave this code as it is, }
  719. { it has now the same behaviaor as TP/Delphi }
  720. if (sp_private in sym^.symoptions) and
  721. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  722. Message(parser_e_cant_access_private_member);
  723. if (sp_protected in sym^.symoptions) and
  724. (pobjectdef(pd)^.owner^.symtabletype=unitsymtable) then
  725. begin
  726. if assigned(aktprocsym^.definition^._class) then
  727. begin
  728. if not aktprocsym^.definition^._class^.is_related(pobjectdef(sym^.owner^.defowner)) then
  729. Message(parser_e_cant_access_protected_member);
  730. end
  731. else
  732. Message(parser_e_cant_access_protected_member);
  733. end;
  734. { we assume, that only procsyms and varsyms are in an object }
  735. { symbol table, for classes, properties are allowed }
  736. case sym^.typ of
  737. procsym:
  738. begin
  739. p1:=genmethodcallnode(pprocsym(sym),srsymtable,p1);
  740. do_proc_call(getaddr or
  741. (getprocvar and
  742. ((block_type=bt_const) or
  743. ((m_tp_procvar in aktmodeswitches) and
  744. proc_to_procvar_equal(pprocsym(sym)^.definition,getprocvardef)
  745. )
  746. )
  747. ),again,p1,pd);
  748. if (block_type=bt_const) and
  749. getprocvar then
  750. handle_procvar(getprocvardef,p1);
  751. { now we know the real method e.g. we can check for a class method }
  752. if isclassref and
  753. assigned(p1^.procdefinition) and
  754. not(po_classmethod in p1^.procdefinition^.procoptions) and
  755. not(p1^.procdefinition^.proctypeoption=potype_constructor) then
  756. Message(parser_e_only_class_methods_via_class_ref);
  757. end;
  758. varsym:
  759. begin
  760. if isclassref then
  761. Message(parser_e_only_class_methods_via_class_ref);
  762. if (sp_static in sym^.symoptions) then
  763. begin
  764. { static_name:=lower(srsymtable^.name^)+'_'+sym^.name;
  765. this is wrong for static field in with symtable (PM) }
  766. static_name:=lower(srsym^.owner^.name^)+'_'+sym^.name;
  767. getsym(static_name,true);
  768. disposetree(p1);
  769. p1:=genloadnode(pvarsym(srsym),srsymtable);
  770. end
  771. else
  772. p1:=gensubscriptnode(pvarsym(sym),p1);
  773. pd:=pvarsym(sym)^.vartype.def;
  774. end;
  775. propertysym:
  776. begin
  777. if isclassref then
  778. Message(parser_e_only_class_methods_via_class_ref);
  779. handle_propertysym(sym,srsymtable,p1,pd);
  780. end;
  781. else internalerror(16);
  782. end;
  783. end;
  784. end;
  785. {****************************************************************************
  786. Factor
  787. ****************************************************************************}
  788. {$ifdef fpc}
  789. {$maxfpuregisters 0}
  790. {$endif fpc}
  791. function factor(getaddr : boolean) : ptree;
  792. var
  793. l : longint;
  794. oldp1,
  795. p1,p2,p3 : ptree;
  796. code : integer;
  797. pd,pd2 : pdef;
  798. possible_error,
  799. unit_specific,
  800. again : boolean;
  801. sym : pvarsym;
  802. classh : pobjectdef;
  803. d : bestreal;
  804. static_name : string;
  805. propsym : ppropertysym;
  806. filepos : tfileposinfo;
  807. {---------------------------------------------
  808. Is_func_ret
  809. ---------------------------------------------}
  810. function is_func_ret(sym : psym) : boolean;
  811. var
  812. p : pprocinfo;
  813. storesymtablestack : psymtable;
  814. begin
  815. is_func_ret:=false;
  816. if not assigned(procinfo) or
  817. ((sym^.typ<>funcretsym) and ((procinfo^.flags and pi_operator)=0)) then
  818. exit;
  819. p:=procinfo;
  820. while assigned(p) do
  821. begin
  822. { is this an access to a function result? Accessing _RESULT is
  823. always allowed and funcretn is generated }
  824. if assigned(p^.funcretsym) and
  825. ((pfuncretsym(sym)=p^.resultfuncretsym) or
  826. ((pfuncretsym(sym)=p^.funcretsym) or
  827. ((pvarsym(sym)=opsym) and ((p^.flags and pi_operator)<>0))) and
  828. (p^.returntype.def<>pdef(voiddef)) and
  829. (token<>_LKLAMMER) and
  830. (not ((m_tp in aktmodeswitches) and (afterassignment or in_args)))
  831. ) then
  832. begin
  833. if ((pvarsym(sym)=opsym) and
  834. ((p^.flags and pi_operator)<>0)) then
  835. inc(opsym^.refs);
  836. p1:=genzeronode(funcretn);
  837. pd:=p^.returntype.def;
  838. p1^.funcretprocinfo:=p;
  839. p1^.rettype.def:=pd;
  840. is_func_ret:=true;
  841. if p^.funcret_state=vs_declared then
  842. begin
  843. p^.funcret_state:=vs_declared_and_first_found;
  844. p1^.is_first_funcret:=true;
  845. end;
  846. exit;
  847. end;
  848. p:=p^.parent;
  849. end;
  850. { we must use the function call }
  851. if (sym^.typ=funcretsym) then
  852. begin
  853. storesymtablestack:=symtablestack;
  854. symtablestack:=srsymtable^.next;
  855. getsym(sym^.name,true);
  856. if srsym^.typ<>procsym then
  857. Message(cg_e_illegal_expression);
  858. symtablestack:=storesymtablestack;
  859. end;
  860. end;
  861. {---------------------------------------------
  862. Factor_read_id
  863. ---------------------------------------------}
  864. procedure factor_read_id;
  865. var
  866. pc : pchar;
  867. len : longint;
  868. begin
  869. { allow post fix operators }
  870. again:=true;
  871. begin
  872. if lastsymknown then
  873. begin
  874. srsym:=lastsrsym;
  875. srsymtable:=lastsrsymtable;
  876. lastsymknown:=false;
  877. end
  878. else
  879. getsym(pattern,true);
  880. consume(_ID);
  881. if not is_func_ret(srsym) then
  882. { else it's a normal symbol }
  883. begin
  884. { is it defined like UNIT.SYMBOL ? }
  885. if srsym^.typ=unitsym then
  886. begin
  887. consume(_POINT);
  888. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  889. unit_specific:=true;
  890. consume(_ID);
  891. end
  892. else
  893. unit_specific:=false;
  894. if not assigned(srsym) then
  895. Begin
  896. p1:=genzeronode(errorn);
  897. { try to clean up }
  898. pd:=generrordef;
  899. end
  900. else
  901. Begin
  902. { check semantics of private }
  903. if (srsym^.typ in [propertysym,procsym,varsym]) and
  904. (srsymtable^.symtabletype=objectsymtable) then
  905. begin
  906. if (sp_private in srsym^.symoptions) and
  907. (pobjectdef(srsym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  908. Message(parser_e_cant_access_private_member);
  909. end;
  910. case srsym^.typ of
  911. absolutesym : begin
  912. p1:=genloadnode(pvarsym(srsym),srsymtable);
  913. pd:=pabsolutesym(srsym)^.vartype.def;
  914. end;
  915. varsym : begin
  916. { are we in a class method ? }
  917. if (srsymtable^.symtabletype=objectsymtable) and
  918. assigned(aktprocsym) and
  919. (po_classmethod in aktprocsym^.definition^.procoptions) then
  920. Message(parser_e_only_class_methods);
  921. if (sp_static in srsym^.symoptions) then
  922. begin
  923. static_name:=lower(srsym^.owner^.name^)+'_'+srsym^.name;
  924. getsym(static_name,true);
  925. end;
  926. p1:=genloadnode(pvarsym(srsym),srsymtable);
  927. if pvarsym(srsym)^.varstate=vs_declared then
  928. begin
  929. p1^.is_first := true;
  930. { set special between first loaded until checked in firstpass }
  931. pvarsym(srsym)^.varstate:=vs_declared_and_first_found;
  932. end;
  933. pd:=pvarsym(srsym)^.vartype.def;
  934. end;
  935. typedconstsym : begin
  936. p1:=gentypedconstloadnode(ptypedconstsym(srsym),srsymtable);
  937. pd:=ptypedconstsym(srsym)^.typedconsttype.def;
  938. end;
  939. syssym : p1:=statement_syssym(psyssym(srsym)^.number,pd);
  940. typesym : begin
  941. pd:=ptypesym(srsym)^.restype.def;
  942. if not assigned(pd) then
  943. begin
  944. pd:=generrordef;
  945. again:=false;
  946. end
  947. else
  948. begin
  949. { if we read a type declaration }
  950. { we have to return the type and }
  951. { nothing else }
  952. if block_type=bt_type then
  953. begin
  954. { we don't need sym reference when it's in the
  955. current unit or system unit, because those
  956. units are always loaded (PFV) }
  957. if not(assigned(pd^.owner)) or
  958. (pd^.owner^.unitid=0) or
  959. (pd^.owner^.unitid=1) then
  960. p1:=gentypenode(pd,nil)
  961. else
  962. p1:=gentypenode(pd,ptypesym(srsym));
  963. { here we can also set resulttype !! }
  964. p1^.resulttype:=pd;
  965. pd:=voiddef;
  966. end
  967. else { not type block }
  968. begin
  969. if token=_LKLAMMER then
  970. begin
  971. consume(_LKLAMMER);
  972. p1:=comp_expr(true);
  973. consume(_RKLAMMER);
  974. p1:=gentypeconvnode(p1,pd);
  975. p1^.explizit:=true;
  976. end
  977. else { not LKLAMMER}
  978. if (token=_POINT) and
  979. (pd^.deftype=objectdef) and
  980. not(pobjectdef(pd)^.is_class) then
  981. begin
  982. consume(_POINT);
  983. if assigned(procinfo) and
  984. assigned(procinfo^._class) and
  985. not(getaddr) then
  986. begin
  987. if procinfo^._class^.is_related(pobjectdef(pd)) then
  988. begin
  989. p1:=gentypenode(pd,ptypesym(srsym));
  990. p1^.resulttype:=pd;
  991. { search also in inherited methods }
  992. repeat
  993. srsymtable:=pobjectdef(pd)^.symtable;
  994. sym:=pvarsym(srsymtable^.search(pattern));
  995. if assigned(sym) then
  996. break;
  997. pd:=pobjectdef(pd)^.childof;
  998. until not assigned(pd);
  999. consume(_ID);
  1000. do_member_read(false,sym,p1,pd,again);
  1001. end
  1002. else
  1003. begin
  1004. Message(parser_e_no_super_class);
  1005. pd:=generrordef;
  1006. again:=false;
  1007. end;
  1008. end
  1009. else
  1010. begin
  1011. { allows @TObject.Load }
  1012. { also allows static methods and variables }
  1013. p1:=genzeronode(typen);
  1014. p1^.resulttype:=pd;
  1015. { TP allows also @TMenu.Load if Load is only }
  1016. { defined in an anchestor class }
  1017. sym:=pvarsym(search_class_member(pobjectdef(pd),pattern));
  1018. if not assigned(sym) then
  1019. Message1(sym_e_id_no_member,pattern)
  1020. else if not(getaddr) and not(sp_static in sym^.symoptions) then
  1021. Message(sym_e_only_static_in_static)
  1022. else
  1023. begin
  1024. consume(_ID);
  1025. do_member_read(getaddr,sym,p1,pd,again);
  1026. end;
  1027. end;
  1028. end
  1029. else
  1030. begin
  1031. { class reference ? }
  1032. if (pd^.deftype=objectdef)
  1033. and pobjectdef(pd)^.is_class then
  1034. begin
  1035. p1:=gentypenode(pd,nil);
  1036. p1^.resulttype:=pd;
  1037. pd:=new(pclassrefdef,init(pd));
  1038. p1:=gensinglenode(loadvmtn,p1);
  1039. p1^.resulttype:=pd;
  1040. end
  1041. else
  1042. begin
  1043. { generate a type node }
  1044. { (for typeof etc) }
  1045. if allow_type then
  1046. begin
  1047. p1:=gentypenode(pd,nil);
  1048. { here we must use typenodetype explicitly !! PM
  1049. p1^.resulttype:=pd; }
  1050. pd:=voiddef;
  1051. end
  1052. else
  1053. Message(parser_e_no_type_not_allowed_here);
  1054. end;
  1055. end;
  1056. end;
  1057. end;
  1058. end;
  1059. enumsym : begin
  1060. p1:=genenumnode(penumsym(srsym));
  1061. pd:=p1^.resulttype;
  1062. end;
  1063. constsym : begin
  1064. case pconstsym(srsym)^.consttyp of
  1065. constint :
  1066. p1:=genordinalconstnode(pconstsym(srsym)^.value,s32bitdef);
  1067. conststring :
  1068. begin
  1069. len:=pconstsym(srsym)^.len;
  1070. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1071. len:=255;
  1072. getmem(pc,len+1);
  1073. move(pchar(pconstsym(srsym)^.value)^,pc^,len);
  1074. pc[len]:=#0;
  1075. p1:=genpcharconstnode(pc,len);
  1076. end;
  1077. constchar :
  1078. p1:=genordinalconstnode(pconstsym(srsym)^.value,cchardef);
  1079. constreal :
  1080. p1:=genrealconstnode(pbestreal(pconstsym(srsym)^.value)^,bestrealdef^);
  1081. constbool :
  1082. p1:=genordinalconstnode(pconstsym(srsym)^.value,booldef);
  1083. constset :
  1084. p1:=gensetconstnode(pconstset(pconstsym(srsym)^.value),
  1085. psetdef(pconstsym(srsym)^.consttype.def));
  1086. constord :
  1087. p1:=genordinalconstnode(pconstsym(srsym)^.value,
  1088. pconstsym(srsym)^.consttype.def);
  1089. constpointer :
  1090. p1:=genpointerconstnode(pconstsym(srsym)^.value,
  1091. pconstsym(srsym)^.consttype.def);
  1092. constnil :
  1093. p1:=genzeronode(niln);
  1094. constresourcestring:
  1095. begin
  1096. p1:=genloadnode(pvarsym(srsym),srsymtable);
  1097. p1^.resulttype:=cansistringdef;
  1098. end;
  1099. end;
  1100. pd:=p1^.resulttype;
  1101. end;
  1102. procsym : begin
  1103. { are we in a class method ? }
  1104. possible_error:=(srsymtable^.symtabletype=objectsymtable) and
  1105. assigned(aktprocsym) and
  1106. (po_classmethod in aktprocsym^.definition^.procoptions);
  1107. p1:=gencallnode(pprocsym(srsym),srsymtable);
  1108. p1^.unit_specific:=unit_specific;
  1109. do_proc_call(getaddr or
  1110. (getprocvar and
  1111. ((block_type=bt_const) or
  1112. ((m_tp_procvar in aktmodeswitches) and
  1113. proc_to_procvar_equal(pprocsym(srsym)^.definition,getprocvardef)
  1114. )
  1115. )
  1116. ),again,p1,pd);
  1117. if (block_type=bt_const) and
  1118. getprocvar then
  1119. handle_procvar(getprocvardef,p1);
  1120. if possible_error and
  1121. not(po_classmethod in p1^.procdefinition^.procoptions) then
  1122. Message(parser_e_only_class_methods);
  1123. end;
  1124. propertysym : begin
  1125. { access to property in a method }
  1126. { are we in a class method ? }
  1127. if (srsymtable^.symtabletype=objectsymtable) and
  1128. assigned(aktprocsym) and
  1129. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1130. Message(parser_e_only_class_methods);
  1131. { no method pointer }
  1132. p1:=nil;
  1133. handle_propertysym(srsym,srsymtable,p1,pd);
  1134. end;
  1135. errorsym : begin
  1136. p1:=genzeronode(errorn);
  1137. p1^.resulttype:=generrordef;
  1138. pd:=generrordef;
  1139. if token=_LKLAMMER then
  1140. begin
  1141. consume(_LKLAMMER);
  1142. parse_paras(false,false);
  1143. consume(_RKLAMMER);
  1144. end;
  1145. end;
  1146. else
  1147. begin
  1148. p1:=genzeronode(errorn);
  1149. pd:=generrordef;
  1150. Message(cg_e_illegal_expression);
  1151. end;
  1152. end; { end case }
  1153. end;
  1154. end;
  1155. end;
  1156. end;
  1157. {---------------------------------------------
  1158. Factor_Read_Set
  1159. ---------------------------------------------}
  1160. { Read a set between [] }
  1161. function factor_read_set:ptree;
  1162. var
  1163. p1,
  1164. lastp,
  1165. buildp : ptree;
  1166. begin
  1167. buildp:=nil;
  1168. { be sure that a least one arrayconstructn is used, also for an
  1169. empty [] }
  1170. if token=_RECKKLAMMER then
  1171. buildp:=gennode(arrayconstructn,nil,buildp)
  1172. else
  1173. begin
  1174. while true do
  1175. begin
  1176. p1:=comp_expr(true);
  1177. if token=_POINTPOINT then
  1178. begin
  1179. consume(_POINTPOINT);
  1180. p2:=comp_expr(true);
  1181. p1:=gennode(arrayconstructrangen,p1,p2);
  1182. end;
  1183. { insert at the end of the tree, to get the correct order }
  1184. if not assigned(buildp) then
  1185. begin
  1186. buildp:=gennode(arrayconstructn,p1,nil);
  1187. lastp:=buildp;
  1188. end
  1189. else
  1190. begin
  1191. lastp^.right:=gennode(arrayconstructn,p1,nil);
  1192. lastp:=lastp^.right;
  1193. end;
  1194. { there could be more elements }
  1195. if token=_COMMA then
  1196. consume(_COMMA)
  1197. else
  1198. break;
  1199. end;
  1200. end;
  1201. factor_read_set:=buildp;
  1202. end;
  1203. {---------------------------------------------
  1204. Helpers
  1205. ---------------------------------------------}
  1206. procedure check_tokenpos;
  1207. begin
  1208. if (p1<>oldp1) then
  1209. begin
  1210. if assigned(p1) then
  1211. set_tree_filepos(p1,filepos);
  1212. oldp1:=p1;
  1213. filepos:=tokenpos;
  1214. end;
  1215. end;
  1216. {---------------------------------------------
  1217. PostFixOperators
  1218. ---------------------------------------------}
  1219. procedure postfixoperators;
  1220. var
  1221. store_static : boolean;
  1222. { p1 and p2 must contain valid value_str }
  1223. begin
  1224. check_tokenpos;
  1225. while again do
  1226. begin
  1227. { prevent crashes with unknown types }
  1228. if not assigned(pd) then
  1229. begin
  1230. { try to recover }
  1231. repeat
  1232. case token of
  1233. _CARET:
  1234. consume(_CARET);
  1235. _POINT:
  1236. begin
  1237. consume(_POINT);
  1238. consume(_ID);
  1239. end;
  1240. _LECKKLAMMER:
  1241. begin
  1242. repeat
  1243. consume(token);
  1244. until token in [_RECKKLAMMER,_SEMICOLON];
  1245. end;
  1246. else
  1247. break;
  1248. end;
  1249. until false;
  1250. exit;
  1251. end;
  1252. { handle token }
  1253. case token of
  1254. _CARET:
  1255. begin
  1256. consume(_CARET);
  1257. if (pd^.deftype<>pointerdef) then
  1258. begin
  1259. { ^ as binary operator is a problem!!!! (FK) }
  1260. again:=false;
  1261. Message(cg_e_invalid_qualifier);
  1262. disposetree(p1);
  1263. p1:=genzeronode(errorn);
  1264. end
  1265. else
  1266. begin
  1267. p1:=gensinglenode(derefn,p1);
  1268. pd:=ppointerdef(pd)^.pointertype.def;
  1269. end;
  1270. end;
  1271. _LECKKLAMMER:
  1272. begin
  1273. if (pd^.deftype=objectdef) and pobjectdef(pd)^.is_class then
  1274. begin
  1275. { default property }
  1276. propsym:=search_default_property(pobjectdef(pd));
  1277. if not(assigned(propsym)) then
  1278. begin
  1279. disposetree(p1);
  1280. p1:=genzeronode(errorn);
  1281. again:=false;
  1282. message(parser_e_no_default_property_available);
  1283. end
  1284. else
  1285. handle_propertysym(propsym,propsym^.owner,p1,pd);
  1286. end
  1287. else
  1288. begin
  1289. consume(_LECKKLAMMER);
  1290. repeat
  1291. case pd^.deftype of
  1292. pointerdef:
  1293. begin
  1294. p2:=comp_expr(true);
  1295. p1:=gennode(vecn,p1,p2);
  1296. pd:=ppointerdef(pd)^.pointertype.def;
  1297. end;
  1298. stringdef : begin
  1299. p2:=comp_expr(true);
  1300. p1:=gennode(vecn,p1,p2);
  1301. pd:=cchardef
  1302. end;
  1303. arraydef : begin
  1304. p2:=comp_expr(true);
  1305. { support SEG:OFS for go32v2 Mem[] }
  1306. if (target_info.target=target_i386_go32v2) and
  1307. (p1^.treetype=loadn) and
  1308. assigned(p1^.symtableentry) and
  1309. assigned(p1^.symtableentry^.owner^.name) and
  1310. (p1^.symtableentry^.owner^.name^='SYSTEM') and
  1311. ((p1^.symtableentry^.name='MEM') or
  1312. (p1^.symtableentry^.name='MEMW') or
  1313. (p1^.symtableentry^.name='MEML')) then
  1314. begin
  1315. if (token=_COLON) then
  1316. begin
  1317. consume(_COLON);
  1318. p3:=gennode(muln,genordinalconstnode($10,s32bitdef),p2);
  1319. p2:=comp_expr(true);
  1320. p2:=gennode(addn,p2,p3);
  1321. p1:=gennode(vecn,p1,p2);
  1322. p1^.memseg:=true;
  1323. p1^.memindex:=true;
  1324. end
  1325. else
  1326. begin
  1327. p1:=gennode(vecn,p1,p2);
  1328. p1^.memindex:=true;
  1329. end;
  1330. end
  1331. else
  1332. p1:=gennode(vecn,p1,p2);
  1333. pd:=parraydef(pd)^.elementtype.def;
  1334. end;
  1335. else
  1336. begin
  1337. Message(cg_e_invalid_qualifier);
  1338. disposetree(p1);
  1339. p1:=genzeronode(errorn);
  1340. again:=false;
  1341. end;
  1342. end;
  1343. if token=_COMMA then
  1344. consume(_COMMA)
  1345. else
  1346. break;
  1347. until false;
  1348. consume(_RECKKLAMMER);
  1349. end;
  1350. end;
  1351. _POINT : begin
  1352. consume(_POINT);
  1353. if (pd^.deftype=pointerdef) and
  1354. (m_autoderef in aktmodeswitches) then
  1355. begin
  1356. p1:=gensinglenode(derefn,p1);
  1357. pd:=ppointerdef(pd)^.pointertype.def;
  1358. end;
  1359. case pd^.deftype of
  1360. recorddef:
  1361. begin
  1362. sym:=pvarsym(precorddef(pd)^.symtable^.search(pattern));
  1363. if sym=nil then
  1364. begin
  1365. Message1(sym_e_illegal_field,pattern);
  1366. disposetree(p1);
  1367. p1:=genzeronode(errorn);
  1368. end
  1369. else
  1370. begin
  1371. p1:=gensubscriptnode(sym,p1);
  1372. pd:=sym^.vartype.def;
  1373. end;
  1374. consume(_ID);
  1375. end;
  1376. classrefdef:
  1377. begin
  1378. classh:=pobjectdef(pclassrefdef(pd)^.pointertype.def);
  1379. sym:=nil;
  1380. while assigned(classh) do
  1381. begin
  1382. sym:=pvarsym(classh^.symtable^.search(pattern));
  1383. srsymtable:=classh^.symtable;
  1384. if assigned(sym) then
  1385. break;
  1386. classh:=classh^.childof;
  1387. end;
  1388. if sym=nil then
  1389. begin
  1390. Message1(sym_e_id_no_member,pattern);
  1391. disposetree(p1);
  1392. p1:=genzeronode(errorn);
  1393. { try to clean up }
  1394. pd:=generrordef;
  1395. consume(_ID);
  1396. end
  1397. else
  1398. begin
  1399. consume(_ID);
  1400. do_member_read(getaddr,sym,p1,pd,again);
  1401. end;
  1402. end;
  1403. objectdef:
  1404. begin
  1405. classh:=pobjectdef(pd);
  1406. sym:=nil;
  1407. store_static:=allow_only_static;
  1408. allow_only_static:=false;
  1409. while assigned(classh) do
  1410. begin
  1411. sym:=pvarsym(classh^.symtable^.search(pattern));
  1412. srsymtable:=classh^.symtable;
  1413. if assigned(sym) then
  1414. break;
  1415. classh:=classh^.childof;
  1416. end;
  1417. allow_only_static:=store_static;
  1418. if sym=nil then
  1419. begin
  1420. Message1(sym_e_id_no_member,pattern);
  1421. disposetree(p1);
  1422. p1:=genzeronode(errorn);
  1423. { try to clean up }
  1424. pd:=generrordef;
  1425. consume(_ID);
  1426. end
  1427. else
  1428. begin
  1429. consume(_ID);
  1430. do_member_read(getaddr,sym,p1,pd,again);
  1431. end;
  1432. end;
  1433. pointerdef:
  1434. begin
  1435. Message(cg_e_invalid_qualifier);
  1436. if ppointerdef(pd)^.pointertype.def^.deftype in [recorddef,objectdef,classrefdef] then
  1437. Message(parser_h_maybe_deref_caret_missing);
  1438. end;
  1439. else
  1440. begin
  1441. Message(cg_e_invalid_qualifier);
  1442. disposetree(p1);
  1443. p1:=genzeronode(errorn);
  1444. end;
  1445. end;
  1446. end;
  1447. else
  1448. begin
  1449. { is this a procedure variable ? }
  1450. if assigned(pd) then
  1451. begin
  1452. if (pd^.deftype=procvardef) then
  1453. begin
  1454. if getprocvar and is_equal(pd,getprocvardef) then
  1455. again:=false
  1456. else
  1457. if (token=_LKLAMMER) or
  1458. ((pprocvardef(pd)^.para^.empty) and
  1459. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1460. (not afterassignment) and
  1461. (not in_args)) then
  1462. begin
  1463. { do this in a strange way }
  1464. { it's not a clean solution }
  1465. p2:=p1;
  1466. p1:=gencallnode(nil,nil);
  1467. p1^.right:=p2;
  1468. p1^.unit_specific:=unit_specific;
  1469. p1^.symtableprocentry:=pprocsym(sym);
  1470. if token=_LKLAMMER then
  1471. begin
  1472. consume(_LKLAMMER);
  1473. p1^.left:=parse_paras(false,false);
  1474. consume(_RKLAMMER);
  1475. end;
  1476. pd:=pprocvardef(pd)^.rettype.def;
  1477. { proc():= is never possible }
  1478. if token=_ASSIGNMENT then
  1479. begin
  1480. Message(cg_e_illegal_expression);
  1481. p1:=genzeronode(errorn);
  1482. again:=false;
  1483. end;
  1484. p1^.resulttype:=pd;
  1485. end
  1486. else
  1487. again:=false;
  1488. p1^.resulttype:=pd;
  1489. end
  1490. else
  1491. again:=false;
  1492. end
  1493. else
  1494. again:=false;
  1495. end;
  1496. end;
  1497. check_tokenpos;
  1498. end; { while again }
  1499. end;
  1500. {---------------------------------------------
  1501. Factor (Main)
  1502. ---------------------------------------------}
  1503. begin
  1504. oldp1:=nil;
  1505. p1:=nil;
  1506. filepos:=tokenpos;
  1507. if token=_ID then
  1508. begin
  1509. factor_read_id;
  1510. { handle post fix operators }
  1511. postfixoperators;
  1512. end
  1513. else
  1514. case token of
  1515. _NEW : begin
  1516. consume(_NEW);
  1517. consume(_LKLAMMER);
  1518. {allow_type:=true;}
  1519. p1:=factor(false);
  1520. {allow_type:=false;}
  1521. if p1^.treetype<>typen then
  1522. begin
  1523. Message(type_e_type_id_expected);
  1524. disposetree(p1);
  1525. pd:=generrordef;
  1526. end
  1527. else
  1528. pd:=p1^.typenodetype;
  1529. pd2:=pd;
  1530. if (pd^.deftype<>pointerdef) then
  1531. Message1(type_e_pointer_type_expected,pd^.typename)
  1532. else
  1533. if token=_RKLAMMER then
  1534. begin
  1535. if (ppointerdef(pd)^.pointertype.def^.deftype=objectdef) and
  1536. (oo_has_vmt in pobjectdef(ppointerdef(pd)^.pointertype.def)^.objectoptions) then
  1537. Message(parser_w_use_extended_syntax_for_objects);
  1538. p1:=gensinglenode(newn,nil);
  1539. p1^.resulttype:=pd2;
  1540. consume(_RKLAMMER);
  1541. end
  1542. else
  1543. begin
  1544. disposetree(p1);
  1545. p1:=genzeronode(hnewn);
  1546. p1^.resulttype:=ppointerdef(pd)^.pointertype.def;
  1547. consume(_COMMA);
  1548. afterassignment:=false;
  1549. { determines the current object defintion }
  1550. classh:=pobjectdef(ppointerdef(pd)^.pointertype.def);
  1551. if classh^.deftype<>objectdef then
  1552. Message(parser_e_pointer_to_class_expected)
  1553. else
  1554. begin
  1555. { check for an abstract class }
  1556. if (oo_has_abstract in classh^.objectoptions) then
  1557. Message(sym_e_no_instance_of_abstract_object);
  1558. { search the constructor also in the symbol tables of
  1559. the parents }
  1560. sym:=nil;
  1561. while assigned(classh) do
  1562. begin
  1563. sym:=pvarsym(classh^.symtable^.search(pattern));
  1564. srsymtable:=classh^.symtable;
  1565. if assigned(sym) then
  1566. break;
  1567. classh:=classh^.childof;
  1568. end;
  1569. consume(_ID);
  1570. do_member_read(false,sym,p1,pd,again);
  1571. if (p1^.treetype<>calln) or
  1572. (assigned(p1^.procdefinition) and
  1573. (p1^.procdefinition^.proctypeoption<>potype_constructor)) then
  1574. Message(parser_e_expr_have_to_be_constructor_call);
  1575. end;
  1576. p1:=gensinglenode(newn,p1);
  1577. { set the resulttype }
  1578. p1^.resulttype:=pd2;
  1579. consume(_RKLAMMER);
  1580. end;
  1581. postfixoperators;
  1582. end;
  1583. _SELF : begin
  1584. again:=true;
  1585. consume(_SELF);
  1586. if not assigned(procinfo^._class) then
  1587. begin
  1588. p1:=genzeronode(errorn);
  1589. pd:=generrordef;
  1590. again:=false;
  1591. Message(parser_e_self_not_in_method);
  1592. end
  1593. else
  1594. begin
  1595. if (po_classmethod in aktprocsym^.definition^.procoptions) then
  1596. begin
  1597. { self in class methods is a class reference type }
  1598. pd:=new(pclassrefdef,init(procinfo^._class));
  1599. p1:=genselfnode(pd);
  1600. p1^.resulttype:=pd;
  1601. end
  1602. else
  1603. begin
  1604. p1:=genselfnode(procinfo^._class);
  1605. p1^.resulttype:=procinfo^._class;
  1606. end;
  1607. pd:=p1^.resulttype;
  1608. postfixoperators;
  1609. end;
  1610. end;
  1611. _INHERITED : begin
  1612. again:=true;
  1613. consume(_INHERITED);
  1614. if assigned(procinfo^._class) then
  1615. begin
  1616. classh:=procinfo^._class^.childof;
  1617. while assigned(classh) do
  1618. begin
  1619. srsymtable:=pobjectdef(classh)^.symtable;
  1620. sym:=pvarsym(srsymtable^.search(pattern));
  1621. if assigned(sym) then
  1622. begin
  1623. { only for procsyms we need to set the type (PFV) }
  1624. if sym^.typ=procsym then
  1625. begin
  1626. p1:=genzeronode(typen);
  1627. p1^.resulttype:=classh;
  1628. pd:=p1^.resulttype;
  1629. end
  1630. else
  1631. p1:=nil;
  1632. consume(_ID);
  1633. do_member_read(false,sym,p1,pd,again);
  1634. break;
  1635. end;
  1636. classh:=classh^.childof;
  1637. end;
  1638. if classh=nil then
  1639. begin
  1640. Message1(sym_e_id_no_member,pattern);
  1641. again:=false;
  1642. pd:=generrordef;
  1643. p1:=genzeronode(errorn);
  1644. end;
  1645. end
  1646. else
  1647. begin
  1648. Message(parser_e_generic_methods_only_in_methods);
  1649. again:=false;
  1650. pd:=generrordef;
  1651. p1:=genzeronode(errorn);
  1652. end;
  1653. postfixoperators;
  1654. end;
  1655. _INTCONST : begin
  1656. valint(pattern,l,code);
  1657. if code<>0 then
  1658. begin
  1659. val(pattern,d,code);
  1660. if code<>0 then
  1661. begin
  1662. Message(cg_e_invalid_integer);
  1663. consume(_INTCONST);
  1664. l:=1;
  1665. p1:=genordinalconstnode(l,s32bitdef);
  1666. end
  1667. else
  1668. begin
  1669. consume(_INTCONST);
  1670. p1:=genrealconstnode(d,bestrealdef^);
  1671. end;
  1672. end
  1673. else
  1674. begin
  1675. consume(_INTCONST);
  1676. p1:=genordinalconstnode(l,s32bitdef);
  1677. end;
  1678. end;
  1679. _REALNUMBER : begin
  1680. val(pattern,d,code);
  1681. if code<>0 then
  1682. begin
  1683. Message(parser_e_error_in_real);
  1684. d:=1.0;
  1685. end;
  1686. consume(_REALNUMBER);
  1687. p1:=genrealconstnode(d,bestrealdef^);
  1688. end;
  1689. _STRING : begin
  1690. pd:=string_dec;
  1691. { STRING can be also a type cast }
  1692. if token=_LKLAMMER then
  1693. begin
  1694. consume(_LKLAMMER);
  1695. p1:=comp_expr(true);
  1696. consume(_RKLAMMER);
  1697. p1:=gentypeconvnode(p1,pd);
  1698. p1^.explizit:=true;
  1699. { handle postfix operators here e.g. string(a)[10] }
  1700. again:=true;
  1701. postfixoperators;
  1702. end
  1703. else
  1704. p1:=gentypenode(pd,nil);
  1705. end;
  1706. _FILE : begin
  1707. pd:=cfiledef;
  1708. consume(_FILE);
  1709. { FILE can be also a type cast }
  1710. if token=_LKLAMMER then
  1711. begin
  1712. consume(_LKLAMMER);
  1713. p1:=comp_expr(true);
  1714. consume(_RKLAMMER);
  1715. p1:=gentypeconvnode(p1,pd);
  1716. p1^.explizit:=true;
  1717. { handle postfix operators here e.g. string(a)[10] }
  1718. again:=true;
  1719. postfixoperators;
  1720. end
  1721. else
  1722. p1:=gentypenode(pd,nil);
  1723. end;
  1724. _CSTRING : begin
  1725. p1:=genstringconstnode(pattern,st_default);
  1726. consume(_CSTRING);
  1727. end;
  1728. _CCHAR : begin
  1729. p1:=genordinalconstnode(ord(pattern[1]),cchardef);
  1730. consume(_CCHAR);
  1731. end;
  1732. _KLAMMERAFFE : begin
  1733. consume(_KLAMMERAFFE);
  1734. got_addrn:=true;
  1735. { support both @<x> and @(<x>) }
  1736. if token=_LKLAMMER then
  1737. begin
  1738. consume(_LKLAMMER);
  1739. p1:=factor(true);
  1740. consume(_RKLAMMER);
  1741. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1742. begin
  1743. { we need the resulttype }
  1744. { of the expression in pd }
  1745. do_firstpass(p1);
  1746. pd:=p1^.resulttype;
  1747. again:=true;
  1748. postfixoperators;
  1749. end;
  1750. end
  1751. else
  1752. p1:=factor(true);
  1753. got_addrn:=false;
  1754. p1:=gensinglenode(addrn,p1);
  1755. end;
  1756. _LKLAMMER : begin
  1757. consume(_LKLAMMER);
  1758. p1:=comp_expr(true);
  1759. consume(_RKLAMMER);
  1760. { it's not a good solution }
  1761. { but (a+b)^ makes some problems }
  1762. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1763. begin
  1764. { we need the resulttype }
  1765. { of the expression in pd }
  1766. do_firstpass(p1);
  1767. pd:=p1^.resulttype;
  1768. again:=true;
  1769. postfixoperators;
  1770. end;
  1771. end;
  1772. _LECKKLAMMER : begin
  1773. consume(_LECKKLAMMER);
  1774. p1:=factor_read_set;
  1775. consume(_RECKKLAMMER);
  1776. end;
  1777. _PLUS : begin
  1778. consume(_PLUS);
  1779. p1:=factor(false);
  1780. end;
  1781. _MINUS : begin
  1782. consume(_MINUS);
  1783. p1:=sub_expr(oppower,false);
  1784. p1:=gensinglenode(unaryminusn,p1);
  1785. end;
  1786. _NOT : begin
  1787. consume(_NOT);
  1788. p1:=factor(false);
  1789. p1:=gensinglenode(notn,p1);
  1790. end;
  1791. _TRUE : begin
  1792. consume(_TRUE);
  1793. p1:=genordinalconstnode(1,booldef);
  1794. end;
  1795. _FALSE : begin
  1796. consume(_FALSE);
  1797. p1:=genordinalconstnode(0,booldef);
  1798. end;
  1799. _NIL : begin
  1800. consume(_NIL);
  1801. p1:=genzeronode(niln);
  1802. end;
  1803. else
  1804. begin
  1805. p1:=genzeronode(errorn);
  1806. consume(token);
  1807. Message(cg_e_illegal_expression);
  1808. end;
  1809. end;
  1810. { generate error node if no node is created }
  1811. if not assigned(p1) then
  1812. p1:=genzeronode(errorn);
  1813. { tp7 procvar handling, but not if the next token
  1814. will be a := }
  1815. if (m_tp_procvar in aktmodeswitches) and
  1816. (token<>_ASSIGNMENT) then
  1817. check_tp_procvar(p1);
  1818. factor:=p1;
  1819. check_tokenpos;
  1820. end;
  1821. {$ifdef fpc}
  1822. {$maxfpuregisters default}
  1823. {$endif fpc}
  1824. {****************************************************************************
  1825. Sub_Expr
  1826. ****************************************************************************}
  1827. type
  1828. Ttok2nodeRec=record
  1829. tok : ttoken;
  1830. nod : ttreetyp;
  1831. end;
  1832. const
  1833. tok2nodes=23;
  1834. tok2node:array[1..tok2nodes] of ttok2noderec=(
  1835. (tok:_PLUS ;nod:addn),
  1836. (tok:_MINUS ;nod:subn),
  1837. (tok:_STAR ;nod:muln),
  1838. (tok:_SLASH ;nod:slashn),
  1839. (tok:_EQUAL ;nod:equaln),
  1840. (tok:_GT ;nod:gtn),
  1841. (tok:_LT ;nod:ltn),
  1842. (tok:_GTE ;nod:gten),
  1843. (tok:_LTE ;nod:lten),
  1844. (tok:_SYMDIF ;nod:symdifn),
  1845. (tok:_STARSTAR;nod:starstarn),
  1846. (tok:_OP_AS ;nod:asn),
  1847. (tok:_OP_IN ;nod:inn),
  1848. (tok:_OP_IS ;nod:isn),
  1849. (tok:_OP_OR ;nod:orn),
  1850. (tok:_OP_AND ;nod:andn),
  1851. (tok:_OP_DIV ;nod:divn),
  1852. (tok:_OP_MOD ;nod:modn),
  1853. (tok:_OP_SHL ;nod:shln),
  1854. (tok:_OP_SHR ;nod:shrn),
  1855. (tok:_OP_XOR ;nod:xorn),
  1856. (tok:_CARET ;nod:caretn),
  1857. (tok:_UNEQUAL ;nod:unequaln)
  1858. );
  1859. { Warning these stay be ordered !! }
  1860. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1861. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1862. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1863. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1864. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1865. [_STARSTAR] );
  1866. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):Ptree;
  1867. {Reads a subexpression while the operators are of the current precedence
  1868. level, or any higher level. Replaces the old term, simpl_expr and
  1869. simpl2_expr.}
  1870. var
  1871. low,high,mid : longint;
  1872. p1,p2 : Ptree;
  1873. oldt : Ttoken;
  1874. filepos : tfileposinfo;
  1875. begin
  1876. if pred_level=highest_precedence then
  1877. p1:=factor(false)
  1878. else
  1879. p1:=sub_expr(succ(pred_level),true);
  1880. repeat
  1881. if (token in operator_levels[pred_level]) and
  1882. ((token<>_EQUAL) or accept_equal) then
  1883. begin
  1884. oldt:=token;
  1885. filepos:=tokenpos;
  1886. consume(token);
  1887. if pred_level=highest_precedence then
  1888. p2:=factor(false)
  1889. else
  1890. p2:=sub_expr(succ(pred_level),true);
  1891. low:=1;
  1892. high:=tok2nodes;
  1893. while (low<high) do
  1894. begin
  1895. mid:=(low+high+1) shr 1;
  1896. if oldt<tok2node[mid].tok then
  1897. high:=mid-1
  1898. else
  1899. low:=mid;
  1900. end;
  1901. if tok2node[high].tok=oldt then
  1902. p1:=gennode(tok2node[high].nod,p1,p2)
  1903. else
  1904. p1:=gennode(nothingn,p1,p2);
  1905. set_tree_filepos(p1,filepos);
  1906. end
  1907. else
  1908. break;
  1909. until false;
  1910. sub_expr:=p1;
  1911. end;
  1912. function comp_expr(accept_equal : boolean):Ptree;
  1913. var
  1914. oldafterassignment : boolean;
  1915. p1 : ptree;
  1916. begin
  1917. oldafterassignment:=afterassignment;
  1918. afterassignment:=true;
  1919. p1:=sub_expr(opcompare,accept_equal);
  1920. afterassignment:=oldafterassignment;
  1921. comp_expr:=p1;
  1922. end;
  1923. function expr : ptree;
  1924. var
  1925. p1,p2 : ptree;
  1926. oldafterassignment : boolean;
  1927. oldp1 : ptree;
  1928. filepos : tfileposinfo;
  1929. begin
  1930. oldafterassignment:=afterassignment;
  1931. p1:=sub_expr(opcompare,true);
  1932. filepos:=tokenpos;
  1933. if (m_tp_procvar in aktmodeswitches) and
  1934. (token<>_ASSIGNMENT) then
  1935. check_tp_procvar(p1);
  1936. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  1937. afterassignment:=true;
  1938. oldp1:=p1;
  1939. case token of
  1940. _POINTPOINT : begin
  1941. consume(_POINTPOINT);
  1942. p2:=sub_expr(opcompare,true);
  1943. p1:=gennode(rangen,p1,p2);
  1944. end;
  1945. _ASSIGNMENT : begin
  1946. consume(_ASSIGNMENT);
  1947. { avoid a firstpass of a procedure if
  1948. it must be assigned to a procvar }
  1949. { should be recursive for a:=b:=c !!! }
  1950. if (p1^.resulttype<>nil) and (p1^.resulttype^.deftype=procvardef) then
  1951. begin
  1952. getprocvar:=true;
  1953. getprocvardef:=pprocvardef(p1^.resulttype);
  1954. end;
  1955. p2:=sub_expr(opcompare,true);
  1956. if getprocvar then
  1957. handle_procvar(getprocvardef,p2);
  1958. getprocvar:=false;
  1959. p1:=gennode(assignn,p1,p2);
  1960. end;
  1961. { this is the code for C like assignements }
  1962. { from an improvement of Peter Schaefer }
  1963. _PLUSASN : begin
  1964. consume(_PLUSASN );
  1965. p2:=sub_expr(opcompare,true);
  1966. p1:=gennode(assignn,p1,gennode(addn,getcopy(p1),p2));
  1967. { was first
  1968. p1:=gennode(assignn,p1,gennode(addn,p1,p2));
  1969. but disposetree assumes that we have a real
  1970. *** tree *** }
  1971. end;
  1972. _MINUSASN : begin
  1973. consume(_MINUSASN );
  1974. p2:=sub_expr(opcompare,true);
  1975. p1:=gennode(assignn,p1,gennode(subn,getcopy(p1),p2));
  1976. end;
  1977. _STARASN : begin
  1978. consume(_STARASN );
  1979. p2:=sub_expr(opcompare,true);
  1980. p1:=gennode(assignn,p1,gennode(muln,getcopy(p1),p2));
  1981. end;
  1982. _SLASHASN : begin
  1983. consume(_SLASHASN );
  1984. p2:=sub_expr(opcompare,true);
  1985. p1:=gennode(assignn,p1,gennode(slashn,getcopy(p1),p2));
  1986. end;
  1987. end;
  1988. afterassignment:=oldafterassignment;
  1989. if p1<>oldp1 then
  1990. set_tree_filepos(p1,filepos);
  1991. expr:=p1;
  1992. end;
  1993. function get_intconst:longint;
  1994. {Reads an expression, tries to evalute it and check if it is an integer
  1995. constant. Then the constant is returned.}
  1996. var
  1997. p:Ptree;
  1998. begin
  1999. p:=comp_expr(true);
  2000. do_firstpass(p);
  2001. if not codegenerror then
  2002. begin
  2003. if (p^.treetype<>ordconstn) and
  2004. (p^.resulttype^.deftype=orddef) and
  2005. not(Porddef(p^.resulttype)^.typ in [uvoid,uchar,bool8bit,bool16bit,bool32bit]) then
  2006. Message(cg_e_illegal_expression)
  2007. else
  2008. get_intconst:=p^.value;
  2009. end;
  2010. disposetree(p);
  2011. end;
  2012. function get_stringconst:string;
  2013. {Reads an expression, tries to evaluate it and checks if it is a string
  2014. constant. Then the constant is returned.}
  2015. var
  2016. p:Ptree;
  2017. begin
  2018. get_stringconst:='';
  2019. p:=comp_expr(true);
  2020. do_firstpass(p);
  2021. if p^.treetype<>stringconstn then
  2022. begin
  2023. if (p^.treetype=ordconstn) and is_char(p^.resulttype) then
  2024. get_stringconst:=char(p^.value)
  2025. else
  2026. Message(cg_e_illegal_expression);
  2027. end
  2028. else
  2029. get_stringconst:=strpas(p^.value_str);
  2030. disposetree(p);
  2031. end;
  2032. end.
  2033. {
  2034. $Log$
  2035. Revision 1.171 2000-03-16 15:13:03 pierre
  2036. + oppower
  2037. Revision 1.170 2000/03/14 15:50:19 pierre
  2038. * - 1**4 = -1 fix
  2039. Revision 1.169 2000/02/13 14:21:50 jonas
  2040. * modifications to make the compiler functional when compiled with
  2041. -Or
  2042. Revision 1.168 2000/02/09 13:22:56 peter
  2043. * log truncated
  2044. Revision 1.167 2000/01/19 22:41:58 florian
  2045. * corrected wrong error message of a member of a class/object/classref wasn't found
  2046. Revision 1.166 2000/01/09 23:16:05 peter
  2047. * added st_default stringtype
  2048. * genstringconstnode extended with stringtype parameter using st_default
  2049. will do the old behaviour
  2050. Revision 1.165 2000/01/07 01:14:28 peter
  2051. * updated copyright to 2000
  2052. Revision 1.164 1999/12/20 21:24:29 pierre
  2053. * web bug769 fix
  2054. Revision 1.163 1999/12/01 12:42:32 peter
  2055. * fixed bug 698
  2056. * removed some notes about unused vars
  2057. Revision 1.162 1999/11/30 10:40:44 peter
  2058. + ttype, tsymlist
  2059. Revision 1.161 1999/11/18 15:34:47 pierre
  2060. * Notes/Hints for local syms changed to
  2061. Set_varstate function
  2062. Revision 1.160 1999/11/17 17:05:01 pierre
  2063. * Notes/hints changes
  2064. Revision 1.159 1999/11/15 17:52:59 pierre
  2065. + one field added for ttoken record for operator
  2066. linking the id to the corresponding operator token that
  2067. can now now all be overloaded
  2068. * overloaded operators are resetted to nil in InitSymtable
  2069. (bug when trying to compile a uint that overloads operators twice)
  2070. Revision 1.158 1999/11/14 15:57:35 peter
  2071. * fixed crash with an errordef
  2072. Revision 1.157 1999/11/08 14:02:16 florian
  2073. * problem with "index X"-properties solved
  2074. * typed constants of class references are now allowed
  2075. Revision 1.156 1999/11/07 23:21:30 florian
  2076. * previous fix for 517 was imcomplete: there was a problem if the property
  2077. had only an index
  2078. Revision 1.155 1999/11/07 23:16:49 florian
  2079. * finally bug 517 solved ...
  2080. Revision 1.154 1999/11/06 14:34:21 peter
  2081. * truncated log to 20 revs
  2082. Revision 1.153 1999/11/05 00:10:30 peter
  2083. * fixed inherited with properties
  2084. Revision 1.152 1999/10/27 16:06:19 peter
  2085. * check for object in extended new
  2086. Revision 1.151 1999/10/26 12:30:44 peter
  2087. * const parameter is now checked
  2088. * better and generic check if a node can be used for assigning
  2089. * export fixes
  2090. * procvar equal works now (it never had worked at least from 0.99.8)
  2091. * defcoll changed to linkedlist with pparaitem so it can easily be
  2092. walked both directions
  2093. Revision 1.150 1999/10/22 14:37:30 peter
  2094. * error when properties are passed to var parameters
  2095. Revision 1.149 1999/10/22 10:39:34 peter
  2096. * split type reading from pdecl to ptype unit
  2097. * parameter_dec routine is now used for procedure and procvars
  2098. Revision 1.148 1999/10/14 14:57:52 florian
  2099. - removed the hcodegen use in the new cg, use cgbase instead
  2100. }