AST.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <AK/FlyString.h>
  29. #include <AK/HashMap.h>
  30. #include <AK/NonnullRefPtrVector.h>
  31. #include <AK/RefPtr.h>
  32. #include <AK/String.h>
  33. #include <AK/Vector.h>
  34. #include <LibJS/Forward.h>
  35. #include <LibJS/Runtime/PropertyName.h>
  36. #include <LibJS/Runtime/Value.h>
  37. namespace JS {
  38. class VariableDeclaration;
  39. class FunctionDeclaration;
  40. template<class T, class... Args>
  41. static inline NonnullRefPtr<T>
  42. create_ast_node(Args&&... args)
  43. {
  44. return adopt(*new T(forward<Args>(args)...));
  45. }
  46. class ASTNode : public RefCounted<ASTNode> {
  47. public:
  48. virtual ~ASTNode() { }
  49. virtual const char* class_name() const = 0;
  50. virtual Value execute(Interpreter&, GlobalObject&) const = 0;
  51. virtual void dump(int indent) const;
  52. virtual bool is_identifier() const { return false; }
  53. virtual bool is_spread_expression() const { return false; }
  54. virtual bool is_member_expression() const { return false; }
  55. virtual bool is_scope_node() const { return false; }
  56. virtual bool is_program() const { return false; }
  57. virtual bool is_variable_declaration() const { return false; }
  58. virtual bool is_call_expression() const { return false; }
  59. virtual bool is_new_expression() const { return false; }
  60. protected:
  61. ASTNode() { }
  62. private:
  63. };
  64. class Statement : public ASTNode {
  65. public:
  66. const FlyString& label() const { return m_label; }
  67. void set_label(FlyString string) { m_label = string; }
  68. protected:
  69. FlyString m_label;
  70. };
  71. class EmptyStatement final : public Statement {
  72. public:
  73. Value execute(Interpreter&, GlobalObject&) const override { return js_undefined(); }
  74. const char* class_name() const override { return "EmptyStatement"; }
  75. };
  76. class ErrorStatement final : public Statement {
  77. public:
  78. Value execute(Interpreter&, GlobalObject&) const override { return js_undefined(); }
  79. const char* class_name() const override { return "ErrorStatement"; }
  80. };
  81. class ExpressionStatement final : public Statement {
  82. public:
  83. ExpressionStatement(NonnullRefPtr<Expression> expression)
  84. : m_expression(move(expression))
  85. {
  86. }
  87. Value execute(Interpreter&, GlobalObject&) const override;
  88. const char* class_name() const override { return "ExpressionStatement"; }
  89. virtual void dump(int indent) const override;
  90. private:
  91. NonnullRefPtr<Expression> m_expression;
  92. };
  93. class ScopeNode : public Statement {
  94. public:
  95. template<typename T, typename... Args>
  96. T& append(Args&&... args)
  97. {
  98. auto child = create_ast_node<T>(forward<Args>(args)...);
  99. m_children.append(move(child));
  100. return static_cast<T&>(m_children.last());
  101. }
  102. void append(NonnullRefPtr<Statement> child)
  103. {
  104. m_children.append(move(child));
  105. }
  106. const NonnullRefPtrVector<Statement>& children() const { return m_children; }
  107. virtual Value execute(Interpreter&, GlobalObject&) const override;
  108. virtual void dump(int indent) const override;
  109. void add_variables(NonnullRefPtrVector<VariableDeclaration>);
  110. void add_functions(NonnullRefPtrVector<FunctionDeclaration>);
  111. const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; }
  112. const NonnullRefPtrVector<FunctionDeclaration>& functions() const { return m_functions; }
  113. bool in_strict_mode() const { return m_strict_mode; }
  114. void set_strict_mode() { m_strict_mode = true; }
  115. protected:
  116. ScopeNode() { }
  117. private:
  118. virtual bool is_scope_node() const final { return true; }
  119. NonnullRefPtrVector<Statement> m_children;
  120. NonnullRefPtrVector<VariableDeclaration> m_variables;
  121. NonnullRefPtrVector<FunctionDeclaration> m_functions;
  122. bool m_strict_mode { false };
  123. };
  124. class Program : public ScopeNode {
  125. public:
  126. Program() { }
  127. private:
  128. virtual bool is_program() const override { return true; }
  129. virtual const char* class_name() const override { return "Program"; }
  130. };
  131. class BlockStatement : public ScopeNode {
  132. public:
  133. BlockStatement() { }
  134. private:
  135. virtual const char* class_name() const override { return "BlockStatement"; }
  136. };
  137. class Expression : public ASTNode {
  138. public:
  139. virtual Reference to_reference(Interpreter&, GlobalObject&) const;
  140. };
  141. class Declaration : public Statement {
  142. };
  143. class FunctionNode {
  144. public:
  145. struct Parameter {
  146. FlyString name;
  147. RefPtr<Expression> default_value;
  148. bool is_rest { false };
  149. };
  150. const FlyString& name() const { return m_name; }
  151. const Statement& body() const { return *m_body; }
  152. const Vector<Parameter>& parameters() const { return m_parameters; };
  153. i32 function_length() const { return m_function_length; }
  154. protected:
  155. FunctionNode(const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables)
  156. : m_name(name)
  157. , m_body(move(body))
  158. , m_parameters(move(parameters))
  159. , m_variables(move(variables))
  160. , m_function_length(function_length)
  161. {
  162. }
  163. void dump(int indent, const char* class_name) const;
  164. const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; }
  165. private:
  166. FlyString m_name;
  167. NonnullRefPtr<Statement> m_body;
  168. const Vector<Parameter> m_parameters;
  169. NonnullRefPtrVector<VariableDeclaration> m_variables;
  170. const i32 m_function_length;
  171. };
  172. class FunctionDeclaration final
  173. : public Declaration
  174. , public FunctionNode {
  175. public:
  176. static bool must_have_name() { return true; }
  177. FunctionDeclaration(const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables)
  178. : FunctionNode(name, move(body), move(parameters), function_length, move(variables))
  179. {
  180. }
  181. virtual Value execute(Interpreter&, GlobalObject&) const override;
  182. virtual void dump(int indent) const override;
  183. private:
  184. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  185. };
  186. class FunctionExpression final
  187. : public Expression
  188. , public FunctionNode {
  189. public:
  190. static bool must_have_name() { return false; }
  191. FunctionExpression(const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables, bool is_arrow_function = false)
  192. : FunctionNode(name, move(body), move(parameters), function_length, move(variables))
  193. , m_is_arrow_function(is_arrow_function)
  194. {
  195. }
  196. virtual Value execute(Interpreter&, GlobalObject&) const override;
  197. virtual void dump(int indent) const override;
  198. private:
  199. virtual const char* class_name() const override { return "FunctionExpression"; }
  200. bool m_is_arrow_function;
  201. };
  202. class ErrorExpression final : public Expression {
  203. public:
  204. Value execute(Interpreter&, GlobalObject&) const override { return js_undefined(); }
  205. const char* class_name() const override { return "ErrorExpression"; }
  206. };
  207. class ReturnStatement : public Statement {
  208. public:
  209. explicit ReturnStatement(RefPtr<Expression> argument)
  210. : m_argument(move(argument))
  211. {
  212. }
  213. const Expression* argument() const { return m_argument; }
  214. virtual Value execute(Interpreter&, GlobalObject&) const override;
  215. virtual void dump(int indent) const override;
  216. private:
  217. virtual const char* class_name() const override { return "ReturnStatement"; }
  218. RefPtr<Expression> m_argument;
  219. };
  220. class IfStatement : public Statement {
  221. public:
  222. IfStatement(NonnullRefPtr<Expression> predicate, NonnullRefPtr<Statement> consequent, RefPtr<Statement> alternate)
  223. : m_predicate(move(predicate))
  224. , m_consequent(move(consequent))
  225. , m_alternate(move(alternate))
  226. {
  227. }
  228. const Expression& predicate() const { return *m_predicate; }
  229. const Statement& consequent() const { return *m_consequent; }
  230. const Statement* alternate() const { return m_alternate; }
  231. virtual Value execute(Interpreter&, GlobalObject&) const override;
  232. virtual void dump(int indent) const override;
  233. private:
  234. virtual const char* class_name() const override { return "IfStatement"; }
  235. NonnullRefPtr<Expression> m_predicate;
  236. NonnullRefPtr<Statement> m_consequent;
  237. RefPtr<Statement> m_alternate;
  238. };
  239. class WhileStatement : public Statement {
  240. public:
  241. WhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  242. : m_test(move(test))
  243. , m_body(move(body))
  244. {
  245. }
  246. const Expression& test() const { return *m_test; }
  247. const Statement& body() const { return *m_body; }
  248. virtual Value execute(Interpreter&, GlobalObject&) const override;
  249. virtual void dump(int indent) const override;
  250. private:
  251. virtual const char* class_name() const override { return "WhileStatement"; }
  252. NonnullRefPtr<Expression> m_test;
  253. NonnullRefPtr<Statement> m_body;
  254. };
  255. class DoWhileStatement : public Statement {
  256. public:
  257. DoWhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  258. : m_test(move(test))
  259. , m_body(move(body))
  260. {
  261. }
  262. const Expression& test() const { return *m_test; }
  263. const Statement& body() const { return *m_body; }
  264. virtual Value execute(Interpreter&, GlobalObject&) const override;
  265. virtual void dump(int indent) const override;
  266. private:
  267. virtual const char* class_name() const override { return "DoWhileStatement"; }
  268. NonnullRefPtr<Expression> m_test;
  269. NonnullRefPtr<Statement> m_body;
  270. };
  271. class ForStatement : public Statement {
  272. public:
  273. ForStatement(RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<Statement> body)
  274. : m_init(move(init))
  275. , m_test(move(test))
  276. , m_update(move(update))
  277. , m_body(move(body))
  278. {
  279. }
  280. const ASTNode* init() const { return m_init; }
  281. const Expression* test() const { return m_test; }
  282. const Expression* update() const { return m_update; }
  283. const Statement& body() const { return *m_body; }
  284. virtual Value execute(Interpreter&, GlobalObject&) const override;
  285. virtual void dump(int indent) const override;
  286. private:
  287. virtual const char* class_name() const override { return "ForStatement"; }
  288. RefPtr<ASTNode> m_init;
  289. RefPtr<Expression> m_test;
  290. RefPtr<Expression> m_update;
  291. NonnullRefPtr<Statement> m_body;
  292. };
  293. class ForInStatement : public Statement {
  294. public:
  295. ForInStatement(NonnullRefPtr<ASTNode> lhs, NonnullRefPtr<Expression> rhs, NonnullRefPtr<Statement> body)
  296. : m_lhs(move(lhs))
  297. , m_rhs(move(rhs))
  298. , m_body(move(body))
  299. {
  300. }
  301. const ASTNode& lhs() const { return *m_lhs; }
  302. const Expression& rhs() const { return *m_rhs; }
  303. const Statement& body() const { return *m_body; }
  304. virtual Value execute(Interpreter&, GlobalObject&) const override;
  305. virtual void dump(int indent) const override;
  306. private:
  307. virtual const char* class_name() const override { return "ForInStatement"; }
  308. NonnullRefPtr<ASTNode> m_lhs;
  309. NonnullRefPtr<Expression> m_rhs;
  310. NonnullRefPtr<Statement> m_body;
  311. };
  312. class ForOfStatement : public Statement {
  313. public:
  314. ForOfStatement(NonnullRefPtr<ASTNode> lhs, NonnullRefPtr<Expression> rhs, NonnullRefPtr<Statement> body)
  315. : m_lhs(move(lhs))
  316. , m_rhs(move(rhs))
  317. , m_body(move(body))
  318. {
  319. }
  320. const ASTNode& lhs() const { return *m_lhs; }
  321. const Expression& rhs() const { return *m_rhs; }
  322. const Statement& body() const { return *m_body; }
  323. virtual Value execute(Interpreter&, GlobalObject&) const override;
  324. virtual void dump(int indent) const override;
  325. private:
  326. virtual const char* class_name() const override { return "ForOfStatement"; }
  327. NonnullRefPtr<ASTNode> m_lhs;
  328. NonnullRefPtr<Expression> m_rhs;
  329. NonnullRefPtr<Statement> m_body;
  330. };
  331. enum class BinaryOp {
  332. Addition,
  333. Subtraction,
  334. Multiplication,
  335. Division,
  336. Modulo,
  337. Exponentiation,
  338. TypedEquals,
  339. TypedInequals,
  340. AbstractEquals,
  341. AbstractInequals,
  342. GreaterThan,
  343. GreaterThanEquals,
  344. LessThan,
  345. LessThanEquals,
  346. BitwiseAnd,
  347. BitwiseOr,
  348. BitwiseXor,
  349. LeftShift,
  350. RightShift,
  351. UnsignedRightShift,
  352. In,
  353. InstanceOf,
  354. };
  355. class BinaryExpression : public Expression {
  356. public:
  357. BinaryExpression(BinaryOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  358. : m_op(op)
  359. , m_lhs(move(lhs))
  360. , m_rhs(move(rhs))
  361. {
  362. }
  363. virtual Value execute(Interpreter&, GlobalObject&) const override;
  364. virtual void dump(int indent) const override;
  365. private:
  366. virtual const char* class_name() const override { return "BinaryExpression"; }
  367. BinaryOp m_op;
  368. NonnullRefPtr<Expression> m_lhs;
  369. NonnullRefPtr<Expression> m_rhs;
  370. };
  371. enum class LogicalOp {
  372. And,
  373. Or,
  374. NullishCoalescing,
  375. };
  376. class LogicalExpression : public Expression {
  377. public:
  378. LogicalExpression(LogicalOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  379. : m_op(op)
  380. , m_lhs(move(lhs))
  381. , m_rhs(move(rhs))
  382. {
  383. }
  384. virtual Value execute(Interpreter&, GlobalObject&) const override;
  385. virtual void dump(int indent) const override;
  386. private:
  387. virtual const char* class_name() const override { return "LogicalExpression"; }
  388. LogicalOp m_op;
  389. NonnullRefPtr<Expression> m_lhs;
  390. NonnullRefPtr<Expression> m_rhs;
  391. };
  392. enum class UnaryOp {
  393. BitwiseNot,
  394. Not,
  395. Plus,
  396. Minus,
  397. Typeof,
  398. Void,
  399. Delete,
  400. };
  401. class UnaryExpression : public Expression {
  402. public:
  403. UnaryExpression(UnaryOp op, NonnullRefPtr<Expression> lhs)
  404. : m_op(op)
  405. , m_lhs(move(lhs))
  406. {
  407. }
  408. virtual Value execute(Interpreter&, GlobalObject&) const override;
  409. virtual void dump(int indent) const override;
  410. private:
  411. virtual const char* class_name() const override { return "UnaryExpression"; }
  412. UnaryOp m_op;
  413. NonnullRefPtr<Expression> m_lhs;
  414. };
  415. class SequenceExpression final : public Expression {
  416. public:
  417. SequenceExpression(NonnullRefPtrVector<Expression> expressions)
  418. : m_expressions(move(expressions))
  419. {
  420. }
  421. virtual void dump(int indent) const override;
  422. virtual Value execute(Interpreter&, GlobalObject&) const override;
  423. private:
  424. virtual const char* class_name() const override { return "SequenceExpression"; }
  425. NonnullRefPtrVector<Expression> m_expressions;
  426. };
  427. class Literal : public Expression {
  428. protected:
  429. explicit Literal() { }
  430. };
  431. class BooleanLiteral final : public Literal {
  432. public:
  433. explicit BooleanLiteral(bool value)
  434. : m_value(value)
  435. {
  436. }
  437. virtual Value execute(Interpreter&, GlobalObject&) const override;
  438. virtual void dump(int indent) const override;
  439. private:
  440. virtual const char* class_name() const override { return "BooleanLiteral"; }
  441. bool m_value { false };
  442. };
  443. class NumericLiteral final : public Literal {
  444. public:
  445. explicit NumericLiteral(double value)
  446. : m_value(value)
  447. {
  448. }
  449. virtual Value execute(Interpreter&, GlobalObject&) const override;
  450. virtual void dump(int indent) const override;
  451. private:
  452. virtual const char* class_name() const override { return "NumericLiteral"; }
  453. double m_value { 0 };
  454. };
  455. class BigIntLiteral final : public Literal {
  456. public:
  457. explicit BigIntLiteral(String value)
  458. : m_value(move(value))
  459. {
  460. }
  461. virtual Value execute(Interpreter&, GlobalObject&) const override;
  462. virtual void dump(int indent) const override;
  463. private:
  464. virtual const char* class_name() const override { return "BigIntLiteral"; }
  465. String m_value;
  466. };
  467. class StringLiteral final : public Literal {
  468. public:
  469. explicit StringLiteral(String value)
  470. : m_value(move(value))
  471. {
  472. }
  473. virtual Value execute(Interpreter&, GlobalObject&) const override;
  474. virtual void dump(int indent) const override;
  475. private:
  476. virtual const char* class_name() const override { return "StringLiteral"; }
  477. String m_value;
  478. };
  479. class NullLiteral final : public Literal {
  480. public:
  481. explicit NullLiteral() { }
  482. virtual Value execute(Interpreter&, GlobalObject&) const override;
  483. virtual void dump(int indent) const override;
  484. private:
  485. virtual const char* class_name() const override { return "NullLiteral"; }
  486. };
  487. class RegExpLiteral final : public Literal {
  488. public:
  489. explicit RegExpLiteral(String content, String flags)
  490. : m_content(content)
  491. , m_flags(flags)
  492. {
  493. }
  494. virtual Value execute(Interpreter&, GlobalObject&) const override;
  495. virtual void dump(int indent) const override;
  496. const String& content() const { return m_content; }
  497. const String& flags() const { return m_flags; }
  498. private:
  499. virtual const char* class_name() const override { return "RegexLiteral"; }
  500. String m_content;
  501. String m_flags;
  502. };
  503. class Identifier final : public Expression {
  504. public:
  505. explicit Identifier(const FlyString& string)
  506. : m_string(string)
  507. {
  508. }
  509. const FlyString& string() const { return m_string; }
  510. virtual Value execute(Interpreter&, GlobalObject&) const override;
  511. virtual void dump(int indent) const override;
  512. virtual bool is_identifier() const override { return true; }
  513. virtual Reference to_reference(Interpreter&, GlobalObject&) const override;
  514. private:
  515. virtual const char* class_name() const override { return "Identifier"; }
  516. FlyString m_string;
  517. };
  518. class SpreadExpression final : public Expression {
  519. public:
  520. explicit SpreadExpression(NonnullRefPtr<Expression> target)
  521. : m_target(target)
  522. {
  523. }
  524. virtual Value execute(Interpreter&, GlobalObject&) const override;
  525. virtual void dump(int indent) const override;
  526. virtual bool is_spread_expression() const override { return true; }
  527. private:
  528. virtual const char* class_name() const override { return "SpreadExpression"; }
  529. NonnullRefPtr<Expression> m_target;
  530. };
  531. class ThisExpression final : public Expression {
  532. public:
  533. virtual Value execute(Interpreter&, GlobalObject&) const override;
  534. virtual void dump(int indent) const override;
  535. private:
  536. virtual const char* class_name() const override { return "ThisExpression"; }
  537. };
  538. class CallExpression : public Expression {
  539. public:
  540. struct Argument {
  541. NonnullRefPtr<Expression> value;
  542. bool is_spread;
  543. };
  544. CallExpression(NonnullRefPtr<Expression> callee, Vector<Argument> arguments = {})
  545. : m_callee(move(callee))
  546. , m_arguments(move(arguments))
  547. {
  548. }
  549. virtual Value execute(Interpreter&, GlobalObject&) const override;
  550. virtual void dump(int indent) const override;
  551. private:
  552. virtual const char* class_name() const override { return "CallExpression"; }
  553. virtual bool is_call_expression() const override { return true; }
  554. struct ThisAndCallee {
  555. Value this_value;
  556. Value callee;
  557. };
  558. ThisAndCallee compute_this_and_callee(Interpreter&, GlobalObject&) const;
  559. NonnullRefPtr<Expression> m_callee;
  560. const Vector<Argument> m_arguments;
  561. };
  562. class NewExpression final : public CallExpression {
  563. public:
  564. NewExpression(NonnullRefPtr<Expression> callee, Vector<Argument> arguments = {})
  565. : CallExpression(move(callee), move(arguments))
  566. {
  567. }
  568. private:
  569. virtual const char* class_name() const override { return "NewExpression"; }
  570. virtual bool is_call_expression() const override { return false; }
  571. virtual bool is_new_expression() const override { return true; }
  572. };
  573. enum class AssignmentOp {
  574. Assignment,
  575. AdditionAssignment,
  576. SubtractionAssignment,
  577. MultiplicationAssignment,
  578. DivisionAssignment,
  579. ModuloAssignment,
  580. ExponentiationAssignment,
  581. BitwiseAndAssignment,
  582. BitwiseOrAssignment,
  583. BitwiseXorAssignment,
  584. LeftShiftAssignment,
  585. RightShiftAssignment,
  586. UnsignedRightShiftAssignment,
  587. };
  588. class AssignmentExpression : public Expression {
  589. public:
  590. AssignmentExpression(AssignmentOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  591. : m_op(op)
  592. , m_lhs(move(lhs))
  593. , m_rhs(move(rhs))
  594. {
  595. }
  596. virtual Value execute(Interpreter&, GlobalObject&) const override;
  597. virtual void dump(int indent) const override;
  598. private:
  599. virtual const char* class_name() const override { return "AssignmentExpression"; }
  600. AssignmentOp m_op;
  601. NonnullRefPtr<Expression> m_lhs;
  602. NonnullRefPtr<Expression> m_rhs;
  603. };
  604. enum class UpdateOp {
  605. Increment,
  606. Decrement,
  607. };
  608. class UpdateExpression : public Expression {
  609. public:
  610. UpdateExpression(UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false)
  611. : m_op(op)
  612. , m_argument(move(argument))
  613. , m_prefixed(prefixed)
  614. {
  615. }
  616. virtual Value execute(Interpreter&, GlobalObject&) const override;
  617. virtual void dump(int indent) const override;
  618. private:
  619. virtual const char* class_name() const override { return "UpdateExpression"; }
  620. UpdateOp m_op;
  621. NonnullRefPtr<Expression> m_argument;
  622. bool m_prefixed;
  623. };
  624. enum class DeclarationKind {
  625. Var,
  626. Let,
  627. Const,
  628. };
  629. class VariableDeclarator final : public ASTNode {
  630. public:
  631. VariableDeclarator(NonnullRefPtr<Identifier> id)
  632. : m_id(move(id))
  633. {
  634. }
  635. VariableDeclarator(NonnullRefPtr<Identifier> id, RefPtr<Expression> init)
  636. : m_id(move(id))
  637. , m_init(move(init))
  638. {
  639. }
  640. const Identifier& id() const { return m_id; }
  641. const Expression* init() const { return m_init; }
  642. virtual Value execute(Interpreter&, GlobalObject&) const override;
  643. virtual void dump(int indent) const override;
  644. private:
  645. virtual const char* class_name() const override { return "VariableDeclarator"; }
  646. NonnullRefPtr<Identifier> m_id;
  647. RefPtr<Expression> m_init;
  648. };
  649. class VariableDeclaration : public Declaration {
  650. public:
  651. VariableDeclaration(DeclarationKind declaration_kind, NonnullRefPtrVector<VariableDeclarator> declarations)
  652. : m_declaration_kind(declaration_kind)
  653. , m_declarations(move(declarations))
  654. {
  655. }
  656. virtual bool is_variable_declaration() const override { return true; }
  657. DeclarationKind declaration_kind() const { return m_declaration_kind; }
  658. virtual Value execute(Interpreter&, GlobalObject&) const override;
  659. virtual void dump(int indent) const override;
  660. const NonnullRefPtrVector<VariableDeclarator>& declarations() const { return m_declarations; }
  661. private:
  662. virtual const char* class_name() const override { return "VariableDeclaration"; }
  663. DeclarationKind m_declaration_kind;
  664. NonnullRefPtrVector<VariableDeclarator> m_declarations;
  665. };
  666. class ObjectProperty final : public ASTNode {
  667. public:
  668. enum class Type {
  669. KeyValue,
  670. Getter,
  671. Setter,
  672. Spread,
  673. };
  674. ObjectProperty(NonnullRefPtr<Expression> key, RefPtr<Expression> value, Type property_type)
  675. : m_key(move(key))
  676. , m_value(move(value))
  677. , m_property_type(property_type)
  678. {
  679. }
  680. const Expression& key() const { return m_key; }
  681. const Expression& value() const
  682. {
  683. ASSERT(m_value);
  684. return *m_value;
  685. }
  686. Type type() const { return m_property_type; }
  687. virtual void dump(int indent) const override;
  688. virtual Value execute(Interpreter&, GlobalObject&) const override;
  689. private:
  690. virtual const char* class_name() const override { return "ObjectProperty"; }
  691. NonnullRefPtr<Expression> m_key;
  692. RefPtr<Expression> m_value;
  693. Type m_property_type;
  694. };
  695. class ObjectExpression : public Expression {
  696. public:
  697. ObjectExpression(NonnullRefPtrVector<ObjectProperty> properties = {})
  698. : m_properties(move(properties))
  699. {
  700. }
  701. virtual Value execute(Interpreter&, GlobalObject&) const override;
  702. virtual void dump(int indent) const override;
  703. private:
  704. virtual const char* class_name() const override { return "ObjectExpression"; }
  705. NonnullRefPtrVector<ObjectProperty> m_properties;
  706. };
  707. class ArrayExpression : public Expression {
  708. public:
  709. ArrayExpression(Vector<RefPtr<Expression>> elements)
  710. : m_elements(move(elements))
  711. {
  712. }
  713. const Vector<RefPtr<Expression>>& elements() const { return m_elements; }
  714. virtual Value execute(Interpreter&, GlobalObject&) const override;
  715. virtual void dump(int indent) const override;
  716. private:
  717. virtual const char* class_name() const override { return "ArrayExpression"; }
  718. Vector<RefPtr<Expression>> m_elements;
  719. };
  720. class TemplateLiteral final : public Expression {
  721. public:
  722. TemplateLiteral(NonnullRefPtrVector<Expression> expressions)
  723. : m_expressions(move(expressions))
  724. {
  725. }
  726. TemplateLiteral(NonnullRefPtrVector<Expression> expressions, NonnullRefPtrVector<Expression> raw_strings)
  727. : m_expressions(move(expressions))
  728. , m_raw_strings(move(raw_strings))
  729. {
  730. }
  731. virtual Value execute(Interpreter&, GlobalObject&) const override;
  732. virtual void dump(int indent) const override;
  733. const NonnullRefPtrVector<Expression>& expressions() const { return m_expressions; }
  734. const NonnullRefPtrVector<Expression>& raw_strings() const { return m_raw_strings; }
  735. private:
  736. virtual const char* class_name() const override { return "TemplateLiteral"; }
  737. const NonnullRefPtrVector<Expression> m_expressions;
  738. const NonnullRefPtrVector<Expression> m_raw_strings;
  739. };
  740. class TaggedTemplateLiteral final : public Expression {
  741. public:
  742. TaggedTemplateLiteral(NonnullRefPtr<Expression> tag, NonnullRefPtr<TemplateLiteral> template_literal)
  743. : m_tag(move(tag))
  744. , m_template_literal(move(template_literal))
  745. {
  746. }
  747. virtual Value execute(Interpreter&, GlobalObject&) const override;
  748. virtual void dump(int indent) const override;
  749. private:
  750. virtual const char* class_name() const override { return "TaggedTemplateLiteral"; }
  751. const NonnullRefPtr<Expression> m_tag;
  752. const NonnullRefPtr<TemplateLiteral> m_template_literal;
  753. };
  754. class MemberExpression final : public Expression {
  755. public:
  756. MemberExpression(NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false)
  757. : m_object(move(object))
  758. , m_property(move(property))
  759. , m_computed(computed)
  760. {
  761. }
  762. virtual Value execute(Interpreter&, GlobalObject&) const override;
  763. virtual void dump(int indent) const override;
  764. virtual Reference to_reference(Interpreter&, GlobalObject&) const override;
  765. bool is_computed() const { return m_computed; }
  766. const Expression& object() const { return *m_object; }
  767. const Expression& property() const { return *m_property; }
  768. PropertyName computed_property_name(Interpreter&, GlobalObject&) const;
  769. String to_string_approximation() const;
  770. private:
  771. virtual bool is_member_expression() const override { return true; }
  772. virtual const char* class_name() const override { return "MemberExpression"; }
  773. NonnullRefPtr<Expression> m_object;
  774. NonnullRefPtr<Expression> m_property;
  775. bool m_computed { false };
  776. };
  777. class ConditionalExpression final : public Expression {
  778. public:
  779. ConditionalExpression(NonnullRefPtr<Expression> test, NonnullRefPtr<Expression> consequent, NonnullRefPtr<Expression> alternate)
  780. : m_test(move(test))
  781. , m_consequent(move(consequent))
  782. , m_alternate(move(alternate))
  783. {
  784. }
  785. virtual void dump(int indent) const override;
  786. virtual Value execute(Interpreter&, GlobalObject&) const override;
  787. private:
  788. virtual const char* class_name() const override { return "ConditionalExpression"; }
  789. NonnullRefPtr<Expression> m_test;
  790. NonnullRefPtr<Expression> m_consequent;
  791. NonnullRefPtr<Expression> m_alternate;
  792. };
  793. class CatchClause final : public ASTNode {
  794. public:
  795. CatchClause(const FlyString& parameter, NonnullRefPtr<BlockStatement> body)
  796. : m_parameter(parameter)
  797. , m_body(move(body))
  798. {
  799. }
  800. const FlyString& parameter() const { return m_parameter; }
  801. const BlockStatement& body() const { return m_body; }
  802. virtual void dump(int indent) const override;
  803. virtual Value execute(Interpreter&, GlobalObject&) const override;
  804. private:
  805. virtual const char* class_name() const override { return "CatchClause"; }
  806. FlyString m_parameter;
  807. NonnullRefPtr<BlockStatement> m_body;
  808. };
  809. class TryStatement final : public Statement {
  810. public:
  811. TryStatement(NonnullRefPtr<BlockStatement> block, RefPtr<CatchClause> handler, RefPtr<BlockStatement> finalizer)
  812. : m_block(move(block))
  813. , m_handler(move(handler))
  814. , m_finalizer(move(finalizer))
  815. {
  816. }
  817. const BlockStatement& block() const { return m_block; }
  818. const CatchClause* handler() const { return m_handler; }
  819. const BlockStatement* finalizer() const { return m_finalizer; }
  820. virtual void dump(int indent) const override;
  821. virtual Value execute(Interpreter&, GlobalObject&) const override;
  822. private:
  823. virtual const char* class_name() const override { return "TryStatement"; }
  824. NonnullRefPtr<BlockStatement> m_block;
  825. RefPtr<CatchClause> m_handler;
  826. RefPtr<BlockStatement> m_finalizer;
  827. };
  828. class ThrowStatement final : public Statement {
  829. public:
  830. explicit ThrowStatement(NonnullRefPtr<Expression> argument)
  831. : m_argument(move(argument))
  832. {
  833. }
  834. const Expression& argument() const { return m_argument; }
  835. virtual void dump(int indent) const override;
  836. virtual Value execute(Interpreter&, GlobalObject&) const override;
  837. private:
  838. virtual const char* class_name() const override { return "ThrowStatement"; }
  839. NonnullRefPtr<Expression> m_argument;
  840. };
  841. class SwitchCase final : public ASTNode {
  842. public:
  843. SwitchCase(RefPtr<Expression> test, NonnullRefPtrVector<Statement> consequent)
  844. : m_test(move(test))
  845. , m_consequent(move(consequent))
  846. {
  847. }
  848. const Expression* test() const { return m_test; }
  849. const NonnullRefPtrVector<Statement>& consequent() const { return m_consequent; }
  850. virtual void dump(int indent) const override;
  851. virtual Value execute(Interpreter&, GlobalObject&) const override;
  852. private:
  853. virtual const char* class_name() const override { return "SwitchCase"; }
  854. RefPtr<Expression> m_test;
  855. NonnullRefPtrVector<Statement> m_consequent;
  856. };
  857. class SwitchStatement final : public Statement {
  858. public:
  859. SwitchStatement(NonnullRefPtr<Expression> discriminant, NonnullRefPtrVector<SwitchCase> cases)
  860. : m_discriminant(move(discriminant))
  861. , m_cases(move(cases))
  862. {
  863. }
  864. virtual void dump(int indent) const override;
  865. virtual Value execute(Interpreter&, GlobalObject&) const override;
  866. private:
  867. virtual const char* class_name() const override { return "SwitchStatement"; }
  868. NonnullRefPtr<Expression> m_discriminant;
  869. NonnullRefPtrVector<SwitchCase> m_cases;
  870. };
  871. class BreakStatement final : public Statement {
  872. public:
  873. BreakStatement(FlyString target_label)
  874. : m_target_label(target_label)
  875. {
  876. }
  877. virtual Value execute(Interpreter&, GlobalObject&) const override;
  878. const FlyString& target_label() const { return m_target_label; }
  879. private:
  880. virtual const char* class_name() const override { return "BreakStatement"; }
  881. FlyString m_target_label;
  882. };
  883. class ContinueStatement final : public Statement {
  884. public:
  885. ContinueStatement(FlyString target_label)
  886. : m_target_label(target_label)
  887. {
  888. }
  889. virtual Value execute(Interpreter&, GlobalObject&) const override;
  890. const FlyString& target_label() const { return m_target_label; }
  891. private:
  892. virtual const char* class_name() const override { return "ContinueStatement"; }
  893. FlyString m_target_label;
  894. };
  895. class DebuggerStatement final : public Statement {
  896. public:
  897. DebuggerStatement() { }
  898. virtual Value execute(Interpreter&, GlobalObject&) const override;
  899. private:
  900. virtual const char* class_name() const override { return "DebuggerStatement"; }
  901. };
  902. }