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