1 //===- IndexTypeSourceInfo.cpp - Indexing types ---------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "IndexingContext.h" 11 #include "clang/AST/RecursiveASTVisitor.h" 12 13 using namespace clang; 14 using namespace index; 15 16 namespace { 17 18 class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> { 19 IndexingContext &IndexCtx; 20 const NamedDecl *Parent; 21 const DeclContext *ParentDC; 22 bool IsBase; 23 SmallVector<SymbolRelation, 3> Relations; 24 25 typedef RecursiveASTVisitor<TypeIndexer> base; 26 27 public: 28 TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent, 29 const DeclContext *DC, bool isBase, bool isIBType) 30 : IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) { 31 if (IsBase) { 32 assert(Parent); 33 Relations.emplace_back((unsigned)SymbolRole::RelationBaseOf, Parent); 34 } 35 if (isIBType) { 36 assert(Parent); 37 Relations.emplace_back((unsigned)SymbolRole::RelationIBTypeOf, Parent); 38 } 39 } 40 41 bool shouldWalkTypesOfTypeLocs() const { return false; } 42 43 #define TRY_TO(CALL_EXPR) \ 44 do { \ 45 if (!CALL_EXPR) \ 46 return false; \ 47 } while (0) 48 49 bool VisitTypedefTypeLoc(TypedefTypeLoc TL) { 50 SourceLocation Loc = TL.getNameLoc(); 51 TypedefNameDecl *ND = TL.getTypedefNameDecl(); 52 if (ND->isTransparentTag()) { 53 TagDecl *Underlying = ND->getUnderlyingType()->getAsTagDecl(); 54 return IndexCtx.handleReference(Underlying, Loc, Parent, 55 ParentDC, SymbolRoleSet(), Relations); 56 } 57 if (IsBase) { 58 TRY_TO(IndexCtx.handleReference(ND, Loc, 59 Parent, ParentDC, SymbolRoleSet())); 60 if (auto *CD = TL.getType()->getAsCXXRecordDecl()) { 61 TRY_TO(IndexCtx.handleReference(CD, Loc, Parent, ParentDC, 62 (unsigned)SymbolRole::Implicit, 63 Relations)); 64 } 65 } else { 66 TRY_TO(IndexCtx.handleReference(ND, Loc, 67 Parent, ParentDC, SymbolRoleSet(), 68 Relations)); 69 } 70 return true; 71 } 72 73 bool traverseParamVarHelper(ParmVarDecl *D) { 74 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 75 if (D->getTypeSourceInfo()) 76 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 77 return true; 78 } 79 80 bool TraverseParmVarDecl(ParmVarDecl *D) { 81 // Avoid visiting default arguments from the definition that were already 82 // visited in the declaration. 83 // FIXME: A free function definition can have default arguments. 84 // Avoiding double visitaiton of default arguments should be handled by the 85 // visitor probably with a bit in the AST to indicate if the attached 86 // default argument was 'inherited' or written in source. 87 if (auto FD = dyn_cast<FunctionDecl>(D->getDeclContext())) { 88 if (FD->isThisDeclarationADefinition()) { 89 return traverseParamVarHelper(D); 90 } 91 } 92 93 return base::TraverseParmVarDecl(D); 94 } 95 96 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { 97 IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC); 98 return true; 99 } 100 101 bool VisitTagTypeLoc(TagTypeLoc TL) { 102 TagDecl *D = TL.getDecl(); 103 if (D->getParentFunctionOrMethod()) 104 return true; 105 106 if (TL.isDefinition()) { 107 IndexCtx.indexTagDecl(D); 108 return true; 109 } 110 111 return IndexCtx.handleReference(D, TL.getNameLoc(), 112 Parent, ParentDC, SymbolRoleSet(), 113 Relations); 114 } 115 116 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 117 return IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(), 118 Parent, ParentDC, SymbolRoleSet(), Relations); 119 } 120 121 bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 122 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) { 123 IndexCtx.handleReference(TL.getProtocol(i), TL.getProtocolLoc(i), 124 Parent, ParentDC, SymbolRoleSet(), Relations); 125 } 126 return true; 127 } 128 129 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { 130 if (const TemplateSpecializationType *T = TL.getTypePtr()) { 131 if (IndexCtx.shouldIndexImplicitTemplateInsts()) { 132 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) 133 IndexCtx.handleReference(RD, TL.getTemplateNameLoc(), 134 Parent, ParentDC, SymbolRoleSet(), Relations); 135 } else { 136 if (const TemplateDecl *D = T->getTemplateName().getAsTemplateDecl()) 137 IndexCtx.handleReference(D, TL.getTemplateNameLoc(), 138 Parent, ParentDC, SymbolRoleSet(), Relations); 139 } 140 } 141 return true; 142 } 143 144 bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 145 const DependentNameType *DNT = TL.getTypePtr(); 146 const NestedNameSpecifier *NNS = DNT->getQualifier(); 147 const Type *T = NNS->getAsType(); 148 if (!T) 149 return true; 150 const TemplateSpecializationType *TST = 151 T->getAs<TemplateSpecializationType>(); 152 if (!TST) 153 return true; 154 TemplateName TN = TST->getTemplateName(); 155 const ClassTemplateDecl *TD = 156 dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl()); 157 if (!TD) 158 return true; 159 CXXRecordDecl *RD = TD->getTemplatedDecl(); 160 DeclarationName Name(DNT->getIdentifier()); 161 std::vector<const NamedDecl *> Symbols = RD->lookupDependentName( 162 Name, [](const NamedDecl *ND) { return isa<TypeDecl>(ND); }); 163 if (Symbols.size() != 1) 164 return true; 165 return IndexCtx.handleReference(Symbols[0], TL.getNameLoc(), Parent, 166 ParentDC, SymbolRoleSet(), Relations); 167 } 168 169 bool TraverseStmt(Stmt *S) { 170 IndexCtx.indexBody(S, Parent, ParentDC); 171 return true; 172 } 173 }; 174 175 } // anonymous namespace 176 177 void IndexingContext::indexTypeSourceInfo(TypeSourceInfo *TInfo, 178 const NamedDecl *Parent, 179 const DeclContext *DC, 180 bool isBase, 181 bool isIBType) { 182 if (!TInfo || TInfo->getTypeLoc().isNull()) 183 return; 184 185 indexTypeLoc(TInfo->getTypeLoc(), Parent, DC, isBase, isIBType); 186 } 187 188 void IndexingContext::indexTypeLoc(TypeLoc TL, 189 const NamedDecl *Parent, 190 const DeclContext *DC, 191 bool isBase, 192 bool isIBType) { 193 if (TL.isNull()) 194 return; 195 196 if (!DC) 197 DC = Parent->getLexicalDeclContext(); 198 TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL); 199 } 200 201 void IndexingContext::indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, 202 const NamedDecl *Parent, 203 const DeclContext *DC) { 204 if (!NNS) 205 return; 206 207 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix()) 208 indexNestedNameSpecifierLoc(Prefix, Parent, DC); 209 210 if (!DC) 211 DC = Parent->getLexicalDeclContext(); 212 SourceLocation Loc = NNS.getSourceRange().getBegin(); 213 214 switch (NNS.getNestedNameSpecifier()->getKind()) { 215 case NestedNameSpecifier::Identifier: 216 case NestedNameSpecifier::Global: 217 case NestedNameSpecifier::Super: 218 break; 219 220 case NestedNameSpecifier::Namespace: 221 handleReference(NNS.getNestedNameSpecifier()->getAsNamespace(), 222 Loc, Parent, DC, SymbolRoleSet()); 223 break; 224 case NestedNameSpecifier::NamespaceAlias: 225 handleReference(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), 226 Loc, Parent, DC, SymbolRoleSet()); 227 break; 228 229 case NestedNameSpecifier::TypeSpec: 230 case NestedNameSpecifier::TypeSpecWithTemplate: 231 indexTypeLoc(NNS.getTypeLoc(), Parent, DC); 232 break; 233 } 234 } 235 236 void IndexingContext::indexTagDecl(const TagDecl *D, 237 ArrayRef<SymbolRelation> Relations) { 238 if (!shouldIndex(D)) 239 return; 240 if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D)) 241 return; 242 243 if (handleDecl(D, /*Roles=*/SymbolRoleSet(), Relations)) { 244 if (D->isThisDeclarationADefinition()) { 245 indexNestedNameSpecifierLoc(D->getQualifierLoc(), D); 246 if (auto CXXRD = dyn_cast<CXXRecordDecl>(D)) { 247 for (const auto &I : CXXRD->bases()) { 248 indexTypeSourceInfo(I.getTypeSourceInfo(), CXXRD, CXXRD, /*isBase=*/true); 249 } 250 } 251 indexDeclContext(D); 252 } 253 } 254 } 255