AST.h 83 KB

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