| 1 | //===- IndexTypeSourceInfo.cpp - Indexing types ---------------------------===// |
| 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 | #include "IndexingContext.h" |
| 10 | #include "clang/AST/ASTConcept.h" |
| 11 | #include "clang/AST/PrettyPrinter.h" |
| 12 | #include "clang/AST/RecursiveASTVisitor.h" |
| 13 | #include "clang/AST/TypeLoc.h" |
| 14 | #include "clang/Sema/HeuristicResolver.h" |
| 15 | #include "llvm/ADT/ScopeExit.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | using namespace index; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> { |
| 23 | IndexingContext &IndexCtx; |
| 24 | const NamedDecl *Parent; |
| 25 | const DeclContext *ParentDC; |
| 26 | bool IsBase; |
| 27 | SmallVector<SymbolRelation, 3> Relations; |
| 28 | |
| 29 | typedef RecursiveASTVisitor<TypeIndexer> base; |
| 30 | |
| 31 | public: |
| 32 | TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent, |
| 33 | const DeclContext *DC, bool isBase, bool isIBType) |
| 34 | : IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) { |
| 35 | if (IsBase) { |
| 36 | assert(Parent); |
| 37 | Relations.emplace_back(Args: (unsigned)SymbolRole::RelationBaseOf, Args&: Parent); |
| 38 | } |
| 39 | if (isIBType) { |
| 40 | assert(Parent); |
| 41 | Relations.emplace_back(Args: (unsigned)SymbolRole::RelationIBTypeOf, Args&: Parent); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 46 | |
| 47 | #define TRY_TO(CALL_EXPR) \ |
| 48 | do { \ |
| 49 | if (!CALL_EXPR) \ |
| 50 | return false; \ |
| 51 | } while (0) |
| 52 | |
| 53 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TTPL) { |
| 54 | SourceLocation Loc = TTPL.getNameLoc(); |
| 55 | TemplateTypeParmDecl *TTPD = TTPL.getDecl(); |
| 56 | return IndexCtx.handleReference(D: TTPD, Loc, Parent, DC: ParentDC, |
| 57 | Roles: SymbolRoleSet()); |
| 58 | } |
| 59 | |
| 60 | bool handleTypeNameReference(NamedDecl *ND, SourceLocation Loc, QualType T) { |
| 61 | if (auto *TND = dyn_cast<TypedefNameDecl>(Val: ND)) |
| 62 | if (TND->isTransparentTag()) |
| 63 | ND = TND->getUnderlyingType()->castAsTagDecl(); |
| 64 | if (IsBase && !isa<TagDecl>(Val: ND)) { |
| 65 | TRY_TO( |
| 66 | IndexCtx.handleReference(ND, Loc, Parent, ParentDC, SymbolRoleSet())); |
| 67 | if (auto *CD = T->getAsCXXRecordDecl()) { |
| 68 | TRY_TO(IndexCtx.handleReference(CD, Loc, Parent, ParentDC, |
| 69 | (unsigned)SymbolRole::Implicit, |
| 70 | Relations)); |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | return IndexCtx.handleReference(D: ND, Loc, Parent, DC: ParentDC, Roles: SymbolRoleSet(), |
| 75 | Relations); |
| 76 | } |
| 77 | |
| 78 | bool VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 79 | return handleTypeNameReference(ND: TL.getDecl(), Loc: TL.getNameLoc(), T: TL.getType()); |
| 80 | } |
| 81 | |
| 82 | // A type found via a using-declaration, e.g. `ns2::A` after |
| 83 | // `namespace ns2 { using ns1::A; }`. Like DeclRefExpr, report the target |
| 84 | // declaration rather than the UsingShadowDecl. |
| 85 | bool VisitUsingTypeLoc(UsingTypeLoc TL) { |
| 86 | return handleTypeNameReference(ND: TL.getDecl()->getTargetDecl(), |
| 87 | Loc: TL.getNameLoc(), T: TL.getType()); |
| 88 | } |
| 89 | |
| 90 | bool VisitAutoTypeLoc(AutoTypeLoc TL) { |
| 91 | if (auto *C = TL.getNamedConcept().getAsTemplateDecl()) |
| 92 | return IndexCtx.handleReference(D: C, Loc: TL.getConceptNameLoc(), Parent, |
| 93 | DC: ParentDC); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | bool traverseParamVarHelper(ParmVarDecl *D) { |
| 98 | TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); |
| 99 | if (D->getTypeSourceInfo()) |
| 100 | TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | bool TraverseParmVarDecl(ParmVarDecl *D) { |
| 105 | // Avoid visiting default arguments from the definition that were already |
| 106 | // visited in the declaration. |
| 107 | // FIXME: A free function definition can have default arguments. |
| 108 | // Avoiding double visitaiton of default arguments should be handled by the |
| 109 | // visitor probably with a bit in the AST to indicate if the attached |
| 110 | // default argument was 'inherited' or written in source. |
| 111 | if (auto FD = dyn_cast<FunctionDecl>(Val: D->getDeclContext())) { |
| 112 | if (FD->isThisDeclarationADefinition()) { |
| 113 | return traverseParamVarHelper(D); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return base::TraverseParmVarDecl(D); |
| 118 | } |
| 119 | |
| 120 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) { |
| 121 | IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, DC: ParentDC); |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | bool VisitTagTypeLoc(TagTypeLoc TL) { |
| 126 | TagDecl *D = TL.getDecl(); |
| 127 | if (!IndexCtx.shouldIndexFunctionLocalSymbols() && |
| 128 | D->getParentFunctionOrMethod()) |
| 129 | return true; |
| 130 | |
| 131 | if (TL.isDefinition()) { |
| 132 | IndexCtx.indexTagDecl(D); |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | return IndexCtx.handleReference(D, Loc: TL.getNameLoc(), |
| 137 | Parent, DC: ParentDC, Roles: SymbolRoleSet(), |
| 138 | Relations); |
| 139 | } |
| 140 | |
| 141 | bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 142 | return IndexCtx.handleReference(D: TL.getIFaceDecl(), Loc: TL.getNameLoc(), |
| 143 | Parent, DC: ParentDC, Roles: SymbolRoleSet(), Relations); |
| 144 | } |
| 145 | |
| 146 | bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 147 | for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) { |
| 148 | IndexCtx.handleReference(D: TL.getProtocol(i), Loc: TL.getProtocolLoc(i), |
| 149 | Parent, DC: ParentDC, Roles: SymbolRoleSet(), Relations); |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | void HandleTemplateSpecializationTypeLoc(TemplateName TemplName, |
| 155 | SourceLocation TemplNameLoc, |
| 156 | CXXRecordDecl *ResolvedClass, |
| 157 | bool IsTypeAlias) { |
| 158 | // In presence of type aliases, the resolved class was never written in |
| 159 | // the code so don't report it. |
| 160 | if (!IsTypeAlias && ResolvedClass && |
| 161 | (!ResolvedClass->isImplicit() || |
| 162 | IndexCtx.shouldIndexImplicitInstantiation())) { |
| 163 | IndexCtx.handleReference(D: ResolvedClass, Loc: TemplNameLoc, Parent, DC: ParentDC, |
| 164 | Roles: SymbolRoleSet(), Relations); |
| 165 | } else if (const TemplateDecl *D = TemplName.getAsTemplateDecl()) { |
| 166 | IndexCtx.handleReference(D, Loc: TemplNameLoc, Parent, DC: ParentDC, |
| 167 | Roles: SymbolRoleSet(), Relations); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { |
| 172 | auto *T = TL.getTypePtr(); |
| 173 | if (!T) |
| 174 | return true; |
| 175 | HandleTemplateSpecializationTypeLoc( |
| 176 | TemplName: T->getTemplateName(), TemplNameLoc: TL.getTemplateNameLoc(), ResolvedClass: T->getAsCXXRecordDecl(), |
| 177 | IsTypeAlias: T->isTypeAlias()); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | bool TraverseTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL, |
| 182 | bool TraverseQualifier) { |
| 183 | if (!WalkUpFromTemplateSpecializationTypeLoc(TL)) |
| 184 | return false; |
| 185 | if (!TraverseTemplateName(Template: TL.getTypePtr()->getTemplateName())) |
| 186 | return false; |
| 187 | |
| 188 | // The relations we have to `Parent` do not apply to our template arguments, |
| 189 | // so clear them while visiting the args. |
| 190 | SmallVector<SymbolRelation, 3> SavedRelations = Relations; |
| 191 | Relations.clear(); |
| 192 | llvm::scope_exit ResetSavedRelations( |
| 193 | [&] { this->Relations = SavedRelations; }); |
| 194 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 195 | if (!TraverseTemplateArgumentLoc(ArgLoc: TL.getArgLoc(i: I))) |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) { |
| 203 | auto *T = TL.getTypePtr(); |
| 204 | if (!T) |
| 205 | return true; |
| 206 | HandleTemplateSpecializationTypeLoc( |
| 207 | TemplName: T->getTemplateName(), TemplNameLoc: TL.getTemplateNameLoc(), ResolvedClass: T->getAsCXXRecordDecl(), |
| 208 | /*IsTypeAlias=*/false); |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 213 | std::vector<const NamedDecl *> Symbols = |
| 214 | IndexCtx.getResolver()->resolveDependentNameType(DNT: TL.getTypePtr()); |
| 215 | if (Symbols.size() != 1) |
| 216 | return true; |
| 217 | return IndexCtx.handleReference(D: Symbols[0], Loc: TL.getNameLoc(), Parent, |
| 218 | DC: ParentDC, Roles: SymbolRoleSet(), Relations); |
| 219 | } |
| 220 | |
| 221 | bool TraverseStmt(Stmt *S) { |
| 222 | IndexCtx.indexBody(S, Parent, DC: ParentDC); |
| 223 | return true; |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | } // anonymous namespace |
| 228 | |
| 229 | void IndexingContext::indexTypeSourceInfo(TypeSourceInfo *TInfo, |
| 230 | const NamedDecl *Parent, |
| 231 | const DeclContext *DC, |
| 232 | bool isBase, |
| 233 | bool isIBType) { |
| 234 | if (!TInfo || TInfo->getTypeLoc().isNull()) |
| 235 | return; |
| 236 | |
| 237 | indexTypeLoc(TL: TInfo->getTypeLoc(), Parent, DC, isBase, isIBType); |
| 238 | } |
| 239 | |
| 240 | void IndexingContext::indexTypeLoc(TypeLoc TL, |
| 241 | const NamedDecl *Parent, |
| 242 | const DeclContext *DC, |
| 243 | bool isBase, |
| 244 | bool isIBType) { |
| 245 | if (TL.isNull()) |
| 246 | return; |
| 247 | |
| 248 | if (!DC) |
| 249 | DC = Parent->getLexicalDeclContext(); |
| 250 | TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL); |
| 251 | } |
| 252 | |
| 253 | void IndexingContext::indexNestedNameSpecifierLoc( |
| 254 | NestedNameSpecifierLoc QualifierLoc, const NamedDecl *Parent, |
| 255 | const DeclContext *DC) { |
| 256 | if (!DC) |
| 257 | DC = Parent->getLexicalDeclContext(); |
| 258 | switch (NestedNameSpecifier Qualifier = QualifierLoc.getNestedNameSpecifier(); |
| 259 | Qualifier.getKind()) { |
| 260 | case NestedNameSpecifier::Kind::Null: |
| 261 | case NestedNameSpecifier::Kind::Global: |
| 262 | case NestedNameSpecifier::Kind::MicrosoftSuper: |
| 263 | break; |
| 264 | |
| 265 | case NestedNameSpecifier::Kind::Namespace: { |
| 266 | auto [Namespace, Prefix] = QualifierLoc.castAsNamespaceAndPrefix(); |
| 267 | indexNestedNameSpecifierLoc(QualifierLoc: Prefix, Parent, DC); |
| 268 | handleReference(D: Namespace, Loc: QualifierLoc.getLocalBeginLoc(), Parent, DC, |
| 269 | Roles: SymbolRoleSet()); |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | case NestedNameSpecifier::Kind::Type: |
| 274 | indexTypeLoc(TL: QualifierLoc.castAsTypeLoc(), Parent, DC); |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void IndexingContext::indexTagDecl(const TagDecl *D, |
| 280 | ArrayRef<SymbolRelation> Relations) { |
| 281 | if (!shouldIndex(D)) |
| 282 | return; |
| 283 | if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D)) |
| 284 | return; |
| 285 | |
| 286 | if (handleDecl(D, /*Roles=*/SymbolRoleSet(), Relations)) { |
| 287 | if (D->isThisDeclarationADefinition()) { |
| 288 | indexNestedNameSpecifierLoc(QualifierLoc: D->getQualifierLoc(), Parent: D); |
| 289 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(Val: D)) { |
| 290 | for (const auto &I : CXXRD->bases()) { |
| 291 | indexTypeSourceInfo(TInfo: I.getTypeSourceInfo(), Parent: CXXRD, DC: CXXRD, /*isBase=*/true); |
| 292 | } |
| 293 | } |
| 294 | indexDeclContext(DC: D); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |