pexpr.pas 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816
  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 def: tdef);
  31. procedure propaccesslist_to_node(var p1:tnode;st:tsymtable;pl:tpropaccesslist);
  32. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  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 def:tdef);
  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. def:=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:=tstringdef.createlong(tordconstnode(p).value))}
  100. Message(parser_e_invalid_string_size);
  101. tordconstnode(p).value:=255;
  102. def:=tstringdef.createshort(tordconstnode(p).value);
  103. end
  104. else
  105. if tordconstnode(p).value<>255 then
  106. def:=tstringdef.createshort(tordconstnode(p).value);
  107. end;
  108. p.free;
  109. end
  110. else
  111. begin
  112. if cs_ansistrings in current_settings.localswitches then
  113. def:=cansistringtype
  114. else
  115. def:=cshortstringtype;
  116. end;
  117. end;
  118. procedure propaccesslist_to_node(var p1:tnode;st:tsymtable;pl:tpropaccesslist);
  119. var
  120. plist : ppropaccesslistitem;
  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^.def);
  155. sl_absolutetype :
  156. begin
  157. p1:=ctypeconvnode.create(p1,plist^.def);
  158. include(p1.flags,nf_absolute);
  159. end;
  160. sl_vec :
  161. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  162. else
  163. internalerror(200110205);
  164. end;
  165. plist:=plist^.next;
  166. end;
  167. end;
  168. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  169. var
  170. sl : tpropaccesslist;
  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).totypedef)
  184. else
  185. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  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.resultdef)
  192. else
  193. begin
  194. Message(parser_e_illegal_expression);
  195. { recovery }
  196. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  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:=tpropaccesslist.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. hdef : tdef;
  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. typecheckpass(p1);
  277. result:=internalstatements(newstatement);
  278. hdef:=tpointerdef.create(p1.resultdef);
  279. temp:=ctempcreatenode.create(hdef,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 current_settings.modeswitches) 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.returndef)) 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 current_settings.modeswitches) 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 current_settings.modeswitches) 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 current_settings.modeswitches 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 current_settings.modeswitches 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.resultdef.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.resultdef.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.resultdef) and
  435. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  436. is_open_array(p1.resultdef) or
  437. is_array_of_const(p1.resultdef) or
  438. is_open_string(p1.resultdef)
  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.resultdef)) and
  451. not((p1.nodetype = subscriptn) and
  452. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  453. begin
  454. statement_syssym:=cordconstnode.create(p1.resultdef.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.resultdef.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.resultdef.deftype of
  521. procdef, { procvar }
  522. pointerdef,
  523. procvardef,
  524. classrefdef : ;
  525. objectdef :
  526. if not is_class_or_interface(p1.resultdef) then
  527. begin
  528. Message(parser_e_illegal_parameter_list);
  529. err:=true;
  530. end;
  531. arraydef :
  532. if not is_dynamic_array(p1.resultdef) 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 current_settings.localswitches 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_typecheckpass(p1);
  577. { Ofs() returns a cardinal/qword, not a pointer }
  578. p1.resultdef:=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.resultdef.deftype=stringdef) or
  681. is_chararray(p1.resultdef) or
  682. is_char(p1.resultdef)
  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. (*
  791. in_get_frame:
  792. begin
  793. statement_syssym:=geninlinenode(l,false,nil);
  794. end;
  795. in_get_caller_frame:
  796. begin
  797. if try_to_consume(_LKLAMMER) then
  798. begin
  799. {You used to call get_caller_frame as get_caller_frame(get_frame),
  800. however, as a stack frame may not exist, it does more harm than
  801. good, so ignore it.}
  802. in_args:=true;
  803. p1:=comp_expr(true);
  804. p1.destroy;
  805. consume(_RKLAMMER);
  806. end;
  807. statement_syssym:=geninlinenode(l,false,nil);
  808. end;
  809. *)
  810. else
  811. internalerror(15);
  812. end;
  813. in_args:=prev_in_args;
  814. end;
  815. function maybe_load_methodpointer(st:tsymtable;var p1:tnode):boolean;
  816. begin
  817. maybe_load_methodpointer:=false;
  818. if not assigned(p1) then
  819. begin
  820. case st.symtabletype of
  821. withsymtable :
  822. begin
  823. if (st.defowner.deftype=objectdef) then
  824. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  825. end;
  826. objectsymtable :
  827. begin
  828. p1:=load_self_node;
  829. { We are calling a member }
  830. maybe_load_methodpointer:=true;
  831. end;
  832. end;
  833. end;
  834. end;
  835. { reads the parameter for a subroutine call }
  836. procedure do_proc_call(sym:tsym;st:tsymtable;obj:tobjectdef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  837. var
  838. membercall,
  839. prevafterassn : boolean;
  840. i : integer;
  841. para,p2 : tnode;
  842. currpara : tparavarsym;
  843. aprocdef : tprocdef;
  844. begin
  845. prevafterassn:=afterassignment;
  846. afterassignment:=false;
  847. membercall:=false;
  848. aprocdef:=nil;
  849. { when it is a call to a member we need to load the
  850. methodpointer first }
  851. membercall:=maybe_load_methodpointer(st,p1);
  852. { When we are expecting a procvar we also need
  853. to get the address in some cases }
  854. if assigned(getprocvardef) then
  855. begin
  856. if (block_type=bt_const) or
  857. getaddr then
  858. begin
  859. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  860. getaddr:=true;
  861. end
  862. else
  863. if (m_tp_procvar in current_settings.modeswitches) or
  864. (m_mac_procvar in current_settings.modeswitches) then
  865. begin
  866. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  867. if assigned(aprocdef) then
  868. getaddr:=true;
  869. end;
  870. end;
  871. { only need to get the address of the procedure? }
  872. if getaddr then
  873. begin
  874. { Retrieve info which procvar to call. For tp_procvar the
  875. aprocdef is already loaded above so we can reuse it }
  876. if not assigned(aprocdef) and
  877. assigned(getprocvardef) then
  878. aprocdef:=Tprocsym(sym).search_procdef_byprocvardef(getprocvardef);
  879. { generate a methodcallnode or proccallnode }
  880. { we shouldn't convert things like @tcollection.load }
  881. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  882. if assigned(p1) then
  883. begin
  884. { for loading methodpointer of an inherited function
  885. we use self as instance and load the address of
  886. the function directly and not through the vmt (PFV) }
  887. if (cnf_inherited in callflags) then
  888. begin
  889. include(p2.flags,nf_inherited);
  890. p1.free;
  891. p1:=load_self_node;
  892. end;
  893. if (p1.nodetype<>typen) then
  894. tloadnode(p2).set_mp(p1)
  895. else
  896. p1.free;
  897. end;
  898. p1:=p2;
  899. { no postfix operators }
  900. again:=false;
  901. end
  902. else
  903. begin
  904. para:=nil;
  905. if anon_inherited then
  906. begin
  907. if not assigned(current_procinfo) then
  908. internalerror(200305054);
  909. for i:=0 to current_procinfo.procdef.paras.count-1 do
  910. begin
  911. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  912. if not(vo_is_hidden_para in currpara.varoptions) then
  913. begin
  914. { inheritance by msgint? }
  915. if assigned(srdef) then
  916. { anonymous inherited via msgid calls only require a var parameter for
  917. both methods, so we need some type casting here }
  918. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  919. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  920. para)
  921. else
  922. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  923. end;
  924. end;
  925. end
  926. else
  927. begin
  928. if try_to_consume(_LKLAMMER) then
  929. begin
  930. para:=parse_paras(false,_RKLAMMER);
  931. consume(_RKLAMMER);
  932. end;
  933. end;
  934. { indicate if this call was generated by a member and
  935. no explicit self is used, this is needed to determine
  936. how to handle a destructor call (PFV) }
  937. if membercall then
  938. include(callflags,cnf_member_call);
  939. if assigned(obj) then
  940. begin
  941. if (st.symtabletype<>objectsymtable) then
  942. internalerror(200310031);
  943. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  944. end
  945. else
  946. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  947. end;
  948. afterassignment:=prevafterassn;
  949. end;
  950. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  951. var
  952. hp,hp2 : tnode;
  953. hpp : ^tnode;
  954. currprocdef : tprocdef;
  955. begin
  956. if not assigned(pv) then
  957. internalerror(200301121);
  958. if (m_tp_procvar in current_settings.modeswitches) or
  959. (m_mac_procvar in current_settings.modeswitches) then
  960. begin
  961. hp:=p2;
  962. hpp:=@p2;
  963. while assigned(hp) and
  964. (hp.nodetype=typeconvn) do
  965. begin
  966. hp:=ttypeconvnode(hp).left;
  967. { save orignal address of the old tree so we can replace the node }
  968. hpp:=@hp;
  969. end;
  970. if (hp.nodetype=calln) and
  971. { a procvar can't have parameters! }
  972. not assigned(tcallnode(hp).left) then
  973. begin
  974. currprocdef:=tcallnode(hp).symtableprocentry.search_procdef_byprocvardef(pv);
  975. if assigned(currprocdef) then
  976. begin
  977. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  978. if (po_methodpointer in pv.procoptions) then
  979. tloadnode(hp2).set_mp(tcallnode(hp).get_load_methodpointer);
  980. hp.destroy;
  981. { replace the old callnode with the new loadnode }
  982. hpp^:=hp2;
  983. end;
  984. end;
  985. end;
  986. end;
  987. { the following procedure handles the access to a property symbol }
  988. procedure handle_propertysym(propsym : tpropertysym;st : tsymtable;var p1 : tnode);
  989. var
  990. paras : tnode;
  991. p2 : tnode;
  992. membercall : boolean;
  993. callflags : tcallnodeflags;
  994. propaccesslist : tpropaccesslist;
  995. function getpropaccesslist(pap:tpropaccesslisttypes):boolean;
  996. var
  997. hpropsym : tpropertysym;
  998. begin
  999. result:=false;
  1000. { find property in the overriden list }
  1001. hpropsym:=propsym;
  1002. repeat
  1003. propaccesslist:=hpropsym.propaccesslist[pap];
  1004. if not propaccesslist.empty then
  1005. begin
  1006. result:=true;
  1007. exit;
  1008. end;
  1009. hpropsym:=hpropsym.overridenpropsym;
  1010. until not assigned(hpropsym);
  1011. end;
  1012. begin
  1013. { property parameters? read them only if the property really }
  1014. { has parameters }
  1015. paras:=nil;
  1016. if (ppo_hasparameters in propsym.propoptions) then
  1017. begin
  1018. if try_to_consume(_LECKKLAMMER) then
  1019. begin
  1020. paras:=parse_paras(false,_RECKKLAMMER);
  1021. consume(_RECKKLAMMER);
  1022. end;
  1023. end;
  1024. { indexed property }
  1025. if (ppo_indexed in propsym.propoptions) then
  1026. begin
  1027. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  1028. paras:=ccallparanode.create(p2,paras);
  1029. end;
  1030. { we need only a write property if a := follows }
  1031. { if not(afterassignment) and not(in_args) then }
  1032. if token=_ASSIGNMENT then
  1033. begin
  1034. if getpropaccesslist(palt_write) then
  1035. begin
  1036. case propaccesslist.firstsym^.sym.typ of
  1037. procsym :
  1038. begin
  1039. callflags:=[];
  1040. { generate the method call }
  1041. membercall:=maybe_load_methodpointer(st,p1);
  1042. if membercall then
  1043. include(callflags,cnf_member_call);
  1044. p1:=ccallnode.create(paras,tprocsym(propaccesslist.firstsym^.sym),st,p1,callflags);
  1045. addsymref(propaccesslist.firstsym^.sym);
  1046. paras:=nil;
  1047. consume(_ASSIGNMENT);
  1048. { read the expression }
  1049. if propsym.propdef.deftype=procvardef then
  1050. getprocvardef:=tprocvardef(propsym.propdef);
  1051. p2:=comp_expr(true);
  1052. if assigned(getprocvardef) then
  1053. handle_procvar(getprocvardef,p2);
  1054. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1055. { mark as property, both the tcallnode and the real call block }
  1056. include(p1.flags,nf_isproperty);
  1057. getprocvardef:=nil;
  1058. end;
  1059. fieldvarsym :
  1060. begin
  1061. { generate access code }
  1062. propaccesslist_to_node(p1,st,propaccesslist);
  1063. include(p1.flags,nf_isproperty);
  1064. consume(_ASSIGNMENT);
  1065. { read the expression }
  1066. p2:=comp_expr(true);
  1067. p1:=cassignmentnode.create(p1,p2);
  1068. end
  1069. else
  1070. begin
  1071. p1:=cerrornode.create;
  1072. Message(parser_e_no_procedure_to_access_property);
  1073. end;
  1074. end;
  1075. end
  1076. else
  1077. begin
  1078. p1:=cerrornode.create;
  1079. Message(parser_e_no_procedure_to_access_property);
  1080. end;
  1081. end
  1082. else
  1083. begin
  1084. if getpropaccesslist(palt_read) then
  1085. begin
  1086. case propaccesslist.firstsym^.sym.typ of
  1087. fieldvarsym :
  1088. begin
  1089. { generate access code }
  1090. propaccesslist_to_node(p1,st,propaccesslist);
  1091. include(p1.flags,nf_isproperty);
  1092. end;
  1093. procsym :
  1094. begin
  1095. callflags:=[];
  1096. { generate the method call }
  1097. membercall:=maybe_load_methodpointer(st,p1);
  1098. if membercall then
  1099. include(callflags,cnf_member_call);
  1100. p1:=ccallnode.create(paras,tprocsym(propaccesslist.firstsym^.sym),st,p1,callflags);
  1101. paras:=nil;
  1102. include(p1.flags,nf_isproperty);
  1103. end
  1104. else
  1105. begin
  1106. p1:=cerrornode.create;
  1107. Message(type_e_mismatch);
  1108. end;
  1109. end;
  1110. end
  1111. else
  1112. begin
  1113. { error, no function to read property }
  1114. p1:=cerrornode.create;
  1115. Message(parser_e_no_procedure_to_access_property);
  1116. end;
  1117. end;
  1118. { release paras if not used }
  1119. if assigned(paras) then
  1120. paras.free;
  1121. end;
  1122. { the ID token has to be consumed before calling this function }
  1123. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  1124. var
  1125. static_name : string;
  1126. isclassref : boolean;
  1127. srsymtable : tsymtable;
  1128. begin
  1129. if sym=nil then
  1130. begin
  1131. { pattern is still valid unless
  1132. there is another ID just after the ID of sym }
  1133. Message1(sym_e_id_no_member,orgpattern);
  1134. p1.free;
  1135. p1:=cerrornode.create;
  1136. { try to clean up }
  1137. again:=false;
  1138. end
  1139. else
  1140. begin
  1141. if assigned(p1) then
  1142. begin
  1143. if not assigned(p1.resultdef) then
  1144. do_typecheckpass(p1);
  1145. isclassref:=(p1.resultdef.deftype=classrefdef);
  1146. end
  1147. else
  1148. isclassref:=false;
  1149. { we assume, that only procsyms and varsyms are in an object }
  1150. { symbol table, for classes, properties are allowed }
  1151. case sym.typ of
  1152. procsym:
  1153. begin
  1154. do_proc_call(sym,sym.owner,classh,
  1155. (getaddr and not(token in [_CARET,_POINT])),
  1156. again,p1,callflags);
  1157. { we need to know which procedure is called }
  1158. do_typecheckpass(p1);
  1159. { calling using classref? }
  1160. if isclassref and
  1161. (p1.nodetype=calln) and
  1162. assigned(tcallnode(p1).procdefinition) and
  1163. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1164. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1165. Message(parser_e_only_class_methods_via_class_ref);
  1166. end;
  1167. fieldvarsym:
  1168. begin
  1169. if (sp_static in sym.symoptions) then
  1170. begin
  1171. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1172. searchsym(static_name,sym,srsymtable);
  1173. if assigned(sym) then
  1174. check_hints(sym,sym.symoptions);
  1175. p1.free;
  1176. p1:=cloadnode.create(sym,srsymtable);
  1177. end
  1178. else
  1179. begin
  1180. if isclassref then
  1181. Message(parser_e_only_class_methods_via_class_ref);
  1182. p1:=csubscriptnode.create(sym,p1);
  1183. end;
  1184. end;
  1185. propertysym:
  1186. begin
  1187. if isclassref then
  1188. Message(parser_e_only_class_methods_via_class_ref);
  1189. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1190. end;
  1191. else internalerror(16);
  1192. end;
  1193. end;
  1194. end;
  1195. {****************************************************************************
  1196. Factor
  1197. ****************************************************************************}
  1198. {$maxfpuregisters 0}
  1199. function factor(getaddr : boolean) : tnode;
  1200. {---------------------------------------------
  1201. Factor_read_id
  1202. ---------------------------------------------}
  1203. procedure factor_read_id(var p1:tnode;var again:boolean);
  1204. var
  1205. pc : pchar;
  1206. len : longint;
  1207. srsym : tsym;
  1208. possible_error : boolean;
  1209. srsymtable : tsymtable;
  1210. hdef : tdef;
  1211. static_name : string;
  1212. begin
  1213. { allow post fix operators }
  1214. again:=true;
  1215. consume_sym(srsym,srsymtable);
  1216. { Access to funcret or need to call the function? }
  1217. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1218. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1219. (
  1220. (token=_LKLAMMER) or
  1221. (
  1222. (
  1223. (m_tp7 in current_settings.modeswitches) or
  1224. (m_delphi in current_settings.modeswitches)
  1225. ) and
  1226. (afterassignment or in_args) and
  1227. not(vo_is_result in tabstractvarsym(srsym).varoptions)
  1228. )
  1229. ) then
  1230. begin
  1231. hdef:=tdef(srsym.owner.defowner);
  1232. if assigned(hdef) and
  1233. (hdef.deftype=procdef) then
  1234. srsym:=tprocdef(hdef).procsym
  1235. else
  1236. begin
  1237. Message(parser_e_illegal_expression);
  1238. srsym:=generrorsym;
  1239. end;
  1240. srsymtable:=srsym.owner;
  1241. end;
  1242. begin
  1243. case srsym.typ of
  1244. absolutevarsym :
  1245. begin
  1246. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1247. begin
  1248. p1:=nil;
  1249. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1250. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1251. include(p1.flags,nf_absolute);
  1252. end
  1253. else
  1254. p1:=cloadnode.create(srsym,srsymtable);
  1255. end;
  1256. globalvarsym,
  1257. localvarsym,
  1258. paravarsym,
  1259. fieldvarsym :
  1260. begin
  1261. if (sp_static in srsym.symoptions) then
  1262. begin
  1263. static_name:=lower(srsym.owner.name^)+'_'+srsym.name;
  1264. searchsym(static_name,srsym,srsymtable);
  1265. if assigned(srsym) then
  1266. check_hints(srsym,srsym.symoptions);
  1267. end
  1268. else
  1269. begin
  1270. { are we in a class method, we check here the
  1271. srsymtable, because a field in another object
  1272. also has objectsymtable. And withsymtable is
  1273. not possible for self in class methods (PFV) }
  1274. if (srsymtable.symtabletype=objectsymtable) and
  1275. assigned(current_procinfo) and
  1276. (po_classmethod in current_procinfo.procdef.procoptions) then
  1277. Message(parser_e_only_class_methods);
  1278. end;
  1279. case srsymtable.symtabletype of
  1280. objectsymtable :
  1281. begin
  1282. p1:=csubscriptnode.create(srsym,load_self_node);
  1283. node_tree_set_filepos(p1,aktfilepos);
  1284. end;
  1285. withsymtable :
  1286. begin
  1287. p1:=csubscriptnode.create(srsym,tnode(twithsymtable(srsymtable).withrefnode).getcopy);
  1288. node_tree_set_filepos(p1,aktfilepos);
  1289. end;
  1290. else
  1291. p1:=cloadnode.create(srsym,srsymtable);
  1292. end;
  1293. end;
  1294. typedconstsym :
  1295. begin
  1296. p1:=cloadnode.create(srsym,srsymtable);
  1297. end;
  1298. syssym :
  1299. begin
  1300. p1:=statement_syssym(tsyssym(srsym).number);
  1301. end;
  1302. typesym :
  1303. begin
  1304. hdef:=ttypesym(srsym).typedef;
  1305. if not assigned(hdef) then
  1306. begin
  1307. again:=false;
  1308. end
  1309. else
  1310. begin
  1311. { We need to know if this unit uses Variants }
  1312. if (hdef=cvarianttype) and
  1313. not(cs_compilesystem in current_settings.moduleswitches) then
  1314. current_module.flags:=current_module.flags or uf_uses_variants;
  1315. if (block_type<>bt_specialize) and
  1316. try_to_consume(_LKLAMMER) then
  1317. begin
  1318. p1:=comp_expr(true);
  1319. consume(_RKLAMMER);
  1320. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1321. end
  1322. else { not LKLAMMER }
  1323. if (token=_POINT) and
  1324. is_object(hdef) then
  1325. begin
  1326. consume(_POINT);
  1327. if assigned(current_procinfo) and
  1328. assigned(current_procinfo.procdef._class) and
  1329. not(getaddr) then
  1330. begin
  1331. if current_procinfo.procdef._class.is_related(tobjectdef(hdef)) then
  1332. begin
  1333. p1:=ctypenode.create(hdef);
  1334. { search also in inherited methods }
  1335. searchsym_in_class(tobjectdef(hdef),current_procinfo.procdef._class,pattern,srsym,srsymtable);
  1336. if assigned(srsym) then
  1337. check_hints(srsym,srsym.symoptions);
  1338. consume(_ID);
  1339. do_member_read(tobjectdef(hdef),false,srsym,p1,again,[]);
  1340. end
  1341. else
  1342. begin
  1343. Message(parser_e_no_super_class);
  1344. again:=false;
  1345. end;
  1346. end
  1347. else
  1348. begin
  1349. { allows @TObject.Load }
  1350. { also allows static methods and variables }
  1351. p1:=ctypenode.create(hdef);
  1352. { TP allows also @TMenu.Load if Load is only }
  1353. { defined in an anchestor class }
  1354. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1355. if assigned(srsym) then
  1356. begin
  1357. check_hints(srsym,srsym.symoptions);
  1358. if not(getaddr) and not(sp_static in srsym.symoptions) then
  1359. Message(sym_e_only_static_in_static)
  1360. else
  1361. begin
  1362. consume(_ID);
  1363. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1364. end;
  1365. end
  1366. else
  1367. Message1(sym_e_id_no_member,orgpattern);
  1368. end;
  1369. end
  1370. else
  1371. begin
  1372. { class reference ? }
  1373. if is_class(hdef) then
  1374. begin
  1375. if getaddr and (token=_POINT) then
  1376. begin
  1377. consume(_POINT);
  1378. { allows @Object.Method }
  1379. { also allows static methods and variables }
  1380. p1:=ctypenode.create(hdef);
  1381. { TP allows also @TMenu.Load if Load is only }
  1382. { defined in an anchestor class }
  1383. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1384. if assigned(srsym) then
  1385. begin
  1386. check_hints(srsym,srsym.symoptions);
  1387. consume(_ID);
  1388. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1389. end
  1390. else
  1391. begin
  1392. Message1(sym_e_id_no_member,orgpattern);
  1393. consume(_ID);
  1394. end;
  1395. end
  1396. else
  1397. begin
  1398. p1:=ctypenode.create(hdef);
  1399. { For a type block we simply return only
  1400. the type. For all other blocks we return
  1401. a loadvmt node }
  1402. if not(block_type in [bt_type,bt_specialize]) then
  1403. p1:=cloadvmtaddrnode.create(p1);
  1404. end;
  1405. end
  1406. else
  1407. p1:=ctypenode.create(hdef);
  1408. end;
  1409. end;
  1410. end;
  1411. enumsym :
  1412. begin
  1413. p1:=genenumnode(tenumsym(srsym));
  1414. end;
  1415. constsym :
  1416. begin
  1417. case tconstsym(srsym).consttyp of
  1418. constord :
  1419. begin
  1420. if tconstsym(srsym).constdef=nil then
  1421. internalerror(200403232);
  1422. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).constdef,true);
  1423. end;
  1424. conststring :
  1425. begin
  1426. len:=tconstsym(srsym).value.len;
  1427. if not(cs_ansistrings in current_settings.localswitches) and (len>255) then
  1428. len:=255;
  1429. getmem(pc,len+1);
  1430. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1431. pc[len]:=#0;
  1432. p1:=cstringconstnode.createpchar(pc,len);
  1433. end;
  1434. constwstring :
  1435. p1:=cstringconstnode.createwstr(pcompilerwidestring(tconstsym(srsym).value.valueptr));
  1436. constreal :
  1437. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1438. constset :
  1439. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).constdef);
  1440. constpointer :
  1441. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).constdef);
  1442. constnil :
  1443. p1:=cnilnode.create;
  1444. constresourcestring:
  1445. begin
  1446. p1:=cloadnode.create(srsym,srsymtable);
  1447. do_typecheckpass(p1);
  1448. p1.resultdef:=cansistringtype;
  1449. end;
  1450. constguid :
  1451. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1452. else
  1453. internalerror(200507181);
  1454. end;
  1455. end;
  1456. procsym :
  1457. begin
  1458. { are we in a class method ? }
  1459. possible_error:=(srsymtable.symtabletype<>withsymtable) and
  1460. (srsym.owner.symtabletype=objectsymtable) and
  1461. not(is_interface(tdef(srsym.owner.defowner))) and
  1462. assigned(current_procinfo) and
  1463. (po_classmethod in current_procinfo.procdef.procoptions);
  1464. do_proc_call(srsym,srsymtable,nil,
  1465. (getaddr and not(token in [_CARET,_POINT])),
  1466. again,p1,[]);
  1467. { we need to know which procedure is called }
  1468. if possible_error then
  1469. begin
  1470. do_typecheckpass(p1);
  1471. if (p1.nodetype=calln) and
  1472. assigned(tcallnode(p1).procdefinition) and
  1473. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1474. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1475. Message(parser_e_only_class_methods);
  1476. end;
  1477. end;
  1478. propertysym :
  1479. begin
  1480. { access to property in a method }
  1481. { are we in a class method ? }
  1482. if (srsymtable.symtabletype=objectsymtable) and
  1483. assigned(current_procinfo) and
  1484. (po_classmethod in current_procinfo.procdef.procoptions) then
  1485. Message(parser_e_only_class_methods);
  1486. { no method pointer }
  1487. p1:=nil;
  1488. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1489. end;
  1490. labelsym :
  1491. begin
  1492. { Support @label }
  1493. if getaddr then
  1494. p1:=cloadnode.create(srsym,srsym.owner)
  1495. else
  1496. begin
  1497. consume(_COLON);
  1498. if tlabelsym(srsym).defined then
  1499. Message(sym_e_label_already_defined);
  1500. tlabelsym(srsym).defined:=true;
  1501. p1:=clabelnode.create(nil);
  1502. tlabelsym(srsym).code:=p1;
  1503. end;
  1504. end;
  1505. errorsym :
  1506. begin
  1507. p1:=cerrornode.create;
  1508. if try_to_consume(_LKLAMMER) then
  1509. begin
  1510. parse_paras(false,_RKLAMMER);
  1511. consume(_RKLAMMER);
  1512. end;
  1513. end;
  1514. else
  1515. begin
  1516. p1:=cerrornode.create;
  1517. Message(parser_e_illegal_expression);
  1518. end;
  1519. end; { end case }
  1520. end;
  1521. end;
  1522. {---------------------------------------------
  1523. Factor_Read_Set
  1524. ---------------------------------------------}
  1525. { Read a set between [] }
  1526. function factor_read_set:tnode;
  1527. var
  1528. p1,p2 : tnode;
  1529. lastp,
  1530. buildp : tarrayconstructornode;
  1531. begin
  1532. buildp:=nil;
  1533. { be sure that a least one arrayconstructn is used, also for an
  1534. empty [] }
  1535. if token=_RECKKLAMMER then
  1536. buildp:=carrayconstructornode.create(nil,buildp)
  1537. else
  1538. repeat
  1539. p1:=comp_expr(true);
  1540. if try_to_consume(_POINTPOINT) then
  1541. begin
  1542. p2:=comp_expr(true);
  1543. p1:=carrayconstructorrangenode.create(p1,p2);
  1544. end;
  1545. { insert at the end of the tree, to get the correct order }
  1546. if not assigned(buildp) then
  1547. begin
  1548. buildp:=carrayconstructornode.create(p1,nil);
  1549. lastp:=buildp;
  1550. end
  1551. else
  1552. begin
  1553. lastp.right:=carrayconstructornode.create(p1,nil);
  1554. lastp:=tarrayconstructornode(lastp.right);
  1555. end;
  1556. { there could be more elements }
  1557. until not try_to_consume(_COMMA);
  1558. factor_read_set:=buildp;
  1559. end;
  1560. {---------------------------------------------
  1561. PostFixOperators
  1562. ---------------------------------------------}
  1563. procedure postfixoperators(var p1:tnode;var again:boolean);
  1564. { tries to avoid syntax errors after invalid qualifiers }
  1565. procedure recoverconsume_postfixops;
  1566. begin
  1567. repeat
  1568. if not try_to_consume(_CARET) then
  1569. if try_to_consume(_POINT) then
  1570. try_to_consume(_ID)
  1571. else if try_to_consume(_LECKKLAMMER) then
  1572. begin
  1573. repeat
  1574. comp_expr(true);
  1575. until not try_to_consume(_COMMA);
  1576. consume(_RECKKLAMMER);
  1577. end
  1578. else
  1579. break;
  1580. until false;
  1581. end;
  1582. procedure handle_variantarray;
  1583. var
  1584. p4 : tnode;
  1585. newstatement : tstatementnode;
  1586. tempresultvariant,
  1587. temp : ttempcreatenode;
  1588. paras : tcallparanode;
  1589. newblock : tnode;
  1590. countindices : aint;
  1591. begin
  1592. { create statements with call initialize the arguments and
  1593. call fpc_dynarr_setlength }
  1594. newblock:=internalstatements(newstatement);
  1595. { get temp for array of indicies,
  1596. we set the real size later }
  1597. temp:=ctempcreatenode.create(sinttype,4,tt_persistent,false);
  1598. addstatement(newstatement,temp);
  1599. countindices:=0;
  1600. repeat
  1601. p4:=comp_expr(true);
  1602. addstatement(newstatement,cassignmentnode.create(
  1603. ctemprefnode.create_offset(temp,countindices*sinttype.size),p4));
  1604. inc(countindices);
  1605. until not try_to_consume(_COMMA);
  1606. { set real size }
  1607. temp.size:=countindices*sinttype.size;
  1608. consume(_RECKKLAMMER);
  1609. { we need only a write access if a := follows }
  1610. if token=_ASSIGNMENT then
  1611. begin
  1612. consume(_ASSIGNMENT);
  1613. p4:=comp_expr(true);
  1614. { create call to fpc_vararray_put }
  1615. paras:=ccallparanode.create(cordconstnode.create
  1616. (countindices,sinttype,true),
  1617. ccallparanode.create(caddrnode.create_internal
  1618. (ctemprefnode.create(temp)),
  1619. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1620. ccallparanode.create(p1
  1621. ,nil))));
  1622. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1623. addstatement(newstatement,ctempdeletenode.create(temp));
  1624. end
  1625. else
  1626. begin
  1627. { create temp for result }
  1628. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1629. addstatement(newstatement,tempresultvariant);
  1630. { create call to fpc_vararray_get }
  1631. paras:=ccallparanode.create(cordconstnode.create
  1632. (countindices,sinttype,true),
  1633. ccallparanode.create(caddrnode.create_internal
  1634. (ctemprefnode.create(temp)),
  1635. ccallparanode.create(p1,
  1636. ccallparanode.create(
  1637. ctemprefnode.create(tempresultvariant)
  1638. ,nil))));
  1639. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1640. addstatement(newstatement,ctempdeletenode.create(temp));
  1641. { the last statement should return the value as
  1642. location and type, this is done be referencing the
  1643. temp and converting it first from a persistent temp to
  1644. normal temp }
  1645. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1646. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1647. end;
  1648. p1:=newblock;
  1649. end;
  1650. var
  1651. store_static : boolean;
  1652. protsym : tpropertysym;
  1653. p2,p3 : tnode;
  1654. srsym : tsym;
  1655. srsymtable : tsymtable;
  1656. classh : tobjectdef;
  1657. ok: boolean;
  1658. label
  1659. skipreckklammercheck;
  1660. begin
  1661. again:=true;
  1662. while again do
  1663. begin
  1664. { we need the resultdef }
  1665. do_typecheckpass(p1);
  1666. if codegenerror then
  1667. begin
  1668. recoverconsume_postfixops;
  1669. exit;
  1670. end;
  1671. { handle token }
  1672. case token of
  1673. _CARET:
  1674. begin
  1675. consume(_CARET);
  1676. { support tp/mac procvar^ if the procvar returns a
  1677. pointer type }
  1678. if ((m_tp_procvar in current_settings.modeswitches) or
  1679. (m_mac_procvar in current_settings.modeswitches)) and
  1680. (p1.resultdef.deftype=procvardef) and
  1681. (tprocvardef(p1.resultdef).returndef.deftype=pointerdef) then
  1682. begin
  1683. p1:=ccallnode.create_procvar(nil,p1);
  1684. typecheckpass(p1);
  1685. end;
  1686. if (p1.resultdef.deftype<>pointerdef) then
  1687. begin
  1688. { ^ as binary operator is a problem!!!! (FK) }
  1689. again:=false;
  1690. Message(parser_e_invalid_qualifier);
  1691. recoverconsume_postfixops;
  1692. p1.destroy;
  1693. p1:=cerrornode.create;
  1694. end
  1695. else
  1696. p1:=cderefnode.create(p1);
  1697. end;
  1698. _LECKKLAMMER:
  1699. begin
  1700. if is_class_or_interface(p1.resultdef) then
  1701. begin
  1702. { default property }
  1703. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1704. if not(assigned(protsym)) then
  1705. begin
  1706. p1.destroy;
  1707. p1:=cerrornode.create;
  1708. again:=false;
  1709. message(parser_e_no_default_property_available);
  1710. end
  1711. else
  1712. begin
  1713. { The property symbol is referenced indirect }
  1714. inc(protsym.refs);
  1715. handle_propertysym(protsym,protsym.owner,p1);
  1716. end;
  1717. end
  1718. else
  1719. begin
  1720. consume(_LECKKLAMMER);
  1721. repeat
  1722. ok := true;
  1723. case p1.resultdef.deftype of
  1724. pointerdef:
  1725. begin
  1726. { support delphi autoderef }
  1727. if (tpointerdef(p1.resultdef).pointeddef.deftype=arraydef) and
  1728. (m_autoderef in current_settings.modeswitches) then
  1729. p1:=cderefnode.create(p1);
  1730. p2:=comp_expr(true);
  1731. { Support Pbytevar[0..9] which returns array [0..9].}
  1732. if try_to_consume(_POINTPOINT) then
  1733. p2:=crangenode.create(p2,comp_expr(true));
  1734. p1:=cvecnode.create(p1,p2);
  1735. end;
  1736. variantdef:
  1737. begin
  1738. handle_variantarray;
  1739. { the RECKKLAMMER is already read }
  1740. goto skipreckklammercheck;
  1741. end;
  1742. stringdef :
  1743. begin
  1744. p2:=comp_expr(true);
  1745. { Support string[0..9] which returns array [0..9] of char.}
  1746. if try_to_consume(_POINTPOINT) then
  1747. p2:=crangenode.create(p2,comp_expr(true));
  1748. p1:=cvecnode.create(p1,p2);
  1749. end;
  1750. arraydef,
  1751. orddef :
  1752. begin
  1753. { in MacPas mode, you can treat a 32bit int as }
  1754. { an array[1..4] of char. The }
  1755. { FPC_Internal_Four_Char_Array is defined in }
  1756. { the macpas unit }
  1757. if (p1.resultdef.deftype = orddef) then
  1758. begin
  1759. if (m_mac in current_settings.modeswitches) and
  1760. is_integer(p1.resultdef) and
  1761. (p1.resultdef.size = 4) then
  1762. int_to_4cc(p1)
  1763. else
  1764. ok := false;
  1765. end;
  1766. if ok then
  1767. begin
  1768. p2:=comp_expr(true);
  1769. { support SEG:OFS for go32v2 Mem[] }
  1770. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1771. (p1.nodetype=loadn) and
  1772. assigned(tloadnode(p1).symtableentry) and
  1773. assigned(tloadnode(p1).symtableentry.owner.name) and
  1774. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1775. ((tloadnode(p1).symtableentry.name='MEM') or
  1776. (tloadnode(p1).symtableentry.name='MEMW') or
  1777. (tloadnode(p1).symtableentry.name='MEML')) then
  1778. begin
  1779. if try_to_consume(_COLON) then
  1780. begin
  1781. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1782. p2:=comp_expr(true);
  1783. p2:=caddnode.create(addn,p2,p3);
  1784. if try_to_consume(_POINTPOINT) then
  1785. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1786. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true),p3.getcopy));
  1787. p1:=cvecnode.create(p1,p2);
  1788. include(tvecnode(p1).flags,nf_memseg);
  1789. include(tvecnode(p1).flags,nf_memindex);
  1790. end
  1791. else
  1792. begin
  1793. if try_to_consume(_POINTPOINT) then
  1794. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1795. p2:=crangenode.create(p2,comp_expr(true));
  1796. p1:=cvecnode.create(p1,p2);
  1797. include(tvecnode(p1).flags,nf_memindex);
  1798. end;
  1799. end
  1800. else
  1801. begin
  1802. if try_to_consume(_POINTPOINT) then
  1803. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1804. p2:=crangenode.create(p2,comp_expr(true));
  1805. p1:=cvecnode.create(p1,p2);
  1806. end;
  1807. end;
  1808. end;
  1809. else
  1810. ok := false;
  1811. end;
  1812. if not ok then
  1813. begin
  1814. Message(parser_e_invalid_qualifier);
  1815. p1.destroy;
  1816. p1:=cerrornode.create;
  1817. comp_expr(true);
  1818. again:=false;
  1819. end;
  1820. do_typecheckpass(p1);
  1821. until not try_to_consume(_COMMA);
  1822. consume(_RECKKLAMMER);
  1823. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1824. skipreckklammercheck:
  1825. end;
  1826. end;
  1827. _POINT :
  1828. begin
  1829. consume(_POINT);
  1830. if (p1.resultdef.deftype=pointerdef) and
  1831. (m_autoderef in current_settings.modeswitches) then
  1832. begin
  1833. p1:=cderefnode.create(p1);
  1834. do_typecheckpass(p1);
  1835. end;
  1836. case p1.resultdef.deftype of
  1837. recorddef:
  1838. begin
  1839. if token=_ID then
  1840. begin
  1841. srsym:=tsym(trecorddef(p1.resultdef).symtable.search(pattern));
  1842. if assigned(srsym) and
  1843. (srsym.typ=fieldvarsym) then
  1844. begin
  1845. check_hints(srsym,srsym.symoptions);
  1846. p1:=csubscriptnode.create(srsym,p1)
  1847. end
  1848. else
  1849. begin
  1850. Message1(sym_e_illegal_field,pattern);
  1851. p1.destroy;
  1852. p1:=cerrornode.create;
  1853. end;
  1854. end;
  1855. consume(_ID);
  1856. end;
  1857. variantdef:
  1858. { dispatch call }
  1859. try_to_consume(_ID);
  1860. classrefdef:
  1861. begin
  1862. if token=_ID then
  1863. begin
  1864. classh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1865. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1866. if assigned(srsym) then
  1867. begin
  1868. check_hints(srsym,srsym.symoptions);
  1869. consume(_ID);
  1870. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1871. end
  1872. else
  1873. begin
  1874. Message1(sym_e_id_no_member,orgpattern);
  1875. p1.destroy;
  1876. p1:=cerrornode.create;
  1877. { try to clean up }
  1878. consume(_ID);
  1879. end;
  1880. end
  1881. else { Error }
  1882. Consume(_ID);
  1883. end;
  1884. objectdef:
  1885. begin
  1886. if token=_ID then
  1887. begin
  1888. store_static:=allow_only_static;
  1889. allow_only_static:=false;
  1890. classh:=tobjectdef(p1.resultdef);
  1891. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1892. allow_only_static:=store_static;
  1893. if assigned(srsym) then
  1894. begin
  1895. check_hints(srsym,srsym.symoptions);
  1896. consume(_ID);
  1897. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1898. end
  1899. else
  1900. begin
  1901. Message1(sym_e_id_no_member,orgpattern);
  1902. p1.destroy;
  1903. p1:=cerrornode.create;
  1904. { try to clean up }
  1905. consume(_ID);
  1906. end;
  1907. end
  1908. else { Error }
  1909. Consume(_ID);
  1910. end;
  1911. pointerdef:
  1912. begin
  1913. Message(parser_e_invalid_qualifier);
  1914. if tpointerdef(p1.resultdef).pointeddef.deftype in [recorddef,objectdef,classrefdef] then
  1915. Message(parser_h_maybe_deref_caret_missing);
  1916. end;
  1917. else
  1918. begin
  1919. Message(parser_e_invalid_qualifier);
  1920. p1.destroy;
  1921. p1:=cerrornode.create;
  1922. { Error }
  1923. consume(_ID);
  1924. end;
  1925. end;
  1926. end;
  1927. else
  1928. begin
  1929. { is this a procedure variable ? }
  1930. if assigned(p1.resultdef) and
  1931. (p1.resultdef.deftype=procvardef) then
  1932. begin
  1933. if assigned(getprocvardef) and
  1934. equal_defs(p1.resultdef,getprocvardef) then
  1935. again:=false
  1936. else
  1937. begin
  1938. if try_to_consume(_LKLAMMER) then
  1939. begin
  1940. p2:=parse_paras(false,_RKLAMMER);
  1941. consume(_RKLAMMER);
  1942. p1:=ccallnode.create_procvar(p2,p1);
  1943. { proc():= is never possible }
  1944. if token=_ASSIGNMENT then
  1945. begin
  1946. Message(parser_e_illegal_expression);
  1947. p1.free;
  1948. p1:=cerrornode.create;
  1949. again:=false;
  1950. end;
  1951. end
  1952. else
  1953. again:=false;
  1954. end;
  1955. end
  1956. else
  1957. again:=false;
  1958. end;
  1959. end;
  1960. end; { while again }
  1961. end;
  1962. {---------------------------------------------
  1963. Factor (Main)
  1964. ---------------------------------------------}
  1965. var
  1966. l : longint;
  1967. ic : int64;
  1968. qc : qword;
  1969. {$ifndef cpu64}
  1970. card : cardinal;
  1971. {$endif cpu64}
  1972. oldp1,
  1973. p1 : tnode;
  1974. code : integer;
  1975. again : boolean;
  1976. srsym : tsym;
  1977. srsymtable : tsymtable;
  1978. pd : tprocdef;
  1979. hclassdef : tobjectdef;
  1980. d : bestreal;
  1981. hs,hsorg : string;
  1982. hdef : tdef;
  1983. filepos : tfileposinfo;
  1984. begin
  1985. oldp1:=nil;
  1986. p1:=nil;
  1987. filepos:=akttokenpos;
  1988. again:=false;
  1989. if token=_ID then
  1990. begin
  1991. again:=true;
  1992. { Handle references to self }
  1993. if (idtoken=_SELF) and
  1994. not(block_type in [bt_const,bt_type]) and
  1995. assigned(current_procinfo) and
  1996. assigned(current_procinfo.procdef._class) then
  1997. begin
  1998. p1:=load_self_node;
  1999. consume(_ID);
  2000. again:=true;
  2001. end
  2002. else
  2003. factor_read_id(p1,again);
  2004. if again then
  2005. begin
  2006. if (p1<>oldp1) then
  2007. begin
  2008. if assigned(p1) then
  2009. p1.fileinfo:=filepos;
  2010. oldp1:=p1;
  2011. filepos:=akttokenpos;
  2012. end;
  2013. { handle post fix operators }
  2014. postfixoperators(p1,again);
  2015. end;
  2016. end
  2017. else
  2018. case token of
  2019. _RETURN :
  2020. begin
  2021. consume(_RETURN);
  2022. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2023. p1 := cexitnode.create(comp_expr(true))
  2024. else
  2025. p1 := cexitnode.create(nil);
  2026. end;
  2027. _INHERITED :
  2028. begin
  2029. again:=true;
  2030. consume(_INHERITED);
  2031. if assigned(current_procinfo) and
  2032. assigned(current_procinfo.procdef._class) then
  2033. begin
  2034. hclassdef:=current_procinfo.procdef._class.childof;
  2035. { if inherited; only then we need the method with
  2036. the same name }
  2037. if token in endtokens then
  2038. begin
  2039. hs:=current_procinfo.procdef.procsym.name;
  2040. hsorg:=current_procinfo.procdef.procsym.realname;
  2041. anon_inherited:=true;
  2042. { For message methods we need to search using the message
  2043. number or string }
  2044. pd:=tprocsym(current_procinfo.procdef.procsym).first_procdef;
  2045. srdef:=nil;
  2046. if (po_msgint in pd.procoptions) then
  2047. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2048. else
  2049. if (po_msgstr in pd.procoptions) then
  2050. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2051. else
  2052. searchsym_in_class(hclassdef,current_procinfo.procdef._class,hs,srsym,srsymtable);
  2053. end
  2054. else
  2055. begin
  2056. hs:=pattern;
  2057. hsorg:=orgpattern;
  2058. consume(_ID);
  2059. anon_inherited:=false;
  2060. searchsym_in_class(hclassdef,current_procinfo.procdef._class,hs,srsym,srsymtable);
  2061. end;
  2062. if assigned(srsym) then
  2063. begin
  2064. check_hints(srsym,srsym.symoptions);
  2065. { load the procdef from the inherited class and
  2066. not from self }
  2067. if srsym.typ in [procsym,propertysym] then
  2068. begin
  2069. if (srsym.typ = procsym) then
  2070. begin
  2071. hdef:=hclassdef;
  2072. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2073. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2074. hdef:=tclassrefdef.create(hdef);
  2075. p1:=ctypenode.create(hdef);
  2076. end;
  2077. end
  2078. else
  2079. begin
  2080. Message(parser_e_methode_id_expected);
  2081. p1:=cerrornode.create;
  2082. end;
  2083. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2084. end
  2085. else
  2086. begin
  2087. if anon_inherited then
  2088. begin
  2089. { For message methods we need to call DefaultHandler }
  2090. if (po_msgint in pd.procoptions) or
  2091. (po_msgstr in pd.procoptions) then
  2092. begin
  2093. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2094. if not assigned(srsym) or
  2095. (srsym.typ<>procsym) then
  2096. internalerror(200303171);
  2097. p1:=nil;
  2098. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2099. end
  2100. else
  2101. begin
  2102. { we need to ignore the inherited; }
  2103. p1:=cnothingnode.create;
  2104. end;
  2105. end
  2106. else
  2107. begin
  2108. Message1(sym_e_id_no_member,hsorg);
  2109. p1:=cerrornode.create;
  2110. end;
  2111. again:=false;
  2112. end;
  2113. { turn auto inheriting off }
  2114. anon_inherited:=false;
  2115. end
  2116. else
  2117. begin
  2118. Message(parser_e_generic_methods_only_in_methods);
  2119. again:=false;
  2120. p1:=cerrornode.create;
  2121. end;
  2122. postfixoperators(p1,again);
  2123. end;
  2124. _INTCONST :
  2125. begin
  2126. {$ifdef cpu64}
  2127. { when already running under 64bit must read int64 constant, because reading
  2128. cardinal first will also succeed (code=0) for values > maxcardinal, because
  2129. range checking is off by default (PFV) }
  2130. val(pattern,ic,code);
  2131. if code=0 then
  2132. begin
  2133. consume(_INTCONST);
  2134. int_to_type(ic,hdef);
  2135. p1:=cordconstnode.create(ic,hdef,true);
  2136. end
  2137. else
  2138. begin
  2139. { try qword next }
  2140. val(pattern,qc,code);
  2141. if code=0 then
  2142. begin
  2143. consume(_INTCONST);
  2144. hdef:=u64inttype;
  2145. p1:=cordconstnode.create(qc,hdef,true);
  2146. end;
  2147. end;
  2148. {$else}
  2149. { try cardinal first }
  2150. val(pattern,card,code);
  2151. if code=0 then
  2152. begin
  2153. consume(_INTCONST);
  2154. int_to_type(card,hdef);
  2155. p1:=cordconstnode.create(card,hdef,true);
  2156. end
  2157. else
  2158. begin
  2159. { then longint }
  2160. val(pattern,l,code);
  2161. if code = 0 then
  2162. begin
  2163. consume(_INTCONST);
  2164. int_to_type(l,hdef);
  2165. p1:=cordconstnode.create(l,hdef,true);
  2166. end
  2167. else
  2168. begin
  2169. { then int64 }
  2170. val(pattern,ic,code);
  2171. if code=0 then
  2172. begin
  2173. consume(_INTCONST);
  2174. int_to_type(ic,hdef);
  2175. p1:=cordconstnode.create(ic,hdef,true);
  2176. end
  2177. else
  2178. begin
  2179. { try qword next }
  2180. val(pattern,qc,code);
  2181. if code=0 then
  2182. begin
  2183. consume(_INTCONST);
  2184. hdef:=u64inttype;
  2185. p1:=cordconstnode.create(tconstexprint(qc),hdef,true);
  2186. end;
  2187. end;
  2188. end;
  2189. end;
  2190. {$endif}
  2191. if code<>0 then
  2192. begin
  2193. { finally float }
  2194. val(pattern,d,code);
  2195. if code<>0 then
  2196. begin
  2197. Message(parser_e_invalid_integer);
  2198. consume(_INTCONST);
  2199. l:=1;
  2200. p1:=cordconstnode.create(l,sinttype,true);
  2201. end
  2202. else
  2203. begin
  2204. consume(_INTCONST);
  2205. p1:=crealconstnode.create(d,pbestrealtype^);
  2206. end;
  2207. end;
  2208. end;
  2209. _REALNUMBER :
  2210. begin
  2211. val(pattern,d,code);
  2212. if code<>0 then
  2213. begin
  2214. Message(parser_e_error_in_real);
  2215. d:=1.0;
  2216. end;
  2217. consume(_REALNUMBER);
  2218. p1:=crealconstnode.create(d,pbestrealtype^);
  2219. end;
  2220. _STRING :
  2221. begin
  2222. string_dec(hdef);
  2223. { STRING can be also a type cast }
  2224. if try_to_consume(_LKLAMMER) then
  2225. begin
  2226. p1:=comp_expr(true);
  2227. consume(_RKLAMMER);
  2228. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2229. { handle postfix operators here e.g. string(a)[10] }
  2230. again:=true;
  2231. postfixoperators(p1,again);
  2232. end
  2233. else
  2234. p1:=ctypenode.create(hdef);
  2235. end;
  2236. _FILE :
  2237. begin
  2238. hdef:=cfiletype;
  2239. consume(_FILE);
  2240. { FILE can be also a type cast }
  2241. if try_to_consume(_LKLAMMER) then
  2242. begin
  2243. p1:=comp_expr(true);
  2244. consume(_RKLAMMER);
  2245. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2246. { handle postfix operators here e.g. string(a)[10] }
  2247. again:=true;
  2248. postfixoperators(p1,again);
  2249. end
  2250. else
  2251. begin
  2252. p1:=ctypenode.create(hdef);
  2253. end;
  2254. end;
  2255. _CSTRING :
  2256. begin
  2257. p1:=cstringconstnode.createstr(pattern);
  2258. consume(_CSTRING);
  2259. end;
  2260. _CCHAR :
  2261. begin
  2262. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2263. consume(_CCHAR);
  2264. end;
  2265. _CWSTRING:
  2266. begin
  2267. p1:=cstringconstnode.createwstr(patternw);
  2268. consume(_CWSTRING);
  2269. end;
  2270. _CWCHAR:
  2271. begin
  2272. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2273. consume(_CWCHAR);
  2274. end;
  2275. _KLAMMERAFFE :
  2276. begin
  2277. consume(_KLAMMERAFFE);
  2278. got_addrn:=true;
  2279. { support both @<x> and @(<x>) }
  2280. if try_to_consume(_LKLAMMER) then
  2281. begin
  2282. p1:=factor(true);
  2283. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2284. begin
  2285. again:=true;
  2286. postfixoperators(p1,again);
  2287. end;
  2288. consume(_RKLAMMER);
  2289. end
  2290. else
  2291. p1:=factor(true);
  2292. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2293. begin
  2294. again:=true;
  2295. postfixoperators(p1,again);
  2296. end;
  2297. got_addrn:=false;
  2298. p1:=caddrnode.create(p1);
  2299. if cs_typed_addresses in current_settings.localswitches then
  2300. include(p1.flags,nf_typedaddr);
  2301. { Store the procvar that we are expecting, the
  2302. addrn will use the information to find the correct
  2303. procdef or it will return an error }
  2304. if assigned(getprocvardef) and
  2305. (taddrnode(p1).left.nodetype = loadn) then
  2306. taddrnode(p1).getprocvardef:=getprocvardef;
  2307. end;
  2308. _LKLAMMER :
  2309. begin
  2310. consume(_LKLAMMER);
  2311. p1:=comp_expr(true);
  2312. consume(_RKLAMMER);
  2313. { it's not a good solution }
  2314. { but (a+b)^ makes some problems }
  2315. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2316. begin
  2317. again:=true;
  2318. postfixoperators(p1,again);
  2319. end;
  2320. end;
  2321. _LECKKLAMMER :
  2322. begin
  2323. consume(_LECKKLAMMER);
  2324. p1:=factor_read_set;
  2325. consume(_RECKKLAMMER);
  2326. end;
  2327. _PLUS :
  2328. begin
  2329. consume(_PLUS);
  2330. p1:=factor(false);
  2331. { we must generate a new node to do 0+<p1> otherwise the + will
  2332. not be checked }
  2333. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2334. end;
  2335. _MINUS :
  2336. begin
  2337. consume(_MINUS);
  2338. if (token = _INTCONST) then
  2339. begin
  2340. { ugly hack, but necessary to be able to parse }
  2341. { -9223372036854775808 as int64 (JM) }
  2342. pattern := '-'+pattern;
  2343. p1:=sub_expr(oppower,false);
  2344. { -1 ** 4 should be - (1 ** 4) and not
  2345. (-1) ** 4
  2346. This was the reason of tw0869.pp test failure PM }
  2347. if p1.nodetype=starstarn then
  2348. begin
  2349. if tbinarynode(p1).left.nodetype=ordconstn then
  2350. begin
  2351. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2352. p1:=cunaryminusnode.create(p1);
  2353. end
  2354. else if tbinarynode(p1).left.nodetype=realconstn then
  2355. begin
  2356. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2357. p1:=cunaryminusnode.create(p1);
  2358. end
  2359. else
  2360. internalerror(20021029);
  2361. end;
  2362. end
  2363. else
  2364. begin
  2365. p1:=sub_expr(oppower,false);
  2366. p1:=cunaryminusnode.create(p1);
  2367. end;
  2368. end;
  2369. _OP_NOT :
  2370. begin
  2371. consume(_OP_NOT);
  2372. p1:=factor(false);
  2373. p1:=cnotnode.create(p1);
  2374. end;
  2375. _TRUE :
  2376. begin
  2377. consume(_TRUE);
  2378. p1:=cordconstnode.create(1,booltype,false);
  2379. end;
  2380. _FALSE :
  2381. begin
  2382. consume(_FALSE);
  2383. p1:=cordconstnode.create(0,booltype,false);
  2384. end;
  2385. _NIL :
  2386. begin
  2387. consume(_NIL);
  2388. p1:=cnilnode.create;
  2389. { It's really ugly code nil^, but delphi allows it }
  2390. if token in [_CARET] then
  2391. begin
  2392. again:=true;
  2393. postfixoperators(p1,again);
  2394. end;
  2395. end;
  2396. else
  2397. begin
  2398. Message(parser_e_illegal_expression);
  2399. p1:=cerrornode.create;
  2400. { recover }
  2401. consume(token);
  2402. end;
  2403. end;
  2404. { generate error node if no node is created }
  2405. if not assigned(p1) then
  2406. begin
  2407. {$ifdef EXTDEBUG}
  2408. Comment(V_Warning,'factor: p1=nil');
  2409. {$endif}
  2410. p1:=cerrornode.create;
  2411. end;
  2412. { get the resultdef for the node }
  2413. if (not assigned(p1.resultdef)) then
  2414. do_typecheckpass(p1);
  2415. if assigned(p1) and
  2416. (p1<>oldp1) then
  2417. p1.fileinfo:=filepos;
  2418. factor:=p1;
  2419. end;
  2420. {$maxfpuregisters default}
  2421. {****************************************************************************
  2422. Sub_Expr
  2423. ****************************************************************************}
  2424. const
  2425. { Warning these stay be ordered !! }
  2426. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2427. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2428. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2429. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2430. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2431. [_STARSTAR] );
  2432. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2433. {Reads a subexpression while the operators are of the current precedence
  2434. level, or any higher level. Replaces the old term, simpl_expr and
  2435. simpl2_expr.}
  2436. var
  2437. p1,p2 : tnode;
  2438. oldt : Ttoken;
  2439. filepos : tfileposinfo;
  2440. begin
  2441. if pred_level=highest_precedence then
  2442. p1:=factor(false)
  2443. else
  2444. p1:=sub_expr(succ(pred_level),true);
  2445. repeat
  2446. if (token in operator_levels[pred_level]) and
  2447. ((token<>_EQUAL) or accept_equal) then
  2448. begin
  2449. oldt:=token;
  2450. filepos:=akttokenpos;
  2451. consume(token);
  2452. if pred_level=highest_precedence then
  2453. p2:=factor(false)
  2454. else
  2455. p2:=sub_expr(succ(pred_level),true);
  2456. case oldt of
  2457. _PLUS :
  2458. p1:=caddnode.create(addn,p1,p2);
  2459. _MINUS :
  2460. p1:=caddnode.create(subn,p1,p2);
  2461. _STAR :
  2462. p1:=caddnode.create(muln,p1,p2);
  2463. _SLASH :
  2464. p1:=caddnode.create(slashn,p1,p2);
  2465. _EQUAL :
  2466. p1:=caddnode.create(equaln,p1,p2);
  2467. _GT :
  2468. p1:=caddnode.create(gtn,p1,p2);
  2469. _LT :
  2470. p1:=caddnode.create(ltn,p1,p2);
  2471. _GTE :
  2472. p1:=caddnode.create(gten,p1,p2);
  2473. _LTE :
  2474. p1:=caddnode.create(lten,p1,p2);
  2475. _SYMDIF :
  2476. p1:=caddnode.create(symdifn,p1,p2);
  2477. _STARSTAR :
  2478. p1:=caddnode.create(starstarn,p1,p2);
  2479. _OP_AS :
  2480. p1:=casnode.create(p1,p2);
  2481. _OP_IN :
  2482. p1:=cinnode.create(p1,p2);
  2483. _OP_IS :
  2484. p1:=cisnode.create(p1,p2);
  2485. _OP_OR,
  2486. _PIPE {macpas only} :
  2487. begin
  2488. p1:=caddnode.create(orn,p1,p2);
  2489. if (oldt = _PIPE) then
  2490. include(p1.flags,nf_short_bool);
  2491. end;
  2492. _OP_AND,
  2493. _AMPERSAND {macpas only} :
  2494. begin
  2495. p1:=caddnode.create(andn,p1,p2);
  2496. if (oldt = _AMPERSAND) then
  2497. include(p1.flags,nf_short_bool);
  2498. end;
  2499. _OP_DIV :
  2500. p1:=cmoddivnode.create(divn,p1,p2);
  2501. _OP_NOT :
  2502. p1:=cnotnode.create(p1);
  2503. _OP_MOD :
  2504. p1:=cmoddivnode.create(modn,p1,p2);
  2505. _OP_SHL :
  2506. p1:=cshlshrnode.create(shln,p1,p2);
  2507. _OP_SHR :
  2508. p1:=cshlshrnode.create(shrn,p1,p2);
  2509. _OP_XOR :
  2510. p1:=caddnode.create(xorn,p1,p2);
  2511. _ASSIGNMENT :
  2512. p1:=cassignmentnode.create(p1,p2);
  2513. _CARET :
  2514. p1:=caddnode.create(caretn,p1,p2);
  2515. _UNEQUAL :
  2516. p1:=caddnode.create(unequaln,p1,p2);
  2517. end;
  2518. p1.fileinfo:=filepos;
  2519. end
  2520. else
  2521. break;
  2522. until false;
  2523. sub_expr:=p1;
  2524. end;
  2525. function comp_expr(accept_equal : boolean):tnode;
  2526. var
  2527. oldafterassignment : boolean;
  2528. p1 : tnode;
  2529. begin
  2530. oldafterassignment:=afterassignment;
  2531. afterassignment:=true;
  2532. p1:=sub_expr(opcompare,accept_equal);
  2533. { get the resultdef for this expression }
  2534. if not assigned(p1.resultdef) then
  2535. do_typecheckpass(p1);
  2536. afterassignment:=oldafterassignment;
  2537. comp_expr:=p1;
  2538. end;
  2539. function expr : tnode;
  2540. var
  2541. p1,p2 : tnode;
  2542. oldafterassignment : boolean;
  2543. oldp1 : tnode;
  2544. filepos : tfileposinfo;
  2545. begin
  2546. oldafterassignment:=afterassignment;
  2547. p1:=sub_expr(opcompare,true);
  2548. { get the resultdef for this expression }
  2549. if not assigned(p1.resultdef) then
  2550. do_typecheckpass(p1);
  2551. filepos:=akttokenpos;
  2552. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2553. afterassignment:=true;
  2554. oldp1:=p1;
  2555. case token of
  2556. _POINTPOINT :
  2557. begin
  2558. consume(_POINTPOINT);
  2559. p2:=sub_expr(opcompare,true);
  2560. p1:=crangenode.create(p1,p2);
  2561. end;
  2562. _ASSIGNMENT :
  2563. begin
  2564. consume(_ASSIGNMENT);
  2565. if (p1.resultdef.deftype=procvardef) then
  2566. getprocvardef:=tprocvardef(p1.resultdef);
  2567. p2:=sub_expr(opcompare,true);
  2568. if assigned(getprocvardef) then
  2569. handle_procvar(getprocvardef,p2);
  2570. getprocvardef:=nil;
  2571. p1:=cassignmentnode.create(p1,p2);
  2572. end;
  2573. _PLUSASN :
  2574. begin
  2575. consume(_PLUSASN);
  2576. p2:=sub_expr(opcompare,true);
  2577. p1:=gen_c_style_operator(addn,p1,p2);
  2578. end;
  2579. _MINUSASN :
  2580. begin
  2581. consume(_MINUSASN);
  2582. p2:=sub_expr(opcompare,true);
  2583. p1:=gen_c_style_operator(subn,p1,p2);
  2584. end;
  2585. _STARASN :
  2586. begin
  2587. consume(_STARASN );
  2588. p2:=sub_expr(opcompare,true);
  2589. p1:=gen_c_style_operator(muln,p1,p2);
  2590. end;
  2591. _SLASHASN :
  2592. begin
  2593. consume(_SLASHASN );
  2594. p2:=sub_expr(opcompare,true);
  2595. p1:=gen_c_style_operator(slashn,p1,p2);
  2596. end;
  2597. end;
  2598. { get the resultdef for this expression }
  2599. if not assigned(p1.resultdef) then
  2600. do_typecheckpass(p1);
  2601. afterassignment:=oldafterassignment;
  2602. if p1<>oldp1 then
  2603. p1.fileinfo:=filepos;
  2604. expr:=p1;
  2605. end;
  2606. function get_intconst:TConstExprInt;
  2607. {Reads an expression, tries to evalute it and check if it is an integer
  2608. constant. Then the constant is returned.}
  2609. var
  2610. p:tnode;
  2611. begin
  2612. result:=0;
  2613. p:=comp_expr(true);
  2614. if not codegenerror then
  2615. begin
  2616. if (p.nodetype<>ordconstn) or
  2617. not(is_integer(p.resultdef)) then
  2618. Message(parser_e_illegal_expression)
  2619. else
  2620. result:=tordconstnode(p).value;
  2621. end;
  2622. p.free;
  2623. end;
  2624. function get_stringconst:string;
  2625. {Reads an expression, tries to evaluate it and checks if it is a string
  2626. constant. Then the constant is returned.}
  2627. var
  2628. p:tnode;
  2629. begin
  2630. get_stringconst:='';
  2631. p:=comp_expr(true);
  2632. if p.nodetype<>stringconstn then
  2633. begin
  2634. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2635. get_stringconst:=char(tordconstnode(p).value)
  2636. else
  2637. Message(parser_e_illegal_expression);
  2638. end
  2639. else
  2640. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2641. p.free;
  2642. end;
  2643. end.