AST.h 81 KB

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