AST.h 45 KB

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