AST.h 76 KB

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