IndexDecl.cpp revision d7c15a64ba8ebdca0dd48dd1d2f233107d34494e
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/DeclVisitor.h"
13
14using namespace clang;
15using namespace cxindex;
16
17namespace {
18
19class IndexingDeclVisitor : public DeclVisitor<IndexingDeclVisitor, bool> {
20  IndexingContext &IndexCtx;
21
22public:
23  explicit IndexingDeclVisitor(IndexingContext &indexCtx)
24    : IndexCtx(indexCtx) { }
25
26  void handleDeclarator(DeclaratorDecl *D, const NamedDecl *Parent = 0) {
27    if (!Parent) Parent = D;
28
29    if (!IndexCtx.shouldIndexFunctionLocalSymbols()) {
30      IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent);
31      IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
32    } else {
33      if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
34        IndexCtx.handleVar(Parm);
35      } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
36        for (FunctionDecl::param_iterator
37               PI = FD->param_begin(), PE = FD->param_end(); PI != PE; ++PI) {
38          IndexCtx.handleVar(*PI);
39        }
40      }
41    }
42  }
43
44  void handleObjCMethod(ObjCMethodDecl *D) {
45    IndexCtx.handleObjCMethod(D);
46    if (D->isImplicit())
47      return;
48
49    IndexCtx.indexTypeSourceInfo(D->getResultTypeSourceInfo(), D);
50    for (ObjCMethodDecl::param_iterator
51           I = D->param_begin(), E = D->param_end(); I != E; ++I)
52      handleDeclarator(*I, D);
53
54    if (D->isThisDeclarationADefinition()) {
55      const Stmt *Body = D->getBody();
56      if (Body) {
57        IndexCtx.indexBody(Body, D, D);
58      }
59    }
60  }
61
62  bool VisitFunctionDecl(FunctionDecl *D) {
63    IndexCtx.handleFunction(D);
64    handleDeclarator(D);
65
66    if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
67      // Constructor initializers.
68      for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
69                                             E = Ctor->init_end();
70           I != E; ++I) {
71        CXXCtorInitializer *Init = *I;
72        if (Init->isWritten()) {
73          IndexCtx.indexTypeSourceInfo(Init->getTypeSourceInfo(), D);
74          if (const FieldDecl *Member = Init->getAnyMember())
75            IndexCtx.handleReference(Member, Init->getMemberLocation(), D, D);
76          IndexCtx.indexBody(Init->getInit(), D, D);
77        }
78      }
79    }
80
81    if (D->isThisDeclarationADefinition()) {
82      const Stmt *Body = D->getBody();
83      if (Body) {
84        IndexCtx.indexBody(Body, D, D);
85      }
86    }
87    return true;
88  }
89
90  bool VisitVarDecl(VarDecl *D) {
91    IndexCtx.handleVar(D);
92    handleDeclarator(D);
93    IndexCtx.indexBody(D->getInit(), D);
94    return true;
95  }
96
97  bool VisitFieldDecl(FieldDecl *D) {
98    IndexCtx.handleField(D);
99    handleDeclarator(D);
100    if (D->isBitField())
101      IndexCtx.indexBody(D->getBitWidth(), D);
102    else if (D->hasInClassInitializer())
103      IndexCtx.indexBody(D->getInClassInitializer(), D);
104    return true;
105  }
106
107  bool VisitEnumConstantDecl(EnumConstantDecl *D) {
108    IndexCtx.handleEnumerator(D);
109    IndexCtx.indexBody(D->getInitExpr(), D);
110    return true;
111  }
112
113  bool VisitTypedefDecl(TypedefNameDecl *D) {
114    IndexCtx.handleTypedefName(D);
115    IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
116    return true;
117  }
118
119  bool VisitTagDecl(TagDecl *D) {
120    // Non-free standing tags are handled in indexTypeSourceInfo.
121    if (D->isFreeStanding())
122      IndexCtx.indexTagDecl(D);
123    return true;
124  }
125
126  bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
127    IndexCtx.handleObjCInterface(D);
128
129    if (D->isThisDeclarationADefinition()) {
130      IndexCtx.indexTUDeclsInObjCContainer();
131      IndexCtx.indexDeclContext(D);
132    }
133    return true;
134  }
135
136  bool VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
137    IndexCtx.handleObjCProtocol(D);
138
139    if (D->isThisDeclarationADefinition()) {
140      IndexCtx.indexTUDeclsInObjCContainer();
141      IndexCtx.indexDeclContext(D);
142    }
143    return true;
144  }
145
146  bool VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
147    const ObjCInterfaceDecl *Class = D->getClassInterface();
148    if (!Class)
149      return true;
150
151    if (Class->isImplicitInterfaceDecl())
152      IndexCtx.handleObjCInterface(Class);
153
154    IndexCtx.handleObjCImplementation(D);
155
156    IndexCtx.indexTUDeclsInObjCContainer();
157    IndexCtx.indexDeclContext(D);
158    return true;
159  }
160
161  bool VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
162    IndexCtx.handleObjCCategory(D);
163
164    IndexCtx.indexTUDeclsInObjCContainer();
165    IndexCtx.indexDeclContext(D);
166    return true;
167  }
168
169  bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
170    const ObjCCategoryDecl *Cat = D->getCategoryDecl();
171    if (!Cat)
172      return true;
173
174    IndexCtx.handleObjCCategoryImpl(D);
175
176    IndexCtx.indexTUDeclsInObjCContainer();
177    IndexCtx.indexDeclContext(D);
178    return true;
179  }
180
181  bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
182    // Methods associated with a property, even user-declared ones, are
183    // handled when we handle the property.
184    if (D->isSynthesized())
185      return true;
186
187    handleObjCMethod(D);
188    return true;
189  }
190
191  bool VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
192    if (ObjCMethodDecl *MD = D->getGetterMethodDecl())
193      if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
194        handleObjCMethod(MD);
195    if (ObjCMethodDecl *MD = D->getSetterMethodDecl())
196      if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
197        handleObjCMethod(MD);
198    IndexCtx.handleObjCProperty(D);
199    IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
200    return true;
201  }
202
203  bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
204    ObjCPropertyDecl *PD = D->getPropertyDecl();
205    IndexCtx.handleSynthesizedObjCProperty(D);
206
207    if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
208      return true;
209    assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
210
211    if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
212      if (!IvarD->getSynthesize())
213        IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), 0,
214                                 D->getDeclContext());
215    }
216
217    if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
218      if (MD->isSynthesized())
219        IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
220    }
221    if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
222      if (MD->isSynthesized())
223        IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
224    }
225    return true;
226  }
227
228  bool VisitNamespaceDecl(NamespaceDecl *D) {
229    IndexCtx.handleNamespace(D);
230    IndexCtx.indexDeclContext(D);
231    return true;
232  }
233
234  bool VisitUsingDecl(UsingDecl *D) {
235    // FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
236    // we should do better.
237
238    IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
239    for (UsingDecl::shadow_iterator
240           I = D->shadow_begin(), E = D->shadow_end(); I != E; ++I) {
241      IndexCtx.handleReference((*I)->getUnderlyingDecl(), D->getLocation(),
242                               D, D->getLexicalDeclContext());
243    }
244    return true;
245  }
246
247  bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
248    // FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
249    // we should do better.
250
251    IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
252    IndexCtx.handleReference(D->getNominatedNamespaceAsWritten(),
253                             D->getLocation(), D, D->getLexicalDeclContext());
254    return true;
255  }
256
257  bool VisitClassTemplateDecl(ClassTemplateDecl *D) {
258    IndexCtx.handleClassTemplate(D);
259    if (D->isThisDeclarationADefinition())
260      IndexCtx.indexDeclContext(D->getTemplatedDecl());
261    return true;
262  }
263
264  bool VisitClassTemplateSpecializationDecl(
265                                           ClassTemplateSpecializationDecl *D) {
266    // FIXME: Notify subsequent callbacks if info comes from implicit
267    // instantiation.
268    if (D->isThisDeclarationADefinition() &&
269        (IndexCtx.shouldIndexImplicitTemplateInsts() ||
270         !IndexCtx.isTemplateImplicitInstantiation(D)))
271      IndexCtx.indexTagDecl(D);
272    return true;
273  }
274
275  bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
276    IndexCtx.handleFunctionTemplate(D);
277    FunctionDecl *FD = D->getTemplatedDecl();
278    handleDeclarator(FD, D);
279    if (FD->isThisDeclarationADefinition()) {
280      const Stmt *Body = FD->getBody();
281      if (Body) {
282        IndexCtx.indexBody(Body, D, FD);
283      }
284    }
285    return true;
286  }
287
288  bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
289    IndexCtx.handleTypeAliasTemplate(D);
290    IndexCtx.indexTypeSourceInfo(D->getTemplatedDecl()->getTypeSourceInfo(), D);
291    return true;
292  }
293};
294
295} // anonymous namespace
296
297void IndexingContext::indexDecl(const Decl *D) {
298  if (D->isImplicit() && shouldIgnoreIfImplicit(D))
299    return;
300
301  bool Handled = IndexingDeclVisitor(*this).Visit(const_cast<Decl*>(D));
302  if (!Handled && isa<DeclContext>(D))
303    indexDeclContext(cast<DeclContext>(D));
304}
305
306void IndexingContext::indexDeclContext(const DeclContext *DC) {
307  for (DeclContext::decl_iterator
308         I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
309    indexDecl(*I);
310  }
311}
312
313void IndexingContext::indexTopLevelDecl(Decl *D) {
314  if (isNotFromSourceFile(D->getLocation()))
315    return;
316
317  if (isa<ObjCMethodDecl>(D))
318    return; // Wait for the objc container.
319
320  indexDecl(D);
321}
322
323void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
324  for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
325    indexTopLevelDecl(*I);
326}
327
328void IndexingContext::indexTUDeclsInObjCContainer() {
329  for (unsigned i = 0, e = TUDeclsInObjCContainer.size(); i != e; ++i)
330    indexDeclGroupRef(TUDeclsInObjCContainer[i]);
331  TUDeclsInObjCContainer.clear();
332}
333