AST.h 50 KB

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