pexpr.pas 116 KB

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