AST.h 76 KB

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