pexpr.pas 105 KB

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