pexpr.pas 88 KB

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