pexpr.pas 89 KB

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