pexpr.pas 92 KB

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