clang 19.0.0git
Nodes.h
Go to the documentation of this file.
1//===- Nodes.h - syntax nodes for C/C++ grammar constructs ----*- C++ -*-=====//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8// Syntax tree nodes for C, C++ and Objective-C grammar constructs.
9//
10// Nodes provide access to their syntactic components, e.g. IfStatement provides
11// a way to get its condition, then and else branches, tokens for 'if' and
12// 'else' keywords.
13// When using the accessors, please assume they can return null. This happens
14// because:
15// - the corresponding subnode is optional in the C++ grammar, e.g. an else
16// branch of an if statement,
17// - syntactic errors occurred while parsing the corresponding subnode.
18// One notable exception is "introducer" keywords, e.g. the accessor for the
19// 'if' keyword of an if statement will never return null.
20//===----------------------------------------------------------------------===//
21#ifndef LLVM_CLANG_TOOLING_SYNTAX_NODES_H
22#define LLVM_CLANG_TOOLING_SYNTAX_NODES_H
23
24#include "clang/Basic/LLVM.h"
26namespace clang {
27namespace syntax {
28
29/// A kind of a syntax node, used for implementing casts. The ordering and
30/// blocks of enumerator constants must correspond to the inheritance hierarchy
31/// of syntax::Node.
32enum class NodeKind : uint16_t {
33#define CONCRETE_NODE(Kind, Base) Kind,
34#include "clang/Tooling/Syntax/Nodes.inc"
35};
36/// For debugging purposes.
37raw_ostream &operator<<(raw_ostream &OS, NodeKind K);
38
39/// A relation between a parent and child node, e.g. 'left-hand-side of
40/// a binary expression'. Used for implementing accessors.
41///
42/// In general `NodeRole`s should be named the same as their accessors.
43///
44/// Some roles describe parent/child relations that occur multiple times in
45/// language grammar. We define only one role to describe all instances of such
46/// recurring relations. For example, grammar for both "if" and "while"
47/// statements requires an opening paren and a closing paren. The opening
48/// paren token is assigned the OpenParen role regardless of whether it appears
49/// as a child of IfStatement or WhileStatement node. More generally, when
50/// grammar requires a certain fixed token (like a specific keyword, or an
51/// opening paren), we define a role for this token and use it across all
52/// grammar rules with the same requirement. Names of such reusable roles end
53/// with a ~Token or a ~Keyword suffix.
54enum class NodeRole : uint8_t {
55 // Roles common to multiple node kinds.
56 /// A node without a parent
58 /// Children of an unknown semantic nature, e.g. skipped tokens, comments.
59 Unknown,
60 /// An opening parenthesis in argument lists and blocks, e.g. '{', '(', etc.
62 /// A closing parenthesis in argument lists and blocks, e.g. '}', ')', etc.
64 /// A keywords that introduces some grammar construct, e.g. 'if', 'try', etc.
66 /// A token that represents a literal, e.g. 'nullptr', '1', 'true', etc.
68 /// Tokens or Keywords.
72 /// An inner statement for those that have only a single child of kind
73 /// statement, e.g. loop body for while, for, etc; inner statement for case,
74 /// default, etc.
76 /// List API roles.
79
80 // Roles specific to particular node kinds.
82 Operand,
93 Message,
96 Size,
100 Qualifier,
102 Object,
104 Member,
105 Callee,
106 Arguments,
108};
109/// For debugging purposes.
110raw_ostream &operator<<(raw_ostream &OS, NodeRole R);
111
112#include "clang/Tooling/Syntax/NodeClasses.inc"
113
114/// Models a `nested-name-specifier`. C++ [expr.prim.id.qual]
115/// e.g. the `std::vector<int>::` in `std::vector<int>::size`.
116class NestedNameSpecifier final : public List {
117public:
119 static bool classof(const Node *N);
120 std::vector<NameSpecifier *> getSpecifiers();
121 std::vector<List::ElementAndDelimiter<syntax::NameSpecifier>>
123};
124
125/// Models an `unqualified-id`. C++ [expr.prim.id.unqual]
126/// e.g. the `size` in `std::vector<int>::size`.
127class UnqualifiedId final : public Tree {
128public:
130 static bool classof(const Node *N);
131};
132
133/// An expression of an unknown kind, i.e. one not currently handled by the
134/// syntax tree.
135class UnknownExpression final : public Expression {
136public:
138 static bool classof(const Node *N);
139};
140
141/// Models arguments of a function call.
142/// call-arguments:
143/// delimited_list(expression, ',')
144/// Note: This construct is a simplification of the grammar rule for
145/// `expression-list`, that is used in the definition of `call-expression`
146class CallArguments final : public List {
147public:
149 static bool classof(const Node *N);
150 std::vector<Expression *> getArguments();
151 std::vector<List::ElementAndDelimiter<Expression>> getArgumentsAndCommas();
152};
153
154/// An abstract class for prefix and postfix unary operators.
156public:
158 static bool classof(const Node *N);
161};
162
163/// <operator> <operand>
164///
165/// For example:
166/// +a -b
167/// !c not c
168/// ~d compl d
169/// *e &f
170/// ++h --h
171/// __real i __imag i
173public:
176 static bool classof(const Node *N);
177};
178
179/// <operand> <operator>
180///
181/// For example:
182/// a++
183/// b--
185public:
188 static bool classof(const Node *N);
189};
190
191/// <lhs> <operator> <rhs>
192///
193/// For example:
194/// a + b
195/// a bitor 1
196/// a |= b
197/// a and_eq b
199public:
201 static bool classof(const Node *N);
205};
206
207/// An abstract node for C++ statements, e.g. 'while', 'if', etc.
208/// FIXME: add accessors for semicolon of statements that have it.
209class Statement : public Tree {
210public:
212 static bool classof(const Node *N);
213};
214
215/// A statement of an unknown kind, i.e. one not currently handled by the syntax
216/// tree.
217class UnknownStatement final : public Statement {
218public:
220 static bool classof(const Node *N);
221};
222
223/// E.g. 'int a, b = 10;'
224class DeclarationStatement final : public Statement {
225public:
227 static bool classof(const Node *N);
228};
229
230/// The no-op statement, i.e. ';'.
231class EmptyStatement final : public Statement {
232public:
234 static bool classof(const Node *N);
235};
236
237/// switch (<cond>) <body>
238class SwitchStatement final : public Statement {
239public:
241 static bool classof(const Node *N);
244};
245
246/// case <value>: <body>
247class CaseStatement final : public Statement {
248public:
250 static bool classof(const Node *N);
254};
255
256/// default: <body>
257class DefaultStatement final : public Statement {
258public:
260 static bool classof(const Node *N);
263};
264
265/// if (cond) <then-statement> else <else-statement>
266/// FIXME: add condition that models 'expression or variable declaration'
267class IfStatement final : public Statement {
268public:
270 static bool classof(const Node *N);
275};
276
277/// for (<init>; <cond>; <increment>) <body>
278class ForStatement final : public Statement {
279public:
281 static bool classof(const Node *N);
284};
285
286/// while (<cond>) <body>
287class WhileStatement final : public Statement {
288public:
290 static bool classof(const Node *N);
293};
294
295/// continue;
296class ContinueStatement final : public Statement {
297public:
299 static bool classof(const Node *N);
301};
302
303/// break;
304class BreakStatement final : public Statement {
305public:
307 static bool classof(const Node *N);
309};
310
311/// return <expr>;
312/// return;
313class ReturnStatement final : public Statement {
314public:
316 static bool classof(const Node *N);
319};
320
321/// for (<decl> : <init>) <body>
322class RangeBasedForStatement final : public Statement {
323public:
325 static bool classof(const Node *N);
328};
329
330/// Expression in a statement position, e.g. functions calls inside compound
331/// statements or inside a loop body.
332class ExpressionStatement final : public Statement {
333public:
335 static bool classof(const Node *N);
337};
338
339/// { statement1; statement2; … }
340class CompoundStatement final : public Statement {
341public:
343 static bool classof(const Node *N);
344 Leaf *getLbrace();
345 /// FIXME: use custom iterator instead of 'vector'.
346 std::vector<Statement *> getStatements();
347 Leaf *getRbrace();
348};
349
350/// A declaration that can appear at the top-level. Note that this does *not*
351/// correspond 1-to-1 to clang::Decl. Syntax trees distinguish between top-level
352/// declarations (e.g. namespace definitions) and declarators (e.g. variables,
353/// typedefs, etc.). Declarators are stored inside SimpleDeclaration.
354class Declaration : public Tree {
355public:
357 static bool classof(const Node *N);
358};
359
360/// Declaration of an unknown kind, e.g. not yet supported in syntax trees.
361class UnknownDeclaration final : public Declaration {
362public:
364 static bool classof(const Node *N);
365};
366
367/// A semicolon in the top-level context. Does not declare anything.
368class EmptyDeclaration final : public Declaration {
369public:
371 static bool classof(const Node *N);
372};
373
374/// static_assert(<condition>, <message>)
375/// static_assert(<condition>)
377public:
379 static bool classof(const Node *N);
382};
383
384/// extern <string-literal> declaration
385/// extern <string-literal> { <decls> }
387public:
390 static bool classof(const Node *N);
391};
392
393class DeclaratorList final : public List {
394public:
396 static bool classof(const Node *N);
397 std::vector<SimpleDeclarator *> getDeclarators();
398 std::vector<List::ElementAndDelimiter<syntax::SimpleDeclarator>>
400};
401
402/// Groups multiple declarators (e.g. variables, typedefs, etc.) together. All
403/// grouped declarators share the same declaration specifiers (e.g. 'int' or
404/// 'typedef').
405class SimpleDeclaration final : public Declaration {
406public:
408 static bool classof(const Node *N);
409 /// FIXME: use custom iterator instead of 'vector'.
410 std::vector<SimpleDeclarator *> getDeclarators();
411};
412
413/// template <template-parameters> <declaration>
414class TemplateDeclaration final : public Declaration {
415public:
417 static bool classof(const Node *N);
420};
421
422/// template <declaration>
423/// Examples:
424/// template struct X<int>
425/// template void foo<int>()
426/// template int var<double>
428public:
431 static bool classof(const Node *N);
435};
436
437/// namespace <name> { <decls> }
438class NamespaceDefinition final : public Declaration {
439public:
441 static bool classof(const Node *N);
442};
443
444/// namespace <name> = <namespace-reference>
446public:
449 static bool classof(const Node *N);
450};
451
452/// using namespace <name>
454public:
456 static bool classof(const Node *N);
457};
458
459/// using <scope>::<name>
460/// using typename <scope>::<name>
461class UsingDeclaration final : public Declaration {
462public:
464 static bool classof(const Node *N);
465};
466
467/// using <name> = <type>
468class TypeAliasDeclaration final : public Declaration {
469public:
471 static bool classof(const Node *N);
472};
473
474/// Covers a name, an initializer and a part of the type outside declaration
475/// specifiers. Examples are:
476/// `*a` in `int *a`
477/// `a[10]` in `int a[10]`
478/// `*a = nullptr` in `int *a = nullptr`
479/// Declarators can be unnamed too:
480/// `**` in `new int**`
481/// `* = nullptr` in `void foo(int* = nullptr)`
482/// Most declarators you encounter are instances of SimpleDeclarator. They may
483/// contain an inner declarator inside parentheses, we represent it as
484/// ParenDeclarator. E.g.
485/// `(*a)` in `int (*a) = 10`
486class Declarator : public Tree {
487public:
489 static bool classof(const Node *N);
490};
491
492/// A top-level declarator without parentheses. See comment of Declarator for
493/// more details.
494class SimpleDeclarator final : public Declarator {
495public:
497 static bool classof(const Node *N);
498};
499
500/// Declarator inside parentheses.
501/// E.g. `(***a)` from `int (***a) = nullptr;`
502/// See comment of Declarator for more details.
503class ParenDeclarator final : public Declarator {
504public:
506 static bool classof(const Node *N);
507 Leaf *getLparen();
508 Leaf *getRparen();
509};
510
511/// Array size specified inside a declarator.
512/// E.g:
513/// `[10]` in `int a[10];`
514/// `[static 10]` in `void f(int xs[static 10]);`
515class ArraySubscript final : public Tree {
516public:
518 static bool classof(const Node *N);
519 // TODO: add an accessor for the "static" keyword.
520 Leaf *getLbracket();
522 Leaf *getRbracket();
523};
524
525/// Trailing return type after the parameter list, including the arrow token.
526/// E.g. `-> int***`.
527class TrailingReturnType final : public Tree {
528public:
530 static bool classof(const Node *N);
531 // TODO: add accessors for specifiers.
533 // FIXME: This should be a `type-id` following the grammar. Fix this once we
534 // have a representation of `type-id`s.
536};
537
538/// Models a `parameter-declaration-list` which appears within
539/// `parameters-and-qualifiers`. See C++ [dcl.fct]
540class ParameterDeclarationList final : public List {
541public:
543 static bool classof(const Node *N);
544 std::vector<SimpleDeclaration *> getParameterDeclarations();
545 std::vector<List::ElementAndDelimiter<syntax::SimpleDeclaration>>
547};
548
549/// Parameter list for a function type and a trailing return type, if the
550/// function has one.
551/// E.g.:
552/// `(int a) volatile ` in `int foo(int a) volatile;`
553/// `(int a) &&` in `int foo(int a) &&;`
554/// `() -> int` in `auto foo() -> int;`
555/// `() const` in `int foo() const;`
556/// `() noexcept` in `int foo() noexcept;`
557/// `() throw()` in `int foo() throw();`
558///
559/// (!) override doesn't belong here.
560class ParametersAndQualifiers final : public Tree {
561public:
563 static bool classof(const Node *N);
564 Leaf *getLparen();
566 Leaf *getRparen();
568};
569
570/// Member pointer inside a declarator
571/// E.g. `X::*` in `int X::* a = 0;`
572class MemberPointer final : public Tree {
573public:
575 static bool classof(const Node *N);
576};
577
578#define CONCRETE_NODE(Kind, Base) \
579 inline bool Kind::classof(const Node *N) { \
580 return N->getKind() == NodeKind::Kind; \
581 }
582#define ABSTRACT_NODE(Kind, Base, First, Last) \
583 inline bool Kind::classof(const Node *N) { \
584 return N->getKind() >= NodeKind::First && N->getKind() <= NodeKind::Last; \
585 }
586#include "clang/Tooling/Syntax/Nodes.inc"
587
588} // namespace syntax
589} // namespace clang
590#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Array size specified inside a declarator.
Definition: Nodes.h:515
Expression * getSize()
Definition: Nodes.cpp:402
static bool classof(const Node *N)
<lhs> <operator> <rhs>
Definition: Nodes.h:198
static bool classof(const Node *N)
static bool classof(const Node *N)
Models arguments of a function call.
Definition: Nodes.h:146
static bool classof(const Node *N)
std::vector< List::ElementAndDelimiter< Expression > > getArgumentsAndCommas()
Definition: Nodes.cpp:142
std::vector< Expression * > getArguments()
Definition: Nodes.cpp:132
Expression * getCaseValue()
Definition: Nodes.cpp:236
static bool classof(const Node *N)
{ statement1; statement2; … }
Definition: Nodes.h:340
static bool classof(const Node *N)
std::vector< Statement * > getStatements()
FIXME: use custom iterator instead of 'vector'.
Definition: Nodes.cpp:334
static bool classof(const Node *N)
E.g. 'int a, b = 10;'.
Definition: Nodes.h:224
static bool classof(const Node *N)
A declaration that can appear at the top-level.
Definition: Nodes.h:354
static bool classof(const Node *N)
Declaration(NodeKind K)
Definition: Nodes.h:356
static bool classof(const Node *N)
std::vector< SimpleDeclarator * > getDeclarators()
Definition: Nodes.cpp:177
std::vector< List::ElementAndDelimiter< syntax::SimpleDeclarator > > getDeclaratorsAndCommas()
Definition: Nodes.cpp:187
Covers a name, an initializer and a part of the type outside declaration specifiers.
Definition: Nodes.h:486
Declarator(NodeKind K)
Definition: Nodes.h:488
static bool classof(const Node *N)
static bool classof(const Node *N)
A semicolon in the top-level context. Does not declare anything.
Definition: Nodes.h:368
static bool classof(const Node *N)
The no-op statement, i.e. ';'.
Definition: Nodes.h:231
static bool classof(const Node *N)
template <declaration> Examples: template struct X<int> template void foo<int>() template int var<dou...
Definition: Nodes.h:427
Expression in a statement position, e.g.
Definition: Nodes.h:332
static bool classof(const Node *N)
for (<init>; <cond>; <increment>) <body>
Definition: Nodes.h:278
Statement * getBody()
Definition: Nodes.cpp:280
static bool classof(const Node *N)
if (cond) <then-statement> else <else-statement> FIXME: add condition that models 'expression or vari...
Definition: Nodes.h:267
static bool classof(const Node *N)
Statement * getThenStatement()
Definition: Nodes.cpp:261
Statement * getElseStatement()
Definition: Nodes.cpp:270
A leaf node points to a single token.
Definition: Tree.h:132
extern <string-literal> declaration extern <string-literal> { <decls> }
Definition: Nodes.h:386
A list of Elements separated or terminated by a fixed token.
Definition: Tree.h:254
Member pointer inside a declarator E.g.
Definition: Nodes.h:572
static bool classof(const Node *N)
namespace <name> = <namespace-reference>
Definition: Nodes.h:445
static bool classof(const Node *N)
namespace <name> { <decls> }
Definition: Nodes.h:438
static bool classof(const Node *N)
Models a nested-name-specifier.
Definition: Nodes.h:116
std::vector< NameSpecifier * > getSpecifiers()
Definition: Nodes.cpp:110
std::vector< List::ElementAndDelimiter< syntax::NameSpecifier > > getSpecifiersAndDoubleColons()
Definition: Nodes.cpp:120
static bool classof(const Node *N)
A node in a syntax tree.
Definition: Tree.h:54
Models a parameter-declaration-list which appears within parameters-and-qualifiers.
Definition: Nodes.h:540
std::vector< List::ElementAndDelimiter< syntax::SimpleDeclaration > > getParametersAndCommas()
Definition: Nodes.cpp:164
static bool classof(const Node *N)
std::vector< SimpleDeclaration * > getParameterDeclarations()
Definition: Nodes.cpp:154
Parameter list for a function type and a trailing return type, if the function has one.
Definition: Nodes.h:560
ParameterDeclarationList * getParameters()
Definition: Nodes.cpp:424
TrailingReturnType * getTrailingReturn()
Definition: Nodes.cpp:434
static bool classof(const Node *N)
Declarator inside parentheses.
Definition: Nodes.h:503
static bool classof(const Node *N)
for (<decl> : <init>) <body>
Definition: Nodes.h:322
static bool classof(const Node *N)
return <expr>; return;
Definition: Nodes.h:313
static bool classof(const Node *N)
Expression * getReturnValue()
Definition: Nodes.cpp:310
Groups multiple declarators (e.g.
Definition: Nodes.h:405
std::vector< SimpleDeclarator * > getDeclarators()
FIXME: use custom iterator instead of 'vector'.
Definition: Nodes.cpp:357
static bool classof(const Node *N)
A top-level declarator without parentheses.
Definition: Nodes.h:494
static bool classof(const Node *N)
An abstract node for C++ statements, e.g.
Definition: Nodes.h:209
Statement(NodeKind K)
Definition: Nodes.h:211
static bool classof(const Node *N)
static_assert(<condition>, <message>) static_assert(<condition>)
Definition: Nodes.h:376
static bool classof(const Node *N)
switch (<cond>) <body>
Definition: Nodes.h:238
static bool classof(const Node *N)
template <template-parameters> <declaration>
Definition: Nodes.h:414
Declaration * getDeclaration()
Definition: Nodes.cpp:371
static bool classof(const Node *N)
Trailing return type after the parameter list, including the arrow token.
Definition: Nodes.h:527
SimpleDeclarator * getDeclarator()
Definition: Nodes.cpp:414
static bool classof(const Node *N)
A node that has children and represents a syntactic language construct.
Definition: Tree.h:144
using <name> = <type>
Definition: Nodes.h:468
static bool classof(const Node *N)
An abstract class for prefix and postfix unary operators.
Definition: Nodes.h:155
static bool classof(const Node *N)
Declaration of an unknown kind, e.g. not yet supported in syntax trees.
Definition: Nodes.h:361
static bool classof(const Node *N)
An expression of an unknown kind, i.e.
Definition: Nodes.h:135
static bool classof(const Node *N)
A statement of an unknown kind, i.e.
Definition: Nodes.h:217
static bool classof(const Node *N)
Models an unqualified-id.
Definition: Nodes.h:127
static bool classof(const Node *N)
using <scope>::<name> using typename <scope>::<name>
Definition: Nodes.h:461
static bool classof(const Node *N)
using namespace <name>
Definition: Nodes.h:453
static bool classof(const Node *N)
while (<cond>) <body>
Definition: Nodes.h:287
static bool classof(const Node *N)
NodeRole
A relation between a parent and child node, e.g.
Definition: Nodes.h:54
@ ListElement
List API roles.
@ LiteralToken
A token that represents a literal, e.g. 'nullptr', '1', 'true', etc.
@ Detached
A node without a parent.
@ CloseParen
A closing parenthesis in argument lists and blocks, e.g. '}', ')', etc.
@ IntroducerKeyword
A keywords that introduces some grammar construct, e.g. 'if', 'try', etc.
@ Unknown
Children of an unknown semantic nature, e.g. skipped tokens, comments.
@ BodyStatement
An inner statement for those that have only a single child of kind statement, e.g.
@ OpenParen
An opening parenthesis in argument lists and blocks, e.g. '{', '(', etc.
@ ArrowToken
Tokens or Keywords.
NodeKind
A kind of a syntax node, used for implementing casts.
Definition: Nodes.h:32
raw_ostream & operator<<(raw_ostream &OS, NodeKind K)
For debugging purposes.
Definition: Nodes.cpp:13
The JSON file list parser is used to communicate input to InstallAPI.