pexpr.pas 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  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_dispinterface_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. statements : tstatementnode;
  959. converted_result_data : ttempcreatenode;
  960. begin
  961. { property parameters? read them only if the property really }
  962. { has parameters }
  963. paras:=nil;
  964. if (ppo_hasparameters in propsym.propoptions) then
  965. begin
  966. if try_to_consume(_LECKKLAMMER) then
  967. begin
  968. paras:=parse_paras(false,false,_RECKKLAMMER);
  969. consume(_RECKKLAMMER);
  970. end;
  971. end;
  972. { indexed property }
  973. if (ppo_indexed in propsym.propoptions) then
  974. begin
  975. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  976. paras:=ccallparanode.create(p2,paras);
  977. end;
  978. { we need only a write property if a := follows }
  979. { if not(afterassignment) and not(in_args) then }
  980. if token=_ASSIGNMENT then
  981. begin
  982. if getpropaccesslist(propsym,palt_write,propaccesslist) then
  983. begin
  984. sym:=propaccesslist.firstsym^.sym;
  985. case sym.typ of
  986. procsym :
  987. begin
  988. callflags:=[];
  989. { generate the method call }
  990. membercall:=maybe_load_methodpointer(st,p1);
  991. if membercall then
  992. include(callflags,cnf_member_call);
  993. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  994. addsymref(sym);
  995. paras:=nil;
  996. consume(_ASSIGNMENT);
  997. { read the expression }
  998. if propsym.propdef.typ=procvardef then
  999. getprocvardef:=tprocvardef(propsym.propdef);
  1000. p2:=comp_expr(true);
  1001. if assigned(getprocvardef) then
  1002. handle_procvar(getprocvardef,p2);
  1003. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1004. { mark as property, both the tcallnode and the real call block }
  1005. include(p1.flags,nf_isproperty);
  1006. getprocvardef:=nil;
  1007. end;
  1008. fieldvarsym :
  1009. begin
  1010. { generate access code }
  1011. if (sp_static in sym.symoptions) then
  1012. begin
  1013. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1014. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1015. if assigned(sym) then
  1016. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1017. p1.free;
  1018. p1:=nil;
  1019. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1020. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1021. end
  1022. else
  1023. propaccesslist_to_node(p1,st,propaccesslist);
  1024. include(p1.flags,nf_isproperty);
  1025. consume(_ASSIGNMENT);
  1026. { read the expression }
  1027. p2:=comp_expr(true);
  1028. p1:=cassignmentnode.create(p1,p2);
  1029. end
  1030. else
  1031. begin
  1032. p1:=cerrornode.create;
  1033. Message(parser_e_no_procedure_to_access_property);
  1034. end;
  1035. end;
  1036. end
  1037. else
  1038. if (ppo_dispid_write in propsym.propoptions) then
  1039. begin
  1040. consume(_ASSIGNMENT);
  1041. p2:=comp_expr(true);
  1042. { concat value parameter too }
  1043. p2:=ccallparanode.create(p2,nil);
  1044. { passing p3 here is only for information purposes }
  1045. p1:=translate_disp_call(p1,p2,p2,'',propsym.dispid,voidtype);
  1046. end
  1047. else
  1048. begin
  1049. p1:=cerrornode.create;
  1050. Message(parser_e_no_procedure_to_access_property);
  1051. end;
  1052. end
  1053. else
  1054. begin
  1055. if getpropaccesslist(propsym,palt_read,propaccesslist) then
  1056. begin
  1057. sym := propaccesslist.firstsym^.sym;
  1058. case sym.typ of
  1059. fieldvarsym :
  1060. begin
  1061. { generate access code }
  1062. if (sp_static in sym.symoptions) then
  1063. begin
  1064. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1065. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1066. if assigned(sym) then
  1067. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1068. p1.free;
  1069. p1:=nil;
  1070. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1071. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1072. end
  1073. else
  1074. propaccesslist_to_node(p1,st,propaccesslist);
  1075. include(p1.flags,nf_isproperty);
  1076. end;
  1077. procsym :
  1078. begin
  1079. callflags:=[];
  1080. { generate the method call }
  1081. membercall:=maybe_load_methodpointer(st,p1);
  1082. if membercall then
  1083. include(callflags,cnf_member_call);
  1084. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1085. paras:=nil;
  1086. include(p1.flags,nf_isproperty);
  1087. end
  1088. else
  1089. begin
  1090. p1:=cerrornode.create;
  1091. Message(type_e_mismatch);
  1092. end;
  1093. end;
  1094. end
  1095. else
  1096. if (ppo_dispid_read in propsym.propoptions) then
  1097. begin
  1098. p2:=internalstatements(statements);
  1099. converted_result_data:=ctempcreatenode.create(propsym.propdef,sizeof(propsym.propdef),tt_persistent,true);
  1100. addstatement(statements,converted_result_data);
  1101. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  1102. ctypeconvnode.create_internal(translate_disp_call(p1,nil,nil,'',propsym.dispid,propsym.propdef),
  1103. propsym.propdef)));
  1104. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  1105. addstatement(statements,ctemprefnode.create(converted_result_data));
  1106. p1:=p2;
  1107. end
  1108. else
  1109. begin
  1110. { error, no function to read property }
  1111. p1:=cerrornode.create;
  1112. Message(parser_e_no_procedure_to_access_property);
  1113. end;
  1114. end;
  1115. { release paras if not used }
  1116. if assigned(paras) then
  1117. paras.free;
  1118. end;
  1119. { the ID token has to be consumed before calling this function }
  1120. procedure do_member_read(classh:tobjectdef;getaddr : boolean;sym : tsym;var p1 : tnode;var again : boolean;callflags:tcallnodeflags);
  1121. var
  1122. static_name : string;
  1123. isclassref : boolean;
  1124. srsymtable : TSymtable;
  1125. begin
  1126. if sym=nil then
  1127. begin
  1128. { pattern is still valid unless
  1129. there is another ID just after the ID of sym }
  1130. Message1(sym_e_id_no_member,orgpattern);
  1131. p1.free;
  1132. p1:=cerrornode.create;
  1133. { try to clean up }
  1134. again:=false;
  1135. end
  1136. else
  1137. begin
  1138. if assigned(p1) then
  1139. begin
  1140. if not assigned(p1.resultdef) then
  1141. do_typecheckpass(p1);
  1142. isclassref:=(p1.resultdef.typ=classrefdef);
  1143. end
  1144. else
  1145. isclassref:=false;
  1146. { we assume, that only procsyms and varsyms are in an object }
  1147. { symbol table, for classes, properties are allowed }
  1148. case sym.typ of
  1149. procsym:
  1150. begin
  1151. do_proc_call(sym,sym.owner,classh,
  1152. (getaddr and not(token in [_CARET,_POINT])),
  1153. again,p1,callflags);
  1154. { we need to know which procedure is called }
  1155. do_typecheckpass(p1);
  1156. { calling using classref? }
  1157. if isclassref and
  1158. (p1.nodetype=calln) and
  1159. assigned(tcallnode(p1).procdefinition) and
  1160. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1161. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1162. Message(parser_e_only_class_members_via_class_ref);
  1163. end;
  1164. fieldvarsym:
  1165. begin
  1166. if (sp_static in sym.symoptions) then
  1167. begin
  1168. static_name:=lower(sym.owner.name^)+'_'+sym.name;
  1169. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable);
  1170. if assigned(sym) then
  1171. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1172. p1.free;
  1173. p1:=nil;
  1174. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1175. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1176. end
  1177. else
  1178. begin
  1179. if isclassref then
  1180. if assigned(p1) and
  1181. (
  1182. is_self_node(p1) or
  1183. (assigned(current_procinfo) and ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions) and
  1184. (current_procinfo.procdef._class = classh))) then
  1185. Message(parser_e_only_class_members)
  1186. else
  1187. Message(parser_e_only_class_members_via_class_ref);
  1188. p1:=csubscriptnode.create(sym,p1);
  1189. end;
  1190. end;
  1191. propertysym:
  1192. begin
  1193. if isclassref and not (sp_static in sym.symoptions) then
  1194. Message(parser_e_only_class_members_via_class_ref);
  1195. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1196. end;
  1197. typesym:
  1198. begin
  1199. p1.free;
  1200. p1:=ctypenode.create(ttypesym(sym).typedef);
  1201. if (is_class(ttypesym(sym).typedef) or is_objcclass(ttypesym(sym).typedef)) and
  1202. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1203. p1:=cloadvmtaddrnode.create(p1);
  1204. end;
  1205. constsym:
  1206. begin
  1207. p1.free;
  1208. p1:=genconstsymtree(tconstsym(sym));
  1209. end
  1210. else
  1211. internalerror(16);
  1212. end;
  1213. end;
  1214. end;
  1215. {****************************************************************************
  1216. Factor
  1217. ****************************************************************************}
  1218. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1219. out memberparentdef: tdef): boolean;
  1220. var
  1221. hdef : tdef;
  1222. begin
  1223. result:=true;
  1224. memberparentdef:=nil;
  1225. case st.symtabletype of
  1226. ObjectSymtable:
  1227. begin
  1228. memberparentdef:=tdef(st.defowner);
  1229. exit;
  1230. end;
  1231. WithSymtable:
  1232. begin
  1233. if assigned(p1) then
  1234. internalerror(2007012002);
  1235. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1236. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1237. if not(hdef.typ in [objectdef,classrefdef]) then
  1238. exit;
  1239. if (hdef.typ=classrefdef) then
  1240. hdef:=tclassrefdef(hdef).pointeddef;
  1241. memberparentdef:=hdef;
  1242. end;
  1243. else
  1244. result:=false;
  1245. end;
  1246. end;
  1247. {$maxfpuregisters 0}
  1248. function factor(getaddr : boolean) : tnode;
  1249. {---------------------------------------------
  1250. Factor_read_id
  1251. ---------------------------------------------}
  1252. procedure factor_read_id(out p1:tnode;var again:boolean);
  1253. var
  1254. srsym : tsym;
  1255. unit_found : boolean;
  1256. srsymtable : TSymtable;
  1257. hdef : tdef;
  1258. orgstoredpattern,
  1259. storedpattern : string;
  1260. t : ttoken;
  1261. begin
  1262. { allow post fix operators }
  1263. again:=true;
  1264. { first check for identifier }
  1265. if token<>_ID then
  1266. begin
  1267. srsym:=generrorsym;
  1268. srsymtable:=nil;
  1269. consume(_ID);
  1270. end
  1271. else
  1272. begin
  1273. searchsym(pattern,srsym,srsymtable);
  1274. { handle unit specification like System.Writeln }
  1275. unit_found:=try_consume_unitsym(srsym,srsymtable,t);
  1276. storedpattern:=pattern;
  1277. orgstoredpattern:=orgpattern;
  1278. consume(t);
  1279. { named parameter support }
  1280. found_arg_name:=false;
  1281. if not(unit_found) and
  1282. named_args_allowed and
  1283. (token=_ASSIGNMENT) then
  1284. begin
  1285. found_arg_name:=true;
  1286. p1:=cstringconstnode.createstr(storedpattern);
  1287. consume(_ASSIGNMENT);
  1288. exit;
  1289. end;
  1290. { if nothing found give error and return errorsym }
  1291. if assigned(srsym) then
  1292. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  1293. else
  1294. begin
  1295. identifier_not_found(orgstoredpattern);
  1296. srsym:=generrorsym;
  1297. srsymtable:=nil;
  1298. end;
  1299. end;
  1300. { Access to funcret or need to call the function? }
  1301. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1302. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1303. { result(x) is not allowed }
  1304. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  1305. (
  1306. (token=_LKLAMMER) or
  1307. (
  1308. (
  1309. (m_tp7 in current_settings.modeswitches) or
  1310. (m_delphi in current_settings.modeswitches)
  1311. ) and
  1312. (afterassignment or in_args)
  1313. )
  1314. ) then
  1315. begin
  1316. hdef:=tdef(srsym.owner.defowner);
  1317. if assigned(hdef) and
  1318. (hdef.typ=procdef) then
  1319. srsym:=tprocdef(hdef).procsym
  1320. else
  1321. begin
  1322. Message(parser_e_illegal_expression);
  1323. srsym:=generrorsym;
  1324. end;
  1325. srsymtable:=srsym.owner;
  1326. end;
  1327. begin
  1328. case srsym.typ of
  1329. absolutevarsym :
  1330. begin
  1331. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1332. begin
  1333. p1:=nil;
  1334. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1335. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1336. include(p1.flags,nf_absolute);
  1337. end
  1338. else
  1339. p1:=cloadnode.create(srsym,srsymtable);
  1340. end;
  1341. staticvarsym,
  1342. localvarsym,
  1343. paravarsym,
  1344. fieldvarsym :
  1345. begin
  1346. { check if we are reading a field of an object/class/ }
  1347. { record. is_member_read() will deal with withsymtables }
  1348. { if needed. }
  1349. p1:=nil;
  1350. if is_member_read(srsym,srsymtable,p1,hdef) then
  1351. begin
  1352. { if the field was originally found in an }
  1353. { objectsymtable, it means it's part of self
  1354. if only method from which it was called is
  1355. not class static }
  1356. if (srsymtable.symtabletype=ObjectSymtable) then
  1357. if (assigned(current_procinfo) and ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions)) then
  1358. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef._class))
  1359. else
  1360. p1:=load_self_node;
  1361. { now, if the field itself is part of an objectsymtab }
  1362. { (it can be even if it was found in a withsymtable, }
  1363. { e.g., "with classinstance do field := 5"), then }
  1364. { let do_member_read handle it }
  1365. if (srsym.owner.symtabletype=ObjectSymtable) then
  1366. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1367. else
  1368. { otherwise it's a regular record subscript }
  1369. p1:=csubscriptnode.create(srsym,p1);
  1370. end
  1371. else
  1372. { regular non-field load }
  1373. p1:=cloadnode.create(srsym,srsymtable);
  1374. end;
  1375. syssym :
  1376. begin
  1377. p1:=statement_syssym(tsyssym(srsym).number);
  1378. end;
  1379. typesym :
  1380. begin
  1381. hdef:=ttypesym(srsym).typedef;
  1382. if not assigned(hdef) then
  1383. begin
  1384. again:=false;
  1385. end
  1386. else
  1387. begin
  1388. { We need to know if this unit uses Variants }
  1389. if (hdef=cvarianttype) and
  1390. not(cs_compilesystem in current_settings.moduleswitches) then
  1391. current_module.flags:=current_module.flags or uf_uses_variants;
  1392. if try_to_consume(_LKLAMMER) then
  1393. begin
  1394. p1:=comp_expr(true);
  1395. consume(_RKLAMMER);
  1396. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1397. end
  1398. else { not LKLAMMER }
  1399. if (token=_POINT) and
  1400. is_object(hdef) then
  1401. begin
  1402. consume(_POINT);
  1403. if assigned(current_objectdef) and
  1404. not(getaddr) then
  1405. begin
  1406. if current_objectdef.is_related(tobjectdef(hdef)) then
  1407. begin
  1408. p1:=ctypenode.create(hdef);
  1409. { search also in inherited methods }
  1410. searchsym_in_class(tobjectdef(hdef),current_objectdef,pattern,srsym,srsymtable);
  1411. if assigned(srsym) then
  1412. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1413. consume(_ID);
  1414. do_member_read(tobjectdef(hdef),false,srsym,p1,again,[]);
  1415. end
  1416. else
  1417. begin
  1418. Message(parser_e_no_super_class);
  1419. again:=false;
  1420. end;
  1421. end
  1422. else
  1423. begin
  1424. { allows @TObject.Load }
  1425. { also allows static methods and variables }
  1426. p1:=ctypenode.create(hdef);
  1427. { TP allows also @TMenu.Load if Load is only }
  1428. { defined in an anchestor class }
  1429. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1430. if assigned(srsym) then
  1431. begin
  1432. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1433. if not(getaddr) and not(sp_static in srsym.symoptions) then
  1434. Message(sym_e_only_static_in_static)
  1435. else
  1436. begin
  1437. consume(_ID);
  1438. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1439. end;
  1440. end
  1441. else
  1442. Message1(sym_e_id_no_member,orgpattern);
  1443. end;
  1444. end
  1445. else
  1446. begin
  1447. { class reference ? }
  1448. if is_class(hdef) or
  1449. is_objcclass(hdef) then
  1450. begin
  1451. if getaddr and (token=_POINT) then
  1452. begin
  1453. consume(_POINT);
  1454. { allows @Object.Method }
  1455. { also allows static methods and variables }
  1456. p1:=ctypenode.create(hdef);
  1457. { TP allows also @TMenu.Load if Load is only }
  1458. { defined in an anchestor class }
  1459. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1460. if assigned(srsym) then
  1461. begin
  1462. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1463. consume(_ID);
  1464. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1465. end
  1466. else
  1467. begin
  1468. Message1(sym_e_id_no_member,orgpattern);
  1469. consume(_ID);
  1470. end;
  1471. end
  1472. else
  1473. begin
  1474. p1:=ctypenode.create(hdef);
  1475. { For a type block we simply return only
  1476. the type. For all other blocks we return
  1477. a loadvmt node }
  1478. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1479. p1:=cloadvmtaddrnode.create(p1);
  1480. end;
  1481. end
  1482. else
  1483. p1:=ctypenode.create(hdef);
  1484. end;
  1485. end;
  1486. end;
  1487. enumsym :
  1488. begin
  1489. p1:=genenumnode(tenumsym(srsym));
  1490. end;
  1491. constsym :
  1492. begin
  1493. if tconstsym(srsym).consttyp=constresourcestring then
  1494. begin
  1495. p1:=cloadnode.create(srsym,srsymtable);
  1496. do_typecheckpass(p1);
  1497. p1.resultdef:=cansistringtype;
  1498. end
  1499. else
  1500. p1:=genconstsymtree(tconstsym(srsym));
  1501. end;
  1502. procsym :
  1503. begin
  1504. p1:=nil;
  1505. { check if it's a method/class method }
  1506. if is_member_read(srsym,srsymtable,p1,hdef) then
  1507. begin
  1508. { not srsymtable.symtabletype since that can be }
  1509. { withsymtable as well }
  1510. if (srsym.owner.symtabletype=ObjectSymtable) then
  1511. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1512. else
  1513. { no procsyms in records (yet) }
  1514. internalerror(2007012006);
  1515. end
  1516. else
  1517. { regular procedure/function call }
  1518. do_proc_call(srsym,srsymtable,nil,
  1519. (getaddr and not(token in [_CARET,_POINT])),
  1520. again,p1,[]);
  1521. end;
  1522. propertysym :
  1523. begin
  1524. p1:=nil;
  1525. { property of a class/object? }
  1526. if is_member_read(srsym,srsymtable,p1,hdef) then
  1527. begin
  1528. if (srsymtable.symtabletype=ObjectSymtable) then
  1529. if (assigned(current_procinfo) and ([po_staticmethod,po_classmethod] <= current_procinfo.procdef.procoptions)) then
  1530. { no self node in static class methods }
  1531. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  1532. else
  1533. p1:=load_self_node;
  1534. { not srsymtable.symtabletype since that can be }
  1535. { withsymtable as well }
  1536. if (srsym.owner.symtabletype=ObjectSymtable) then
  1537. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[])
  1538. else
  1539. { no propertysyms in records (yet) }
  1540. internalerror(2009111510);
  1541. end
  1542. else
  1543. { no method pointer }
  1544. begin
  1545. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1546. end;
  1547. end;
  1548. labelsym :
  1549. begin
  1550. { Support @label }
  1551. if getaddr then
  1552. begin
  1553. if srsym.owner<>current_procinfo.procdef.localst then
  1554. CGMessage(parser_e_label_outside_proc);
  1555. p1:=cloadnode.create(srsym,srsym.owner)
  1556. end
  1557. else
  1558. begin
  1559. consume(_COLON);
  1560. if tlabelsym(srsym).defined then
  1561. Message(sym_e_label_already_defined);
  1562. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1563. begin
  1564. tlabelsym(srsym).nonlocal:=true;
  1565. exclude(current_procinfo.procdef.procoptions,po_inline);
  1566. end;
  1567. tlabelsym(srsym).defined:=true;
  1568. p1:=clabelnode.create(nil,tlabelsym(srsym));
  1569. tlabelsym(srsym).code:=p1;
  1570. end;
  1571. end;
  1572. errorsym :
  1573. begin
  1574. p1:=cerrornode.create;
  1575. if try_to_consume(_LKLAMMER) then
  1576. begin
  1577. parse_paras(false,false,_RKLAMMER);
  1578. consume(_RKLAMMER);
  1579. end;
  1580. end;
  1581. else
  1582. begin
  1583. p1:=cerrornode.create;
  1584. Message(parser_e_illegal_expression);
  1585. end;
  1586. end; { end case }
  1587. end;
  1588. end;
  1589. {---------------------------------------------
  1590. Factor_Read_Set
  1591. ---------------------------------------------}
  1592. { Read a set between [] }
  1593. function factor_read_set:tnode;
  1594. var
  1595. p1,p2 : tnode;
  1596. lastp,
  1597. buildp : tarrayconstructornode;
  1598. begin
  1599. buildp:=nil;
  1600. { be sure that a least one arrayconstructn is used, also for an
  1601. empty [] }
  1602. if token=_RECKKLAMMER then
  1603. buildp:=carrayconstructornode.create(nil,buildp)
  1604. else
  1605. repeat
  1606. p1:=comp_expr(true);
  1607. if try_to_consume(_POINTPOINT) then
  1608. begin
  1609. p2:=comp_expr(true);
  1610. p1:=carrayconstructorrangenode.create(p1,p2);
  1611. end;
  1612. { insert at the end of the tree, to get the correct order }
  1613. if not assigned(buildp) then
  1614. begin
  1615. buildp:=carrayconstructornode.create(p1,nil);
  1616. lastp:=buildp;
  1617. end
  1618. else
  1619. begin
  1620. lastp.right:=carrayconstructornode.create(p1,nil);
  1621. lastp:=tarrayconstructornode(lastp.right);
  1622. end;
  1623. { there could be more elements }
  1624. until not try_to_consume(_COMMA);
  1625. factor_read_set:=buildp;
  1626. end;
  1627. {---------------------------------------------
  1628. PostFixOperators
  1629. ---------------------------------------------}
  1630. { returns whether or not p1 has been changed }
  1631. function postfixoperators(var p1:tnode;var again:boolean): boolean;
  1632. { tries to avoid syntax errors after invalid qualifiers }
  1633. procedure recoverconsume_postfixops;
  1634. begin
  1635. repeat
  1636. if not try_to_consume(_CARET) then
  1637. if try_to_consume(_POINT) then
  1638. try_to_consume(_ID)
  1639. else if try_to_consume(_LECKKLAMMER) then
  1640. begin
  1641. repeat
  1642. comp_expr(true);
  1643. until not try_to_consume(_COMMA);
  1644. consume(_RECKKLAMMER);
  1645. end
  1646. else if try_to_consume(_LKLAMMER) then
  1647. begin
  1648. repeat
  1649. comp_expr(true);
  1650. until not try_to_consume(_COMMA);
  1651. consume(_RKLAMMER);
  1652. end
  1653. else
  1654. break;
  1655. until false;
  1656. end;
  1657. procedure handle_variantarray;
  1658. var
  1659. p4 : tnode;
  1660. newstatement : tstatementnode;
  1661. tempresultvariant,
  1662. temp : ttempcreatenode;
  1663. paras : tcallparanode;
  1664. newblock : tnode;
  1665. countindices : aint;
  1666. begin
  1667. { create statements with call initialize the arguments and
  1668. call fpc_dynarr_setlength }
  1669. newblock:=internalstatements(newstatement);
  1670. { get temp for array of indicies,
  1671. we set the real size later }
  1672. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1673. addstatement(newstatement,temp);
  1674. countindices:=0;
  1675. repeat
  1676. p4:=comp_expr(true);
  1677. addstatement(newstatement,cassignmentnode.create(
  1678. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1679. inc(countindices);
  1680. until not try_to_consume(_COMMA);
  1681. { set real size }
  1682. temp.size:=countindices*s32inttype.size;
  1683. consume(_RECKKLAMMER);
  1684. { we need only a write access if a := follows }
  1685. if token=_ASSIGNMENT then
  1686. begin
  1687. consume(_ASSIGNMENT);
  1688. p4:=comp_expr(true);
  1689. { create call to fpc_vararray_put }
  1690. paras:=ccallparanode.create(cordconstnode.create
  1691. (countindices,s32inttype,true),
  1692. ccallparanode.create(caddrnode.create_internal
  1693. (ctemprefnode.create(temp)),
  1694. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1695. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1696. ,nil))));
  1697. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1698. addstatement(newstatement,ctempdeletenode.create(temp));
  1699. end
  1700. else
  1701. begin
  1702. { create temp for result }
  1703. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1704. addstatement(newstatement,tempresultvariant);
  1705. { create call to fpc_vararray_get }
  1706. paras:=ccallparanode.create(cordconstnode.create
  1707. (countindices,s32inttype,true),
  1708. ccallparanode.create(caddrnode.create_internal
  1709. (ctemprefnode.create(temp)),
  1710. ccallparanode.create(p1,
  1711. ccallparanode.create(
  1712. ctemprefnode.create(tempresultvariant)
  1713. ,nil))));
  1714. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1715. addstatement(newstatement,ctempdeletenode.create(temp));
  1716. { the last statement should return the value as
  1717. location and type, this is done be referencing the
  1718. temp and converting it first from a persistent temp to
  1719. normal temp }
  1720. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1721. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1722. end;
  1723. p1:=newblock;
  1724. end;
  1725. var
  1726. protsym : tpropertysym;
  1727. p2,p3 : tnode;
  1728. srsym : tsym;
  1729. srsymtable : TSymtable;
  1730. classh : tobjectdef;
  1731. { shouldn't be used that often, so the extra overhead is ok to save
  1732. stack space }
  1733. dispatchstring : ansistring;
  1734. nodechanged : boolean;
  1735. label
  1736. skipreckklammercheck;
  1737. begin
  1738. result:=false;
  1739. again:=true;
  1740. while again do
  1741. begin
  1742. { we need the resultdef }
  1743. do_typecheckpass_changed(p1,nodechanged);
  1744. result:=result or nodechanged;
  1745. if codegenerror then
  1746. begin
  1747. recoverconsume_postfixops;
  1748. exit;
  1749. end;
  1750. { handle token }
  1751. case token of
  1752. _CARET:
  1753. begin
  1754. consume(_CARET);
  1755. { support tp/mac procvar^ if the procvar returns a
  1756. pointer type }
  1757. if ((m_tp_procvar in current_settings.modeswitches) or
  1758. (m_mac_procvar in current_settings.modeswitches)) and
  1759. (p1.resultdef.typ=procvardef) and
  1760. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1761. begin
  1762. p1:=ccallnode.create_procvar(nil,p1);
  1763. typecheckpass(p1);
  1764. end;
  1765. if (p1.resultdef.typ<>pointerdef) then
  1766. begin
  1767. { ^ as binary operator is a problem!!!! (FK) }
  1768. again:=false;
  1769. Message(parser_e_invalid_qualifier);
  1770. recoverconsume_postfixops;
  1771. p1.destroy;
  1772. p1:=cerrornode.create;
  1773. end
  1774. else
  1775. p1:=cderefnode.create(p1);
  1776. end;
  1777. _LECKKLAMMER:
  1778. begin
  1779. if is_class_or_interface_or_object(p1.resultdef) then
  1780. begin
  1781. { default property }
  1782. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1783. if not(assigned(protsym)) then
  1784. begin
  1785. p1.destroy;
  1786. p1:=cerrornode.create;
  1787. again:=false;
  1788. message(parser_e_no_default_property_available);
  1789. end
  1790. else
  1791. begin
  1792. { The property symbol is referenced indirect }
  1793. protsym.IncRefCount;
  1794. handle_propertysym(protsym,protsym.owner,p1);
  1795. end;
  1796. end
  1797. else
  1798. begin
  1799. consume(_LECKKLAMMER);
  1800. repeat
  1801. { in all of the cases below, p1 is changed }
  1802. case p1.resultdef.typ of
  1803. pointerdef:
  1804. begin
  1805. { support delphi autoderef }
  1806. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1807. (m_autoderef in current_settings.modeswitches) then
  1808. p1:=cderefnode.create(p1);
  1809. p2:=comp_expr(true);
  1810. { Support Pbytevar[0..9] which returns array [0..9].}
  1811. if try_to_consume(_POINTPOINT) then
  1812. p2:=crangenode.create(p2,comp_expr(true));
  1813. p1:=cvecnode.create(p1,p2);
  1814. end;
  1815. variantdef:
  1816. begin
  1817. handle_variantarray;
  1818. { the RECKKLAMMER is already read }
  1819. goto skipreckklammercheck;
  1820. end;
  1821. stringdef :
  1822. begin
  1823. p2:=comp_expr(true);
  1824. { Support string[0..9] which returns array [0..9] of char.}
  1825. if try_to_consume(_POINTPOINT) then
  1826. p2:=crangenode.create(p2,comp_expr(true));
  1827. p1:=cvecnode.create(p1,p2);
  1828. end;
  1829. arraydef:
  1830. begin
  1831. p2:=comp_expr(true);
  1832. { support SEG:OFS for go32v2 Mem[] }
  1833. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1834. (p1.nodetype=loadn) and
  1835. assigned(tloadnode(p1).symtableentry) and
  1836. assigned(tloadnode(p1).symtableentry.owner.name) and
  1837. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1838. ((tloadnode(p1).symtableentry.name='MEM') or
  1839. (tloadnode(p1).symtableentry.name='MEMW') or
  1840. (tloadnode(p1).symtableentry.name='MEML')) then
  1841. begin
  1842. if try_to_consume(_COLON) then
  1843. begin
  1844. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1845. p2:=comp_expr(true);
  1846. p2:=caddnode.create(addn,p2,p3);
  1847. if try_to_consume(_POINTPOINT) then
  1848. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1849. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true),p3.getcopy));
  1850. p1:=cvecnode.create(p1,p2);
  1851. include(tvecnode(p1).flags,nf_memseg);
  1852. include(tvecnode(p1).flags,nf_memindex);
  1853. end
  1854. else
  1855. begin
  1856. if try_to_consume(_POINTPOINT) then
  1857. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1858. p2:=crangenode.create(p2,comp_expr(true));
  1859. p1:=cvecnode.create(p1,p2);
  1860. include(tvecnode(p1).flags,nf_memindex);
  1861. end;
  1862. end
  1863. else
  1864. begin
  1865. if try_to_consume(_POINTPOINT) then
  1866. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1867. p2:=crangenode.create(p2,comp_expr(true));
  1868. p1:=cvecnode.create(p1,p2);
  1869. end;
  1870. end;
  1871. else
  1872. begin
  1873. if p1.resultdef.typ<>undefineddef then
  1874. Message(parser_e_invalid_qualifier);
  1875. p1.destroy;
  1876. p1:=cerrornode.create;
  1877. comp_expr(true);
  1878. again:=false;
  1879. end;
  1880. end;
  1881. do_typecheckpass(p1);
  1882. until not try_to_consume(_COMMA);
  1883. consume(_RECKKLAMMER);
  1884. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1885. skipreckklammercheck:
  1886. end;
  1887. end;
  1888. _POINT :
  1889. begin
  1890. consume(_POINT);
  1891. if (p1.resultdef.typ=pointerdef) and
  1892. (m_autoderef in current_settings.modeswitches) then
  1893. begin
  1894. p1:=cderefnode.create(p1);
  1895. do_typecheckpass(p1);
  1896. end;
  1897. case p1.resultdef.typ of
  1898. recorddef:
  1899. begin
  1900. if token=_ID then
  1901. begin
  1902. srsym:=tsym(trecorddef(p1.resultdef).symtable.Find(pattern));
  1903. if assigned(srsym) and
  1904. (srsym.typ=fieldvarsym) then
  1905. begin
  1906. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1907. p1:=csubscriptnode.create(srsym,p1)
  1908. end
  1909. else
  1910. begin
  1911. Message1(sym_e_illegal_field,pattern);
  1912. p1.destroy;
  1913. p1:=cerrornode.create;
  1914. end;
  1915. end;
  1916. consume(_ID);
  1917. end;
  1918. variantdef:
  1919. begin
  1920. { dispatch call? }
  1921. if token=_ID then
  1922. begin
  1923. dispatchstring:=orgpattern;
  1924. consume(_ID);
  1925. if try_to_consume(_LKLAMMER) then
  1926. begin
  1927. p2:=parse_paras(false,true,_RKLAMMER);
  1928. consume(_RKLAMMER);
  1929. end
  1930. else
  1931. p2:=nil;
  1932. { property setter? }
  1933. if (token=_ASSIGNMENT) and not(afterassignment) then
  1934. begin
  1935. consume(_ASSIGNMENT);
  1936. { read the expression }
  1937. p3:=comp_expr(true);
  1938. { concat value parameter too }
  1939. p2:=ccallparanode.create(p3,p2);
  1940. { passing p3 here is only for information purposes }
  1941. p1:=translate_disp_call(p1,p2,p3,dispatchstring,0,voidtype);
  1942. end
  1943. else
  1944. { this is only an approximation
  1945. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  1946. if afterassignment or in_args or (token<>_SEMICOLON) then
  1947. p1:=translate_disp_call(p1,p2,nil,dispatchstring,0,cvarianttype)
  1948. else
  1949. p1:=translate_disp_call(p1,p2,nil,dispatchstring,0,voidtype);
  1950. end
  1951. else { Error }
  1952. Consume(_ID);
  1953. end;
  1954. classrefdef:
  1955. begin
  1956. if token=_ID then
  1957. begin
  1958. classh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1959. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1960. if assigned(srsym) then
  1961. begin
  1962. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1963. consume(_ID);
  1964. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1965. end
  1966. else
  1967. begin
  1968. Message1(sym_e_id_no_member,orgpattern);
  1969. p1.destroy;
  1970. p1:=cerrornode.create;
  1971. { try to clean up }
  1972. consume(_ID);
  1973. end;
  1974. end
  1975. else { Error }
  1976. Consume(_ID);
  1977. end;
  1978. objectdef:
  1979. begin
  1980. if token=_ID then
  1981. begin
  1982. classh:=tobjectdef(p1.resultdef);
  1983. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1984. if assigned(srsym) then
  1985. begin
  1986. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1987. consume(_ID);
  1988. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1989. end
  1990. else
  1991. begin
  1992. Message1(sym_e_id_no_member,orgpattern);
  1993. p1.destroy;
  1994. p1:=cerrornode.create;
  1995. { try to clean up }
  1996. consume(_ID);
  1997. end;
  1998. end
  1999. else { Error }
  2000. Consume(_ID);
  2001. end;
  2002. pointerdef:
  2003. begin
  2004. if (p1.resultdef=objc_idtype) then
  2005. begin
  2006. { objc's id type can be used to call any
  2007. Objective-C method of any Objective-C class
  2008. type that's currently in scope }
  2009. if search_objc_method(pattern,srsym,srsymtable) then
  2010. begin
  2011. consume(_ID);
  2012. do_proc_call(srsym,srsymtable,nil,
  2013. (getaddr and not(token in [_CARET,_POINT])),
  2014. again,p1,[cnf_objc_id_call]);
  2015. { we need to know which procedure is called }
  2016. do_typecheckpass(p1);
  2017. end
  2018. else
  2019. begin
  2020. consume(_ID);
  2021. Message(parser_e_methode_id_expected);
  2022. end;
  2023. end
  2024. else
  2025. begin
  2026. Message(parser_e_invalid_qualifier);
  2027. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2028. Message(parser_h_maybe_deref_caret_missing);
  2029. end
  2030. end;
  2031. else
  2032. begin
  2033. if p1.resultdef.typ<>undefineddef then
  2034. Message(parser_e_invalid_qualifier);
  2035. p1.destroy;
  2036. p1:=cerrornode.create;
  2037. { Error }
  2038. consume(_ID);
  2039. end;
  2040. end;
  2041. end;
  2042. else
  2043. begin
  2044. { is this a procedure variable ? }
  2045. if assigned(p1.resultdef) and
  2046. (p1.resultdef.typ=procvardef) then
  2047. begin
  2048. { Typenode for typecasting or expecting a procvar }
  2049. if (p1.nodetype=typen) or
  2050. (
  2051. assigned(getprocvardef) and
  2052. equal_defs(p1.resultdef,getprocvardef)
  2053. ) then
  2054. begin
  2055. if try_to_consume(_LKLAMMER) then
  2056. begin
  2057. p1:=comp_expr(true);
  2058. consume(_RKLAMMER);
  2059. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2060. end
  2061. else
  2062. again:=false
  2063. end
  2064. else
  2065. begin
  2066. if try_to_consume(_LKLAMMER) then
  2067. begin
  2068. p2:=parse_paras(false,false,_RKLAMMER);
  2069. consume(_RKLAMMER);
  2070. p1:=ccallnode.create_procvar(p2,p1);
  2071. { proc():= is never possible }
  2072. if token=_ASSIGNMENT then
  2073. begin
  2074. Message(parser_e_illegal_expression);
  2075. p1.free;
  2076. p1:=cerrornode.create;
  2077. again:=false;
  2078. end;
  2079. end
  2080. else
  2081. again:=false;
  2082. end;
  2083. end
  2084. else
  2085. again:=false;
  2086. end;
  2087. end;
  2088. { we only try again if p1 was changed }
  2089. if again or
  2090. (p1.nodetype=errorn) then
  2091. result:=true;
  2092. end; { while again }
  2093. end;
  2094. {---------------------------------------------
  2095. Factor (Main)
  2096. ---------------------------------------------}
  2097. var
  2098. l : longint;
  2099. ic : int64;
  2100. qc : qword;
  2101. p1 : tnode;
  2102. code : integer;
  2103. srsym : tsym;
  2104. srsymtable : TSymtable;
  2105. pd : tprocdef;
  2106. hclassdef : tobjectdef;
  2107. d : bestreal;
  2108. cur : currency;
  2109. hs,hsorg : string;
  2110. hdef : tdef;
  2111. filepos : tfileposinfo;
  2112. again,
  2113. updatefpos,
  2114. nodechanged : boolean;
  2115. begin
  2116. { can't keep a copy of p1 and compare pointers afterwards, because
  2117. p1 may be freed and reallocated in the same place! }
  2118. updatefpos:=false;
  2119. p1:=nil;
  2120. filepos:=current_tokenpos;
  2121. again:=false;
  2122. if token=_ID then
  2123. begin
  2124. again:=true;
  2125. { Handle references to self }
  2126. if (idtoken=_SELF) and
  2127. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2128. assigned(current_objectdef) then
  2129. begin
  2130. p1:=load_self_node;
  2131. consume(_ID);
  2132. again:=true;
  2133. end
  2134. else
  2135. factor_read_id(p1,again);
  2136. if assigned(p1) then
  2137. begin
  2138. { factor_read_id will set the filepos to after the id,
  2139. and in case of _SELF the filepos will already be the
  2140. same as filepos (so setting it again doesn't hurt). }
  2141. p1.fileinfo:=filepos;
  2142. filepos:=current_tokenpos;
  2143. end;
  2144. { handle post fix operators }
  2145. updatefpos:=postfixoperators(p1,again);
  2146. end
  2147. else
  2148. begin
  2149. updatefpos:=true;
  2150. case token of
  2151. _RETURN :
  2152. begin
  2153. consume(_RETURN);
  2154. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2155. p1 := cexitnode.create(comp_expr(true))
  2156. else
  2157. p1 := cexitnode.create(nil);
  2158. end;
  2159. _INHERITED :
  2160. begin
  2161. again:=true;
  2162. consume(_INHERITED);
  2163. if assigned(current_procinfo) and
  2164. assigned(current_objectdef) then
  2165. begin
  2166. hclassdef:=current_objectdef.childof;
  2167. { Objective-C categories *replace* methods in the class
  2168. they extend, or add methods to it. So calling an
  2169. inherited method always calls the method inherited from
  2170. the parent of the extended class }
  2171. if is_objccategory(current_objectdef) then
  2172. hclassdef:=hclassdef.childof;
  2173. { if inherited; only then we need the method with
  2174. the same name }
  2175. if token in endtokens then
  2176. begin
  2177. hs:=current_procinfo.procdef.procsym.name;
  2178. hsorg:=current_procinfo.procdef.procsym.realname;
  2179. anon_inherited:=true;
  2180. { For message methods we need to search using the message
  2181. number or string }
  2182. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2183. srdef:=nil;
  2184. if (po_msgint in pd.procoptions) then
  2185. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2186. else
  2187. if (po_msgstr in pd.procoptions) then
  2188. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2189. else
  2190. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2191. end
  2192. else
  2193. begin
  2194. hs:=pattern;
  2195. hsorg:=orgpattern;
  2196. consume(_ID);
  2197. anon_inherited:=false;
  2198. searchsym_in_class(hclassdef,current_objectdef,hs,srsym,srsymtable);
  2199. end;
  2200. if assigned(srsym) then
  2201. begin
  2202. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2203. { load the procdef from the inherited class and
  2204. not from self }
  2205. case srsym.typ of
  2206. procsym:
  2207. begin
  2208. hdef:=hclassdef;
  2209. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2210. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2211. hdef:=tclassrefdef.create(hdef);
  2212. p1:=ctypenode.create(hdef);
  2213. end;
  2214. propertysym:
  2215. ;
  2216. else
  2217. begin
  2218. Message(parser_e_methode_id_expected);
  2219. p1:=cerrornode.create;
  2220. end;
  2221. end;
  2222. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2223. end
  2224. else
  2225. begin
  2226. if anon_inherited then
  2227. begin
  2228. { For message methods we need to call DefaultHandler }
  2229. if (po_msgint in pd.procoptions) or
  2230. (po_msgstr in pd.procoptions) then
  2231. begin
  2232. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2233. if not assigned(srsym) or
  2234. (srsym.typ<>procsym) then
  2235. internalerror(200303171);
  2236. p1:=nil;
  2237. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2238. end
  2239. else
  2240. begin
  2241. { we need to ignore the inherited; }
  2242. p1:=cnothingnode.create;
  2243. end;
  2244. end
  2245. else
  2246. begin
  2247. Message1(sym_e_id_no_member,hsorg);
  2248. p1:=cerrornode.create;
  2249. end;
  2250. again:=false;
  2251. end;
  2252. { turn auto inheriting off }
  2253. anon_inherited:=false;
  2254. end
  2255. else
  2256. begin
  2257. Message(parser_e_generic_methods_only_in_methods);
  2258. again:=false;
  2259. p1:=cerrornode.create;
  2260. end;
  2261. postfixoperators(p1,again);
  2262. end;
  2263. _INTCONST :
  2264. begin
  2265. {Try first wether the value fits in an int64.}
  2266. val(pattern,ic,code);
  2267. if code=0 then
  2268. begin
  2269. consume(_INTCONST);
  2270. int_to_type(ic,hdef);
  2271. p1:=cordconstnode.create(ic,hdef,true);
  2272. end
  2273. else
  2274. begin
  2275. { try qword next }
  2276. val(pattern,qc,code);
  2277. if code=0 then
  2278. begin
  2279. consume(_INTCONST);
  2280. int_to_type(qc,hdef);
  2281. p1:=cordconstnode.create(qc,hdef,true);
  2282. end;
  2283. end;
  2284. if code<>0 then
  2285. begin
  2286. { finally float }
  2287. val(pattern,d,code);
  2288. if code<>0 then
  2289. begin
  2290. Message(parser_e_invalid_integer);
  2291. consume(_INTCONST);
  2292. l:=1;
  2293. p1:=cordconstnode.create(l,sinttype,true);
  2294. end
  2295. else
  2296. begin
  2297. consume(_INTCONST);
  2298. p1:=crealconstnode.create(d,pbestrealtype^);
  2299. end;
  2300. end
  2301. else
  2302. { the necessary range checking has already been done by val }
  2303. tordconstnode(p1).rangecheck:=false;
  2304. end;
  2305. _REALNUMBER :
  2306. begin
  2307. val(pattern,d,code);
  2308. if code<>0 then
  2309. begin
  2310. Message(parser_e_error_in_real);
  2311. d:=1.0;
  2312. end;
  2313. consume(_REALNUMBER);
  2314. {$ifdef FPC_REAL2REAL_FIXED}
  2315. if current_settings.fputype=fpu_none then
  2316. Message(parser_e_unsupported_real);
  2317. if (current_settings.minfpconstprec=s32real) and
  2318. (d = single(d)) then
  2319. p1:=crealconstnode.create(d,s32floattype)
  2320. else if (current_settings.minfpconstprec=s64real) and
  2321. (d = double(d)) then
  2322. p1:=crealconstnode.create(d,s64floattype)
  2323. else
  2324. {$endif FPC_REAL2REAL_FIXED}
  2325. p1:=crealconstnode.create(d,pbestrealtype^);
  2326. {$ifdef FPC_HAS_STR_CURRENCY}
  2327. val(pattern,cur,code);
  2328. if code=0 then
  2329. trealconstnode(p1).value_currency:=cur;
  2330. {$endif FPC_HAS_STR_CURRENCY}
  2331. end;
  2332. _STRING :
  2333. begin
  2334. string_dec(hdef,true);
  2335. { STRING can be also a type cast }
  2336. if try_to_consume(_LKLAMMER) then
  2337. begin
  2338. p1:=comp_expr(true);
  2339. consume(_RKLAMMER);
  2340. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2341. { handle postfix operators here e.g. string(a)[10] }
  2342. again:=true;
  2343. postfixoperators(p1,again);
  2344. end
  2345. else
  2346. p1:=ctypenode.create(hdef);
  2347. end;
  2348. _FILE :
  2349. begin
  2350. hdef:=cfiletype;
  2351. consume(_FILE);
  2352. { FILE can be also a type cast }
  2353. if try_to_consume(_LKLAMMER) then
  2354. begin
  2355. p1:=comp_expr(true);
  2356. consume(_RKLAMMER);
  2357. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2358. { handle postfix operators here e.g. string(a)[10] }
  2359. again:=true;
  2360. postfixoperators(p1,again);
  2361. end
  2362. else
  2363. begin
  2364. p1:=ctypenode.create(hdef);
  2365. end;
  2366. end;
  2367. _CSTRING :
  2368. begin
  2369. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2370. consume(_CSTRING);
  2371. end;
  2372. _CCHAR :
  2373. begin
  2374. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2375. consume(_CCHAR);
  2376. end;
  2377. _CWSTRING:
  2378. begin
  2379. p1:=cstringconstnode.createwstr(patternw);
  2380. consume(_CWSTRING);
  2381. end;
  2382. _CWCHAR:
  2383. begin
  2384. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2385. consume(_CWCHAR);
  2386. end;
  2387. _KLAMMERAFFE :
  2388. begin
  2389. consume(_KLAMMERAFFE);
  2390. got_addrn:=true;
  2391. { support both @<x> and @(<x>) }
  2392. if try_to_consume(_LKLAMMER) then
  2393. begin
  2394. p1:=factor(true);
  2395. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2396. begin
  2397. again:=true;
  2398. postfixoperators(p1,again);
  2399. end
  2400. else
  2401. consume(_RKLAMMER);
  2402. end
  2403. else
  2404. p1:=factor(true);
  2405. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2406. begin
  2407. again:=true;
  2408. postfixoperators(p1,again);
  2409. end;
  2410. got_addrn:=false;
  2411. p1:=caddrnode.create(p1);
  2412. p1.fileinfo:=filepos;
  2413. if cs_typed_addresses in current_settings.localswitches then
  2414. include(p1.flags,nf_typedaddr);
  2415. { Store the procvar that we are expecting, the
  2416. addrn will use the information to find the correct
  2417. procdef or it will return an error }
  2418. if assigned(getprocvardef) and
  2419. (taddrnode(p1).left.nodetype = loadn) then
  2420. taddrnode(p1).getprocvardef:=getprocvardef;
  2421. end;
  2422. _LKLAMMER :
  2423. begin
  2424. consume(_LKLAMMER);
  2425. p1:=comp_expr(true);
  2426. consume(_RKLAMMER);
  2427. { it's not a good solution }
  2428. { but (a+b)^ makes some problems }
  2429. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2430. begin
  2431. again:=true;
  2432. postfixoperators(p1,again);
  2433. end;
  2434. end;
  2435. _LECKKLAMMER :
  2436. begin
  2437. consume(_LECKKLAMMER);
  2438. p1:=factor_read_set;
  2439. consume(_RECKKLAMMER);
  2440. end;
  2441. _PLUS :
  2442. begin
  2443. consume(_PLUS);
  2444. p1:=factor(false);
  2445. { we must generate a new node to do 0+<p1> otherwise the + will
  2446. not be checked }
  2447. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2448. end;
  2449. _MINUS :
  2450. begin
  2451. consume(_MINUS);
  2452. if (token = _INTCONST) then
  2453. begin
  2454. { ugly hack, but necessary to be able to parse }
  2455. { -9223372036854775808 as int64 (JM) }
  2456. pattern := '-'+pattern;
  2457. p1:=sub_expr(oppower,false);
  2458. { -1 ** 4 should be - (1 ** 4) and not
  2459. (-1) ** 4
  2460. This was the reason of tw0869.pp test failure PM }
  2461. if p1.nodetype=starstarn then
  2462. begin
  2463. if tbinarynode(p1).left.nodetype=ordconstn then
  2464. begin
  2465. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2466. p1:=cunaryminusnode.create(p1);
  2467. end
  2468. else if tbinarynode(p1).left.nodetype=realconstn then
  2469. begin
  2470. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2471. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2472. p1:=cunaryminusnode.create(p1);
  2473. end
  2474. else
  2475. internalerror(20021029);
  2476. end;
  2477. end
  2478. else
  2479. begin
  2480. p1:=sub_expr(oppower,false);
  2481. p1:=cunaryminusnode.create(p1);
  2482. end;
  2483. end;
  2484. _OP_NOT :
  2485. begin
  2486. consume(_OP_NOT);
  2487. p1:=factor(false);
  2488. p1:=cnotnode.create(p1);
  2489. end;
  2490. _TRUE :
  2491. begin
  2492. consume(_TRUE);
  2493. p1:=cordconstnode.create(1,booltype,false);
  2494. end;
  2495. _FALSE :
  2496. begin
  2497. consume(_FALSE);
  2498. p1:=cordconstnode.create(0,booltype,false);
  2499. end;
  2500. _NIL :
  2501. begin
  2502. consume(_NIL);
  2503. p1:=cnilnode.create;
  2504. { It's really ugly code nil^, but delphi allows it }
  2505. if token in [_CARET] then
  2506. begin
  2507. again:=true;
  2508. postfixoperators(p1,again);
  2509. end;
  2510. end;
  2511. _OBJCPROTOCOL:
  2512. begin
  2513. { The @protocol keyword is used in two ways in Objective-C:
  2514. 1) to declare protocols (~ Object Pascal interfaces)
  2515. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2516. of a declared protocol
  2517. This code is for handling the second case. Because of 1),
  2518. we cannot simply use a system unit symbol.
  2519. }
  2520. consume(_OBJCPROTOCOL);
  2521. consume(_LKLAMMER);
  2522. p1:=factor(false);
  2523. consume(_RKLAMMER);
  2524. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2525. end;
  2526. else
  2527. begin
  2528. Message(parser_e_illegal_expression);
  2529. p1:=cerrornode.create;
  2530. { recover }
  2531. consume(token);
  2532. end;
  2533. end;
  2534. end;
  2535. { generate error node if no node is created }
  2536. if not assigned(p1) then
  2537. begin
  2538. {$ifdef EXTDEBUG}
  2539. Comment(V_Warning,'factor: p1=nil');
  2540. {$endif}
  2541. p1:=cerrornode.create;
  2542. updatefpos:=true;
  2543. end;
  2544. { get the resultdef for the node }
  2545. if (not assigned(p1.resultdef)) then
  2546. begin
  2547. do_typecheckpass_changed(p1,nodechanged);
  2548. updatefpos:=updatefpos or nodechanged;
  2549. end;
  2550. if assigned(p1) and
  2551. updatefpos then
  2552. p1.fileinfo:=filepos;
  2553. factor:=p1;
  2554. end;
  2555. {$maxfpuregisters default}
  2556. {****************************************************************************
  2557. Sub_Expr
  2558. ****************************************************************************}
  2559. const
  2560. { Warning these stay be ordered !! }
  2561. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2562. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2563. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2564. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2565. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2566. [_STARSTAR] );
  2567. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2568. {Reads a subexpression while the operators are of the current precedence
  2569. level, or any higher level. Replaces the old term, simpl_expr and
  2570. simpl2_expr.}
  2571. var
  2572. p1,p2 : tnode;
  2573. oldt : Ttoken;
  2574. filepos : tfileposinfo;
  2575. begin
  2576. if pred_level=highest_precedence then
  2577. p1:=factor(false)
  2578. else
  2579. p1:=sub_expr(succ(pred_level),true);
  2580. repeat
  2581. if (token in operator_levels[pred_level]) and
  2582. ((token<>_EQUAL) or accept_equal) then
  2583. begin
  2584. oldt:=token;
  2585. filepos:=current_tokenpos;
  2586. consume(token);
  2587. if pred_level=highest_precedence then
  2588. p2:=factor(false)
  2589. else
  2590. p2:=sub_expr(succ(pred_level),true);
  2591. case oldt of
  2592. _PLUS :
  2593. p1:=caddnode.create(addn,p1,p2);
  2594. _MINUS :
  2595. p1:=caddnode.create(subn,p1,p2);
  2596. _STAR :
  2597. p1:=caddnode.create(muln,p1,p2);
  2598. _SLASH :
  2599. p1:=caddnode.create(slashn,p1,p2);
  2600. _EQUAL :
  2601. p1:=caddnode.create(equaln,p1,p2);
  2602. _GT :
  2603. p1:=caddnode.create(gtn,p1,p2);
  2604. _LT :
  2605. p1:=caddnode.create(ltn,p1,p2);
  2606. _GTE :
  2607. p1:=caddnode.create(gten,p1,p2);
  2608. _LTE :
  2609. p1:=caddnode.create(lten,p1,p2);
  2610. _SYMDIF :
  2611. p1:=caddnode.create(symdifn,p1,p2);
  2612. _STARSTAR :
  2613. p1:=caddnode.create(starstarn,p1,p2);
  2614. _OP_AS :
  2615. p1:=casnode.create(p1,p2);
  2616. _OP_IN :
  2617. p1:=cinnode.create(p1,p2);
  2618. _OP_IS :
  2619. p1:=cisnode.create(p1,p2);
  2620. _OP_OR,
  2621. _PIPE {macpas only} :
  2622. begin
  2623. p1:=caddnode.create(orn,p1,p2);
  2624. if (oldt = _PIPE) then
  2625. include(p1.flags,nf_short_bool);
  2626. end;
  2627. _OP_AND,
  2628. _AMPERSAND {macpas only} :
  2629. begin
  2630. p1:=caddnode.create(andn,p1,p2);
  2631. if (oldt = _AMPERSAND) then
  2632. include(p1.flags,nf_short_bool);
  2633. end;
  2634. _OP_DIV :
  2635. p1:=cmoddivnode.create(divn,p1,p2);
  2636. _OP_NOT :
  2637. p1:=cnotnode.create(p1);
  2638. _OP_MOD :
  2639. p1:=cmoddivnode.create(modn,p1,p2);
  2640. _OP_SHL :
  2641. p1:=cshlshrnode.create(shln,p1,p2);
  2642. _OP_SHR :
  2643. p1:=cshlshrnode.create(shrn,p1,p2);
  2644. _OP_XOR :
  2645. p1:=caddnode.create(xorn,p1,p2);
  2646. _ASSIGNMENT :
  2647. p1:=cassignmentnode.create(p1,p2);
  2648. _UNEQUAL :
  2649. p1:=caddnode.create(unequaln,p1,p2);
  2650. end;
  2651. p1.fileinfo:=filepos;
  2652. end
  2653. else
  2654. break;
  2655. until false;
  2656. sub_expr:=p1;
  2657. end;
  2658. function comp_expr(accept_equal : boolean):tnode;
  2659. var
  2660. oldafterassignment : boolean;
  2661. p1 : tnode;
  2662. begin
  2663. oldafterassignment:=afterassignment;
  2664. afterassignment:=true;
  2665. p1:=sub_expr(opcompare,accept_equal);
  2666. { get the resultdef for this expression }
  2667. if not assigned(p1.resultdef) then
  2668. do_typecheckpass(p1);
  2669. afterassignment:=oldafterassignment;
  2670. comp_expr:=p1;
  2671. end;
  2672. function expr : tnode;
  2673. var
  2674. p1,p2 : tnode;
  2675. filepos : tfileposinfo;
  2676. oldafterassignment,
  2677. updatefpos : boolean;
  2678. begin
  2679. oldafterassignment:=afterassignment;
  2680. p1:=sub_expr(opcompare,true);
  2681. { get the resultdef for this expression }
  2682. if not assigned(p1.resultdef) then
  2683. do_typecheckpass(p1);
  2684. filepos:=current_tokenpos;
  2685. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2686. afterassignment:=true;
  2687. updatefpos:=true;
  2688. case token of
  2689. _POINTPOINT :
  2690. begin
  2691. consume(_POINTPOINT);
  2692. p2:=sub_expr(opcompare,true);
  2693. p1:=crangenode.create(p1,p2);
  2694. end;
  2695. _ASSIGNMENT :
  2696. begin
  2697. consume(_ASSIGNMENT);
  2698. if (p1.resultdef.typ=procvardef) then
  2699. getprocvardef:=tprocvardef(p1.resultdef);
  2700. p2:=sub_expr(opcompare,true);
  2701. if assigned(getprocvardef) then
  2702. handle_procvar(getprocvardef,p2);
  2703. getprocvardef:=nil;
  2704. p1:=cassignmentnode.create(p1,p2);
  2705. end;
  2706. _PLUSASN :
  2707. begin
  2708. consume(_PLUSASN);
  2709. p2:=sub_expr(opcompare,true);
  2710. p1:=gen_c_style_operator(addn,p1,p2);
  2711. end;
  2712. _MINUSASN :
  2713. begin
  2714. consume(_MINUSASN);
  2715. p2:=sub_expr(opcompare,true);
  2716. p1:=gen_c_style_operator(subn,p1,p2);
  2717. end;
  2718. _STARASN :
  2719. begin
  2720. consume(_STARASN );
  2721. p2:=sub_expr(opcompare,true);
  2722. p1:=gen_c_style_operator(muln,p1,p2);
  2723. end;
  2724. _SLASHASN :
  2725. begin
  2726. consume(_SLASHASN );
  2727. p2:=sub_expr(opcompare,true);
  2728. p1:=gen_c_style_operator(slashn,p1,p2);
  2729. end;
  2730. else
  2731. updatefpos:=false;
  2732. end;
  2733. { get the resultdef for this expression }
  2734. if not assigned(p1.resultdef) then
  2735. do_typecheckpass(p1);
  2736. afterassignment:=oldafterassignment;
  2737. if updatefpos then
  2738. p1.fileinfo:=filepos;
  2739. expr:=p1;
  2740. end;
  2741. function get_intconst:TConstExprInt;
  2742. {Reads an expression, tries to evalute it and check if it is an integer
  2743. constant. Then the constant is returned.}
  2744. var
  2745. p:tnode;
  2746. begin
  2747. result:=0;
  2748. p:=comp_expr(true);
  2749. if not codegenerror then
  2750. begin
  2751. if (p.nodetype<>ordconstn) or
  2752. not(is_integer(p.resultdef)) then
  2753. Message(parser_e_illegal_expression)
  2754. else
  2755. result:=tordconstnode(p).value;
  2756. end;
  2757. p.free;
  2758. end;
  2759. function get_stringconst:string;
  2760. {Reads an expression, tries to evaluate it and checks if it is a string
  2761. constant. Then the constant is returned.}
  2762. var
  2763. p:tnode;
  2764. begin
  2765. get_stringconst:='';
  2766. p:=comp_expr(true);
  2767. if p.nodetype<>stringconstn then
  2768. begin
  2769. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2770. get_stringconst:=char(int64(tordconstnode(p).value))
  2771. else
  2772. Message(parser_e_illegal_expression);
  2773. end
  2774. else
  2775. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2776. p.free;
  2777. end;
  2778. end.