AST.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, David Tuin <davidot@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/FlyString.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/NonnullRefPtrVector.h>
  12. #include <AK/OwnPtr.h>
  13. #include <AK/RefPtr.h>
  14. #include <AK/String.h>
  15. #include <AK/Variant.h>
  16. #include <AK/Vector.h>
  17. #include <LibJS/Forward.h>
  18. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  19. #include <LibJS/Runtime/PropertyName.h>
  20. #include <LibJS/Runtime/Reference.h>
  21. #include <LibJS/Runtime/Value.h>
  22. #include <LibJS/SourceRange.h>
  23. #include <LibRegex/Regex.h>
  24. namespace JS {
  25. class Declaration;
  26. class ClassDeclaration;
  27. class FunctionDeclaration;
  28. class Identifier;
  29. class MemberExpression;
  30. class VariableDeclaration;
  31. enum class FunctionKind {
  32. Generator,
  33. Regular,
  34. };
  35. template<class T, class... Args>
  36. static inline NonnullRefPtr<T>
  37. create_ast_node(SourceRange range, Args&&... args)
  38. {
  39. return adopt_ref(*new T(range, forward<Args>(args)...));
  40. }
  41. class ASTNode : public RefCounted<ASTNode> {
  42. public:
  43. virtual ~ASTNode() { }
  44. virtual Value execute(Interpreter&, GlobalObject&) const = 0;
  45. virtual void generate_bytecode(Bytecode::Generator&) const;
  46. virtual void dump(int indent) const;
  47. SourceRange const& source_range() const { return m_source_range; }
  48. SourceRange& source_range() { return m_source_range; }
  49. String class_name() const;
  50. template<typename T>
  51. bool fast_is() const = delete;
  52. virtual bool is_new_expression() const { return false; }
  53. virtual bool is_member_expression() const { return false; }
  54. virtual bool is_super_expression() const { return false; }
  55. virtual bool is_expression_statement() const { return false; }
  56. virtual bool is_identifier() const { return false; }
  57. virtual bool is_scope_node() const { return false; }
  58. virtual bool is_program() const { return false; }
  59. protected:
  60. explicit ASTNode(SourceRange source_range)
  61. : m_source_range(source_range)
  62. {
  63. }
  64. private:
  65. SourceRange m_source_range;
  66. };
  67. class Statement : public ASTNode {
  68. public:
  69. explicit Statement(SourceRange source_range)
  70. : ASTNode(source_range)
  71. {
  72. }
  73. };
  74. class LabelableStatement : public Statement {
  75. public:
  76. using Statement::Statement;
  77. Vector<FlyString> const& labels() const { return m_labels; }
  78. virtual void add_label(FlyString string) { m_labels.append(move(string)); }
  79. protected:
  80. Vector<FlyString> m_labels;
  81. };
  82. class IterationStatement : public LabelableStatement {
  83. public:
  84. using LabelableStatement::LabelableStatement;
  85. };
  86. class EmptyStatement final : public Statement {
  87. public:
  88. explicit EmptyStatement(SourceRange source_range)
  89. : Statement(source_range)
  90. {
  91. }
  92. Value execute(Interpreter&, GlobalObject&) const override { return {}; }
  93. virtual void generate_bytecode(Bytecode::Generator&) const override;
  94. };
  95. class ErrorStatement final : public Statement {
  96. public:
  97. explicit ErrorStatement(SourceRange source_range)
  98. : Statement(source_range)
  99. {
  100. }
  101. Value execute(Interpreter&, GlobalObject&) const override { return {}; }
  102. };
  103. class ExpressionStatement final : public Statement {
  104. public:
  105. ExpressionStatement(SourceRange source_range, NonnullRefPtr<Expression> expression)
  106. : Statement(source_range)
  107. , m_expression(move(expression))
  108. {
  109. }
  110. virtual Value execute(Interpreter&, GlobalObject&) const override;
  111. virtual void dump(int indent) const override;
  112. virtual void generate_bytecode(Bytecode::Generator&) const override;
  113. Expression const& expression() const { return m_expression; };
  114. private:
  115. virtual bool is_expression_statement() const override { return true; }
  116. NonnullRefPtr<Expression> m_expression;
  117. };
  118. template<typename... Args>
  119. class IteratorOrVoidFunction : public Function<IterationDecision(Args...)> {
  120. public:
  121. template<typename CallableType>
  122. IteratorOrVoidFunction(CallableType&& callable) requires(VoidFunction<CallableType, Args...>)
  123. : Function<IterationDecision(Args...)>([callable = forward<CallableType>(callable)](Args... args) {
  124. callable(args...);
  125. return IterationDecision::Continue;
  126. })
  127. {
  128. }
  129. template<typename CallableType>
  130. IteratorOrVoidFunction(CallableType&& callable) requires(IteratorFunction<CallableType, Args...>)
  131. : Function<IterationDecision(Args...)>(forward<CallableType>(callable))
  132. {
  133. }
  134. };
  135. class ScopeNode : public LabelableStatement {
  136. public:
  137. template<typename T, typename... Args>
  138. T& append(SourceRange range, Args&&... args)
  139. {
  140. auto child = create_ast_node<T>(range, forward<Args>(args)...);
  141. m_children.append(move(child));
  142. return static_cast<T&>(m_children.last());
  143. }
  144. void append(NonnullRefPtr<Statement> child)
  145. {
  146. m_children.append(move(child));
  147. }
  148. NonnullRefPtrVector<Statement> const& children() const { return m_children; }
  149. virtual void dump(int indent) const override;
  150. virtual void generate_bytecode(Bytecode::Generator&) const override;
  151. Value evaluate_statements(Interpreter& interpreter, GlobalObject& global_object) const;
  152. void add_var_scoped_declaration(NonnullRefPtr<Declaration> variables);
  153. void add_lexical_declaration(NonnullRefPtr<Declaration> variables);
  154. void add_hoisted_function(NonnullRefPtr<FunctionDeclaration> declaration);
  155. [[nodiscard]] bool has_lexical_declarations() const { return !m_lexical_declarations.is_empty(); }
  156. [[nodiscard]] bool has_var_declarations() const { return !m_var_declarations.is_empty(); }
  157. void for_each_lexically_scoped_declaration(IteratorOrVoidFunction<Declaration const&>&& callback) const;
  158. void for_each_lexically_declared_name(IteratorOrVoidFunction<FlyString const&>&& callback) const;
  159. void for_each_var_declared_name(IteratorOrVoidFunction<FlyString const&>&& callback) const;
  160. void for_each_var_function_declaration_in_reverse_order(IteratorOrVoidFunction<FunctionDeclaration const&>&& callback) const;
  161. void for_each_var_scoped_variable_declaration(IteratorOrVoidFunction<VariableDeclaration const&>&& callback) const;
  162. void block_declaration_instantiation(GlobalObject& global_object, Environment* environment) const;
  163. void for_each_function_hoistable_with_annexB_extension(IteratorOrVoidFunction<FunctionDeclaration&>&& callback) const;
  164. protected:
  165. explicit ScopeNode(SourceRange source_range)
  166. : LabelableStatement(source_range)
  167. {
  168. }
  169. private:
  170. virtual bool is_scope_node() const final { return true; }
  171. NonnullRefPtrVector<Statement> m_children;
  172. NonnullRefPtrVector<Declaration> m_lexical_declarations;
  173. NonnullRefPtrVector<Declaration> m_var_declarations;
  174. NonnullRefPtrVector<FunctionDeclaration> m_functions_hoistable_with_annexB_extension;
  175. };
  176. class ImportStatement final : public Statement {
  177. public:
  178. struct ImportEntry {
  179. String import_name;
  180. String local_name;
  181. };
  182. explicit ImportStatement(SourceRange source_range, StringView from_module, Vector<ImportEntry> entries = {})
  183. : Statement(source_range)
  184. , m_module_request(from_module)
  185. , m_entries(move(entries))
  186. {
  187. }
  188. virtual Value execute(Interpreter&, GlobalObject&) const override;
  189. virtual void dump(int indent) const override;
  190. bool has_bound_name(StringView name) const;
  191. private:
  192. String m_module_request;
  193. Vector<ImportEntry> m_entries;
  194. };
  195. class ExportStatement final : public Statement {
  196. public:
  197. struct ExportEntry {
  198. enum Kind {
  199. ModuleRequest,
  200. LocalExport
  201. } kind;
  202. // Can always have
  203. String export_name;
  204. // Only if module request
  205. String module_request;
  206. // Has just one of ones below
  207. String local_or_import_name;
  208. ExportEntry(String export_name, String local_name)
  209. : kind(LocalExport)
  210. , export_name(export_name)
  211. , local_or_import_name(local_name)
  212. {
  213. }
  214. };
  215. explicit ExportStatement(SourceRange source_range, RefPtr<ASTNode> statement, Vector<ExportEntry> entries)
  216. : Statement(source_range)
  217. , m_statement(move(statement))
  218. , m_entries(move(entries))
  219. {
  220. }
  221. virtual Value execute(Interpreter&, GlobalObject&) const override;
  222. virtual void dump(int indent) const override;
  223. bool has_export(StringView export_name) const;
  224. private:
  225. RefPtr<ASTNode> m_statement;
  226. Vector<ExportEntry> m_entries;
  227. };
  228. class Program final : public ScopeNode {
  229. public:
  230. enum class Type {
  231. Script,
  232. Module
  233. };
  234. explicit Program(SourceRange source_range, Type program_type)
  235. : ScopeNode(source_range)
  236. , m_type(program_type)
  237. {
  238. }
  239. virtual Value execute(Interpreter&, GlobalObject&) const override;
  240. bool is_strict_mode() const { return m_is_strict_mode; }
  241. void set_strict_mode() { m_is_strict_mode = true; }
  242. Type type() const { return m_type; }
  243. void append_import(NonnullRefPtr<ImportStatement> import_statement)
  244. {
  245. m_imports.append(import_statement);
  246. append(import_statement);
  247. }
  248. void append_export(NonnullRefPtr<ExportStatement> export_statement)
  249. {
  250. m_exports.append(export_statement);
  251. append(export_statement);
  252. }
  253. NonnullRefPtrVector<ImportStatement> const& imports() const { return m_imports; }
  254. NonnullRefPtrVector<ExportStatement> const& exports() const { return m_exports; }
  255. ThrowCompletionOr<void> global_declaration_instantiation(Interpreter& interpreter, GlobalObject& global_object, GlobalEnvironment& global_environment) const;
  256. private:
  257. virtual bool is_program() const override { return true; }
  258. bool m_is_strict_mode { false };
  259. Type m_type { Type::Script };
  260. NonnullRefPtrVector<ImportStatement> m_imports;
  261. NonnullRefPtrVector<ExportStatement> m_exports;
  262. };
  263. class BlockStatement final : public ScopeNode {
  264. public:
  265. explicit BlockStatement(SourceRange source_range)
  266. : ScopeNode(source_range)
  267. {
  268. }
  269. Value execute(Interpreter& interpreter, GlobalObject& object) const override;
  270. };
  271. class FunctionBody final : public ScopeNode {
  272. public:
  273. explicit FunctionBody(SourceRange source_range)
  274. : ScopeNode(source_range)
  275. {
  276. }
  277. void set_strict_mode() { m_in_strict_mode = true; }
  278. bool in_strict_mode() const { return m_in_strict_mode; }
  279. virtual Value execute(Interpreter&, GlobalObject&) const override;
  280. private:
  281. bool m_in_strict_mode { false };
  282. };
  283. class Expression : public ASTNode {
  284. public:
  285. explicit Expression(SourceRange source_range)
  286. : ASTNode(source_range)
  287. {
  288. }
  289. virtual Reference to_reference(Interpreter&, GlobalObject&) const;
  290. };
  291. class Declaration : public Statement {
  292. public:
  293. explicit Declaration(SourceRange source_range)
  294. : Statement(source_range)
  295. {
  296. }
  297. virtual void for_each_bound_name(IteratorOrVoidFunction<FlyString const&> callback) const = 0;
  298. // 8.1.3 Static Semantics: IsConstantDeclaration, https://tc39.es/ecma262/#sec-static-semantics-isconstantdeclaration
  299. virtual bool is_constant_declaration() const { return false; }
  300. virtual bool is_function_declaration() const { return false; }
  301. virtual bool is_lexical_declaration() const { return false; }
  302. };
  303. class ErrorDeclaration final : public Declaration {
  304. public:
  305. explicit ErrorDeclaration(SourceRange source_range)
  306. : Declaration(source_range)
  307. {
  308. }
  309. Value execute(Interpreter&, GlobalObject&) const override { return {}; }
  310. void for_each_bound_name(IteratorOrVoidFunction<FlyString const&>) const override
  311. {
  312. VERIFY_NOT_REACHED();
  313. }
  314. };
  315. struct BindingPattern : RefCounted<BindingPattern> {
  316. // This covers both BindingProperty and BindingElement, hence the more generic name
  317. struct BindingEntry {
  318. // If this entry represents a BindingElement, then name will be Empty
  319. Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<Expression>, Empty> name {};
  320. Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>, NonnullRefPtr<MemberExpression>, Empty> alias {};
  321. RefPtr<Expression> initializer {};
  322. bool is_rest { false };
  323. bool is_elision() const { return name.has<Empty>() && alias.has<Empty>(); }
  324. };
  325. enum class Kind {
  326. Array,
  327. Object,
  328. };
  329. void dump(int indent) const;
  330. template<typename C>
  331. void for_each_bound_name(C&& callback) const;
  332. bool contains_expression() const;
  333. Vector<BindingEntry> entries;
  334. Kind kind { Kind::Object };
  335. };
  336. class FunctionNode {
  337. public:
  338. struct Parameter {
  339. Variant<FlyString, NonnullRefPtr<BindingPattern>> binding;
  340. RefPtr<Expression> default_value;
  341. bool is_rest { false };
  342. };
  343. FlyString const& name() const { return m_name; }
  344. Statement const& body() const { return *m_body; }
  345. Vector<Parameter> const& parameters() const { return m_parameters; };
  346. i32 function_length() const { return m_function_length; }
  347. bool is_strict_mode() const { return m_is_strict_mode; }
  348. bool might_need_arguments_object() const { return m_might_need_arguments_object; }
  349. bool is_arrow_function() const { return m_is_arrow_function; }
  350. FunctionKind kind() const { return m_kind; }
  351. protected:
  352. FunctionNode(FlyString name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool is_arrow_function)
  353. : m_name(move(name))
  354. , m_body(move(body))
  355. , m_parameters(move(parameters))
  356. , m_function_length(function_length)
  357. , m_kind(kind)
  358. , m_is_strict_mode(is_strict_mode)
  359. , m_might_need_arguments_object(might_need_arguments_object)
  360. , m_is_arrow_function(is_arrow_function)
  361. {
  362. if (m_is_arrow_function)
  363. VERIFY(!m_might_need_arguments_object);
  364. }
  365. void dump(int indent, String const& class_name) const;
  366. protected:
  367. void set_name(FlyString name)
  368. {
  369. VERIFY(m_name.is_empty());
  370. m_name = move(name);
  371. }
  372. private:
  373. FlyString m_name;
  374. NonnullRefPtr<Statement> m_body;
  375. Vector<Parameter> const m_parameters;
  376. const i32 m_function_length;
  377. FunctionKind m_kind;
  378. bool m_is_strict_mode { false };
  379. bool m_might_need_arguments_object { false };
  380. bool m_is_arrow_function { false };
  381. };
  382. class FunctionDeclaration final
  383. : public Declaration
  384. , public FunctionNode {
  385. public:
  386. static bool must_have_name() { return true; }
  387. FunctionDeclaration(SourceRange source_range, FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object)
  388. : Declaration(source_range)
  389. , FunctionNode(name, move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, false)
  390. {
  391. }
  392. virtual Value execute(Interpreter&, GlobalObject&) const override;
  393. virtual void dump(int indent) const override;
  394. virtual void generate_bytecode(Bytecode::Generator&) const override;
  395. void for_each_bound_name(IteratorOrVoidFunction<FlyString const&> callback) const override;
  396. virtual bool is_function_declaration() const override { return true; }
  397. void set_should_do_additional_annexB_steps() { m_is_hoisted = true; }
  398. private:
  399. bool m_is_hoisted { false };
  400. };
  401. class FunctionExpression final
  402. : public Expression
  403. , public FunctionNode {
  404. public:
  405. static bool must_have_name() { return false; }
  406. FunctionExpression(SourceRange source_range, FlyString const& name, NonnullRefPtr<Statement> body, Vector<Parameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool is_arrow_function = false)
  407. : Expression(source_range)
  408. , FunctionNode(name, move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, is_arrow_function)
  409. {
  410. }
  411. virtual Value execute(Interpreter&, GlobalObject&) const override;
  412. virtual void dump(int indent) const override;
  413. virtual void generate_bytecode(Bytecode::Generator&) const override;
  414. bool has_name() const { return !name().is_empty(); }
  415. Value instantiate_ordinary_function_expression(Interpreter& interpreter, GlobalObject& global_object, FlyString given_name) const;
  416. };
  417. class ErrorExpression final : public Expression {
  418. public:
  419. explicit ErrorExpression(SourceRange source_range)
  420. : Expression(source_range)
  421. {
  422. }
  423. Value execute(Interpreter&, GlobalObject&) const override { return {}; }
  424. };
  425. class YieldExpression final : public Expression {
  426. public:
  427. explicit YieldExpression(SourceRange source_range, RefPtr<Expression> argument, bool is_yield_from)
  428. : Expression(source_range)
  429. , m_argument(move(argument))
  430. , m_is_yield_from(is_yield_from)
  431. {
  432. }
  433. Expression const* argument() const { return m_argument; }
  434. bool is_yield_from() const { return m_is_yield_from; }
  435. virtual Value execute(Interpreter&, GlobalObject&) const override;
  436. virtual void dump(int indent) const override;
  437. virtual void generate_bytecode(Bytecode::Generator&) const override;
  438. private:
  439. RefPtr<Expression> m_argument;
  440. bool m_is_yield_from { false };
  441. };
  442. class ReturnStatement final : public Statement {
  443. public:
  444. explicit ReturnStatement(SourceRange source_range, RefPtr<Expression> argument)
  445. : Statement(source_range)
  446. , m_argument(move(argument))
  447. {
  448. }
  449. Expression const* argument() const { return m_argument; }
  450. virtual Value execute(Interpreter&, GlobalObject&) const override;
  451. virtual void dump(int indent) const override;
  452. virtual void generate_bytecode(Bytecode::Generator&) const override;
  453. private:
  454. RefPtr<Expression> m_argument;
  455. };
  456. class IfStatement final : public Statement {
  457. public:
  458. IfStatement(SourceRange source_range, NonnullRefPtr<Expression> predicate, NonnullRefPtr<Statement> consequent, RefPtr<Statement> alternate)
  459. : Statement(source_range)
  460. , m_predicate(move(predicate))
  461. , m_consequent(move(consequent))
  462. , m_alternate(move(alternate))
  463. {
  464. }
  465. Expression const& predicate() const { return *m_predicate; }
  466. Statement const& consequent() const { return *m_consequent; }
  467. Statement const* alternate() const { return m_alternate; }
  468. virtual Value execute(Interpreter&, GlobalObject&) const override;
  469. virtual void dump(int indent) const override;
  470. virtual void generate_bytecode(Bytecode::Generator&) const override;
  471. private:
  472. NonnullRefPtr<Expression> m_predicate;
  473. NonnullRefPtr<Statement> m_consequent;
  474. RefPtr<Statement> m_alternate;
  475. };
  476. class WhileStatement final : public IterationStatement {
  477. public:
  478. WhileStatement(SourceRange source_range, NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  479. : IterationStatement(source_range)
  480. , m_test(move(test))
  481. , m_body(move(body))
  482. {
  483. }
  484. Expression const& test() const { return *m_test; }
  485. Statement const& body() const { return *m_body; }
  486. virtual Value execute(Interpreter&, GlobalObject&) const override;
  487. virtual void dump(int indent) const override;
  488. virtual void generate_bytecode(Bytecode::Generator&) const override;
  489. private:
  490. NonnullRefPtr<Expression> m_test;
  491. NonnullRefPtr<Statement> m_body;
  492. };
  493. class DoWhileStatement final : public IterationStatement {
  494. public:
  495. DoWhileStatement(SourceRange source_range, NonnullRefPtr<Expression> test, NonnullRefPtr<Statement> body)
  496. : IterationStatement(source_range)
  497. , m_test(move(test))
  498. , m_body(move(body))
  499. {
  500. }
  501. Expression const& test() const { return *m_test; }
  502. Statement const& body() const { return *m_body; }
  503. virtual Value execute(Interpreter&, GlobalObject&) const override;
  504. virtual void dump(int indent) const override;
  505. virtual void generate_bytecode(Bytecode::Generator&) const override;
  506. private:
  507. NonnullRefPtr<Expression> m_test;
  508. NonnullRefPtr<Statement> m_body;
  509. };
  510. class WithStatement final : public Statement {
  511. public:
  512. WithStatement(SourceRange source_range, NonnullRefPtr<Expression> object, NonnullRefPtr<Statement> body)
  513. : Statement(source_range)
  514. , m_object(move(object))
  515. , m_body(move(body))
  516. {
  517. }
  518. Expression const& object() const { return *m_object; }
  519. Statement const& body() const { return *m_body; }
  520. virtual Value execute(Interpreter&, GlobalObject&) const override;
  521. virtual void dump(int indent) const override;
  522. private:
  523. NonnullRefPtr<Expression> m_object;
  524. NonnullRefPtr<Statement> m_body;
  525. };
  526. class ForStatement final : public IterationStatement {
  527. public:
  528. ForStatement(SourceRange source_range, RefPtr<ASTNode> init, RefPtr<Expression> test, RefPtr<Expression> update, NonnullRefPtr<Statement> body)
  529. : IterationStatement(source_range)
  530. , m_init(move(init))
  531. , m_test(move(test))
  532. , m_update(move(update))
  533. , m_body(move(body))
  534. {
  535. }
  536. ASTNode const* init() const { return m_init; }
  537. Expression const* test() const { return m_test; }
  538. Expression const* update() const { return m_update; }
  539. Statement const& body() const { return *m_body; }
  540. virtual Value execute(Interpreter&, GlobalObject&) const override;
  541. virtual void dump(int indent) const override;
  542. virtual void generate_bytecode(Bytecode::Generator&) const override;
  543. private:
  544. RefPtr<ASTNode> m_init;
  545. RefPtr<Expression> m_test;
  546. RefPtr<Expression> m_update;
  547. NonnullRefPtr<Statement> m_body;
  548. };
  549. class ForInStatement final : public IterationStatement {
  550. public:
  551. ForInStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> lhs, NonnullRefPtr<Expression> rhs, NonnullRefPtr<Statement> body)
  552. : IterationStatement(source_range)
  553. , m_lhs(move(lhs))
  554. , m_rhs(move(rhs))
  555. , m_body(move(body))
  556. {
  557. }
  558. auto const& lhs() const { return m_lhs; }
  559. Expression const& rhs() const { return *m_rhs; }
  560. Statement const& body() const { return *m_body; }
  561. virtual Value execute(Interpreter&, GlobalObject&) const override;
  562. virtual void dump(int indent) const override;
  563. private:
  564. Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> m_lhs;
  565. NonnullRefPtr<Expression> m_rhs;
  566. NonnullRefPtr<Statement> m_body;
  567. };
  568. class ForOfStatement final : public IterationStatement {
  569. public:
  570. ForOfStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> lhs, NonnullRefPtr<Expression> rhs, NonnullRefPtr<Statement> body)
  571. : IterationStatement(source_range)
  572. , m_lhs(move(lhs))
  573. , m_rhs(move(rhs))
  574. , m_body(move(body))
  575. {
  576. }
  577. auto const& lhs() const { return m_lhs; }
  578. Expression const& rhs() const { return *m_rhs; }
  579. Statement const& body() const { return *m_body; }
  580. virtual Value execute(Interpreter&, GlobalObject&) const override;
  581. virtual void dump(int indent) const override;
  582. private:
  583. Variant<NonnullRefPtr<ASTNode>, NonnullRefPtr<BindingPattern>> m_lhs;
  584. NonnullRefPtr<Expression> m_rhs;
  585. NonnullRefPtr<Statement> m_body;
  586. };
  587. enum class BinaryOp {
  588. Addition,
  589. Subtraction,
  590. Multiplication,
  591. Division,
  592. Modulo,
  593. Exponentiation,
  594. StrictlyEquals,
  595. StrictlyInequals,
  596. LooselyEquals,
  597. LooselyInequals,
  598. GreaterThan,
  599. GreaterThanEquals,
  600. LessThan,
  601. LessThanEquals,
  602. BitwiseAnd,
  603. BitwiseOr,
  604. BitwiseXor,
  605. LeftShift,
  606. RightShift,
  607. UnsignedRightShift,
  608. In,
  609. InstanceOf,
  610. };
  611. class BinaryExpression final : public Expression {
  612. public:
  613. BinaryExpression(SourceRange source_range, BinaryOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  614. : Expression(source_range)
  615. , m_op(op)
  616. , m_lhs(move(lhs))
  617. , m_rhs(move(rhs))
  618. {
  619. }
  620. virtual Value execute(Interpreter&, GlobalObject&) const override;
  621. virtual void dump(int indent) const override;
  622. virtual void generate_bytecode(Bytecode::Generator&) const override;
  623. private:
  624. BinaryOp m_op;
  625. NonnullRefPtr<Expression> m_lhs;
  626. NonnullRefPtr<Expression> m_rhs;
  627. };
  628. enum class LogicalOp {
  629. And,
  630. Or,
  631. NullishCoalescing,
  632. };
  633. class LogicalExpression final : public Expression {
  634. public:
  635. LogicalExpression(SourceRange source_range, LogicalOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  636. : Expression(source_range)
  637. , m_op(op)
  638. , m_lhs(move(lhs))
  639. , m_rhs(move(rhs))
  640. {
  641. }
  642. virtual Value execute(Interpreter&, GlobalObject&) const override;
  643. virtual void dump(int indent) const override;
  644. virtual void generate_bytecode(Bytecode::Generator&) const override;
  645. private:
  646. LogicalOp m_op;
  647. NonnullRefPtr<Expression> m_lhs;
  648. NonnullRefPtr<Expression> m_rhs;
  649. };
  650. enum class UnaryOp {
  651. BitwiseNot,
  652. Not,
  653. Plus,
  654. Minus,
  655. Typeof,
  656. Void,
  657. Delete,
  658. };
  659. class UnaryExpression final : public Expression {
  660. public:
  661. UnaryExpression(SourceRange source_range, UnaryOp op, NonnullRefPtr<Expression> lhs)
  662. : Expression(source_range)
  663. , m_op(op)
  664. , m_lhs(move(lhs))
  665. {
  666. }
  667. virtual Value execute(Interpreter&, GlobalObject&) const override;
  668. virtual void dump(int indent) const override;
  669. virtual void generate_bytecode(Bytecode::Generator&) const override;
  670. private:
  671. UnaryOp m_op;
  672. NonnullRefPtr<Expression> m_lhs;
  673. };
  674. class SequenceExpression final : public Expression {
  675. public:
  676. SequenceExpression(SourceRange source_range, NonnullRefPtrVector<Expression> expressions)
  677. : Expression(source_range)
  678. , m_expressions(move(expressions))
  679. {
  680. VERIFY(m_expressions.size() >= 2);
  681. }
  682. virtual void dump(int indent) const override;
  683. virtual Value execute(Interpreter&, GlobalObject&) const override;
  684. virtual void generate_bytecode(Bytecode::Generator&) const override;
  685. private:
  686. NonnullRefPtrVector<Expression> m_expressions;
  687. };
  688. class Literal : public Expression {
  689. protected:
  690. explicit Literal(SourceRange source_range)
  691. : Expression(source_range)
  692. {
  693. }
  694. };
  695. class BooleanLiteral final : public Literal {
  696. public:
  697. explicit BooleanLiteral(SourceRange source_range, bool value)
  698. : Literal(source_range)
  699. , m_value(value)
  700. {
  701. }
  702. virtual Value execute(Interpreter&, GlobalObject&) const override;
  703. virtual void dump(int indent) const override;
  704. virtual void generate_bytecode(Bytecode::Generator&) const override;
  705. private:
  706. bool m_value { false };
  707. };
  708. class NumericLiteral final : public Literal {
  709. public:
  710. explicit NumericLiteral(SourceRange source_range, double value)
  711. : Literal(source_range)
  712. , m_value(value)
  713. {
  714. }
  715. virtual Value execute(Interpreter&, GlobalObject&) const override;
  716. virtual void dump(int indent) const override;
  717. virtual void generate_bytecode(Bytecode::Generator&) const override;
  718. private:
  719. Value m_value;
  720. };
  721. class BigIntLiteral final : public Literal {
  722. public:
  723. explicit BigIntLiteral(SourceRange source_range, String value)
  724. : Literal(source_range)
  725. , m_value(move(value))
  726. {
  727. }
  728. virtual Value execute(Interpreter&, GlobalObject&) const override;
  729. virtual void dump(int indent) const override;
  730. virtual void generate_bytecode(Bytecode::Generator&) const override;
  731. private:
  732. String m_value;
  733. };
  734. class StringLiteral final : public Literal {
  735. public:
  736. explicit StringLiteral(SourceRange source_range, String value, bool is_use_strict_directive = false)
  737. : Literal(source_range)
  738. , m_value(move(value))
  739. , m_is_use_strict_directive(is_use_strict_directive)
  740. {
  741. }
  742. virtual Value execute(Interpreter&, GlobalObject&) const override;
  743. virtual void dump(int indent) const override;
  744. virtual void generate_bytecode(Bytecode::Generator&) const override;
  745. StringView value() const { return m_value; }
  746. bool is_use_strict_directive() const { return m_is_use_strict_directive; };
  747. private:
  748. String m_value;
  749. bool m_is_use_strict_directive;
  750. };
  751. class NullLiteral final : public Literal {
  752. public:
  753. explicit NullLiteral(SourceRange source_range)
  754. : Literal(source_range)
  755. {
  756. }
  757. virtual Value execute(Interpreter&, GlobalObject&) const override;
  758. virtual void dump(int indent) const override;
  759. virtual void generate_bytecode(Bytecode::Generator&) const override;
  760. };
  761. class RegExpLiteral final : public Literal {
  762. public:
  763. RegExpLiteral(SourceRange source_range, regex::Parser::Result parsed_regex, String parsed_pattern, regex::RegexOptions<ECMAScriptFlags> parsed_flags, String pattern, String flags)
  764. : Literal(source_range)
  765. , m_parsed_regex(move(parsed_regex))
  766. , m_parsed_pattern(move(parsed_pattern))
  767. , m_parsed_flags(move(parsed_flags))
  768. , m_pattern(move(pattern))
  769. , m_flags(move(flags))
  770. {
  771. }
  772. virtual Value execute(Interpreter&, GlobalObject&) const override;
  773. virtual void dump(int indent) const override;
  774. virtual void generate_bytecode(Bytecode::Generator&) const override;
  775. regex::Parser::Result const& parsed_regex() const { return m_parsed_regex; }
  776. String const& parsed_pattern() const { return m_parsed_pattern; }
  777. regex::RegexOptions<ECMAScriptFlags> const& parsed_flags() const { return m_parsed_flags; }
  778. String const& pattern() const { return m_pattern; }
  779. String const& flags() const { return m_flags; }
  780. private:
  781. regex::Parser::Result m_parsed_regex;
  782. String m_parsed_pattern;
  783. regex::RegexOptions<ECMAScriptFlags> m_parsed_flags;
  784. String m_pattern;
  785. String m_flags;
  786. };
  787. class Identifier final : public Expression {
  788. public:
  789. explicit Identifier(SourceRange source_range, FlyString string)
  790. : Expression(source_range)
  791. , m_string(move(string))
  792. {
  793. }
  794. FlyString const& string() const { return m_string; }
  795. virtual Value execute(Interpreter&, GlobalObject&) const override;
  796. virtual void dump(int indent) const override;
  797. virtual Reference to_reference(Interpreter&, GlobalObject&) const override;
  798. virtual void generate_bytecode(Bytecode::Generator&) const override;
  799. private:
  800. virtual bool is_identifier() const override { return true; }
  801. FlyString m_string;
  802. mutable Optional<EnvironmentCoordinate> m_cached_environment_coordinate;
  803. };
  804. class ClassMethod final : public ASTNode {
  805. public:
  806. enum class Kind {
  807. Method,
  808. Getter,
  809. Setter,
  810. };
  811. ClassMethod(SourceRange source_range, NonnullRefPtr<Expression> key, NonnullRefPtr<FunctionExpression> function, Kind kind, bool is_static)
  812. : ASTNode(source_range)
  813. , m_key(move(key))
  814. , m_function(move(function))
  815. , m_kind(kind)
  816. , m_is_static(is_static)
  817. {
  818. }
  819. Expression const& key() const { return *m_key; }
  820. Kind kind() const { return m_kind; }
  821. bool is_static() const { return m_is_static; }
  822. virtual Value execute(Interpreter&, GlobalObject&) const override;
  823. virtual void dump(int indent) const override;
  824. private:
  825. NonnullRefPtr<Expression> m_key;
  826. NonnullRefPtr<FunctionExpression> m_function;
  827. Kind m_kind;
  828. bool m_is_static;
  829. };
  830. class ClassField final : public ASTNode {
  831. public:
  832. ClassField(SourceRange source_range, NonnullRefPtr<Expression> key, RefPtr<Expression> init, bool is_static)
  833. : ASTNode(source_range)
  834. , m_key(move(key))
  835. , m_initializer(move(init))
  836. , m_is_static(is_static)
  837. {
  838. }
  839. Expression const& key() const { return *m_key; }
  840. bool is_static() const { return m_is_static; }
  841. RefPtr<Expression> const& initializer() const { return m_initializer; }
  842. RefPtr<Expression>& initializer() { return m_initializer; }
  843. virtual Value execute(Interpreter&, GlobalObject&) const override;
  844. virtual void dump(int indent) const override;
  845. private:
  846. NonnullRefPtr<Expression> m_key;
  847. RefPtr<Expression> m_initializer;
  848. bool m_is_static;
  849. };
  850. class SuperExpression final : public Expression {
  851. public:
  852. explicit SuperExpression(SourceRange source_range)
  853. : Expression(source_range)
  854. {
  855. }
  856. virtual Value execute(Interpreter&, GlobalObject&) const override;
  857. virtual void dump(int indent) const override;
  858. virtual bool is_super_expression() const override { return true; }
  859. };
  860. class ClassExpression final : public Expression {
  861. public:
  862. ClassExpression(SourceRange source_range, String name, RefPtr<FunctionExpression> constructor, RefPtr<Expression> super_class, NonnullRefPtrVector<ClassMethod> methods, NonnullRefPtrVector<ClassField> fields)
  863. : Expression(source_range)
  864. , m_name(move(name))
  865. , m_constructor(move(constructor))
  866. , m_super_class(move(super_class))
  867. , m_methods(move(methods))
  868. , m_fields(move(fields))
  869. {
  870. }
  871. StringView name() const { return m_name; }
  872. RefPtr<FunctionExpression> constructor() const { return m_constructor; }
  873. virtual Value execute(Interpreter&, GlobalObject&) const override;
  874. virtual void dump(int indent) const override;
  875. bool has_name() const { return !m_name.is_empty(); }
  876. ThrowCompletionOr<Value> class_definition_evaluation(Interpreter& interpreter, GlobalObject& global_object, FlyString const& binding_name = {}, FlyString const& class_name = {}) const;
  877. private:
  878. String m_name;
  879. RefPtr<FunctionExpression> m_constructor;
  880. RefPtr<Expression> m_super_class;
  881. NonnullRefPtrVector<ClassMethod> m_methods;
  882. NonnullRefPtrVector<ClassField> m_fields;
  883. };
  884. class ClassDeclaration final : public Declaration {
  885. public:
  886. ClassDeclaration(SourceRange source_range, NonnullRefPtr<ClassExpression> class_expression)
  887. : Declaration(source_range)
  888. , m_class_expression(move(class_expression))
  889. {
  890. }
  891. virtual Value execute(Interpreter&, GlobalObject&) const override;
  892. virtual void dump(int indent) const override;
  893. virtual void generate_bytecode(Bytecode::Generator&) const override;
  894. void for_each_bound_name(IteratorOrVoidFunction<FlyString const&> callback) const override;
  895. virtual bool is_lexical_declaration() const override { return true; }
  896. private:
  897. NonnullRefPtr<ClassExpression> m_class_expression;
  898. };
  899. class SpreadExpression final : public Expression {
  900. public:
  901. explicit SpreadExpression(SourceRange source_range, NonnullRefPtr<Expression> target)
  902. : Expression(source_range)
  903. , m_target(move(target))
  904. {
  905. }
  906. virtual Value execute(Interpreter&, GlobalObject&) const override;
  907. virtual void dump(int indent) const override;
  908. private:
  909. NonnullRefPtr<Expression> m_target;
  910. };
  911. class ThisExpression final : public Expression {
  912. public:
  913. explicit ThisExpression(SourceRange source_range)
  914. : Expression(source_range)
  915. {
  916. }
  917. virtual Value execute(Interpreter&, GlobalObject&) const override;
  918. virtual void dump(int indent) const override;
  919. };
  920. class CallExpression : public Expression {
  921. public:
  922. struct Argument {
  923. NonnullRefPtr<Expression> value;
  924. bool is_spread;
  925. };
  926. CallExpression(SourceRange source_range, NonnullRefPtr<Expression> callee, Vector<Argument> arguments = {})
  927. : Expression(source_range)
  928. , m_callee(move(callee))
  929. , m_arguments(move(arguments))
  930. {
  931. }
  932. virtual Value execute(Interpreter&, GlobalObject&) const override;
  933. virtual void dump(int indent) const override;
  934. virtual void generate_bytecode(Bytecode::Generator&) const override;
  935. Expression const& callee() const { return m_callee; }
  936. protected:
  937. void throw_type_error_for_callee(Interpreter&, GlobalObject&, Value callee_value, StringView call_type) const;
  938. NonnullRefPtr<Expression> m_callee;
  939. Vector<Argument> const m_arguments;
  940. private:
  941. struct ThisAndCallee {
  942. Value this_value;
  943. Value callee;
  944. };
  945. ThisAndCallee compute_this_and_callee(Interpreter&, GlobalObject&, Reference const&) const;
  946. };
  947. class NewExpression final : public CallExpression {
  948. public:
  949. NewExpression(SourceRange source_range, NonnullRefPtr<Expression> callee, Vector<Argument> arguments = {})
  950. : CallExpression(source_range, move(callee), move(arguments))
  951. {
  952. }
  953. virtual Value execute(Interpreter&, GlobalObject&) const override;
  954. virtual bool is_new_expression() const override { return true; }
  955. };
  956. class SuperCall final : public Expression {
  957. public:
  958. SuperCall(SourceRange source_range, Vector<CallExpression::Argument> arguments)
  959. : Expression(source_range)
  960. , m_arguments(move(arguments))
  961. {
  962. }
  963. virtual Value execute(Interpreter&, GlobalObject&) const override;
  964. virtual void dump(int indent) const override;
  965. private:
  966. Vector<CallExpression::Argument> const m_arguments;
  967. };
  968. enum class AssignmentOp {
  969. Assignment,
  970. AdditionAssignment,
  971. SubtractionAssignment,
  972. MultiplicationAssignment,
  973. DivisionAssignment,
  974. ModuloAssignment,
  975. ExponentiationAssignment,
  976. BitwiseAndAssignment,
  977. BitwiseOrAssignment,
  978. BitwiseXorAssignment,
  979. LeftShiftAssignment,
  980. RightShiftAssignment,
  981. UnsignedRightShiftAssignment,
  982. AndAssignment,
  983. OrAssignment,
  984. NullishAssignment,
  985. };
  986. class AssignmentExpression final : public Expression {
  987. public:
  988. AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  989. : Expression(source_range)
  990. , m_op(op)
  991. , m_lhs(move(lhs))
  992. , m_rhs(move(rhs))
  993. {
  994. }
  995. AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr<BindingPattern> lhs, NonnullRefPtr<Expression> rhs)
  996. : Expression(source_range)
  997. , m_op(op)
  998. , m_lhs(move(lhs))
  999. , m_rhs(move(rhs))
  1000. {
  1001. }
  1002. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1003. virtual void dump(int indent) const override;
  1004. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1005. private:
  1006. AssignmentOp m_op;
  1007. Variant<NonnullRefPtr<Expression>, NonnullRefPtr<BindingPattern>> m_lhs;
  1008. NonnullRefPtr<Expression> m_rhs;
  1009. };
  1010. enum class UpdateOp {
  1011. Increment,
  1012. Decrement,
  1013. };
  1014. class UpdateExpression final : public Expression {
  1015. public:
  1016. UpdateExpression(SourceRange source_range, UpdateOp op, NonnullRefPtr<Expression> argument, bool prefixed = false)
  1017. : Expression(source_range)
  1018. , m_op(op)
  1019. , m_argument(move(argument))
  1020. , m_prefixed(prefixed)
  1021. {
  1022. }
  1023. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1024. virtual void dump(int indent) const override;
  1025. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1026. private:
  1027. UpdateOp m_op;
  1028. NonnullRefPtr<Expression> m_argument;
  1029. bool m_prefixed;
  1030. };
  1031. enum class DeclarationKind {
  1032. Var,
  1033. Let,
  1034. Const,
  1035. };
  1036. class VariableDeclarator final : public ASTNode {
  1037. public:
  1038. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier> id)
  1039. : ASTNode(source_range)
  1040. , m_target(move(id))
  1041. {
  1042. }
  1043. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier> target, RefPtr<Expression> init)
  1044. : ASTNode(source_range)
  1045. , m_target(move(target))
  1046. , m_init(move(init))
  1047. {
  1048. }
  1049. VariableDeclarator(SourceRange source_range, Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>> target, RefPtr<Expression> init)
  1050. : ASTNode(source_range)
  1051. , m_target(move(target))
  1052. , m_init(move(init))
  1053. {
  1054. }
  1055. auto& target() const { return m_target; }
  1056. Expression const* init() const { return m_init; }
  1057. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1058. virtual void dump(int indent) const override;
  1059. private:
  1060. Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>> m_target;
  1061. RefPtr<Expression> m_init;
  1062. };
  1063. class VariableDeclaration final : public Declaration {
  1064. public:
  1065. VariableDeclaration(SourceRange source_range, DeclarationKind declaration_kind, NonnullRefPtrVector<VariableDeclarator> declarations)
  1066. : Declaration(source_range)
  1067. , m_declaration_kind(declaration_kind)
  1068. , m_declarations(move(declarations))
  1069. {
  1070. }
  1071. DeclarationKind declaration_kind() const { return m_declaration_kind; }
  1072. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1073. virtual void dump(int indent) const override;
  1074. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1075. NonnullRefPtrVector<VariableDeclarator> const& declarations() const { return m_declarations; }
  1076. virtual void for_each_bound_name(IteratorOrVoidFunction<FlyString const&> callback) const override;
  1077. virtual bool is_constant_declaration() const override { return m_declaration_kind == DeclarationKind::Const; };
  1078. virtual bool is_lexical_declaration() const override { return m_declaration_kind != DeclarationKind::Var; }
  1079. private:
  1080. DeclarationKind m_declaration_kind;
  1081. NonnullRefPtrVector<VariableDeclarator> m_declarations;
  1082. };
  1083. class ObjectProperty final : public ASTNode {
  1084. public:
  1085. enum class Type {
  1086. KeyValue,
  1087. Getter,
  1088. Setter,
  1089. Spread,
  1090. };
  1091. ObjectProperty(SourceRange source_range, NonnullRefPtr<Expression> key, RefPtr<Expression> value, Type property_type, bool is_method)
  1092. : ASTNode(source_range)
  1093. , m_key(move(key))
  1094. , m_value(move(value))
  1095. , m_property_type(property_type)
  1096. , m_is_method(is_method)
  1097. {
  1098. }
  1099. Expression const& key() const { return m_key; }
  1100. Expression const& value() const
  1101. {
  1102. VERIFY(m_value);
  1103. return *m_value;
  1104. }
  1105. Type type() const { return m_property_type; }
  1106. bool is_method() const { return m_is_method; }
  1107. virtual void dump(int indent) const override;
  1108. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1109. private:
  1110. NonnullRefPtr<Expression> m_key;
  1111. RefPtr<Expression> m_value;
  1112. Type m_property_type;
  1113. bool m_is_method { false };
  1114. };
  1115. class ObjectExpression final : public Expression {
  1116. public:
  1117. explicit ObjectExpression(SourceRange source_range, NonnullRefPtrVector<ObjectProperty> properties = {}, Optional<SourceRange> first_invalid_property_range = {})
  1118. : Expression(source_range)
  1119. , m_properties(move(properties))
  1120. , m_first_invalid_property_range(move(first_invalid_property_range))
  1121. {
  1122. }
  1123. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1124. virtual void dump(int indent) const override;
  1125. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1126. Optional<SourceRange> const& invalid_property_range() const { return m_first_invalid_property_range; }
  1127. private:
  1128. NonnullRefPtrVector<ObjectProperty> m_properties;
  1129. Optional<SourceRange> m_first_invalid_property_range;
  1130. };
  1131. class ArrayExpression final : public Expression {
  1132. public:
  1133. ArrayExpression(SourceRange source_range, Vector<RefPtr<Expression>> elements)
  1134. : Expression(source_range)
  1135. , m_elements(move(elements))
  1136. {
  1137. }
  1138. Vector<RefPtr<Expression>> const& elements() const { return m_elements; }
  1139. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1140. virtual void dump(int indent) const override;
  1141. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1142. private:
  1143. Vector<RefPtr<Expression>> m_elements;
  1144. };
  1145. class TemplateLiteral final : public Expression {
  1146. public:
  1147. TemplateLiteral(SourceRange source_range, NonnullRefPtrVector<Expression> expressions)
  1148. : Expression(source_range)
  1149. , m_expressions(move(expressions))
  1150. {
  1151. }
  1152. TemplateLiteral(SourceRange source_range, NonnullRefPtrVector<Expression> expressions, NonnullRefPtrVector<Expression> raw_strings)
  1153. : Expression(source_range)
  1154. , m_expressions(move(expressions))
  1155. , m_raw_strings(move(raw_strings))
  1156. {
  1157. }
  1158. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1159. virtual void dump(int indent) const override;
  1160. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1161. NonnullRefPtrVector<Expression> const& expressions() const { return m_expressions; }
  1162. NonnullRefPtrVector<Expression> const& raw_strings() const { return m_raw_strings; }
  1163. private:
  1164. NonnullRefPtrVector<Expression> const m_expressions;
  1165. NonnullRefPtrVector<Expression> const m_raw_strings;
  1166. };
  1167. class TaggedTemplateLiteral final : public Expression {
  1168. public:
  1169. TaggedTemplateLiteral(SourceRange source_range, NonnullRefPtr<Expression> tag, NonnullRefPtr<TemplateLiteral> template_literal)
  1170. : Expression(source_range)
  1171. , m_tag(move(tag))
  1172. , m_template_literal(move(template_literal))
  1173. {
  1174. }
  1175. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1176. virtual void dump(int indent) const override;
  1177. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1178. private:
  1179. NonnullRefPtr<Expression> const m_tag;
  1180. NonnullRefPtr<TemplateLiteral> const m_template_literal;
  1181. };
  1182. class MemberExpression final : public Expression {
  1183. public:
  1184. MemberExpression(SourceRange source_range, NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false)
  1185. : Expression(source_range)
  1186. , m_object(move(object))
  1187. , m_property(move(property))
  1188. , m_computed(computed)
  1189. {
  1190. }
  1191. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1192. virtual void dump(int indent) const override;
  1193. virtual Reference to_reference(Interpreter&, GlobalObject&) const override;
  1194. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1195. bool is_computed() const { return m_computed; }
  1196. Expression const& object() const { return *m_object; }
  1197. Expression const& property() const { return *m_property; }
  1198. PropertyName computed_property_name(Interpreter&, GlobalObject&) const;
  1199. String to_string_approximation() const;
  1200. private:
  1201. virtual bool is_member_expression() const override { return true; }
  1202. NonnullRefPtr<Expression> m_object;
  1203. NonnullRefPtr<Expression> m_property;
  1204. bool m_computed { false };
  1205. };
  1206. class OptionalChain final : public Expression {
  1207. public:
  1208. enum class Mode {
  1209. Optional,
  1210. NotOptional,
  1211. };
  1212. struct Call {
  1213. Vector<CallExpression::Argument> arguments;
  1214. Mode mode;
  1215. };
  1216. struct ComputedReference {
  1217. NonnullRefPtr<Expression> expression;
  1218. Mode mode;
  1219. };
  1220. struct MemberReference {
  1221. NonnullRefPtr<Identifier> identifier;
  1222. Mode mode;
  1223. };
  1224. using Reference = Variant<Call, ComputedReference, MemberReference>;
  1225. OptionalChain(SourceRange source_range, NonnullRefPtr<Expression> base, Vector<Reference> references)
  1226. : Expression(source_range)
  1227. , m_base(move(base))
  1228. , m_references(move(references))
  1229. {
  1230. }
  1231. virtual Value execute(Interpreter& interpreter, GlobalObject& global_object) const override;
  1232. virtual JS::Reference to_reference(Interpreter& interpreter, GlobalObject& global_object) const override;
  1233. virtual void dump(int indent) const override;
  1234. private:
  1235. struct ReferenceAndValue {
  1236. JS::Reference reference;
  1237. Value value;
  1238. };
  1239. Optional<ReferenceAndValue> to_reference_and_value(Interpreter&, GlobalObject&) const;
  1240. NonnullRefPtr<Expression> m_base;
  1241. Vector<Reference> m_references;
  1242. };
  1243. class MetaProperty final : public Expression {
  1244. public:
  1245. enum class Type {
  1246. NewTarget,
  1247. ImportMeta,
  1248. };
  1249. MetaProperty(SourceRange source_range, Type type)
  1250. : Expression(source_range)
  1251. , m_type(type)
  1252. {
  1253. }
  1254. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1255. virtual void dump(int indent) const override;
  1256. private:
  1257. Type m_type;
  1258. };
  1259. class ConditionalExpression final : public Expression {
  1260. public:
  1261. ConditionalExpression(SourceRange source_range, NonnullRefPtr<Expression> test, NonnullRefPtr<Expression> consequent, NonnullRefPtr<Expression> alternate)
  1262. : Expression(source_range)
  1263. , m_test(move(test))
  1264. , m_consequent(move(consequent))
  1265. , m_alternate(move(alternate))
  1266. {
  1267. }
  1268. virtual void dump(int indent) const override;
  1269. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1270. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1271. private:
  1272. NonnullRefPtr<Expression> m_test;
  1273. NonnullRefPtr<Expression> m_consequent;
  1274. NonnullRefPtr<Expression> m_alternate;
  1275. };
  1276. class CatchClause final : public ASTNode {
  1277. public:
  1278. CatchClause(SourceRange source_range, FlyString parameter, NonnullRefPtr<BlockStatement> body)
  1279. : ASTNode(source_range)
  1280. , m_parameter(move(parameter))
  1281. , m_body(move(body))
  1282. {
  1283. }
  1284. CatchClause(SourceRange source_range, NonnullRefPtr<BindingPattern> parameter, NonnullRefPtr<BlockStatement> body)
  1285. : ASTNode(source_range)
  1286. , m_parameter(move(parameter))
  1287. , m_body(move(body))
  1288. {
  1289. }
  1290. auto& parameter() const { return m_parameter; }
  1291. BlockStatement const& body() const { return m_body; }
  1292. virtual void dump(int indent) const override;
  1293. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1294. private:
  1295. Variant<FlyString, NonnullRefPtr<BindingPattern>> m_parameter;
  1296. NonnullRefPtr<BlockStatement> m_body;
  1297. };
  1298. class TryStatement final : public LabelableStatement {
  1299. public:
  1300. TryStatement(SourceRange source_range, NonnullRefPtr<BlockStatement> block, RefPtr<CatchClause> handler, RefPtr<BlockStatement> finalizer)
  1301. : LabelableStatement(source_range)
  1302. , m_block(move(block))
  1303. , m_handler(move(handler))
  1304. , m_finalizer(move(finalizer))
  1305. {
  1306. }
  1307. BlockStatement const& block() const { return m_block; }
  1308. CatchClause const* handler() const { return m_handler; }
  1309. BlockStatement const* finalizer() const { return m_finalizer; }
  1310. virtual void dump(int indent) const override;
  1311. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1312. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1313. void add_label(FlyString string) override;
  1314. private:
  1315. NonnullRefPtr<BlockStatement> m_block;
  1316. RefPtr<CatchClause> m_handler;
  1317. RefPtr<BlockStatement> m_finalizer;
  1318. };
  1319. class ThrowStatement final : public Statement {
  1320. public:
  1321. explicit ThrowStatement(SourceRange source_range, NonnullRefPtr<Expression> argument)
  1322. : Statement(source_range)
  1323. , m_argument(move(argument))
  1324. {
  1325. }
  1326. Expression const& argument() const { return m_argument; }
  1327. virtual void dump(int indent) const override;
  1328. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1329. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1330. private:
  1331. NonnullRefPtr<Expression> m_argument;
  1332. };
  1333. class SwitchCase final : public ScopeNode {
  1334. public:
  1335. SwitchCase(SourceRange source_range, RefPtr<Expression> test)
  1336. : ScopeNode(source_range)
  1337. , m_test(move(test))
  1338. {
  1339. }
  1340. Expression const* test() const { return m_test; }
  1341. virtual void dump(int indent) const override;
  1342. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1343. private:
  1344. RefPtr<Expression> m_test;
  1345. };
  1346. class SwitchStatement final : public ScopeNode {
  1347. public:
  1348. SwitchStatement(SourceRange source_range, NonnullRefPtr<Expression> discriminant)
  1349. : ScopeNode(source_range)
  1350. , m_discriminant(move(discriminant))
  1351. {
  1352. }
  1353. virtual void dump(int indent) const override;
  1354. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1355. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1356. void add_case(NonnullRefPtr<SwitchCase> switch_case) { m_cases.append(move(switch_case)); }
  1357. private:
  1358. NonnullRefPtr<Expression> m_discriminant;
  1359. NonnullRefPtrVector<SwitchCase> m_cases;
  1360. };
  1361. class BreakStatement final : public Statement {
  1362. public:
  1363. BreakStatement(SourceRange source_range, FlyString target_label)
  1364. : Statement(source_range)
  1365. , m_target_label(move(target_label))
  1366. {
  1367. }
  1368. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1369. FlyString const& target_label() const { return m_target_label; }
  1370. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1371. private:
  1372. FlyString m_target_label;
  1373. };
  1374. class ContinueStatement final : public Statement {
  1375. public:
  1376. ContinueStatement(SourceRange source_range, FlyString target_label)
  1377. : Statement(source_range)
  1378. , m_target_label(move(target_label))
  1379. {
  1380. }
  1381. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1382. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1383. FlyString const& target_label() const { return m_target_label; }
  1384. private:
  1385. FlyString m_target_label;
  1386. };
  1387. class DebuggerStatement final : public Statement {
  1388. public:
  1389. explicit DebuggerStatement(SourceRange source_range)
  1390. : Statement(source_range)
  1391. {
  1392. }
  1393. virtual Value execute(Interpreter&, GlobalObject&) const override;
  1394. virtual void generate_bytecode(Bytecode::Generator&) const override;
  1395. };
  1396. class SyntheticReferenceExpression final : public Expression {
  1397. public:
  1398. explicit SyntheticReferenceExpression(SourceRange source_range, Reference reference, Value value)
  1399. : Expression(source_range)
  1400. , m_reference(move(reference))
  1401. , m_value(value)
  1402. {
  1403. }
  1404. virtual Value execute(Interpreter&, GlobalObject&) const override { return m_value; }
  1405. virtual Reference to_reference(Interpreter&, GlobalObject&) const override { return m_reference; }
  1406. private:
  1407. Reference m_reference;
  1408. Value m_value;
  1409. };
  1410. template<typename C>
  1411. void BindingPattern::for_each_bound_name(C&& callback) const
  1412. {
  1413. for (auto& entry : entries) {
  1414. auto& alias = entry.alias;
  1415. if (alias.has<NonnullRefPtr<Identifier>>()) {
  1416. callback(alias.get<NonnullRefPtr<Identifier>>()->string());
  1417. } else if (alias.has<NonnullRefPtr<BindingPattern>>()) {
  1418. alias.get<NonnullRefPtr<BindingPattern>>()->for_each_bound_name(forward<C>(callback));
  1419. } else {
  1420. auto& name = entry.name;
  1421. if (name.has<NonnullRefPtr<Identifier>>())
  1422. callback(name.get<NonnullRefPtr<Identifier>>()->string());
  1423. }
  1424. }
  1425. }
  1426. template<>
  1427. inline bool ASTNode::fast_is<NewExpression>() const { return is_new_expression(); }
  1428. template<>
  1429. inline bool ASTNode::fast_is<MemberExpression>() const { return is_member_expression(); }
  1430. template<>
  1431. inline bool ASTNode::fast_is<SuperExpression>() const { return is_super_expression(); }
  1432. template<>
  1433. inline bool ASTNode::fast_is<Identifier>() const { return is_identifier(); }
  1434. template<>
  1435. inline bool ASTNode::fast_is<ExpressionStatement>() const { return is_expression_statement(); }
  1436. template<>
  1437. inline bool ASTNode::fast_is<ScopeNode>() const { return is_scope_node(); }
  1438. template<>
  1439. inline bool ASTNode::fast_is<Program>() const { return is_program(); }
  1440. }