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 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclCXX.h"
19 
20 namespace clang {
21 namespace ast_type_traits {
22 
23 const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = {
24   { NKI_None, "<None>" },
25   { NKI_None, "CXXCtorInitializer" },
26   { NKI_None, "TemplateArgument" },
27   { NKI_None, "NestedNameSpecifier" },
28   { NKI_None, "NestedNameSpecifierLoc" },
29   { NKI_None, "QualType" },
30   { NKI_None, "TypeLoc" },
31   { NKI_None, "Decl" },
32 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
33 #include "clang/AST/DeclNodes.inc"
34   { NKI_None, "Stmt" },
35 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
36 #include "clang/AST/StmtNodes.inc"
37   { NKI_None, "Type" },
38 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
39 #include "clang/AST/TypeNodes.def"
40 };
41 
42 bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
43   return isBaseOf(KindId, Other.KindId, Distance);
44 }
45 
46 bool ASTNodeKind::isSame(ASTNodeKind Other) const {
47   return KindId != NKI_None && KindId == Other.KindId;
48 }
49 
50 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
51                            unsigned *Distance) {
52   if (Base == NKI_None || Derived == NKI_None) return false;
53   unsigned Dist = 0;
54   while (Derived != Base && Derived != NKI_None) {
55     Derived = AllKindInfo[Derived].ParentId;
56     ++Dist;
57   }
58   if (Distance)
59     *Distance = Dist;
60   return Derived == Base;
61 }
62 
63 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
64 
65 ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
66   switch (D.getKind()) {
67 #define DECL(DERIVED, BASE)                                                    \
68     case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
69 #define ABSTRACT_DECL(D)
70 #include "clang/AST/DeclNodes.inc"
71   };
72   llvm_unreachable("invalid decl kind");
73 }
74 
75 ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
76   switch (S.getStmtClass()) {
77     case Stmt::NoStmtClass: return NKI_None;
78 #define STMT(CLASS, PARENT)                                                    \
79     case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
80 #define ABSTRACT_STMT(S)
81 #include "clang/AST/StmtNodes.inc"
82   }
83   llvm_unreachable("invalid stmt kind");
84 }
85 
86 ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
87   switch (T.getTypeClass()) {
88 #define TYPE(Class, Base)                                                      \
89     case Type::Class: return ASTNodeKind(NKI_##Class##Type);
90 #define ABSTRACT_TYPE(Class, Base)
91 #include "clang/AST/TypeNodes.def"
92   }
93   llvm_unreachable("invalid type kind");
94 }
95 
96 void DynTypedNode::print(llvm::raw_ostream &OS,
97                          const PrintingPolicy &PP) const {
98   if (const TemplateArgument *TA = get<TemplateArgument>())
99     TA->print(PP, OS);
100   else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
101     NNS->print(OS, PP);
102   else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
103     NNSL->getNestedNameSpecifier()->print(OS, PP);
104   else if (const QualType *QT = get<QualType>())
105     QT->print(OS, PP);
106   else if (const TypeLoc *TL = get<TypeLoc>())
107     TL->getType().print(OS, PP);
108   else if (const Decl *D = get<Decl>())
109     D->print(OS, PP);
110   else if (const Stmt *S = get<Stmt>())
111     S->printPretty(OS, nullptr, PP);
112   else if (const Type *T = get<Type>())
113     QualType(T, 0).print(OS, PP);
114   else
115     OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
116 }
117 
118 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
119   if (const Decl *D = get<Decl>())
120     D->dump(OS);
121   else if (const Stmt *S = get<Stmt>())
122     S->dump(OS, SM);
123   else
124     OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
125 }
126 
127 SourceRange DynTypedNode::getSourceRange() const {
128   if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
129     return CCI->getSourceRange();
130   if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
131     return NNSL->getSourceRange();
132   if (const TypeLoc *TL = get<TypeLoc>())
133     return TL->getSourceRange();
134   if (const Decl *D = get<Decl>())
135     return D->getSourceRange();
136   if (const Stmt *S = get<Stmt>())
137     return S->getSourceRange();
138   return SourceRange();
139 }
140 
141 } // end namespace ast_type_traits
142 } // end namespace clang
143