pexpr.pas 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  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 : psym;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(new(pstringdef,longinit(tordconstnode(p).value)))
  102. else
  103. if tordconstnode(p).value<>255 then
  104. t.setdef(new(pstringdef,shortinit(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 pobjectdef(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. (porddef(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:psym;st:psymtable;getaddr:boolean;var again : boolean;var p1:tnode);
  502. var
  503. prev_in_args : boolean;
  504. prevafterassn : boolean;
  505. hs,hs1 : pvarsym;
  506. para,p2 : tnode;
  507. hst : psymtable;
  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:=pvarsym(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:=pvarsym(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,pprocsym(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(pwithsymtable(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(pobjectdef(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 : pprocvardef;var p2 : tnode);
  585. procedure doconv(procvar : pprocvardef;var t : tnode);
  586. var
  587. hp : tnode;
  588. begin
  589. hp:=nil;
  590. if (proc_to_procvar_equal(pprocsym(tcallnode(t).symtableprocentry)^.definition,procvar)) then
  591. begin
  592. hp:=cloadnode.create(pprocsym(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 : psym;st : psymtable;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 ppropertysym(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 ppropertysym(sym)^.propoptions) then
  630. begin
  631. p2:=cordconstnode.create(ppropertysym(sym)^.index,ppropertysym(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 ppropertysym(sym)^.writeaccess^.empty then
  641. begin
  642. case ppropertysym(sym)^.writeaccess^.firstsym^.sym^.typ of
  643. procsym :
  644. begin
  645. { generate the method call }
  646. p1:=ccallnode.create(paras,
  647. pprocsym(ppropertysym(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:=pprocdef(ppropertysym(sym)^.writeaccess^.def);
  651. consume(_ASSIGNMENT);
  652. { read the expression }
  653. getprocvar:=(ppropertysym(sym)^.proptype.def^.deftype=procvardef);
  654. p2:=comp_expr(true);
  655. if getprocvar then
  656. handle_procvar(pprocvardef(ppropertysym(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:=ppropertysym(sym)^.writeaccess^.firstsym;
  667. while assigned(plist) do
  668. begin
  669. if p1=nil then
  670. p1:=cloadnode.create(pvarsym(pList^.sym),st)
  671. else
  672. p1:=csubscriptnode.create(pvarsym(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 ppropertysym(sym)^.readaccess^.empty then
  698. begin
  699. case ppropertysym(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:=ppropertysym(sym)^.readaccess^.firstsym;
  706. while assigned(plist) do
  707. begin
  708. if p1=nil then
  709. p1:=cloadnode.create(pvarsym(pList^.sym),st)
  710. else
  711. p1:=csubscriptnode.create(pvarsym(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,pprocsym(ppropertysym(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:=pprocdef(ppropertysym(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 : psym;var p1 : tnode;var again : boolean);
  742. var
  743. static_name : string;
  744. isclassref : boolean;
  745. srsymtable : psymtable;
  746. objdef : pobjectdef;
  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:=pobjectdef(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=unitsymtable) then
  774. Message(parser_e_cant_access_private_member);
  775. if (sp_protected in sym^.symoptions) and
  776. (objdef^.owner^.symtabletype=unitsymtable) then
  777. begin
  778. if assigned(aktprocsym^.definition^._class) then
  779. begin
  780. if not aktprocsym^.definition^._class^.is_related(objdef) then
  781. Message(parser_e_cant_access_protected_member);
  782. end
  783. else
  784. Message(parser_e_cant_access_protected_member);
  785. end;
  786. { we assume, that only procsyms and varsyms are in an object }
  787. { symbol table, for classes, properties are allowed }
  788. case sym^.typ of
  789. procsym:
  790. begin
  791. do_proc_call(sym,sym^.owner,
  792. getaddr or
  793. (getprocvar and
  794. ((block_type=bt_const) or
  795. ((m_tp_procvar in aktmodeswitches) and
  796. proc_to_procvar_equal(pprocsym(sym)^.definition,getprocvardef)
  797. )
  798. )
  799. ),again,tcallnode(p1));
  800. if (block_type=bt_const) and
  801. getprocvar then
  802. handle_procvar(getprocvardef,p1);
  803. { we need to know which procedure is called }
  804. do_resulttypepass(p1);
  805. { now we know the real method e.g. we can check for a class method }
  806. if isclassref and
  807. assigned(tcallnode(p1).procdefinition) and
  808. not(po_classmethod in tcallnode(p1).procdefinition^.procoptions) and
  809. not(tcallnode(p1).procdefinition^.proctypeoption=potype_constructor) then
  810. Message(parser_e_only_class_methods_via_class_ref);
  811. end;
  812. varsym:
  813. begin
  814. if isclassref then
  815. Message(parser_e_only_class_methods_via_class_ref);
  816. if (sp_static in sym^.symoptions) then
  817. begin
  818. static_name:=lower(sym^.owner^.name^)+'_'+sym^.name;
  819. searchsym(static_name,sym,srsymtable);
  820. p1.free;
  821. p1:=cloadnode.create(pvarsym(sym),srsymtable);
  822. end
  823. else
  824. p1:=csubscriptnode.create(pvarsym(sym),p1);
  825. end;
  826. propertysym:
  827. begin
  828. if isclassref then
  829. Message(parser_e_only_class_methods_via_class_ref);
  830. handle_propertysym(sym,sym^.owner,p1);
  831. end;
  832. else internalerror(16);
  833. end;
  834. end;
  835. end;
  836. {****************************************************************************
  837. Factor
  838. ****************************************************************************}
  839. {$ifdef fpc}
  840. {$maxfpuregisters 0}
  841. {$endif fpc}
  842. function factor(getaddr : boolean) : tnode;
  843. {---------------------------------------------
  844. Is_func_ret
  845. ---------------------------------------------}
  846. function is_func_ret(var p1:tnode;var sym : psym;var srsymtable:psymtable) : boolean;
  847. var
  848. p : pprocinfo;
  849. storesymtablestack : psymtable;
  850. begin
  851. is_func_ret:=false;
  852. if not assigned(procinfo) or
  853. ((sym^.typ<>funcretsym) and ((procinfo^.flags and pi_operator)=0)) then
  854. exit;
  855. p:=procinfo;
  856. while assigned(p) do
  857. begin
  858. { is this an access to a function result? Accessing _RESULT is
  859. always allowed and funcretn is generated }
  860. if assigned(p^.funcretsym) and
  861. ((pfuncretsym(sym)=p^.resultfuncretsym) or
  862. ((pfuncretsym(sym)=p^.funcretsym) or
  863. ((pvarsym(sym)=opsym) and ((p^.flags and pi_operator)<>0))) and
  864. (not is_void(p^.returntype.def)) and
  865. (token<>_LKLAMMER) and
  866. (not ((m_tp in aktmodeswitches) and (afterassignment or in_args)))
  867. ) then
  868. begin
  869. if ((pvarsym(sym)=opsym) and
  870. ((p^.flags and pi_operator)<>0)) then
  871. inc(opsym^.refs);
  872. p1:=cfuncretnode.create(p);
  873. is_func_ret:=true;
  874. if p^.funcret_state=vs_declared then
  875. begin
  876. p^.funcret_state:=vs_declared_and_first_found;
  877. include(p1.flags,nf_is_first_funcret);
  878. end;
  879. exit;
  880. end;
  881. p:=p^.parent;
  882. end;
  883. { we must use the function call, update the
  884. sym to be the procsym }
  885. if (sym^.typ=funcretsym) then
  886. begin
  887. storesymtablestack:=symtablestack;
  888. symtablestack:=sym^.owner^.next;
  889. searchsym(sym^.name,sym,srsymtable);
  890. if not assigned(sym) then
  891. sym:=generrorsym;
  892. if (sym^.typ<>procsym) then
  893. Message(cg_e_illegal_expression);
  894. symtablestack:=storesymtablestack;
  895. end;
  896. end;
  897. {---------------------------------------------
  898. Factor_read_id
  899. ---------------------------------------------}
  900. procedure factor_read_id(var p1:tnode;var again:boolean);
  901. var
  902. pc : pchar;
  903. len : longint;
  904. srsym : psym;
  905. possible_error : boolean;
  906. srsymtable : psymtable;
  907. htype : ttype;
  908. static_name : string;
  909. begin
  910. { allow post fix operators }
  911. again:=true;
  912. consume_sym(srsym,srsymtable);
  913. if not is_func_ret(p1,srsym,srsymtable) then
  914. begin
  915. { check semantics of private }
  916. if (srsym^.typ in [propertysym,procsym,varsym]) and
  917. (srsym^.owner^.symtabletype=objectsymtable) then
  918. begin
  919. if (sp_private in srsym^.symoptions) and
  920. (pobjectdef(srsym^.owner^.defowner)^.owner^.symtabletype=unitsymtable) then
  921. Message(parser_e_cant_access_private_member);
  922. end;
  923. case srsym^.typ of
  924. absolutesym :
  925. begin
  926. p1:=cloadnode.create(pvarsym(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(pvarsym(srsym),srsymtable);
  941. if pvarsym(srsym)^.varstate=vs_declared then
  942. begin
  943. include(p1.flags,nf_first);
  944. { set special between first loaded until checked in resulttypepass }
  945. pvarsym(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(psyssym(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(pobjectdef(htype.def)) then
  990. begin
  991. p1:=ctypenode.create(htype);
  992. { search also in inherited methods }
  993. repeat
  994. srsym:=pvarsym(pobjectdef(htype.def)^.symtable^.search(pattern));
  995. if assigned(srsym) then
  996. break;
  997. htype.def:=pobjectdef(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:=ctypenode.create(htype);
  1013. { TP allows also @TMenu.Load if Load is only }
  1014. { defined in an anchestor class }
  1015. srsym:=pvarsym(search_class_member(pobjectdef(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:=pvarsym(search_class_member(pobjectdef(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. begin
  1057. { generate a type node }
  1058. { (for typeof etc) }
  1059. if allow_type then
  1060. p1:=ctypenode.create(htype)
  1061. else
  1062. Message(parser_e_no_type_not_allowed_here);
  1063. end;
  1064. end;
  1065. end;
  1066. end;
  1067. end;
  1068. enumsym :
  1069. begin
  1070. p1:=genenumnode(penumsym(srsym));
  1071. end;
  1072. constsym :
  1073. begin
  1074. case pconstsym(srsym)^.consttyp of
  1075. constint :
  1076. begin
  1077. { do a very dirty trick to bootstrap this code }
  1078. if (pconstsym(srsym)^.value>=-(int64(2147483647)+int64(1))) and (pconstsym(srsym)^.value<=2147483647) then
  1079. p1:=cordconstnode.create(pconstsym(srsym)^.value,s32bittype)
  1080. else if (pconstsym(srsym)^.value > maxlongint) and (pconstsym(srsym)^.value <= int64(maxlongint)+int64(maxlongint)+1) then
  1081. p1:=cordconstnode.create(pconstsym(srsym)^.value,u32bittype)
  1082. else
  1083. p1:=cordconstnode.create(pconstsym(srsym)^.value,cs64bittype);
  1084. end;
  1085. conststring :
  1086. begin
  1087. len:=pconstsym(srsym)^.len;
  1088. if not(cs_ansistrings in aktlocalswitches) and (len>255) then
  1089. len:=255;
  1090. getmem(pc,len+1);
  1091. move(pchar(tpointerord(pconstsym(srsym)^.value))^,pc^,len);
  1092. pc[len]:=#0;
  1093. p1:=cstringconstnode.createpchar(pc,len);
  1094. end;
  1095. constchar :
  1096. p1:=cordconstnode.create(pconstsym(srsym)^.value,cchartype);
  1097. constreal :
  1098. p1:=crealconstnode.create(pbestreal(tpointerord(pconstsym(srsym)^.value))^,pbestrealtype^);
  1099. constbool :
  1100. p1:=cordconstnode.create(pconstsym(srsym)^.value,booltype);
  1101. constset :
  1102. p1:=csetconstnode.create(pconstset(tpointerord(pconstsym(srsym)^.value)),pconstsym(srsym)^.consttype);
  1103. constord :
  1104. p1:=cordconstnode.create(pconstsym(srsym)^.value,pconstsym(srsym)^.consttype);
  1105. constpointer :
  1106. p1:=cpointerconstnode.create(pconstsym(srsym)^.value,pconstsym(srsym)^.consttype);
  1107. constnil :
  1108. p1:=cnilnode.create;
  1109. constresourcestring:
  1110. begin
  1111. p1:=cloadnode.create(pvarsym(srsym),srsymtable);
  1112. do_resulttypepass(p1);
  1113. p1.resulttype:=cansistringtype;
  1114. end;
  1115. end;
  1116. end;
  1117. procsym :
  1118. begin
  1119. { are we in a class method ? }
  1120. possible_error:=(srsym^.owner^.symtabletype=objectsymtable) and
  1121. assigned(aktprocsym) and
  1122. (po_classmethod in aktprocsym^.definition^.procoptions);
  1123. do_proc_call(srsym,srsymtable,
  1124. getaddr or
  1125. (getprocvar and
  1126. ((block_type=bt_const) or
  1127. ((m_tp_procvar in aktmodeswitches) and
  1128. proc_to_procvar_equal(pprocsym(srsym)^.definition,getprocvardef)
  1129. )
  1130. )
  1131. ),again,p1);
  1132. if (block_type=bt_const) and
  1133. getprocvar then
  1134. handle_procvar(getprocvardef,p1);
  1135. { we need to know which procedure is called }
  1136. if possible_error then
  1137. begin
  1138. do_resulttypepass(p1);
  1139. if not(po_classmethod in tcallnode(p1).procdefinition^.procoptions) then
  1140. Message(parser_e_only_class_methods);
  1141. end;
  1142. end;
  1143. propertysym :
  1144. begin
  1145. { access to property in a method }
  1146. { are we in a class method ? }
  1147. if (srsym^.owner^.symtabletype=objectsymtable) and
  1148. assigned(aktprocsym) and
  1149. (po_classmethod in aktprocsym^.definition^.procoptions) then
  1150. Message(parser_e_only_class_methods);
  1151. { no method pointer }
  1152. p1:=nil;
  1153. handle_propertysym(srsym,srsymtable,p1);
  1154. end;
  1155. labelsym :
  1156. begin
  1157. consume(_COLON);
  1158. if plabelsym(srsym)^.defined then
  1159. Message(sym_e_label_already_defined);
  1160. plabelsym(srsym)^.defined:=true;
  1161. p1:=clabelnode.create(plabelsym(srsym)^.lab,nil);
  1162. end;
  1163. errorsym :
  1164. begin
  1165. p1:=cerrornode.create;
  1166. if token=_LKLAMMER then
  1167. begin
  1168. consume(_LKLAMMER);
  1169. parse_paras(false,false);
  1170. consume(_RKLAMMER);
  1171. end;
  1172. end;
  1173. else
  1174. begin
  1175. p1:=cerrornode.create;
  1176. Message(cg_e_illegal_expression);
  1177. end;
  1178. end; { end case }
  1179. end;
  1180. end;
  1181. {---------------------------------------------
  1182. Factor_Read_Set
  1183. ---------------------------------------------}
  1184. { Read a set between [] }
  1185. function factor_read_set:tnode;
  1186. var
  1187. p1,p2 : tnode;
  1188. lastp,
  1189. buildp : tarrayconstructornode;
  1190. begin
  1191. buildp:=nil;
  1192. { be sure that a least one arrayconstructn is used, also for an
  1193. empty [] }
  1194. if token=_RECKKLAMMER then
  1195. buildp:=carrayconstructornode.create(nil,buildp)
  1196. else
  1197. begin
  1198. while true do
  1199. begin
  1200. p1:=comp_expr(true);
  1201. if token=_POINTPOINT then
  1202. begin
  1203. consume(_POINTPOINT);
  1204. p2:=comp_expr(true);
  1205. p1:=carrayconstructorrangenode.create(p1,p2);
  1206. end;
  1207. { insert at the end of the tree, to get the correct order }
  1208. if not assigned(buildp) then
  1209. begin
  1210. buildp:=carrayconstructornode.create(p1,nil);
  1211. lastp:=buildp;
  1212. end
  1213. else
  1214. begin
  1215. lastp.right:=carrayconstructornode.create(p1,nil);
  1216. lastp:=tarrayconstructornode(lastp.right);
  1217. end;
  1218. { there could be more elements }
  1219. if token=_COMMA then
  1220. consume(_COMMA)
  1221. else
  1222. break;
  1223. end;
  1224. end;
  1225. factor_read_set:=buildp;
  1226. end;
  1227. {---------------------------------------------
  1228. PostFixOperators
  1229. ---------------------------------------------}
  1230. procedure postfixoperators(var p1:tnode;var again:boolean);
  1231. { tries to avoid syntax errors after invalid qualifiers }
  1232. procedure recoverconsume_postfixops;
  1233. begin
  1234. while true do
  1235. begin
  1236. case token of
  1237. _CARET:
  1238. consume(_CARET);
  1239. _POINT:
  1240. begin
  1241. consume(_POINT);
  1242. if token=_ID then
  1243. consume(_ID);
  1244. end;
  1245. _LECKKLAMMER:
  1246. begin
  1247. consume(_LECKKLAMMER);
  1248. repeat
  1249. comp_expr(true);
  1250. if token=_COMMA then
  1251. consume(_COMMA)
  1252. else
  1253. break;
  1254. until false;
  1255. consume(_RECKKLAMMER);
  1256. end
  1257. else
  1258. break;
  1259. end;
  1260. end;
  1261. end;
  1262. var
  1263. store_static : boolean;
  1264. propsym : ppropertysym;
  1265. p2,p3 : tnode;
  1266. hsym : psym;
  1267. classh : pobjectdef;
  1268. begin
  1269. again:=true;
  1270. while again do
  1271. begin
  1272. { we need the resulttype }
  1273. do_resulttypepass(p1);
  1274. if codegenerror then
  1275. begin
  1276. recoverconsume_postfixops;
  1277. exit;
  1278. end;
  1279. { handle token }
  1280. case token of
  1281. _CARET:
  1282. begin
  1283. consume(_CARET);
  1284. if (p1.resulttype.def^.deftype<>pointerdef) then
  1285. begin
  1286. { ^ as binary operator is a problem!!!! (FK) }
  1287. again:=false;
  1288. Message(cg_e_invalid_qualifier);
  1289. recoverconsume_postfixops;
  1290. p1.destroy;
  1291. p1:=cerrornode.create;
  1292. end
  1293. else
  1294. begin
  1295. p1:=cderefnode.create(p1);
  1296. end;
  1297. end;
  1298. _LECKKLAMMER:
  1299. begin
  1300. if is_class_or_interface(p1.resulttype.def) then
  1301. begin
  1302. { default property }
  1303. propsym:=search_default_property(pobjectdef(p1.resulttype.def));
  1304. if not(assigned(propsym)) then
  1305. begin
  1306. p1.destroy;
  1307. p1:=cerrornode.create;
  1308. again:=false;
  1309. message(parser_e_no_default_property_available);
  1310. end
  1311. else
  1312. handle_propertysym(propsym,propsym^.owner,p1);
  1313. end
  1314. else
  1315. begin
  1316. consume(_LECKKLAMMER);
  1317. repeat
  1318. case p1.resulttype.def^.deftype of
  1319. pointerdef:
  1320. begin
  1321. { support delphi autoderef }
  1322. if (ppointerdef(p1.resulttype.def)^.pointertype.def^.deftype=arraydef) and
  1323. (m_autoderef in aktmodeswitches) then
  1324. begin
  1325. p1:=cderefnode.create(p1);
  1326. end;
  1327. p2:=comp_expr(true);
  1328. p1:=cvecnode.create(p1,p2);
  1329. end;
  1330. stringdef :
  1331. begin
  1332. p2:=comp_expr(true);
  1333. p1:=cvecnode.create(p1,p2);
  1334. end;
  1335. arraydef :
  1336. begin
  1337. p2:=comp_expr(true);
  1338. { support SEG:OFS for go32v2 Mem[] }
  1339. if (target_info.target=target_i386_go32v2) and
  1340. (p1.nodetype=loadn) and
  1341. assigned(tloadnode(p1).symtableentry) and
  1342. assigned(tloadnode(p1).symtableentry^.owner^.name) and
  1343. (tloadnode(p1).symtableentry^.owner^.name^='SYSTEM') and
  1344. ((tloadnode(p1).symtableentry^.name='MEM') or
  1345. (tloadnode(p1).symtableentry^.name='MEMW') or
  1346. (tloadnode(p1).symtableentry^.name='MEML')) then
  1347. begin
  1348. if (token=_COLON) then
  1349. begin
  1350. consume(_COLON);
  1351. p3:=caddnode.create(muln,cordconstnode.create($10,s32bittype),p2);
  1352. p2:=comp_expr(true);
  1353. p2:=caddnode.create(addn,p2,p3);
  1354. p1:=cvecnode.create(p1,p2);
  1355. include(tvecnode(p1).flags,nf_memseg);
  1356. include(tvecnode(p1).flags,nf_memindex);
  1357. end
  1358. else
  1359. begin
  1360. p1:=cvecnode.create(p1,p2);
  1361. include(tvecnode(p1).flags,nf_memindex);
  1362. end;
  1363. end
  1364. else
  1365. p1:=cvecnode.create(p1,p2);
  1366. end;
  1367. else
  1368. begin
  1369. Message(cg_e_invalid_qualifier);
  1370. p1.destroy;
  1371. p1:=cerrornode.create;
  1372. comp_expr(true);
  1373. again:=false;
  1374. end;
  1375. end;
  1376. do_resulttypepass(p1);
  1377. if token=_COMMA then
  1378. consume(_COMMA)
  1379. else
  1380. break;
  1381. until false;
  1382. consume(_RECKKLAMMER);
  1383. end;
  1384. end;
  1385. _POINT :
  1386. begin
  1387. consume(_POINT);
  1388. if (p1.resulttype.def^.deftype=pointerdef) and
  1389. (m_autoderef in aktmodeswitches) then
  1390. p1:=cderefnode.create(p1);
  1391. case p1.resulttype.def^.deftype of
  1392. recorddef:
  1393. begin
  1394. hsym:=psym(precorddef(p1.resulttype.def)^.symtable^.search(pattern));
  1395. if assigned(hsym) and
  1396. (hsym^.typ=varsym) then
  1397. p1:=csubscriptnode.create(pvarsym(hsym),p1)
  1398. else
  1399. begin
  1400. Message1(sym_e_illegal_field,pattern);
  1401. p1.destroy;
  1402. p1:=cerrornode.create;
  1403. end;
  1404. consume(_ID);
  1405. end;
  1406. classrefdef:
  1407. begin
  1408. classh:=pobjectdef(pclassrefdef(p1.resulttype.def)^.pointertype.def);
  1409. hsym:=nil;
  1410. while assigned(classh) do
  1411. begin
  1412. hsym:=psym(classh^.symtable^.search(pattern));
  1413. if assigned(hsym) then
  1414. break;
  1415. classh:=classh^.childof;
  1416. end;
  1417. if hsym=nil then
  1418. begin
  1419. Message1(sym_e_id_no_member,pattern);
  1420. p1.destroy;
  1421. p1:=cerrornode.create;
  1422. { try to clean up }
  1423. consume(_ID);
  1424. end
  1425. else
  1426. begin
  1427. consume(_ID);
  1428. do_member_read(getaddr,hsym,p1,again);
  1429. end;
  1430. end;
  1431. objectdef:
  1432. begin
  1433. classh:=pobjectdef(p1.resulttype.def);
  1434. hsym:=nil;
  1435. store_static:=allow_only_static;
  1436. allow_only_static:=false;
  1437. while assigned(classh) do
  1438. begin
  1439. hsym:=psym(classh^.symtable^.search(pattern));
  1440. if assigned(hsym) then
  1441. break;
  1442. classh:=classh^.childof;
  1443. end;
  1444. allow_only_static:=store_static;
  1445. if hsym=nil then
  1446. begin
  1447. Message1(sym_e_id_no_member,pattern);
  1448. p1.destroy;
  1449. p1:=cerrornode.create;
  1450. { try to clean up }
  1451. consume(_ID);
  1452. end
  1453. else
  1454. begin
  1455. consume(_ID);
  1456. do_member_read(getaddr,hsym,p1,again);
  1457. end;
  1458. end;
  1459. pointerdef:
  1460. begin
  1461. Message(cg_e_invalid_qualifier);
  1462. if ppointerdef(p1.resulttype.def)^.pointertype.def^.deftype in [recorddef,objectdef,classrefdef] then
  1463. Message(parser_h_maybe_deref_caret_missing);
  1464. end;
  1465. else
  1466. begin
  1467. Message(cg_e_invalid_qualifier);
  1468. p1.destroy;
  1469. p1:=cerrornode.create;
  1470. consume(_ID);
  1471. end;
  1472. end;
  1473. end;
  1474. else
  1475. begin
  1476. { is this a procedure variable ? }
  1477. if assigned(p1.resulttype.def) then
  1478. begin
  1479. if (p1.resulttype.def^.deftype=procvardef) then
  1480. begin
  1481. if getprocvar and is_equal(p1.resulttype.def,getprocvardef) then
  1482. again:=false
  1483. else
  1484. if (token=_LKLAMMER) or
  1485. ((pprocvardef(p1.resulttype.def)^.para.empty) and
  1486. (not((token in [_ASSIGNMENT,_UNEQUAL,_EQUAL]))) and
  1487. (not afterassignment) and
  1488. (not in_args)) then
  1489. begin
  1490. { do this in a strange way }
  1491. { it's not a clean solution }
  1492. p2:=p1;
  1493. p1:=ccallnode.create(nil,nil,nil,nil);
  1494. tcallnode(p1).set_procvar(p2);
  1495. {$ifdef TEST_PROCSYMS}
  1496. p1.unit_specific:=unit_specific;
  1497. {$endif TEST_PROCSYMS}
  1498. {tcallnode(p1).symtableprocentry:=pprocsym(sym);}
  1499. if token=_LKLAMMER then
  1500. begin
  1501. consume(_LKLAMMER);
  1502. tcallnode(p1).left:=parse_paras(false,false);
  1503. consume(_RKLAMMER);
  1504. end;
  1505. { proc():= is never possible }
  1506. if token=_ASSIGNMENT then
  1507. begin
  1508. Message(cg_e_illegal_expression);
  1509. p1:=cerrornode.create;
  1510. again:=false;
  1511. end;
  1512. end
  1513. else
  1514. again:=false;
  1515. end
  1516. else
  1517. again:=false;
  1518. end
  1519. else
  1520. again:=false;
  1521. end;
  1522. end;
  1523. end; { while again }
  1524. end;
  1525. {---------------------------------------------
  1526. Factor (Main)
  1527. ---------------------------------------------}
  1528. var
  1529. l : longint;
  1530. card : cardinal;
  1531. ic : TConstExprInt;
  1532. oldp1,
  1533. p1,p2 : tnode;
  1534. code : integer;
  1535. {$ifdef TEST_PROCSYMS}
  1536. unit_specific,
  1537. {$endif TEST_PROCSYMS}
  1538. again : boolean;
  1539. sym : psym;
  1540. classh : pobjectdef;
  1541. d : bestreal;
  1542. hs : string;
  1543. htype : ttype;
  1544. filepos : tfileposinfo;
  1545. {---------------------------------------------
  1546. Helpers
  1547. ---------------------------------------------}
  1548. procedure check_tokenpos;
  1549. begin
  1550. if (p1<>oldp1) then
  1551. begin
  1552. if assigned(p1) then
  1553. p1.set_tree_filepos(filepos);
  1554. oldp1:=p1;
  1555. filepos:=akttokenpos;
  1556. end;
  1557. end;
  1558. begin
  1559. oldp1:=nil;
  1560. p1:=nil;
  1561. filepos:=akttokenpos;
  1562. again:=false;
  1563. if token=_ID then
  1564. begin
  1565. factor_read_id(p1,again);
  1566. if again then
  1567. begin
  1568. check_tokenpos;
  1569. { handle post fix operators }
  1570. postfixoperators(p1,again);
  1571. end;
  1572. end
  1573. else
  1574. case token of
  1575. _NEW :
  1576. begin
  1577. consume(_NEW);
  1578. consume(_LKLAMMER);
  1579. {allow_type:=true;}
  1580. p1:=factor(false);
  1581. {allow_type:=false;}
  1582. if p1.nodetype<>typen then
  1583. begin
  1584. Message(type_e_type_id_expected);
  1585. p1.destroy;
  1586. p1:=cerrornode.create;
  1587. do_resulttypepass(p1);
  1588. end;
  1589. if (p1.resulttype.def^.deftype<>pointerdef) then
  1590. Message1(type_e_pointer_type_expected,p1.resulttype.def^.typename)
  1591. else
  1592. if token=_RKLAMMER then
  1593. begin
  1594. if (ppointerdef(p1.resulttype.def)^.pointertype.def^.deftype=objectdef) and
  1595. (oo_has_vmt in pobjectdef(ppointerdef(p1.resulttype.def)^.pointertype.def)^.objectoptions) then
  1596. Message(parser_w_use_extended_syntax_for_objects);
  1597. p2:=cnewnode.create(nil);
  1598. do_resulttypepass(p2);
  1599. p2.resulttype:=p1.resulttype;
  1600. p1.destroy;
  1601. p1:=p2;
  1602. consume(_RKLAMMER);
  1603. end
  1604. else
  1605. begin
  1606. p2:=chnewnode.create;
  1607. do_resulttypepass(p2);
  1608. p2.resulttype:=ppointerdef(p1.resulttype.def)^.pointertype;
  1609. consume(_COMMA);
  1610. afterassignment:=false;
  1611. { determines the current object defintion }
  1612. classh:=pobjectdef(p2.resulttype.def);
  1613. if classh^.deftype<>objectdef then
  1614. Message(parser_e_pointer_to_class_expected)
  1615. else
  1616. begin
  1617. { check for an abstract class }
  1618. if (oo_has_abstract in classh^.objectoptions) then
  1619. Message(sym_e_no_instance_of_abstract_object);
  1620. { search the constructor also in the symbol tables of
  1621. the parents }
  1622. sym:=nil;
  1623. while assigned(classh) do
  1624. begin
  1625. sym:=psym(classh^.symtable^.search(pattern));
  1626. if assigned(sym) then
  1627. break;
  1628. classh:=classh^.childof;
  1629. end;
  1630. consume(_ID);
  1631. do_member_read(false,sym,p2,again);
  1632. { we need to know which procedure is called }
  1633. do_resulttypepass(p2);
  1634. if (p2.nodetype<>calln) or
  1635. (assigned(tcallnode(p2).procdefinition) and
  1636. (tcallnode(p2).procdefinition^.proctypeoption<>potype_constructor)) then
  1637. Message(parser_e_expr_have_to_be_constructor_call);
  1638. end;
  1639. p2:=cnewnode.create(p2);
  1640. do_resulttypepass(p2);
  1641. p2.resulttype:=p1.resulttype;
  1642. p1.destroy;
  1643. p1:=p2;
  1644. consume(_RKLAMMER);
  1645. end;
  1646. postfixoperators(p1,again);
  1647. end;
  1648. _SELF :
  1649. begin
  1650. again:=true;
  1651. consume(_SELF);
  1652. if not assigned(procinfo^._class) then
  1653. begin
  1654. p1:=cerrornode.create;
  1655. again:=false;
  1656. Message(parser_e_self_not_in_method);
  1657. end
  1658. else
  1659. begin
  1660. if (po_classmethod in aktprocsym^.definition^.procoptions) then
  1661. begin
  1662. { self in class methods is a class reference type }
  1663. htype.setdef(procinfo^._class);
  1664. p1:=cselfnode.create(pobjectdef(new(pclassrefdef,init(htype))));
  1665. end
  1666. else
  1667. p1:=cselfnode.create(procinfo^._class);
  1668. postfixoperators(p1,again);
  1669. end;
  1670. end;
  1671. _INHERITED :
  1672. begin
  1673. again:=true;
  1674. consume(_INHERITED);
  1675. if assigned(procinfo^._class) then
  1676. begin
  1677. { if inherited; only then we need the method with
  1678. the same name }
  1679. if token=_SEMICOLON then
  1680. begin
  1681. hs:=aktprocsym^.name;
  1682. auto_inherited:=true
  1683. end
  1684. else
  1685. begin
  1686. hs:=pattern;
  1687. consume(_ID);
  1688. auto_inherited:=false;
  1689. end;
  1690. classh:=procinfo^._class^.childof;
  1691. while assigned(classh) do
  1692. begin
  1693. sym:=psym(pobjectdef(classh)^.symtable^.search(hs));
  1694. if assigned(sym) then
  1695. begin
  1696. if sym^.typ=procsym then
  1697. begin
  1698. htype.setdef(classh);
  1699. p1:=ctypenode.create(htype);
  1700. end;
  1701. do_member_read(false,sym,p1,again);
  1702. break;
  1703. end;
  1704. classh:=classh^.childof;
  1705. end;
  1706. if classh=nil then
  1707. begin
  1708. Message1(sym_e_id_no_member,hs);
  1709. again:=false;
  1710. p1:=cerrornode.create;
  1711. end;
  1712. { turn auto inheriting off }
  1713. auto_inherited:=false;
  1714. end
  1715. else
  1716. begin
  1717. Message(parser_e_generic_methods_only_in_methods);
  1718. again:=false;
  1719. p1:=cerrornode.create;
  1720. end;
  1721. postfixoperators(p1,again);
  1722. end;
  1723. _INTCONST :
  1724. begin
  1725. { try cardinal first }
  1726. val(pattern,card,code);
  1727. if code<>0 then
  1728. begin
  1729. { then longint }
  1730. valint(pattern,l,code);
  1731. if code <> 0 then
  1732. begin
  1733. { then int64 }
  1734. val(pattern,ic,code);
  1735. if code<>0 then
  1736. begin
  1737. {finally float }
  1738. val(pattern,d,code);
  1739. if code<>0 then
  1740. begin
  1741. Message(cg_e_invalid_integer);
  1742. consume(_INTCONST);
  1743. l:=1;
  1744. p1:=cordconstnode.create(l,s32bittype);
  1745. end
  1746. else
  1747. begin
  1748. consume(_INTCONST);
  1749. p1:=crealconstnode.create(d,pbestrealtype^);
  1750. end;
  1751. end
  1752. else
  1753. begin
  1754. consume(_INTCONST);
  1755. p1:=cordconstnode.create(ic,cs64bittype);
  1756. end
  1757. end
  1758. else
  1759. begin
  1760. consume(_INTCONST);
  1761. p1:=cordconstnode.create(l,s32bittype)
  1762. end
  1763. end
  1764. else
  1765. begin
  1766. consume(_INTCONST);
  1767. { check whether the value isn't in the longint range as well }
  1768. { (longint is easier to perform calculations with) (JM) }
  1769. if card <= $7fffffff then
  1770. { no sign extension necessary, so not longint typecast (JM) }
  1771. p1:=cordconstnode.create(card,s32bittype)
  1772. else
  1773. p1:=cordconstnode.create(card,u32bittype)
  1774. end;
  1775. end;
  1776. _REALNUMBER :
  1777. begin
  1778. val(pattern,d,code);
  1779. if code<>0 then
  1780. begin
  1781. Message(parser_e_error_in_real);
  1782. d:=1.0;
  1783. end;
  1784. consume(_REALNUMBER);
  1785. p1:=crealconstnode.create(d,pbestrealtype^);
  1786. end;
  1787. _STRING :
  1788. begin
  1789. string_dec(htype);
  1790. { STRING can be also a type cast }
  1791. if token=_LKLAMMER then
  1792. begin
  1793. consume(_LKLAMMER);
  1794. p1:=comp_expr(true);
  1795. consume(_RKLAMMER);
  1796. p1:=ctypeconvnode.create(p1,htype);
  1797. include(p1.flags,nf_explizit);
  1798. { handle postfix operators here e.g. string(a)[10] }
  1799. again:=true;
  1800. postfixoperators(p1,again);
  1801. end
  1802. else
  1803. p1:=ctypenode.create(htype);
  1804. end;
  1805. _FILE :
  1806. begin
  1807. htype:=cfiletype;
  1808. consume(_FILE);
  1809. { FILE can be also a type cast }
  1810. if token=_LKLAMMER then
  1811. begin
  1812. consume(_LKLAMMER);
  1813. p1:=comp_expr(true);
  1814. consume(_RKLAMMER);
  1815. p1:=ctypeconvnode.create(p1,htype);
  1816. include(p1.flags,nf_explizit);
  1817. { handle postfix operators here e.g. string(a)[10] }
  1818. again:=true;
  1819. postfixoperators(p1,again);
  1820. end
  1821. else
  1822. begin
  1823. p1:=ctypenode.create(htype);
  1824. end;
  1825. end;
  1826. _CSTRING :
  1827. begin
  1828. p1:=cstringconstnode.createstr(pattern,st_default);
  1829. consume(_CSTRING);
  1830. end;
  1831. _CCHAR :
  1832. begin
  1833. p1:=cordconstnode.create(ord(pattern[1]),cchartype);
  1834. consume(_CCHAR);
  1835. end;
  1836. _CWSTRING:
  1837. begin
  1838. p1:=cstringconstnode.createwstr(patternw);
  1839. consume(_CWSTRING);
  1840. end;
  1841. _CWCHAR:
  1842. begin
  1843. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype);
  1844. consume(_CWCHAR);
  1845. end;
  1846. _KLAMMERAFFE :
  1847. begin
  1848. consume(_KLAMMERAFFE);
  1849. got_addrn:=true;
  1850. { support both @<x> and @(<x>) }
  1851. if token=_LKLAMMER then
  1852. begin
  1853. consume(_LKLAMMER);
  1854. p1:=factor(true);
  1855. consume(_RKLAMMER);
  1856. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1857. begin
  1858. again:=true;
  1859. postfixoperators(p1,again);
  1860. end;
  1861. end
  1862. else
  1863. p1:=factor(true);
  1864. got_addrn:=false;
  1865. p1:=caddrnode.create(p1);
  1866. end;
  1867. _LKLAMMER :
  1868. begin
  1869. consume(_LKLAMMER);
  1870. p1:=comp_expr(true);
  1871. consume(_RKLAMMER);
  1872. { it's not a good solution }
  1873. { but (a+b)^ makes some problems }
  1874. if token in [_CARET,_POINT,_LECKKLAMMER] then
  1875. begin
  1876. again:=true;
  1877. postfixoperators(p1,again);
  1878. end;
  1879. end;
  1880. _LECKKLAMMER :
  1881. begin
  1882. consume(_LECKKLAMMER);
  1883. p1:=factor_read_set;
  1884. consume(_RECKKLAMMER);
  1885. end;
  1886. _PLUS :
  1887. begin
  1888. consume(_PLUS);
  1889. p1:=factor(false);
  1890. end;
  1891. _MINUS :
  1892. begin
  1893. consume(_MINUS);
  1894. p1:=sub_expr(oppower,false);
  1895. p1:=cunaryminusnode.create(p1);
  1896. end;
  1897. _OP_NOT :
  1898. begin
  1899. consume(_OP_NOT);
  1900. p1:=factor(false);
  1901. p1:=cnotnode.create(p1);
  1902. end;
  1903. _TRUE :
  1904. begin
  1905. consume(_TRUE);
  1906. p1:=cordconstnode.create(1,booltype);
  1907. end;
  1908. _FALSE :
  1909. begin
  1910. consume(_FALSE);
  1911. p1:=cordconstnode.create(0,booltype);
  1912. end;
  1913. _NIL :
  1914. begin
  1915. consume(_NIL);
  1916. p1:=cnilnode.create;
  1917. end;
  1918. else
  1919. begin
  1920. p1:=cerrornode.create;
  1921. consume(token);
  1922. Message(cg_e_illegal_expression);
  1923. end;
  1924. end;
  1925. { generate error node if no node is created }
  1926. if not assigned(p1) then
  1927. begin
  1928. {$ifdef EXTDEBUG}
  1929. Comment(V_Warning,'factor: p1=nil');
  1930. {$endif}
  1931. p1:=cerrornode.create;
  1932. end;
  1933. { get the resulttype for the node }
  1934. if (not assigned(p1.resulttype.def)) then
  1935. do_resulttypepass(p1);
  1936. { tp7 procvar handling, but not if the next token
  1937. will be a := }
  1938. if (m_tp_procvar in aktmodeswitches) and
  1939. (token<>_ASSIGNMENT) then
  1940. check_tp_procvar(p1);
  1941. factor:=p1;
  1942. check_tokenpos;
  1943. end;
  1944. {$ifdef fpc}
  1945. {$maxfpuregisters default}
  1946. {$endif fpc}
  1947. {****************************************************************************
  1948. Sub_Expr
  1949. ****************************************************************************}
  1950. const
  1951. { Warning these stay be ordered !! }
  1952. operator_levels:array[Toperator_precedence] of set of Ttoken=
  1953. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN,_OP_IS],
  1954. [_PLUS,_MINUS,_OP_OR,_OP_XOR],
  1955. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  1956. _OP_AS,_OP_AND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  1957. [_STARSTAR] );
  1958. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  1959. {Reads a subexpression while the operators are of the current precedence
  1960. level, or any higher level. Replaces the old term, simpl_expr and
  1961. simpl2_expr.}
  1962. var
  1963. p1,p2 : tnode;
  1964. oldt : Ttoken;
  1965. filepos : tfileposinfo;
  1966. begin
  1967. if pred_level=highest_precedence then
  1968. p1:=factor(false)
  1969. else
  1970. p1:=sub_expr(succ(pred_level),true);
  1971. repeat
  1972. if (token in operator_levels[pred_level]) and
  1973. ((token<>_EQUAL) or accept_equal) then
  1974. begin
  1975. oldt:=token;
  1976. filepos:=akttokenpos;
  1977. consume(token);
  1978. if pred_level=highest_precedence then
  1979. p2:=factor(false)
  1980. else
  1981. p2:=sub_expr(succ(pred_level),true);
  1982. case oldt of
  1983. _PLUS :
  1984. p1:=caddnode.create(addn,p1,p2);
  1985. _MINUS :
  1986. p1:=caddnode.create(subn,p1,p2);
  1987. _STAR :
  1988. p1:=caddnode.create(muln,p1,p2);
  1989. _SLASH :
  1990. p1:=caddnode.create(slashn,p1,p2);
  1991. _EQUAL :
  1992. p1:=caddnode.create(equaln,p1,p2);
  1993. _GT :
  1994. p1:=caddnode.create(gtn,p1,p2);
  1995. _LT :
  1996. p1:=caddnode.create(ltn,p1,p2);
  1997. _GTE :
  1998. p1:=caddnode.create(gten,p1,p2);
  1999. _LTE :
  2000. p1:=caddnode.create(lten,p1,p2);
  2001. _SYMDIF :
  2002. p1:=caddnode.create(symdifn,p1,p2);
  2003. _STARSTAR :
  2004. p1:=caddnode.create(starstarn,p1,p2);
  2005. _OP_AS :
  2006. p1:=casnode.create(p1,p2);
  2007. _OP_IN :
  2008. p1:=cinnode.create(p1,p2);
  2009. _OP_IS :
  2010. p1:=cisnode.create(p1,p2);
  2011. _OP_OR :
  2012. p1:=caddnode.create(orn,p1,p2);
  2013. _OP_AND :
  2014. p1:=caddnode.create(andn,p1,p2);
  2015. _OP_DIV :
  2016. p1:=cmoddivnode.create(divn,p1,p2);
  2017. _OP_NOT :
  2018. p1:=cnotnode.create(p1);
  2019. _OP_MOD :
  2020. p1:=cmoddivnode.create(modn,p1,p2);
  2021. _OP_SHL :
  2022. p1:=cshlshrnode.create(shln,p1,p2);
  2023. _OP_SHR :
  2024. p1:=cshlshrnode.create(shrn,p1,p2);
  2025. _OP_XOR :
  2026. p1:=caddnode.create(xorn,p1,p2);
  2027. _ASSIGNMENT :
  2028. p1:=cassignmentnode.create(p1,p2);
  2029. _CARET :
  2030. p1:=caddnode.create(caretn,p1,p2);
  2031. _UNEQUAL :
  2032. p1:=caddnode.create(unequaln,p1,p2);
  2033. end;
  2034. p1.set_tree_filepos(filepos);
  2035. end
  2036. else
  2037. break;
  2038. until false;
  2039. sub_expr:=p1;
  2040. end;
  2041. function comp_expr(accept_equal : boolean):tnode;
  2042. var
  2043. oldafterassignment : boolean;
  2044. p1 : tnode;
  2045. begin
  2046. oldafterassignment:=afterassignment;
  2047. afterassignment:=true;
  2048. p1:=sub_expr(opcompare,accept_equal);
  2049. { get the resulttype for this expression }
  2050. if not assigned(p1.resulttype.def) then
  2051. do_resulttypepass(p1);
  2052. afterassignment:=oldafterassignment;
  2053. comp_expr:=p1;
  2054. end;
  2055. function expr : tnode;
  2056. var
  2057. p1,p2 : tnode;
  2058. oldafterassignment : boolean;
  2059. oldp1 : tnode;
  2060. filepos : tfileposinfo;
  2061. begin
  2062. oldafterassignment:=afterassignment;
  2063. p1:=sub_expr(opcompare,true);
  2064. { get the resulttype for this expression }
  2065. if not assigned(p1.resulttype.def) then
  2066. do_resulttypepass(p1);
  2067. filepos:=akttokenpos;
  2068. if (m_tp_procvar in aktmodeswitches) and
  2069. (token<>_ASSIGNMENT) then
  2070. check_tp_procvar(p1);
  2071. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2072. afterassignment:=true;
  2073. oldp1:=p1;
  2074. case token of
  2075. _POINTPOINT :
  2076. begin
  2077. consume(_POINTPOINT);
  2078. p2:=sub_expr(opcompare,true);
  2079. p1:=crangenode.create(p1,p2);
  2080. end;
  2081. _ASSIGNMENT :
  2082. begin
  2083. consume(_ASSIGNMENT);
  2084. if (p1.resulttype.def^.deftype=procvardef) then
  2085. begin
  2086. getprocvar:=true;
  2087. getprocvardef:=pprocvardef(p1.resulttype.def);
  2088. end;
  2089. p2:=sub_expr(opcompare,true);
  2090. if getprocvar then
  2091. handle_procvar(getprocvardef,p2);
  2092. getprocvar:=false;
  2093. p1:=cassignmentnode.create(p1,p2);
  2094. end;
  2095. _PLUSASN :
  2096. begin
  2097. consume(_PLUSASN);
  2098. p2:=sub_expr(opcompare,true);
  2099. p1:=cassignmentnode.create(p1,caddnode.create(addn,p1.getcopy,p2));
  2100. end;
  2101. _MINUSASN :
  2102. begin
  2103. consume(_MINUSASN);
  2104. p2:=sub_expr(opcompare,true);
  2105. p1:=cassignmentnode.create(p1,caddnode.create(subn,p1.getcopy,p2));
  2106. end;
  2107. _STARASN :
  2108. begin
  2109. consume(_STARASN );
  2110. p2:=sub_expr(opcompare,true);
  2111. p1:=cassignmentnode.create(p1,caddnode.create(muln,p1.getcopy,p2));
  2112. end;
  2113. _SLASHASN :
  2114. begin
  2115. consume(_SLASHASN );
  2116. p2:=sub_expr(opcompare,true);
  2117. p1:=cassignmentnode.create(p1,caddnode.create(slashn,p1.getcopy,p2));
  2118. end;
  2119. end;
  2120. afterassignment:=oldafterassignment;
  2121. if p1<>oldp1 then
  2122. p1.set_tree_filepos(filepos);
  2123. expr:=p1;
  2124. end;
  2125. {$ifdef int64funcresok}
  2126. function get_intconst:TConstExprInt;
  2127. {$else int64funcresok}
  2128. function get_intconst:longint;
  2129. {$endif int64funcresok}
  2130. {Reads an expression, tries to evalute it and check if it is an integer
  2131. constant. Then the constant is returned.}
  2132. var
  2133. p:tnode;
  2134. begin
  2135. p:=comp_expr(true);
  2136. if not codegenerror then
  2137. begin
  2138. if (p.nodetype<>ordconstn) or
  2139. (p.resulttype.def^.deftype<>orddef) or
  2140. (Porddef(p.resulttype.def)^.typ in [uvoid,uchar,bool8bit,bool16bit,bool32bit]) then
  2141. Message(cg_e_illegal_expression)
  2142. else
  2143. get_intconst:=tordconstnode(p).value;
  2144. end;
  2145. p.free;
  2146. end;
  2147. function get_stringconst:string;
  2148. {Reads an expression, tries to evaluate it and checks if it is a string
  2149. constant. Then the constant is returned.}
  2150. var
  2151. p:tnode;
  2152. begin
  2153. get_stringconst:='';
  2154. p:=comp_expr(true);
  2155. if p.nodetype<>stringconstn then
  2156. begin
  2157. if (p.nodetype=ordconstn) and is_char(p.resulttype.def) then
  2158. get_stringconst:=char(tordconstnode(p).value)
  2159. else
  2160. Message(cg_e_illegal_expression);
  2161. end
  2162. else
  2163. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2164. p.free;
  2165. end;
  2166. end.
  2167. {
  2168. $Log$
  2169. Revision 1.27 2001-04-04 22:43:52 peter
  2170. * remove unnecessary calls to firstpass
  2171. Revision 1.26 2001/04/02 21:20:33 peter
  2172. * resulttype rewrite
  2173. Revision 1.25 2001/03/11 22:58:50 peter
  2174. * getsym redesign, removed the globals srsym,srsymtable
  2175. Revision 1.24 2000/12/25 00:07:27 peter
  2176. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  2177. tlinkedlist objects)
  2178. Revision 1.23 2000/12/19 20:36:03 peter
  2179. * cardinal const expr fix from jonas
  2180. Revision 1.22 2000/12/17 14:00:18 peter
  2181. * fixed static variables
  2182. Revision 1.21 2000/12/15 13:26:01 jonas
  2183. * only return int64's from functions if it int64funcresok is defined
  2184. + added int64funcresok define to options.pas
  2185. Revision 1.20 2000/12/15 12:13:52 michael
  2186. + Fix from Peter
  2187. Revision 1.19 2000/12/07 17:19:42 jonas
  2188. * new constant handling: from now on, hex constants >$7fffffff are
  2189. parsed as unsigned constants (otherwise, $80000000 got sign extended
  2190. and became $ffffffff80000000), all constants in the longint range
  2191. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  2192. are cardinals and the rest are int64's.
  2193. * added lots of longint typecast to prevent range check errors in the
  2194. compiler and rtl
  2195. * type casts of symbolic ordinal constants are now preserved
  2196. * fixed bug where the original resulttype.def wasn't restored correctly
  2197. after doing a 64bit rangecheck
  2198. Revision 1.18 2000/11/29 00:30:36 florian
  2199. * unused units removed from uses clause
  2200. * some changes for widestrings
  2201. Revision 1.17 2000/11/09 17:46:55 florian
  2202. * System.TypeInfo fixed
  2203. + System.Finalize implemented
  2204. + some new keywords for interface support added
  2205. Revision 1.16 2000/11/06 20:30:55 peter
  2206. * more fixes to get make cycle working
  2207. Revision 1.15 2000/11/04 14:25:20 florian
  2208. + merged Attila's changes for interfaces, not tested yet
  2209. Revision 1.14 2000/10/31 22:02:49 peter
  2210. * symtable splitted, no real code changes
  2211. Revision 1.13 2000/10/26 23:40:54 peter
  2212. * fixed crash with call from type decl which is not allowed (merged)
  2213. Revision 1.12 2000/10/21 18:16:12 florian
  2214. * a lot of changes:
  2215. - basic dyn. array support
  2216. - basic C++ support
  2217. - some work for interfaces done
  2218. ....
  2219. Revision 1.11 2000/10/14 10:14:51 peter
  2220. * moehrendorf oct 2000 rewrite
  2221. Revision 1.10 2000/10/01 19:48:25 peter
  2222. * lot of compile updates for cg11
  2223. Revision 1.9 2000/09/24 21:19:50 peter
  2224. * delphi compile fixes
  2225. Revision 1.8 2000/09/24 15:06:22 peter
  2226. * use defines.inc
  2227. Revision 1.7 2000/08/27 16:11:51 peter
  2228. * moved some util functions from globals,cobjects to cutils
  2229. * splitted files into finput,fmodule
  2230. Revision 1.6 2000/08/20 15:12:49 peter
  2231. * auto derefence mode for array pointer (merged)
  2232. Revision 1.5 2000/08/16 18:33:53 peter
  2233. * splitted namedobjectitem.next into indexnext and listnext so it
  2234. can be used in both lists
  2235. * don't allow "word = word" type definitions (merged)
  2236. Revision 1.4 2000/08/16 13:06:06 florian
  2237. + support of 64 bit integer constants
  2238. Revision 1.3 2000/08/04 22:00:52 peter
  2239. * merges from fixes
  2240. Revision 1.2 2000/07/13 11:32:44 michael
  2241. + removed logs
  2242. }