AST.h 71 KB

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