AST.h 38 KB

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