AST.h 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021-2022, David Tuin <davidot@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/DeprecatedFlyString.h>
  10. #include <AK/DeprecatedString.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/OwnPtr.h>
  13. #include <AK/RefPtr.h>
  14. #include <AK/Variant.h>
  15. #include <AK/Vector.h>
  16. #include <LibJS/Bytecode/CodeGenerationError.h>
  17. #include <LibJS/Bytecode/IdentifierTable.h>
  18. #include <LibJS/Forward.h>
  19. #include <LibJS/Heap/Handle.h>
  20. #include <LibJS/Runtime/ClassFieldDefinition.h>
  21. #include <LibJS/Runtime/Completion.h>
  22. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  23. #include <LibJS/Runtime/FunctionKind.h>
  24. #include <LibJS/Runtime/ModuleRequest.h>
  25. #include <LibJS/Runtime/PropertyKey.h>
  26. #include <LibJS/Runtime/Reference.h>
  27. #include <LibJS/Runtime/Value.h>
  28. #include <LibJS/SourceRange.h>
  29. #include <LibRegex/Regex.h>
  30. namespace JS {
  31. class Declaration;
  32. class ClassDeclaration;
  33. class FunctionDeclaration;
  34. class Identifier;
  35. class MemberExpression;
  36. class VariableDeclaration;
  37. template<class T, class... Args>
  38. static inline NonnullRefPtr<T>
  39. create_ast_node(SourceRange range, Args&&... args)
  40. {
  41. return adopt_ref(*new T(move(range), forward<Args>(args)...));
  42. }
  43. class ASTNode : public RefCounted<ASTNode> {
  44. public:
  45. virtual ~ASTNode() = default;
  46. // NOTE: This is here to stop ASAN complaining about mismatch between new/delete sizes in ASTNodeWithTailArray.
  47. void operator delete(void* ptr) { ::operator delete(ptr); }
  48. virtual Completion execute(Interpreter&) const = 0;
  49. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const;
  50. virtual void dump(int indent) const;
  51. [[nodiscard]] SourceRange source_range() const;
  52. u32 start_offset() const { return m_start_offset; }
  53. u32 end_offset() const { return m_end_offset; }
  54. SourceCode const& source_code() const { return *m_source_code; }
  55. void set_end_offset(Badge<Parser>, u32 end_offset) { m_end_offset = end_offset; }
  56. DeprecatedString class_name() const;
  57. template<typename T>
  58. bool fast_is() const = delete;
  59. virtual bool is_new_expression() const { return false; }
  60. virtual bool is_member_expression() const { return false; }
  61. virtual bool is_super_expression() const { return false; }
  62. virtual bool is_function_expression() const { return false; }
  63. virtual bool is_class_expression() const { return false; }
  64. virtual bool is_expression_statement() const { return false; }
  65. virtual bool is_identifier() const { return false; }
  66. virtual bool is_private_identifier() const { return false; }
  67. virtual bool is_scope_node() const { return false; }
  68. virtual bool is_program() const { return false; }
  69. virtual bool is_class_declaration() const { return false; }
  70. virtual bool is_function_declaration() const { return false; }
  71. virtual bool is_variable_declaration() const { return false; }
  72. virtual bool is_import_call() const { return false; }
  73. virtual bool is_array_expression() const { return false; }
  74. virtual bool is_object_expression() const { return false; }
  75. virtual bool is_string_literal() const { return false; }
  76. virtual bool is_update_expression() const { return false; }
  77. virtual bool is_call_expression() const { return false; }
  78. virtual bool is_labelled_statement() const { return false; }
  79. virtual bool is_iteration_statement() const { return false; }
  80. virtual bool is_class_method() const { return false; }
  81. protected:
  82. explicit ASTNode(SourceRange);
  83. private:
  84. // NOTE: These members are carefully ordered so that `m_start_offset` is packed with the padding after RefCounted::m_ref_count.
  85. // This creates a 4-byte padding hole after `m_end_offset` which is used to pack subclasses better.
  86. u32 m_start_offset { 0 };
  87. RefPtr<SourceCode const> m_source_code;
  88. u32 m_end_offset { 0 };
  89. };
  90. // This is a helper class that packs an array of T after the AST node, all in the same allocation.
  91. template<typename Derived, typename Base, typename T>
  92. class ASTNodeWithTailArray : public Base {
  93. public:
  94. virtual ~ASTNodeWithTailArray() override
  95. {
  96. for (auto& value : tail_span())
  97. value.~T();
  98. }
  99. ReadonlySpan<T> tail_span() const { return { tail_data(), tail_size() }; }
  100. T const* tail_data() const { return reinterpret_cast<T const*>(reinterpret_cast<uintptr_t>(this) + sizeof(Derived)); }
  101. size_t tail_size() const { return m_tail_size; }
  102. protected:
  103. template<typename ActualDerived = Derived, typename... Args>
  104. static NonnullRefPtr<ActualDerived> create(size_t tail_size, SourceRange source_range, Args&&... args)
  105. {
  106. static_assert(sizeof(ActualDerived) == sizeof(Derived), "This leaf class cannot add more members");
  107. static_assert(alignof(ActualDerived) % alignof(T) == 0, "Need padding for tail array");
  108. auto memory = ::operator new(sizeof(ActualDerived) + tail_size * sizeof(T));
  109. return adopt_ref(*::new (memory) ActualDerived(move(source_range), forward<Args>(args)...));
  110. }
  111. ASTNodeWithTailArray(SourceRange source_range, ReadonlySpan<T> values)
  112. : Base(move(source_range))
  113. , m_tail_size(values.size())
  114. {
  115. VERIFY(values.size() <= NumericLimits<u32>::max());
  116. for (size_t i = 0; i < values.size(); ++i)
  117. new (&tail_data()[i]) T(values[i]);
  118. }
  119. private:
  120. T* tail_data() { return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(this) + sizeof(Derived)); }
  121. u32 m_tail_size { 0 };
  122. };
  123. class Statement : public ASTNode {
  124. public:
  125. explicit Statement(SourceRange source_range)
  126. : ASTNode(source_range)
  127. {
  128. }
  129. };
  130. // 14.13 Labelled Statements, https://tc39.es/ecma262/#sec-labelled-statements
  131. class LabelledStatement : public Statement {
  132. public:
  133. LabelledStatement(SourceRange source_range, DeprecatedFlyString label, NonnullRefPtr<Statement const> labelled_item)
  134. : Statement(source_range)
  135. , m_label(move(label))
  136. , m_labelled_item(move(labelled_item))
  137. {
  138. }
  139. virtual Completion execute(Interpreter&) const override;
  140. virtual void dump(int indent) const override;
  141. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  142. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const;
  143. DeprecatedFlyString const& label() const { return m_label; }
  144. DeprecatedFlyString& label() { return m_label; }
  145. NonnullRefPtr<Statement const> const& labelled_item() const { return m_labelled_item; }
  146. private:
  147. virtual bool is_labelled_statement() const final { return true; }
  148. DeprecatedFlyString m_label;
  149. NonnullRefPtr<Statement const> m_labelled_item;
  150. };
  151. class LabelableStatement : public Statement {
  152. public:
  153. using Statement::Statement;
  154. Vector<DeprecatedFlyString> const& labels() const { return m_labels; }
  155. virtual void add_label(DeprecatedFlyString string) { m_labels.append(move(string)); }
  156. protected:
  157. Vector<DeprecatedFlyString> m_labels;
  158. };
  159. class IterationStatement : public Statement {
  160. public:
  161. using Statement::Statement;
  162. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const = 0;
  163. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const;
  164. private:
  165. virtual bool is_iteration_statement() const final { return true; }
  166. };
  167. class EmptyStatement final : public Statement {
  168. public:
  169. explicit EmptyStatement(SourceRange source_range)
  170. : Statement(source_range)
  171. {
  172. }
  173. Completion execute(Interpreter&) const override;
  174. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  175. };
  176. class ErrorStatement final : public Statement {
  177. public:
  178. explicit ErrorStatement(SourceRange source_range)
  179. : Statement(source_range)
  180. {
  181. }
  182. Completion execute(Interpreter&) const override { return {}; }
  183. };
  184. class ExpressionStatement final : public Statement {
  185. public:
  186. ExpressionStatement(SourceRange source_range, NonnullRefPtr<Expression const> expression)
  187. : Statement(source_range)
  188. , m_expression(move(expression))
  189. {
  190. }
  191. virtual Completion execute(Interpreter&) const override;
  192. virtual void dump(int indent) const override;
  193. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  194. Expression const& expression() const { return m_expression; };
  195. private:
  196. virtual bool is_expression_statement() const override { return true; }
  197. NonnullRefPtr<Expression const> m_expression;
  198. };
  199. template<typename Func, typename... Args>
  200. concept ThrowCompletionOrVoidFunction = requires(Func func, Args... args) {
  201. {
  202. func(args...)
  203. }
  204. -> SameAs<ThrowCompletionOr<void>>;
  205. };
  206. template<typename... Args>
  207. class ThrowCompletionOrVoidCallback : public Function<ThrowCompletionOr<void>(Args...)> {
  208. public:
  209. template<typename CallableType>
  210. ThrowCompletionOrVoidCallback(CallableType&& callable)
  211. requires(VoidFunction<CallableType, Args...>)
  212. : Function<ThrowCompletionOr<void>(Args...)>([callable = forward<CallableType>(callable)](Args... args) {
  213. callable(args...);
  214. return ThrowCompletionOr<void> {};
  215. })
  216. {
  217. }
  218. template<typename CallableType>
  219. ThrowCompletionOrVoidCallback(CallableType&& callable)
  220. requires(ThrowCompletionOrVoidFunction<CallableType, Args...>)
  221. : Function<ThrowCompletionOr<void>(Args...)>(forward<CallableType>(callable))
  222. {
  223. }
  224. };
  225. class ScopeNode : public Statement {
  226. public:
  227. template<typename T, typename... Args>
  228. T& append(SourceRange range, Args&&... args)
  229. {
  230. auto child = create_ast_node<T>(range, forward<Args>(args)...);
  231. m_children.append(move(child));
  232. return static_cast<T&>(*m_children.last());
  233. }
  234. void append(NonnullRefPtr<Statement const> child)
  235. {
  236. m_children.append(move(child));
  237. }
  238. void shrink_to_fit()
  239. {
  240. m_children.shrink_to_fit();
  241. m_lexical_declarations.shrink_to_fit();
  242. m_var_declarations.shrink_to_fit();
  243. m_functions_hoistable_with_annexB_extension.shrink_to_fit();
  244. }
  245. Vector<NonnullRefPtr<Statement const>> const& children() const { return m_children; }
  246. virtual void dump(int indent) const override;
  247. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  248. Completion evaluate_statements(Interpreter&) const;
  249. void add_var_scoped_declaration(NonnullRefPtr<Declaration const> variables);
  250. void add_lexical_declaration(NonnullRefPtr<Declaration const> variables);
  251. void add_hoisted_function(NonnullRefPtr<FunctionDeclaration const> declaration);
  252. [[nodiscard]] bool has_lexical_declarations() const { return !m_lexical_declarations.is_empty(); }
  253. [[nodiscard]] bool has_var_declarations() const { return !m_var_declarations.is_empty(); }
  254. [[nodiscard]] size_t var_declaration_count() const { return m_var_declarations.size(); }
  255. [[nodiscard]] size_t lexical_declaration_count() const { return m_lexical_declarations.size(); }
  256. ThrowCompletionOr<void> for_each_lexically_scoped_declaration(ThrowCompletionOrVoidCallback<Declaration const&>&& callback) const;
  257. ThrowCompletionOr<void> for_each_lexically_declared_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
  258. ThrowCompletionOr<void> for_each_var_declared_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
  259. ThrowCompletionOr<void> for_each_var_declared_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const;
  260. ThrowCompletionOr<void> for_each_var_function_declaration_in_reverse_order(ThrowCompletionOrVoidCallback<FunctionDeclaration const&>&& callback) const;
  261. ThrowCompletionOr<void> for_each_lexical_function_declaration_in_reverse_order(ThrowCompletionOrVoidCallback<FunctionDeclaration const&>&& callback) const;
  262. ThrowCompletionOr<void> for_each_var_scoped_variable_declaration(ThrowCompletionOrVoidCallback<VariableDeclaration const&>&& callback) const;
  263. void block_declaration_instantiation(VM&, Environment*) const;
  264. ThrowCompletionOr<void> for_each_function_hoistable_with_annexB_extension(ThrowCompletionOrVoidCallback<FunctionDeclaration&>&& callback) const;
  265. Vector<DeprecatedFlyString> const& local_variables_names() const { return m_local_variables_names; }
  266. size_t add_local_variable(DeprecatedFlyString name)
  267. {
  268. auto index = m_local_variables_names.size();
  269. m_local_variables_names.append(name);
  270. return index;
  271. }
  272. protected:
  273. explicit ScopeNode(SourceRange source_range)
  274. : Statement(source_range)
  275. {
  276. }
  277. private:
  278. virtual bool is_scope_node() const final { return true; }
  279. Vector<NonnullRefPtr<Statement const>> m_children;
  280. Vector<NonnullRefPtr<Declaration const>> m_lexical_declarations;
  281. Vector<NonnullRefPtr<Declaration const>> m_var_declarations;
  282. Vector<NonnullRefPtr<FunctionDeclaration const>> m_functions_hoistable_with_annexB_extension;
  283. Vector<DeprecatedFlyString> m_local_variables_names;
  284. };
  285. // ImportEntry Record, https://tc39.es/ecma262/#table-importentry-record-fields
  286. struct ImportEntry {
  287. DeprecatedFlyString import_name; // [[ImportName]] if a String
  288. DeprecatedFlyString local_name; // [[LocalName]]
  289. bool is_namespace { false }; // [[ImportName]] if `namespace-object`
  290. ImportEntry(DeprecatedFlyString import_name_, DeprecatedFlyString local_name_, bool is_namespace_ = false)
  291. : import_name(move(import_name_))
  292. , local_name(move(local_name_))
  293. , is_namespace(is_namespace_)
  294. {
  295. VERIFY(!is_namespace || import_name.is_null());
  296. }
  297. ModuleRequest const& module_request() const
  298. {
  299. VERIFY(m_module_request);
  300. return *m_module_request;
  301. }
  302. private:
  303. friend class ImportStatement;
  304. ModuleRequest* m_module_request; // [[ModuleRequest]]
  305. };
  306. class ImportStatement final : public Statement {
  307. public:
  308. explicit ImportStatement(SourceRange source_range, ModuleRequest from_module, Vector<ImportEntry> entries = {})
  309. : Statement(source_range)
  310. , m_module_request(move(from_module))
  311. , m_entries(move(entries))
  312. {
  313. for (auto& entry : m_entries)
  314. entry.m_module_request = &m_module_request;
  315. }
  316. virtual Completion execute(Interpreter&) const override;
  317. virtual void dump(int indent) const override;
  318. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  319. bool has_bound_name(DeprecatedFlyString const& name) const;
  320. Vector<ImportEntry> const& entries() const { return m_entries; }
  321. ModuleRequest const& module_request() const { return m_module_request; }
  322. private:
  323. ModuleRequest m_module_request;
  324. Vector<ImportEntry> m_entries;
  325. };
  326. // ExportEntry Record, https://tc39.es/ecma262/#table-exportentry-records
  327. struct ExportEntry {
  328. enum class Kind {
  329. NamedExport,
  330. ModuleRequestAll,
  331. ModuleRequestAllButDefault,
  332. // EmptyNamedExport is a special type for export {} from "module",
  333. // which should import the module without getting any of the exports
  334. // however we don't want give it a fake export name which may get
  335. // duplicates
  336. EmptyNamedExport,
  337. } kind;
  338. DeprecatedFlyString export_name; // [[ExportName]]
  339. DeprecatedFlyString local_or_import_name; // Either [[ImportName]] or [[LocalName]]
  340. ExportEntry(Kind export_kind, DeprecatedFlyString export_name_, DeprecatedFlyString local_or_import_name_)
  341. : kind(export_kind)
  342. , export_name(move(export_name_))
  343. , local_or_import_name(move(local_or_import_name_))
  344. {
  345. }
  346. bool is_module_request() const
  347. {
  348. return m_module_request != nullptr;
  349. }
  350. static ExportEntry indirect_export_entry(ModuleRequest const& module_request, DeprecatedFlyString export_name, DeprecatedFlyString import_name)
  351. {
  352. ExportEntry entry { Kind::NamedExport, move(export_name), move(import_name) };
  353. entry.m_module_request = &module_request;
  354. return entry;
  355. }
  356. ModuleRequest const& module_request() const
  357. {
  358. VERIFY(m_module_request);
  359. return *m_module_request;
  360. }
  361. private:
  362. ModuleRequest const* m_module_request { nullptr }; // [[ModuleRequest]]
  363. friend class ExportStatement;
  364. public:
  365. static ExportEntry named_export(DeprecatedFlyString export_name, DeprecatedFlyString local_name)
  366. {
  367. return ExportEntry { Kind::NamedExport, move(export_name), move(local_name) };
  368. }
  369. static ExportEntry all_but_default_entry()
  370. {
  371. return ExportEntry { Kind::ModuleRequestAllButDefault, {}, {} };
  372. }
  373. static ExportEntry all_module_request(DeprecatedFlyString export_name)
  374. {
  375. return ExportEntry { Kind::ModuleRequestAll, move(export_name), {} };
  376. }
  377. static ExportEntry empty_named_export()
  378. {
  379. return ExportEntry { Kind::EmptyNamedExport, {}, {} };
  380. }
  381. };
  382. class ExportStatement final : public Statement {
  383. public:
  384. static DeprecatedFlyString local_name_for_default;
  385. ExportStatement(SourceRange source_range, RefPtr<ASTNode const> statement, Vector<ExportEntry> entries, bool is_default_export, ModuleRequest module_request)
  386. : Statement(source_range)
  387. , m_statement(move(statement))
  388. , m_entries(move(entries))
  389. , m_is_default_export(is_default_export)
  390. , m_module_request(move(module_request))
  391. {
  392. if (!m_module_request.module_specifier.is_null()) {
  393. for (auto& entry : m_entries)
  394. entry.m_module_request = &m_module_request;
  395. }
  396. }
  397. virtual Completion execute(Interpreter&) const override;
  398. virtual void dump(int indent) const override;
  399. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  400. bool has_export(DeprecatedFlyString const& export_name) const;
  401. bool has_statement() const { return m_statement; }
  402. Vector<ExportEntry> const& entries() const { return m_entries; }
  403. bool is_default_export() const { return m_is_default_export; }
  404. ASTNode const& statement() const
  405. {
  406. VERIFY(m_statement);
  407. return *m_statement;
  408. }
  409. ModuleRequest const& module_request() const
  410. {
  411. VERIFY(!m_module_request.module_specifier.is_null());
  412. return m_module_request;
  413. }
  414. private:
  415. RefPtr<ASTNode const> m_statement;
  416. Vector<ExportEntry> m_entries;
  417. bool m_is_default_export { false };
  418. ModuleRequest m_module_request;
  419. };
  420. class Program final : public ScopeNode {
  421. public:
  422. enum class Type {
  423. Script,
  424. Module
  425. };
  426. explicit Program(SourceRange source_range, Type program_type)
  427. : ScopeNode(source_range)
  428. , m_type(program_type)
  429. {
  430. }
  431. virtual Completion execute(Interpreter&) const override;
  432. bool is_strict_mode() const { return m_is_strict_mode; }
  433. void set_strict_mode() { m_is_strict_mode = true; }
  434. Type type() const { return m_type; }
  435. void append_import(NonnullRefPtr<ImportStatement const> import_statement)
  436. {
  437. m_imports.append(import_statement);
  438. append(import_statement);
  439. }
  440. void append_export(NonnullRefPtr<ExportStatement const> export_statement)
  441. {
  442. m_exports.append(export_statement);
  443. append(export_statement);
  444. }
  445. Vector<NonnullRefPtr<ImportStatement const>> const& imports() const { return m_imports; }
  446. Vector<NonnullRefPtr<ExportStatement const>> const& exports() const { return m_exports; }
  447. Vector<NonnullRefPtr<ImportStatement const>>& imports() { return m_imports; }
  448. Vector<NonnullRefPtr<ExportStatement const>>& exports() { return m_exports; }
  449. bool has_top_level_await() const { return m_has_top_level_await; }
  450. void set_has_top_level_await() { m_has_top_level_await = true; }
  451. ThrowCompletionOr<void> global_declaration_instantiation(VM&, GlobalEnvironment&) const;
  452. private:
  453. virtual bool is_program() const override { return true; }
  454. bool m_is_strict_mode { false };
  455. Type m_type { Type::Script };
  456. Vector<NonnullRefPtr<ImportStatement const>> m_imports;
  457. Vector<NonnullRefPtr<ExportStatement const>> m_exports;
  458. bool m_has_top_level_await { false };
  459. };
  460. class BlockStatement final : public ScopeNode {
  461. public:
  462. explicit BlockStatement(SourceRange source_range)
  463. : ScopeNode(source_range)
  464. {
  465. }
  466. Completion execute(Interpreter&) const override;
  467. };
  468. class FunctionBody final : public ScopeNode {
  469. public:
  470. explicit FunctionBody(SourceRange source_range)
  471. : ScopeNode(source_range)
  472. {
  473. }
  474. void set_strict_mode() { m_in_strict_mode = true; }
  475. bool in_strict_mode() const { return m_in_strict_mode; }
  476. virtual Completion execute(Interpreter&) const override;
  477. private:
  478. bool m_in_strict_mode { false };
  479. };
  480. class Expression : public ASTNode {
  481. public:
  482. explicit Expression(SourceRange source_range)
  483. : ASTNode(source_range)
  484. {
  485. }
  486. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const;
  487. };
  488. class Declaration : public Statement {
  489. public:
  490. explicit Declaration(SourceRange source_range)
  491. : Statement(source_range)
  492. {
  493. }
  494. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const = 0;
  495. virtual ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const = 0;
  496. // 8.1.3 Static Semantics: IsConstantDeclaration, https://tc39.es/ecma262/#sec-static-semantics-isconstantdeclaration
  497. virtual bool is_constant_declaration() const { return false; }
  498. virtual bool is_lexical_declaration() const { return false; }
  499. };
  500. class ErrorDeclaration final : public Declaration {
  501. public:
  502. explicit ErrorDeclaration(SourceRange source_range)
  503. : Declaration(source_range)
  504. {
  505. }
  506. Completion execute(Interpreter&) const override { return {}; }
  507. ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&&) const override
  508. {
  509. VERIFY_NOT_REACHED();
  510. }
  511. ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override
  512. {
  513. VERIFY_NOT_REACHED();
  514. }
  515. };
  516. struct BindingPattern : RefCounted<BindingPattern> {
  517. // This covers both BindingProperty and BindingElement, hence the more generic name
  518. struct BindingEntry {
  519. // If this entry represents a BindingElement, then name will be Empty
  520. Variant<NonnullRefPtr<Identifier const>, NonnullRefPtr<Expression const>, Empty> name {};
  521. Variant<NonnullRefPtr<Identifier const>, NonnullRefPtr<BindingPattern const>, NonnullRefPtr<MemberExpression const>, Empty> alias {};
  522. RefPtr<Expression const> initializer {};
  523. bool is_rest { false };
  524. bool is_elision() const { return name.has<Empty>() && alias.has<Empty>(); }
  525. };
  526. enum class Kind {
  527. Array,
  528. Object,
  529. };
  530. void dump(int indent) const;
  531. ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const;
  532. ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&& callback) const;
  533. bool contains_expression() const;
  534. Vector<BindingEntry> entries;
  535. Kind kind { Kind::Object };
  536. };
  537. struct FunctionParameter {
  538. Variant<DeprecatedFlyString, NonnullRefPtr<BindingPattern const>> binding;
  539. RefPtr<Expression const> default_value;
  540. bool is_rest { false };
  541. };
  542. class Identifier final : public Expression {
  543. public:
  544. explicit Identifier(SourceRange source_range, DeprecatedFlyString string)
  545. : Expression(source_range)
  546. , m_string(move(string))
  547. {
  548. }
  549. DeprecatedFlyString const& string() const { return m_string; }
  550. bool is_local() const { return m_local_variable_index.has_value(); }
  551. size_t local_variable_index() const
  552. {
  553. VERIFY(m_local_variable_index.has_value());
  554. return m_local_variable_index.value();
  555. }
  556. void set_local_variable_index(size_t index) { m_local_variable_index = index; }
  557. virtual Completion execute(Interpreter&) const override;
  558. virtual void dump(int indent) const override;
  559. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const override;
  560. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  561. private:
  562. virtual bool is_identifier() const override { return true; }
  563. DeprecatedFlyString m_string;
  564. mutable EnvironmentCoordinate m_cached_environment_coordinate;
  565. Optional<size_t> m_local_variable_index;
  566. };
  567. class FunctionNode {
  568. public:
  569. StringView name() const { return m_name ? m_name->string().view() : ""sv; }
  570. DeprecatedString const& source_text() const { return m_source_text; }
  571. Statement const& body() const { return *m_body; }
  572. Vector<FunctionParameter> const& parameters() const { return m_parameters; };
  573. i32 function_length() const { return m_function_length; }
  574. Vector<DeprecatedFlyString> const& local_variables_names() const { return m_local_variables_names; }
  575. bool is_strict_mode() const { return m_is_strict_mode; }
  576. bool might_need_arguments_object() const { return m_might_need_arguments_object; }
  577. bool contains_direct_call_to_eval() const { return m_contains_direct_call_to_eval; }
  578. bool is_arrow_function() const { return m_is_arrow_function; }
  579. FunctionKind kind() const { return m_kind; }
  580. protected:
  581. FunctionNode(RefPtr<Identifier const> name, DeprecatedString source_text, NonnullRefPtr<Statement const> body, Vector<FunctionParameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Vector<DeprecatedFlyString> local_variables_names)
  582. : m_name(move(name))
  583. , m_source_text(move(source_text))
  584. , m_body(move(body))
  585. , m_parameters(move(parameters))
  586. , m_function_length(function_length)
  587. , m_kind(kind)
  588. , m_is_strict_mode(is_strict_mode)
  589. , m_might_need_arguments_object(might_need_arguments_object)
  590. , m_contains_direct_call_to_eval(contains_direct_call_to_eval)
  591. , m_is_arrow_function(is_arrow_function)
  592. , m_local_variables_names(local_variables_names)
  593. {
  594. if (m_is_arrow_function)
  595. VERIFY(!m_might_need_arguments_object);
  596. }
  597. void dump(int indent, DeprecatedString const& class_name) const;
  598. RefPtr<Identifier const> m_name { nullptr };
  599. private:
  600. DeprecatedString m_source_text;
  601. NonnullRefPtr<Statement const> m_body;
  602. Vector<FunctionParameter> const m_parameters;
  603. const i32 m_function_length;
  604. FunctionKind m_kind;
  605. bool m_is_strict_mode : 1 { false };
  606. bool m_might_need_arguments_object : 1 { false };
  607. bool m_contains_direct_call_to_eval : 1 { false };
  608. bool m_is_arrow_function : 1 { false };
  609. Vector<DeprecatedFlyString> m_local_variables_names;
  610. };
  611. class FunctionDeclaration final
  612. : public Declaration
  613. , public FunctionNode {
  614. public:
  615. static bool must_have_name() { return true; }
  616. FunctionDeclaration(SourceRange source_range, RefPtr<Identifier const> name, DeprecatedString source_text, NonnullRefPtr<Statement const> body, Vector<FunctionParameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, Vector<DeprecatedFlyString> local_variables_names)
  617. : Declaration(source_range)
  618. , FunctionNode(name, move(source_text), move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, contains_direct_call_to_eval, false, move(local_variables_names))
  619. {
  620. }
  621. virtual Completion execute(Interpreter&) const override;
  622. virtual void dump(int indent) const override;
  623. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  624. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
  625. ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override;
  626. virtual bool is_function_declaration() const override { return true; }
  627. void set_should_do_additional_annexB_steps() { m_is_hoisted = true; }
  628. private:
  629. bool m_is_hoisted { false };
  630. };
  631. class FunctionExpression final
  632. : public Expression
  633. , public FunctionNode {
  634. public:
  635. static bool must_have_name() { return false; }
  636. FunctionExpression(SourceRange source_range, RefPtr<Identifier const> name, DeprecatedString source_text, NonnullRefPtr<Statement const> body, Vector<FunctionParameter> parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, Vector<DeprecatedFlyString> local_variables_names, bool is_arrow_function = false)
  637. : Expression(source_range)
  638. , FunctionNode(name, move(source_text), move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function, move(local_variables_names))
  639. {
  640. }
  641. virtual Completion execute(Interpreter&) const override;
  642. virtual void dump(int indent) const override;
  643. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  644. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode_with_lhs_name(Bytecode::Generator&, Optional<Bytecode::IdentifierTableIndex> lhs_name) const;
  645. bool has_name() const { return !name().is_empty(); }
  646. Value instantiate_ordinary_function_expression(VM&, DeprecatedFlyString given_name) const;
  647. private:
  648. virtual bool is_function_expression() const override { return true; }
  649. };
  650. class ErrorExpression final : public Expression {
  651. public:
  652. explicit ErrorExpression(SourceRange source_range)
  653. : Expression(source_range)
  654. {
  655. }
  656. Completion execute(Interpreter&) const override { return {}; }
  657. };
  658. class YieldExpression final : public Expression {
  659. public:
  660. explicit YieldExpression(SourceRange source_range, RefPtr<Expression const> argument, bool is_yield_from)
  661. : Expression(source_range)
  662. , m_argument(move(argument))
  663. , m_is_yield_from(is_yield_from)
  664. {
  665. }
  666. Expression const* argument() const { return m_argument; }
  667. bool is_yield_from() const { return m_is_yield_from; }
  668. virtual Completion execute(Interpreter&) const override;
  669. virtual void dump(int indent) const override;
  670. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  671. private:
  672. RefPtr<Expression const> m_argument;
  673. bool m_is_yield_from { false };
  674. };
  675. class AwaitExpression final : public Expression {
  676. public:
  677. explicit AwaitExpression(SourceRange source_range, NonnullRefPtr<Expression const> argument)
  678. : Expression(source_range)
  679. , m_argument(move(argument))
  680. {
  681. }
  682. virtual Completion execute(Interpreter&) const override;
  683. virtual void dump(int indent) const override;
  684. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  685. private:
  686. NonnullRefPtr<Expression const> m_argument;
  687. };
  688. class ReturnStatement final : public Statement {
  689. public:
  690. explicit ReturnStatement(SourceRange source_range, RefPtr<Expression const> argument)
  691. : Statement(source_range)
  692. , m_argument(move(argument))
  693. {
  694. }
  695. Expression const* argument() const { return m_argument; }
  696. virtual Completion execute(Interpreter&) const override;
  697. virtual void dump(int indent) const override;
  698. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  699. private:
  700. RefPtr<Expression const> m_argument;
  701. };
  702. class IfStatement final : public Statement {
  703. public:
  704. IfStatement(SourceRange source_range, NonnullRefPtr<Expression const> predicate, NonnullRefPtr<Statement const> consequent, RefPtr<Statement const> alternate)
  705. : Statement(source_range)
  706. , m_predicate(move(predicate))
  707. , m_consequent(move(consequent))
  708. , m_alternate(move(alternate))
  709. {
  710. }
  711. Expression const& predicate() const { return *m_predicate; }
  712. Statement const& consequent() const { return *m_consequent; }
  713. Statement const* alternate() const { return m_alternate; }
  714. virtual Completion execute(Interpreter&) const override;
  715. virtual void dump(int indent) const override;
  716. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  717. private:
  718. NonnullRefPtr<Expression const> m_predicate;
  719. NonnullRefPtr<Statement const> m_consequent;
  720. RefPtr<Statement const> m_alternate;
  721. };
  722. class WhileStatement final : public IterationStatement {
  723. public:
  724. WhileStatement(SourceRange source_range, NonnullRefPtr<Expression const> test, NonnullRefPtr<Statement const> body)
  725. : IterationStatement(source_range)
  726. , m_test(move(test))
  727. , m_body(move(body))
  728. {
  729. }
  730. Expression const& test() const { return *m_test; }
  731. Statement const& body() const { return *m_body; }
  732. virtual Completion execute(Interpreter&) const override;
  733. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  734. virtual void dump(int indent) const override;
  735. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  736. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  737. private:
  738. NonnullRefPtr<Expression const> m_test;
  739. NonnullRefPtr<Statement const> m_body;
  740. };
  741. class DoWhileStatement final : public IterationStatement {
  742. public:
  743. DoWhileStatement(SourceRange source_range, NonnullRefPtr<Expression const> test, NonnullRefPtr<Statement const> body)
  744. : IterationStatement(source_range)
  745. , m_test(move(test))
  746. , m_body(move(body))
  747. {
  748. }
  749. Expression const& test() const { return *m_test; }
  750. Statement const& body() const { return *m_body; }
  751. virtual Completion execute(Interpreter&) const override;
  752. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  753. virtual void dump(int indent) const override;
  754. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  755. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  756. private:
  757. NonnullRefPtr<Expression const> m_test;
  758. NonnullRefPtr<Statement const> m_body;
  759. };
  760. class WithStatement final : public Statement {
  761. public:
  762. WithStatement(SourceRange source_range, NonnullRefPtr<Expression const> object, NonnullRefPtr<Statement const> body)
  763. : Statement(source_range)
  764. , m_object(move(object))
  765. , m_body(move(body))
  766. {
  767. }
  768. Expression const& object() const { return *m_object; }
  769. Statement const& body() const { return *m_body; }
  770. virtual Completion execute(Interpreter&) const override;
  771. virtual void dump(int indent) const override;
  772. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  773. private:
  774. NonnullRefPtr<Expression const> m_object;
  775. NonnullRefPtr<Statement const> m_body;
  776. };
  777. class ForStatement final : public IterationStatement {
  778. public:
  779. ForStatement(SourceRange source_range, RefPtr<ASTNode const> init, RefPtr<Expression const> test, RefPtr<Expression const> update, NonnullRefPtr<Statement const> body)
  780. : IterationStatement(source_range)
  781. , m_init(move(init))
  782. , m_test(move(test))
  783. , m_update(move(update))
  784. , m_body(move(body))
  785. {
  786. }
  787. ASTNode const* init() const { return m_init; }
  788. Expression const* test() const { return m_test; }
  789. Expression const* update() const { return m_update; }
  790. Statement const& body() const { return *m_body; }
  791. virtual Completion execute(Interpreter&) const override;
  792. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  793. virtual void dump(int indent) const override;
  794. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  795. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  796. private:
  797. Completion for_body_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&, size_t per_iteration_bindings_size) const;
  798. RefPtr<ASTNode const> m_init;
  799. RefPtr<Expression const> m_test;
  800. RefPtr<Expression const> m_update;
  801. NonnullRefPtr<Statement const> m_body;
  802. };
  803. class ForInStatement final : public IterationStatement {
  804. public:
  805. ForInStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> lhs, NonnullRefPtr<Expression const> rhs, NonnullRefPtr<Statement const> body)
  806. : IterationStatement(source_range)
  807. , m_lhs(move(lhs))
  808. , m_rhs(move(rhs))
  809. , m_body(move(body))
  810. {
  811. }
  812. auto const& lhs() const { return m_lhs; }
  813. Expression const& rhs() const { return *m_rhs; }
  814. Statement const& body() const { return *m_body; }
  815. virtual Completion execute(Interpreter&) const override;
  816. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  817. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  818. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  819. virtual void dump(int indent) const override;
  820. private:
  821. Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> m_lhs;
  822. NonnullRefPtr<Expression const> m_rhs;
  823. NonnullRefPtr<Statement const> m_body;
  824. };
  825. class ForOfStatement final : public IterationStatement {
  826. public:
  827. ForOfStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> lhs, NonnullRefPtr<Expression const> rhs, NonnullRefPtr<Statement const> body)
  828. : IterationStatement(source_range)
  829. , m_lhs(move(lhs))
  830. , m_rhs(move(rhs))
  831. , m_body(move(body))
  832. {
  833. }
  834. auto const& lhs() const { return m_lhs; }
  835. Expression const& rhs() const { return *m_rhs; }
  836. Statement const& body() const { return *m_body; }
  837. virtual Completion execute(Interpreter&) const override;
  838. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  839. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  840. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  841. virtual void dump(int indent) const override;
  842. private:
  843. Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> m_lhs;
  844. NonnullRefPtr<Expression const> m_rhs;
  845. NonnullRefPtr<Statement const> m_body;
  846. };
  847. class ForAwaitOfStatement final : public IterationStatement {
  848. public:
  849. ForAwaitOfStatement(SourceRange source_range, Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> lhs, NonnullRefPtr<Expression const> rhs, NonnullRefPtr<Statement const> body)
  850. : IterationStatement(source_range)
  851. , m_lhs(move(lhs))
  852. , m_rhs(move(rhs))
  853. , m_body(move(body))
  854. {
  855. }
  856. virtual Completion execute(Interpreter&) const override;
  857. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  858. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const override;
  859. virtual Completion loop_evaluation(Interpreter&, Vector<DeprecatedFlyString> const&) const override;
  860. virtual void dump(int indent) const override;
  861. private:
  862. Variant<NonnullRefPtr<ASTNode const>, NonnullRefPtr<BindingPattern const>> m_lhs;
  863. NonnullRefPtr<Expression const> m_rhs;
  864. NonnullRefPtr<Statement const> m_body;
  865. };
  866. enum class BinaryOp {
  867. Addition,
  868. Subtraction,
  869. Multiplication,
  870. Division,
  871. Modulo,
  872. Exponentiation,
  873. StrictlyEquals,
  874. StrictlyInequals,
  875. LooselyEquals,
  876. LooselyInequals,
  877. GreaterThan,
  878. GreaterThanEquals,
  879. LessThan,
  880. LessThanEquals,
  881. BitwiseAnd,
  882. BitwiseOr,
  883. BitwiseXor,
  884. LeftShift,
  885. RightShift,
  886. UnsignedRightShift,
  887. In,
  888. InstanceOf,
  889. };
  890. class BinaryExpression final : public Expression {
  891. public:
  892. BinaryExpression(SourceRange source_range, BinaryOp op, NonnullRefPtr<Expression const> lhs, NonnullRefPtr<Expression const> rhs)
  893. : Expression(source_range)
  894. , m_op(op)
  895. , m_lhs(move(lhs))
  896. , m_rhs(move(rhs))
  897. {
  898. }
  899. virtual Completion execute(Interpreter&) const override;
  900. virtual void dump(int indent) const override;
  901. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  902. private:
  903. BinaryOp m_op;
  904. NonnullRefPtr<Expression const> m_lhs;
  905. NonnullRefPtr<Expression const> m_rhs;
  906. };
  907. enum class LogicalOp {
  908. And,
  909. Or,
  910. NullishCoalescing,
  911. };
  912. class LogicalExpression final : public Expression {
  913. public:
  914. LogicalExpression(SourceRange source_range, LogicalOp op, NonnullRefPtr<Expression const> lhs, NonnullRefPtr<Expression const> rhs)
  915. : Expression(source_range)
  916. , m_op(op)
  917. , m_lhs(move(lhs))
  918. , m_rhs(move(rhs))
  919. {
  920. }
  921. virtual Completion execute(Interpreter&) const override;
  922. virtual void dump(int indent) const override;
  923. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  924. private:
  925. LogicalOp m_op;
  926. NonnullRefPtr<Expression const> m_lhs;
  927. NonnullRefPtr<Expression const> m_rhs;
  928. };
  929. enum class UnaryOp {
  930. BitwiseNot,
  931. Not,
  932. Plus,
  933. Minus,
  934. Typeof,
  935. Void,
  936. Delete,
  937. };
  938. class UnaryExpression final : public Expression {
  939. public:
  940. UnaryExpression(SourceRange source_range, UnaryOp op, NonnullRefPtr<Expression const> lhs)
  941. : Expression(source_range)
  942. , m_op(op)
  943. , m_lhs(move(lhs))
  944. {
  945. }
  946. virtual Completion execute(Interpreter&) const override;
  947. virtual void dump(int indent) const override;
  948. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  949. private:
  950. UnaryOp m_op;
  951. NonnullRefPtr<Expression const> m_lhs;
  952. };
  953. class SequenceExpression final : public Expression {
  954. public:
  955. SequenceExpression(SourceRange source_range, Vector<NonnullRefPtr<Expression const>> expressions)
  956. : Expression(source_range)
  957. , m_expressions(move(expressions))
  958. {
  959. VERIFY(m_expressions.size() >= 2);
  960. }
  961. virtual void dump(int indent) const override;
  962. virtual Completion execute(Interpreter&) const override;
  963. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  964. private:
  965. Vector<NonnullRefPtr<Expression const>> m_expressions;
  966. };
  967. class Literal : public Expression {
  968. protected:
  969. explicit Literal(SourceRange source_range)
  970. : Expression(source_range)
  971. {
  972. }
  973. };
  974. class BooleanLiteral final : public Literal {
  975. public:
  976. explicit BooleanLiteral(SourceRange source_range, bool value)
  977. : Literal(source_range)
  978. , m_value(value)
  979. {
  980. }
  981. virtual Completion execute(Interpreter&) const override;
  982. virtual void dump(int indent) const override;
  983. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  984. private:
  985. bool m_value { false };
  986. };
  987. class NumericLiteral final : public Literal {
  988. public:
  989. explicit NumericLiteral(SourceRange source_range, double value)
  990. : Literal(source_range)
  991. , m_value(value)
  992. {
  993. }
  994. virtual Completion execute(Interpreter&) const override;
  995. virtual void dump(int indent) const override;
  996. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  997. private:
  998. Value m_value;
  999. };
  1000. class BigIntLiteral final : public Literal {
  1001. public:
  1002. explicit BigIntLiteral(SourceRange source_range, DeprecatedString value)
  1003. : Literal(source_range)
  1004. , m_value(move(value))
  1005. {
  1006. }
  1007. virtual Completion execute(Interpreter&) const override;
  1008. virtual void dump(int indent) const override;
  1009. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1010. private:
  1011. DeprecatedString m_value;
  1012. };
  1013. class StringLiteral final : public Literal {
  1014. public:
  1015. explicit StringLiteral(SourceRange source_range, DeprecatedString value)
  1016. : Literal(source_range)
  1017. , m_value(move(value))
  1018. {
  1019. }
  1020. virtual Completion execute(Interpreter&) const override;
  1021. virtual void dump(int indent) const override;
  1022. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1023. StringView value() const { return m_value; }
  1024. private:
  1025. virtual bool is_string_literal() const override { return true; }
  1026. DeprecatedString m_value;
  1027. };
  1028. class NullLiteral final : public Literal {
  1029. public:
  1030. explicit NullLiteral(SourceRange source_range)
  1031. : Literal(source_range)
  1032. {
  1033. }
  1034. virtual Completion execute(Interpreter&) const override;
  1035. virtual void dump(int indent) const override;
  1036. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1037. };
  1038. class RegExpLiteral final : public Literal {
  1039. public:
  1040. RegExpLiteral(SourceRange source_range, regex::Parser::Result parsed_regex, DeprecatedString parsed_pattern, regex::RegexOptions<ECMAScriptFlags> parsed_flags, DeprecatedString pattern, DeprecatedString flags)
  1041. : Literal(source_range)
  1042. , m_parsed_regex(move(parsed_regex))
  1043. , m_parsed_pattern(move(parsed_pattern))
  1044. , m_parsed_flags(move(parsed_flags))
  1045. , m_pattern(move(pattern))
  1046. , m_flags(move(flags))
  1047. {
  1048. }
  1049. virtual Completion execute(Interpreter&) const override;
  1050. virtual void dump(int indent) const override;
  1051. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1052. regex::Parser::Result const& parsed_regex() const { return m_parsed_regex; }
  1053. DeprecatedString const& parsed_pattern() const { return m_parsed_pattern; }
  1054. regex::RegexOptions<ECMAScriptFlags> const& parsed_flags() const { return m_parsed_flags; }
  1055. DeprecatedString const& pattern() const { return m_pattern; }
  1056. DeprecatedString const& flags() const { return m_flags; }
  1057. private:
  1058. regex::Parser::Result m_parsed_regex;
  1059. DeprecatedString m_parsed_pattern;
  1060. regex::RegexOptions<ECMAScriptFlags> m_parsed_flags;
  1061. DeprecatedString m_pattern;
  1062. DeprecatedString m_flags;
  1063. };
  1064. class PrivateIdentifier final : public Expression {
  1065. public:
  1066. explicit PrivateIdentifier(SourceRange source_range, DeprecatedFlyString string)
  1067. : Expression(source_range)
  1068. , m_string(move(string))
  1069. {
  1070. }
  1071. DeprecatedFlyString const& string() const { return m_string; }
  1072. virtual Completion execute(Interpreter&) const override;
  1073. virtual void dump(int indent) const override;
  1074. virtual bool is_private_identifier() const override { return true; }
  1075. private:
  1076. DeprecatedFlyString m_string;
  1077. };
  1078. class ClassElement : public ASTNode {
  1079. public:
  1080. ClassElement(SourceRange source_range, bool is_static)
  1081. : ASTNode(source_range)
  1082. , m_is_static(is_static)
  1083. {
  1084. }
  1085. virtual Completion execute(Interpreter&) const override;
  1086. enum class ElementKind {
  1087. Method,
  1088. Field,
  1089. StaticInitializer,
  1090. };
  1091. virtual ElementKind class_element_kind() const = 0;
  1092. bool is_static() const { return m_is_static; }
  1093. // We use the Completion also as a ClassStaticBlockDefinition Record.
  1094. using ClassValue = Variant<ClassFieldDefinition, Completion, PrivateElement>;
  1095. virtual ThrowCompletionOr<ClassValue> class_element_evaluation(VM&, Object& home_object) const = 0;
  1096. virtual Optional<DeprecatedFlyString> private_bound_identifier() const { return {}; };
  1097. private:
  1098. bool m_is_static { false };
  1099. };
  1100. class ClassMethod final : public ClassElement {
  1101. public:
  1102. enum class Kind {
  1103. Method,
  1104. Getter,
  1105. Setter,
  1106. };
  1107. ClassMethod(SourceRange source_range, NonnullRefPtr<Expression const> key, NonnullRefPtr<FunctionExpression const> function, Kind kind, bool is_static)
  1108. : ClassElement(source_range, is_static)
  1109. , m_key(move(key))
  1110. , m_function(move(function))
  1111. , m_kind(kind)
  1112. {
  1113. }
  1114. Expression const& key() const { return *m_key; }
  1115. Kind kind() const { return m_kind; }
  1116. virtual ElementKind class_element_kind() const override { return ElementKind::Method; }
  1117. virtual void dump(int indent) const override;
  1118. virtual ThrowCompletionOr<ClassValue> class_element_evaluation(VM&, Object& home_object) const override;
  1119. virtual Optional<DeprecatedFlyString> private_bound_identifier() const override;
  1120. private:
  1121. virtual bool is_class_method() const override { return true; }
  1122. NonnullRefPtr<Expression const> m_key;
  1123. NonnullRefPtr<FunctionExpression const> m_function;
  1124. Kind m_kind;
  1125. };
  1126. class ClassField final : public ClassElement {
  1127. public:
  1128. ClassField(SourceRange source_range, NonnullRefPtr<Expression const> key, RefPtr<Expression const> init, bool contains_direct_call_to_eval, bool is_static)
  1129. : ClassElement(source_range, is_static)
  1130. , m_key(move(key))
  1131. , m_initializer(move(init))
  1132. , m_contains_direct_call_to_eval(contains_direct_call_to_eval)
  1133. {
  1134. }
  1135. Expression const& key() const { return *m_key; }
  1136. RefPtr<Expression const> const& initializer() const { return m_initializer; }
  1137. RefPtr<Expression const>& initializer() { return m_initializer; }
  1138. virtual ElementKind class_element_kind() const override { return ElementKind::Field; }
  1139. virtual void dump(int indent) const override;
  1140. virtual ThrowCompletionOr<ClassValue> class_element_evaluation(VM&, Object& home_object) const override;
  1141. virtual Optional<DeprecatedFlyString> private_bound_identifier() const override;
  1142. private:
  1143. NonnullRefPtr<Expression const> m_key;
  1144. RefPtr<Expression const> m_initializer;
  1145. bool m_contains_direct_call_to_eval { false };
  1146. };
  1147. class StaticInitializer final : public ClassElement {
  1148. public:
  1149. StaticInitializer(SourceRange source_range, NonnullRefPtr<FunctionBody> function_body, bool contains_direct_call_to_eval)
  1150. : ClassElement(source_range, true)
  1151. , m_function_body(move(function_body))
  1152. , m_contains_direct_call_to_eval(contains_direct_call_to_eval)
  1153. {
  1154. }
  1155. virtual ElementKind class_element_kind() const override { return ElementKind::StaticInitializer; }
  1156. virtual ThrowCompletionOr<ClassValue> class_element_evaluation(VM&, Object& home_object) const override;
  1157. virtual void dump(int indent) const override;
  1158. private:
  1159. NonnullRefPtr<FunctionBody> m_function_body;
  1160. bool m_contains_direct_call_to_eval { false };
  1161. };
  1162. class SuperExpression final : public Expression {
  1163. public:
  1164. explicit SuperExpression(SourceRange source_range)
  1165. : Expression(source_range)
  1166. {
  1167. }
  1168. virtual Completion execute(Interpreter&) const override;
  1169. virtual void dump(int indent) const override;
  1170. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1171. virtual bool is_super_expression() const override { return true; }
  1172. };
  1173. class ClassExpression final : public Expression {
  1174. public:
  1175. ClassExpression(SourceRange source_range, RefPtr<Identifier const> name, DeprecatedString source_text, RefPtr<FunctionExpression const> constructor, RefPtr<Expression const> super_class, Vector<NonnullRefPtr<ClassElement const>> elements)
  1176. : Expression(source_range)
  1177. , m_name(move(name))
  1178. , m_source_text(move(source_text))
  1179. , m_constructor(move(constructor))
  1180. , m_super_class(move(super_class))
  1181. , m_elements(move(elements))
  1182. {
  1183. }
  1184. StringView name() const { return m_name ? m_name->string().view() : ""sv; }
  1185. DeprecatedString const& source_text() const { return m_source_text; }
  1186. RefPtr<FunctionExpression const> constructor() const { return m_constructor; }
  1187. virtual Completion execute(Interpreter&) const override;
  1188. virtual void dump(int indent) const override;
  1189. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1190. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode_with_lhs_name(Bytecode::Generator&, Optional<Bytecode::IdentifierTableIndex> lhs_name) const;
  1191. bool has_name() const { return m_name; }
  1192. ThrowCompletionOr<ECMAScriptFunctionObject*> class_definition_evaluation(VM&, DeprecatedFlyString const& binding_name = {}, DeprecatedFlyString const& class_name = {}) const;
  1193. ThrowCompletionOr<ECMAScriptFunctionObject*> create_class_constructor(VM&, Environment* class_environment, Environment* environment, Value super_class, DeprecatedFlyString const& binding_name = {}, DeprecatedFlyString const& class_name = {}) const;
  1194. private:
  1195. virtual bool is_class_expression() const override { return true; }
  1196. friend ClassDeclaration;
  1197. RefPtr<Identifier const> m_name;
  1198. DeprecatedString m_source_text;
  1199. RefPtr<FunctionExpression const> m_constructor;
  1200. RefPtr<Expression const> m_super_class;
  1201. Vector<NonnullRefPtr<ClassElement const>> m_elements;
  1202. };
  1203. class ClassDeclaration final : public Declaration {
  1204. public:
  1205. ClassDeclaration(SourceRange source_range, NonnullRefPtr<ClassExpression const> class_expression)
  1206. : Declaration(source_range)
  1207. , m_class_expression(move(class_expression))
  1208. {
  1209. }
  1210. virtual Completion execute(Interpreter&) const override;
  1211. virtual void dump(int indent) const override;
  1212. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1213. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
  1214. ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override;
  1215. virtual bool is_lexical_declaration() const override { return true; }
  1216. StringView name() const { return m_class_expression->name(); }
  1217. private:
  1218. virtual bool is_class_declaration() const override { return true; }
  1219. friend ExportStatement;
  1220. NonnullRefPtr<ClassExpression const> m_class_expression;
  1221. };
  1222. // We use this class to mimic Initializer : = AssignmentExpression of
  1223. // 10.2.1.3 Runtime Semantics: EvaluateBody, https://tc39.es/ecma262/#sec-runtime-semantics-evaluatebody
  1224. class ClassFieldInitializerStatement final : public Statement {
  1225. public:
  1226. ClassFieldInitializerStatement(SourceRange source_range, NonnullRefPtr<Expression const> expression, DeprecatedFlyString field_name)
  1227. : Statement(move(source_range))
  1228. , m_expression(move(expression))
  1229. , m_class_field_identifier_name(move(field_name))
  1230. {
  1231. }
  1232. virtual Completion execute(Interpreter& interpreter) const override;
  1233. virtual void dump(int) const override;
  1234. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1235. private:
  1236. NonnullRefPtr<Expression const> m_expression;
  1237. DeprecatedFlyString m_class_field_identifier_name; // [[ClassFieldIdentifierName]]
  1238. };
  1239. class SpreadExpression final : public Expression {
  1240. public:
  1241. explicit SpreadExpression(SourceRange source_range, NonnullRefPtr<Expression const> target)
  1242. : Expression(source_range)
  1243. , m_target(move(target))
  1244. {
  1245. }
  1246. virtual Completion execute(Interpreter&) const override;
  1247. virtual void dump(int indent) const override;
  1248. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1249. private:
  1250. NonnullRefPtr<Expression const> m_target;
  1251. };
  1252. class ThisExpression final : public Expression {
  1253. public:
  1254. explicit ThisExpression(SourceRange source_range)
  1255. : Expression(source_range)
  1256. {
  1257. }
  1258. virtual Completion execute(Interpreter&) const override;
  1259. virtual void dump(int indent) const override;
  1260. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1261. };
  1262. struct CallExpressionArgument {
  1263. NonnullRefPtr<Expression const> value;
  1264. bool is_spread;
  1265. };
  1266. enum InvocationStyleEnum {
  1267. Parenthesized,
  1268. NotParenthesized,
  1269. };
  1270. enum InsideParenthesesEnum {
  1271. InsideParentheses,
  1272. NotInsideParentheses,
  1273. };
  1274. class CallExpression : public ASTNodeWithTailArray<CallExpression, Expression, CallExpressionArgument> {
  1275. friend class ASTNodeWithTailArray;
  1276. InvocationStyleEnum m_invocation_style;
  1277. InsideParenthesesEnum m_inside_parentheses;
  1278. public:
  1279. using Argument = CallExpressionArgument;
  1280. static NonnullRefPtr<CallExpression> create(SourceRange, NonnullRefPtr<Expression const> callee, ReadonlySpan<Argument> arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens);
  1281. virtual Completion execute(Interpreter&) const override;
  1282. virtual void dump(int indent) const override;
  1283. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1284. Expression const& callee() const { return m_callee; }
  1285. ReadonlySpan<Argument> arguments() const { return tail_span(); }
  1286. bool is_parenthesized() const { return m_invocation_style == InvocationStyleEnum::Parenthesized; }
  1287. bool is_inside_parens() const { return m_inside_parentheses == InsideParenthesesEnum::InsideParentheses; }
  1288. void set_inside_parens() { m_inside_parentheses = InsideParenthesesEnum::InsideParentheses; }
  1289. protected:
  1290. CallExpression(SourceRange source_range, NonnullRefPtr<Expression const> callee, ReadonlySpan<Argument> arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens = InsideParenthesesEnum::NotInsideParentheses)
  1291. : ASTNodeWithTailArray(move(source_range), arguments)
  1292. , m_invocation_style(invocation_style)
  1293. , m_inside_parentheses(inside_parens)
  1294. , m_callee(move(callee))
  1295. {
  1296. }
  1297. private:
  1298. struct ThisAndCallee {
  1299. Value this_value;
  1300. Value callee;
  1301. };
  1302. ThrowCompletionOr<ThisAndCallee> compute_this_and_callee(Interpreter&, Reference const&) const;
  1303. protected:
  1304. virtual bool is_call_expression() const override { return true; }
  1305. Completion throw_type_error_for_callee(Interpreter&, Value callee_value, StringView call_type) const;
  1306. Optional<DeprecatedString> expression_string() const;
  1307. NonnullRefPtr<Expression const> m_callee;
  1308. };
  1309. class NewExpression final : public CallExpression {
  1310. friend class ASTNodeWithTailArray;
  1311. public:
  1312. static NonnullRefPtr<NewExpression> create(SourceRange, NonnullRefPtr<Expression const> callee, ReadonlySpan<Argument> arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens);
  1313. virtual Completion execute(Interpreter&) const override;
  1314. virtual bool is_new_expression() const override { return true; }
  1315. private:
  1316. NewExpression(SourceRange source_range, NonnullRefPtr<Expression const> callee, ReadonlySpan<Argument> arguments, InvocationStyleEnum invocation_style, InsideParenthesesEnum inside_parens)
  1317. : CallExpression(move(source_range), move(callee), arguments, invocation_style, inside_parens)
  1318. {
  1319. }
  1320. };
  1321. static_assert(sizeof(NewExpression) == sizeof(CallExpression), "Adding members to NewExpression will break CallExpression memory layout");
  1322. class SuperCall final : public Expression {
  1323. public:
  1324. // This is here to be able to make a constructor like
  1325. // constructor(...args) { super(...args); } which does not use @@iterator of %Array.prototype%.
  1326. enum class IsPartOfSyntheticConstructor {
  1327. No,
  1328. Yes,
  1329. };
  1330. SuperCall(SourceRange source_range, Vector<CallExpression::Argument> arguments)
  1331. : Expression(source_range)
  1332. , m_arguments(move(arguments))
  1333. , m_is_synthetic(IsPartOfSyntheticConstructor::No)
  1334. {
  1335. }
  1336. SuperCall(SourceRange source_range, IsPartOfSyntheticConstructor is_part_of_synthetic_constructor, CallExpression::Argument constructor_argument)
  1337. : Expression(source_range)
  1338. , m_arguments({ move(constructor_argument) })
  1339. , m_is_synthetic(IsPartOfSyntheticConstructor::Yes)
  1340. {
  1341. VERIFY(is_part_of_synthetic_constructor == IsPartOfSyntheticConstructor::Yes);
  1342. }
  1343. virtual Completion execute(Interpreter&) const override;
  1344. virtual void dump(int indent) const override;
  1345. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1346. private:
  1347. Vector<CallExpression::Argument> const m_arguments;
  1348. IsPartOfSyntheticConstructor const m_is_synthetic;
  1349. };
  1350. enum class AssignmentOp {
  1351. Assignment,
  1352. AdditionAssignment,
  1353. SubtractionAssignment,
  1354. MultiplicationAssignment,
  1355. DivisionAssignment,
  1356. ModuloAssignment,
  1357. ExponentiationAssignment,
  1358. BitwiseAndAssignment,
  1359. BitwiseOrAssignment,
  1360. BitwiseXorAssignment,
  1361. LeftShiftAssignment,
  1362. RightShiftAssignment,
  1363. UnsignedRightShiftAssignment,
  1364. AndAssignment,
  1365. OrAssignment,
  1366. NullishAssignment,
  1367. };
  1368. class AssignmentExpression final : public Expression {
  1369. public:
  1370. AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr<Expression const> lhs, NonnullRefPtr<Expression const> rhs)
  1371. : Expression(source_range)
  1372. , m_op(op)
  1373. , m_lhs(move(lhs))
  1374. , m_rhs(move(rhs))
  1375. {
  1376. }
  1377. AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr<BindingPattern const> lhs, NonnullRefPtr<Expression const> rhs)
  1378. : Expression(source_range)
  1379. , m_op(op)
  1380. , m_lhs(move(lhs))
  1381. , m_rhs(move(rhs))
  1382. {
  1383. }
  1384. virtual Completion execute(Interpreter&) const override;
  1385. virtual void dump(int indent) const override;
  1386. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1387. private:
  1388. AssignmentOp m_op;
  1389. Variant<NonnullRefPtr<Expression const>, NonnullRefPtr<BindingPattern const>> m_lhs;
  1390. NonnullRefPtr<Expression const> m_rhs;
  1391. };
  1392. enum class UpdateOp {
  1393. Increment,
  1394. Decrement,
  1395. };
  1396. class UpdateExpression final : public Expression {
  1397. public:
  1398. UpdateExpression(SourceRange source_range, UpdateOp op, NonnullRefPtr<Expression const> argument, bool prefixed = false)
  1399. : Expression(source_range)
  1400. , m_op(op)
  1401. , m_argument(move(argument))
  1402. , m_prefixed(prefixed)
  1403. {
  1404. }
  1405. virtual Completion execute(Interpreter&) const override;
  1406. virtual void dump(int indent) const override;
  1407. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1408. private:
  1409. virtual bool is_update_expression() const override { return true; }
  1410. UpdateOp m_op;
  1411. NonnullRefPtr<Expression const> m_argument;
  1412. bool m_prefixed;
  1413. };
  1414. enum class DeclarationKind {
  1415. Var,
  1416. Let,
  1417. Const,
  1418. };
  1419. class VariableDeclarator final : public ASTNode {
  1420. public:
  1421. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier const> id)
  1422. : ASTNode(source_range)
  1423. , m_target(move(id))
  1424. {
  1425. }
  1426. VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier const> target, RefPtr<Expression const> init)
  1427. : ASTNode(source_range)
  1428. , m_target(move(target))
  1429. , m_init(move(init))
  1430. {
  1431. }
  1432. VariableDeclarator(SourceRange source_range, Variant<NonnullRefPtr<Identifier const>, NonnullRefPtr<BindingPattern const>> target, RefPtr<Expression const> init)
  1433. : ASTNode(source_range)
  1434. , m_target(move(target))
  1435. , m_init(move(init))
  1436. {
  1437. }
  1438. auto& target() const { return m_target; }
  1439. Expression const* init() const { return m_init; }
  1440. virtual Completion execute(Interpreter&) const override;
  1441. virtual void dump(int indent) const override;
  1442. private:
  1443. Variant<NonnullRefPtr<Identifier const>, NonnullRefPtr<BindingPattern const>> m_target;
  1444. RefPtr<Expression const> m_init;
  1445. };
  1446. class VariableDeclaration final : public Declaration {
  1447. public:
  1448. VariableDeclaration(SourceRange source_range, DeclarationKind declaration_kind, Vector<NonnullRefPtr<VariableDeclarator const>> declarations)
  1449. : Declaration(source_range)
  1450. , m_declaration_kind(declaration_kind)
  1451. , m_declarations(move(declarations))
  1452. {
  1453. }
  1454. DeclarationKind declaration_kind() const { return m_declaration_kind; }
  1455. virtual Completion execute(Interpreter&) const override;
  1456. virtual void dump(int indent) const override;
  1457. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1458. Vector<NonnullRefPtr<VariableDeclarator const>> const& declarations() const { return m_declarations; }
  1459. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
  1460. ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override;
  1461. virtual bool is_constant_declaration() const override { return m_declaration_kind == DeclarationKind::Const; };
  1462. virtual bool is_lexical_declaration() const override { return m_declaration_kind != DeclarationKind::Var; }
  1463. private:
  1464. virtual bool is_variable_declaration() const override { return true; }
  1465. DeclarationKind m_declaration_kind;
  1466. Vector<NonnullRefPtr<VariableDeclarator const>> m_declarations;
  1467. };
  1468. class UsingDeclaration final : public Declaration {
  1469. public:
  1470. UsingDeclaration(SourceRange source_range, Vector<NonnullRefPtr<VariableDeclarator const>> declarations)
  1471. : Declaration(move(source_range))
  1472. , m_declarations(move(declarations))
  1473. {
  1474. }
  1475. virtual Completion execute(Interpreter&) const override;
  1476. virtual void dump(int indent) const override;
  1477. virtual ThrowCompletionOr<void> for_each_bound_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const override;
  1478. ThrowCompletionOr<void> for_each_bound_identifier(ThrowCompletionOrVoidCallback<Identifier const&>&&) const override;
  1479. virtual bool is_constant_declaration() const override { return true; };
  1480. virtual bool is_lexical_declaration() const override { return true; }
  1481. Vector<NonnullRefPtr<VariableDeclarator const>> const& declarations() const { return m_declarations; }
  1482. private:
  1483. Vector<NonnullRefPtr<VariableDeclarator const>> m_declarations;
  1484. };
  1485. class ObjectProperty final : public ASTNode {
  1486. public:
  1487. enum class Type : u8 {
  1488. KeyValue,
  1489. Getter,
  1490. Setter,
  1491. Spread,
  1492. ProtoSetter,
  1493. };
  1494. ObjectProperty(SourceRange source_range, NonnullRefPtr<Expression const> key, RefPtr<Expression const> value, Type property_type, bool is_method)
  1495. : ASTNode(source_range)
  1496. , m_property_type(property_type)
  1497. , m_is_method(is_method)
  1498. , m_key(move(key))
  1499. , m_value(move(value))
  1500. {
  1501. }
  1502. Expression const& key() const { return m_key; }
  1503. Expression const& value() const
  1504. {
  1505. VERIFY(m_value);
  1506. return *m_value;
  1507. }
  1508. Type type() const { return m_property_type; }
  1509. bool is_method() const { return m_is_method; }
  1510. virtual void dump(int indent) const override;
  1511. virtual Completion execute(Interpreter&) const override;
  1512. private:
  1513. Type m_property_type;
  1514. bool m_is_method { false };
  1515. NonnullRefPtr<Expression const> m_key;
  1516. RefPtr<Expression const> m_value;
  1517. };
  1518. class ObjectExpression final : public Expression {
  1519. public:
  1520. explicit ObjectExpression(SourceRange source_range, Vector<NonnullRefPtr<ObjectProperty>> properties = {})
  1521. : Expression(source_range)
  1522. , m_properties(move(properties))
  1523. {
  1524. }
  1525. virtual Completion execute(Interpreter&) const override;
  1526. virtual void dump(int indent) const override;
  1527. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1528. private:
  1529. virtual bool is_object_expression() const override { return true; }
  1530. Vector<NonnullRefPtr<ObjectProperty>> m_properties;
  1531. };
  1532. class ArrayExpression final : public Expression {
  1533. public:
  1534. ArrayExpression(SourceRange source_range, Vector<RefPtr<Expression const>> elements)
  1535. : Expression(source_range)
  1536. , m_elements(move(elements))
  1537. {
  1538. }
  1539. Vector<RefPtr<Expression const>> const& elements() const { return m_elements; }
  1540. virtual Completion execute(Interpreter&) const override;
  1541. virtual void dump(int indent) const override;
  1542. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1543. private:
  1544. virtual bool is_array_expression() const override { return true; }
  1545. Vector<RefPtr<Expression const>> m_elements;
  1546. };
  1547. class TemplateLiteral final : public Expression {
  1548. public:
  1549. TemplateLiteral(SourceRange source_range, Vector<NonnullRefPtr<Expression const>> expressions)
  1550. : Expression(source_range)
  1551. , m_expressions(move(expressions))
  1552. {
  1553. }
  1554. TemplateLiteral(SourceRange source_range, Vector<NonnullRefPtr<Expression const>> expressions, Vector<NonnullRefPtr<Expression const>> raw_strings)
  1555. : Expression(source_range)
  1556. , m_expressions(move(expressions))
  1557. , m_raw_strings(move(raw_strings))
  1558. {
  1559. }
  1560. virtual Completion execute(Interpreter&) const override;
  1561. virtual void dump(int indent) const override;
  1562. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1563. Vector<NonnullRefPtr<Expression const>> const& expressions() const { return m_expressions; }
  1564. Vector<NonnullRefPtr<Expression const>> const& raw_strings() const { return m_raw_strings; }
  1565. private:
  1566. Vector<NonnullRefPtr<Expression const>> const m_expressions;
  1567. Vector<NonnullRefPtr<Expression const>> const m_raw_strings;
  1568. };
  1569. class TaggedTemplateLiteral final : public Expression {
  1570. public:
  1571. TaggedTemplateLiteral(SourceRange source_range, NonnullRefPtr<Expression const> tag, NonnullRefPtr<TemplateLiteral const> template_literal)
  1572. : Expression(source_range)
  1573. , m_tag(move(tag))
  1574. , m_template_literal(move(template_literal))
  1575. {
  1576. }
  1577. virtual Completion execute(Interpreter&) const override;
  1578. virtual void dump(int indent) const override;
  1579. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1580. ThrowCompletionOr<Value> get_template_object(Interpreter&) const;
  1581. private:
  1582. NonnullRefPtr<Expression const> const m_tag;
  1583. NonnullRefPtr<TemplateLiteral const> const m_template_literal;
  1584. mutable HashMap<GCPtr<Realm>, Handle<Array>> m_cached_values;
  1585. };
  1586. class MemberExpression final : public Expression {
  1587. public:
  1588. MemberExpression(SourceRange source_range, NonnullRefPtr<Expression const> object, NonnullRefPtr<Expression const> property, bool computed = false)
  1589. : Expression(source_range)
  1590. , m_computed(computed)
  1591. , m_object(move(object))
  1592. , m_property(move(property))
  1593. {
  1594. }
  1595. virtual Completion execute(Interpreter&) const override;
  1596. virtual void dump(int indent) const override;
  1597. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const override;
  1598. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1599. bool is_computed() const { return m_computed; }
  1600. Expression const& object() const { return *m_object; }
  1601. Expression const& property() const { return *m_property; }
  1602. DeprecatedString to_string_approximation() const;
  1603. bool ends_in_private_name() const;
  1604. private:
  1605. virtual bool is_member_expression() const override { return true; }
  1606. bool m_computed { false };
  1607. NonnullRefPtr<Expression const> m_object;
  1608. NonnullRefPtr<Expression const> m_property;
  1609. };
  1610. class OptionalChain final : public Expression {
  1611. public:
  1612. enum class Mode {
  1613. Optional,
  1614. NotOptional,
  1615. };
  1616. struct Call {
  1617. Vector<CallExpression::Argument> arguments;
  1618. Mode mode;
  1619. };
  1620. struct ComputedReference {
  1621. NonnullRefPtr<Expression const> expression;
  1622. Mode mode;
  1623. };
  1624. struct MemberReference {
  1625. NonnullRefPtr<Identifier const> identifier;
  1626. Mode mode;
  1627. };
  1628. struct PrivateMemberReference {
  1629. NonnullRefPtr<PrivateIdentifier const> private_identifier;
  1630. Mode mode;
  1631. };
  1632. using Reference = Variant<Call, ComputedReference, MemberReference, PrivateMemberReference>;
  1633. OptionalChain(SourceRange source_range, NonnullRefPtr<Expression const> base, Vector<Reference> references)
  1634. : Expression(source_range)
  1635. , m_base(move(base))
  1636. , m_references(move(references))
  1637. {
  1638. }
  1639. virtual Completion execute(Interpreter&) const override;
  1640. virtual ThrowCompletionOr<JS::Reference> to_reference(Interpreter&) const override;
  1641. virtual void dump(int indent) const override;
  1642. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1643. Expression const& base() const { return *m_base; }
  1644. Vector<Reference> const& references() const { return m_references; }
  1645. private:
  1646. struct ReferenceAndValue {
  1647. JS::Reference reference;
  1648. Value value;
  1649. };
  1650. ThrowCompletionOr<ReferenceAndValue> to_reference_and_value(Interpreter&) const;
  1651. NonnullRefPtr<Expression const> m_base;
  1652. Vector<Reference> m_references;
  1653. };
  1654. class MetaProperty final : public Expression {
  1655. public:
  1656. enum class Type {
  1657. NewTarget,
  1658. ImportMeta,
  1659. };
  1660. MetaProperty(SourceRange source_range, Type type)
  1661. : Expression(source_range)
  1662. , m_type(type)
  1663. {
  1664. }
  1665. virtual Completion execute(Interpreter&) const override;
  1666. virtual void dump(int indent) const override;
  1667. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1668. private:
  1669. Type m_type;
  1670. };
  1671. class ImportCall final : public Expression {
  1672. public:
  1673. ImportCall(SourceRange source_range, NonnullRefPtr<Expression const> specifier, RefPtr<Expression const> options)
  1674. : Expression(source_range)
  1675. , m_specifier(move(specifier))
  1676. , m_options(move(options))
  1677. {
  1678. }
  1679. virtual void dump(int indent) const override;
  1680. virtual Completion execute(Interpreter&) const override;
  1681. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1682. private:
  1683. virtual bool is_import_call() const override { return true; }
  1684. NonnullRefPtr<Expression const> m_specifier;
  1685. RefPtr<Expression const> m_options;
  1686. };
  1687. class ConditionalExpression final : public Expression {
  1688. public:
  1689. ConditionalExpression(SourceRange source_range, NonnullRefPtr<Expression const> test, NonnullRefPtr<Expression const> consequent, NonnullRefPtr<Expression const> alternate)
  1690. : Expression(source_range)
  1691. , m_test(move(test))
  1692. , m_consequent(move(consequent))
  1693. , m_alternate(move(alternate))
  1694. {
  1695. }
  1696. virtual void dump(int indent) const override;
  1697. virtual Completion execute(Interpreter&) const override;
  1698. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1699. private:
  1700. NonnullRefPtr<Expression const> m_test;
  1701. NonnullRefPtr<Expression const> m_consequent;
  1702. NonnullRefPtr<Expression const> m_alternate;
  1703. };
  1704. class CatchClause final : public ASTNode {
  1705. public:
  1706. CatchClause(SourceRange source_range, DeprecatedFlyString parameter, NonnullRefPtr<BlockStatement const> body)
  1707. : ASTNode(source_range)
  1708. , m_parameter(move(parameter))
  1709. , m_body(move(body))
  1710. {
  1711. }
  1712. CatchClause(SourceRange source_range, NonnullRefPtr<BindingPattern const> parameter, NonnullRefPtr<BlockStatement const> body)
  1713. : ASTNode(source_range)
  1714. , m_parameter(move(parameter))
  1715. , m_body(move(body))
  1716. {
  1717. }
  1718. auto& parameter() const { return m_parameter; }
  1719. BlockStatement const& body() const { return m_body; }
  1720. virtual void dump(int indent) const override;
  1721. virtual Completion execute(Interpreter&) const override;
  1722. private:
  1723. Variant<DeprecatedFlyString, NonnullRefPtr<BindingPattern const>> m_parameter;
  1724. NonnullRefPtr<BlockStatement const> m_body;
  1725. };
  1726. class TryStatement final : public Statement {
  1727. public:
  1728. TryStatement(SourceRange source_range, NonnullRefPtr<BlockStatement const> block, RefPtr<CatchClause const> handler, RefPtr<BlockStatement const> finalizer)
  1729. : Statement(source_range)
  1730. , m_block(move(block))
  1731. , m_handler(move(handler))
  1732. , m_finalizer(move(finalizer))
  1733. {
  1734. }
  1735. BlockStatement const& block() const { return m_block; }
  1736. CatchClause const* handler() const { return m_handler; }
  1737. BlockStatement const* finalizer() const { return m_finalizer; }
  1738. virtual void dump(int indent) const override;
  1739. virtual Completion execute(Interpreter&) const override;
  1740. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1741. private:
  1742. NonnullRefPtr<BlockStatement const> m_block;
  1743. RefPtr<CatchClause const> m_handler;
  1744. RefPtr<BlockStatement const> m_finalizer;
  1745. };
  1746. class ThrowStatement final : public Statement {
  1747. public:
  1748. explicit ThrowStatement(SourceRange source_range, NonnullRefPtr<Expression const> argument)
  1749. : Statement(source_range)
  1750. , m_argument(move(argument))
  1751. {
  1752. }
  1753. Expression const& argument() const { return m_argument; }
  1754. virtual void dump(int indent) const override;
  1755. virtual Completion execute(Interpreter&) const override;
  1756. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1757. private:
  1758. NonnullRefPtr<Expression const> m_argument;
  1759. };
  1760. class SwitchCase final : public ScopeNode {
  1761. public:
  1762. SwitchCase(SourceRange source_range, RefPtr<Expression const> test)
  1763. : ScopeNode(source_range)
  1764. , m_test(move(test))
  1765. {
  1766. }
  1767. Expression const* test() const { return m_test; }
  1768. virtual void dump(int indent) const override;
  1769. virtual Completion execute(Interpreter&) const override;
  1770. private:
  1771. RefPtr<Expression const> m_test;
  1772. };
  1773. class SwitchStatement final : public ScopeNode {
  1774. public:
  1775. SwitchStatement(SourceRange source_range, NonnullRefPtr<Expression const> discriminant)
  1776. : ScopeNode(source_range)
  1777. , m_discriminant(move(discriminant))
  1778. {
  1779. }
  1780. virtual void dump(int indent) const override;
  1781. virtual Completion execute(Interpreter&) const override;
  1782. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1783. virtual Bytecode::CodeGenerationErrorOr<void> generate_labelled_evaluation(Bytecode::Generator&, Vector<DeprecatedFlyString> const&) const;
  1784. Completion execute_impl(Interpreter&) const;
  1785. void add_case(NonnullRefPtr<SwitchCase const> switch_case) { m_cases.append(move(switch_case)); }
  1786. private:
  1787. NonnullRefPtr<Expression const> m_discriminant;
  1788. Vector<NonnullRefPtr<SwitchCase const>> m_cases;
  1789. };
  1790. class BreakStatement final : public Statement {
  1791. public:
  1792. BreakStatement(SourceRange source_range, DeprecatedFlyString target_label)
  1793. : Statement(source_range)
  1794. , m_target_label(move(target_label))
  1795. {
  1796. }
  1797. virtual Completion execute(Interpreter&) const override;
  1798. DeprecatedFlyString const& target_label() const { return m_target_label; }
  1799. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1800. private:
  1801. DeprecatedFlyString m_target_label;
  1802. };
  1803. class ContinueStatement final : public Statement {
  1804. public:
  1805. ContinueStatement(SourceRange source_range, DeprecatedFlyString target_label)
  1806. : Statement(source_range)
  1807. , m_target_label(move(target_label))
  1808. {
  1809. }
  1810. virtual Completion execute(Interpreter&) const override;
  1811. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1812. DeprecatedFlyString const& target_label() const { return m_target_label; }
  1813. private:
  1814. DeprecatedFlyString m_target_label;
  1815. };
  1816. class DebuggerStatement final : public Statement {
  1817. public:
  1818. explicit DebuggerStatement(SourceRange source_range)
  1819. : Statement(source_range)
  1820. {
  1821. }
  1822. virtual Completion execute(Interpreter&) const override;
  1823. virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
  1824. };
  1825. class SyntheticReferenceExpression final : public Expression {
  1826. public:
  1827. explicit SyntheticReferenceExpression(SourceRange source_range, Reference reference, Value value)
  1828. : Expression(source_range)
  1829. , m_reference(move(reference))
  1830. , m_value(value)
  1831. {
  1832. }
  1833. virtual Completion execute(Interpreter&) const override { return m_value; }
  1834. virtual ThrowCompletionOr<Reference> to_reference(Interpreter&) const override { return m_reference; }
  1835. private:
  1836. Reference m_reference;
  1837. Value m_value;
  1838. };
  1839. template<>
  1840. inline bool ASTNode::fast_is<NewExpression>() const { return is_new_expression(); }
  1841. template<>
  1842. inline bool ASTNode::fast_is<MemberExpression>() const { return is_member_expression(); }
  1843. template<>
  1844. inline bool ASTNode::fast_is<SuperExpression>() const { return is_super_expression(); }
  1845. template<>
  1846. inline bool ASTNode::fast_is<FunctionExpression>() const { return is_function_expression(); }
  1847. template<>
  1848. inline bool ASTNode::fast_is<ClassExpression>() const { return is_class_expression(); }
  1849. template<>
  1850. inline bool ASTNode::fast_is<Identifier>() const { return is_identifier(); }
  1851. template<>
  1852. inline bool ASTNode::fast_is<PrivateIdentifier>() const { return is_private_identifier(); }
  1853. template<>
  1854. inline bool ASTNode::fast_is<ExpressionStatement>() const { return is_expression_statement(); }
  1855. template<>
  1856. inline bool ASTNode::fast_is<ScopeNode>() const { return is_scope_node(); }
  1857. template<>
  1858. inline bool ASTNode::fast_is<Program>() const { return is_program(); }
  1859. template<>
  1860. inline bool ASTNode::fast_is<ClassDeclaration>() const { return is_class_declaration(); }
  1861. template<>
  1862. inline bool ASTNode::fast_is<FunctionDeclaration>() const { return is_function_declaration(); }
  1863. template<>
  1864. inline bool ASTNode::fast_is<VariableDeclaration>() const { return is_variable_declaration(); }
  1865. template<>
  1866. inline bool ASTNode::fast_is<ArrayExpression>() const { return is_array_expression(); }
  1867. template<>
  1868. inline bool ASTNode::fast_is<ObjectExpression>() const { return is_object_expression(); }
  1869. template<>
  1870. inline bool ASTNode::fast_is<ImportCall>() const { return is_import_call(); }
  1871. template<>
  1872. inline bool ASTNode::fast_is<StringLiteral>() const { return is_string_literal(); }
  1873. template<>
  1874. inline bool ASTNode::fast_is<UpdateExpression>() const { return is_update_expression(); }
  1875. template<>
  1876. inline bool ASTNode::fast_is<CallExpression>() const { return is_call_expression(); }
  1877. template<>
  1878. inline bool ASTNode::fast_is<LabelledStatement>() const { return is_labelled_statement(); }
  1879. template<>
  1880. inline bool ASTNode::fast_is<IterationStatement>() const { return is_iteration_statement(); }
  1881. template<>
  1882. inline bool ASTNode::fast_is<ClassMethod>() const { return is_class_method(); }
  1883. }