pexpr.pas 118 KB

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