Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:11

0001 //===- Nodes.h - syntax nodes for C/C++ grammar constructs ----*- C++ -*-=====//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 // Syntax tree nodes for C, C++ and Objective-C grammar constructs.
0009 //
0010 // Nodes provide access to their syntactic components, e.g. IfStatement provides
0011 // a way to get its condition, then and else branches, tokens for 'if' and
0012 // 'else' keywords.
0013 // When using the accessors, please assume they can return null. This happens
0014 // because:
0015 //   - the corresponding subnode is optional in the C++ grammar, e.g. an else
0016 //     branch of an if statement,
0017 //   - syntactic errors occurred while parsing the corresponding subnode.
0018 // One notable exception is "introducer" keywords, e.g. the accessor for the
0019 // 'if' keyword of an if statement will never return null.
0020 //===----------------------------------------------------------------------===//
0021 #ifndef LLVM_CLANG_TOOLING_SYNTAX_NODES_H
0022 #define LLVM_CLANG_TOOLING_SYNTAX_NODES_H
0023 
0024 #include "clang/Basic/LLVM.h"
0025 #include "clang/Tooling/Syntax/Tree.h"
0026 namespace clang {
0027 namespace syntax {
0028 
0029 /// A kind of a syntax node, used for implementing casts. The ordering and
0030 /// blocks of enumerator constants must correspond to the inheritance hierarchy
0031 /// of syntax::Node.
0032 enum class NodeKind : uint16_t {
0033 #define CONCRETE_NODE(Kind, Base) Kind,
0034 #include "clang/Tooling/Syntax/Nodes.inc"
0035 };
0036 /// For debugging purposes.
0037 raw_ostream &operator<<(raw_ostream &OS, NodeKind K);
0038 
0039 /// A relation between a parent and child node, e.g. 'left-hand-side of
0040 /// a binary expression'. Used for implementing accessors.
0041 ///
0042 /// In general `NodeRole`s should be named the same as their accessors.
0043 ///
0044 /// Some roles describe parent/child relations that occur multiple times in
0045 /// language grammar. We define only one role to describe all instances of such
0046 /// recurring relations. For example, grammar for both "if" and "while"
0047 /// statements requires an opening paren and a closing paren. The opening
0048 /// paren token is assigned the OpenParen role regardless of whether it appears
0049 /// as a child of IfStatement or WhileStatement node. More generally, when
0050 /// grammar requires a certain fixed token (like a specific keyword, or an
0051 /// opening paren), we define a role for this token and use it across all
0052 /// grammar rules with the same requirement. Names of such reusable roles end
0053 /// with a ~Token or a ~Keyword suffix.
0054 enum class NodeRole : uint8_t {
0055   // Roles common to multiple node kinds.
0056   /// A node without a parent
0057   Detached,
0058   /// Children of an unknown semantic nature, e.g. skipped tokens, comments.
0059   Unknown,
0060   /// An opening parenthesis in argument lists and blocks, e.g. '{', '(', etc.
0061   OpenParen,
0062   /// A closing parenthesis in argument lists and blocks, e.g. '}', ')', etc.
0063   CloseParen,
0064   /// A keywords that introduces some grammar construct, e.g. 'if', 'try', etc.
0065   IntroducerKeyword,
0066   /// A token that represents a literal, e.g. 'nullptr', '1', 'true', etc.
0067   LiteralToken,
0068   /// Tokens or Keywords.
0069   ArrowToken,
0070   ExternKeyword,
0071   TemplateKeyword,
0072   /// An inner statement for those that have only a single child of kind
0073   /// statement, e.g. loop body for while, for, etc; inner statement for case,
0074   /// default, etc.
0075   BodyStatement,
0076   /// List API roles.
0077   ListElement,
0078   ListDelimiter,
0079 
0080   // Roles specific to particular node kinds.
0081   OperatorToken,
0082   Operand,
0083   LeftHandSide,
0084   RightHandSide,
0085   ReturnValue,
0086   CaseValue,
0087   ThenStatement,
0088   ElseKeyword,
0089   ElseStatement,
0090   Expression,
0091   Statement,
0092   Condition,
0093   Message,
0094   Declarator,
0095   Declaration,
0096   Size,
0097   Parameters,
0098   TrailingReturn,
0099   UnqualifiedId,
0100   Qualifier,
0101   SubExpression,
0102   Object,
0103   AccessToken,
0104   Member,
0105   Callee,
0106   Arguments,
0107   Declarators
0108 };
0109 /// For debugging purposes.
0110 raw_ostream &operator<<(raw_ostream &OS, NodeRole R);
0111 
0112 #include "clang/Tooling/Syntax/NodeClasses.inc"
0113 
0114 /// Models a `nested-name-specifier`. C++ [expr.prim.id.qual]
0115 /// e.g. the `std::vector<int>::` in `std::vector<int>::size`.
0116 class NestedNameSpecifier final : public List {
0117 public:
0118   NestedNameSpecifier() : List(NodeKind::NestedNameSpecifier) {}
0119   static bool classof(const Node *N);
0120   std::vector<NameSpecifier *> getSpecifiers();
0121   std::vector<List::ElementAndDelimiter<syntax::NameSpecifier>>
0122   getSpecifiersAndDoubleColons();
0123 };
0124 
0125 /// Models an `unqualified-id`. C++ [expr.prim.id.unqual]
0126 /// e.g. the `size` in `std::vector<int>::size`.
0127 class UnqualifiedId final : public Tree {
0128 public:
0129   UnqualifiedId() : Tree(NodeKind::UnqualifiedId) {}
0130   static bool classof(const Node *N);
0131 };
0132 
0133 /// An expression of an unknown kind, i.e. one not currently handled by the
0134 /// syntax tree.
0135 class UnknownExpression final : public Expression {
0136 public:
0137   UnknownExpression() : Expression(NodeKind::UnknownExpression) {}
0138   static bool classof(const Node *N);
0139 };
0140 
0141 /// Models arguments of a function call.
0142 ///   call-arguments:
0143 ///     delimited_list(expression, ',')
0144 /// Note: This construct is a simplification of the grammar rule for
0145 /// `expression-list`, that is used in the definition of `call-expression`
0146 class CallArguments final : public List {
0147 public:
0148   CallArguments() : List(NodeKind::CallArguments) {}
0149   static bool classof(const Node *N);
0150   std::vector<Expression *> getArguments();
0151   std::vector<List::ElementAndDelimiter<Expression>> getArgumentsAndCommas();
0152 };
0153 
0154 /// An abstract class for prefix and postfix unary operators.
0155 class UnaryOperatorExpression : public Expression {
0156 public:
0157   UnaryOperatorExpression(NodeKind K) : Expression(K) {}
0158   static bool classof(const Node *N);
0159   Leaf *getOperatorToken();
0160   Expression *getOperand();
0161 };
0162 
0163 /// <operator> <operand>
0164 ///
0165 /// For example:
0166 ///   +a          -b
0167 ///   !c          not c
0168 ///   ~d          compl d
0169 ///   *e          &f
0170 ///   ++h         --h
0171 ///   __real i    __imag i
0172 class PrefixUnaryOperatorExpression final : public UnaryOperatorExpression {
0173 public:
0174   PrefixUnaryOperatorExpression()
0175       : UnaryOperatorExpression(NodeKind::PrefixUnaryOperatorExpression) {}
0176   static bool classof(const Node *N);
0177 };
0178 
0179 /// <operand> <operator>
0180 ///
0181 /// For example:
0182 ///   a++
0183 ///   b--
0184 class PostfixUnaryOperatorExpression final : public UnaryOperatorExpression {
0185 public:
0186   PostfixUnaryOperatorExpression()
0187       : UnaryOperatorExpression(NodeKind::PostfixUnaryOperatorExpression) {}
0188   static bool classof(const Node *N);
0189 };
0190 
0191 /// <lhs> <operator> <rhs>
0192 ///
0193 /// For example:
0194 ///   a + b
0195 ///   a bitor 1
0196 ///   a |= b
0197 ///   a and_eq b
0198 class BinaryOperatorExpression final : public Expression {
0199 public:
0200   BinaryOperatorExpression() : Expression(NodeKind::BinaryOperatorExpression) {}
0201   static bool classof(const Node *N);
0202   Expression *getLhs();
0203   Leaf *getOperatorToken();
0204   Expression *getRhs();
0205 };
0206 
0207 /// An abstract node for C++ statements, e.g. 'while', 'if', etc.
0208 /// FIXME: add accessors for semicolon of statements that have it.
0209 class Statement : public Tree {
0210 public:
0211   Statement(NodeKind K) : Tree(K) {}
0212   static bool classof(const Node *N);
0213 };
0214 
0215 /// A statement of an unknown kind, i.e. one not currently handled by the syntax
0216 /// tree.
0217 class UnknownStatement final : public Statement {
0218 public:
0219   UnknownStatement() : Statement(NodeKind::UnknownStatement) {}
0220   static bool classof(const Node *N);
0221 };
0222 
0223 /// E.g. 'int a, b = 10;'
0224 class DeclarationStatement final : public Statement {
0225 public:
0226   DeclarationStatement() : Statement(NodeKind::DeclarationStatement) {}
0227   static bool classof(const Node *N);
0228 };
0229 
0230 /// The no-op statement, i.e. ';'.
0231 class EmptyStatement final : public Statement {
0232 public:
0233   EmptyStatement() : Statement(NodeKind::EmptyStatement) {}
0234   static bool classof(const Node *N);
0235 };
0236 
0237 /// switch (<cond>) <body>
0238 class SwitchStatement final : public Statement {
0239 public:
0240   SwitchStatement() : Statement(NodeKind::SwitchStatement) {}
0241   static bool classof(const Node *N);
0242   Leaf *getSwitchKeyword();
0243   Statement *getBody();
0244 };
0245 
0246 /// case <value>: <body>
0247 class CaseStatement final : public Statement {
0248 public:
0249   CaseStatement() : Statement(NodeKind::CaseStatement) {}
0250   static bool classof(const Node *N);
0251   Leaf *getCaseKeyword();
0252   Expression *getCaseValue();
0253   Statement *getBody();
0254 };
0255 
0256 /// default: <body>
0257 class DefaultStatement final : public Statement {
0258 public:
0259   DefaultStatement() : Statement(NodeKind::DefaultStatement) {}
0260   static bool classof(const Node *N);
0261   Leaf *getDefaultKeyword();
0262   Statement *getBody();
0263 };
0264 
0265 /// if (cond) <then-statement> else <else-statement>
0266 /// FIXME: add condition that models 'expression  or variable declaration'
0267 class IfStatement final : public Statement {
0268 public:
0269   IfStatement() : Statement(NodeKind::IfStatement) {}
0270   static bool classof(const Node *N);
0271   Leaf *getIfKeyword();
0272   Statement *getThenStatement();
0273   Leaf *getElseKeyword();
0274   Statement *getElseStatement();
0275 };
0276 
0277 /// for (<init>; <cond>; <increment>) <body>
0278 class ForStatement final : public Statement {
0279 public:
0280   ForStatement() : Statement(NodeKind::ForStatement) {}
0281   static bool classof(const Node *N);
0282   Leaf *getForKeyword();
0283   Statement *getBody();
0284 };
0285 
0286 /// while (<cond>) <body>
0287 class WhileStatement final : public Statement {
0288 public:
0289   WhileStatement() : Statement(NodeKind::WhileStatement) {}
0290   static bool classof(const Node *N);
0291   Leaf *getWhileKeyword();
0292   Statement *getBody();
0293 };
0294 
0295 /// continue;
0296 class ContinueStatement final : public Statement {
0297 public:
0298   ContinueStatement() : Statement(NodeKind::ContinueStatement) {}
0299   static bool classof(const Node *N);
0300   Leaf *getContinueKeyword();
0301 };
0302 
0303 /// break;
0304 class BreakStatement final : public Statement {
0305 public:
0306   BreakStatement() : Statement(NodeKind::BreakStatement) {}
0307   static bool classof(const Node *N);
0308   Leaf *getBreakKeyword();
0309 };
0310 
0311 /// return <expr>;
0312 /// return;
0313 class ReturnStatement final : public Statement {
0314 public:
0315   ReturnStatement() : Statement(NodeKind::ReturnStatement) {}
0316   static bool classof(const Node *N);
0317   Leaf *getReturnKeyword();
0318   Expression *getReturnValue();
0319 };
0320 
0321 /// for (<decl> : <init>) <body>
0322 class RangeBasedForStatement final : public Statement {
0323 public:
0324   RangeBasedForStatement() : Statement(NodeKind::RangeBasedForStatement) {}
0325   static bool classof(const Node *N);
0326   Leaf *getForKeyword();
0327   Statement *getBody();
0328 };
0329 
0330 /// Expression in a statement position, e.g. functions calls inside compound
0331 /// statements or inside a loop body.
0332 class ExpressionStatement final : public Statement {
0333 public:
0334   ExpressionStatement() : Statement(NodeKind::ExpressionStatement) {}
0335   static bool classof(const Node *N);
0336   Expression *getExpression();
0337 };
0338 
0339 /// { statement1; statement2; … }
0340 class CompoundStatement final : public Statement {
0341 public:
0342   CompoundStatement() : Statement(NodeKind::CompoundStatement) {}
0343   static bool classof(const Node *N);
0344   Leaf *getLbrace();
0345   /// FIXME: use custom iterator instead of 'vector'.
0346   std::vector<Statement *> getStatements();
0347   Leaf *getRbrace();
0348 };
0349 
0350 /// A declaration that can appear at the top-level. Note that this does *not*
0351 /// correspond 1-to-1 to clang::Decl. Syntax trees distinguish between top-level
0352 /// declarations (e.g. namespace definitions) and declarators (e.g. variables,
0353 /// typedefs, etc.). Declarators are stored inside SimpleDeclaration.
0354 class Declaration : public Tree {
0355 public:
0356   Declaration(NodeKind K) : Tree(K) {}
0357   static bool classof(const Node *N);
0358 };
0359 
0360 /// Declaration of an unknown kind, e.g. not yet supported in syntax trees.
0361 class UnknownDeclaration final : public Declaration {
0362 public:
0363   UnknownDeclaration() : Declaration(NodeKind::UnknownDeclaration) {}
0364   static bool classof(const Node *N);
0365 };
0366 
0367 /// A semicolon in the top-level context. Does not declare anything.
0368 class EmptyDeclaration final : public Declaration {
0369 public:
0370   EmptyDeclaration() : Declaration(NodeKind::EmptyDeclaration) {}
0371   static bool classof(const Node *N);
0372 };
0373 
0374 /// static_assert(<condition>, <message>)
0375 /// static_assert(<condition>)
0376 class StaticAssertDeclaration final : public Declaration {
0377 public:
0378   StaticAssertDeclaration() : Declaration(NodeKind::StaticAssertDeclaration) {}
0379   static bool classof(const Node *N);
0380   Expression *getCondition();
0381   Expression *getMessage();
0382 };
0383 
0384 /// extern <string-literal> declaration
0385 /// extern <string-literal> { <decls>  }
0386 class LinkageSpecificationDeclaration final : public Declaration {
0387 public:
0388   LinkageSpecificationDeclaration()
0389       : Declaration(NodeKind::LinkageSpecificationDeclaration) {}
0390   static bool classof(const Node *N);
0391 };
0392 
0393 class DeclaratorList final : public List {
0394 public:
0395   DeclaratorList() : List(NodeKind::DeclaratorList) {}
0396   static bool classof(const Node *N);
0397   std::vector<SimpleDeclarator *> getDeclarators();
0398   std::vector<List::ElementAndDelimiter<syntax::SimpleDeclarator>>
0399   getDeclaratorsAndCommas();
0400 };
0401 
0402 /// Groups multiple declarators (e.g. variables, typedefs, etc.) together. All
0403 /// grouped declarators share the same declaration specifiers (e.g. 'int' or
0404 /// 'typedef').
0405 class SimpleDeclaration final : public Declaration {
0406 public:
0407   SimpleDeclaration() : Declaration(NodeKind::SimpleDeclaration) {}
0408   static bool classof(const Node *N);
0409   /// FIXME: use custom iterator instead of 'vector'.
0410   std::vector<SimpleDeclarator *> getDeclarators();
0411 };
0412 
0413 /// template <template-parameters> <declaration>
0414 class TemplateDeclaration final : public Declaration {
0415 public:
0416   TemplateDeclaration() : Declaration(NodeKind::TemplateDeclaration) {}
0417   static bool classof(const Node *N);
0418   Leaf *getTemplateKeyword();
0419   Declaration *getDeclaration();
0420 };
0421 
0422 /// template <declaration>
0423 /// Examples:
0424 ///     template struct X<int>
0425 ///     template void foo<int>()
0426 ///     template int var<double>
0427 class ExplicitTemplateInstantiation final : public Declaration {
0428 public:
0429   ExplicitTemplateInstantiation()
0430       : Declaration(NodeKind::ExplicitTemplateInstantiation) {}
0431   static bool classof(const Node *N);
0432   Leaf *getTemplateKeyword();
0433   Leaf *getExternKeyword();
0434   Declaration *getDeclaration();
0435 };
0436 
0437 /// namespace <name> { <decls> }
0438 class NamespaceDefinition final : public Declaration {
0439 public:
0440   NamespaceDefinition() : Declaration(NodeKind::NamespaceDefinition) {}
0441   static bool classof(const Node *N);
0442 };
0443 
0444 /// namespace <name> = <namespace-reference>
0445 class NamespaceAliasDefinition final : public Declaration {
0446 public:
0447   NamespaceAliasDefinition()
0448       : Declaration(NodeKind::NamespaceAliasDefinition) {}
0449   static bool classof(const Node *N);
0450 };
0451 
0452 /// using namespace <name>
0453 class UsingNamespaceDirective final : public Declaration {
0454 public:
0455   UsingNamespaceDirective() : Declaration(NodeKind::UsingNamespaceDirective) {}
0456   static bool classof(const Node *N);
0457 };
0458 
0459 /// using <scope>::<name>
0460 /// using typename <scope>::<name>
0461 class UsingDeclaration final : public Declaration {
0462 public:
0463   UsingDeclaration() : Declaration(NodeKind::UsingDeclaration) {}
0464   static bool classof(const Node *N);
0465 };
0466 
0467 /// using <name> = <type>
0468 class TypeAliasDeclaration final : public Declaration {
0469 public:
0470   TypeAliasDeclaration() : Declaration(NodeKind::TypeAliasDeclaration) {}
0471   static bool classof(const Node *N);
0472 };
0473 
0474 /// Covers a name, an initializer and a part of the type outside declaration
0475 /// specifiers. Examples are:
0476 ///     `*a` in `int *a`
0477 ///     `a[10]` in `int a[10]`
0478 ///     `*a = nullptr` in `int *a = nullptr`
0479 /// Declarators can be unnamed too:
0480 ///     `**` in `new int**`
0481 ///     `* = nullptr` in `void foo(int* = nullptr)`
0482 /// Most declarators you encounter are instances of SimpleDeclarator. They may
0483 /// contain an inner declarator inside parentheses, we represent it as
0484 /// ParenDeclarator. E.g.
0485 ///     `(*a)` in `int (*a) = 10`
0486 class Declarator : public Tree {
0487 public:
0488   Declarator(NodeKind K) : Tree(K) {}
0489   static bool classof(const Node *N);
0490 };
0491 
0492 /// A top-level declarator without parentheses. See comment of Declarator for
0493 /// more details.
0494 class SimpleDeclarator final : public Declarator {
0495 public:
0496   SimpleDeclarator() : Declarator(NodeKind::SimpleDeclarator) {}
0497   static bool classof(const Node *N);
0498 };
0499 
0500 /// Declarator inside parentheses.
0501 /// E.g. `(***a)` from `int (***a) = nullptr;`
0502 /// See comment of Declarator for more details.
0503 class ParenDeclarator final : public Declarator {
0504 public:
0505   ParenDeclarator() : Declarator(NodeKind::ParenDeclarator) {}
0506   static bool classof(const Node *N);
0507   Leaf *getLparen();
0508   Leaf *getRparen();
0509 };
0510 
0511 /// Array size specified inside a declarator.
0512 /// E.g:
0513 ///   `[10]` in `int a[10];`
0514 ///   `[static 10]` in `void f(int xs[static 10]);`
0515 class ArraySubscript final : public Tree {
0516 public:
0517   ArraySubscript() : Tree(NodeKind::ArraySubscript) {}
0518   static bool classof(const Node *N);
0519   // TODO: add an accessor for the "static" keyword.
0520   Leaf *getLbracket();
0521   Expression *getSize();
0522   Leaf *getRbracket();
0523 };
0524 
0525 /// Trailing return type after the parameter list, including the arrow token.
0526 /// E.g. `-> int***`.
0527 class TrailingReturnType final : public Tree {
0528 public:
0529   TrailingReturnType() : Tree(NodeKind::TrailingReturnType) {}
0530   static bool classof(const Node *N);
0531   // TODO: add accessors for specifiers.
0532   Leaf *getArrowToken();
0533   // FIXME: This should be a `type-id` following the grammar. Fix this once we
0534   // have a representation of `type-id`s.
0535   SimpleDeclarator *getDeclarator();
0536 };
0537 
0538 /// Models a `parameter-declaration-list` which appears within
0539 /// `parameters-and-qualifiers`. See C++ [dcl.fct]
0540 class ParameterDeclarationList final : public List {
0541 public:
0542   ParameterDeclarationList() : List(NodeKind::ParameterDeclarationList) {}
0543   static bool classof(const Node *N);
0544   std::vector<SimpleDeclaration *> getParameterDeclarations();
0545   std::vector<List::ElementAndDelimiter<syntax::SimpleDeclaration>>
0546   getParametersAndCommas();
0547 };
0548 
0549 /// Parameter list for a function type and a trailing return type, if the
0550 /// function has one.
0551 /// E.g.:
0552 ///  `(int a) volatile ` in `int foo(int a) volatile;`
0553 ///  `(int a) &&` in `int foo(int a) &&;`
0554 ///  `() -> int` in `auto foo() -> int;`
0555 ///  `() const` in `int foo() const;`
0556 ///  `() noexcept` in `int foo() noexcept;`
0557 ///  `() throw()` in `int foo() throw();`
0558 ///
0559 /// (!) override doesn't belong here.
0560 class ParametersAndQualifiers final : public Tree {
0561 public:
0562   ParametersAndQualifiers() : Tree(NodeKind::ParametersAndQualifiers) {}
0563   static bool classof(const Node *N);
0564   Leaf *getLparen();
0565   ParameterDeclarationList *getParameters();
0566   Leaf *getRparen();
0567   TrailingReturnType *getTrailingReturn();
0568 };
0569 
0570 /// Member pointer inside a declarator
0571 /// E.g. `X::*` in `int X::* a = 0;`
0572 class MemberPointer final : public Tree {
0573 public:
0574   MemberPointer() : Tree(NodeKind::MemberPointer) {}
0575   static bool classof(const Node *N);
0576 };
0577 
0578 #define CONCRETE_NODE(Kind, Base)                                              \
0579   inline bool Kind::classof(const Node *N) {                                   \
0580     return N->getKind() == NodeKind::Kind;                                     \
0581   }
0582 #define ABSTRACT_NODE(Kind, Base, First, Last)                                 \
0583   inline bool Kind::classof(const Node *N) {                                   \
0584     return N->getKind() >= NodeKind::First && N->getKind() <= NodeKind::Last;  \
0585   }
0586 #include "clang/Tooling/Syntax/Nodes.inc"
0587 
0588 } // namespace syntax
0589 } // namespace clang
0590 #endif