Hi klimek,

Added DynNodeType as a standalone way to represent node types and their 
hierarchy.
This change is to support ongoing work on D815.

http://llvm-reviews.chandlerc.com/D829

Files:
  include/clang/AST/ASTFwd.h
  include/clang/AST/ASTTypeTraits.h
  lib/AST/ASTTypeTraits.cpp
  lib/AST/CMakeLists.txt
  unittests/AST/ASTTypeTraitsTest.cpp
  unittests/AST/CMakeLists.txt
Index: include/clang/AST/ASTFwd.h
===================================================================
--- /dev/null
+++ include/clang/AST/ASTFwd.h
@@ -0,0 +1,28 @@
+//===--- ASTFwd.h ----------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--------------------------------------------------------------===//
+///
+/// \file
+/// \brief Forward declaration of all AST node types.
+///
+//===-------------------------------------------------------------===//
+
+
+namespace clang {
+
+class Decl;
+#define DECL(DERIVED, BASE) class DERIVED##Decl;
+#include "clang/AST/DeclNodes.inc"
+class Stmt;
+#define STMT(DERIVED, BASE) class DERIVED;
+#include "clang/AST/StmtNodes.inc"
+class Type;
+#define TYPE(DERIVED, BASE) class DERIVED##Type;
+#include "clang/AST/TypeNodes.def"
+
+} // end namespace clang
Index: include/clang/AST/ASTTypeTraits.h
===================================================================
--- include/clang/AST/ASTTypeTraits.h
+++ include/clang/AST/ASTTypeTraits.h
@@ -7,22 +7,131 @@
 //
 //===----------------------------------------------------------------------===//
 //
-//  Provides a dynamically typed node container that can be used to store
-//  an AST base node at runtime in the same storage in a type safe way.
+//  Provides a dynamic type identifier and a dynamically typed node container
+//  that can be used to store an AST base node at runtime in the same storage in
+//  a type safe way.
 //
 //===----------------------------------------------------------------------===//
 
 #ifndef LLVM_CLANG_AST_AST_TYPE_TRAITS_H
 #define LLVM_CLANG_AST_AST_TYPE_TRAITS_H
 
+#include "clang/AST/ASTFwd.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/Stmt.h"
 #include "clang/AST/TypeLoc.h"
+#include "clang/Basic/LLVM.h"
 #include "llvm/Support/AlignOf.h"
 
 namespace clang {
 namespace ast_type_traits {
 
+/// \brief Dynamic type identifier.
+///
+/// It can be constructed from any node type and allows for runtime type
+/// hierarchy checks.
+/// Use getFromNodeType<T>() to construct them. There are also shortcut
+/// functions defined for each base type.
+class DynNodeType {
+public:
+  /// \brief Empty identifier. It matches nothing.
+  DynNodeType() : TypeId(NTI_None) {}
+
+  /// \brief Some shortcuts for base types.
+  static DynNodeType decl() { return DynNodeType(NTI_Decl); }
+  static DynNodeType stmt() { return DynNodeType(NTI_Stmt); }
+  static DynNodeType nestedNameSpecifier() {
+    return DynNodeType(NTI_NestedNameSpecifier);
+  }
+  static DynNodeType nestedNameSpecifierLoc() {
+    return DynNodeType(NTI_NestedNameSpecifierLoc);
+  }
+  static DynNodeType qualType() { return DynNodeType(NTI_QualType); }
+  static DynNodeType type() { return DynNodeType(NTI_Type); }
+  static DynNodeType typeLoc() { return DynNodeType(NTI_TypeLoc); }
+
+  /// \brief Construct an identifier for T.
+  template <class T>
+  static DynNodeType getFromNodeType() {
+    return DynNodeType(TypeToTypeId<T>::Id);
+  }
+
+  /// \brief Returns \c true if \c this and \c Other represent the same type.
+  bool isSame(DynNodeType Other);
+
+  /// \brief Returns \c true if \c this is a base type of (or same as) \c Other
+  bool isBaseOf(DynNodeType Other);
+
+  /// \brief A string representation of the type.
+  StringRef asStringRef() const;
+
+private:
+  /// \brief Type ids.
+  ///
+  /// Includes all possible base and derived types.
+  enum NodeTypeId {
+    NTI_None,
+    NTI_NestedNameSpecifier,
+    NTI_NestedNameSpecifierLoc,
+    NTI_QualType,
+    NTI_TypeLoc,
+    NTI_Decl,
+#define DECL(DERIVED, BASE) NTI_##DERIVED##Decl,
+#include "clang/AST/DeclNodes.inc"
+    NTI_Stmt,
+#define STMT(DERIVED, BASE) NTI_##DERIVED,
+#include "clang/AST/StmtNodes.inc"
+    NTI_Type,
+#define TYPE(DERIVED, BASE) NTI_##DERIVED##Type,
+#include "clang/AST/TypeNodes.def"
+    NTI_NumberOfTypes
+  };
+
+  /// \brief Use getFromNodeType<T>() to construct the type.
+  DynNodeType(NodeTypeId TypeId) : TypeId(TypeId) {}
+
+  /// \brief Returns \c true if \c Base is a base type of (or same as) \c
+  ///   Derived
+  static bool isBaseOf(NodeTypeId Base, NodeTypeId Derived);
+
+  /// \brief Helper meta-function to convert a type T to its enum value.
+  ///
+  /// This struct is specialized below for all known types.
+  template <class T> struct TypeToTypeId {
+    static const NodeTypeId Id = NTI_None;
+  };
+
+  /// \brief Per type info.
+  struct TypeInfo {
+    /// \brief The id of the parent type, or None if it has no parent.
+    NodeTypeId ParentId;
+    /// \brief Name of the type.
+    const char *Name;
+  };
+  static const TypeInfo AllTypeInfo[NTI_NumberOfTypes];
+
+  NodeTypeId TypeId;
+};
+
+#define TYPE_TO_TYPE_ID(Class)                                                 \
+  template <> struct DynNodeType::TypeToTypeId<Class> {                        \
+    static const NodeTypeId Id = NTI_##Class;                                  \
+  };
+TYPE_TO_TYPE_ID(NestedNameSpecifier)
+TYPE_TO_TYPE_ID(NestedNameSpecifierLoc)
+TYPE_TO_TYPE_ID(QualType)
+TYPE_TO_TYPE_ID(TypeLoc)
+TYPE_TO_TYPE_ID(Decl)
+TYPE_TO_TYPE_ID(Stmt)
+TYPE_TO_TYPE_ID(Type)
+#define DECL(DERIVED, BASE) TYPE_TO_TYPE_ID(DERIVED##Decl)
+#include "clang/AST/DeclNodes.inc"
+#define STMT(DERIVED, BASE) TYPE_TO_TYPE_ID(DERIVED)
+#include "clang/AST/StmtNodes.inc"
+#define TYPE(DERIVED, BASE) TYPE_TO_TYPE_ID(DERIVED##Type)
+#include "clang/AST/TypeNodes.def"
+#undef TYPE_TO_TYPE_ID
+
 /// \brief A dynamically typed AST node container.
 ///
 /// Stores an AST node in a type safe way. This allows writing code that
@@ -57,7 +166,7 @@
   /// use the pointer outside the scope of the DynTypedNode.
   template <typename T>
   const T *get() const {
-    return BaseConverter<T>::get(Tag, Storage.buffer);
+    return BaseConverter<T>::get(NodeType, Storage.buffer);
   }
 
   /// \brief Returns a pointer that identifies the stored AST node.
@@ -71,120 +180,112 @@
   /// \brief Takes care of converting from and to \c T.
   template <typename T, typename EnablerT = void> struct BaseConverter;
 
-  /// \brief Supported base node types.
-  enum NodeTypeTag {
-    NT_Decl,
-    NT_Stmt,
-    NT_NestedNameSpecifier,
-    NT_NestedNameSpecifierLoc,
-    NT_QualType,
-    NT_Type,
-    NT_TypeLoc
-  } Tag;
+  DynNodeType NodeType;
 
   /// \brief Stores the data of the node.
   ///
   /// Note that we can store \c Decls and \c Stmts by pointer as they are
   /// guaranteed to be unique pointers pointing to dedicated storage in the
   /// AST. \c QualTypes on the other hand do not have storage or unique
   /// pointers and thus need to be stored by value.
-  llvm::AlignedCharArrayUnion<Decl *, Stmt *, NestedNameSpecifier,
-                              NestedNameSpecifierLoc, QualType, Type,
+  llvm::AlignedCharArrayUnion<Decl *, Stmt *, NestedNameSpecifier *,
+                              NestedNameSpecifierLoc *, QualType, Type *,
                               TypeLoc> Storage;
 };
 
 // FIXME: Pull out abstraction for the following.
 template<typename T> struct DynTypedNode::BaseConverter<T,
     typename llvm::enable_if<llvm::is_base_of<Decl, T> >::type> {
-  static const T *get(NodeTypeTag Tag, const char Storage[]) {
-    if (Tag == NT_Decl)
+  static const T *get(DynNodeType NodeType, const char Storage[]) {
+    if (DynNodeType::decl().isBaseOf(NodeType))
       return dyn_cast<T>(*reinterpret_cast<Decl*const*>(Storage));
     return NULL;
   }
   static DynTypedNode create(const Decl &Node) {
     DynTypedNode Result;
-    Result.Tag = NT_Decl;
+    Result.NodeType = DynNodeType::getFromNodeType<T>();
     new (Result.Storage.buffer) const Decl*(&Node);
     return Result;
   }
 };
 template<typename T> struct DynTypedNode::BaseConverter<T,
     typename llvm::enable_if<llvm::is_base_of<Stmt, T> >::type> {
-  static const T *get(NodeTypeTag Tag, const char Storage[]) {
-    if (Tag == NT_Stmt)
+  static const T *get(DynNodeType NodeType, const char Storage[]) {
+    if (DynNodeType::stmt().isBaseOf(NodeType))
       return dyn_cast<T>(*reinterpret_cast<Stmt*const*>(Storage));
     return NULL;
   }
   static DynTypedNode create(const Stmt &Node) {
     DynTypedNode Result;
-    Result.Tag = NT_Stmt;
+    Result.NodeType = DynNodeType::getFromNodeType<T>();
     new (Result.Storage.buffer) const Stmt*(&Node);
     return Result;
   }
 };
 template<typename T> struct DynTypedNode::BaseConverter<T,
     typename llvm::enable_if<llvm::is_base_of<Type, T> >::type> {
-  static const T *get(NodeTypeTag Tag, const char Storage[]) {
-    if (Tag == NT_Type)
+  static const T *get(DynNodeType NodeType, const char Storage[]) {
+    if (DynNodeType::type().isBaseOf(NodeType))
       return dyn_cast<T>(*reinterpret_cast<Type*const*>(Storage));
     return NULL;
   }
   static DynTypedNode create(const Type &Node) {
     DynTypedNode Result;
-    Result.Tag = NT_Type;
+    Result.NodeType = DynNodeType::getFromNodeType<T>();
     new (Result.Storage.buffer) const Type*(&Node);
     return Result;
   }
 };
 template<> struct DynTypedNode::BaseConverter<NestedNameSpecifier, void> {
-  static const NestedNameSpecifier *get(NodeTypeTag Tag, const char Storage[]) {
-    if (Tag == NT_NestedNameSpecifier)
+  static const NestedNameSpecifier *get(DynNodeType NodeType,
+                                        const char Storage[]) {
+    if (DynNodeType::nestedNameSpecifier().isBaseOf(NodeType))
       return *reinterpret_cast<NestedNameSpecifier*const*>(Storage);
     return NULL;
   }
   static DynTypedNode create(const NestedNameSpecifier &Node) {
     DynTypedNode Result;
-    Result.Tag = NT_NestedNameSpecifier;
+    Result.NodeType = DynNodeType::getFromNodeType<NestedNameSpecifier>();
     new (Result.Storage.buffer) const NestedNameSpecifier*(&Node);
     return Result;
   }
 };
 template<> struct DynTypedNode::BaseConverter<NestedNameSpecifierLoc, void> {
-  static const NestedNameSpecifierLoc *get(NodeTypeTag Tag,
+  static const NestedNameSpecifierLoc *get(DynNodeType NodeType,
                                            const char Storage[]) {
-    if (Tag == NT_NestedNameSpecifierLoc)
+    if (DynNodeType::nestedNameSpecifierLoc().isBaseOf(NodeType))
       return reinterpret_cast<const NestedNameSpecifierLoc*>(Storage);
     return NULL;
   }
   static DynTypedNode create(const NestedNameSpecifierLoc &Node) {
     DynTypedNode Result;
-    Result.Tag = NT_NestedNameSpecifierLoc;
+    Result.NodeType = DynNodeType::getFromNodeType<NestedNameSpecifierLoc>();
     new (Result.Storage.buffer) NestedNameSpecifierLoc(Node);
     return Result;
   }
 };
 template<> struct DynTypedNode::BaseConverter<QualType, void> {
-  static const QualType *get(NodeTypeTag Tag, const char Storage[]) {
-    if (Tag == NT_QualType)
+  static const QualType *get(DynNodeType NodeType, const char Storage[]) {
+    if (DynNodeType::qualType().isBaseOf(NodeType))
       return reinterpret_cast<const QualType*>(Storage);
     return NULL;
   }
   static DynTypedNode create(const QualType &Node) {
     DynTypedNode Result;
-    Result.Tag = NT_QualType;
+    Result.NodeType = DynNodeType::getFromNodeType<QualType>();
     new (Result.Storage.buffer) QualType(Node);
     return Result;
   }
 };
 template<> struct DynTypedNode::BaseConverter<TypeLoc, void> {
-  static const TypeLoc *get(NodeTypeTag Tag, const char Storage[]) {
-    if (Tag == NT_TypeLoc)
+  static const TypeLoc *get(DynNodeType NodeType, const char Storage[]) {
+    if (DynNodeType::typeLoc().isBaseOf(NodeType))
       return reinterpret_cast<const TypeLoc*>(Storage);
     return NULL;
   }
   static DynTypedNode create(const TypeLoc &Node) {
     DynTypedNode Result;
-    Result.Tag = NT_TypeLoc;
+    Result.NodeType = DynNodeType::getFromNodeType<TypeLoc>();
     new (Result.Storage.buffer) TypeLoc(Node);
     return Result;
   }
@@ -194,15 +295,18 @@
 // AST node that is not supported, but prevents misuse - a user cannot create
 // a DynTypedNode from arbitrary types.
 template <typename T, typename EnablerT> struct DynTypedNode::BaseConverter {
-  static const T *get(NodeTypeTag Tag, const char Storage[]) { return NULL; }
+  static const T *get(DynNodeType NodeType, const char Storage[]) {
+    return NULL;
+  }
 };
 
 inline const void *DynTypedNode::getMemoizationData() const {
-  switch (Tag) {
-    case NT_Decl: return BaseConverter<Decl>::get(Tag, Storage.buffer);
-    case NT_Stmt: return BaseConverter<Stmt>::get(Tag, Storage.buffer);
-    default: return NULL;
-  };
+  if (DynNodeType::decl().isBaseOf(NodeType)) {
+    return BaseConverter<Decl>::get(NodeType, Storage.buffer);
+  } else if (DynNodeType::stmt().isBaseOf(NodeType)) {
+    return BaseConverter<Stmt>::get(NodeType, Storage.buffer);
+  }
+  return NULL;
 }
 
 } // end namespace ast_type_traits
Index: lib/AST/ASTTypeTraits.cpp
===================================================================
--- /dev/null
+++ lib/AST/ASTTypeTraits.cpp
@@ -0,0 +1,56 @@
+//===--- ASTTypeTraits.cpp --------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  Provides a dynamic type identifier and a dynamically typed node container
+//  that can be used to store an AST base node at runtime in the same storage in
+//  a type safe way.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/ASTTypeTraits.h"
+
+namespace clang {
+namespace ast_type_traits {
+
+const DynNodeType::TypeInfo DynNodeType::AllTypeInfo[] = {
+  { NTI_None, "<None>" },
+  { NTI_None, "NestedNameSpecifier" },
+  { NTI_None, "NestedNameSpecifierLoc" },
+  { NTI_None, "QualType" },
+  { NTI_None, "TypeLoc" },
+  { NTI_None, "Decl" },
+#define DECL(DERIVED, BASE) { NTI_##BASE, #DERIVED "Decl" },
+#include "clang/AST/DeclNodes.inc"
+  { NTI_None, "Stmt" },
+#define STMT(DERIVED, BASE) { NTI_##BASE, #DERIVED },
+#include "clang/AST/StmtNodes.inc"
+  { NTI_None, "Type" },
+#define TYPE(DERIVED, BASE) { NTI_##BASE, #DERIVED "Type" },
+#include "clang/AST/TypeNodes.def"
+};
+
+bool DynNodeType::isBaseOf(DynNodeType Other) {
+  return isBaseOf(TypeId, Other.TypeId);
+}
+
+bool DynNodeType::isSame(DynNodeType Other) {
+  return TypeId != NTI_None && TypeId == Other.TypeId;
+}
+
+bool DynNodeType::isBaseOf(NodeTypeId Base, NodeTypeId Derived) {
+  if (Base == NTI_None || Derived == NTI_None) return false;
+  while (Derived != Base && Derived != NTI_None)
+    Derived = AllTypeInfo[Derived].ParentId;
+  return Derived == Base;
+}
+
+StringRef DynNodeType::asStringRef() const { return AllTypeInfo[TypeId].Name; }
+
+} // end namespace ast_type_traits
+} // end namespace clang
Index: lib/AST/CMakeLists.txt
===================================================================
--- lib/AST/CMakeLists.txt
+++ lib/AST/CMakeLists.txt
@@ -7,6 +7,7 @@
   ASTDiagnostic.cpp
   ASTDumper.cpp
   ASTImporter.cpp
+  ASTTypeTraits.cpp
   AttrImpl.cpp
   CXXInheritance.cpp
   Comment.cpp
Index: unittests/AST/ASTTypeTraitsTest.cpp
===================================================================
--- /dev/null
+++ unittests/AST/ASTTypeTraitsTest.cpp
@@ -0,0 +1,62 @@
+//===- unittest/AST/ASTTypeTraits.cpp - AST type traits unit tests ------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--------------------------------------------------------------------===//
+
+
+#include "clang/AST/ASTTypeTraits.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace ast_type_traits {
+
+TEST(DynNodeType, NoType) {
+  EXPECT_FALSE(DynNodeType().isBaseOf(DynNodeType()));
+  EXPECT_FALSE(DynNodeType().isSame(DynNodeType()));
+}
+
+template <typename T> static DynNodeType DNT() {
+  return DynNodeType::getFromNodeType<T>();
+}
+
+TEST(DynNodeType, Bases) {
+  EXPECT_TRUE(DynNodeType::decl().isBaseOf(DNT<VarDecl>()));
+  EXPECT_FALSE(DynNodeType::decl().isSame(DNT<VarDecl>()));
+  EXPECT_FALSE(DNT<VarDecl>().isBaseOf(DynNodeType::decl()));
+
+  EXPECT_TRUE(DynNodeType::decl().isSame(DNT<Decl>()));
+}
+
+TEST(DynNodeType, SameBase) {
+  EXPECT_TRUE(DNT<Expr>().isBaseOf(DNT<CallExpr>()));
+  EXPECT_TRUE(DNT<Expr>().isBaseOf(DNT<BinaryOperator>()));
+  EXPECT_FALSE(DNT<CallExpr>().isBaseOf(DNT<BinaryOperator>()));
+  EXPECT_FALSE(DNT<BinaryOperator>().isBaseOf(DNT<CallExpr>()));
+}
+
+TEST(DynNodeType, DiffBase) {
+  EXPECT_FALSE(DNT<Expr>().isBaseOf(DNT<ArrayType>()));
+  EXPECT_FALSE(DNT<QualType>().isBaseOf(DNT<FunctionDecl>()));
+  EXPECT_FALSE(DNT<Type>().isSame(DNT<QualType>()));
+}
+
+struct Foo {};
+
+TEST(DynNodeType, UnknownType) {
+  // We can construct one, but it is nowhere in the hierarchy.
+  EXPECT_FALSE(DNT<Foo>().isSame(DNT<Foo>()));
+}
+
+TEST(DynNodeType, Name) {
+  EXPECT_EQ("Decl", DNT<Decl>().asStringRef());
+  EXPECT_EQ("CallExpr", DNT<CallExpr>().asStringRef());
+  EXPECT_EQ("ConstantArrayType", DNT<ConstantArrayType>().asStringRef());
+  EXPECT_EQ("<None>", DynNodeType().asStringRef());
+}
+
+}  // namespace ast_type_traits
+}  // namespace clang
Index: unittests/AST/CMakeLists.txt
===================================================================
--- unittests/AST/CMakeLists.txt
+++ unittests/AST/CMakeLists.txt
@@ -1,5 +1,6 @@
 add_clang_unittest(ASTTests
   ASTContextParentMapTest.cpp
+  ASTTypeTraitsTest.cpp
   CommentLexer.cpp
   CommentParser.cpp
   DeclPrinterTest.cpp
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to