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