pexpr.pas 119 KB

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