pexpr.pas 88 KB

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