AST.h 71 KB

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