pexpr.pas 113 KB

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