AST.h 82 KB

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