pexpr.pas 120 KB

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