1 //===--- ASTTypeTraits.cpp --------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Provides a dynamic type identifier and a dynamically typed node container 10 // that can be used to store an AST base node at runtime in the same storage in 11 // a type safe way. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/AST/ASTTypeTraits.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/DeclCXX.h" 18 19 namespace clang { 20 namespace ast_type_traits { 21 22 const ASTNodeKind::KindInfo ASTNodeKind::AllKindInfo[] = { 23 { NKI_None, "<None>" }, 24 { NKI_None, "TemplateArgument" }, 25 { NKI_None, "TemplateName" }, 26 { NKI_None, "NestedNameSpecifierLoc" }, 27 { NKI_None, "QualType" }, 28 { NKI_None, "TypeLoc" }, 29 { NKI_None, "CXXCtorInitializer" }, 30 { NKI_None, "NestedNameSpecifier" }, 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::isBaseOf(NodeKindId Base, NodeKindId Derived, 47 unsigned *Distance) { 48 if (Base == NKI_None || Derived == NKI_None) return false; 49 unsigned Dist = 0; 50 while (Derived != Base && Derived != NKI_None) { 51 Derived = AllKindInfo[Derived].ParentId; 52 ++Dist; 53 } 54 if (Distance) 55 *Distance = Dist; 56 return Derived == Base; 57 } 58 59 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; } 60 61 ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1, 62 ASTNodeKind Kind2) { 63 if (Kind1.isBaseOf(Kind2)) return Kind2; 64 if (Kind2.isBaseOf(Kind1)) return Kind1; 65 return ASTNodeKind(); 66 } 67 68 ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1, 69 ASTNodeKind Kind2) { 70 NodeKindId Parent = Kind1.KindId; 71 while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) { 72 Parent = AllKindInfo[Parent].ParentId; 73 } 74 return ASTNodeKind(Parent); 75 } 76 77 ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) { 78 switch (D.getKind()) { 79 #define DECL(DERIVED, BASE) \ 80 case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl); 81 #define ABSTRACT_DECL(D) 82 #include "clang/AST/DeclNodes.inc" 83 }; 84 llvm_unreachable("invalid decl kind"); 85 } 86 87 ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) { 88 switch (S.getStmtClass()) { 89 case Stmt::NoStmtClass: return NKI_None; 90 #define STMT(CLASS, PARENT) \ 91 case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS); 92 #define ABSTRACT_STMT(S) 93 #include "clang/AST/StmtNodes.inc" 94 } 95 llvm_unreachable("invalid stmt kind"); 96 } 97 98 ASTNodeKind ASTNodeKind::getFromNode(const Type &T) { 99 switch (T.getTypeClass()) { 100 #define TYPE(Class, Base) \ 101 case Type::Class: return ASTNodeKind(NKI_##Class##Type); 102 #define ABSTRACT_TYPE(Class, Base) 103 #include "clang/AST/TypeNodes.def" 104 } 105 llvm_unreachable("invalid type kind"); 106 } 107 108 void DynTypedNode::print(llvm::raw_ostream &OS, 109 const PrintingPolicy &PP) const { 110 if (const TemplateArgument *TA = get<TemplateArgument>()) 111 TA->print(PP, OS); 112 else if (const TemplateName *TN = get<TemplateName>()) 113 TN->print(OS, PP); 114 else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>()) 115 NNS->print(OS, PP); 116 else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) 117 NNSL->getNestedNameSpecifier()->print(OS, PP); 118 else if (const QualType *QT = get<QualType>()) 119 QT->print(OS, PP); 120 else if (const TypeLoc *TL = get<TypeLoc>()) 121 TL->getType().print(OS, PP); 122 else if (const Decl *D = get<Decl>()) 123 D->print(OS, PP); 124 else if (const Stmt *S = get<Stmt>()) 125 S->printPretty(OS, nullptr, PP); 126 else if (const Type *T = get<Type>()) 127 QualType(T, 0).print(OS, PP); 128 else 129 OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n"; 130 } 131 132 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const { 133 if (const Decl *D = get<Decl>()) 134 D->dump(OS); 135 else if (const Stmt *S = get<Stmt>()) 136 S->dump(OS, SM); 137 else if (const Type *T = get<Type>()) 138 T->dump(OS); 139 else 140 OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n"; 141 } 142 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(); 155 } 156 157 } // end namespace ast_type_traits 158 } // end namespace clang 159