pexpr.pas 107 KB

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