pexpr.pas 102 KB

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