pexpr.pas 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981
  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. tlabelsym(srsym).defined:=true;
  1573. p1:=clabelnode.create(nil,tlabelsym(srsym));
  1574. tlabelsym(srsym).code:=p1;
  1575. end;
  1576. end;
  1577. errorsym :
  1578. begin
  1579. p1:=cerrornode.create;
  1580. if try_to_consume(_LKLAMMER) then
  1581. begin
  1582. parse_paras(false,false,_RKLAMMER);
  1583. consume(_RKLAMMER);
  1584. end;
  1585. end;
  1586. else
  1587. begin
  1588. p1:=cerrornode.create;
  1589. Message(parser_e_illegal_expression);
  1590. end;
  1591. end; { end case }
  1592. end;
  1593. end;
  1594. {---------------------------------------------
  1595. Factor_Read_Set
  1596. ---------------------------------------------}
  1597. { Read a set between [] }
  1598. function factor_read_set:tnode;
  1599. var
  1600. p1,p2 : tnode;
  1601. lastp,
  1602. buildp : tarrayconstructornode;
  1603. begin
  1604. buildp:=nil;
  1605. { be sure that a least one arrayconstructn is used, also for an
  1606. empty [] }
  1607. if token=_RECKKLAMMER then
  1608. buildp:=carrayconstructornode.create(nil,buildp)
  1609. else
  1610. repeat
  1611. p1:=comp_expr(true);
  1612. if try_to_consume(_POINTPOINT) then
  1613. begin
  1614. p2:=comp_expr(true);
  1615. p1:=carrayconstructorrangenode.create(p1,p2);
  1616. end;
  1617. { insert at the end of the tree, to get the correct order }
  1618. if not assigned(buildp) then
  1619. begin
  1620. buildp:=carrayconstructornode.create(p1,nil);
  1621. lastp:=buildp;
  1622. end
  1623. else
  1624. begin
  1625. lastp.right:=carrayconstructornode.create(p1,nil);
  1626. lastp:=tarrayconstructornode(lastp.right);
  1627. end;
  1628. { there could be more elements }
  1629. until not try_to_consume(_COMMA);
  1630. factor_read_set:=buildp;
  1631. end;
  1632. {---------------------------------------------
  1633. PostFixOperators
  1634. ---------------------------------------------}
  1635. { returns whether or not p1 has been changed }
  1636. function postfixoperators(var p1:tnode;var again:boolean): boolean;
  1637. { tries to avoid syntax errors after invalid qualifiers }
  1638. procedure recoverconsume_postfixops;
  1639. begin
  1640. repeat
  1641. if not try_to_consume(_CARET) then
  1642. if try_to_consume(_POINT) then
  1643. try_to_consume(_ID)
  1644. else if try_to_consume(_LECKKLAMMER) then
  1645. begin
  1646. repeat
  1647. comp_expr(true);
  1648. until not try_to_consume(_COMMA);
  1649. consume(_RECKKLAMMER);
  1650. end
  1651. else if try_to_consume(_LKLAMMER) then
  1652. begin
  1653. repeat
  1654. comp_expr(true);
  1655. until not try_to_consume(_COMMA);
  1656. consume(_RKLAMMER);
  1657. end
  1658. else
  1659. break;
  1660. until false;
  1661. end;
  1662. procedure handle_variantarray;
  1663. var
  1664. p4 : tnode;
  1665. newstatement : tstatementnode;
  1666. tempresultvariant,
  1667. temp : ttempcreatenode;
  1668. paras : tcallparanode;
  1669. newblock : tnode;
  1670. countindices : aint;
  1671. begin
  1672. { create statements with call initialize the arguments and
  1673. call fpc_dynarr_setlength }
  1674. newblock:=internalstatements(newstatement);
  1675. { get temp for array of indicies,
  1676. we set the real size later }
  1677. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1678. addstatement(newstatement,temp);
  1679. countindices:=0;
  1680. repeat
  1681. p4:=comp_expr(true);
  1682. addstatement(newstatement,cassignmentnode.create(
  1683. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1684. inc(countindices);
  1685. until not try_to_consume(_COMMA);
  1686. { set real size }
  1687. temp.size:=countindices*s32inttype.size;
  1688. consume(_RECKKLAMMER);
  1689. { we need only a write access if a := follows }
  1690. if token=_ASSIGNMENT then
  1691. begin
  1692. consume(_ASSIGNMENT);
  1693. p4:=comp_expr(true);
  1694. { create call to fpc_vararray_put }
  1695. paras:=ccallparanode.create(cordconstnode.create
  1696. (countindices,s32inttype,true),
  1697. ccallparanode.create(caddrnode.create_internal
  1698. (ctemprefnode.create(temp)),
  1699. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1700. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1701. ,nil))));
  1702. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1703. addstatement(newstatement,ctempdeletenode.create(temp));
  1704. end
  1705. else
  1706. begin
  1707. { create temp for result }
  1708. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1709. addstatement(newstatement,tempresultvariant);
  1710. { create call to fpc_vararray_get }
  1711. paras:=ccallparanode.create(cordconstnode.create
  1712. (countindices,s32inttype,true),
  1713. ccallparanode.create(caddrnode.create_internal
  1714. (ctemprefnode.create(temp)),
  1715. ccallparanode.create(p1,
  1716. ccallparanode.create(
  1717. ctemprefnode.create(tempresultvariant)
  1718. ,nil))));
  1719. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1720. addstatement(newstatement,ctempdeletenode.create(temp));
  1721. { the last statement should return the value as
  1722. location and type, this is done be referencing the
  1723. temp and converting it first from a persistent temp to
  1724. normal temp }
  1725. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1726. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1727. end;
  1728. p1:=newblock;
  1729. end;
  1730. var
  1731. protsym : tpropertysym;
  1732. p2,p3 : tnode;
  1733. srsym : tsym;
  1734. srsymtable : TSymtable;
  1735. classh : tobjectdef;
  1736. { shouldn't be used that often, so the extra overhead is ok to save
  1737. stack space }
  1738. dispatchstring : ansistring;
  1739. nodechanged : boolean;
  1740. label
  1741. skipreckklammercheck;
  1742. begin
  1743. result:=false;
  1744. again:=true;
  1745. while again do
  1746. begin
  1747. { we need the resultdef }
  1748. do_typecheckpass_changed(p1,nodechanged);
  1749. result:=result or nodechanged;
  1750. if codegenerror then
  1751. begin
  1752. recoverconsume_postfixops;
  1753. exit;
  1754. end;
  1755. { handle token }
  1756. case token of
  1757. _CARET:
  1758. begin
  1759. consume(_CARET);
  1760. { support tp/mac procvar^ if the procvar returns a
  1761. pointer type }
  1762. if ((m_tp_procvar in current_settings.modeswitches) or
  1763. (m_mac_procvar in current_settings.modeswitches)) and
  1764. (p1.resultdef.typ=procvardef) and
  1765. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1766. begin
  1767. p1:=ccallnode.create_procvar(nil,p1);
  1768. typecheckpass(p1);
  1769. end;
  1770. if (p1.resultdef.typ<>pointerdef) then
  1771. begin
  1772. { ^ as binary operator is a problem!!!! (FK) }
  1773. again:=false;
  1774. Message(parser_e_invalid_qualifier);
  1775. recoverconsume_postfixops;
  1776. p1.destroy;
  1777. p1:=cerrornode.create;
  1778. end
  1779. else
  1780. p1:=cderefnode.create(p1);
  1781. end;
  1782. _LECKKLAMMER:
  1783. begin
  1784. if is_class_or_interface_or_object(p1.resultdef) then
  1785. begin
  1786. { default property }
  1787. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1788. if not(assigned(protsym)) then
  1789. begin
  1790. p1.destroy;
  1791. p1:=cerrornode.create;
  1792. again:=false;
  1793. message(parser_e_no_default_property_available);
  1794. end
  1795. else
  1796. begin
  1797. { The property symbol is referenced indirect }
  1798. protsym.IncRefCount;
  1799. handle_propertysym(protsym,protsym.owner,p1);
  1800. end;
  1801. end
  1802. else
  1803. begin
  1804. consume(_LECKKLAMMER);
  1805. repeat
  1806. { in all of the cases below, p1 is changed }
  1807. case p1.resultdef.typ of
  1808. pointerdef:
  1809. begin
  1810. { support delphi autoderef }
  1811. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1812. (m_autoderef in current_settings.modeswitches) then
  1813. p1:=cderefnode.create(p1);
  1814. p2:=comp_expr(true);
  1815. { Support Pbytevar[0..9] which returns array [0..9].}
  1816. if try_to_consume(_POINTPOINT) then
  1817. p2:=crangenode.create(p2,comp_expr(true));
  1818. p1:=cvecnode.create(p1,p2);
  1819. end;
  1820. variantdef:
  1821. begin
  1822. handle_variantarray;
  1823. { the RECKKLAMMER is already read }
  1824. goto skipreckklammercheck;
  1825. end;
  1826. stringdef :
  1827. begin
  1828. p2:=comp_expr(true);
  1829. { Support string[0..9] which returns array [0..9] of char.}
  1830. if try_to_consume(_POINTPOINT) then
  1831. p2:=crangenode.create(p2,comp_expr(true));
  1832. p1:=cvecnode.create(p1,p2);
  1833. end;
  1834. arraydef:
  1835. begin
  1836. p2:=comp_expr(true);
  1837. { support SEG:OFS for go32v2 Mem[] }
  1838. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1839. (p1.nodetype=loadn) and
  1840. assigned(tloadnode(p1).symtableentry) and
  1841. assigned(tloadnode(p1).symtableentry.owner.name) and
  1842. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1843. ((tloadnode(p1).symtableentry.name='MEM') or
  1844. (tloadnode(p1).symtableentry.name='MEMW') or
  1845. (tloadnode(p1).symtableentry.name='MEML')) then
  1846. begin
  1847. if try_to_consume(_COLON) then
  1848. begin
  1849. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1850. p2:=comp_expr(true);
  1851. p2:=caddnode.create(addn,p2,p3);
  1852. if try_to_consume(_POINTPOINT) then
  1853. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1854. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true),p3.getcopy));
  1855. p1:=cvecnode.create(p1,p2);
  1856. include(tvecnode(p1).flags,nf_memseg);
  1857. include(tvecnode(p1).flags,nf_memindex);
  1858. end
  1859. else
  1860. begin
  1861. if try_to_consume(_POINTPOINT) then
  1862. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1863. p2:=crangenode.create(p2,comp_expr(true));
  1864. p1:=cvecnode.create(p1,p2);
  1865. include(tvecnode(p1).flags,nf_memindex);
  1866. end;
  1867. end
  1868. else
  1869. begin
  1870. if try_to_consume(_POINTPOINT) then
  1871. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1872. p2:=crangenode.create(p2,comp_expr(true));
  1873. p1:=cvecnode.create(p1,p2);
  1874. end;
  1875. end;
  1876. else
  1877. begin
  1878. if p1.resultdef.typ<>undefineddef then
  1879. Message(parser_e_invalid_qualifier);
  1880. p1.destroy;
  1881. p1:=cerrornode.create;
  1882. comp_expr(true);
  1883. again:=false;
  1884. end;
  1885. end;
  1886. do_typecheckpass(p1);
  1887. until not try_to_consume(_COMMA);
  1888. consume(_RECKKLAMMER);
  1889. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1890. skipreckklammercheck:
  1891. end;
  1892. end;
  1893. _POINT :
  1894. begin
  1895. consume(_POINT);
  1896. if (p1.resultdef.typ=pointerdef) and
  1897. (m_autoderef in current_settings.modeswitches) and
  1898. { don't auto-deref objc.id, because then the code
  1899. below for supporting id.anyobjcmethod isn't triggered }
  1900. (p1.resultdef<>objc_idtype) then
  1901. begin
  1902. p1:=cderefnode.create(p1);
  1903. do_typecheckpass(p1);
  1904. end;
  1905. { procvar.<something> can never mean anything so always
  1906. try to call it in case it returns a record/object/... }
  1907. maybe_call_procvar(p1,false);
  1908. case p1.resultdef.typ of
  1909. recorddef:
  1910. begin
  1911. if token=_ID then
  1912. begin
  1913. srsym:=tsym(trecorddef(p1.resultdef).symtable.Find(pattern));
  1914. if assigned(srsym) and
  1915. (srsym.typ=fieldvarsym) then
  1916. begin
  1917. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1918. p1:=csubscriptnode.create(srsym,p1)
  1919. end
  1920. else
  1921. begin
  1922. Message1(sym_e_illegal_field,pattern);
  1923. p1.destroy;
  1924. p1:=cerrornode.create;
  1925. end;
  1926. end;
  1927. consume(_ID);
  1928. end;
  1929. enumdef:
  1930. begin
  1931. if token=_ID then
  1932. begin
  1933. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  1934. p1.destroy;
  1935. if assigned(srsym) and (srsym.typ=enumsym) then
  1936. begin
  1937. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1938. p1:=genenumnode(tenumsym(srsym));
  1939. end
  1940. else
  1941. begin
  1942. Message1(sym_e_id_no_member,orgpattern);
  1943. p1:=cerrornode.create;
  1944. end;
  1945. end;
  1946. consume(_ID);
  1947. end;
  1948. variantdef:
  1949. begin
  1950. { dispatch call? }
  1951. if token=_ID then
  1952. begin
  1953. dispatchstring:=orgpattern;
  1954. consume(_ID);
  1955. if try_to_consume(_LKLAMMER) then
  1956. begin
  1957. p2:=parse_paras(false,true,_RKLAMMER);
  1958. consume(_RKLAMMER);
  1959. end
  1960. else
  1961. p2:=nil;
  1962. { property setter? }
  1963. if (token=_ASSIGNMENT) and not(afterassignment) then
  1964. begin
  1965. consume(_ASSIGNMENT);
  1966. { read the expression }
  1967. p3:=comp_expr(true);
  1968. { concat value parameter too }
  1969. p2:=ccallparanode.create(p3,p2);
  1970. { passing p3 here is only for information purposes }
  1971. p1:=translate_disp_call(p1,p2,p3,dispatchstring,0,voidtype);
  1972. end
  1973. else
  1974. { this is only an approximation
  1975. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1976. if afterassignment or in_args or (token<>_SEMICOLON) then
  1977. p1:=translate_disp_call(p1,p2,nil,dispatchstring,0,cvarianttype)
  1978. else
  1979. p1:=translate_disp_call(p1,p2,nil,dispatchstring,0,voidtype);
  1980. end
  1981. else { Error }
  1982. Consume(_ID);
  1983. end;
  1984. classrefdef:
  1985. begin
  1986. if token=_ID then
  1987. begin
  1988. classh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1989. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1990. if assigned(srsym) then
  1991. begin
  1992. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1993. consume(_ID);
  1994. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1995. end
  1996. else
  1997. begin
  1998. Message1(sym_e_id_no_member,orgpattern);
  1999. p1.destroy;
  2000. p1:=cerrornode.create;
  2001. { try to clean up }
  2002. consume(_ID);
  2003. end;
  2004. end
  2005. else { Error }
  2006. Consume(_ID);
  2007. end;
  2008. objectdef:
  2009. begin
  2010. if token=_ID then
  2011. begin
  2012. classh:=tobjectdef(p1.resultdef);
  2013. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  2014. if assigned(srsym) then
  2015. begin
  2016. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2017. consume(_ID);
  2018. do_member_read(classh,getaddr,srsym,p1,again,[]);
  2019. end
  2020. else
  2021. begin
  2022. Message1(sym_e_id_no_member,orgpattern);
  2023. p1.destroy;
  2024. p1:=cerrornode.create;
  2025. { try to clean up }
  2026. consume(_ID);
  2027. end;
  2028. end
  2029. else { Error }
  2030. Consume(_ID);
  2031. end;
  2032. pointerdef:
  2033. begin
  2034. if (p1.resultdef=objc_idtype) then
  2035. begin
  2036. { objc's id type can be used to call any
  2037. Objective-C method of any Objective-C class
  2038. type that's currently in scope }
  2039. if search_objc_method(pattern,srsym,srsymtable) then
  2040. begin
  2041. consume(_ID);
  2042. do_proc_call(srsym,srsymtable,nil,
  2043. (getaddr and not(token in [_CARET,_POINT])),
  2044. again,p1,[cnf_objc_id_call]);
  2045. { we need to know which procedure is called }
  2046. do_typecheckpass(p1);
  2047. end
  2048. else
  2049. begin
  2050. consume(_ID);
  2051. Message(parser_e_methode_id_expected);
  2052. end;
  2053. end
  2054. else
  2055. begin
  2056. Message(parser_e_invalid_qualifier);
  2057. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2058. Message(parser_h_maybe_deref_caret_missing);
  2059. end
  2060. end;
  2061. else
  2062. begin
  2063. if p1.resultdef.typ<>undefineddef then
  2064. Message(parser_e_invalid_qualifier);
  2065. p1.destroy;
  2066. p1:=cerrornode.create;
  2067. { Error }
  2068. consume(_ID);
  2069. end;
  2070. end;
  2071. end;
  2072. else
  2073. begin
  2074. { is this a procedure variable ? }
  2075. if assigned(p1.resultdef) and
  2076. (p1.resultdef.typ=procvardef) then
  2077. begin
  2078. { Typenode for typecasting or expecting a procvar }
  2079. if (p1.nodetype=typen) or
  2080. (
  2081. assigned(getprocvardef) and
  2082. equal_defs(p1.resultdef,getprocvardef)
  2083. ) then
  2084. begin
  2085. if try_to_consume(_LKLAMMER) then
  2086. begin
  2087. p1:=comp_expr(true);
  2088. consume(_RKLAMMER);
  2089. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2090. end
  2091. else
  2092. again:=false
  2093. end
  2094. else
  2095. begin
  2096. if try_to_consume(_LKLAMMER) then
  2097. begin
  2098. p2:=parse_paras(false,false,_RKLAMMER);
  2099. consume(_RKLAMMER);
  2100. p1:=ccallnode.create_procvar(p2,p1);
  2101. { proc():= is never possible }
  2102. if token=_ASSIGNMENT then
  2103. begin
  2104. Message(parser_e_illegal_expression);
  2105. p1.free;
  2106. p1:=cerrornode.create;
  2107. again:=false;
  2108. end;
  2109. end
  2110. else
  2111. again:=false;
  2112. end;
  2113. end
  2114. else
  2115. again:=false;
  2116. end;
  2117. end;
  2118. { we only try again if p1 was changed }
  2119. if again or
  2120. (p1.nodetype=errorn) then
  2121. result:=true;
  2122. end; { while again }
  2123. end;
  2124. {---------------------------------------------
  2125. Factor (Main)
  2126. ---------------------------------------------}
  2127. var
  2128. l : longint;
  2129. ic : int64;
  2130. qc : qword;
  2131. p1 : tnode;
  2132. code : integer;
  2133. srsym : tsym;
  2134. srsymtable : TSymtable;
  2135. pd : tprocdef;
  2136. hclassdef : tobjectdef;
  2137. d : bestreal;
  2138. cur : currency;
  2139. hs,hsorg : string;
  2140. hdef : tdef;
  2141. filepos : tfileposinfo;
  2142. again,
  2143. updatefpos,
  2144. nodechanged : boolean;
  2145. begin
  2146. { can't keep a copy of p1 and compare pointers afterwards, because
  2147. p1 may be freed and reallocated in the same place! }
  2148. updatefpos:=false;
  2149. p1:=nil;
  2150. filepos:=current_tokenpos;
  2151. again:=false;
  2152. if token=_ID then
  2153. begin
  2154. again:=true;
  2155. { Handle references to self }
  2156. if (idtoken=_SELF) and
  2157. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2158. assigned(current_objectdef) then
  2159. begin
  2160. p1:=load_self_node;
  2161. consume(_ID);
  2162. again:=true;
  2163. end
  2164. else
  2165. factor_read_id(p1,again);
  2166. if assigned(p1) then
  2167. begin
  2168. { factor_read_id will set the filepos to after the id,
  2169. and in case of _SELF the filepos will already be the
  2170. same as filepos (so setting it again doesn't hurt). }
  2171. p1.fileinfo:=filepos;
  2172. filepos:=current_tokenpos;
  2173. end;
  2174. { handle post fix operators }
  2175. updatefpos:=postfixoperators(p1,again);
  2176. end
  2177. else
  2178. begin
  2179. updatefpos:=true;
  2180. case token of
  2181. _RETURN :
  2182. begin
  2183. consume(_RETURN);
  2184. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2185. p1 := cexitnode.create(comp_expr(true))
  2186. else
  2187. p1 := cexitnode.create(nil);
  2188. end;
  2189. _INHERITED :
  2190. begin
  2191. again:=true;
  2192. consume(_INHERITED);
  2193. if assigned(current_procinfo) and
  2194. assigned(current_objectdef) then
  2195. begin
  2196. hclassdef:=current_objectdef.childof;
  2197. { Objective-C categories *replace* methods in the class
  2198. they extend, or add methods to it. So calling an
  2199. inherited method always calls the method inherited from
  2200. the parent of the extended class }
  2201. if is_objccategory(current_objectdef) then
  2202. hclassdef:=hclassdef.childof;
  2203. { if inherited; only then we need the method with
  2204. the same name }
  2205. if token in endtokens then
  2206. begin
  2207. hs:=current_procinfo.procdef.procsym.name;
  2208. hsorg:=current_procinfo.procdef.procsym.realname;
  2209. anon_inherited:=true;
  2210. { For message methods we need to search using the message
  2211. number or string }
  2212. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2213. srdef:=nil;
  2214. if (po_msgint in pd.procoptions) then
  2215. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2216. else
  2217. if (po_msgstr in pd.procoptions) then
  2218. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2219. else
  2220. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2221. end
  2222. else
  2223. begin
  2224. hs:=pattern;
  2225. hsorg:=orgpattern;
  2226. consume(_ID);
  2227. anon_inherited:=false;
  2228. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2229. end;
  2230. if assigned(srsym) then
  2231. begin
  2232. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2233. { load the procdef from the inherited class and
  2234. not from self }
  2235. case srsym.typ of
  2236. procsym:
  2237. begin
  2238. hdef:=hclassdef;
  2239. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2240. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2241. hdef:=tclassrefdef.create(hdef);
  2242. p1:=ctypenode.create(hdef);
  2243. end;
  2244. propertysym:
  2245. ;
  2246. else
  2247. begin
  2248. Message(parser_e_methode_id_expected);
  2249. p1:=cerrornode.create;
  2250. end;
  2251. end;
  2252. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2253. end
  2254. else
  2255. begin
  2256. if anon_inherited then
  2257. begin
  2258. { For message methods we need to call DefaultHandler }
  2259. if (po_msgint in pd.procoptions) or
  2260. (po_msgstr in pd.procoptions) then
  2261. begin
  2262. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2263. if not assigned(srsym) or
  2264. (srsym.typ<>procsym) then
  2265. internalerror(200303171);
  2266. p1:=nil;
  2267. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2268. end
  2269. else
  2270. begin
  2271. { we need to ignore the inherited; }
  2272. p1:=cnothingnode.create;
  2273. end;
  2274. end
  2275. else
  2276. begin
  2277. Message1(sym_e_id_no_member,hsorg);
  2278. p1:=cerrornode.create;
  2279. end;
  2280. again:=false;
  2281. end;
  2282. { turn auto inheriting off }
  2283. anon_inherited:=false;
  2284. end
  2285. else
  2286. begin
  2287. Message(parser_e_generic_methods_only_in_methods);
  2288. again:=false;
  2289. p1:=cerrornode.create;
  2290. end;
  2291. postfixoperators(p1,again);
  2292. end;
  2293. _INTCONST :
  2294. begin
  2295. {Try first wether the value fits in an int64.}
  2296. val(pattern,ic,code);
  2297. if code=0 then
  2298. begin
  2299. consume(_INTCONST);
  2300. int_to_type(ic,hdef);
  2301. p1:=cordconstnode.create(ic,hdef,true);
  2302. end
  2303. else
  2304. begin
  2305. { try qword next }
  2306. val(pattern,qc,code);
  2307. if code=0 then
  2308. begin
  2309. consume(_INTCONST);
  2310. int_to_type(qc,hdef);
  2311. p1:=cordconstnode.create(qc,hdef,true);
  2312. end;
  2313. end;
  2314. if code<>0 then
  2315. begin
  2316. { finally float }
  2317. val(pattern,d,code);
  2318. if code<>0 then
  2319. begin
  2320. Message(parser_e_invalid_integer);
  2321. consume(_INTCONST);
  2322. l:=1;
  2323. p1:=cordconstnode.create(l,sinttype,true);
  2324. end
  2325. else
  2326. begin
  2327. consume(_INTCONST);
  2328. p1:=crealconstnode.create(d,pbestrealtype^);
  2329. end;
  2330. end
  2331. else
  2332. { the necessary range checking has already been done by val }
  2333. tordconstnode(p1).rangecheck:=false;
  2334. end;
  2335. _REALNUMBER :
  2336. begin
  2337. val(pattern,d,code);
  2338. if code<>0 then
  2339. begin
  2340. Message(parser_e_error_in_real);
  2341. d:=1.0;
  2342. end;
  2343. consume(_REALNUMBER);
  2344. {$ifdef FPC_REAL2REAL_FIXED}
  2345. if current_settings.fputype=fpu_none then
  2346. Message(parser_e_unsupported_real);
  2347. if (current_settings.minfpconstprec=s32real) and
  2348. (d = single(d)) then
  2349. p1:=crealconstnode.create(d,s32floattype)
  2350. else if (current_settings.minfpconstprec=s64real) and
  2351. (d = double(d)) then
  2352. p1:=crealconstnode.create(d,s64floattype)
  2353. else
  2354. {$endif FPC_REAL2REAL_FIXED}
  2355. p1:=crealconstnode.create(d,pbestrealtype^);
  2356. {$ifdef FPC_HAS_STR_CURRENCY}
  2357. val(pattern,cur,code);
  2358. if code=0 then
  2359. trealconstnode(p1).value_currency:=cur;
  2360. {$endif FPC_HAS_STR_CURRENCY}
  2361. end;
  2362. _STRING :
  2363. begin
  2364. string_dec(hdef,true);
  2365. { STRING can be also a type cast }
  2366. if try_to_consume(_LKLAMMER) then
  2367. begin
  2368. p1:=comp_expr(true);
  2369. consume(_RKLAMMER);
  2370. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2371. { handle postfix operators here e.g. string(a)[10] }
  2372. again:=true;
  2373. postfixoperators(p1,again);
  2374. end
  2375. else
  2376. p1:=ctypenode.create(hdef);
  2377. end;
  2378. _FILE :
  2379. begin
  2380. hdef:=cfiletype;
  2381. consume(_FILE);
  2382. { FILE can be also a type cast }
  2383. if try_to_consume(_LKLAMMER) then
  2384. begin
  2385. p1:=comp_expr(true);
  2386. consume(_RKLAMMER);
  2387. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2388. { handle postfix operators here e.g. string(a)[10] }
  2389. again:=true;
  2390. postfixoperators(p1,again);
  2391. end
  2392. else
  2393. begin
  2394. p1:=ctypenode.create(hdef);
  2395. end;
  2396. end;
  2397. _CSTRING :
  2398. begin
  2399. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2400. consume(_CSTRING);
  2401. end;
  2402. _CCHAR :
  2403. begin
  2404. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2405. consume(_CCHAR);
  2406. end;
  2407. _CWSTRING:
  2408. begin
  2409. p1:=cstringconstnode.createwstr(patternw);
  2410. consume(_CWSTRING);
  2411. end;
  2412. _CWCHAR:
  2413. begin
  2414. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2415. consume(_CWCHAR);
  2416. end;
  2417. _KLAMMERAFFE :
  2418. begin
  2419. consume(_KLAMMERAFFE);
  2420. got_addrn:=true;
  2421. { support both @<x> and @(<x>) }
  2422. if try_to_consume(_LKLAMMER) then
  2423. begin
  2424. p1:=factor(true);
  2425. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2426. begin
  2427. again:=true;
  2428. postfixoperators(p1,again);
  2429. end
  2430. else
  2431. consume(_RKLAMMER);
  2432. end
  2433. else
  2434. p1:=factor(true);
  2435. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2436. begin
  2437. again:=true;
  2438. postfixoperators(p1,again);
  2439. end;
  2440. got_addrn:=false;
  2441. p1:=caddrnode.create(p1);
  2442. p1.fileinfo:=filepos;
  2443. if cs_typed_addresses in current_settings.localswitches then
  2444. include(p1.flags,nf_typedaddr);
  2445. { Store the procvar that we are expecting, the
  2446. addrn will use the information to find the correct
  2447. procdef or it will return an error }
  2448. if assigned(getprocvardef) and
  2449. (taddrnode(p1).left.nodetype = loadn) then
  2450. taddrnode(p1).getprocvardef:=getprocvardef;
  2451. end;
  2452. _LKLAMMER :
  2453. begin
  2454. consume(_LKLAMMER);
  2455. p1:=comp_expr(true);
  2456. consume(_RKLAMMER);
  2457. { it's not a good solution }
  2458. { but (a+b)^ makes some problems }
  2459. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2460. begin
  2461. again:=true;
  2462. postfixoperators(p1,again);
  2463. end;
  2464. end;
  2465. _LECKKLAMMER :
  2466. begin
  2467. consume(_LECKKLAMMER);
  2468. p1:=factor_read_set;
  2469. consume(_RECKKLAMMER);
  2470. end;
  2471. _PLUS :
  2472. begin
  2473. consume(_PLUS);
  2474. p1:=factor(false);
  2475. { we must generate a new node to do 0+<p1> otherwise the + will
  2476. not be checked }
  2477. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2478. end;
  2479. _MINUS :
  2480. begin
  2481. consume(_MINUS);
  2482. if (token = _INTCONST) then
  2483. begin
  2484. { ugly hack, but necessary to be able to parse }
  2485. { -9223372036854775808 as int64 (JM) }
  2486. pattern := '-'+pattern;
  2487. p1:=sub_expr(oppower,false);
  2488. { -1 ** 4 should be - (1 ** 4) and not
  2489. (-1) ** 4
  2490. This was the reason of tw0869.pp test failure PM }
  2491. if p1.nodetype=starstarn then
  2492. begin
  2493. if tbinarynode(p1).left.nodetype=ordconstn then
  2494. begin
  2495. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2496. p1:=cunaryminusnode.create(p1);
  2497. end
  2498. else if tbinarynode(p1).left.nodetype=realconstn then
  2499. begin
  2500. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2501. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2502. p1:=cunaryminusnode.create(p1);
  2503. end
  2504. else
  2505. internalerror(20021029);
  2506. end;
  2507. end
  2508. else
  2509. begin
  2510. p1:=sub_expr(oppower,false);
  2511. p1:=cunaryminusnode.create(p1);
  2512. end;
  2513. end;
  2514. _OP_NOT :
  2515. begin
  2516. consume(_OP_NOT);
  2517. p1:=factor(false);
  2518. p1:=cnotnode.create(p1);
  2519. end;
  2520. _TRUE :
  2521. begin
  2522. consume(_TRUE);
  2523. p1:=cordconstnode.create(1,booltype,false);
  2524. end;
  2525. _FALSE :
  2526. begin
  2527. consume(_FALSE);
  2528. p1:=cordconstnode.create(0,booltype,false);
  2529. end;
  2530. _NIL :
  2531. begin
  2532. consume(_NIL);
  2533. p1:=cnilnode.create;
  2534. { It's really ugly code nil^, but delphi allows it }
  2535. if token in [_CARET] then
  2536. begin
  2537. again:=true;
  2538. postfixoperators(p1,again);
  2539. end;
  2540. end;
  2541. _OBJCPROTOCOL:
  2542. begin
  2543. { The @protocol keyword is used in two ways in Objective-C:
  2544. 1) to declare protocols (~ Object Pascal interfaces)
  2545. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2546. of a declared protocol
  2547. This code is for handling the second case. Because of 1),
  2548. we cannot simply use a system unit symbol.
  2549. }
  2550. consume(_OBJCPROTOCOL);
  2551. consume(_LKLAMMER);
  2552. p1:=factor(false);
  2553. consume(_RKLAMMER);
  2554. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2555. end;
  2556. else
  2557. begin
  2558. Message(parser_e_illegal_expression);
  2559. p1:=cerrornode.create;
  2560. { recover }
  2561. consume(token);
  2562. end;
  2563. end;
  2564. end;
  2565. { generate error node if no node is created }
  2566. if not assigned(p1) then
  2567. begin
  2568. {$ifdef EXTDEBUG}
  2569. Comment(V_Warning,'factor: p1=nil');
  2570. {$endif}
  2571. p1:=cerrornode.create;
  2572. updatefpos:=true;
  2573. end;
  2574. { get the resultdef for the node }
  2575. if (not assigned(p1.resultdef)) then
  2576. begin
  2577. do_typecheckpass_changed(p1,nodechanged);
  2578. updatefpos:=updatefpos or nodechanged;
  2579. end;
  2580. if assigned(p1) and
  2581. updatefpos then
  2582. p1.fileinfo:=filepos;
  2583. factor:=p1;
  2584. end;
  2585. {$maxfpuregisters default}
  2586. {****************************************************************************
  2587. Sub_Expr
  2588. ****************************************************************************}
  2589. const
  2590. { Warning these stay be ordered !! }
  2591. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2592. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2593. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2594. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2595. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2596. [_STARSTAR] );
  2597. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2598. {Reads a subexpression while the operators are of the current precedence
  2599. level, or any higher level. Replaces the old term, simpl_expr and
  2600. simpl2_expr.}
  2601. var
  2602. p1,p2 : tnode;
  2603. oldt : Ttoken;
  2604. filepos : tfileposinfo;
  2605. begin
  2606. if pred_level=highest_precedence then
  2607. p1:=factor(false)
  2608. else
  2609. p1:=sub_expr(succ(pred_level),true);
  2610. repeat
  2611. if (token in operator_levels[pred_level]) and
  2612. ((token<>_EQUAL) or accept_equal) then
  2613. begin
  2614. oldt:=token;
  2615. filepos:=current_tokenpos;
  2616. consume(token);
  2617. if pred_level=highest_precedence then
  2618. p2:=factor(false)
  2619. else
  2620. p2:=sub_expr(succ(pred_level),true);
  2621. case oldt of
  2622. _PLUS :
  2623. p1:=caddnode.create(addn,p1,p2);
  2624. _MINUS :
  2625. p1:=caddnode.create(subn,p1,p2);
  2626. _STAR :
  2627. p1:=caddnode.create(muln,p1,p2);
  2628. _SLASH :
  2629. p1:=caddnode.create(slashn,p1,p2);
  2630. _EQUAL :
  2631. p1:=caddnode.create(equaln,p1,p2);
  2632. _GT :
  2633. p1:=caddnode.create(gtn,p1,p2);
  2634. _LT :
  2635. p1:=caddnode.create(ltn,p1,p2);
  2636. _GTE :
  2637. p1:=caddnode.create(gten,p1,p2);
  2638. _LTE :
  2639. p1:=caddnode.create(lten,p1,p2);
  2640. _SYMDIF :
  2641. p1:=caddnode.create(symdifn,p1,p2);
  2642. _STARSTAR :
  2643. p1:=caddnode.create(starstarn,p1,p2);
  2644. _OP_AS :
  2645. p1:=casnode.create(p1,p2);
  2646. _OP_IN :
  2647. p1:=cinnode.create(p1,p2);
  2648. _OP_IS :
  2649. p1:=cisnode.create(p1,p2);
  2650. _OP_OR,
  2651. _PIPE {macpas only} :
  2652. begin
  2653. p1:=caddnode.create(orn,p1,p2);
  2654. if (oldt = _PIPE) then
  2655. include(p1.flags,nf_short_bool);
  2656. end;
  2657. _OP_AND,
  2658. _AMPERSAND {macpas only} :
  2659. begin
  2660. p1:=caddnode.create(andn,p1,p2);
  2661. if (oldt = _AMPERSAND) then
  2662. include(p1.flags,nf_short_bool);
  2663. end;
  2664. _OP_DIV :
  2665. p1:=cmoddivnode.create(divn,p1,p2);
  2666. _OP_NOT :
  2667. p1:=cnotnode.create(p1);
  2668. _OP_MOD :
  2669. p1:=cmoddivnode.create(modn,p1,p2);
  2670. _OP_SHL :
  2671. p1:=cshlshrnode.create(shln,p1,p2);
  2672. _OP_SHR :
  2673. p1:=cshlshrnode.create(shrn,p1,p2);
  2674. _OP_XOR :
  2675. p1:=caddnode.create(xorn,p1,p2);
  2676. _ASSIGNMENT :
  2677. p1:=cassignmentnode.create(p1,p2);
  2678. _UNEQUAL :
  2679. p1:=caddnode.create(unequaln,p1,p2);
  2680. end;
  2681. p1.fileinfo:=filepos;
  2682. end
  2683. else
  2684. break;
  2685. until false;
  2686. sub_expr:=p1;
  2687. end;
  2688. function comp_expr(accept_equal : boolean):tnode;
  2689. var
  2690. oldafterassignment : boolean;
  2691. p1 : tnode;
  2692. begin
  2693. oldafterassignment:=afterassignment;
  2694. afterassignment:=true;
  2695. p1:=sub_expr(opcompare,accept_equal);
  2696. { get the resultdef for this expression }
  2697. if not assigned(p1.resultdef) then
  2698. do_typecheckpass(p1);
  2699. afterassignment:=oldafterassignment;
  2700. comp_expr:=p1;
  2701. end;
  2702. function expr(dotypecheck : boolean) : tnode;
  2703. var
  2704. p1,p2 : tnode;
  2705. filepos : tfileposinfo;
  2706. oldafterassignment,
  2707. updatefpos : boolean;
  2708. begin
  2709. oldafterassignment:=afterassignment;
  2710. p1:=sub_expr(opcompare,true);
  2711. { get the resultdef for this expression }
  2712. if not assigned(p1.resultdef) and
  2713. dotypecheck then
  2714. do_typecheckpass(p1);
  2715. filepos:=current_tokenpos;
  2716. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2717. afterassignment:=true;
  2718. updatefpos:=true;
  2719. case token of
  2720. _POINTPOINT :
  2721. begin
  2722. consume(_POINTPOINT);
  2723. p2:=sub_expr(opcompare,true);
  2724. p1:=crangenode.create(p1,p2);
  2725. end;
  2726. _ASSIGNMENT :
  2727. begin
  2728. consume(_ASSIGNMENT);
  2729. if (p1.resultdef.typ=procvardef) then
  2730. getprocvardef:=tprocvardef(p1.resultdef);
  2731. p2:=sub_expr(opcompare,true);
  2732. if assigned(getprocvardef) then
  2733. handle_procvar(getprocvardef,p2);
  2734. getprocvardef:=nil;
  2735. p1:=cassignmentnode.create(p1,p2);
  2736. end;
  2737. _PLUSASN :
  2738. begin
  2739. consume(_PLUSASN);
  2740. p2:=sub_expr(opcompare,true);
  2741. p1:=gen_c_style_operator(addn,p1,p2);
  2742. end;
  2743. _MINUSASN :
  2744. begin
  2745. consume(_MINUSASN);
  2746. p2:=sub_expr(opcompare,true);
  2747. p1:=gen_c_style_operator(subn,p1,p2);
  2748. end;
  2749. _STARASN :
  2750. begin
  2751. consume(_STARASN );
  2752. p2:=sub_expr(opcompare,true);
  2753. p1:=gen_c_style_operator(muln,p1,p2);
  2754. end;
  2755. _SLASHASN :
  2756. begin
  2757. consume(_SLASHASN );
  2758. p2:=sub_expr(opcompare,true);
  2759. p1:=gen_c_style_operator(slashn,p1,p2);
  2760. end;
  2761. else
  2762. updatefpos:=false;
  2763. end;
  2764. { get the resultdef for this expression }
  2765. if not assigned(p1.resultdef) and
  2766. dotypecheck then
  2767. do_typecheckpass(p1);
  2768. afterassignment:=oldafterassignment;
  2769. if updatefpos then
  2770. p1.fileinfo:=filepos;
  2771. expr:=p1;
  2772. end;
  2773. function get_intconst:TConstExprInt;
  2774. {Reads an expression, tries to evalute it and check if it is an integer
  2775. constant. Then the constant is returned.}
  2776. var
  2777. p:tnode;
  2778. begin
  2779. result:=0;
  2780. p:=comp_expr(true);
  2781. if not codegenerror then
  2782. begin
  2783. if (p.nodetype<>ordconstn) or
  2784. not(is_integer(p.resultdef)) then
  2785. Message(parser_e_illegal_expression)
  2786. else
  2787. result:=tordconstnode(p).value;
  2788. end;
  2789. p.free;
  2790. end;
  2791. function get_stringconst:string;
  2792. {Reads an expression, tries to evaluate it and checks if it is a string
  2793. constant. Then the constant is returned.}
  2794. var
  2795. p:tnode;
  2796. begin
  2797. get_stringconst:='';
  2798. p:=comp_expr(true);
  2799. if p.nodetype<>stringconstn then
  2800. begin
  2801. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2802. get_stringconst:=char(int64(tordconstnode(p).value))
  2803. else
  2804. Message(parser_e_illegal_expression);
  2805. end
  2806. else
  2807. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2808. p.free;
  2809. end;
  2810. end.