pexpr.pas 90 KB

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