pexpr.pas 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888
  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).get_load_methodpointer);
  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. len : longint;
  1265. srsym : tsym;
  1266. unit_found,
  1267. possible_error : boolean;
  1268. srsymtable : TSymtable;
  1269. hdef : tdef;
  1270. static_name : string;
  1271. orgstoredpattern,
  1272. storedpattern : string;
  1273. begin
  1274. { allow post fix operators }
  1275. again:=true;
  1276. { first check for identifier }
  1277. if token<>_ID then
  1278. begin
  1279. srsym:=generrorsym;
  1280. srsymtable:=nil;
  1281. consume(_ID);
  1282. end
  1283. else
  1284. begin
  1285. searchsym(pattern,srsym,srsymtable);
  1286. { handle unit specification like System.Writeln }
  1287. unit_found:=try_consume_unitsym(srsym,srsymtable);
  1288. storedpattern:=pattern;
  1289. orgstoredpattern:=orgpattern;
  1290. consume(_ID);
  1291. { named parameter support }
  1292. found_arg_name:=false;
  1293. if not(unit_found) and
  1294. named_args_allowed and
  1295. (token=_ASSIGNMENT) then
  1296. begin
  1297. found_arg_name:=true;
  1298. p1:=cstringconstnode.createstr(storedpattern);
  1299. consume(_ASSIGNMENT);
  1300. exit;
  1301. end;
  1302. { if nothing found give error and return errorsym }
  1303. if assigned(srsym) then
  1304. check_hints(srsym,srsym.symoptions)
  1305. else
  1306. begin
  1307. identifier_not_found(orgstoredpattern);
  1308. srsym:=generrorsym;
  1309. srsymtable:=nil;
  1310. end;
  1311. end;
  1312. { Access to funcret or need to call the function? }
  1313. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1314. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1315. (
  1316. (token=_LKLAMMER) or
  1317. (
  1318. (
  1319. (m_tp7 in current_settings.modeswitches) or
  1320. (m_delphi in current_settings.modeswitches)
  1321. ) and
  1322. (afterassignment or in_args) and
  1323. not(vo_is_result in tabstractvarsym(srsym).varoptions)
  1324. )
  1325. ) then
  1326. begin
  1327. hdef:=tdef(srsym.owner.defowner);
  1328. if assigned(hdef) and
  1329. (hdef.typ=procdef) then
  1330. srsym:=tprocdef(hdef).procsym
  1331. else
  1332. begin
  1333. Message(parser_e_illegal_expression);
  1334. srsym:=generrorsym;
  1335. end;
  1336. srsymtable:=srsym.owner;
  1337. end;
  1338. begin
  1339. case srsym.typ of
  1340. absolutevarsym :
  1341. begin
  1342. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1343. begin
  1344. p1:=nil;
  1345. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1346. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1347. include(p1.flags,nf_absolute);
  1348. end
  1349. else
  1350. p1:=cloadnode.create(srsym,srsymtable);
  1351. end;
  1352. staticvarsym,
  1353. localvarsym,
  1354. paravarsym,
  1355. fieldvarsym :
  1356. begin
  1357. { check if we are reading a field of an object/class/ }
  1358. { record. is_member_read() will deal with withsymtables }
  1359. { if needed. }
  1360. if is_member_read(srsym,srsymtable,p1,hdef) then
  1361. begin
  1362. { if the field was originally found in an }
  1363. { objectsymtable, it means it's part of self }
  1364. if (srsymtable.symtabletype=ObjectSymtable) then
  1365. p1:=load_self_node;
  1366. { now, if the field itself is part of an objectsymtab }
  1367. { (it can be even if it was found in a withsymtable, }
  1368. { e.g., "with classinstance do field := 5"), then }
  1369. { let do_member_read handle it }
  1370. if (srsym.owner.symtabletype=ObjectSymtable) then
  1371. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1372. else
  1373. { otherwise it's a regular record subscript }
  1374. p1:=csubscriptnode.create(srsym,p1);
  1375. end
  1376. else
  1377. { regular non-field load }
  1378. p1:=cloadnode.create(srsym,srsymtable);
  1379. end;
  1380. syssym :
  1381. begin
  1382. p1:=statement_syssym(tsyssym(srsym).number);
  1383. end;
  1384. typesym :
  1385. begin
  1386. hdef:=ttypesym(srsym).typedef;
  1387. if not assigned(hdef) then
  1388. begin
  1389. again:=false;
  1390. end
  1391. else
  1392. begin
  1393. { We need to know if this unit uses Variants }
  1394. if (hdef=cvarianttype) and
  1395. not(cs_compilesystem in current_settings.moduleswitches) then
  1396. current_module.flags:=current_module.flags or uf_uses_variants;
  1397. if (block_type<>bt_specialize) and
  1398. try_to_consume(_LKLAMMER) then
  1399. begin
  1400. p1:=comp_expr(true);
  1401. consume(_RKLAMMER);
  1402. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1403. end
  1404. else { not LKLAMMER }
  1405. if (token=_POINT) and
  1406. is_object(hdef) then
  1407. begin
  1408. consume(_POINT);
  1409. if assigned(current_procinfo) and
  1410. assigned(current_procinfo.procdef._class) and
  1411. not(getaddr) then
  1412. begin
  1413. if current_procinfo.procdef._class.is_related(tobjectdef(hdef)) then
  1414. begin
  1415. p1:=ctypenode.create(hdef);
  1416. { search also in inherited methods }
  1417. searchsym_in_class(tobjectdef(hdef),current_procinfo.procdef._class,pattern,srsym,srsymtable);
  1418. if assigned(srsym) then
  1419. check_hints(srsym,srsym.symoptions);
  1420. consume(_ID);
  1421. do_member_read(tobjectdef(hdef),false,srsym,p1,again,[]);
  1422. end
  1423. else
  1424. begin
  1425. Message(parser_e_no_super_class);
  1426. again:=false;
  1427. end;
  1428. end
  1429. else
  1430. begin
  1431. { allows @TObject.Load }
  1432. { also allows static methods and variables }
  1433. p1:=ctypenode.create(hdef);
  1434. { TP allows also @TMenu.Load if Load is only }
  1435. { defined in an anchestor class }
  1436. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1437. if assigned(srsym) then
  1438. begin
  1439. check_hints(srsym,srsym.symoptions);
  1440. if not(getaddr) and not(sp_static in srsym.symoptions) then
  1441. Message(sym_e_only_static_in_static)
  1442. else
  1443. begin
  1444. consume(_ID);
  1445. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1446. end;
  1447. end
  1448. else
  1449. Message1(sym_e_id_no_member,orgpattern);
  1450. end;
  1451. end
  1452. else
  1453. begin
  1454. { class reference ? }
  1455. if is_class(hdef) then
  1456. begin
  1457. if getaddr and (token=_POINT) then
  1458. begin
  1459. consume(_POINT);
  1460. { allows @Object.Method }
  1461. { also allows static methods and variables }
  1462. p1:=ctypenode.create(hdef);
  1463. { TP allows also @TMenu.Load if Load is only }
  1464. { defined in an anchestor class }
  1465. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1466. if assigned(srsym) then
  1467. begin
  1468. check_hints(srsym,srsym.symoptions);
  1469. consume(_ID);
  1470. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1471. end
  1472. else
  1473. begin
  1474. Message1(sym_e_id_no_member,orgpattern);
  1475. consume(_ID);
  1476. end;
  1477. end
  1478. else
  1479. begin
  1480. p1:=ctypenode.create(hdef);
  1481. { For a type block we simply return only
  1482. the type. For all other blocks we return
  1483. a loadvmt node }
  1484. if not(block_type in [bt_type,bt_specialize]) then
  1485. p1:=cloadvmtaddrnode.create(p1);
  1486. end;
  1487. end
  1488. else
  1489. p1:=ctypenode.create(hdef);
  1490. end;
  1491. end;
  1492. end;
  1493. enumsym :
  1494. begin
  1495. p1:=genenumnode(tenumsym(srsym));
  1496. end;
  1497. constsym :
  1498. begin
  1499. case tconstsym(srsym).consttyp of
  1500. constord :
  1501. begin
  1502. if tconstsym(srsym).constdef=nil then
  1503. internalerror(200403232);
  1504. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).constdef,true);
  1505. end;
  1506. conststring :
  1507. begin
  1508. len:=tconstsym(srsym).value.len;
  1509. if not(cs_ansistrings in current_settings.localswitches) and (len>255) then
  1510. len:=255;
  1511. getmem(pc,len+1);
  1512. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1513. pc[len]:=#0;
  1514. p1:=cstringconstnode.createpchar(pc,len);
  1515. end;
  1516. constwstring :
  1517. p1:=cstringconstnode.createwstr(pcompilerwidestring(tconstsym(srsym).value.valueptr));
  1518. constreal :
  1519. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1520. constset :
  1521. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).constdef);
  1522. constpointer :
  1523. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).constdef);
  1524. constnil :
  1525. p1:=cnilnode.create;
  1526. constresourcestring:
  1527. begin
  1528. p1:=cloadnode.create(srsym,srsymtable);
  1529. do_typecheckpass(p1);
  1530. p1.resultdef:=cansistringtype;
  1531. end;
  1532. constguid :
  1533. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1534. else
  1535. internalerror(200507181);
  1536. end;
  1537. end;
  1538. procsym :
  1539. begin
  1540. { check if it's a method/class method }
  1541. if is_member_read(srsym,srsymtable,p1,hdef) then
  1542. begin
  1543. { not srsymtable.symtabletype since that can be }
  1544. { withsymtable as well }
  1545. if (srsym.owner.symtabletype=ObjectSymtable) then
  1546. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1547. else
  1548. { no procsyms in records (yet) }
  1549. internalerror(2007012006);
  1550. end
  1551. else
  1552. { regular procedure/function call }
  1553. do_proc_call(srsym,srsymtable,nil,
  1554. (getaddr and not(token in [_CARET,_POINT])),
  1555. again,p1,[]);
  1556. end;
  1557. propertysym :
  1558. begin
  1559. { property of a class/object? }
  1560. if is_member_read(srsym,srsymtable,p1,hdef) then
  1561. begin
  1562. { not srsymtable.symtabletype since that can be }
  1563. { withsymtable as well }
  1564. if (srsym.owner.symtabletype=ObjectSymtable) then
  1565. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1566. else
  1567. { no propertysyms in records (yet) }
  1568. internalerror(2007012006);
  1569. end
  1570. else
  1571. { no method pointer }
  1572. begin
  1573. p1:=nil;
  1574. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1575. end;
  1576. end;
  1577. labelsym :
  1578. begin
  1579. { Support @label }
  1580. if getaddr then
  1581. begin
  1582. if srsym.owner<>current_procinfo.procdef.localst then
  1583. CGMessage(parser_e_label_outside_proc);
  1584. p1:=cloadnode.create(srsym,srsym.owner)
  1585. end
  1586. else
  1587. begin
  1588. consume(_COLON);
  1589. if tlabelsym(srsym).defined then
  1590. Message(sym_e_label_already_defined);
  1591. tlabelsym(srsym).defined:=true;
  1592. p1:=clabelnode.create(nil);
  1593. tlabelsym(srsym).code:=p1;
  1594. end;
  1595. end;
  1596. errorsym :
  1597. begin
  1598. p1:=cerrornode.create;
  1599. if try_to_consume(_LKLAMMER) then
  1600. begin
  1601. parse_paras(false,false,_RKLAMMER);
  1602. consume(_RKLAMMER);
  1603. end;
  1604. end;
  1605. else
  1606. begin
  1607. p1:=cerrornode.create;
  1608. Message(parser_e_illegal_expression);
  1609. end;
  1610. end; { end case }
  1611. end;
  1612. end;
  1613. {---------------------------------------------
  1614. Factor_Read_Set
  1615. ---------------------------------------------}
  1616. { Read a set between [] }
  1617. function factor_read_set:tnode;
  1618. var
  1619. p1,p2 : tnode;
  1620. lastp,
  1621. buildp : tarrayconstructornode;
  1622. begin
  1623. buildp:=nil;
  1624. { be sure that a least one arrayconstructn is used, also for an
  1625. empty [] }
  1626. if token=_RECKKLAMMER then
  1627. buildp:=carrayconstructornode.create(nil,buildp)
  1628. else
  1629. repeat
  1630. p1:=comp_expr(true);
  1631. if try_to_consume(_POINTPOINT) then
  1632. begin
  1633. p2:=comp_expr(true);
  1634. p1:=carrayconstructorrangenode.create(p1,p2);
  1635. end;
  1636. { insert at the end of the tree, to get the correct order }
  1637. if not assigned(buildp) then
  1638. begin
  1639. buildp:=carrayconstructornode.create(p1,nil);
  1640. lastp:=buildp;
  1641. end
  1642. else
  1643. begin
  1644. lastp.right:=carrayconstructornode.create(p1,nil);
  1645. lastp:=tarrayconstructornode(lastp.right);
  1646. end;
  1647. { there could be more elements }
  1648. until not try_to_consume(_COMMA);
  1649. factor_read_set:=buildp;
  1650. end;
  1651. {---------------------------------------------
  1652. PostFixOperators
  1653. ---------------------------------------------}
  1654. procedure postfixoperators(var p1:tnode;var again:boolean);
  1655. { tries to avoid syntax errors after invalid qualifiers }
  1656. procedure recoverconsume_postfixops;
  1657. begin
  1658. repeat
  1659. if not try_to_consume(_CARET) then
  1660. if try_to_consume(_POINT) then
  1661. try_to_consume(_ID)
  1662. else if try_to_consume(_LECKKLAMMER) then
  1663. begin
  1664. repeat
  1665. comp_expr(true);
  1666. until not try_to_consume(_COMMA);
  1667. consume(_RECKKLAMMER);
  1668. end
  1669. else
  1670. break;
  1671. until false;
  1672. end;
  1673. procedure handle_variantarray;
  1674. var
  1675. p4 : tnode;
  1676. newstatement : tstatementnode;
  1677. tempresultvariant,
  1678. temp : ttempcreatenode;
  1679. paras : tcallparanode;
  1680. newblock : tnode;
  1681. countindices : aint;
  1682. begin
  1683. { create statements with call initialize the arguments and
  1684. call fpc_dynarr_setlength }
  1685. newblock:=internalstatements(newstatement);
  1686. { get temp for array of indicies,
  1687. we set the real size later }
  1688. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1689. addstatement(newstatement,temp);
  1690. countindices:=0;
  1691. repeat
  1692. p4:=comp_expr(true);
  1693. addstatement(newstatement,cassignmentnode.create(
  1694. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1695. inc(countindices);
  1696. until not try_to_consume(_COMMA);
  1697. { set real size }
  1698. temp.size:=countindices*s32inttype.size;
  1699. consume(_RECKKLAMMER);
  1700. { we need only a write access if a := follows }
  1701. if token=_ASSIGNMENT then
  1702. begin
  1703. consume(_ASSIGNMENT);
  1704. p4:=comp_expr(true);
  1705. { create call to fpc_vararray_put }
  1706. paras:=ccallparanode.create(cordconstnode.create
  1707. (countindices,s32inttype,true),
  1708. ccallparanode.create(caddrnode.create_internal
  1709. (ctemprefnode.create(temp)),
  1710. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1711. ccallparanode.create(p1
  1712. ,nil))));
  1713. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1714. addstatement(newstatement,ctempdeletenode.create(temp));
  1715. end
  1716. else
  1717. begin
  1718. { create temp for result }
  1719. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1720. addstatement(newstatement,tempresultvariant);
  1721. { create call to fpc_vararray_get }
  1722. paras:=ccallparanode.create(cordconstnode.create
  1723. (countindices,s32inttype,true),
  1724. ccallparanode.create(caddrnode.create_internal
  1725. (ctemprefnode.create(temp)),
  1726. ccallparanode.create(p1,
  1727. ccallparanode.create(
  1728. ctemprefnode.create(tempresultvariant)
  1729. ,nil))));
  1730. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1731. addstatement(newstatement,ctempdeletenode.create(temp));
  1732. { the last statement should return the value as
  1733. location and type, this is done be referencing the
  1734. temp and converting it first from a persistent temp to
  1735. normal temp }
  1736. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1737. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1738. end;
  1739. p1:=newblock;
  1740. end;
  1741. var
  1742. protsym : tpropertysym;
  1743. p2,p3 : tnode;
  1744. srsym : tsym;
  1745. srsymtable : TSymtable;
  1746. classh : tobjectdef;
  1747. { shouldn't be used that often, so the extra overhead is ok to save
  1748. stack space }
  1749. dispatchstring : ansistring;
  1750. label
  1751. skipreckklammercheck;
  1752. begin
  1753. again:=true;
  1754. while again do
  1755. begin
  1756. { we need the resultdef }
  1757. do_typecheckpass(p1);
  1758. if codegenerror then
  1759. begin
  1760. recoverconsume_postfixops;
  1761. exit;
  1762. end;
  1763. { handle token }
  1764. case token of
  1765. _CARET:
  1766. begin
  1767. consume(_CARET);
  1768. { support tp/mac procvar^ if the procvar returns a
  1769. pointer type }
  1770. if ((m_tp_procvar in current_settings.modeswitches) or
  1771. (m_mac_procvar in current_settings.modeswitches)) and
  1772. (p1.resultdef.typ=procvardef) and
  1773. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1774. begin
  1775. p1:=ccallnode.create_procvar(nil,p1);
  1776. typecheckpass(p1);
  1777. end;
  1778. if (p1.resultdef.typ<>pointerdef) then
  1779. begin
  1780. { ^ as binary operator is a problem!!!! (FK) }
  1781. again:=false;
  1782. Message(parser_e_invalid_qualifier);
  1783. recoverconsume_postfixops;
  1784. p1.destroy;
  1785. p1:=cerrornode.create;
  1786. end
  1787. else
  1788. p1:=cderefnode.create(p1);
  1789. end;
  1790. _LECKKLAMMER:
  1791. begin
  1792. if is_class_or_interface(p1.resultdef) then
  1793. begin
  1794. { default property }
  1795. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1796. if not(assigned(protsym)) then
  1797. begin
  1798. p1.destroy;
  1799. p1:=cerrornode.create;
  1800. again:=false;
  1801. message(parser_e_no_default_property_available);
  1802. end
  1803. else
  1804. begin
  1805. { The property symbol is referenced indirect }
  1806. protsym.IncRefCount;
  1807. handle_propertysym(protsym,protsym.owner,p1);
  1808. end;
  1809. end
  1810. else
  1811. begin
  1812. consume(_LECKKLAMMER);
  1813. repeat
  1814. case p1.resultdef.typ of
  1815. pointerdef:
  1816. begin
  1817. { support delphi autoderef }
  1818. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1819. (m_autoderef in current_settings.modeswitches) then
  1820. p1:=cderefnode.create(p1);
  1821. p2:=comp_expr(true);
  1822. { Support Pbytevar[0..9] which returns array [0..9].}
  1823. if try_to_consume(_POINTPOINT) then
  1824. p2:=crangenode.create(p2,comp_expr(true));
  1825. p1:=cvecnode.create(p1,p2);
  1826. end;
  1827. variantdef:
  1828. begin
  1829. handle_variantarray;
  1830. { the RECKKLAMMER is already read }
  1831. goto skipreckklammercheck;
  1832. end;
  1833. stringdef :
  1834. begin
  1835. p2:=comp_expr(true);
  1836. { Support string[0..9] which returns array [0..9] of char.}
  1837. if try_to_consume(_POINTPOINT) then
  1838. p2:=crangenode.create(p2,comp_expr(true));
  1839. p1:=cvecnode.create(p1,p2);
  1840. end;
  1841. arraydef:
  1842. begin
  1843. p2:=comp_expr(true);
  1844. { support SEG:OFS for go32v2 Mem[] }
  1845. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1846. (p1.nodetype=loadn) and
  1847. assigned(tloadnode(p1).symtableentry) and
  1848. assigned(tloadnode(p1).symtableentry.owner.name) and
  1849. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1850. ((tloadnode(p1).symtableentry.name='MEM') or
  1851. (tloadnode(p1).symtableentry.name='MEMW') or
  1852. (tloadnode(p1).symtableentry.name='MEML')) then
  1853. begin
  1854. if try_to_consume(_COLON) then
  1855. begin
  1856. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1857. p2:=comp_expr(true);
  1858. p2:=caddnode.create(addn,p2,p3);
  1859. if try_to_consume(_POINTPOINT) then
  1860. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1861. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true),p3.getcopy));
  1862. p1:=cvecnode.create(p1,p2);
  1863. include(tvecnode(p1).flags,nf_memseg);
  1864. include(tvecnode(p1).flags,nf_memindex);
  1865. end
  1866. else
  1867. begin
  1868. if try_to_consume(_POINTPOINT) then
  1869. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1870. p2:=crangenode.create(p2,comp_expr(true));
  1871. p1:=cvecnode.create(p1,p2);
  1872. include(tvecnode(p1).flags,nf_memindex);
  1873. end;
  1874. end
  1875. else
  1876. begin
  1877. if try_to_consume(_POINTPOINT) then
  1878. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1879. p2:=crangenode.create(p2,comp_expr(true));
  1880. p1:=cvecnode.create(p1,p2);
  1881. end;
  1882. end;
  1883. else
  1884. begin
  1885. Message(parser_e_invalid_qualifier);
  1886. p1.destroy;
  1887. p1:=cerrornode.create;
  1888. comp_expr(true);
  1889. again:=false;
  1890. end;
  1891. end;
  1892. do_typecheckpass(p1);
  1893. until not try_to_consume(_COMMA);
  1894. consume(_RECKKLAMMER);
  1895. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1896. skipreckklammercheck:
  1897. end;
  1898. end;
  1899. _POINT :
  1900. begin
  1901. consume(_POINT);
  1902. if (p1.resultdef.typ=pointerdef) and
  1903. (m_autoderef in current_settings.modeswitches) then
  1904. begin
  1905. p1:=cderefnode.create(p1);
  1906. do_typecheckpass(p1);
  1907. end;
  1908. case p1.resultdef.typ of
  1909. recorddef:
  1910. begin
  1911. if token=_ID then
  1912. begin
  1913. srsym:=tsym(trecorddef(p1.resultdef).symtable.Find(pattern));
  1914. if assigned(srsym) and
  1915. (srsym.typ=fieldvarsym) then
  1916. begin
  1917. check_hints(srsym,srsym.symoptions);
  1918. p1:=csubscriptnode.create(srsym,p1)
  1919. end
  1920. else
  1921. begin
  1922. Message1(sym_e_illegal_field,pattern);
  1923. p1.destroy;
  1924. p1:=cerrornode.create;
  1925. end;
  1926. end;
  1927. consume(_ID);
  1928. end;
  1929. variantdef:
  1930. begin
  1931. { dispatch call? }
  1932. if token=_ID then
  1933. begin
  1934. dispatchstring:=orgpattern;
  1935. consume(_ID);
  1936. if try_to_consume(_LKLAMMER) then
  1937. begin
  1938. p2:=parse_paras(false,true,_RKLAMMER);
  1939. consume(_RKLAMMER);
  1940. end
  1941. else
  1942. p2:=nil;
  1943. p1:=translate_disp_call(p1,p2,dispatchstring);
  1944. end
  1945. else { Error }
  1946. Consume(_ID);
  1947. end;
  1948. classrefdef:
  1949. begin
  1950. if token=_ID then
  1951. begin
  1952. classh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1953. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1954. if assigned(srsym) then
  1955. begin
  1956. check_hints(srsym,srsym.symoptions);
  1957. consume(_ID);
  1958. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1959. end
  1960. else
  1961. begin
  1962. Message1(sym_e_id_no_member,orgpattern);
  1963. p1.destroy;
  1964. p1:=cerrornode.create;
  1965. { try to clean up }
  1966. consume(_ID);
  1967. end;
  1968. end
  1969. else { Error }
  1970. Consume(_ID);
  1971. end;
  1972. objectdef:
  1973. begin
  1974. if token=_ID then
  1975. begin
  1976. classh:=tobjectdef(p1.resultdef);
  1977. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1978. if assigned(srsym) then
  1979. begin
  1980. check_hints(srsym,srsym.symoptions);
  1981. consume(_ID);
  1982. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1983. end
  1984. else
  1985. begin
  1986. Message1(sym_e_id_no_member,orgpattern);
  1987. p1.destroy;
  1988. p1:=cerrornode.create;
  1989. { try to clean up }
  1990. consume(_ID);
  1991. end;
  1992. end
  1993. else { Error }
  1994. Consume(_ID);
  1995. end;
  1996. pointerdef:
  1997. begin
  1998. Message(parser_e_invalid_qualifier);
  1999. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2000. Message(parser_h_maybe_deref_caret_missing);
  2001. end;
  2002. else
  2003. begin
  2004. Message(parser_e_invalid_qualifier);
  2005. p1.destroy;
  2006. p1:=cerrornode.create;
  2007. { Error }
  2008. consume(_ID);
  2009. end;
  2010. end;
  2011. end;
  2012. else
  2013. begin
  2014. { is this a procedure variable ? }
  2015. if assigned(p1.resultdef) and
  2016. (p1.resultdef.typ=procvardef) then
  2017. begin
  2018. if assigned(getprocvardef) and
  2019. equal_defs(p1.resultdef,getprocvardef) then
  2020. begin
  2021. { classes can define now types so we've to allow
  2022. type casts with these nested types as well }
  2023. if (p1.nodetype=typen) and
  2024. try_to_consume(_LKLAMMER) then
  2025. begin
  2026. p1:=comp_expr(true);
  2027. consume(_RKLAMMER);
  2028. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2029. end
  2030. else
  2031. again:=false
  2032. end
  2033. else
  2034. begin
  2035. if try_to_consume(_LKLAMMER) then
  2036. begin
  2037. p2:=parse_paras(false,false,_RKLAMMER);
  2038. consume(_RKLAMMER);
  2039. p1:=ccallnode.create_procvar(p2,p1);
  2040. { proc():= is never possible }
  2041. if token=_ASSIGNMENT then
  2042. begin
  2043. Message(parser_e_illegal_expression);
  2044. p1.free;
  2045. p1:=cerrornode.create;
  2046. again:=false;
  2047. end;
  2048. end
  2049. else
  2050. again:=false;
  2051. end;
  2052. end
  2053. else
  2054. again:=false;
  2055. end;
  2056. end;
  2057. end; { while again }
  2058. end;
  2059. {---------------------------------------------
  2060. Factor (Main)
  2061. ---------------------------------------------}
  2062. var
  2063. l : longint;
  2064. ic : int64;
  2065. qc : qword;
  2066. {$ifndef cpu64}
  2067. card : cardinal;
  2068. {$endif cpu64}
  2069. oldp1,
  2070. p1 : tnode;
  2071. code : integer;
  2072. again : boolean;
  2073. srsym : tsym;
  2074. srsymtable : TSymtable;
  2075. pd : tprocdef;
  2076. hclassdef : tobjectdef;
  2077. d : bestreal;
  2078. cur : currency;
  2079. hs,hsorg : string;
  2080. hdef : tdef;
  2081. filepos : tfileposinfo;
  2082. begin
  2083. oldp1:=nil;
  2084. p1:=nil;
  2085. filepos:=current_tokenpos;
  2086. again:=false;
  2087. if token=_ID then
  2088. begin
  2089. again:=true;
  2090. { Handle references to self }
  2091. if (idtoken=_SELF) and
  2092. not(block_type in [bt_const,bt_type]) and
  2093. assigned(current_procinfo) and
  2094. assigned(current_procinfo.procdef._class) then
  2095. begin
  2096. p1:=load_self_node;
  2097. consume(_ID);
  2098. again:=true;
  2099. end
  2100. else
  2101. factor_read_id(p1,again);
  2102. if again then
  2103. begin
  2104. if (p1<>oldp1) then
  2105. begin
  2106. if assigned(p1) then
  2107. p1.fileinfo:=filepos;
  2108. oldp1:=p1;
  2109. filepos:=current_tokenpos;
  2110. end;
  2111. { handle post fix operators }
  2112. postfixoperators(p1,again);
  2113. end;
  2114. end
  2115. else
  2116. case token of
  2117. _RETURN :
  2118. begin
  2119. consume(_RETURN);
  2120. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2121. p1 := cexitnode.create(comp_expr(true))
  2122. else
  2123. p1 := cexitnode.create(nil);
  2124. end;
  2125. _INHERITED :
  2126. begin
  2127. again:=true;
  2128. consume(_INHERITED);
  2129. if assigned(current_procinfo) and
  2130. assigned(current_procinfo.procdef._class) then
  2131. begin
  2132. hclassdef:=current_procinfo.procdef._class.childof;
  2133. { if inherited; only then we need the method with
  2134. the same name }
  2135. if token in endtokens then
  2136. begin
  2137. hs:=current_procinfo.procdef.procsym.name;
  2138. hsorg:=current_procinfo.procdef.procsym.realname;
  2139. anon_inherited:=true;
  2140. { For message methods we need to search using the message
  2141. number or string }
  2142. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2143. srdef:=nil;
  2144. if (po_msgint in pd.procoptions) then
  2145. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2146. else
  2147. if (po_msgstr in pd.procoptions) then
  2148. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2149. else
  2150. searchsym_in_class(hclassdef,current_procinfo.procdef._class,hs,srsym,srsymtable);
  2151. end
  2152. else
  2153. begin
  2154. hs:=pattern;
  2155. hsorg:=orgpattern;
  2156. consume(_ID);
  2157. anon_inherited:=false;
  2158. searchsym_in_class(hclassdef,current_procinfo.procdef._class,hs,srsym,srsymtable);
  2159. end;
  2160. if assigned(srsym) then
  2161. begin
  2162. check_hints(srsym,srsym.symoptions);
  2163. { load the procdef from the inherited class and
  2164. not from self }
  2165. if srsym.typ in [procsym,propertysym] then
  2166. begin
  2167. if (srsym.typ = procsym) then
  2168. begin
  2169. hdef:=hclassdef;
  2170. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2171. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2172. hdef:=tclassrefdef.create(hdef);
  2173. p1:=ctypenode.create(hdef);
  2174. end;
  2175. end
  2176. else
  2177. begin
  2178. Message(parser_e_methode_id_expected);
  2179. p1:=cerrornode.create;
  2180. end;
  2181. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2182. end
  2183. else
  2184. begin
  2185. if anon_inherited then
  2186. begin
  2187. { For message methods we need to call DefaultHandler }
  2188. if (po_msgint in pd.procoptions) or
  2189. (po_msgstr in pd.procoptions) then
  2190. begin
  2191. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2192. if not assigned(srsym) or
  2193. (srsym.typ<>procsym) then
  2194. internalerror(200303171);
  2195. p1:=nil;
  2196. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2197. end
  2198. else
  2199. begin
  2200. { we need to ignore the inherited; }
  2201. p1:=cnothingnode.create;
  2202. end;
  2203. end
  2204. else
  2205. begin
  2206. Message1(sym_e_id_no_member,hsorg);
  2207. p1:=cerrornode.create;
  2208. end;
  2209. again:=false;
  2210. end;
  2211. { turn auto inheriting off }
  2212. anon_inherited:=false;
  2213. end
  2214. else
  2215. begin
  2216. Message(parser_e_generic_methods_only_in_methods);
  2217. again:=false;
  2218. p1:=cerrornode.create;
  2219. end;
  2220. postfixoperators(p1,again);
  2221. end;
  2222. _INTCONST :
  2223. begin
  2224. {Try first wether the value fits in an int64.}
  2225. val(pattern,ic,code);
  2226. if code=0 then
  2227. begin
  2228. consume(_INTCONST);
  2229. int_to_type(ic,hdef);
  2230. p1:=cordconstnode.create(ic,hdef,true);
  2231. end
  2232. else
  2233. begin
  2234. { try qword next }
  2235. val(pattern,qc,code);
  2236. if code=0 then
  2237. begin
  2238. consume(_INTCONST);
  2239. int_to_type(qc,hdef);
  2240. p1:=cordconstnode.create(qc,hdef,true);
  2241. end;
  2242. end;
  2243. if code<>0 then
  2244. begin
  2245. { finally float }
  2246. val(pattern,d,code);
  2247. if code<>0 then
  2248. begin
  2249. Message(parser_e_invalid_integer);
  2250. consume(_INTCONST);
  2251. l:=1;
  2252. p1:=cordconstnode.create(l,sinttype,true);
  2253. end
  2254. else
  2255. begin
  2256. consume(_INTCONST);
  2257. p1:=crealconstnode.create(d,pbestrealtype^);
  2258. end;
  2259. end
  2260. else
  2261. { the necessary range checking has already been done by val }
  2262. tordconstnode(p1).rangecheck:=false;
  2263. end;
  2264. _REALNUMBER :
  2265. begin
  2266. val(pattern,d,code);
  2267. if code<>0 then
  2268. begin
  2269. Message(parser_e_error_in_real);
  2270. d:=1.0;
  2271. end;
  2272. consume(_REALNUMBER);
  2273. {$ifdef FPC_REAL2REAL_FIXED}
  2274. if (d = single(d)) then
  2275. p1:=crealconstnode.create(d,s32floattype)
  2276. else if (d = double(d)) then
  2277. p1:=crealconstnode.create(d,s64floattype)
  2278. else
  2279. {$endif FPC_REAL2REAL_FIXED}
  2280. p1:=crealconstnode.create(d,pbestrealtype^);
  2281. {$ifdef FPC_HAS_STR_CURRENCY}
  2282. val(pattern,cur,code);
  2283. if code=0 then
  2284. trealconstnode(p1).value_currency:=cur;
  2285. {$endif FPC_HAS_STR_CURRENCY}
  2286. end;
  2287. _STRING :
  2288. begin
  2289. string_dec(hdef);
  2290. { STRING can be also a type cast }
  2291. if try_to_consume(_LKLAMMER) then
  2292. begin
  2293. p1:=comp_expr(true);
  2294. consume(_RKLAMMER);
  2295. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2296. { handle postfix operators here e.g. string(a)[10] }
  2297. again:=true;
  2298. postfixoperators(p1,again);
  2299. end
  2300. else
  2301. p1:=ctypenode.create(hdef);
  2302. end;
  2303. _FILE :
  2304. begin
  2305. hdef:=cfiletype;
  2306. consume(_FILE);
  2307. { FILE can be also a type cast }
  2308. if try_to_consume(_LKLAMMER) then
  2309. begin
  2310. p1:=comp_expr(true);
  2311. consume(_RKLAMMER);
  2312. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2313. { handle postfix operators here e.g. string(a)[10] }
  2314. again:=true;
  2315. postfixoperators(p1,again);
  2316. end
  2317. else
  2318. begin
  2319. p1:=ctypenode.create(hdef);
  2320. end;
  2321. end;
  2322. _CSTRING :
  2323. begin
  2324. p1:=cstringconstnode.createstr(pattern);
  2325. consume(_CSTRING);
  2326. end;
  2327. _CCHAR :
  2328. begin
  2329. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2330. consume(_CCHAR);
  2331. end;
  2332. _CWSTRING:
  2333. begin
  2334. p1:=cstringconstnode.createwstr(patternw);
  2335. consume(_CWSTRING);
  2336. end;
  2337. _CWCHAR:
  2338. begin
  2339. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2340. consume(_CWCHAR);
  2341. end;
  2342. _KLAMMERAFFE :
  2343. begin
  2344. consume(_KLAMMERAFFE);
  2345. got_addrn:=true;
  2346. { support both @<x> and @(<x>) }
  2347. if try_to_consume(_LKLAMMER) then
  2348. begin
  2349. p1:=factor(true);
  2350. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2351. begin
  2352. again:=true;
  2353. postfixoperators(p1,again);
  2354. end;
  2355. consume(_RKLAMMER);
  2356. end
  2357. else
  2358. p1:=factor(true);
  2359. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2360. begin
  2361. again:=true;
  2362. postfixoperators(p1,again);
  2363. end;
  2364. got_addrn:=false;
  2365. p1:=caddrnode.create(p1);
  2366. if cs_typed_addresses in current_settings.localswitches then
  2367. include(p1.flags,nf_typedaddr);
  2368. { Store the procvar that we are expecting, the
  2369. addrn will use the information to find the correct
  2370. procdef or it will return an error }
  2371. if assigned(getprocvardef) and
  2372. (taddrnode(p1).left.nodetype = loadn) then
  2373. taddrnode(p1).getprocvardef:=getprocvardef;
  2374. end;
  2375. _LKLAMMER :
  2376. begin
  2377. consume(_LKLAMMER);
  2378. p1:=comp_expr(true);
  2379. consume(_RKLAMMER);
  2380. { it's not a good solution }
  2381. { but (a+b)^ makes some problems }
  2382. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2383. begin
  2384. again:=true;
  2385. postfixoperators(p1,again);
  2386. end;
  2387. end;
  2388. _LECKKLAMMER :
  2389. begin
  2390. consume(_LECKKLAMMER);
  2391. p1:=factor_read_set;
  2392. consume(_RECKKLAMMER);
  2393. end;
  2394. _PLUS :
  2395. begin
  2396. consume(_PLUS);
  2397. p1:=factor(false);
  2398. { we must generate a new node to do 0+<p1> otherwise the + will
  2399. not be checked }
  2400. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2401. end;
  2402. _MINUS :
  2403. begin
  2404. consume(_MINUS);
  2405. if (token = _INTCONST) then
  2406. begin
  2407. { ugly hack, but necessary to be able to parse }
  2408. { -9223372036854775808 as int64 (JM) }
  2409. pattern := '-'+pattern;
  2410. p1:=sub_expr(oppower,false);
  2411. { -1 ** 4 should be - (1 ** 4) and not
  2412. (-1) ** 4
  2413. This was the reason of tw0869.pp test failure PM }
  2414. if p1.nodetype=starstarn then
  2415. begin
  2416. if tbinarynode(p1).left.nodetype=ordconstn then
  2417. begin
  2418. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2419. p1:=cunaryminusnode.create(p1);
  2420. end
  2421. else if tbinarynode(p1).left.nodetype=realconstn then
  2422. begin
  2423. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2424. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2425. p1:=cunaryminusnode.create(p1);
  2426. end
  2427. else
  2428. internalerror(20021029);
  2429. end;
  2430. end
  2431. else
  2432. begin
  2433. p1:=sub_expr(oppower,false);
  2434. p1:=cunaryminusnode.create(p1);
  2435. end;
  2436. end;
  2437. _OP_NOT :
  2438. begin
  2439. consume(_OP_NOT);
  2440. p1:=factor(false);
  2441. p1:=cnotnode.create(p1);
  2442. end;
  2443. _TRUE :
  2444. begin
  2445. consume(_TRUE);
  2446. p1:=cordconstnode.create(1,booltype,false);
  2447. end;
  2448. _FALSE :
  2449. begin
  2450. consume(_FALSE);
  2451. p1:=cordconstnode.create(0,booltype,false);
  2452. end;
  2453. _NIL :
  2454. begin
  2455. consume(_NIL);
  2456. p1:=cnilnode.create;
  2457. { It's really ugly code nil^, but delphi allows it }
  2458. if token in [_CARET] then
  2459. begin
  2460. again:=true;
  2461. postfixoperators(p1,again);
  2462. end;
  2463. end;
  2464. else
  2465. begin
  2466. Message(parser_e_illegal_expression);
  2467. p1:=cerrornode.create;
  2468. { recover }
  2469. consume(token);
  2470. end;
  2471. end;
  2472. { generate error node if no node is created }
  2473. if not assigned(p1) then
  2474. begin
  2475. {$ifdef EXTDEBUG}
  2476. Comment(V_Warning,'factor: p1=nil');
  2477. {$endif}
  2478. p1:=cerrornode.create;
  2479. end;
  2480. { get the resultdef for the node }
  2481. if (not assigned(p1.resultdef)) then
  2482. do_typecheckpass(p1);
  2483. if assigned(p1) and
  2484. (p1<>oldp1) then
  2485. p1.fileinfo:=filepos;
  2486. factor:=p1;
  2487. end;
  2488. {$maxfpuregisters default}
  2489. {****************************************************************************
  2490. Sub_Expr
  2491. ****************************************************************************}
  2492. const
  2493. { Warning these stay be ordered !! }
  2494. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2495. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2496. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2497. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2498. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2499. [_STARSTAR] );
  2500. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2501. {Reads a subexpression while the operators are of the current precedence
  2502. level, or any higher level. Replaces the old term, simpl_expr and
  2503. simpl2_expr.}
  2504. var
  2505. p1,p2 : tnode;
  2506. oldt : Ttoken;
  2507. filepos : tfileposinfo;
  2508. begin
  2509. if pred_level=highest_precedence then
  2510. p1:=factor(false)
  2511. else
  2512. p1:=sub_expr(succ(pred_level),true);
  2513. repeat
  2514. if (token in operator_levels[pred_level]) and
  2515. ((token<>_EQUAL) or accept_equal) then
  2516. begin
  2517. oldt:=token;
  2518. filepos:=current_tokenpos;
  2519. consume(token);
  2520. if pred_level=highest_precedence then
  2521. p2:=factor(false)
  2522. else
  2523. p2:=sub_expr(succ(pred_level),true);
  2524. case oldt of
  2525. _PLUS :
  2526. p1:=caddnode.create(addn,p1,p2);
  2527. _MINUS :
  2528. p1:=caddnode.create(subn,p1,p2);
  2529. _STAR :
  2530. p1:=caddnode.create(muln,p1,p2);
  2531. _SLASH :
  2532. p1:=caddnode.create(slashn,p1,p2);
  2533. _EQUAL :
  2534. p1:=caddnode.create(equaln,p1,p2);
  2535. _GT :
  2536. p1:=caddnode.create(gtn,p1,p2);
  2537. _LT :
  2538. p1:=caddnode.create(ltn,p1,p2);
  2539. _GTE :
  2540. p1:=caddnode.create(gten,p1,p2);
  2541. _LTE :
  2542. p1:=caddnode.create(lten,p1,p2);
  2543. _SYMDIF :
  2544. p1:=caddnode.create(symdifn,p1,p2);
  2545. _STARSTAR :
  2546. p1:=caddnode.create(starstarn,p1,p2);
  2547. _OP_AS :
  2548. p1:=casnode.create(p1,p2);
  2549. _OP_IN :
  2550. p1:=cinnode.create(p1,p2);
  2551. _OP_IS :
  2552. p1:=cisnode.create(p1,p2);
  2553. _OP_OR,
  2554. _PIPE {macpas only} :
  2555. begin
  2556. p1:=caddnode.create(orn,p1,p2);
  2557. if (oldt = _PIPE) then
  2558. include(p1.flags,nf_short_bool);
  2559. end;
  2560. _OP_AND,
  2561. _AMPERSAND {macpas only} :
  2562. begin
  2563. p1:=caddnode.create(andn,p1,p2);
  2564. if (oldt = _AMPERSAND) then
  2565. include(p1.flags,nf_short_bool);
  2566. end;
  2567. _OP_DIV :
  2568. p1:=cmoddivnode.create(divn,p1,p2);
  2569. _OP_NOT :
  2570. p1:=cnotnode.create(p1);
  2571. _OP_MOD :
  2572. p1:=cmoddivnode.create(modn,p1,p2);
  2573. _OP_SHL :
  2574. p1:=cshlshrnode.create(shln,p1,p2);
  2575. _OP_SHR :
  2576. p1:=cshlshrnode.create(shrn,p1,p2);
  2577. _OP_XOR :
  2578. p1:=caddnode.create(xorn,p1,p2);
  2579. _ASSIGNMENT :
  2580. p1:=cassignmentnode.create(p1,p2);
  2581. _UNEQUAL :
  2582. p1:=caddnode.create(unequaln,p1,p2);
  2583. end;
  2584. p1.fileinfo:=filepos;
  2585. end
  2586. else
  2587. break;
  2588. until false;
  2589. sub_expr:=p1;
  2590. end;
  2591. function comp_expr(accept_equal : boolean):tnode;
  2592. var
  2593. oldafterassignment : boolean;
  2594. p1 : tnode;
  2595. begin
  2596. oldafterassignment:=afterassignment;
  2597. afterassignment:=true;
  2598. p1:=sub_expr(opcompare,accept_equal);
  2599. { get the resultdef for this expression }
  2600. if not assigned(p1.resultdef) then
  2601. do_typecheckpass(p1);
  2602. afterassignment:=oldafterassignment;
  2603. comp_expr:=p1;
  2604. end;
  2605. function expr : tnode;
  2606. var
  2607. p1,p2 : tnode;
  2608. oldafterassignment : boolean;
  2609. oldp1 : tnode;
  2610. filepos : tfileposinfo;
  2611. begin
  2612. oldafterassignment:=afterassignment;
  2613. p1:=sub_expr(opcompare,true);
  2614. { get the resultdef for this expression }
  2615. if not assigned(p1.resultdef) then
  2616. do_typecheckpass(p1);
  2617. filepos:=current_tokenpos;
  2618. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2619. afterassignment:=true;
  2620. oldp1:=p1;
  2621. case token of
  2622. _POINTPOINT :
  2623. begin
  2624. consume(_POINTPOINT);
  2625. p2:=sub_expr(opcompare,true);
  2626. p1:=crangenode.create(p1,p2);
  2627. end;
  2628. _ASSIGNMENT :
  2629. begin
  2630. consume(_ASSIGNMENT);
  2631. if (p1.resultdef.typ=procvardef) then
  2632. getprocvardef:=tprocvardef(p1.resultdef);
  2633. p2:=sub_expr(opcompare,true);
  2634. if assigned(getprocvardef) then
  2635. handle_procvar(getprocvardef,p2);
  2636. getprocvardef:=nil;
  2637. p1:=cassignmentnode.create(p1,p2);
  2638. end;
  2639. _PLUSASN :
  2640. begin
  2641. consume(_PLUSASN);
  2642. p2:=sub_expr(opcompare,true);
  2643. p1:=gen_c_style_operator(addn,p1,p2);
  2644. end;
  2645. _MINUSASN :
  2646. begin
  2647. consume(_MINUSASN);
  2648. p2:=sub_expr(opcompare,true);
  2649. p1:=gen_c_style_operator(subn,p1,p2);
  2650. end;
  2651. _STARASN :
  2652. begin
  2653. consume(_STARASN );
  2654. p2:=sub_expr(opcompare,true);
  2655. p1:=gen_c_style_operator(muln,p1,p2);
  2656. end;
  2657. _SLASHASN :
  2658. begin
  2659. consume(_SLASHASN );
  2660. p2:=sub_expr(opcompare,true);
  2661. p1:=gen_c_style_operator(slashn,p1,p2);
  2662. end;
  2663. end;
  2664. { get the resultdef for this expression }
  2665. if not assigned(p1.resultdef) then
  2666. do_typecheckpass(p1);
  2667. afterassignment:=oldafterassignment;
  2668. if p1<>oldp1 then
  2669. p1.fileinfo:=filepos;
  2670. expr:=p1;
  2671. end;
  2672. function get_intconst:TConstExprInt;
  2673. {Reads an expression, tries to evalute it and check if it is an integer
  2674. constant. Then the constant is returned.}
  2675. var
  2676. p:tnode;
  2677. begin
  2678. result:=0;
  2679. p:=comp_expr(true);
  2680. if not codegenerror then
  2681. begin
  2682. if (p.nodetype<>ordconstn) or
  2683. not(is_integer(p.resultdef)) then
  2684. Message(parser_e_illegal_expression)
  2685. else
  2686. result:=tordconstnode(p).value;
  2687. end;
  2688. p.free;
  2689. end;
  2690. function get_stringconst:string;
  2691. {Reads an expression, tries to evaluate it and checks if it is a string
  2692. constant. Then the constant is returned.}
  2693. var
  2694. p:tnode;
  2695. begin
  2696. get_stringconst:='';
  2697. p:=comp_expr(true);
  2698. if p.nodetype<>stringconstn then
  2699. begin
  2700. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2701. get_stringconst:=char(int64(tordconstnode(p).value))
  2702. else
  2703. Message(parser_e_illegal_expression);
  2704. end
  2705. else
  2706. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2707. p.free;
  2708. end;
  2709. end.