AST.h 43 KB

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