IndexTypeSourceInfo.cpp revision 58d2dbea680a75de266c5eff77cc15c323cfd48a
1//===- CIndexHigh.cpp - Higher level API functions ------------------------===//
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
12#include "clang/AST/RecursiveASTVisitor.h"
13
14using namespace clang;
15using namespace cxindex;
16
17namespace {
18
19class TypeIndexer : public RecursiveASTVisitor<TypeIndexer> {
20  IndexingContext &IndexCtx;
21  const NamedDecl *Parent;
22  const DeclContext *ParentDC;
23
24public:
25  TypeIndexer(IndexingContext &indexCtx, const NamedDecl *parent,
26              const DeclContext *DC)
27    : IndexCtx(indexCtx), Parent(parent), ParentDC(DC) { }
28
29  bool shouldWalkTypesOfTypeLocs() const { return false; }
30
31  bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
32    IndexCtx.handleReference(TL.getTypedefNameDecl(), TL.getNameLoc(),
33                             Parent, ParentDC);
34    return true;
35  }
36
37  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
38    IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
39    return true;
40  }
41
42  bool VisitTagTypeLoc(TagTypeLoc TL) {
43    TagDecl *D = TL.getDecl();
44    if (D->getParentFunctionOrMethod())
45      return true;
46
47    if (TL.isDefinition()) {
48      IndexCtx.indexTagDecl(D);
49      return true;
50    }
51
52    if (D->getLocation() == TL.getNameLoc())
53      IndexCtx.handleTagDecl(D);
54    else
55      IndexCtx.handleReference(D, TL.getNameLoc(),
56                               Parent, ParentDC);
57    return true;
58  }
59
60  bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
61    IndexCtx.handleReference(TL.getIFaceDecl(), TL.getNameLoc(),
62                             Parent, ParentDC);
63    return true;
64  }
65
66  bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
67    for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) {
68      IndexCtx.handleReference(TL.getProtocol(i), TL.getProtocolLoc(i),
69                               Parent, ParentDC);
70    }
71    return true;
72  }
73
74  bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
75    if (const TemplateSpecializationType *T = TL.getTypePtr()) {
76      if (IndexCtx.shouldIndexImplicitTemplateInsts()) {
77        if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
78          IndexCtx.handleReference(RD, TL.getTemplateNameLoc(),
79                                   Parent, ParentDC);
80      } else {
81        if (const TemplateDecl *D = T->getTemplateName().getAsTemplateDecl())
82          IndexCtx.handleReference(D, TL.getTemplateNameLoc(),
83                                   Parent, ParentDC);
84      }
85    }
86    return true;
87  }
88};
89
90} // anonymous namespace
91
92void IndexingContext::indexTypeSourceInfo(TypeSourceInfo *TInfo,
93                                          const NamedDecl *Parent,
94                                          const DeclContext *DC) {
95  if (!TInfo || TInfo->getTypeLoc().isNull())
96    return;
97
98  indexTypeLoc(TInfo->getTypeLoc(), Parent, DC);
99}
100
101void IndexingContext::indexTypeLoc(TypeLoc TL,
102                                   const NamedDecl *Parent,
103                                   const DeclContext *DC) {
104  if (TL.isNull())
105    return;
106
107  if (DC == 0)
108    DC = Parent->getLexicalDeclContext();
109  TypeIndexer(*this, Parent, DC).TraverseTypeLoc(TL);
110}
111
112void IndexingContext::indexNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
113                                                  const NamedDecl *Parent,
114                                                  const DeclContext *DC) {
115  if (!NNS)
116    return;
117
118  if (NestedNameSpecifierLoc Prefix = NNS.getPrefix())
119    indexNestedNameSpecifierLoc(Prefix, Parent, DC);
120
121  if (DC == 0)
122    DC = Parent->getLexicalDeclContext();
123  SourceLocation Loc = NNS.getSourceRange().getBegin();
124
125  switch (NNS.getNestedNameSpecifier()->getKind()) {
126  case NestedNameSpecifier::Identifier:
127  case NestedNameSpecifier::Global:
128    break;
129
130  case NestedNameSpecifier::Namespace:
131    handleReference(NNS.getNestedNameSpecifier()->getAsNamespace(),
132                    Loc, Parent, DC);
133    break;
134  case NestedNameSpecifier::NamespaceAlias:
135    handleReference(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(),
136                    Loc, Parent, DC);
137    break;
138
139  case NestedNameSpecifier::TypeSpec:
140  case NestedNameSpecifier::TypeSpecWithTemplate:
141    indexTypeLoc(NNS.getTypeLoc(), Parent, DC);
142    break;
143  }
144}
145
146void IndexingContext::indexTagDecl(const TagDecl *D) {
147  if (handleTagDecl(D)) {
148    if (D->isThisDeclarationADefinition())
149      indexDeclContext(D);
150  }
151}
152