AST.h 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. template<class T, class... Args>
  40. static inline NonnullRefPtr<T>
  41. create_ast_node(Args&&... args)
  42. {
  43. return adopt(*new T(forward<Args>(args)...));
  44. }
  45. class ASTNode : public RefCounted<ASTNode> {
  46. public:
  47. virtual ~ASTNode() { }
  48. virtual const char* class_name() const = 0;
  49. virtual Value execute(Interpreter&) const = 0;
  50. virtual void dump(int indent) const;
  51. virtual bool is_identifier() const { return false; }
  52. virtual bool is_spread_expression() const { return false; }
  53. virtual bool is_member_expression() const { return false; }
  54. virtual bool is_scope_node() const { return false; }
  55. virtual bool is_program() const { return false; }
  56. virtual bool is_variable_declaration() const { return false; }
  57. virtual bool is_call_expression() const { return false; }
  58. virtual bool is_new_expression() const { return false; }
  59. protected:
  60. ASTNode() { }
  61. private:
  62. };
  63. class Statement : public ASTNode {
  64. };
  65. class EmptyStatement final : public Statement {
  66. public:
  67. Value execute(Interpreter&) const override { return js_undefined(); }
  68. const char* class_name() const override { return "EmptyStatement"; }
  69. };
  70. class ErrorStatement final : public Statement {
  71. public:
  72. Value execute(Interpreter&) const override { return js_undefined(); }
  73. const char* class_name() const override { return "ErrorStatement"; }
  74. };
  75. class ExpressionStatement final : public Statement {
  76. public:
  77. ExpressionStatement(NonnullRefPtr<Expression> expression)
  78. : m_expression(move(expression))
  79. {
  80. }
  81. Value execute(Interpreter&) const override;
  82. const char* class_name() const override { return "ExpressionStatement"; }
  83. virtual void dump(int indent) const override;
  84. private:
  85. NonnullRefPtr<Expression> m_expression;
  86. };
  87. class ScopeNode : public Statement {
  88. public:
  89. template<typename T, typename... Args>
  90. T& append(Args&&... args)
  91. {
  92. auto child = create_ast_node<T>(forward<Args>(args)...);
  93. m_children.append(move(child));
  94. return static_cast<T&>(m_children.last());
  95. }
  96. void append(NonnullRefPtr<Statement> child)
  97. {
  98. m_children.append(move(child));
  99. }
  100. const NonnullRefPtrVector<Statement>& children() const { return m_children; }
  101. virtual Value execute(Interpreter&) const override;
  102. virtual void dump(int indent) const override;
  103. void add_variables(NonnullRefPtrVector<VariableDeclaration>);
  104. const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; }
  105. protected:
  106. ScopeNode() { }
  107. private:
  108. virtual bool is_scope_node() const final { return true; }
  109. NonnullRefPtrVector<Statement> m_children;
  110. NonnullRefPtrVector<VariableDeclaration> m_variables;
  111. };
  112. class Program : public ScopeNode {
  113. public:
  114. Program() { }
  115. private:
  116. virtual bool is_program() const override { return true; }
  117. virtual const char* class_name() const override { return "Program"; }
  118. };
  119. class BlockStatement : public ScopeNode {
  120. public:
  121. BlockStatement() { }
  122. private:
  123. virtual const char* class_name() const override { return "BlockStatement"; }
  124. };
  125. class Expression : public ASTNode {
  126. public:
  127. virtual Reference to_reference(Interpreter&) const;
  128. };
  129. class Declaration : public Statement {
  130. };
  131. class FunctionNode {
  132. public:
  133. struct Parameter {
  134. FlyString name;
  135. RefPtr<Expression> default_value;
  136. bool is_rest { false };
  137. };
  138. const FlyString& name() const { return m_name; }
  139. const Statement& body() const { return *m_body; }
  140. const Vector<Parameter>& parameters() const { return m_parameters; };
  141. protected:
  142. FunctionNode(const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables)
  143. : m_name(name)
  144. , m_body(move(body))
  145. , m_parameters(move(parameters))
  146. , m_variables(move(variables))
  147. , m_function_length(function_length)
  148. {
  149. }
  150. void dump(int indent, const char* class_name) const;
  151. const NonnullRefPtrVector<VariableDeclaration>& variables() const { return m_variables; }
  152. i32 function_length() const { return m_function_length; }
  153. private:
  154. FlyString m_name;
  155. NonnullRefPtr<Statement> m_body;
  156. const Vector<Parameter> m_parameters;
  157. NonnullRefPtrVector<VariableDeclaration> m_variables;
  158. const i32 m_function_length;
  159. };
  160. class FunctionDeclaration final
  161. : public Declaration
  162. , public FunctionNode {
  163. public:
  164. static bool must_have_name() { return true; }
  165. FunctionDeclaration(const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables)
  166. : FunctionNode(name, move(body), move(parameters), function_length, move(variables))
  167. {
  168. }
  169. virtual Value execute(Interpreter&) const override;
  170. virtual void dump(int indent) const override;
  171. private:
  172. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  173. };
  174. class FunctionExpression final : public Expression
  175. , public FunctionNode {
  176. public:
  177. static bool must_have_name() { return false; }
  178. FunctionExpression(const FlyString& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, NonnullRefPtrVector<VariableDeclaration> variables)
  179. : FunctionNode(name, move(body), move(parameters), function_length, move(variables))
  180. {
  181. }
  182. virtual Value execute(Interpreter&) const override;
  183. virtual void dump(int indent) const override;
  184. private:
  185. virtual const char* class_name() const override { return "FunctionExpression"; }
  186. };
  187. class ErrorExpression final : public Expression {
  188. public:
  189. Value execute(Interpreter&) const override { return js_undefined(); }
  190. const char* class_name() const override { return "ErrorExpression"; }
  191. };
  192. class ReturnStatement : public Statement {
  193. public:
  194. explicit ReturnStatement(RefPtr<Expression> argument)
  195. : m_argument(move(argument))
  196. {
  197. }
  198. const Expression* argument() const { return m_argument; }
  199. virtual Value execute(Interpreter&) const override;
  200. virtual void dump(int indent) const override;
  201. private:
  202. virtual const char* class_name() const override { return "ReturnStatement"; }
  203. RefPtr<Expression> m_argument;
  204. };
  205. class IfStatement : public Statement {
  206. public:
  207. IfStatement(NonnullRefPtr<Expression> predicate, NonnullRefPtr<Statement> consequent, RefPtr<Statement> alternate)
  208. : m_predicate(move(predicate))
  209. , m_consequent(move(consequent))
  210. , m_alternate(move(alternate))
  211. {
  212. }
  213. const Expression& predicate() const { return *m_predicate; }
  214. const Statement& consequent() const { return *m_consequent; }
  215. const Statement* alternate() const { return m_alternate; }
  216. virtual Value execute(Interpreter&) const override;
  217. virtual void dump(int indent) const override;
  218. private:
  219. virtual const char* class_name() const override { return "IfStatement"; }
  220. NonnullRefPtr<Expression> m_predicate;
  221. NonnullRefPtr<Statement> m_consequent;
  222. RefPtr<Statement> m_alternate;
  223. };
  224. class WhileStatement : public Statement {
  225. public:
  226. WhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  227. : m_test(move(test))
  228. , m_body(move(body))
  229. {
  230. }
  231. const Expression& test() const { return *m_test; }
  232. const Statement& body() const { return *m_body; }
  233. virtual Value execute(Interpreter&) const override;
  234. virtual void dump(int indent) const override;
  235. private:
  236. virtual const char* class_name() const override { return "WhileStatement"; }
  237. NonnullRefPtr<Expression> m_test;
  238. NonnullRefPtr<Statement> m_body;
  239. };
  240. class DoWhileStatement : public Statement {
  241. public:
  242. DoWhileStatement(NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  243. : m_test(move(test))
  244. , m_body(move(body))
  245. {
  246. }
  247. const Expression& test() const { return *m_test; }
  248. const Statement& body() const { return *m_body; }
  249. virtual Value execute(Interpreter&) const override;
  250. virtual void dump(int indent) const override;
  251. private:
  252. virtual const char* class_name() const override { return "DoWhileStatement"; }
  253. NonnullRefPtr<Expression> m_test;
  254. NonnullRefPtr<Statement> m_body;
  255. };
  256. class ForStatement : public Statement {
  257. public:
  258. ForStatement(RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<Statement> body)
  259. : m_init(move(init))
  260. , m_test(move(test))
  261. , m_update(move(update))
  262. , m_body(move(body))
  263. {
  264. }
  265. const ASTNode* init() const { return m_init; }
  266. const Expression* test() const { return m_test; }
  267. const Expression* update() const { return m_update; }
  268. const Statement& body() const { return *m_body; }
  269. virtual Value execute(Interpreter&) const override;
  270. virtual void dump(int indent) const override;
  271. private:
  272. virtual const char* class_name() const override { return "ForStatement"; }
  273. RefPtr<ASTNode> m_init;
  274. RefPtr<Expression> m_test;
  275. RefPtr<Expression> m_update;
  276. NonnullRefPtr<Statement> m_body;
  277. };
  278. enum class BinaryOp {
  279. Addition,
  280. Subtraction,
  281. Multiplication,
  282. Division,
  283. Modulo,
  284. Exponentiation,
  285. TypedEquals,
  286. TypedInequals,
  287. AbstractEquals,
  288. AbstractInequals,
  289. GreaterThan,
  290. GreaterThanEquals,
  291. LessThan,
  292. LessThanEquals,
  293. BitwiseAnd,
  294. BitwiseOr,
  295. BitwiseXor,
  296. LeftShift,
  297. RightShift,
  298. UnsignedRightShift,
  299. In,
  300. InstanceOf,
  301. };
  302. class BinaryExpression : public Expression {
  303. public:
  304. BinaryExpression(BinaryOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  305. : m_op(op)
  306. , m_lhs(move(lhs))
  307. , m_rhs(move(rhs))
  308. {
  309. }
  310. virtual Value execute(Interpreter&) const override;
  311. virtual void dump(int indent) const override;
  312. private:
  313. virtual const char* class_name() const override { return "BinaryExpression"; }
  314. BinaryOp m_op;
  315. NonnullRefPtr<Expression> m_lhs;
  316. NonnullRefPtr<Expression> m_rhs;
  317. };
  318. enum class LogicalOp {
  319. And,
  320. Or,
  321. NullishCoalescing,
  322. };
  323. class LogicalExpression : public Expression {
  324. public:
  325. LogicalExpression(LogicalOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  326. : m_op(op)
  327. , m_lhs(move(lhs))
  328. , m_rhs(move(rhs))
  329. {
  330. }
  331. virtual Value execute(Interpreter&) const override;
  332. virtual void dump(int indent) const override;
  333. private:
  334. virtual const char* class_name() const override { return "LogicalExpression"; }
  335. LogicalOp m_op;
  336. NonnullRefPtr<Expression> m_lhs;
  337. NonnullRefPtr<Expression> m_rhs;
  338. };
  339. enum class UnaryOp {
  340. BitwiseNot,
  341. Not,
  342. Plus,
  343. Minus,
  344. Typeof,
  345. Void,
  346. Delete,
  347. };
  348. class UnaryExpression : public Expression {
  349. public:
  350. UnaryExpression(UnaryOp op, NonnullRefPtr<Expression> lhs)
  351. : m_op(op)
  352. , m_lhs(move(lhs))
  353. {
  354. }
  355. virtual Value execute(Interpreter&) const override;
  356. virtual void dump(int indent) const override;
  357. private:
  358. virtual const char* class_name() const override { return "UnaryExpression"; }
  359. UnaryOp m_op;
  360. NonnullRefPtr<Expression> m_lhs;
  361. };
  362. class SequenceExpression final : public Expression {
  363. public:
  364. SequenceExpression(NonnullRefPtrVector<Expression> expressions)
  365. : m_expressions(move(expressions))
  366. {
  367. }
  368. virtual void dump(int indent) const override;
  369. virtual Value execute(Interpreter&) const override;
  370. private:
  371. virtual const char* class_name() const override { return "SequenceExpression"; }
  372. NonnullRefPtrVector<Expression> m_expressions;
  373. };
  374. class Literal : public Expression {
  375. protected:
  376. explicit Literal() { }
  377. };
  378. class BooleanLiteral final : public Literal {
  379. public:
  380. explicit BooleanLiteral(bool value)
  381. : m_value(value)
  382. {
  383. }
  384. virtual Value execute(Interpreter&) const override;
  385. virtual void dump(int indent) const override;
  386. private:
  387. virtual const char* class_name() const override { return "BooleanLiteral"; }
  388. bool m_value { false };
  389. };
  390. class NumericLiteral final : public Literal {
  391. public:
  392. explicit NumericLiteral(double value)
  393. : m_value(value)
  394. {
  395. }
  396. virtual Value execute(Interpreter&) const override;
  397. virtual void dump(int indent) const override;
  398. private:
  399. virtual const char* class_name() const override { return "NumericLiteral"; }
  400. double m_value { 0 };
  401. };
  402. class StringLiteral final : public Literal {
  403. public:
  404. explicit StringLiteral(String value)
  405. : m_value(move(value))
  406. {
  407. }
  408. virtual Value execute(Interpreter&) const override;
  409. virtual void dump(int indent) const override;
  410. private:
  411. virtual const char* class_name() const override { return "StringLiteral"; }
  412. String m_value;
  413. };
  414. class NullLiteral final : public Literal {
  415. public:
  416. explicit NullLiteral() { }
  417. virtual Value execute(Interpreter&) const override;
  418. virtual void dump(int indent) const override;
  419. private:
  420. virtual const char* class_name() const override { return "NullLiteral"; }
  421. };
  422. class Identifier final : public Expression {
  423. public:
  424. explicit Identifier(const FlyString& string)
  425. : m_string(string)
  426. {
  427. }
  428. const FlyString& string() const { return m_string; }
  429. virtual Value execute(Interpreter&) const override;
  430. virtual void dump(int indent) const override;
  431. virtual bool is_identifier() const override { return true; }
  432. virtual Reference to_reference(Interpreter&) const override;
  433. private:
  434. virtual const char* class_name() const override { return "Identifier"; }
  435. FlyString m_string;
  436. };
  437. class SpreadExpression final : public Expression {
  438. public:
  439. explicit SpreadExpression(NonnullRefPtr<Expression> target)
  440. : m_target(target)
  441. {
  442. }
  443. virtual Value execute(Interpreter&) const override;
  444. virtual void dump(int indent) const override;
  445. virtual bool is_spread_expression() const override { return true; }
  446. private:
  447. virtual const char* class_name() const override { return "SpreadExpression"; }
  448. NonnullRefPtr<Expression> m_target;
  449. };
  450. class ThisExpression final : public Expression {
  451. public:
  452. virtual Value execute(Interpreter&) const override;
  453. virtual void dump(int indent) const override;
  454. private:
  455. virtual const char* class_name() const override { return "ThisExpression"; }
  456. };
  457. class CallExpression : public Expression {
  458. public:
  459. struct Argument {
  460. NonnullRefPtr<Expression> value;
  461. bool is_spread;
  462. };
  463. CallExpression(NonnullRefPtr<Expression> callee, Vector<Argument> arguments = {})
  464. : m_callee(move(callee))
  465. , m_arguments(move(arguments))
  466. {
  467. }
  468. virtual Value execute(Interpreter&) const override;
  469. virtual void dump(int indent) const override;
  470. private:
  471. virtual const char* class_name() const override { return "CallExpression"; }
  472. virtual bool is_call_expression() const override { return true; }
  473. struct ThisAndCallee {
  474. Value this_value;
  475. Value callee;
  476. };
  477. ThisAndCallee compute_this_and_callee(Interpreter&) const;
  478. NonnullRefPtr<Expression> m_callee;
  479. const Vector<Argument> m_arguments;
  480. };
  481. class NewExpression final : public CallExpression {
  482. public:
  483. NewExpression(NonnullRefPtr<Expression> callee, Vector<Argument> arguments = {})
  484. : CallExpression(move(callee), move(arguments))
  485. {
  486. }
  487. private:
  488. virtual const char* class_name() const override { return "NewExpression"; }
  489. virtual bool is_call_expression() const override { return false; }
  490. virtual bool is_new_expression() const override { return true; }
  491. };
  492. enum class AssignmentOp {
  493. Assignment,
  494. AdditionAssignment,
  495. SubtractionAssignment,
  496. MultiplicationAssignment,
  497. DivisionAssignment,
  498. ModuloAssignment,
  499. ExponentiationAssignment,
  500. BitwiseAndAssignment,
  501. BitwiseOrAssignment,
  502. BitwiseXorAssignment,
  503. LeftShiftAssignment,
  504. RightShiftAssignment,
  505. UnsignedRightShiftAssignment,
  506. };
  507. class AssignmentExpression : public Expression {
  508. public:
  509. AssignmentExpression(AssignmentOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  510. : m_op(op)
  511. , m_lhs(move(lhs))
  512. , m_rhs(move(rhs))
  513. {
  514. }
  515. virtual Value execute(Interpreter&) const override;
  516. virtual void dump(int indent) const override;
  517. private:
  518. virtual const char* class_name() const override { return "AssignmentExpression"; }
  519. AssignmentOp m_op;
  520. NonnullRefPtr<Expression> m_lhs;
  521. NonnullRefPtr<Expression> m_rhs;
  522. };
  523. enum class UpdateOp {
  524. Increment,
  525. Decrement,
  526. };
  527. class UpdateExpression : public Expression {
  528. public:
  529. UpdateExpression(UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false)
  530. : m_op(op)
  531. , m_argument(move(argument))
  532. , m_prefixed(prefixed)
  533. {
  534. }
  535. virtual Value execute(Interpreter&) const override;
  536. virtual void dump(int indent) const override;
  537. private:
  538. virtual const char* class_name() const override { return "UpdateExpression"; }
  539. UpdateOp m_op;
  540. NonnullRefPtr<Expression> m_argument;
  541. bool m_prefixed;
  542. };
  543. enum class DeclarationKind {
  544. Var,
  545. Let,
  546. Const,
  547. };
  548. class VariableDeclarator final : public ASTNode {
  549. public:
  550. VariableDeclarator(NonnullRefPtr<Identifier> id, RefPtr<Expression> init)
  551. : m_id(move(id))
  552. , m_init(move(init))
  553. {
  554. }
  555. const Identifier& id() const { return m_id; }
  556. const Expression* init() const { return m_init; }
  557. virtual Value execute(Interpreter&) const override;
  558. virtual void dump(int indent) const override;
  559. private:
  560. virtual const char* class_name() const override { return "VariableDeclarator"; }
  561. NonnullRefPtr<Identifier> m_id;
  562. RefPtr<Expression> m_init;
  563. };
  564. class VariableDeclaration : public Declaration {
  565. public:
  566. VariableDeclaration(DeclarationKind declaration_kind, NonnullRefPtrVector<VariableDeclarator> declarations)
  567. : m_declaration_kind(declaration_kind)
  568. , m_declarations(move(declarations))
  569. {
  570. }
  571. virtual bool is_variable_declaration() const override { return true; }
  572. DeclarationKind declaration_kind() const { return m_declaration_kind; }
  573. virtual Value execute(Interpreter&) const override;
  574. virtual void dump(int indent) const override;
  575. const NonnullRefPtrVector<VariableDeclarator>& declarations() const { return m_declarations; }
  576. private:
  577. virtual const char* class_name() const override { return "VariableDeclaration"; }
  578. DeclarationKind m_declaration_kind;
  579. NonnullRefPtrVector<VariableDeclarator> m_declarations;
  580. };
  581. class ObjectProperty final : public ASTNode {
  582. public:
  583. enum class Type {
  584. KeyValue,
  585. Getter,
  586. Setter,
  587. Spread,
  588. };
  589. ObjectProperty(NonnullRefPtr<Expression> key, NonnullRefPtr<Expression> value, Type property_type)
  590. : m_key(move(key))
  591. , m_value(move(value))
  592. , m_property_type(property_type)
  593. {
  594. }
  595. const Expression& key() const { return m_key; }
  596. const Expression& value() const { return m_value; }
  597. Type type() const { return m_property_type; }
  598. virtual void dump(int indent) const override;
  599. virtual Value execute(Interpreter&) const override;
  600. private:
  601. virtual const char* class_name() const override { return "ObjectProperty"; }
  602. NonnullRefPtr<Expression> m_key;
  603. NonnullRefPtr<Expression> m_value;
  604. Type m_property_type;
  605. };
  606. class ObjectExpression : public Expression {
  607. public:
  608. ObjectExpression(NonnullRefPtrVector<ObjectProperty> properties = {})
  609. : m_properties(move(properties))
  610. {
  611. }
  612. virtual Value execute(Interpreter&) const override;
  613. virtual void dump(int indent) const override;
  614. private:
  615. virtual const char* class_name() const override { return "ObjectExpression"; }
  616. NonnullRefPtrVector<ObjectProperty> m_properties;
  617. };
  618. class ArrayExpression : public Expression {
  619. public:
  620. ArrayExpression(Vector<RefPtr<Expression>> elements)
  621. : m_elements(move(elements))
  622. {
  623. }
  624. const Vector<RefPtr<Expression>>& elements() const { return m_elements; }
  625. virtual Value execute(Interpreter&) const override;
  626. virtual void dump(int indent) const override;
  627. private:
  628. virtual const char* class_name() const override { return "ArrayExpression"; }
  629. Vector<RefPtr<Expression>> m_elements;
  630. };
  631. class TemplateLiteral final : public Expression {
  632. public:
  633. TemplateLiteral(NonnullRefPtrVector<Expression> expressions)
  634. : m_expressions(move(expressions))
  635. {
  636. }
  637. TemplateLiteral(NonnullRefPtrVector<Expression> expressions, NonnullRefPtrVector<Expression> raw_strings)
  638. : m_expressions(move(expressions))
  639. , m_raw_strings(move(raw_strings))
  640. {
  641. }
  642. virtual Value execute(Interpreter&) const override;
  643. virtual void dump(int indent) const override;
  644. const NonnullRefPtrVector<Expression>& expressions() const { return m_expressions; }
  645. const NonnullRefPtrVector<Expression>& raw_strings() const { return m_raw_strings; }
  646. private:
  647. virtual const char* class_name() const override { return "TemplateLiteral"; }
  648. const NonnullRefPtrVector<Expression> m_expressions;
  649. const NonnullRefPtrVector<Expression> m_raw_strings;
  650. };
  651. class TaggedTemplateLiteral final : public Expression {
  652. public:
  653. TaggedTemplateLiteral(NonnullRefPtr<Expression> tag, NonnullRefPtr<TemplateLiteral> template_literal)
  654. : m_tag(move(tag))
  655. , m_template_literal(move(template_literal))
  656. {
  657. }
  658. virtual Value execute(Interpreter&) const override;
  659. virtual void dump(int indent) const override;
  660. private:
  661. virtual const char* class_name() const override { return "TaggedTemplateLiteral"; }
  662. const NonnullRefPtr<Expression> m_tag;
  663. const NonnullRefPtr<TemplateLiteral> m_template_literal;
  664. };
  665. class MemberExpression final : public Expression {
  666. public:
  667. MemberExpression(NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false)
  668. : m_object(move(object))
  669. , m_property(move(property))
  670. , m_computed(computed)
  671. {
  672. }
  673. virtual Value execute(Interpreter&) const override;
  674. virtual void dump(int indent) const override;
  675. virtual Reference to_reference(Interpreter&) const override;
  676. bool is_computed() const { return m_computed; }
  677. const Expression& object() const { return *m_object; }
  678. const Expression& property() const { return *m_property; }
  679. PropertyName computed_property_name(Interpreter&) const;
  680. String to_string_approximation() const;
  681. private:
  682. virtual bool is_member_expression() const override { return true; }
  683. virtual const char* class_name() const override { return "MemberExpression"; }
  684. NonnullRefPtr<Expression> m_object;
  685. NonnullRefPtr<Expression> m_property;
  686. bool m_computed { false };
  687. };
  688. class ConditionalExpression final : public Expression {
  689. public:
  690. ConditionalExpression(NonnullRefPtr<Expression> test, NonnullRefPtr<Expression> consequent, NonnullRefPtr<Expression> alternate)
  691. : m_test(move(test))
  692. , m_consequent(move(consequent))
  693. , m_alternate(move(alternate))
  694. {
  695. }
  696. virtual void dump(int indent) const override;
  697. virtual Value execute(Interpreter&) const override;
  698. private:
  699. virtual const char* class_name() const override { return "ConditionalExpression"; }
  700. NonnullRefPtr<Expression> m_test;
  701. NonnullRefPtr<Expression> m_consequent;
  702. NonnullRefPtr<Expression> m_alternate;
  703. };
  704. class CatchClause final : public ASTNode {
  705. public:
  706. CatchClause(const FlyString& parameter, NonnullRefPtr<BlockStatement> body)
  707. : m_parameter(parameter)
  708. , m_body(move(body))
  709. {
  710. }
  711. const FlyString& parameter() const { return m_parameter; }
  712. const BlockStatement& body() const { return m_body; }
  713. virtual void dump(int indent) const override;
  714. virtual Value execute(Interpreter&) const override;
  715. private:
  716. virtual const char* class_name() const override { return "CatchClause"; }
  717. FlyString m_parameter;
  718. NonnullRefPtr<BlockStatement> m_body;
  719. };
  720. class TryStatement final : public Statement {
  721. public:
  722. TryStatement(NonnullRefPtr<BlockStatement> block, RefPtr<CatchClause> handler, RefPtr<BlockStatement> finalizer)
  723. : m_block(move(block))
  724. , m_handler(move(handler))
  725. , m_finalizer(move(finalizer))
  726. {
  727. }
  728. const BlockStatement& block() const { return m_block; }
  729. const CatchClause* handler() const { return m_handler; }
  730. const BlockStatement* finalizer() const { return m_finalizer; }
  731. virtual void dump(int indent) const override;
  732. virtual Value execute(Interpreter&) const override;
  733. private:
  734. virtual const char* class_name() const override { return "TryStatement"; }
  735. NonnullRefPtr<BlockStatement> m_block;
  736. RefPtr<CatchClause> m_handler;
  737. RefPtr<BlockStatement> m_finalizer;
  738. };
  739. class ThrowStatement final : public Statement {
  740. public:
  741. explicit ThrowStatement(NonnullRefPtr<Expression> argument)
  742. : m_argument(move(argument))
  743. {
  744. }
  745. const Expression& argument() const { return m_argument; }
  746. virtual void dump(int indent) const override;
  747. virtual Value execute(Interpreter&) const override;
  748. private:
  749. virtual const char* class_name() const override { return "ThrowStatement"; }
  750. NonnullRefPtr<Expression> m_argument;
  751. };
  752. class SwitchCase final : public ASTNode {
  753. public:
  754. SwitchCase(RefPtr<Expression> test, NonnullRefPtrVector<Statement> consequent)
  755. : m_test(move(test))
  756. , m_consequent(move(consequent))
  757. {
  758. }
  759. const Expression* test() const { return m_test; }
  760. const NonnullRefPtrVector<Statement>& consequent() const { return m_consequent; }
  761. virtual void dump(int indent) const override;
  762. virtual Value execute(Interpreter&) const override;
  763. private:
  764. virtual const char* class_name() const override { return "SwitchCase"; }
  765. RefPtr<Expression> m_test;
  766. NonnullRefPtrVector<Statement> m_consequent;
  767. };
  768. class SwitchStatement final : public Statement {
  769. public:
  770. SwitchStatement(NonnullRefPtr<Expression> discriminant, NonnullRefPtrVector<SwitchCase> cases)
  771. : m_discriminant(move(discriminant))
  772. , m_cases(move(cases))
  773. {
  774. }
  775. virtual void dump(int indent) const override;
  776. virtual Value execute(Interpreter&) const override;
  777. private:
  778. virtual const char* class_name() const override { return "SwitchStatement"; }
  779. NonnullRefPtr<Expression> m_discriminant;
  780. NonnullRefPtrVector<SwitchCase> m_cases;
  781. };
  782. class BreakStatement final : public Statement {
  783. public:
  784. BreakStatement() { }
  785. virtual Value execute(Interpreter&) const override;
  786. private:
  787. virtual const char* class_name() const override { return "BreakStatement"; }
  788. };
  789. class ContinueStatement final : public Statement {
  790. public:
  791. ContinueStatement() { }
  792. virtual Value execute(Interpreter&) const override;
  793. private:
  794. virtual const char* class_name() const override { return "ContinueStatement"; }
  795. };
  796. class DebuggerStatement final : public Statement {
  797. public:
  798. DebuggerStatement() { }
  799. virtual Value execute(Interpreter&) const override;
  800. private:
  801. virtual const char* class_name() const override { return "DebuggerStatement"; }
  802. };
  803. }