AST.h 71 KB

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