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 #include "clang/AST/NestedNameSpecifier.h" 19 #include "clang/AST/OpenMPClause.h" 20 21 using namespace clang; 22 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, "CXXBaseSpecifier" }, 31 { NKI_None, "CXXCtorInitializer" }, 32 { NKI_None, "NestedNameSpecifier" }, 33 { NKI_None, "Decl" }, 34 #define DECL(DERIVED, BASE) { NKI_##BASE, #DERIVED "Decl" }, 35 #include "clang/AST/DeclNodes.inc" 36 { NKI_None, "Stmt" }, 37 #define STMT(DERIVED, BASE) { NKI_##BASE, #DERIVED }, 38 #include "clang/AST/StmtNodes.inc" 39 { NKI_None, "Type" }, 40 #define TYPE(DERIVED, BASE) { NKI_##BASE, #DERIVED "Type" }, 41 #include "clang/AST/TypeNodes.inc" 42 { NKI_None, "OMPClause" }, 43 #define OMP_CLAUSE_CLASS(Enum, Str, Class) {NKI_OMPClause, #Class}, 44 #include "llvm/Frontend/OpenMP/OMPKinds.def" 45 }; 46 47 bool ASTNodeKind::isBaseOf(ASTNodeKind Other, unsigned *Distance) const { 48 return isBaseOf(KindId, Other.KindId, Distance); 49 } 50 51 bool ASTNodeKind::isBaseOf(NodeKindId Base, NodeKindId Derived, 52 unsigned *Distance) { 53 if (Base == NKI_None || Derived == NKI_None) return false; 54 unsigned Dist = 0; 55 while (Derived != Base && Derived != NKI_None) { 56 Derived = AllKindInfo[Derived].ParentId; 57 ++Dist; 58 } 59 if (Distance) 60 *Distance = Dist; 61 return Derived == Base; 62 } 63 64 StringRef ASTNodeKind::asStringRef() const { return AllKindInfo[KindId].Name; } 65 66 ASTNodeKind ASTNodeKind::getMostDerivedType(ASTNodeKind Kind1, 67 ASTNodeKind Kind2) { 68 if (Kind1.isBaseOf(Kind2)) return Kind2; 69 if (Kind2.isBaseOf(Kind1)) return Kind1; 70 return ASTNodeKind(); 71 } 72 73 ASTNodeKind ASTNodeKind::getMostDerivedCommonAncestor(ASTNodeKind Kind1, 74 ASTNodeKind Kind2) { 75 NodeKindId Parent = Kind1.KindId; 76 while (!isBaseOf(Parent, Kind2.KindId, nullptr) && Parent != NKI_None) { 77 Parent = AllKindInfo[Parent].ParentId; 78 } 79 return ASTNodeKind(Parent); 80 } 81 82 ASTNodeKind ASTNodeKind::getFromNode(const Decl &D) { 83 switch (D.getKind()) { 84 #define DECL(DERIVED, BASE) \ 85 case Decl::DERIVED: return ASTNodeKind(NKI_##DERIVED##Decl); 86 #define ABSTRACT_DECL(D) 87 #include "clang/AST/DeclNodes.inc" 88 }; 89 llvm_unreachable("invalid decl kind"); 90 } 91 92 ASTNodeKind ASTNodeKind::getFromNode(const Stmt &S) { 93 switch (S.getStmtClass()) { 94 case Stmt::NoStmtClass: return NKI_None; 95 #define STMT(CLASS, PARENT) \ 96 case Stmt::CLASS##Class: return ASTNodeKind(NKI_##CLASS); 97 #define ABSTRACT_STMT(S) 98 #include "clang/AST/StmtNodes.inc" 99 } 100 llvm_unreachable("invalid stmt kind"); 101 } 102 103 ASTNodeKind ASTNodeKind::getFromNode(const Type &T) { 104 switch (T.getTypeClass()) { 105 #define TYPE(Class, Base) \ 106 case Type::Class: return ASTNodeKind(NKI_##Class##Type); 107 #define ABSTRACT_TYPE(Class, Base) 108 #include "clang/AST/TypeNodes.inc" 109 } 110 llvm_unreachable("invalid type kind"); 111 } 112 113 ASTNodeKind ASTNodeKind::getFromNode(const OMPClause &C) { 114 switch (C.getClauseKind()) { 115 #define OMP_CLAUSE_CLASS(Enum, Str, Class) \ 116 case llvm::omp::Clause::Enum: \ 117 return ASTNodeKind(NKI_##Class); 118 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 119 case llvm::omp::Clause::Enum: \ 120 llvm_unreachable("unexpected OpenMP clause kind"); 121 #include "llvm/Frontend/OpenMP/OMPKinds.def" 122 } 123 llvm_unreachable("invalid stmt kind"); 124 } 125 126 void DynTypedNode::print(llvm::raw_ostream &OS, 127 const PrintingPolicy &PP) const { 128 if (const TemplateArgument *TA = get<TemplateArgument>()) 129 TA->print(PP, OS); 130 else if (const TemplateName *TN = get<TemplateName>()) 131 TN->print(OS, PP); 132 else if (const NestedNameSpecifier *NNS = get<NestedNameSpecifier>()) 133 NNS->print(OS, PP); 134 else if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) { 135 if (const NestedNameSpecifier *NNS = NNSL->getNestedNameSpecifier()) 136 NNS->print(OS, PP); 137 else 138 OS << "(empty NestedNameSpecifierLoc)"; 139 } else if (const QualType *QT = get<QualType>()) 140 QT->print(OS, PP); 141 else if (const TypeLoc *TL = get<TypeLoc>()) 142 TL->getType().print(OS, PP); 143 else if (const Decl *D = get<Decl>()) 144 D->print(OS, PP); 145 else if (const Stmt *S = get<Stmt>()) 146 S->printPretty(OS, nullptr, PP); 147 else if (const Type *T = get<Type>()) 148 QualType(T, 0).print(OS, PP); 149 else 150 OS << "Unable to print values of type " << NodeKind.asStringRef() << "\n"; 151 } 152 153 void DynTypedNode::dump(llvm::raw_ostream &OS, SourceManager &SM) const { 154 if (const Decl *D = get<Decl>()) 155 D->dump(OS); 156 else if (const Stmt *S = get<Stmt>()) 157 S->dump(OS, SM); 158 else if (const Type *T = get<Type>()) 159 T->dump(OS); 160 else 161 OS << "Unable to dump values of type " << NodeKind.asStringRef() << "\n"; 162 } 163 164 SourceRange DynTypedNode::getSourceRange() const { 165 if (const CXXCtorInitializer *CCI = get<CXXCtorInitializer>()) 166 return CCI->getSourceRange(); 167 if (const NestedNameSpecifierLoc *NNSL = get<NestedNameSpecifierLoc>()) 168 return NNSL->getSourceRange(); 169 if (const TypeLoc *TL = get<TypeLoc>()) 170 return TL->getSourceRange(); 171 if (const Decl *D = get<Decl>()) 172 return D->getSourceRange(); 173 if (const Stmt *S = get<Stmt>()) 174 return S->getSourceRange(); 175 if (const auto *C = get<OMPClause>()) 176 return SourceRange(C->getBeginLoc(), C->getEndLoc()); 177 return SourceRange(); 178 } 179