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, "TemplateArgument" },
26 { NKI_None, "TemplateName" },
27 { NKI_None, "NestedNameSpecifierLoc" },
28 { NKI_None, "QualType" },
29 { NKI_None, "TypeLoc" },
30 { NKI_None, "CXXCtorInitializer" },
31 { NKI_None, "NestedNameSpecifier" },
33 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" },
34 #include "clang/AST/DeclNodes.inc"
36 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED },
37 #include "clang/AST/StmtNodes.inc"
39 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" },
40 #include "clang/AST/TypeNodes.def"
43 bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const {
44 return isBaseOf(KindId, Other.KindId, Distance);
47 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived,
49 if (Base == NKI_None || Derived == NKI_None) return false;
51 while (Derived != Base && Derived != NKI_None) {
52 Derived = AllKindInfo[Derived].ParentId;
57 return Derived == Base;
60 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; }
62 ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1,
64 if (Kind1.isBaseOf(Kind2)) return Kind2;
65 if (Kind2.isBaseOf(Kind1)) return Kind1;
69 ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1,
71 NodeKindId Parent = Kind1.KindId;
72 while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) {
73 Parent = AllKindInfo[Parent].ParentId;
75 return ASTNodeKind(Parent);
78 ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) {
79 switch (D.getKind()) {
80 #define DECL(DERIVED, BASE) \
81 case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl);
82 #define ABSTRACT_DECL(D)
83 #include "clang/AST/DeclNodes.inc"
85 llvm_unreachable("invalid decl kind");
88 ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) {
89 switch (S.getStmtClass()) {
90 case Stmt::NoStmtClass: return NKI_None;
91 #define STMT(CLASS, PARENT) \
92 case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS);
93 #define ABSTRACT_STMT(S)
94 #include "clang/AST/StmtNodes.inc"
96 llvm_unreachable("invalid stmt kind");
99 ASTNodeKind ASTNodeKind::getFromNode(const Type &T) {
100 switch (T.getTypeClass()) {
101 #define TYPE(Class, Base) \
102 case Type::Class: return ASTNodeKind(NKI_##Class##Type);
103 #define ABSTRACT_TYPE(Class, Base)
104 #include "clang/AST/TypeNodes.def"
106 llvm_unreachable("invalid type kind");
109 void DynTypedNode::print(llvm::raw_ostream &OS,
110 const PrintingPolicy &PP) const {
111 if (const TemplateArgument *TA = get<TemplateArgument>())
113 else if (const TemplateName *TN = get<TemplateName>())
115 else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>())
117 else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
118 NNSL->getNestedNameSpecifier()->print(OS, PP);
119 else if (const QualType *QT = get<QualType>())
121 else if (const TypeLoc *TL = get<TypeLoc>())
122 TL->getType().print(OS, PP);
123 else if (const Decl *D = get<Decl>())
125 else if (const Stmt *S = get<Stmt>())
126 S->printPretty(OS, nullptr, PP);
127 else if (const Type *T = get<Type>())
128 QualType(T, 0).print(OS, PP);
130 OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n";
133 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const {
134 if (const Decl *D = get<Decl>())
136 else if (const Stmt *S = get<Stmt>())
138 else if (const Type *T = get<Type>())
141 OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n";
144 SourceRange DynTypedNode::getSourceRange() const {
145 if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>())
146 return CCI->getSourceRange();
147 if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>())
148 return NNSL->getSourceRange();
149 if (const TypeLoc *TL = get<TypeLoc>())
150 return TL->getSourceRange();
151 if (const Decl *D = get<Decl>())
152 return D->getSourceRange();
153 if (const Stmt *S = get<Stmt>())
154 return S->getSourceRange();
155 return SourceRange();
158 } // end namespace ast_type_traits
159 } // end namespace clang