AST.h 71 KB

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