AST.h 31 KB

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