AST.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2023, Volodymyr V. <vvmposeydon@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <AK/Optional.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/String.h>
  12. #include <AK/StringView.h>
  13. #include <AK/TypeCasts.h>
  14. #include <AK/Vector.h>
  15. #include <LibCore/File.h>
  16. #include <LibGLSL/Lexer.h>
  17. namespace GLSL {
  18. class ASTNode;
  19. class TranslationUnit;
  20. class Declaration;
  21. class FunctionDefinition;
  22. class Type;
  23. class Parameter;
  24. class Statement;
  25. class Name;
  26. class ASTNode : public RefCounted<ASTNode> {
  27. public:
  28. virtual ~ASTNode() = default;
  29. virtual StringView class_name() const = 0;
  30. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const;
  31. template<typename T>
  32. bool fast_is() const = delete;
  33. ASTNode const* parent() const { return m_parent; }
  34. Position start() const
  35. {
  36. VERIFY(m_start.has_value());
  37. return m_start.value();
  38. }
  39. Position end() const
  40. {
  41. VERIFY(m_end.has_value());
  42. return m_end.value();
  43. }
  44. FlyString const& filename() const
  45. {
  46. return m_filename;
  47. }
  48. void set_end(Position const& end) { m_end = end; }
  49. void set_parent(ASTNode const& parent) { m_parent = &parent; }
  50. virtual Vector<NonnullRefPtr<Declaration const>> declarations() const { return {}; }
  51. virtual bool is_variable_or_parameter_declaration() const { return false; }
  52. virtual bool is_function_call() const { return false; }
  53. virtual bool is_type() const { return false; }
  54. virtual bool is_declaration() const { return false; }
  55. virtual bool is_name() const { return false; }
  56. virtual bool is_member_expression() const { return true; }
  57. virtual bool is_dummy_node() const { return false; }
  58. protected:
  59. ASTNode(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  60. : m_parent(parent)
  61. , m_start(start)
  62. , m_end(end)
  63. , m_filename(filename)
  64. {
  65. }
  66. private:
  67. ASTNode const* m_parent { nullptr };
  68. Optional<Position> m_start;
  69. Optional<Position> m_end;
  70. FlyString m_filename;
  71. };
  72. class TranslationUnit : public ASTNode {
  73. public:
  74. virtual ~TranslationUnit() override = default;
  75. virtual StringView class_name() const override { return "TranslationUnit"sv; }
  76. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  77. virtual Vector<NonnullRefPtr<Declaration const>> declarations() const override { return m_declarations; }
  78. TranslationUnit(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  79. : ASTNode(parent, start, end, filename)
  80. {
  81. }
  82. void set_declarations(Vector<NonnullRefPtr<Declaration const>>&& declarations) { m_declarations = move(declarations); }
  83. private:
  84. Vector<NonnullRefPtr<Declaration const>> m_declarations;
  85. };
  86. class Statement : public ASTNode {
  87. public:
  88. virtual ~Statement() override = default;
  89. virtual StringView class_name() const override { return "Statement"sv; }
  90. virtual Vector<NonnullRefPtr<Declaration const>> declarations() const override;
  91. protected:
  92. Statement(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  93. : ASTNode(parent, start, end, filename)
  94. {
  95. }
  96. };
  97. class Declaration : public Statement {
  98. public:
  99. virtual bool is_declaration() const override { return true; }
  100. virtual bool is_variable_declaration() const { return false; }
  101. virtual bool is_parameter() const { return false; }
  102. virtual bool is_struct() const { return false; }
  103. virtual bool is_function() const { return false; }
  104. Name const* name() const { return m_name; }
  105. void set_name(RefPtr<Name const> name) { m_name = move(name); }
  106. protected:
  107. Declaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  108. : Statement(parent, start, end, filename)
  109. {
  110. }
  111. RefPtr<Name const> m_name;
  112. };
  113. class InvalidDeclaration : public Declaration {
  114. public:
  115. virtual ~InvalidDeclaration() override = default;
  116. virtual StringView class_name() const override { return "InvalidDeclaration"sv; }
  117. InvalidDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  118. : Declaration(parent, start, end, filename)
  119. {
  120. }
  121. };
  122. class FunctionDeclaration : public Declaration {
  123. public:
  124. virtual ~FunctionDeclaration() override = default;
  125. virtual StringView class_name() const override { return "FunctionDeclaration"sv; }
  126. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  127. virtual bool is_function() const override { return true; }
  128. RefPtr<FunctionDefinition const> definition() { return m_definition; }
  129. FunctionDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  130. : Declaration(parent, start, end, filename)
  131. {
  132. }
  133. virtual Vector<NonnullRefPtr<Declaration const>> declarations() const override;
  134. Type const* return_type() const { return m_return_type.ptr(); }
  135. void set_return_type(RefPtr<Type const> const& return_type) { m_return_type = return_type; }
  136. Vector<NonnullRefPtr<Parameter const>> const& parameters() const { return m_parameters; }
  137. void set_parameters(Vector<NonnullRefPtr<Parameter const>> const& parameters) { m_parameters = parameters; }
  138. FunctionDefinition const* definition() const { return m_definition.ptr(); }
  139. void set_definition(RefPtr<FunctionDefinition const>&& definition) { m_definition = move(definition); }
  140. private:
  141. RefPtr<Type const> m_return_type;
  142. Vector<NonnullRefPtr<Parameter const>> m_parameters;
  143. RefPtr<FunctionDefinition const> m_definition;
  144. };
  145. class VariableOrParameterDeclaration : public Declaration {
  146. public:
  147. virtual ~VariableOrParameterDeclaration() override = default;
  148. virtual bool is_variable_or_parameter_declaration() const override { return true; }
  149. void set_type(RefPtr<Type const>&& type) { m_type = move(type); }
  150. Type const* type() const { return m_type.ptr(); }
  151. protected:
  152. VariableOrParameterDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  153. : Declaration(parent, start, end, filename)
  154. {
  155. }
  156. RefPtr<Type const> m_type;
  157. };
  158. class Parameter : public VariableOrParameterDeclaration {
  159. public:
  160. virtual ~Parameter() override = default;
  161. virtual StringView class_name() const override { return "Parameter"sv; }
  162. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  163. virtual bool is_parameter() const override { return true; }
  164. Parameter(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename, RefPtr<Name const> name)
  165. : VariableOrParameterDeclaration(parent, start, end, filename)
  166. {
  167. m_name = name;
  168. }
  169. };
  170. enum class StorageTypeQualifier {
  171. Const,
  172. In,
  173. Out,
  174. Inout,
  175. Centroid,
  176. Patch,
  177. Sample,
  178. Uniform,
  179. Buffer,
  180. Shared,
  181. Coherent,
  182. Volatile,
  183. Restrict,
  184. Readonly,
  185. Writeonly,
  186. Subroutine,
  187. };
  188. class Type : public ASTNode {
  189. public:
  190. virtual ~Type() override = default;
  191. virtual StringView class_name() const override { return "Type"sv; }
  192. virtual bool is_type() const override { return true; }
  193. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  194. Type(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  195. : ASTNode(parent, start, end, filename)
  196. {
  197. }
  198. Name const* name() const { return m_name.ptr(); }
  199. void set_name(RefPtr<Name const>&& name) { m_name = move(name); }
  200. Vector<StorageTypeQualifier> const& storage_qualifiers() const { return m_storage_qualifiers; }
  201. void set_storage_qualifiers(Vector<StorageTypeQualifier>&& storage_qualifiers) { m_storage_qualifiers = move(storage_qualifiers); }
  202. private:
  203. RefPtr<Name const> m_name;
  204. Vector<StorageTypeQualifier> m_storage_qualifiers;
  205. };
  206. class FunctionDefinition : public ASTNode {
  207. public:
  208. virtual ~FunctionDefinition() override = default;
  209. virtual StringView class_name() const override { return "FunctionDefinition"sv; }
  210. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  211. FunctionDefinition(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  212. : ASTNode(parent, start, end, filename)
  213. {
  214. }
  215. virtual Vector<NonnullRefPtr<Declaration const>> declarations() const override;
  216. Vector<NonnullRefPtr<Statement const>> const& statements() { return m_statements; }
  217. void add_statement(NonnullRefPtr<Statement const>&& statement) { m_statements.append(move(statement)); }
  218. private:
  219. Vector<NonnullRefPtr<Statement const>> m_statements;
  220. };
  221. class InvalidStatement : public Statement {
  222. public:
  223. virtual ~InvalidStatement() override = default;
  224. virtual StringView class_name() const override { return "InvalidStatement"sv; }
  225. InvalidStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  226. : Statement(parent, start, end, filename)
  227. {
  228. }
  229. };
  230. class Expression : public Statement {
  231. public:
  232. virtual ~Expression() override = default;
  233. virtual StringView class_name() const override { return "Expression"sv; }
  234. protected:
  235. Expression(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  236. : Statement(parent, start, end, filename)
  237. {
  238. }
  239. };
  240. class InvalidExpression : public Expression {
  241. public:
  242. virtual ~InvalidExpression() override = default;
  243. virtual StringView class_name() const override { return "InvalidExpression"sv; }
  244. InvalidExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  245. : Expression(parent, start, end, filename)
  246. {
  247. }
  248. };
  249. class VariableDeclaration : public VariableOrParameterDeclaration {
  250. public:
  251. virtual ~VariableDeclaration() override = default;
  252. virtual StringView class_name() const override { return "VariableDeclaration"sv; }
  253. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  254. VariableDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  255. : VariableOrParameterDeclaration(parent, start, end, filename)
  256. {
  257. }
  258. virtual bool is_variable_declaration() const override { return true; }
  259. Expression const* initial_value() const { return m_initial_value; }
  260. void set_initial_value(RefPtr<Expression const>&& initial_value) { m_initial_value = move(initial_value); }
  261. private:
  262. RefPtr<Expression const> m_initial_value;
  263. };
  264. class Name : public Expression {
  265. public:
  266. virtual ~Name() override = default;
  267. virtual StringView class_name() const override { return "Name"sv; }
  268. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  269. virtual bool is_name() const override { return true; }
  270. virtual bool is_sized() const { return false; }
  271. Name(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  272. : Expression(parent, start, end, filename)
  273. {
  274. }
  275. StringView name() const { return m_name; }
  276. void set_name(StringView name) { m_name = name; }
  277. private:
  278. StringView m_name;
  279. };
  280. class SizedName : public Name {
  281. public:
  282. virtual ~SizedName() override = default;
  283. virtual StringView class_name() const override { return "SizedName"sv; }
  284. virtual bool is_sized() const override { return true; }
  285. ErrorOr<void> dump(AK::Stream&, size_t indent) const override;
  286. SizedName(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  287. : Name(parent, start, end, filename)
  288. {
  289. }
  290. void append_dimension(StringView dim) { m_dimensions.append(dim); }
  291. private:
  292. Vector<StringView> m_dimensions;
  293. };
  294. class NumericLiteral : public Expression {
  295. public:
  296. virtual ~NumericLiteral() override = default;
  297. virtual StringView class_name() const override { return "NumericLiteral"sv; }
  298. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  299. NumericLiteral(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename, StringView value)
  300. : Expression(parent, start, end, filename)
  301. , m_value(value)
  302. {
  303. }
  304. private:
  305. StringView m_value;
  306. };
  307. class BooleanLiteral : public Expression {
  308. public:
  309. virtual ~BooleanLiteral() override = default;
  310. virtual StringView class_name() const override { return "BooleanLiteral"sv; }
  311. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  312. BooleanLiteral(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename, bool value)
  313. : Expression(parent, start, end, filename)
  314. , m_value(value)
  315. {
  316. }
  317. private:
  318. bool m_value;
  319. };
  320. enum class BinaryOp {
  321. Addition,
  322. Subtraction,
  323. Multiplication,
  324. Division,
  325. Modulo,
  326. GreaterThan,
  327. GreaterThanEquals,
  328. LessThan,
  329. LessThanEquals,
  330. BitwiseAnd,
  331. BitwiseOr,
  332. BitwiseXor,
  333. LeftShift,
  334. RightShift,
  335. EqualsEquals,
  336. NotEqual,
  337. LogicalOr,
  338. LogicalXor,
  339. LogicalAnd,
  340. Assignment,
  341. AdditionAssignment,
  342. SubtractionAssignment,
  343. MultiplicationAssignment,
  344. DivisionAssignment,
  345. ModuloAssignment,
  346. AndAssignment,
  347. OrAssignment,
  348. XorAssignment,
  349. LeftShiftAssignment,
  350. RightShiftAssignment,
  351. };
  352. class BinaryExpression : public Expression {
  353. public:
  354. BinaryExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  355. : Expression(parent, start, end, filename)
  356. {
  357. }
  358. virtual ~BinaryExpression() override = default;
  359. virtual StringView class_name() const override { return "BinaryExpression"sv; }
  360. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  361. BinaryOp op() const { return m_op; }
  362. void set_op(BinaryOp op) { m_op = op; }
  363. Expression const* lhs() const { return m_lhs.ptr(); }
  364. void set_lhs(RefPtr<Expression const>&& e) { m_lhs = move(e); }
  365. Expression const* rhs() const { return m_rhs.ptr(); }
  366. void set_rhs(RefPtr<Expression const>&& e) { m_rhs = move(e); }
  367. private:
  368. BinaryOp m_op;
  369. RefPtr<Expression const> m_lhs;
  370. RefPtr<Expression const> m_rhs;
  371. };
  372. class FunctionCall : public Expression {
  373. public:
  374. FunctionCall(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  375. : Expression(parent, start, end, filename)
  376. {
  377. }
  378. virtual ~FunctionCall() override = default;
  379. virtual StringView class_name() const override { return "FunctionCall"sv; }
  380. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  381. virtual bool is_function_call() const override { return true; }
  382. Expression const* callee() const { return m_callee; }
  383. void set_callee(RefPtr<Expression const>&& callee) { m_callee = move(callee); }
  384. void set_arguments(Vector<NonnullRefPtr<Expression const>>&& args) { m_arguments = move(args); }
  385. Vector<NonnullRefPtr<Expression const>> const& arguments() const { return m_arguments; }
  386. private:
  387. RefPtr<Expression const> m_callee;
  388. Vector<NonnullRefPtr<Expression const>> m_arguments;
  389. };
  390. class StringLiteral final : public Expression {
  391. public:
  392. StringLiteral(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  393. : Expression(parent, start, end, filename)
  394. {
  395. }
  396. ~StringLiteral() override = default;
  397. virtual StringView class_name() const override { return "StringLiteral"sv; }
  398. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  399. String const& value() const { return m_value; }
  400. void set_value(String value) { m_value = move(value); }
  401. private:
  402. String m_value;
  403. };
  404. class ReturnStatement : public Statement {
  405. public:
  406. virtual ~ReturnStatement() override = default;
  407. virtual StringView class_name() const override { return "ReturnStatement"sv; }
  408. ReturnStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  409. : Statement(parent, start, end, filename)
  410. {
  411. }
  412. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  413. Expression const* value() const { return m_value.ptr(); }
  414. void set_value(RefPtr<Expression const>&& value) { m_value = move(value); }
  415. private:
  416. RefPtr<Expression const> m_value;
  417. };
  418. class DiscardStatement : public Statement {
  419. public:
  420. virtual ~DiscardStatement() override = default;
  421. virtual StringView class_name() const override { return "DiscardStatement"sv; }
  422. DiscardStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  423. : Statement(parent, start, end, filename)
  424. {
  425. }
  426. };
  427. class StructDeclaration : public Declaration {
  428. public:
  429. virtual ~StructDeclaration() override = default;
  430. virtual StringView class_name() const override { return "StructDeclaration"sv; }
  431. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  432. virtual bool is_struct() const override { return true; }
  433. virtual Vector<NonnullRefPtr<Declaration const>> declarations() const override;
  434. StructDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  435. : Declaration(parent, start, end, filename)
  436. {
  437. }
  438. Vector<NonnullRefPtr<Declaration const>> const& members() const { return m_members; }
  439. void set_members(Vector<NonnullRefPtr<Declaration const>>&& members) { m_members = move(members); }
  440. private:
  441. Vector<NonnullRefPtr<Declaration const>> m_members;
  442. };
  443. enum class UnaryOp {
  444. BitwiseNot,
  445. Not,
  446. Plus,
  447. Minus,
  448. PlusPlus,
  449. MinusMinus,
  450. };
  451. class UnaryExpression : public Expression {
  452. public:
  453. UnaryExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  454. : Expression(parent, start, end, filename)
  455. {
  456. }
  457. virtual ~UnaryExpression() override = default;
  458. virtual StringView class_name() const override { return "UnaryExpression"sv; }
  459. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  460. UnaryOp op() const { return m_op; }
  461. void set_op(UnaryOp op) { m_op = op; }
  462. Expression const* lhs() const { return m_lhs.ptr(); }
  463. void set_lhs(RefPtr<Expression const>&& e) { m_lhs = move(e); }
  464. bool is_postfix() const { return m_is_postfix; }
  465. void set_is_postfix(bool is_postfix) { m_is_postfix = is_postfix; }
  466. private:
  467. UnaryOp m_op;
  468. RefPtr<Expression const> m_lhs;
  469. bool m_is_postfix = false;
  470. };
  471. class MemberExpression : public Expression {
  472. public:
  473. MemberExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  474. : Expression(parent, start, end, filename)
  475. {
  476. }
  477. virtual ~MemberExpression() override = default;
  478. virtual StringView class_name() const override { return "MemberExpression"sv; }
  479. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  480. virtual bool is_member_expression() const override { return true; }
  481. Expression const* object() const { return m_object.ptr(); }
  482. void set_object(RefPtr<Expression const>&& object) { m_object = move(object); }
  483. Expression const* property() const { return m_property.ptr(); }
  484. void set_property(RefPtr<Expression const>&& property) { m_property = move(property); }
  485. private:
  486. RefPtr<Expression const> m_object;
  487. RefPtr<Expression const> m_property;
  488. };
  489. class ArrayElementExpression : public Expression {
  490. public:
  491. ArrayElementExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  492. : Expression(parent, start, end, filename)
  493. {
  494. }
  495. virtual ~ArrayElementExpression() override = default;
  496. virtual StringView class_name() const override { return "ArrayElementExpression"sv; }
  497. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  498. Expression const* array() const { return m_array.ptr(); }
  499. void set_array(RefPtr<Expression const>&& array) { m_array = move(array); }
  500. Expression const* index() const { return m_index.ptr(); }
  501. void set_index(RefPtr<Expression const>&& index) { m_index = move(index); }
  502. private:
  503. RefPtr<Expression const> m_array;
  504. RefPtr<Expression const> m_index;
  505. };
  506. class ForStatement : public Statement {
  507. public:
  508. ForStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  509. : Statement(parent, start, end, filename)
  510. {
  511. }
  512. virtual ~ForStatement() override = default;
  513. virtual StringView class_name() const override { return "ForStatement"sv; }
  514. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  515. virtual Vector<NonnullRefPtr<Declaration const>> declarations() const override;
  516. void set_init(RefPtr<VariableDeclaration const>&& init) { m_init = move(init); }
  517. void set_test(RefPtr<Expression const>&& test) { m_test = move(test); }
  518. void set_update(RefPtr<Expression const>&& update) { m_update = move(update); }
  519. void set_body(RefPtr<Statement const>&& body) { m_body = move(body); }
  520. Statement const* body() const { return m_body.ptr(); }
  521. private:
  522. RefPtr<VariableDeclaration const> m_init;
  523. RefPtr<Expression const> m_test;
  524. RefPtr<Expression const> m_update;
  525. RefPtr<Statement const> m_body;
  526. };
  527. class BlockStatement final : public Statement {
  528. public:
  529. BlockStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  530. : Statement(parent, start, end, filename)
  531. {
  532. }
  533. virtual ~BlockStatement() override = default;
  534. virtual StringView class_name() const override { return "BlockStatement"sv; }
  535. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  536. virtual Vector<NonnullRefPtr<Declaration const>> declarations() const override;
  537. void add_statement(NonnullRefPtr<Statement const>&& statement) { m_statements.append(move(statement)); }
  538. private:
  539. Vector<NonnullRefPtr<Statement const>> m_statements;
  540. };
  541. class IfStatement : public Statement {
  542. public:
  543. IfStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  544. : Statement(parent, start, end, filename)
  545. {
  546. }
  547. virtual ~IfStatement() override = default;
  548. virtual StringView class_name() const override { return "IfStatement"sv; }
  549. virtual ErrorOr<void> dump(AK::Stream&, size_t indent = 0) const override;
  550. virtual Vector<NonnullRefPtr<Declaration const>> declarations() const override;
  551. void set_predicate(RefPtr<Expression const>&& predicate) { m_predicate = move(predicate); }
  552. void set_then_statement(RefPtr<Statement const>&& then) { m_then = move(then); }
  553. void set_else_statement(RefPtr<Statement const>&& _else) { m_else = move(_else); }
  554. Statement const* then_statement() const { return m_then.ptr(); }
  555. Statement const* else_statement() const { return m_else.ptr(); }
  556. private:
  557. RefPtr<Expression const> m_predicate;
  558. RefPtr<Statement const> m_then;
  559. RefPtr<Statement const> m_else;
  560. };
  561. class DummyAstNode : public ASTNode {
  562. public:
  563. DummyAstNode(ASTNode const* parent, Optional<Position> start, Optional<Position> end, String const& filename)
  564. : ASTNode(parent, start, end, filename)
  565. {
  566. }
  567. virtual bool is_dummy_node() const override { return true; }
  568. virtual StringView class_name() const override { return "DummyAstNode"sv; }
  569. virtual ErrorOr<void> dump(AK::Stream&, size_t = 0) const override { return {}; }
  570. };
  571. template<>
  572. inline bool ASTNode::fast_is<MemberExpression>() const { return is_member_expression(); }
  573. template<>
  574. inline bool ASTNode::fast_is<VariableOrParameterDeclaration>() const { return is_variable_or_parameter_declaration(); }
  575. template<>
  576. inline bool ASTNode::fast_is<FunctionCall>() const { return is_function_call(); }
  577. template<>
  578. inline bool ASTNode::fast_is<Type>() const { return is_type(); }
  579. template<>
  580. inline bool ASTNode::fast_is<Declaration>() const { return is_declaration(); }
  581. template<>
  582. inline bool ASTNode::fast_is<Name>() const { return is_name(); }
  583. template<>
  584. inline bool ASTNode::fast_is<DummyAstNode>() const { return is_dummy_node(); }
  585. template<>
  586. inline bool ASTNode::fast_is<VariableDeclaration>() const { return is_declaration() && verify_cast<Declaration>(*this).is_variable_declaration(); }
  587. template<>
  588. inline bool ASTNode::fast_is<FunctionDeclaration>() const { return is_declaration() && verify_cast<Declaration>(*this).is_function(); }
  589. template<>
  590. inline bool ASTNode::fast_is<SizedName>() const { return is_name() && verify_cast<Name>(*this).is_sized(); }
  591. }