1 //===--- ASTTypeTraits.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #include "clang/AST/ASTTypeTraits.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclCXX.h"
21 namespace ast_type_traits {
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" },
32 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
33 #include "clang/AST/DeclNodes.inc"
35 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
36 #include "clang/AST/StmtNodes.inc"
38 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
39 #include "clang/AST/TypeNodes.def"
42 bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
43 return isBaseOf(KindId, Other.KindId, Distance);
46 bool ASTNodeKind::isSame(ASTNodeKind Other) const {
47 return KindId != NKI_None && KindId == Other.KindId;
50 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
52 if (Base == NKI_None || Derived == NKI_None) return false;
54 while (Derived != Base && Derived != NKI_None) {
55 Derived = AllKindInfo[Derived].ParentId;
60 return Derived == Base;
63 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
65 ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,
67 if (Kind1.isBaseOf(Kind2)) return Kind2;
68 if (Kind2.isBaseOf(Kind1)) return Kind1;
72 ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
74 NodeKindId Parent = Kind1.KindId;
75 while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) {
76 Parent = AllKindInfo[Parent].ParentId;
78 return ASTNodeKind(Parent);
81 ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
82 switch (D.getKind()) {
83 #define DECL(DERIVED, BASE) \
84 case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
85 #define ABSTRACT_DECL(D)
86 #include "clang/AST/DeclNodes.inc"
88 llvm_unreachable("invalid decl kind");
91 ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
92 switch (S.getStmtClass()) {
93 case Stmt::NoStmtClass: return NKI_None;
94 #define STMT(CLASS, PARENT) \
95 case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
96 #define ABSTRACT_STMT(S)
97 #include "clang/AST/StmtNodes.inc"
99 llvm_unreachable("invalid stmt kind");
102 ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
103 switch (T.getTypeClass()) {
104 #define TYPE(Class, Base) \
105 case Type::Class: return ASTNodeKind(NKI_##Class##Type);
106 #define ABSTRACT_TYPE(Class, Base)
107 #include "clang/AST/TypeNodes.def"
109 llvm_unreachable("invalid type kind");
112 void DynTypedNode::print(llvm::raw_ostream &OS,
113 const PrintingPolicy &PP) const {
114 if (const TemplateArgument *TA = get<TemplateArgument>())
116 else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
118 else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
119 NNSL->getNestedNameSpecifier()->print(OS, PP);
120 else if (const QualType *QT = get<QualType>())
122 else if (const TypeLoc *TL = get<TypeLoc>())
123 TL->getType().print(OS, PP);
124 else if (const Decl *D = get<Decl>())
126 else if (const Stmt *S = get<Stmt>())
127 S->printPretty(OS, nullptr, PP);
128 else if (const Type *T = get<Type>())
129 QualType(T, 0).print(OS, PP);
131 OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
134 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
135 if (const Decl *D = get<Decl>())
137 else if (const Stmt *S = get<Stmt>())
140 OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
143 SourceRange DynTypedNode::getSourceRange() const {
144 if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
145 return CCI->getSourceRange();
146 if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
147 return NNSL->getSourceRange();
148 if (const TypeLoc *TL = get<TypeLoc>())
149 return TL->getSourceRange();
150 if (const Decl *D = get<Decl>())
151 return D->getSourceRange();
152 if (const Stmt *S = get<Stmt>())
153 return S->getSourceRange();
154 return SourceRange();
157 } // end namespace ast_type_traits
158 } // end namespace clang