AST.h 81 KB

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