pexpr.pas 107 KB

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