summaryrefslogtreecommitdiff
path: root/include/clang/Tooling/Syntax/Nodes.h
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2019-08-20 20:50:49 +0000
committerDimitry Andric <dim@FreeBSD.org>2019-08-20 20:50:49 +0000
commit2298981669bf3bd63335a4be179bc0f96823a8f4 (patch)
tree1cbe2eb27f030d2d70b80ee5ca3c86bee7326a9f /include/clang/Tooling/Syntax/Nodes.h
parent9a83721404652cea39e9f02ae3e3b5c964602a5c (diff)
Diffstat (limited to 'include/clang/Tooling/Syntax/Nodes.h')
-rw-r--r--include/clang/Tooling/Syntax/Nodes.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/include/clang/Tooling/Syntax/Nodes.h b/include/clang/Tooling/Syntax/Nodes.h
new file mode 100644
index 000000000000..d20c7cb7b171
--- /dev/null
+++ b/include/clang/Tooling/Syntax/Nodes.h
@@ -0,0 +1,92 @@
+//===- Nodes.h - syntax nodes for C/C++ grammar constructs ----*- C++ -*-=====//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// Syntax tree nodes for C, C++ and Objective-C grammar constructs.
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_TOOLING_SYNTAX_NODES_H
+#define LLVM_CLANG_TOOLING_SYNTAX_NODES_H
+
+#include "clang/Basic/TokenKinds.h"
+#include "clang/Lex/Token.h"
+#include "clang/Tooling/Syntax/Tokens.h"
+#include "clang/Tooling/Syntax/Tree.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace clang {
+namespace syntax {
+
+/// A kind of a syntax node, used for implementing casts.
+enum class NodeKind : uint16_t {
+ Leaf,
+ TranslationUnit,
+ TopLevelDeclaration,
+ CompoundStatement
+};
+/// For debugging purposes.
+llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, NodeKind K);
+
+/// A relation between a parent and child node. Used for implementing accessors.
+enum class NodeRole : uint8_t {
+ // A node without a parent.
+ Detached,
+ // Children of an unknown semantic nature, e.g. skipped tokens, comments.
+ Unknown,
+ // FIXME: should this be shared for all other nodes with braces, e.g. init
+ // lists?
+ CompoundStatement_lbrace,
+ CompoundStatement_rbrace
+};
+
+/// A root node for a translation unit. Parent is always null.
+class TranslationUnit final : public Tree {
+public:
+ TranslationUnit() : Tree(NodeKind::TranslationUnit) {}
+ static bool classof(const Node *N) {
+ return N->kind() == NodeKind::TranslationUnit;
+ }
+};
+
+/// FIXME: this node is temporary and will be replaced with nodes for various
+/// 'declarations' and 'declarators' from the C/C++ grammar
+///
+/// Represents any top-level declaration. Only there to give the syntax tree a
+/// bit of structure until we implement syntax nodes for declarations and
+/// declarators.
+class TopLevelDeclaration final : public Tree {
+public:
+ TopLevelDeclaration() : Tree(NodeKind::TopLevelDeclaration) {}
+ static bool classof(const Node *N) {
+ return N->kind() == NodeKind::TopLevelDeclaration;
+ }
+};
+
+/// An abstract node for C++ statements, e.g. 'while', 'if', etc.
+class Statement : public Tree {
+public:
+ Statement(NodeKind K) : Tree(K) {}
+ static bool classof(const Node *N) {
+ return NodeKind::CompoundStatement <= N->kind() &&
+ N->kind() <= NodeKind::CompoundStatement;
+ }
+};
+
+/// { statement1; statement2; … }
+class CompoundStatement final : public Statement {
+public:
+ CompoundStatement() : Statement(NodeKind::CompoundStatement) {}
+ static bool classof(const Node *N) {
+ return N->kind() == NodeKind::CompoundStatement;
+ }
+ syntax::Leaf *lbrace();
+ syntax::Leaf *rbrace();
+};
+
+} // namespace syntax
+} // namespace clang
+#endif