pexpr.pas 115 KB

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