AST.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/NonnullRefPtrVector.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/RefPtr.h>
  13. #include <AK/String.h>
  14. #include <AK/Variant.h>
  15. #include <AK/Vector.h>
  16. #include <LibJS/Forward.h>
  17. #include <LibJS/Runtime/PropertyName.h>
  18. #include <LibJS/Runtime/Value.h>
  19. #include <LibJS/SourceRange.h>
  20. namespace JS {
  21. class VariableDeclaration;
  22. class FunctionDeclaration;
  23. class Identifier;
  24. enum class FunctionKind {
  25. Generator,
  26. Regular,
  27. };
  28. template<class T, class... Args>
  29. static inline NonnullRefPtr<T>
  30. create_ast_node(SourceRange range, Args&&... args)
  31. {
  32. return adopt_ref(*new T(range, forward<Args>(args)...));
  33. }
  34. class ASTNode : public RefCounted<ASTNode> {
  35. public:
  36. virtual ~ASTNode() { }
  37. virtual Value execute(Interpreter&, GlobalObject&) const = 0;
  38. virtual void generate_bytecode(Bytecode::Generator&) const;
  39. virtual void dump(int indent) const;
  40. SourceRange const& source_range() const { return m_source_range; }
  41. SourceRange& source_range() { return m_source_range; }
  42. String class_name() const;
  43. template<typename T>
  44. bool fast_is() const = delete;
  45. virtual bool is_new_expression() const { return false; }
  46. virtual bool is_member_expression() const { return false; }
  47. virtual bool is_super_expression() const { return false; }
  48. virtual bool is_expression_statement() const { return false; }
  49. virtual bool is_identifier() const { return false; }
  50. virtual bool is_scope_node() const { return false; }
  51. virtual bool is_program() const { return false; }
  52. protected:
  53. explicit ASTNode(SourceRange source_range)
  54. : m_source_range(source_range)
  55. {
  56. }
  57. private:
  58. SourceRange m_source_range;
  59. };
  60. class Statement : public ASTNode {
  61. public:
  62. explicit Statement(SourceRange source_range)
  63. : ASTNode(source_range)
  64. {
  65. }
  66. FlyString const& label() const { return m_label; }
  67. void set_label(FlyString string) { m_label = move(string); }
  68. protected:
  69. FlyString m_label;
  70. };
  71. class EmptyStatement final : public Statement {
  72. public:
  73. explicit EmptyStatement(SourceRange source_range)
  74. : Statement(source_range)
  75. {
  76. }
  77. Value execute(Interpreter&, GlobalObject&) const override { return {}; }
  78. virtual void generate_bytecode(Bytecode::Generator&) const override;
  79. };
  80. class ErrorStatement final : public Statement {
  81. public:
  82. explicit ErrorStatement(SourceRange source_range)
  83. : Statement(source_range)
  84. {
  85. }
  86. Value execute(Interpreter&, GlobalObject&) const override { return {}; }
  87. };
  88. class ExpressionStatement final : public Statement {
  89. public:
  90. ExpressionStatement(SourceRange source_range, NonnullRefPtr<Expression> expression)
  91. : Statement(source_range)
  92. , m_expression(move(expression))
  93. {
  94. }
  95. virtual Value execute(Interpreter&, GlobalObject&) const override;
  96. virtual void dump(int indent) const override;
  97. virtual void generate_bytecode(Bytecode::Generator&) const override;
  98. Expression const& expression() const { return m_expression; };
  99. private:
  100. virtual bool is_expression_statement() const override { return true; }
  101. NonnullRefPtr<Expression> m_expression;
  102. };
  103. class ScopeNode : public Statement {
  104. public:
  105. template<typename T, typename... Args>
  106. T& append(SourceRange range, Args&&... args)
  107. {
  108. auto child = create_ast_node<T>(range, forward<Args>(args)...);
  109. m_children.append(move(child));
  110. return static_cast<T&>(m_children.last());
  111. }
  112. void append(NonnullRefPtr<Statement> child)
  113. {
  114. m_children.append(move(child));
  115. }
  116. NonnullRefPtrVector<Statement> const& children() const { return m_children; }
  117. virtual Value execute(Interpreter&, GlobalObject&) const override;
  118. virtual void dump(int indent) const override;
  119. virtual void generate_bytecode(Bytecode::Generator&) const override;
  120. void add_variables(NonnullRefPtrVector<VariableDeclaration>);
  121. void add_functions(NonnullRefPtrVector<FunctionDeclaration>);
  122. NonnullRefPtrVector<VariableDeclaration> const& variables() const { return m_variables; }
  123. NonnullRefPtrVector<FunctionDeclaration> const& functions() const { return m_functions; }
  124. protected:
  125. explicit ScopeNode(SourceRange source_range)
  126. : Statement(source_range)
  127. {
  128. }
  129. private:
  130. virtual bool is_scope_node() const final { return true; }
  131. NonnullRefPtrVector<Statement> m_children;
  132. NonnullRefPtrVector<VariableDeclaration> m_variables;
  133. NonnullRefPtrVector<FunctionDeclaration> m_functions;
  134. };
  135. class Program final : public ScopeNode {
  136. public:
  137. explicit Program(SourceRange source_range)
  138. : ScopeNode(source_range)
  139. {
  140. }
  141. virtual Value execute(Interpreter&, GlobalObject&) const override;
  142. bool is_strict_mode() const { return m_is_strict_mode; }
  143. void set_strict_mode() { m_is_strict_mode = true; }
  144. private:
  145. virtual bool is_program() const override { return true; }
  146. bool m_is_strict_mode { false };
  147. };
  148. class BlockStatement final : public ScopeNode {
  149. public:
  150. explicit BlockStatement(SourceRange source_range)
  151. : ScopeNode(source_range)
  152. {
  153. }
  154. };
  155. class Expression : public ASTNode {
  156. public:
  157. explicit Expression(SourceRange source_range)
  158. : ASTNode(source_range)
  159. {
  160. }
  161. virtual Reference to_reference(Interpreter&, GlobalObject&) const;
  162. };
  163. class Declaration : public Statement {
  164. public:
  165. explicit Declaration(SourceRange source_range)
  166. : Statement(source_range)
  167. {
  168. }
  169. };
  170. class ErrorDeclaration final : public Declaration {
  171. public:
  172. explicit ErrorDeclaration(SourceRange source_range)
  173. : Declaration(source_range)
  174. {
  175. }
  176. Value execute(Interpreter&, GlobalObject&) const override { return {}; }
  177. };
  178. struct BindingPattern : RefCounted<BindingPattern> {
  179. // This covers both BindingProperty and BindingElement, hence the more generic name
  180. struct BindingEntry {
  181. // If this entry represents a BindingElement, then name will be Empty
  182. Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<Expression>, Empty> name { Empty {} };
  183. Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>, Empty> alias { Empty {} };
  184. RefPtr<Expression> initializer {};
  185. bool is_rest { false };
  186. bool is_elision() const { return name.has<Empty>() && alias.has<Empty>(); }
  187. };
  188. enum class Kind {
  189. Array,
  190. Object,
  191. };
  192. void dump(int indent) const;
  193. template<typename C>
  194. void for_each_bound_name(C&& callback) const;
  195. Vector<BindingEntry> entries;
  196. Kind kind { Kind::Object };
  197. };
  198. class FunctionNode {
  199. public:
  200. struct Parameter {
  201. Variant<FlyString, NonnullRefPtr<BindingPattern>> binding;
  202. RefPtr<Expression> default_value;
  203. bool is_rest { false };
  204. };
  205. FlyString const& name() const { return m_name; }
  206. Statement const& body() const { return *m_body; }
  207. Vector<Parameter> const& parameters() const { return m_parameters; };
  208. i32 function_length() const { return m_function_length; }
  209. bool is_strict_mode() const { return m_is_strict_mode; }
  210. bool is_arrow_function() const { return m_is_arrow_function; }
  211. FunctionKind kind() const { return m_kind; }
  212. protected:
  213. FunctionNode(FlyString name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, FunctionKind kind, bool is_strict_mode, bool is_arrow_function)
  214. : m_name(move(name))
  215. , m_body(move(body))
  216. , m_parameters(move(parameters))
  217. , m_variables(move(variables))
  218. , m_function_length(function_length)
  219. , m_kind(kind)
  220. , m_is_strict_mode(is_strict_mode)
  221. , m_is_arrow_function(is_arrow_function)
  222. {
  223. }
  224. void dump(int indent, String const& class_name) const;
  225. NonnullRefPtrVector<VariableDeclaration> const& variables() const { return m_variables; }
  226. protected:
  227. void set_name(FlyString name)
  228. {
  229. VERIFY(m_name.is_empty());
  230. m_name = move(name);
  231. }
  232. private:
  233. FlyString m_name;
  234. NonnullRefPtr<Statement> m_body;
  235. Vector<Parameter> const m_parameters;
  236. NonnullRefPtrVector<VariableDeclaration> m_variables;
  237. const i32 m_function_length;
  238. FunctionKind m_kind;
  239. bool m_is_strict_mode;
  240. bool m_is_arrow_function { false };
  241. };
  242. class FunctionDeclaration final
  243. : public Declaration
  244. , public FunctionNode {
  245. public:
  246. static bool must_have_name() { return true; }
  247. FunctionDeclaration(SourceRange source_range, FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, FunctionKind kind, bool is_strict_mode = false)
  248. : Declaration(source_range)
  249. , FunctionNode(name, move(body), move(parameters), function_length, move(variables), kind, is_strict_mode, false)
  250. {
  251. }
  252. virtual Value execute(Interpreter&, GlobalObject&) const override;
  253. virtual void dump(int indent) const override;
  254. virtual void generate_bytecode(Bytecode::Generator&) const override;
  255. };
  256. class FunctionExpression final
  257. : public Expression
  258. , public FunctionNode {
  259. public:
  260. static bool must_have_name() { return false; }
  261. FunctionExpression(SourceRange source_range, FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, FunctionKind kind, bool is_strict_mode, bool is_arrow_function = false)
  262. : Expression(source_range)
  263. , FunctionNode(name, move(body), move(parameters), function_length, move(variables), kind, is_strict_mode, is_arrow_function)
  264. {
  265. }
  266. virtual Value execute(Interpreter&, GlobalObject&) const override;
  267. virtual void dump(int indent) const override;
  268. void set_name_if_possible(FlyString new_name)
  269. {
  270. if (m_cannot_auto_rename)
  271. return;
  272. m_cannot_auto_rename = true;
  273. if (name().is_empty())
  274. set_name(move(new_name));
  275. }
  276. bool cannot_auto_rename() const { return m_cannot_auto_rename; }
  277. void set_cannot_auto_rename() { m_cannot_auto_rename = true; }
  278. virtual void generate_bytecode(Bytecode::Generator&) const override;
  279. private:
  280. bool m_cannot_auto_rename { false };
  281. };
  282. class ErrorExpression final : public Expression {
  283. public:
  284. explicit ErrorExpression(SourceRange source_range)
  285. : Expression(source_range)
  286. {
  287. }
  288. Value execute(Interpreter&, GlobalObject&) const override { return {}; }
  289. };
  290. class YieldExpression final : public Expression {
  291. public:
  292. explicit YieldExpression(SourceRange source_range, RefPtr<Expression> argument, bool is_yield_from)
  293. : Expression(source_range)
  294. , m_argument(move(argument))
  295. , m_is_yield_from(is_yield_from)
  296. {
  297. }
  298. Expression const* argument() const { return m_argument; }
  299. bool is_yield_from() const { return m_is_yield_from; }
  300. virtual Value execute(Interpreter&, GlobalObject&) const override;
  301. virtual void dump(int indent) const override;
  302. virtual void generate_bytecode(Bytecode::Generator&) const override;
  303. private:
  304. RefPtr<Expression> m_argument;
  305. bool m_is_yield_from { false };
  306. };
  307. class ReturnStatement final : public Statement {
  308. public:
  309. explicit ReturnStatement(SourceRange source_range, RefPtr<Expression> argument)
  310. : Statement(source_range)
  311. , m_argument(move(argument))
  312. {
  313. }
  314. Expression const* argument() const { return m_argument; }
  315. virtual Value execute(Interpreter&, GlobalObject&) const override;
  316. virtual void dump(int indent) const override;
  317. virtual void generate_bytecode(Bytecode::Generator&) const override;
  318. private:
  319. RefPtr<Expression> m_argument;
  320. };
  321. class IfStatement final : public Statement {
  322. public:
  323. IfStatement(SourceRange source_range, NonnullRefPtr<Expression> predicate, NonnullRefPtr<Statement> consequent, RefPtr<Statement> alternate)
  324. : Statement(source_range)
  325. , m_predicate(move(predicate))
  326. , m_consequent(move(consequent))
  327. , m_alternate(move(alternate))
  328. {
  329. }
  330. Expression const& predicate() const { return *m_predicate; }
  331. Statement const& consequent() const { return *m_consequent; }
  332. Statement const* alternate() const { return m_alternate; }
  333. virtual Value execute(Interpreter&, GlobalObject&) const override;
  334. virtual void dump(int indent) const override;
  335. virtual void generate_bytecode(Bytecode::Generator&) const override;
  336. private:
  337. NonnullRefPtr<Expression> m_predicate;
  338. NonnullRefPtr<Statement> m_consequent;
  339. RefPtr<Statement> m_alternate;
  340. };
  341. class WhileStatement final : public Statement {
  342. public:
  343. WhileStatement(SourceRange source_range, NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  344. : Statement(source_range)
  345. , m_test(move(test))
  346. , m_body(move(body))
  347. {
  348. }
  349. Expression const& test() const { return *m_test; }
  350. Statement const& body() const { return *m_body; }
  351. virtual Value execute(Interpreter&, GlobalObject&) const override;
  352. virtual void dump(int indent) const override;
  353. virtual void generate_bytecode(Bytecode::Generator&) const override;
  354. private:
  355. NonnullRefPtr<Expression> m_test;
  356. NonnullRefPtr<Statement> m_body;
  357. };
  358. class DoWhileStatement final : public Statement {
  359. public:
  360. DoWhileStatement(SourceRange source_range, NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  361. : Statement(source_range)
  362. , m_test(move(test))
  363. , m_body(move(body))
  364. {
  365. }
  366. Expression const& test() const { return *m_test; }
  367. Statement const& body() const { return *m_body; }
  368. virtual Value execute(Interpreter&, GlobalObject&) const override;
  369. virtual void dump(int indent) const override;
  370. virtual void generate_bytecode(Bytecode::Generator&) const override;
  371. private:
  372. NonnullRefPtr<Expression> m_test;
  373. NonnullRefPtr<Statement> m_body;
  374. };
  375. class WithStatement final : public Statement {
  376. public:
  377. WithStatement(SourceRange source_range, NonnullRefPtr<Expression> object, NonnullRefPtr<Statement> body)
  378. : Statement(source_range)
  379. , m_object(move(object))
  380. , m_body(move(body))
  381. {
  382. }
  383. Expression const& object() const { return *m_object; }
  384. Statement const& body() const { return *m_body; }
  385. virtual Value execute(Interpreter&, GlobalObject&) const override;
  386. virtual void dump(int indent) const override;
  387. private:
  388. NonnullRefPtr<Expression> m_object;
  389. NonnullRefPtr<Statement> m_body;
  390. };
  391. class ForStatement final : public Statement {
  392. public:
  393. ForStatement(SourceRange source_range, RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<Statement> body)
  394. : Statement(source_range)
  395. , m_init(move(init))
  396. , m_test(move(test))
  397. , m_update(move(update))
  398. , m_body(move(body))
  399. {
  400. }
  401. ASTNode const* init() const { return m_init; }
  402. Expression const* test() const { return m_test; }
  403. Expression const* update() const { return m_update; }
  404. Statement const& body() const { return *m_body; }
  405. virtual Value execute(Interpreter&, GlobalObject&) const override;
  406. virtual void dump(int indent) const override;
  407. virtual void generate_bytecode(Bytecode::Generator&) const override;
  408. private:
  409. RefPtr<ASTNode> m_init;
  410. RefPtr<Expression> m_test;
  411. RefPtr<Expression> m_update;
  412. NonnullRefPtr<Statement> m_body;
  413. };
  414. class ForInStatement final : public Statement {
  415. public:
  416. ForInStatement(SourceRange source_range, NonnullRefPtr<ASTNode> lhs, NonnullRefPtr<Expression> rhs, NonnullRefPtr<Statement> body)
  417. : Statement(source_range)
  418. , m_lhs(move(lhs))
  419. , m_rhs(move(rhs))
  420. , m_body(move(body))
  421. {
  422. }
  423. ASTNode const& lhs() const { return *m_lhs; }
  424. Expression const& rhs() const { return *m_rhs; }
  425. Statement const& body() const { return *m_body; }
  426. virtual Value execute(Interpreter&, GlobalObject&) const override;
  427. virtual void dump(int indent) const override;
  428. private:
  429. NonnullRefPtr<ASTNode> m_lhs;
  430. NonnullRefPtr<Expression> m_rhs;
  431. NonnullRefPtr<Statement> m_body;
  432. };
  433. class ForOfStatement final : public Statement {
  434. public:
  435. ForOfStatement(SourceRange source_range, NonnullRefPtr<ASTNode> lhs, NonnullRefPtr<Expression> rhs, NonnullRefPtr<Statement> body)
  436. : Statement(source_range)
  437. , m_lhs(move(lhs))
  438. , m_rhs(move(rhs))
  439. , m_body(move(body))
  440. {
  441. }
  442. ASTNode const& lhs() const { return *m_lhs; }
  443. Expression const& rhs() const { return *m_rhs; }
  444. Statement const& body() const { return *m_body; }
  445. virtual Value execute(Interpreter&, GlobalObject&) const override;
  446. virtual void dump(int indent) const override;
  447. private:
  448. NonnullRefPtr<ASTNode> m_lhs;
  449. NonnullRefPtr<Expression> m_rhs;
  450. NonnullRefPtr<Statement> m_body;
  451. };
  452. enum class BinaryOp {
  453. Addition,
  454. Subtraction,
  455. Multiplication,
  456. Division,
  457. Modulo,
  458. Exponentiation,
  459. TypedEquals,
  460. TypedInequals,
  461. AbstractEquals,
  462. AbstractInequals,
  463. GreaterThan,
  464. GreaterThanEquals,
  465. LessThan,
  466. LessThanEquals,
  467. BitwiseAnd,
  468. BitwiseOr,
  469. BitwiseXor,
  470. LeftShift,
  471. RightShift,
  472. UnsignedRightShift,
  473. In,
  474. InstanceOf,
  475. };
  476. class BinaryExpression final : public Expression {
  477. public:
  478. BinaryExpression(SourceRange source_range, BinaryOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  479. : Expression(source_range)
  480. , m_op(op)
  481. , m_lhs(move(lhs))
  482. , m_rhs(move(rhs))
  483. {
  484. }
  485. virtual Value execute(Interpreter&, GlobalObject&) const override;
  486. virtual void dump(int indent) const override;
  487. virtual void generate_bytecode(Bytecode::Generator&) const override;
  488. private:
  489. BinaryOp m_op;
  490. NonnullRefPtr<Expression> m_lhs;
  491. NonnullRefPtr<Expression> m_rhs;
  492. };
  493. enum class LogicalOp {
  494. And,
  495. Or,
  496. NullishCoalescing,
  497. };
  498. class LogicalExpression final : public Expression {
  499. public:
  500. LogicalExpression(SourceRange source_range, LogicalOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  501. : Expression(source_range)
  502. , m_op(op)
  503. , m_lhs(move(lhs))
  504. , m_rhs(move(rhs))
  505. {
  506. }
  507. virtual Value execute(Interpreter&, GlobalObject&) const override;
  508. virtual void dump(int indent) const override;
  509. virtual void generate_bytecode(Bytecode::Generator&) const override;
  510. private:
  511. LogicalOp m_op;
  512. NonnullRefPtr<Expression> m_lhs;
  513. NonnullRefPtr<Expression> m_rhs;
  514. };
  515. enum class UnaryOp {
  516. BitwiseNot,
  517. Not,
  518. Plus,
  519. Minus,
  520. Typeof,
  521. Void,
  522. Delete,
  523. };
  524. class UnaryExpression final : public Expression {
  525. public:
  526. UnaryExpression(SourceRange source_range, UnaryOp op, NonnullRefPtr<Expression> lhs)
  527. : Expression(source_range)
  528. , m_op(op)
  529. , m_lhs(move(lhs))
  530. {
  531. }
  532. virtual Value execute(Interpreter&, GlobalObject&) const override;
  533. virtual void dump(int indent) const override;
  534. virtual void generate_bytecode(Bytecode::Generator&) const override;
  535. private:
  536. UnaryOp m_op;
  537. NonnullRefPtr<Expression> m_lhs;
  538. };
  539. class SequenceExpression final : public Expression {
  540. public:
  541. SequenceExpression(SourceRange source_range, NonnullRefPtrVector<Expression> expressions)
  542. : Expression(source_range)
  543. , m_expressions(move(expressions))
  544. {
  545. VERIFY(m_expressions.size() >= 2);
  546. }
  547. virtual void dump(int indent) const override;
  548. virtual Value execute(Interpreter&, GlobalObject&) const override;
  549. virtual void generate_bytecode(Bytecode::Generator&) const override;
  550. private:
  551. NonnullRefPtrVector<Expression> m_expressions;
  552. };
  553. class Literal : public Expression {
  554. protected:
  555. explicit Literal(SourceRange source_range)
  556. : Expression(source_range)
  557. {
  558. }
  559. };
  560. class BooleanLiteral final : public Literal {
  561. public:
  562. explicit BooleanLiteral(SourceRange source_range, bool value)
  563. : Literal(source_range)
  564. , m_value(value)
  565. {
  566. }
  567. virtual Value execute(Interpreter&, GlobalObject&) const override;
  568. virtual void dump(int indent) const override;
  569. virtual void generate_bytecode(Bytecode::Generator&) const override;
  570. private:
  571. bool m_value { false };
  572. };
  573. class NumericLiteral final : public Literal {
  574. public:
  575. explicit NumericLiteral(SourceRange source_range, double value)
  576. : Literal(source_range)
  577. , m_value(value)
  578. {
  579. }
  580. virtual Value execute(Interpreter&, GlobalObject&) const override;
  581. virtual void dump(int indent) const override;
  582. virtual void generate_bytecode(Bytecode::Generator&) const override;
  583. private:
  584. Value m_value;
  585. };
  586. class BigIntLiteral final : public Literal {
  587. public:
  588. explicit BigIntLiteral(SourceRange source_range, String value)
  589. : Literal(source_range)
  590. , m_value(move(value))
  591. {
  592. }
  593. virtual Value execute(Interpreter&, GlobalObject&) const override;
  594. virtual void dump(int indent) const override;
  595. virtual void generate_bytecode(Bytecode::Generator&) const override;
  596. private:
  597. String m_value;
  598. };
  599. class StringLiteral final : public Literal {
  600. public:
  601. explicit StringLiteral(SourceRange source_range, String value, bool is_use_strict_directive = false)
  602. : Literal(source_range)
  603. , m_value(move(value))
  604. , m_is_use_strict_directive(is_use_strict_directive)
  605. {
  606. }
  607. virtual Value execute(Interpreter&, GlobalObject&) const override;
  608. virtual void dump(int indent) const override;
  609. virtual void generate_bytecode(Bytecode::Generator&) const override;
  610. StringView value() const { return m_value; }
  611. bool is_use_strict_directive() const { return m_is_use_strict_directive; };
  612. private:
  613. String m_value;
  614. bool m_is_use_strict_directive;
  615. };
  616. class NullLiteral final : public Literal {
  617. public:
  618. explicit NullLiteral(SourceRange source_range)
  619. : Literal(source_range)
  620. {
  621. }
  622. virtual Value execute(Interpreter&, GlobalObject&) const override;
  623. virtual void dump(int indent) const override;
  624. virtual void generate_bytecode(Bytecode::Generator&) const override;
  625. };
  626. class RegExpLiteral final : public Literal {
  627. public:
  628. explicit RegExpLiteral(SourceRange source_range, String pattern, String flags)
  629. : Literal(source_range)
  630. , m_pattern(move(pattern))
  631. , m_flags(move(flags))
  632. {
  633. }
  634. virtual Value execute(Interpreter&, GlobalObject&) const override;
  635. virtual void dump(int indent) const override;
  636. virtual void generate_bytecode(Bytecode::Generator&) const override;
  637. String const& pattern() const { return m_pattern; }
  638. String const& flags() const { return m_flags; }
  639. private:
  640. String m_pattern;
  641. String m_flags;
  642. };
  643. class Identifier final : public Expression {
  644. public:
  645. explicit Identifier(SourceRange source_range, FlyString string)
  646. : Expression(source_range)
  647. , m_string(move(string))
  648. {
  649. }
  650. FlyString const& string() const { return m_string; }
  651. virtual Value execute(Interpreter&, GlobalObject&) const override;
  652. virtual void dump(int indent) const override;
  653. virtual Reference to_reference(Interpreter&, GlobalObject&) const override;
  654. virtual void generate_bytecode(Bytecode::Generator&) const override;
  655. private:
  656. virtual bool is_identifier() const override { return true; }
  657. FlyString m_string;
  658. };
  659. class ClassMethod final : public ASTNode {
  660. public:
  661. enum class Kind {
  662. Method,
  663. Getter,
  664. Setter,
  665. };
  666. ClassMethod(SourceRange source_range, NonnullRefPtr<Expression> key, NonnullRefPtr<FunctionExpression> function, Kind kind, bool is_static)
  667. : ASTNode(source_range)
  668. , m_key(move(key))
  669. , m_function(move(function))
  670. , m_kind(kind)
  671. , m_is_static(is_static)
  672. {
  673. }
  674. Expression const& key() const { return *m_key; }
  675. Kind kind() const { return m_kind; }
  676. bool is_static() const { return m_is_static; }
  677. virtual Value execute(Interpreter&, GlobalObject&) const override;
  678. virtual void dump(int indent) const override;
  679. private:
  680. NonnullRefPtr<Expression> m_key;
  681. NonnullRefPtr<FunctionExpression> m_function;
  682. Kind m_kind;
  683. bool m_is_static;
  684. };
  685. class SuperExpression final : public Expression {
  686. public:
  687. explicit SuperExpression(SourceRange source_range)
  688. : Expression(source_range)
  689. {
  690. }
  691. virtual Value execute(Interpreter&, GlobalObject&) const override;
  692. virtual void dump(int indent) const override;
  693. virtual bool is_super_expression() const override { return true; }
  694. };
  695. class ClassExpression final : public Expression {
  696. public:
  697. ClassExpression(SourceRange source_range, String name, RefPtr<FunctionExpression> constructor, RefPtr<Expression> super_class, NonnullRefPtrVector<ClassMethod> methods)
  698. : Expression(source_range)
  699. , m_name(move(name))
  700. , m_constructor(move(constructor))
  701. , m_super_class(move(super_class))
  702. , m_methods(move(methods))
  703. {
  704. }
  705. StringView name() const { return m_name; }
  706. virtual Value execute(Interpreter&, GlobalObject&) const override;
  707. virtual void dump(int indent) const override;
  708. private:
  709. String m_name;
  710. RefPtr<FunctionExpression> m_constructor;
  711. RefPtr<Expression> m_super_class;
  712. NonnullRefPtrVector<ClassMethod> m_methods;
  713. };
  714. class ClassDeclaration final : public Declaration {
  715. public:
  716. ClassDeclaration(SourceRange source_range, NonnullRefPtr<ClassExpression> class_expression)
  717. : Declaration(source_range)
  718. , m_class_expression(move(class_expression))
  719. {
  720. }
  721. virtual Value execute(Interpreter&, GlobalObject&) const override;
  722. virtual void dump(int indent) const override;
  723. private:
  724. NonnullRefPtr<ClassExpression> m_class_expression;
  725. };
  726. class SpreadExpression final : public Expression {
  727. public:
  728. explicit SpreadExpression(SourceRange source_range, NonnullRefPtr<Expression> target)
  729. : Expression(source_range)
  730. , m_target(move(target))
  731. {
  732. }
  733. virtual Value execute(Interpreter&, GlobalObject&) const override;
  734. virtual void dump(int indent) const override;
  735. private:
  736. NonnullRefPtr<Expression> m_target;
  737. };
  738. class ThisExpression final : public Expression {
  739. public:
  740. explicit ThisExpression(SourceRange source_range)
  741. : Expression(source_range)
  742. {
  743. }
  744. virtual Value execute(Interpreter&, GlobalObject&) const override;
  745. virtual void dump(int indent) const override;
  746. };
  747. class CallExpression : public Expression {
  748. public:
  749. struct Argument {
  750. NonnullRefPtr<Expression> value;
  751. bool is_spread;
  752. };
  753. CallExpression(SourceRange source_range, NonnullRefPtr<Expression> callee, Vector<Argument> arguments = {})
  754. : Expression(source_range)
  755. , m_callee(move(callee))
  756. , m_arguments(move(arguments))
  757. {
  758. }
  759. virtual Value execute(Interpreter&, GlobalObject&) const override;
  760. virtual void dump(int indent) const override;
  761. virtual void generate_bytecode(Bytecode::Generator&) const override;
  762. private:
  763. struct ThisAndCallee {
  764. Value this_value;
  765. Value callee;
  766. };
  767. ThisAndCallee compute_this_and_callee(Interpreter&, GlobalObject&) const;
  768. NonnullRefPtr<Expression> m_callee;
  769. Vector<Argument> const m_arguments;
  770. };
  771. class NewExpression final : public CallExpression {
  772. public:
  773. NewExpression(SourceRange source_range, NonnullRefPtr<Expression> callee, Vector<Argument> arguments = {})
  774. : CallExpression(source_range, move(callee), move(arguments))
  775. {
  776. }
  777. virtual bool is_new_expression() const override { return true; }
  778. };
  779. enum class AssignmentOp {
  780. Assignment,
  781. AdditionAssignment,
  782. SubtractionAssignment,
  783. MultiplicationAssignment,
  784. DivisionAssignment,
  785. ModuloAssignment,
  786. ExponentiationAssignment,
  787. BitwiseAndAssignment,
  788. BitwiseOrAssignment,
  789. BitwiseXorAssignment,
  790. LeftShiftAssignment,
  791. RightShiftAssignment,
  792. UnsignedRightShiftAssignment,
  793. AndAssignment,
  794. OrAssignment,
  795. NullishAssignment,
  796. };
  797. class AssignmentExpression final : public Expression {
  798. public:
  799. AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  800. : Expression(source_range)
  801. , m_op(op)
  802. , m_lhs(move(lhs))
  803. , m_rhs(move(rhs))
  804. {
  805. }
  806. virtual Value execute(Interpreter&, GlobalObject&) const override;
  807. virtual void dump(int indent) const override;
  808. virtual void generate_bytecode(Bytecode::Generator&) const override;
  809. private:
  810. AssignmentOp m_op;
  811. NonnullRefPtr<Expression> m_lhs;
  812. NonnullRefPtr<Expression> m_rhs;
  813. };
  814. enum class UpdateOp {
  815. Increment,
  816. Decrement,
  817. };
  818. class UpdateExpression final : public Expression {
  819. public:
  820. UpdateExpression(SourceRange source_range, UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false)
  821. : Expression(source_range)
  822. , m_op(op)
  823. , m_argument(move(argument))
  824. , m_prefixed(prefixed)
  825. {
  826. }
  827. virtual Value execute(Interpreter&, GlobalObject&) const override;
  828. virtual void dump(int indent) const override;
  829. virtual void generate_bytecode(Bytecode::Generator&) const override;
  830. private:
  831. UpdateOp m_op;
  832. NonnullRefPtr<Expression> m_argument;
  833. bool m_prefixed;
  834. };
  835. enum class DeclarationKind {
  836. Var,
  837. Let,
  838. Const,
  839. };
  840. class VariableDeclarator final : public ASTNode {
  841. public:
  842. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier> id)
  843. : ASTNode(source_range)
  844. , m_target(move(id))
  845. {
  846. }
  847. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier> target, RefPtr<Expression> init)
  848. : ASTNode(source_range)
  849. , m_target(move(target))
  850. , m_init(move(init))
  851. {
  852. }
  853. VariableDeclarator(SourceRange source_range, Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>> target, RefPtr<Expression> init)
  854. : ASTNode(source_range)
  855. , m_target(move(target))
  856. , m_init(move(init))
  857. {
  858. }
  859. auto& target() const { return m_target; }
  860. Expression const* init() const { return m_init; }
  861. virtual Value execute(Interpreter&, GlobalObject&) const override;
  862. virtual void dump(int indent) const override;
  863. private:
  864. Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>> m_target;
  865. RefPtr<Expression> m_init;
  866. };
  867. class VariableDeclaration final : public Declaration {
  868. public:
  869. VariableDeclaration(SourceRange source_range, DeclarationKind declaration_kind, NonnullRefPtrVector<VariableDeclarator> declarations)
  870. : Declaration(source_range)
  871. , m_declaration_kind(declaration_kind)
  872. , m_declarations(move(declarations))
  873. {
  874. }
  875. DeclarationKind declaration_kind() const { return m_declaration_kind; }
  876. virtual Value execute(Interpreter&, GlobalObject&) const override;
  877. virtual void dump(int indent) const override;
  878. virtual void generate_bytecode(Bytecode::Generator&) const override;
  879. NonnullRefPtrVector<VariableDeclarator> const& declarations() const { return m_declarations; }
  880. private:
  881. DeclarationKind m_declaration_kind;
  882. NonnullRefPtrVector<VariableDeclarator> m_declarations;
  883. };
  884. class ObjectProperty final : public ASTNode {
  885. public:
  886. enum class Type {
  887. KeyValue,
  888. Getter,
  889. Setter,
  890. Spread,
  891. };
  892. ObjectProperty(SourceRange source_range, NonnullRefPtr<Expression> key, RefPtr<Expression> value, Type property_type, bool is_method)
  893. : ASTNode(source_range)
  894. , m_key(move(key))
  895. , m_value(move(value))
  896. , m_property_type(property_type)
  897. , m_is_method(is_method)
  898. {
  899. }
  900. Expression const& key() const { return m_key; }
  901. Expression const& value() const
  902. {
  903. VERIFY(m_value);
  904. return *m_value;
  905. }
  906. Type type() const { return m_property_type; }
  907. bool is_method() const { return m_is_method; }
  908. virtual void dump(int indent) const override;
  909. virtual Value execute(Interpreter&, GlobalObject&) const override;
  910. private:
  911. NonnullRefPtr<Expression> m_key;
  912. RefPtr<Expression> m_value;
  913. Type m_property_type;
  914. bool m_is_method { false };
  915. };
  916. class ObjectExpression final : public Expression {
  917. public:
  918. explicit ObjectExpression(SourceRange source_range, NonnullRefPtrVector<ObjectProperty> properties = {})
  919. : Expression(source_range)
  920. , m_properties(move(properties))
  921. {
  922. }
  923. virtual Value execute(Interpreter&, GlobalObject&) const override;
  924. virtual void dump(int indent) const override;
  925. virtual void generate_bytecode(Bytecode::Generator&) const override;
  926. private:
  927. NonnullRefPtrVector<ObjectProperty> m_properties;
  928. };
  929. class ArrayExpression final : public Expression {
  930. public:
  931. ArrayExpression(SourceRange source_range, Vector<RefPtr<Expression>> elements)
  932. : Expression(source_range)
  933. , m_elements(move(elements))
  934. {
  935. }
  936. Vector<RefPtr<Expression>> const& elements() const { return m_elements; }
  937. virtual Value execute(Interpreter&, GlobalObject&) const override;
  938. virtual void dump(int indent) const override;
  939. virtual void generate_bytecode(Bytecode::Generator&) const override;
  940. private:
  941. Vector<RefPtr<Expression>> m_elements;
  942. };
  943. class TemplateLiteral final : public Expression {
  944. public:
  945. TemplateLiteral(SourceRange source_range, NonnullRefPtrVector<Expression> expressions)
  946. : Expression(source_range)
  947. , m_expressions(move(expressions))
  948. {
  949. }
  950. TemplateLiteral(SourceRange source_range, NonnullRefPtrVector<Expression> expressions, NonnullRefPtrVector<Expression> raw_strings)
  951. : Expression(source_range)
  952. , m_expressions(move(expressions))
  953. , m_raw_strings(move(raw_strings))
  954. {
  955. }
  956. virtual Value execute(Interpreter&, GlobalObject&) const override;
  957. virtual void dump(int indent) const override;
  958. virtual void generate_bytecode(Bytecode::Generator&) const override;
  959. NonnullRefPtrVector<Expression> const& expressions() const { return m_expressions; }
  960. NonnullRefPtrVector<Expression> const& raw_strings() const { return m_raw_strings; }
  961. private:
  962. NonnullRefPtrVector<Expression> const m_expressions;
  963. NonnullRefPtrVector<Expression> const m_raw_strings;
  964. };
  965. class TaggedTemplateLiteral final : public Expression {
  966. public:
  967. TaggedTemplateLiteral(SourceRange source_range, NonnullRefPtr<Expression> tag, NonnullRefPtr<TemplateLiteral> template_literal)
  968. : Expression(source_range)
  969. , m_tag(move(tag))
  970. , m_template_literal(move(template_literal))
  971. {
  972. }
  973. virtual Value execute(Interpreter&, GlobalObject&) const override;
  974. virtual void dump(int indent) const override;
  975. virtual void generate_bytecode(Bytecode::Generator&) const override;
  976. private:
  977. NonnullRefPtr<Expression> const m_tag;
  978. NonnullRefPtr<TemplateLiteral> const m_template_literal;
  979. };
  980. class MemberExpression final : public Expression {
  981. public:
  982. MemberExpression(SourceRange source_range, NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false)
  983. : Expression(source_range)
  984. , m_object(move(object))
  985. , m_property(move(property))
  986. , m_computed(computed)
  987. {
  988. }
  989. virtual Value execute(Interpreter&, GlobalObject&) const override;
  990. virtual void dump(int indent) const override;
  991. virtual Reference to_reference(Interpreter&, GlobalObject&) const override;
  992. virtual void generate_bytecode(Bytecode::Generator&) const override;
  993. bool is_computed() const { return m_computed; }
  994. Expression const& object() const { return *m_object; }
  995. Expression const& property() const { return *m_property; }
  996. PropertyName computed_property_name(Interpreter&, GlobalObject&) const;
  997. String to_string_approximation() const;
  998. private:
  999. virtual bool is_member_expression() const override { return true; }
  1000. NonnullRefPtr<Expression> m_object;
  1001. NonnullRefPtr<Expression> m_property;
  1002. bool m_computed { false };
  1003. };
  1004. class MetaProperty final : public Expression {
  1005. public:
  1006. enum class Type {
  1007. NewTarget,
  1008. ImportMeta,
  1009. };
  1010. MetaProperty(SourceRange source_range, Type type)
  1011. : Expression(source_range)
  1012. , m_type(type)
  1013. {
  1014. }
  1015. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1016. virtual void dump(int indent) const override;
  1017. private:
  1018. Type m_type;
  1019. };
  1020. class ConditionalExpression final : public Expression {
  1021. public:
  1022. ConditionalExpression(SourceRange source_range, NonnullRefPtr<Expression> test, NonnullRefPtr<Expression> consequent, NonnullRefPtr<Expression> alternate)
  1023. : Expression(source_range)
  1024. , m_test(move(test))
  1025. , m_consequent(move(consequent))
  1026. , m_alternate(move(alternate))
  1027. {
  1028. }
  1029. virtual void dump(int indent) const override;
  1030. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1031. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1032. private:
  1033. NonnullRefPtr<Expression> m_test;
  1034. NonnullRefPtr<Expression> m_consequent;
  1035. NonnullRefPtr<Expression> m_alternate;
  1036. };
  1037. class CatchClause final : public ASTNode {
  1038. public:
  1039. CatchClause(SourceRange source_range, FlyString parameter, NonnullRefPtr<BlockStatement> body)
  1040. : ASTNode(source_range)
  1041. , m_parameter(move(parameter))
  1042. , m_body(move(body))
  1043. {
  1044. }
  1045. FlyString const& parameter() const { return m_parameter; }
  1046. BlockStatement const& body() const { return m_body; }
  1047. virtual void dump(int indent) const override;
  1048. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1049. private:
  1050. FlyString m_parameter;
  1051. NonnullRefPtr<BlockStatement> m_body;
  1052. };
  1053. class TryStatement final : public Statement {
  1054. public:
  1055. TryStatement(SourceRange source_range, NonnullRefPtr<BlockStatement> block, RefPtr<CatchClause> handler, RefPtr<BlockStatement> finalizer)
  1056. : Statement(source_range)
  1057. , m_block(move(block))
  1058. , m_handler(move(handler))
  1059. , m_finalizer(move(finalizer))
  1060. {
  1061. }
  1062. BlockStatement const& block() const { return m_block; }
  1063. CatchClause const* handler() const { return m_handler; }
  1064. BlockStatement const* finalizer() const { return m_finalizer; }
  1065. virtual void dump(int indent) const override;
  1066. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1067. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1068. private:
  1069. NonnullRefPtr<BlockStatement> m_block;
  1070. RefPtr<CatchClause> m_handler;
  1071. RefPtr<BlockStatement> m_finalizer;
  1072. };
  1073. class ThrowStatement final : public Statement {
  1074. public:
  1075. explicit ThrowStatement(SourceRange source_range, NonnullRefPtr<Expression> argument)
  1076. : Statement(source_range)
  1077. , m_argument(move(argument))
  1078. {
  1079. }
  1080. Expression const& argument() const { return m_argument; }
  1081. virtual void dump(int indent) const override;
  1082. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1083. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1084. private:
  1085. NonnullRefPtr<Expression> m_argument;
  1086. };
  1087. class SwitchCase final : public ASTNode {
  1088. public:
  1089. SwitchCase(SourceRange source_range, RefPtr<Expression> test, NonnullRefPtrVector<Statement> consequent)
  1090. : ASTNode(source_range)
  1091. , m_test(move(test))
  1092. , m_consequent(move(consequent))
  1093. {
  1094. }
  1095. Expression const* test() const { return m_test; }
  1096. NonnullRefPtrVector<Statement> const& consequent() const { return m_consequent; }
  1097. virtual void dump(int indent) const override;
  1098. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1099. private:
  1100. RefPtr<Expression> m_test;
  1101. NonnullRefPtrVector<Statement> m_consequent;
  1102. };
  1103. class SwitchStatement final : public Statement {
  1104. public:
  1105. SwitchStatement(SourceRange source_range, NonnullRefPtr<Expression> discriminant, NonnullRefPtrVector<SwitchCase> cases)
  1106. : Statement(source_range)
  1107. , m_discriminant(move(discriminant))
  1108. , m_cases(move(cases))
  1109. {
  1110. }
  1111. virtual void dump(int indent) const override;
  1112. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1113. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1114. private:
  1115. NonnullRefPtr<Expression> m_discriminant;
  1116. NonnullRefPtrVector<SwitchCase> m_cases;
  1117. };
  1118. class BreakStatement final : public Statement {
  1119. public:
  1120. BreakStatement(SourceRange source_range, FlyString target_label)
  1121. : Statement(source_range)
  1122. , m_target_label(move(target_label))
  1123. {
  1124. }
  1125. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1126. FlyString const& target_label() const { return m_target_label; }
  1127. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1128. private:
  1129. FlyString m_target_label;
  1130. };
  1131. class ContinueStatement final : public Statement {
  1132. public:
  1133. ContinueStatement(SourceRange source_range, FlyString target_label)
  1134. : Statement(source_range)
  1135. , m_target_label(move(target_label))
  1136. {
  1137. }
  1138. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1139. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1140. FlyString const& target_label() const { return m_target_label; }
  1141. private:
  1142. FlyString m_target_label;
  1143. };
  1144. class DebuggerStatement final : public Statement {
  1145. public:
  1146. explicit DebuggerStatement(SourceRange source_range)
  1147. : Statement(source_range)
  1148. {
  1149. }
  1150. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1151. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1152. };
  1153. template<typename C>
  1154. void BindingPattern::for_each_bound_name(C&& callback) const
  1155. {
  1156. for (auto& entry : entries) {
  1157. auto& alias = entry.alias;
  1158. if (alias.has<NonnullRefPtr<Identifier>>()) {
  1159. callback(alias.get<NonnullRefPtr<Identifier>>()->string());
  1160. } else if (alias.has<NonnullRefPtr<BindingPattern>>()) {
  1161. alias.get<NonnullRefPtr<BindingPattern>>()->for_each_bound_name(forward<C>(callback));
  1162. }
  1163. }
  1164. }
  1165. template<>
  1166. inline bool ASTNode::fast_is<NewExpression>() const { return is_new_expression(); }
  1167. template<>
  1168. inline bool ASTNode::fast_is<MemberExpression>() const { return is_member_expression(); }
  1169. template<>
  1170. inline bool ASTNode::fast_is<SuperExpression>() const { return is_super_expression(); }
  1171. template<>
  1172. inline bool ASTNode::fast_is<Identifier>() const { return is_identifier(); }
  1173. template<>
  1174. inline bool ASTNode::fast_is<ExpressionStatement>() const { return is_expression_statement(); }
  1175. template<>
  1176. inline bool ASTNode::fast_is<ScopeNode>() const { return is_scope_node(); }
  1177. template<>
  1178. inline bool ASTNode::fast_is<Program>() const { return is_program(); }
  1179. }