AST.h 83 KB

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