AST.h 77 KB

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