pexpr.pas 86 KB

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