pexpr.pas 120 KB

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