pexpr.pas 107 KB

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