pexpr.pas 86 KB

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