pexpr.pas 118 KB

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