AST.h 58 KB

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