pexpr.pas 110 KB

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