blob: 65b0b16a37295012f4776ce553e891e2dbf163f0 [file] [log] [blame]
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001//===- 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"
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +000013#include "clang/Basic/Module.h"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000014
15using namespace clang;
16using namespace cxindex;
17
18namespace {
19
20class IndexingDeclVisitor : public DeclVisitor<IndexingDeclVisitor, bool> {
21 IndexingContext &IndexCtx;
22
23public:
24 explicit IndexingDeclVisitor(IndexingContext &indexCtx)
25 : IndexCtx(indexCtx) { }
26
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000027 void handleDeclarator(DeclaratorDecl *D, const NamedDecl *Parent = 0) {
28 if (!Parent) Parent = D;
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +000029
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +000030 if (!IndexCtx.shouldIndexFunctionLocalSymbols()) {
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +000031 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent);
32 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
33 } else {
34 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
35 IndexCtx.handleVar(Parm);
36 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
37 for (FunctionDecl::param_iterator
38 PI = FD->param_begin(), PE = FD->param_end(); PI != PE; ++PI) {
39 IndexCtx.handleVar(*PI);
40 }
41 }
42 }
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000043 }
44
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +000045 void handleObjCMethod(ObjCMethodDecl *D) {
46 IndexCtx.handleObjCMethod(D);
47 if (D->isImplicit())
48 return;
49
50 IndexCtx.indexTypeSourceInfo(D->getResultTypeSourceInfo(), D);
51 for (ObjCMethodDecl::param_iterator
52 I = D->param_begin(), E = D->param_end(); I != E; ++I)
53 handleDeclarator(*I, D);
54
55 if (D->isThisDeclarationADefinition()) {
56 const Stmt *Body = D->getBody();
57 if (Body) {
58 IndexCtx.indexBody(Body, D, D);
59 }
60 }
61 }
62
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000063 bool VisitFunctionDecl(FunctionDecl *D) {
64 IndexCtx.handleFunction(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000065 handleDeclarator(D);
Argyrios Kyrtzidis8818d452012-01-23 16:58:36 +000066
67 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
68 // Constructor initializers.
69 for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
70 E = Ctor->init_end();
71 I != E; ++I) {
72 CXXCtorInitializer *Init = *I;
73 if (Init->isWritten()) {
74 IndexCtx.indexTypeSourceInfo(Init->getTypeSourceInfo(), D);
75 if (const FieldDecl *Member = Init->getAnyMember())
76 IndexCtx.handleReference(Member, Init->getMemberLocation(), D, D);
77 IndexCtx.indexBody(Init->getInit(), D, D);
78 }
79 }
80 }
81
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000082 if (D->isThisDeclarationADefinition()) {
83 const Stmt *Body = D->getBody();
84 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000085 IndexCtx.indexBody(Body, D, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000086 }
87 }
88 return true;
89 }
90
91 bool VisitVarDecl(VarDecl *D) {
92 IndexCtx.handleVar(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000093 handleDeclarator(D);
94 IndexCtx.indexBody(D->getInit(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000095 return true;
96 }
97
98 bool VisitFieldDecl(FieldDecl *D) {
99 IndexCtx.handleField(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000100 handleDeclarator(D);
101 if (D->isBitField())
102 IndexCtx.indexBody(D->getBitWidth(), D);
103 else if (D->hasInClassInitializer())
104 IndexCtx.indexBody(D->getInClassInitializer(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000105 return true;
106 }
107
108 bool VisitEnumConstantDecl(EnumConstantDecl *D) {
109 IndexCtx.handleEnumerator(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000110 IndexCtx.indexBody(D->getInitExpr(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000111 return true;
112 }
113
Argyrios Kyrtzidis754a5d12012-09-01 19:08:08 +0000114 bool VisitTypedefNameDecl(TypedefNameDecl *D) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000115 IndexCtx.handleTypedefName(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000116 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
117 return true;
118 }
119
120 bool VisitTagDecl(TagDecl *D) {
121 // Non-free standing tags are handled in indexTypeSourceInfo.
122 if (D->isFreeStanding())
123 IndexCtx.indexTagDecl(D);
124 return true;
125 }
126
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000127 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000128 IndexCtx.handleObjCInterface(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000129
Douglas Gregor375bb142011-12-27 22:43:10 +0000130 if (D->isThisDeclarationADefinition()) {
131 IndexCtx.indexTUDeclsInObjCContainer();
132 IndexCtx.indexDeclContext(D);
133 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000134 return true;
135 }
136
137 bool VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000138 IndexCtx.handleObjCProtocol(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000139
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000140 if (D->isThisDeclarationADefinition()) {
141 IndexCtx.indexTUDeclsInObjCContainer();
142 IndexCtx.indexDeclContext(D);
143 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000144 return true;
145 }
146
147 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000148 const ObjCInterfaceDecl *Class = D->getClassInterface();
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000149 if (!Class)
150 return true;
151
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000152 if (Class->isImplicitInterfaceDecl())
153 IndexCtx.handleObjCInterface(Class);
154
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000155 IndexCtx.handleObjCImplementation(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000156
157 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +0000158
159 // Index the ivars first to make sure the synthesized ivars are indexed
160 // before indexing the methods that can reference them.
161 for (ObjCImplementationDecl::ivar_iterator
162 IvarI = D->ivar_begin(),
163 IvarE = D->ivar_end(); IvarI != IvarE; ++IvarI) {
164 IndexCtx.indexDecl(*IvarI);
165 }
166 for (DeclContext::decl_iterator
167 I = D->decls_begin(), E = D->decls_end(); I != E; ++I) {
168 if (!isa<ObjCIvarDecl>(*I))
169 IndexCtx.indexDecl(*I);
170 }
171
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000172 return true;
173 }
174
175 bool VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000176 IndexCtx.handleObjCCategory(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000177
178 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000179 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000180 return true;
181 }
182
183 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000184 const ObjCCategoryDecl *Cat = D->getCategoryDecl();
185 if (!Cat)
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000186 return true;
187
188 IndexCtx.handleObjCCategoryImpl(D);
189
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000190 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000191 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000192 return true;
193 }
194
195 bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000196 // Methods associated with a property, even user-declared ones, are
197 // handled when we handle the property.
198 if (D->isSynthesized())
199 return true;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000200
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000201 handleObjCMethod(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000202 return true;
203 }
204
205 bool VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000206 if (ObjCMethodDecl *MD = D->getGetterMethodDecl())
207 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
208 handleObjCMethod(MD);
209 if (ObjCMethodDecl *MD = D->getSetterMethodDecl())
210 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
211 handleObjCMethod(MD);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000212 IndexCtx.handleObjCProperty(D);
213 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
214 return true;
215 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000216
217 bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
218 ObjCPropertyDecl *PD = D->getPropertyDecl();
219 IndexCtx.handleSynthesizedObjCProperty(D);
220
221 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
222 return true;
223 assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
224
225 if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
226 if (!IvarD->getSynthesize())
227 IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), 0,
228 D->getDeclContext());
229 }
230
231 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
232 if (MD->isSynthesized())
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000233 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation(),
234 D->getLexicalDeclContext());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000235 }
236 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
237 if (MD->isSynthesized())
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000238 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation(),
239 D->getLexicalDeclContext());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000240 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000241 return true;
242 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000243
Argyrios Kyrtzidis68478b02011-12-07 05:52:06 +0000244 bool VisitNamespaceDecl(NamespaceDecl *D) {
245 IndexCtx.handleNamespace(D);
246 IndexCtx.indexDeclContext(D);
247 return true;
248 }
249
Argyrios Kyrtzidis911d7172012-02-10 20:10:48 +0000250 bool VisitUsingDecl(UsingDecl *D) {
251 // FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
252 // we should do better.
253
254 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
255 for (UsingDecl::shadow_iterator
256 I = D->shadow_begin(), E = D->shadow_end(); I != E; ++I) {
257 IndexCtx.handleReference((*I)->getUnderlyingDecl(), D->getLocation(),
258 D, D->getLexicalDeclContext());
259 }
260 return true;
261 }
262
263 bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
264 // FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
265 // we should do better.
266
267 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
268 IndexCtx.handleReference(D->getNominatedNamespaceAsWritten(),
269 D->getLocation(), D, D->getLexicalDeclContext());
270 return true;
271 }
272
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000273 bool VisitClassTemplateDecl(ClassTemplateDecl *D) {
274 IndexCtx.handleClassTemplate(D);
275 if (D->isThisDeclarationADefinition())
276 IndexCtx.indexDeclContext(D->getTemplatedDecl());
277 return true;
278 }
279
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000280 bool VisitClassTemplateSpecializationDecl(
281 ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +0000282 // FIXME: Notify subsequent callbacks if info comes from implicit
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000283 // instantiation.
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +0000284 if (D->isThisDeclarationADefinition() &&
285 (IndexCtx.shouldIndexImplicitTemplateInsts() ||
286 !IndexCtx.isTemplateImplicitInstantiation(D)))
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000287 IndexCtx.indexTagDecl(D);
288 return true;
289 }
290
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000291 bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
292 IndexCtx.handleFunctionTemplate(D);
293 FunctionDecl *FD = D->getTemplatedDecl();
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000294 handleDeclarator(FD, D);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000295 if (FD->isThisDeclarationADefinition()) {
296 const Stmt *Body = FD->getBody();
297 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000298 IndexCtx.indexBody(Body, D, FD);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000299 }
300 }
301 return true;
302 }
303
304 bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
305 IndexCtx.handleTypeAliasTemplate(D);
306 IndexCtx.indexTypeSourceInfo(D->getTemplatedDecl()->getTypeSourceInfo(), D);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000307 return true;
308 }
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +0000309
310 bool VisitImportDecl(ImportDecl *D) {
311 Module *Imported = D->getImportedModule();
312 if (Imported)
313 IndexCtx.importedModule(D->getLocation(), Imported->getFullModuleName(),
314 /*isIncludeDirective=*/false, Imported);
315 return true;
316 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000317};
318
319} // anonymous namespace
320
321void IndexingContext::indexDecl(const Decl *D) {
Argyrios Kyrtzidisd0890082012-02-07 22:46:16 +0000322 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
323 return;
324
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000325 bool Handled = IndexingDeclVisitor(*this).Visit(const_cast<Decl*>(D));
326 if (!Handled && isa<DeclContext>(D))
327 indexDeclContext(cast<DeclContext>(D));
328}
329
330void IndexingContext::indexDeclContext(const DeclContext *DC) {
331 for (DeclContext::decl_iterator
332 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
333 indexDecl(*I);
334 }
335}
336
Argyrios Kyrtzidis3bed3d12012-09-10 22:58:04 +0000337void IndexingContext::indexTopLevelDecl(const Decl *D) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000338 if (isNotFromSourceFile(D->getLocation()))
339 return;
340
341 if (isa<ObjCMethodDecl>(D))
342 return; // Wait for the objc container.
343
344 indexDecl(D);
345}
346
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000347void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000348 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
349 indexTopLevelDecl(*I);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000350}
351
352void IndexingContext::indexTUDeclsInObjCContainer() {
Argyrios Kyrtzidis30a28052012-03-23 23:24:18 +0000353 while (!TUDeclsInObjCContainer.empty()) {
354 DeclGroupRef DG = TUDeclsInObjCContainer.front();
355 TUDeclsInObjCContainer.pop_front();
356 indexDeclGroupRef(DG);
357 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000358}