AST.h 63 KB

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