AST.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/NonnullRefPtrVector.h>
  9. #include <AK/Optional.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/String.h>
  12. #include <AK/TypeCasts.h>
  13. #include <AK/Vector.h>
  14. #include <LibCpp/Lexer.h>
  15. namespace Cpp {
  16. class ASTNode;
  17. class TranslationUnit;
  18. class Declaration;
  19. class FunctionDefinition;
  20. class Type;
  21. class Parameter;
  22. class Statement;
  23. class Name;
  24. class ASTNode : public RefCounted<ASTNode> {
  25. public:
  26. virtual ~ASTNode() = default;
  27. virtual const char* class_name() const = 0;
  28. virtual void dump(FILE* = stdout, size_t indent = 0) const;
  29. template<typename T>
  30. bool fast_is() const = delete;
  31. ASTNode* parent() const { return m_parent; }
  32. Position start() const
  33. {
  34. VERIFY(m_start.has_value());
  35. return m_start.value();
  36. }
  37. Position end() const
  38. {
  39. VERIFY(m_end.has_value());
  40. return m_end.value();
  41. }
  42. const FlyString& filename() const
  43. {
  44. return m_filename;
  45. }
  46. void set_end(const Position& end) { m_end = end; }
  47. void set_parent(ASTNode& parent) { m_parent = &parent; }
  48. virtual NonnullRefPtrVector<Declaration> declarations() const { return {}; }
  49. virtual bool is_identifier() const { return false; }
  50. virtual bool is_member_expression() const { return false; }
  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_dummy_node() const { return false; }
  57. protected:
  58. ASTNode(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  59. : m_parent(parent)
  60. , m_start(start)
  61. , m_end(end)
  62. , m_filename(filename)
  63. {
  64. }
  65. private:
  66. ASTNode* m_parent { nullptr };
  67. Optional<Position> m_start;
  68. Optional<Position> m_end;
  69. FlyString m_filename;
  70. };
  71. class TranslationUnit : public ASTNode {
  72. public:
  73. virtual ~TranslationUnit() override = default;
  74. virtual const char* class_name() const override { return "TranslationUnit"; }
  75. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  76. virtual NonnullRefPtrVector<Declaration> declarations() const override { return m_declarations; }
  77. TranslationUnit(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  78. : ASTNode(parent, start, end, filename)
  79. {
  80. }
  81. void set_declarations(NonnullRefPtrVector<Declaration>&& declarations) { m_declarations = move(declarations); }
  82. private:
  83. NonnullRefPtrVector<Declaration> m_declarations;
  84. };
  85. class Statement : public ASTNode {
  86. public:
  87. virtual ~Statement() override = default;
  88. virtual const char* class_name() const override { return "Statement"; }
  89. virtual NonnullRefPtrVector<Declaration> declarations() const override;
  90. protected:
  91. Statement(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  92. : ASTNode(parent, start, end, filename)
  93. {
  94. }
  95. };
  96. class Declaration : public Statement {
  97. public:
  98. virtual bool is_declaration() const override { return true; }
  99. virtual bool is_variable_declaration() const { return false; }
  100. virtual bool is_parameter() const { return false; }
  101. virtual bool is_struct_or_class() const { return false; }
  102. virtual bool is_struct() const { return false; }
  103. virtual bool is_class() const { return false; }
  104. virtual bool is_function() const { return false; }
  105. virtual bool is_namespace() const { return false; }
  106. virtual bool is_member() const { return false; }
  107. const StringView& name() const { return m_name; }
  108. void set_name(const StringView& name) { m_name = move(name); }
  109. protected:
  110. Declaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  111. : Statement(parent, start, end, filename)
  112. {
  113. }
  114. StringView m_name;
  115. };
  116. class InvalidDeclaration : public Declaration {
  117. public:
  118. virtual ~InvalidDeclaration() override = default;
  119. virtual const char* class_name() const override { return "InvalidDeclaration"; }
  120. InvalidDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  121. : Declaration(parent, start, end, filename)
  122. {
  123. }
  124. };
  125. class FunctionDeclaration : public Declaration {
  126. public:
  127. virtual ~FunctionDeclaration() override = default;
  128. virtual const char* class_name() const override { return "FunctionDeclaration"; }
  129. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  130. virtual bool is_function() const override { return true; }
  131. virtual bool is_constructor() const { return false; }
  132. virtual bool is_destructor() const { return false; }
  133. RefPtr<FunctionDefinition> definition() { return m_definition; }
  134. FunctionDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  135. : Declaration(parent, start, end, filename)
  136. {
  137. }
  138. virtual NonnullRefPtrVector<Declaration> declarations() const override;
  139. const Vector<StringView>& qualifiers() const { return m_qualifiers; }
  140. void set_qualifiers(const Vector<StringView>& qualifiers) { m_qualifiers = qualifiers; }
  141. const Type* return_type() const { return m_return_type.ptr(); }
  142. void set_return_type(const RefPtr<Type>& return_type) { m_return_type = return_type; }
  143. const NonnullRefPtrVector<Parameter>& parameters() const { return m_parameters; }
  144. void set_parameters(const NonnullRefPtrVector<Parameter>& parameters) { m_parameters = parameters; }
  145. const FunctionDefinition* definition() const { return m_definition.ptr(); }
  146. void set_definition(RefPtr<FunctionDefinition>&& definition) { m_definition = move(definition); }
  147. private:
  148. Vector<StringView> m_qualifiers;
  149. RefPtr<Type> m_return_type;
  150. NonnullRefPtrVector<Parameter> m_parameters;
  151. RefPtr<FunctionDefinition> m_definition;
  152. };
  153. class VariableOrParameterDeclaration : public Declaration {
  154. public:
  155. virtual ~VariableOrParameterDeclaration() override = default;
  156. virtual bool is_variable_or_parameter_declaration() const override { return true; }
  157. void set_type(RefPtr<Type>&& type) { m_type = move(type); }
  158. const Type* type() const { return m_type.ptr(); }
  159. protected:
  160. VariableOrParameterDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  161. : Declaration(parent, start, end, filename)
  162. {
  163. }
  164. RefPtr<Type> m_type;
  165. };
  166. class Parameter : public VariableOrParameterDeclaration {
  167. public:
  168. virtual ~Parameter() override = default;
  169. virtual const char* class_name() const override { return "Parameter"; }
  170. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  171. virtual bool is_parameter() const override { return true; }
  172. Parameter(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename, StringView name)
  173. : VariableOrParameterDeclaration(parent, start, end, filename)
  174. {
  175. m_name = name;
  176. }
  177. bool is_ellipsis() const { return m_is_ellipsis; }
  178. void set_ellipsis(bool is_ellipsis) { m_is_ellipsis = is_ellipsis; }
  179. private:
  180. bool m_is_ellipsis { false };
  181. };
  182. class Type : public ASTNode {
  183. public:
  184. virtual ~Type() override = default;
  185. virtual const char* class_name() const override { return "Type"; }
  186. virtual bool is_type() const override { return true; }
  187. virtual bool is_templatized() const { return false; }
  188. virtual bool is_named_type() const { return false; }
  189. virtual String to_string() const = 0;
  190. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  191. bool is_auto() const { return m_is_auto; }
  192. void set_auto(bool is_auto) { m_is_auto = is_auto; }
  193. Vector<StringView> const& qualifiers() const { return m_qualifiers; }
  194. void set_qualifiers(Vector<StringView>&& qualifiers) { m_qualifiers = move(qualifiers); }
  195. protected:
  196. Type(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  197. : ASTNode(parent, start, end, filename)
  198. {
  199. }
  200. private:
  201. bool m_is_auto { false };
  202. Vector<StringView> m_qualifiers;
  203. };
  204. class NamedType : public Type {
  205. public:
  206. virtual ~NamedType() override = default;
  207. virtual const char* class_name() const override { return "NamedType"; }
  208. virtual String to_string() const override;
  209. virtual bool is_named_type() const override { return true; }
  210. NamedType(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  211. : Type(parent, start, end, filename)
  212. {
  213. }
  214. const Name* name() const { return m_name.ptr(); }
  215. void set_name(RefPtr<Name>&& name) { m_name = move(name); }
  216. private:
  217. RefPtr<Name> m_name;
  218. };
  219. class Pointer : public Type {
  220. public:
  221. virtual ~Pointer() override = default;
  222. virtual const char* class_name() const override { return "Pointer"; }
  223. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  224. virtual String to_string() const override;
  225. Pointer(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  226. : Type(parent, start, end, filename)
  227. {
  228. }
  229. const Type* pointee() const { return m_pointee.ptr(); }
  230. void set_pointee(RefPtr<Type>&& pointee) { m_pointee = move(pointee); }
  231. private:
  232. RefPtr<Type> m_pointee;
  233. };
  234. class FunctionDefinition : public ASTNode {
  235. public:
  236. virtual ~FunctionDefinition() override = default;
  237. virtual const char* class_name() const override { return "FunctionDefinition"; }
  238. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  239. FunctionDefinition(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  240. : ASTNode(parent, start, end, filename)
  241. {
  242. }
  243. virtual NonnullRefPtrVector<Declaration> declarations() const override;
  244. NonnullRefPtrVector<Statement> const& statements() { return m_statements; }
  245. void add_statement(NonnullRefPtr<Statement>&& statement) { m_statements.append(move(statement)); }
  246. private:
  247. NonnullRefPtrVector<Statement> m_statements;
  248. };
  249. class InvalidStatement : public Statement {
  250. public:
  251. virtual ~InvalidStatement() override = default;
  252. virtual const char* class_name() const override { return "InvalidStatement"; }
  253. InvalidStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  254. : Statement(parent, start, end, filename)
  255. {
  256. }
  257. };
  258. class Expression : public Statement {
  259. public:
  260. virtual ~Expression() override = default;
  261. virtual const char* class_name() const override { return "Expression"; }
  262. protected:
  263. Expression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  264. : Statement(parent, start, end, filename)
  265. {
  266. }
  267. };
  268. class InvalidExpression : public Expression {
  269. public:
  270. virtual ~InvalidExpression() override = default;
  271. virtual const char* class_name() const override { return "InvalidExpression"; }
  272. InvalidExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  273. : Expression(parent, start, end, filename)
  274. {
  275. }
  276. };
  277. class VariableDeclaration : public VariableOrParameterDeclaration {
  278. public:
  279. virtual ~VariableDeclaration() override = default;
  280. virtual const char* class_name() const override { return "VariableDeclaration"; }
  281. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  282. VariableDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  283. : VariableOrParameterDeclaration(parent, start, end, filename)
  284. {
  285. }
  286. virtual bool is_variable_declaration() const override { return true; }
  287. const Expression* initial_value() const { return m_initial_value; }
  288. void set_initial_value(RefPtr<Expression>&& initial_value) { m_initial_value = move(initial_value); }
  289. private:
  290. RefPtr<Expression> m_initial_value;
  291. };
  292. class Identifier : public Expression {
  293. public:
  294. virtual ~Identifier() override = default;
  295. virtual const char* class_name() const override { return "Identifier"; }
  296. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  297. Identifier(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename, StringView name)
  298. : Expression(parent, start, end, filename)
  299. , m_name(name)
  300. {
  301. }
  302. Identifier(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  303. : Identifier(parent, start, end, filename, {})
  304. {
  305. }
  306. virtual bool is_identifier() const override { return true; }
  307. StringView const& name() const { return m_name; }
  308. void set_name(StringView&& name) { m_name = move(name); }
  309. private:
  310. StringView m_name;
  311. };
  312. class Name : public Expression {
  313. public:
  314. virtual ~Name() override = default;
  315. virtual const char* class_name() const override { return "Name"; }
  316. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  317. virtual bool is_name() const override { return true; }
  318. virtual bool is_templatized() const { return false; }
  319. Name(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  320. : Expression(parent, start, end, filename)
  321. {
  322. }
  323. virtual String full_name() const;
  324. const Identifier* name() const { return m_name.ptr(); }
  325. void set_name(RefPtr<Identifier>&& name) { m_name = move(name); }
  326. NonnullRefPtrVector<Identifier> const& scope() const { return m_scope; }
  327. void set_scope(NonnullRefPtrVector<Identifier> scope) { m_scope = move(scope); }
  328. void add_to_scope(NonnullRefPtr<Identifier>&& part) { m_scope.append(move(part)); }
  329. private:
  330. RefPtr<Identifier> m_name;
  331. NonnullRefPtrVector<Identifier> m_scope;
  332. };
  333. class TemplatizedName : public Name {
  334. public:
  335. virtual ~TemplatizedName() override = default;
  336. virtual const char* class_name() const override { return "TemplatizedName"; }
  337. virtual bool is_templatized() const override { return true; }
  338. virtual String full_name() const override;
  339. TemplatizedName(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  340. : Name(parent, start, end, filename)
  341. {
  342. }
  343. void add_template_argument(NonnullRefPtr<Type>&& type) { m_template_arguments.append(move(type)); }
  344. private:
  345. NonnullRefPtrVector<Type> m_template_arguments;
  346. };
  347. class NumericLiteral : public Expression {
  348. public:
  349. virtual ~NumericLiteral() override = default;
  350. virtual const char* class_name() const override { return "NumericLiteral"; }
  351. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  352. NumericLiteral(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename, StringView value)
  353. : Expression(parent, start, end, filename)
  354. , m_value(value)
  355. {
  356. }
  357. private:
  358. StringView m_value;
  359. };
  360. class NullPointerLiteral : public Expression {
  361. public:
  362. virtual ~NullPointerLiteral() override = default;
  363. virtual const char* class_name() const override { return "NullPointerLiteral"; }
  364. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  365. NullPointerLiteral(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  366. : Expression(parent, start, end, filename)
  367. {
  368. }
  369. };
  370. class BooleanLiteral : public Expression {
  371. public:
  372. virtual ~BooleanLiteral() override = default;
  373. virtual const char* class_name() const override { return "BooleanLiteral"; }
  374. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  375. BooleanLiteral(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename, bool value)
  376. : Expression(parent, start, end, filename)
  377. , m_value(value)
  378. {
  379. }
  380. private:
  381. bool m_value;
  382. };
  383. enum class BinaryOp {
  384. Addition,
  385. Subtraction,
  386. Multiplication,
  387. Division,
  388. Modulo,
  389. GreaterThan,
  390. GreaterThanEquals,
  391. LessThan,
  392. LessThanEquals,
  393. BitwiseAnd,
  394. BitwiseOr,
  395. BitwiseXor,
  396. LeftShift,
  397. RightShift,
  398. EqualsEquals,
  399. NotEqual,
  400. LogicalOr,
  401. LogicalAnd,
  402. Arrow,
  403. };
  404. class BinaryExpression : public Expression {
  405. public:
  406. BinaryExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  407. : Expression(parent, start, end, filename)
  408. {
  409. }
  410. virtual ~BinaryExpression() override = default;
  411. virtual const char* class_name() const override { return "BinaryExpression"; }
  412. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  413. BinaryOp op() const { return m_op; }
  414. void set_op(BinaryOp op) { m_op = op; }
  415. Expression const* lhs() const { return m_lhs.ptr(); }
  416. void set_lhs(RefPtr<Expression>&& e) { m_lhs = move(e); }
  417. Expression const* rhs() const { return m_rhs.ptr(); }
  418. void set_rhs(RefPtr<Expression>&& e) { m_rhs = move(e); }
  419. private:
  420. BinaryOp m_op;
  421. RefPtr<Expression> m_lhs;
  422. RefPtr<Expression> m_rhs;
  423. };
  424. enum class AssignmentOp {
  425. Assignment,
  426. AdditionAssignment,
  427. SubtractionAssignment,
  428. };
  429. class AssignmentExpression : public Expression {
  430. public:
  431. AssignmentExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  432. : Expression(parent, start, end, filename)
  433. {
  434. }
  435. virtual ~AssignmentExpression() override = default;
  436. virtual const char* class_name() const override { return "AssignmentExpression"; }
  437. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  438. AssignmentOp op() const { return m_op; }
  439. void set_op(AssignmentOp op) { m_op = op; }
  440. const Expression* lhs() const { return m_lhs; }
  441. void set_lhs(RefPtr<Expression>&& e) { m_lhs = move(e); }
  442. const Expression* rhs() const { return m_rhs; }
  443. void set_rhs(RefPtr<Expression>&& e) { m_rhs = move(e); }
  444. private:
  445. AssignmentOp m_op {};
  446. RefPtr<Expression> m_lhs;
  447. RefPtr<Expression> m_rhs;
  448. };
  449. class FunctionCall : public Expression {
  450. public:
  451. FunctionCall(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  452. : Expression(parent, start, end, filename)
  453. {
  454. }
  455. virtual ~FunctionCall() override = default;
  456. virtual const char* class_name() const override { return "FunctionCall"; }
  457. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  458. virtual bool is_function_call() const override { return true; }
  459. const Expression* callee() const { return m_callee.ptr(); }
  460. void set_callee(RefPtr<Expression>&& callee) { m_callee = move(callee); }
  461. void add_argument(NonnullRefPtr<Expression>&& arg) { m_arguments.append(move(arg)); }
  462. NonnullRefPtrVector<Expression> const& arguments() const { return m_arguments; }
  463. private:
  464. RefPtr<Expression> m_callee;
  465. NonnullRefPtrVector<Expression> m_arguments;
  466. };
  467. class StringLiteral final : public Expression {
  468. public:
  469. StringLiteral(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  470. : Expression(parent, start, end, filename)
  471. {
  472. }
  473. ~StringLiteral() override = default;
  474. virtual const char* class_name() const override { return "StringLiteral"; }
  475. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  476. String const& value() const { return m_value; }
  477. void set_value(String value) { m_value = move(value); }
  478. private:
  479. String m_value;
  480. };
  481. class ReturnStatement : public Statement {
  482. public:
  483. virtual ~ReturnStatement() override = default;
  484. virtual const char* class_name() const override { return "ReturnStatement"; }
  485. ReturnStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  486. : Statement(parent, start, end, filename)
  487. {
  488. }
  489. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  490. const Expression* value() const { return m_value.ptr(); }
  491. void set_value(RefPtr<Expression>&& value) { m_value = move(value); }
  492. private:
  493. RefPtr<Expression> m_value;
  494. };
  495. class EnumDeclaration : public Declaration {
  496. public:
  497. virtual ~EnumDeclaration() override = default;
  498. virtual const char* class_name() const override { return "EnumDeclaration"; }
  499. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  500. EnumDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  501. : Declaration(parent, start, end, filename)
  502. {
  503. }
  504. enum class Type {
  505. RegularEnum,
  506. EnumClass
  507. };
  508. void set_type(Type type) { m_type = type; }
  509. void add_entry(StringView entry) { m_entries.append(move(entry)); }
  510. private:
  511. Type m_type { Type::RegularEnum };
  512. Vector<StringView> m_entries;
  513. };
  514. class StructOrClassDeclaration : public Declaration {
  515. public:
  516. virtual ~StructOrClassDeclaration() override = default;
  517. virtual const char* class_name() const override { return "StructOrClassDeclaration"; }
  518. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  519. virtual bool is_struct_or_class() const override { return true; }
  520. virtual bool is_struct() const override { return m_type == Type::Struct; }
  521. virtual bool is_class() const override { return m_type == Type::Class; }
  522. virtual NonnullRefPtrVector<Declaration> declarations() const override;
  523. enum class Type {
  524. Struct,
  525. Class
  526. };
  527. StructOrClassDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename, StructOrClassDeclaration::Type type)
  528. : Declaration(parent, start, end, filename)
  529. , m_type(type)
  530. {
  531. }
  532. NonnullRefPtrVector<Declaration> const& members() const { return m_members; }
  533. void set_members(NonnullRefPtrVector<Declaration>&& members) { m_members = move(members); }
  534. private:
  535. StructOrClassDeclaration::Type m_type;
  536. NonnullRefPtrVector<Declaration> m_members;
  537. };
  538. enum class UnaryOp {
  539. Invalid,
  540. BitwiseNot,
  541. Not,
  542. Plus,
  543. Minus,
  544. PlusPlus,
  545. Address,
  546. };
  547. class UnaryExpression : public Expression {
  548. public:
  549. UnaryExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  550. : Expression(parent, start, end, filename)
  551. {
  552. }
  553. virtual ~UnaryExpression() override = default;
  554. virtual const char* class_name() const override { return "UnaryExpression"; }
  555. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  556. void set_op(UnaryOp op) { m_op = op; }
  557. void set_lhs(RefPtr<Expression>&& e) { m_lhs = move(e); }
  558. private:
  559. UnaryOp m_op;
  560. RefPtr<Expression> m_lhs;
  561. };
  562. class MemberExpression : public Expression {
  563. public:
  564. MemberExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  565. : Expression(parent, start, end, filename)
  566. {
  567. }
  568. virtual ~MemberExpression() override = default;
  569. virtual const char* class_name() const override { return "MemberExpression"; }
  570. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  571. virtual bool is_member_expression() const override { return true; }
  572. const Expression* object() const { return m_object.ptr(); }
  573. void set_object(RefPtr<Expression>&& object) { m_object = move(object); }
  574. const Expression* property() const { return m_property.ptr(); }
  575. void set_property(RefPtr<Expression>&& property) { m_property = move(property); }
  576. private:
  577. RefPtr<Expression> m_object;
  578. RefPtr<Expression> m_property;
  579. };
  580. class ForStatement : public Statement {
  581. public:
  582. ForStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  583. : Statement(parent, start, end, filename)
  584. {
  585. }
  586. virtual ~ForStatement() override = default;
  587. virtual const char* class_name() const override { return "ForStatement"; }
  588. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  589. virtual NonnullRefPtrVector<Declaration> declarations() const override;
  590. void set_init(RefPtr<VariableDeclaration>&& init) { m_init = move(init); }
  591. void set_test(RefPtr<Expression>&& test) { m_test = move(test); }
  592. void set_update(RefPtr<Expression>&& update) { m_update = move(update); }
  593. void set_body(RefPtr<Statement>&& body) { m_body = move(body); }
  594. const Statement* body() const { return m_body.ptr(); }
  595. private:
  596. RefPtr<VariableDeclaration> m_init;
  597. RefPtr<Expression> m_test;
  598. RefPtr<Expression> m_update;
  599. RefPtr<Statement> m_body;
  600. };
  601. class BlockStatement final : public Statement {
  602. public:
  603. BlockStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  604. : Statement(parent, start, end, filename)
  605. {
  606. }
  607. virtual ~BlockStatement() override = default;
  608. virtual const char* class_name() const override { return "BlockStatement"; }
  609. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  610. virtual NonnullRefPtrVector<Declaration> declarations() const override;
  611. void add_statement(NonnullRefPtr<Statement>&& statement) { m_statements.append(move(statement)); }
  612. private:
  613. NonnullRefPtrVector<Statement> m_statements;
  614. };
  615. class Comment final : public Statement {
  616. public:
  617. Comment(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  618. : Statement(parent, start, end, filename)
  619. {
  620. }
  621. virtual ~Comment() override = default;
  622. virtual const char* class_name() const override { return "Comment"; }
  623. };
  624. class IfStatement : public Statement {
  625. public:
  626. IfStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  627. : Statement(parent, start, end, filename)
  628. {
  629. }
  630. virtual ~IfStatement() override = default;
  631. virtual const char* class_name() const override { return "IfStatement"; }
  632. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  633. virtual NonnullRefPtrVector<Declaration> declarations() const override;
  634. void set_predicate(RefPtr<Expression>&& predicate) { m_predicate = move(predicate); }
  635. void set_then_statement(RefPtr<Statement>&& then) { m_then = move(then); }
  636. void set_else_statement(RefPtr<Statement>&& _else) { m_else = move(_else); }
  637. const Statement* then_statement() const { return m_then.ptr(); }
  638. const Statement* else_statement() const { return m_else.ptr(); }
  639. private:
  640. RefPtr<Expression> m_predicate;
  641. RefPtr<Statement> m_then;
  642. RefPtr<Statement> m_else;
  643. };
  644. class NamespaceDeclaration : public Declaration {
  645. public:
  646. virtual ~NamespaceDeclaration() override = default;
  647. virtual const char* class_name() const override { return "NamespaceDeclaration"; }
  648. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  649. virtual bool is_namespace() const override { return true; }
  650. NamespaceDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  651. : Declaration(parent, start, end, filename)
  652. {
  653. }
  654. virtual NonnullRefPtrVector<Declaration> declarations() const override { return m_declarations; }
  655. void add_declaration(NonnullRefPtr<Declaration>&& declaration) { m_declarations.append(move(declaration)); }
  656. private:
  657. NonnullRefPtrVector<Declaration> m_declarations;
  658. };
  659. class CppCastExpression : public Expression {
  660. public:
  661. CppCastExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  662. : Expression(parent, start, end, filename)
  663. {
  664. }
  665. virtual ~CppCastExpression() override = default;
  666. virtual const char* class_name() const override { return "CppCastExpression"; }
  667. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  668. void set_cast_type(StringView cast_type) { m_cast_type = move(cast_type); }
  669. void set_type(NonnullRefPtr<Type>&& type) { m_type = move(type); }
  670. void set_expression(NonnullRefPtr<Expression>&& e) { m_expression = move(e); }
  671. private:
  672. StringView m_cast_type;
  673. RefPtr<Type> m_type;
  674. RefPtr<Expression> m_expression;
  675. };
  676. class CStyleCastExpression : public Expression {
  677. public:
  678. CStyleCastExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  679. : Expression(parent, start, end, filename)
  680. {
  681. }
  682. virtual ~CStyleCastExpression() override = default;
  683. virtual const char* class_name() const override { return "CStyleCastExpression"; }
  684. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  685. void set_type(NonnullRefPtr<Type>&& type) { m_type = move(type); }
  686. void set_expression(NonnullRefPtr<Expression>&& e) { m_expression = move(e); }
  687. private:
  688. RefPtr<Type> m_type;
  689. RefPtr<Expression> m_expression;
  690. };
  691. class SizeofExpression : public Expression {
  692. public:
  693. SizeofExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  694. : Expression(parent, start, end, filename)
  695. {
  696. }
  697. virtual ~SizeofExpression() override = default;
  698. virtual const char* class_name() const override { return "SizeofExpression"; }
  699. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  700. void set_type(RefPtr<Type>&& type) { m_type = move(type); }
  701. private:
  702. RefPtr<Type> m_type;
  703. };
  704. class BracedInitList : public Expression {
  705. public:
  706. BracedInitList(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  707. : Expression(parent, start, end, filename)
  708. {
  709. }
  710. virtual ~BracedInitList() override = default;
  711. virtual const char* class_name() const override { return "BracedInitList"; }
  712. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  713. void add_expression(NonnullRefPtr<Expression>&& exp) { m_expressions.append(move(exp)); }
  714. private:
  715. NonnullRefPtrVector<Expression> m_expressions;
  716. };
  717. class DummyAstNode : public ASTNode {
  718. public:
  719. DummyAstNode(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  720. : ASTNode(parent, start, end, filename)
  721. {
  722. }
  723. virtual bool is_dummy_node() const override { return true; }
  724. virtual const char* class_name() const override { return "DummyAstNode"; }
  725. virtual void dump(FILE* = stdout, size_t = 0) const override { }
  726. };
  727. class Constructor : public FunctionDeclaration {
  728. public:
  729. virtual ~Constructor() override = default;
  730. virtual const char* class_name() const override { return "Constructor"; }
  731. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  732. virtual bool is_constructor() const override { return true; }
  733. Constructor(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  734. : FunctionDeclaration(parent, start, end, filename)
  735. {
  736. }
  737. };
  738. class Destructor : public FunctionDeclaration {
  739. public:
  740. virtual ~Destructor() override = default;
  741. virtual const char* class_name() const override { return "Destructor"; }
  742. virtual void dump(FILE* = stdout, size_t indent = 0) const override;
  743. virtual bool is_destructor() const override { return true; }
  744. Destructor(ASTNode* parent, Optional<Position> start, Optional<Position> end, const String& filename)
  745. : FunctionDeclaration(parent, start, end, filename)
  746. {
  747. }
  748. };
  749. template<>
  750. inline bool ASTNode::fast_is<Identifier>() const { return is_identifier(); }
  751. template<>
  752. inline bool ASTNode::fast_is<MemberExpression>() const { return is_member_expression(); }
  753. template<>
  754. inline bool ASTNode::fast_is<VariableOrParameterDeclaration>() const { return is_variable_or_parameter_declaration(); }
  755. template<>
  756. inline bool ASTNode::fast_is<FunctionCall>() const { return is_function_call(); }
  757. template<>
  758. inline bool ASTNode::fast_is<Type>() const { return is_type(); }
  759. template<>
  760. inline bool ASTNode::fast_is<Declaration>() const { return is_declaration(); }
  761. template<>
  762. inline bool ASTNode::fast_is<Name>() const { return is_name(); }
  763. template<>
  764. inline bool ASTNode::fast_is<DummyAstNode>() const { return is_dummy_node(); }
  765. template<>
  766. inline bool ASTNode::fast_is<VariableDeclaration>() const { return is_declaration() && verify_cast<Declaration>(*this).is_variable_declaration(); }
  767. template<>
  768. inline bool ASTNode::fast_is<StructOrClassDeclaration>() const { return is_declaration() && verify_cast<Declaration>(*this).is_struct_or_class(); }
  769. template<>
  770. inline bool ASTNode::fast_is<FunctionDeclaration>() const { return is_declaration() && verify_cast<Declaration>(*this).is_function(); }
  771. template<>
  772. inline bool ASTNode::fast_is<NamespaceDeclaration>() const { return is_declaration() && verify_cast<Declaration>(*this).is_namespace(); }
  773. template<>
  774. inline bool ASTNode::fast_is<Constructor>() const { return is_declaration() && verify_cast<Declaration>(*this).is_function() && verify_cast<FunctionDeclaration>(*this).is_constructor(); }
  775. template<>
  776. inline bool ASTNode::fast_is<Destructor>() const { return is_declaration() && verify_cast<Declaration>(*this).is_function() && verify_cast<FunctionDeclaration>(*this).is_destructor(); }
  777. template<>
  778. inline bool ASTNode::fast_is<NamedType>() const { return is_type() && verify_cast<Type>(*this).is_named_type(); }
  779. template<>
  780. inline bool ASTNode::fast_is<TemplatizedName>() const { return is_name() && verify_cast<Name>(*this).is_templatized(); }
  781. }