1 //===- IndexTypeSourceInfo.cpp - Indexing types ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "IndexingContext.h"
11 #include "clang/AST/RecursiveASTVisitor.h"
13 using namespace clang;
14 using namespace index;
18 class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
19 IndexingContext &IndexCtx;
20 const NamedDecl *Parent;
21 const DeclContext *ParentDC;
23 SmallVector<SymbolRelation, 3> Relations;
25 typedef RecursiveASTVisitor<TypeIndexer> base;
28 TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent,
29 const DeclContext *DC, bool isBase, bool isIBType)
30 : IndexCtx(indexCtx), Parent(parent), ParentDC(DC), IsBase(isBase) {
33 Relations.emplace_back((unsigned)SymbolRole::RelationBaseOf, Parent);
37 Relations.emplace_back((unsigned)SymbolRole::RelationIBTypeOf, Parent);
41 bool shouldWalkTypesOfTypeLocs() const { return false; }
43 #define TRY_TO(CALL_EXPR) \
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);
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,
66 TRY_TO(IndexCtx.handleReference(ND, Loc,
67 Parent, ParentDC, SymbolRoleSet(),
73 bool traverseParamVarHelper(ParmVarDecl *D) {
74 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc()));
75 if (D->getTypeSourceInfo())
76 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
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);
93 return base::TraverseParmVarDecl(D);
96 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
97 IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
101 bool VisitTagTypeLoc(TagTypeLoc TL) {
102 TagDecl *D = TL.getDecl();
103 if (D->getParentFunctionOrMethod())
106 if (TL.isDefinition()) {
107 IndexCtx.indexTagDecl(D);
111 return IndexCtx.handleReference(D, TL.getNameLoc(),
112 Parent, ParentDC, SymbolRoleSet(),
116 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
117 return IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(),
118 Parent, ParentDC, SymbolRoleSet(), Relations);
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);
129 template<typename TypeLocType>
130 bool HandleTemplateSpecializationTypeLoc(TypeLocType TL) {
131 if (const auto *T = TL.getTypePtr()) {
132 if (IndexCtx.shouldIndexImplicitInstantiation()) {
133 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
134 IndexCtx.handleReference(RD, TL.getTemplateNameLoc(),
135 Parent, ParentDC, SymbolRoleSet(), Relations);
137 if (const TemplateDecl *D = T->getTemplateName().getAsTemplateDecl())
138 IndexCtx.handleReference(D, TL.getTemplateNameLoc(),
139 Parent, ParentDC, SymbolRoleSet(), Relations);
145 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
146 return HandleTemplateSpecializationTypeLoc(TL);
149 bool VisitDeducedTemplateSpecializationTypeLoc(DeducedTemplateSpecializationTypeLoc TL) {
150 return HandleTemplateSpecializationTypeLoc(TL);
153 bool VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
154 const DependentNameType *DNT = TL.getTypePtr();
155 const NestedNameSpecifier *NNS = DNT->getQualifier();
156 const Type *T = NNS->getAsType();
159 const TemplateSpecializationType *TST =
160 T->getAs<TemplateSpecializationType>();
163 TemplateName TN = TST->getTemplateName();
164 const ClassTemplateDecl *TD =
165 dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
168 CXXRecordDecl *RD = TD->getTemplatedDecl();
169 if (!RD->hasDefinition())
171 RD = RD->getDefinition();
172 DeclarationName Name(DNT->getIdentifier());
173 std::vector<const NamedDecl *> Symbols = RD->lookupDependentName(
174 Name, [](const NamedDecl *ND) { return isa<TypeDecl>(ND); });
175 if (Symbols.size() != 1)
177 return IndexCtx.handleReference(Symbols[0], TL.getNameLoc(), Parent,
178 ParentDC, SymbolRoleSet(), Relations);
181 bool TraverseStmt(Stmt *S) {
182 IndexCtx.indexBody(S, Parent, ParentDC);
187 } // anonymous namespace
189 void IndexingContext::indexTypeSourceInfo(TypeSourceInfo *TInfo,
190 const NamedDecl *Parent,
191 const DeclContext *DC,
194 if (!TInfo || TInfo->getTypeLoc().isNull())
197 indexTypeLoc(TInfo->getTypeLoc(), Parent, DC, isBase, isIBType);
200 void IndexingContext::indexTypeLoc(TypeLoc TL,
201 const NamedDecl *Parent,
202 const DeclContext *DC,
209 DC = Parent->getLexicalDeclContext();
210 TypeIndexer(*this, Parent, DC, isBase, isIBType).TraverseTypeLoc(TL);
213 void IndexingContext::indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
214 const NamedDecl *Parent,
215 const DeclContext *DC) {
219 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
220 indexNestedNameSpecifierLoc(Prefix, Parent, DC);
223 DC = Parent->getLexicalDeclContext();
224 SourceLocation Loc = NNS.getLocalBeginLoc();
226 switch (NNS.getNestedNameSpecifier()->getKind()) {
227 case NestedNameSpecifier::Identifier:
228 case NestedNameSpecifier::Global:
229 case NestedNameSpecifier::Super:
232 case NestedNameSpecifier::Namespace:
233 handleReference(NNS.getNestedNameSpecifier()->getAsNamespace(),
234 Loc, Parent, DC, SymbolRoleSet());
236 case NestedNameSpecifier::NamespaceAlias:
237 handleReference(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(),
238 Loc, Parent, DC, SymbolRoleSet());
241 case NestedNameSpecifier::TypeSpec:
242 case NestedNameSpecifier::TypeSpecWithTemplate:
243 indexTypeLoc(NNS.getTypeLoc(), Parent, DC);
248 void IndexingContext::indexTagDecl(const TagDecl *D,
249 ArrayRef<SymbolRelation> Relations) {
252 if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D))
255 if (handleDecl(D, /*Roles=*/SymbolRoleSet(), Relations)) {
256 if (D->isThisDeclarationADefinition()) {
257 indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
258 if (auto CXXRD = dyn_cast<CXXRecordDecl>(D)) {
259 for (const auto &I : CXXRD->bases()) {
260 indexTypeSourceInfo(I.getTypeSourceInfo(), CXXRD, CXXRD, /*isBase=*/true);