blob: 3f2c8b57101ad96fc53b086b82ae9adee26cc789 [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"
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
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000026 void handleDeclarator(DeclaratorDecl *D, const NamedDecl *Parent = 0) {
27 if (!Parent) Parent = D;
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +000028
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +000029 if (!IndexCtx.shouldIndexFunctionLocalSymbols()) {
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +000030 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 }
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000042 }
43
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +000044 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
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000062 bool VisitFunctionDecl(FunctionDecl *D) {
63 IndexCtx.handleFunction(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000064 handleDeclarator(D);
Argyrios Kyrtzidis8818d452012-01-23 16:58:36 +000065
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
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000081 if (D->isThisDeclarationADefinition()) {
82 const Stmt *Body = D->getBody();
83 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000084 IndexCtx.indexBody(Body, D, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000085 }
86 }
87 return true;
88 }
89
90 bool VisitVarDecl(VarDecl *D) {
91 IndexCtx.handleVar(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000092 handleDeclarator(D);
93 IndexCtx.indexBody(D->getInit(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000094 return true;
95 }
96
97 bool VisitFieldDecl(FieldDecl *D) {
98 IndexCtx.handleField(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000099 handleDeclarator(D);
100 if (D->isBitField())
101 IndexCtx.indexBody(D->getBitWidth(), D);
102 else if (D->hasInClassInitializer())
103 IndexCtx.indexBody(D->getInClassInitializer(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000104 return true;
105 }
106
107 bool VisitEnumConstantDecl(EnumConstantDecl *D) {
108 IndexCtx.handleEnumerator(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000109 IndexCtx.indexBody(D->getInitExpr(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000110 return true;
111 }
112
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000113 bool VisitTypedefDecl(TypedefNameDecl *D) {
114 IndexCtx.handleTypedefName(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000115 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
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000126 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000127 IndexCtx.handleObjCInterface(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000128
Douglas Gregor375bb142011-12-27 22:43:10 +0000129 if (D->isThisDeclarationADefinition()) {
130 IndexCtx.indexTUDeclsInObjCContainer();
131 IndexCtx.indexDeclContext(D);
132 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000133 return true;
134 }
135
136 bool VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000137 IndexCtx.handleObjCProtocol(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000138
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000139 if (D->isThisDeclarationADefinition()) {
140 IndexCtx.indexTUDeclsInObjCContainer();
141 IndexCtx.indexDeclContext(D);
142 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000143 return true;
144 }
145
146 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000147 const ObjCInterfaceDecl *Class = D->getClassInterface();
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000148 if (!Class)
149 return true;
150
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000151 if (Class->isImplicitInterfaceDecl())
152 IndexCtx.handleObjCInterface(Class);
153
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000154 IndexCtx.handleObjCImplementation(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000155
156 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000157 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000158 return true;
159 }
160
161 bool VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000162 IndexCtx.handleObjCCategory(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000163
164 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000165 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000166 return true;
167 }
168
169 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000170 const ObjCCategoryDecl *Cat = D->getCategoryDecl();
171 if (!Cat)
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000172 return true;
173
174 IndexCtx.handleObjCCategoryImpl(D);
175
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000176 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000177 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000178 return true;
179 }
180
181 bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000182 // Methods associated with a property, even user-declared ones, are
183 // handled when we handle the property.
184 if (D->isSynthesized())
185 return true;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000186
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000187 handleObjCMethod(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000188 return true;
189 }
190
191 bool VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000192 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);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000198 IndexCtx.handleObjCProperty(D);
199 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
200 return true;
201 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000202
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())
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000219 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation(),
220 D->getLexicalDeclContext());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000221 }
222 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
223 if (MD->isSynthesized())
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000224 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation(),
225 D->getLexicalDeclContext());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000226 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000227 return true;
228 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000229
Argyrios Kyrtzidis68478b02011-12-07 05:52:06 +0000230 bool VisitNamespaceDecl(NamespaceDecl *D) {
231 IndexCtx.handleNamespace(D);
232 IndexCtx.indexDeclContext(D);
233 return true;
234 }
235
Argyrios Kyrtzidis911d7172012-02-10 20:10:48 +0000236 bool VisitUsingDecl(UsingDecl *D) {
237 // FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
238 // we should do better.
239
240 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
241 for (UsingDecl::shadow_iterator
242 I = D->shadow_begin(), E = D->shadow_end(); I != E; ++I) {
243 IndexCtx.handleReference((*I)->getUnderlyingDecl(), D->getLocation(),
244 D, D->getLexicalDeclContext());
245 }
246 return true;
247 }
248
249 bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
250 // FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
251 // we should do better.
252
253 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
254 IndexCtx.handleReference(D->getNominatedNamespaceAsWritten(),
255 D->getLocation(), D, D->getLexicalDeclContext());
256 return true;
257 }
258
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000259 bool VisitClassTemplateDecl(ClassTemplateDecl *D) {
260 IndexCtx.handleClassTemplate(D);
261 if (D->isThisDeclarationADefinition())
262 IndexCtx.indexDeclContext(D->getTemplatedDecl());
263 return true;
264 }
265
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000266 bool VisitClassTemplateSpecializationDecl(
267 ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +0000268 // FIXME: Notify subsequent callbacks if info comes from implicit
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000269 // instantiation.
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +0000270 if (D->isThisDeclarationADefinition() &&
271 (IndexCtx.shouldIndexImplicitTemplateInsts() ||
272 !IndexCtx.isTemplateImplicitInstantiation(D)))
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000273 IndexCtx.indexTagDecl(D);
274 return true;
275 }
276
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000277 bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
278 IndexCtx.handleFunctionTemplate(D);
279 FunctionDecl *FD = D->getTemplatedDecl();
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000280 handleDeclarator(FD, D);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000281 if (FD->isThisDeclarationADefinition()) {
282 const Stmt *Body = FD->getBody();
283 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000284 IndexCtx.indexBody(Body, D, FD);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000285 }
286 }
287 return true;
288 }
289
290 bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
291 IndexCtx.handleTypeAliasTemplate(D);
292 IndexCtx.indexTypeSourceInfo(D->getTemplatedDecl()->getTypeSourceInfo(), D);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000293 return true;
294 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000295};
296
297} // anonymous namespace
298
299void IndexingContext::indexDecl(const Decl *D) {
Argyrios Kyrtzidisd0890082012-02-07 22:46:16 +0000300 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
301 return;
302
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000303 bool Handled = IndexingDeclVisitor(*this).Visit(const_cast<Decl*>(D));
304 if (!Handled && isa<DeclContext>(D))
305 indexDeclContext(cast<DeclContext>(D));
306}
307
308void IndexingContext::indexDeclContext(const DeclContext *DC) {
309 for (DeclContext::decl_iterator
310 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
311 indexDecl(*I);
312 }
313}
314
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000315void IndexingContext::indexTopLevelDecl(Decl *D) {
316 if (isNotFromSourceFile(D->getLocation()))
317 return;
318
319 if (isa<ObjCMethodDecl>(D))
320 return; // Wait for the objc container.
321
322 indexDecl(D);
323}
324
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000325void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000326 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
327 indexTopLevelDecl(*I);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000328}
329
330void IndexingContext::indexTUDeclsInObjCContainer() {
331 for (unsigned i = 0, e = TUDeclsInObjCContainer.size(); i != e; ++i)
332 indexDeclGroupRef(TUDeclsInObjCContainer[i]);
333 TUDeclsInObjCContainer.clear();
334}