clang  10.0.0svn
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 #ifndef LLVM_CLANG_TOOLING_SYNTAX_NODES_H
11 #define LLVM_CLANG_TOOLING_SYNTAX_NODES_H
12 
13 #include "clang/Basic/TokenKinds.h"
14 #include "clang/Lex/Token.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 namespace clang {
22 namespace syntax {
23 
24 /// A kind of a syntax node, used for implementing casts.
25 enum class NodeKind : uint16_t {
26  Leaf,
30 };
31 /// For debugging purposes.
32 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, NodeKind K);
33 
34 /// A relation between a parent and child node. Used for implementing accessors.
35 enum class NodeRole : uint8_t {
36  // A node without a parent.
37  Detached,
38  // Children of an unknown semantic nature, e.g. skipped tokens, comments.
39  Unknown,
40  // FIXME: should this be shared for all other nodes with braces, e.g. init
41  // lists?
44 };
45 
46 /// A root node for a translation unit. Parent is always null.
47 class TranslationUnit final : public Tree {
48 public:
50  static bool classof(const Node *N) {
51  return N->kind() == NodeKind::TranslationUnit;
52  }
53 };
54 
55 /// FIXME: this node is temporary and will be replaced with nodes for various
56 /// 'declarations' and 'declarators' from the C/C++ grammar
57 ///
58 /// Represents any top-level declaration. Only there to give the syntax tree a
59 /// bit of structure until we implement syntax nodes for declarations and
60 /// declarators.
61 class TopLevelDeclaration final : public Tree {
62 public:
64  static bool classof(const Node *N) {
65  return N->kind() == NodeKind::TopLevelDeclaration;
66  }
67 };
68 
69 /// An abstract node for C++ statements, e.g. 'while', 'if', etc.
70 class Statement : public Tree {
71 public:
72  Statement(NodeKind K) : Tree(K) {}
73  static bool classof(const Node *N) {
74  return NodeKind::CompoundStatement <= N->kind() &&
76  }
77 };
78 
79 /// { statement1; statement2; … }
80 class CompoundStatement final : public Statement {
81 public:
82  CompoundStatement() : Statement(NodeKind::CompoundStatement) {}
83  static bool classof(const Node *N) {
84  return N->kind() == NodeKind::CompoundStatement;
85  }
86  syntax::Leaf *lbrace();
87  syntax::Leaf *rbrace();
88 };
89 
90 } // namespace syntax
91 } // namespace clang
92 #endif
static bool classof(const Node *N)
Definition: Nodes.h:73
{ statement1; statement2; … }
Definition: Nodes.h:80
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, NodeKind K)
For debugging purposes.
Definition: Nodes.cpp:13
static bool classof(const Node *N)
Definition: Nodes.h:83
A node in a syntax tree.
Definition: Tree.h:73
A root node for a translation unit. Parent is always null.
Definition: Nodes.h:47
static bool classof(const Node *N)
Definition: Nodes.h:64
FIXME: this node is temporary and will be replaced with nodes for various &#39;declarations&#39; and &#39;declara...
Definition: Nodes.h:61
An abstract node for C++ statements, e.g. &#39;while&#39;, &#39;if&#39;, etc.
Definition: Nodes.h:70
A node that has children and represents a syntactic language construct.
Definition: Tree.h:116
NodeKind
A kind of a syntax node, used for implementing casts.
Definition: Nodes.h:25
NodeRole
A relation between a parent and child node. Used for implementing accessors.
Definition: Nodes.h:35
Dataflow Directional Tag Classes.
A leaf node points to a single token inside the expanded token stream.
Definition: Tree.h:104
static bool classof(const Node *N)
Definition: Nodes.h:50
Statement(NodeKind K)
Definition: Nodes.h:72
Defines the clang::TokenKind enum and support functions.
NodeKind kind() const
Definition: Tree.h:79