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