pexpr.pas 106 KB

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