pexpr.pas 116 KB

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