pexpr.pas 117 KB

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