AST.h 76 KB

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