AST.h 40 KB

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