AST.h 81 KB

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