AST.h 82 KB

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