1 //===--- ASTTypeTraits.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  Provides a dynamic type identifier and a dynamically typed node container
11 //  that can be used to store an AST base node at runtime in the same storage in
12 //  a type safe way.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "clang/AST/ASTTypeTraits.h"
17 
18 namespace clang {
19 namespace ast_type_traits {
20 
21 const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
22   { NKI_None, "<None>" },
23   { NKI_None, "NestedNameSpecifier" },
24   { NKI_None, "NestedNameSpecifierLoc" },
25   { NKI_None, "QualType" },
26   { NKI_None, "TypeLoc" },
27   { NKI_None, "Decl" },
28 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
29 #include "clang/AST/DeclNodes.inc"
30   { NKI_None, "Stmt" },
31 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
32 #include "clang/AST/StmtNodes.inc"
33   { NKI_None, "Type" },
34 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
35 #include "clang/AST/TypeNodes.def"
36 };
37 
38 bool ASTNodeKind::isBaseOf(ASTNodeKind Other) const {
39   return isBaseOf(KindId, Other.KindId);
40 }
41 
42 bool ASTNodeKind::isSame(ASTNodeKind Other) const {
43   return KindId != NKI_None && KindId == Other.KindId;
44 }
45 
46 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived) {
47   if (Base == NKI_None || Derived == NKI_None) return false;
48   while (Derived != Base && Derived != NKI_None)
49     Derived = AllKindInfo[Derived].ParentId;
50   return Derived == Base;
51 }
52 
53 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
54 
55 } // end namespace ast_type_traits
56 } // end namespace clang
57