pexpr.pas 108 KB

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