blob: d7fb959b1ede9fbc5644e51e6fc9a9710194d85c [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"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000011#include "clang/AST/DeclVisitor.h"
12
13using namespace clang;
14using namespace cxindex;
15
16namespace {
17
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000018class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000019 IndexingContext &IndexCtx;
20
21public:
22 explicit IndexingDeclVisitor(IndexingContext &indexCtx)
23 : IndexCtx(indexCtx) { }
24
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000025 void handleDeclarator(const DeclaratorDecl *D, const NamedDecl *Parent = 0) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000026 if (!Parent) Parent = D;
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +000027
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +000028 if (!IndexCtx.shouldIndexFunctionLocalSymbols()) {
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +000029 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent);
30 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
31 } else {
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000032 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +000033 IndexCtx.handleVar(Parm);
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000034 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
35 for (FunctionDecl::param_const_iterator PI = FD->param_begin(),
36 PE = FD->param_end();
37 PI != PE; ++PI) {
Argyrios Kyrtzidisdb4d7a52012-01-14 02:05:51 +000038 IndexCtx.handleVar(*PI);
39 }
40 }
41 }
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000042 }
43
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000044 void handleObjCMethod(const ObjCMethodDecl *D) {
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +000045 IndexCtx.handleObjCMethod(D);
46 if (D->isImplicit())
47 return;
48
49 IndexCtx.indexTypeSourceInfo(D->getResultTypeSourceInfo(), D);
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000050 for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
51 E = D->param_end();
52 I != E; ++I)
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +000053 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000063 bool VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000064 IndexCtx.handleFunction(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000065 handleDeclarator(D);
Argyrios Kyrtzidis8818d452012-01-23 16:58:36 +000066
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000067 if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
Argyrios Kyrtzidis8818d452012-01-23 16:58:36 +000068 // Constructor initializers.
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000069 for (CXXConstructorDecl::init_const_iterator I = Ctor->init_begin(),
70 E = Ctor->init_end();
Argyrios Kyrtzidis8818d452012-01-23 16:58:36 +000071 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000091 bool VisitVarDecl(const VarDecl *D) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000092 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +000098 bool VisitFieldDecl(const FieldDecl *D) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000099 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 }
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000107
108 bool VisitEnumConstantDecl(const EnumConstantDecl *D) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000109 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000114 bool VisitTypedefNameDecl(const 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000120 bool VisitTagDecl(const TagDecl *D) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000121 // Non-free standing tags are handled in indexTypeSourceInfo.
122 if (D->isFreeStanding())
123 IndexCtx.indexTagDecl(D);
124 return true;
125 }
126
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000127 bool VisitObjCInterfaceDecl(const 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000137 bool VisitObjCProtocolDecl(const 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000147 bool VisitObjCImplementationDecl(const 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000175 bool VisitObjCCategoryDecl(const 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000183 bool VisitObjCCategoryImplDecl(const 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000195 bool VisitObjCMethodDecl(const 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.
Jordan Rose1e4691b2012-10-10 16:42:25 +0000198 if (D->isPropertyAccessor())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000199 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000205 bool VisitObjCPropertyDecl(const 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000217 bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000218 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()) {
Jordan Rose1e4691b2012-10-10 16:42:25 +0000232 if (MD->isPropertyAccessor())
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()) {
Jordan Rose1e4691b2012-10-10 16:42:25 +0000237 if (MD->isPropertyAccessor())
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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000244 bool VisitNamespaceDecl(const NamespaceDecl *D) {
Argyrios Kyrtzidis68478b02011-12-07 05:52:06 +0000245 IndexCtx.handleNamespace(D);
246 IndexCtx.indexDeclContext(D);
247 return true;
248 }
249
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000250 bool VisitUsingDecl(const UsingDecl *D) {
Argyrios Kyrtzidis911d7172012-02-10 20:10:48 +0000251 // 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000263 bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Argyrios Kyrtzidis911d7172012-02-10 20:10:48 +0000264 // 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000273 bool VisitClassTemplateDecl(const ClassTemplateDecl *D) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000274 IndexCtx.handleClassTemplate(D);
275 if (D->isThisDeclarationADefinition())
276 IndexCtx.indexDeclContext(D->getTemplatedDecl());
277 return true;
278 }
279
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000280 bool VisitClassTemplateSpecializationDecl(const
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000281 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000291 bool VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000292 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000304 bool VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000305 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
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000310 bool VisitImportDecl(const ImportDecl *D) {
Argyrios Kyrtzidis37f2f522012-10-03 21:05:44 +0000311 IndexCtx.importedModule(D);
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +0000312 return true;
313 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000314};
315
316} // anonymous namespace
317
318void IndexingContext::indexDecl(const Decl *D) {
Argyrios Kyrtzidisd0890082012-02-07 22:46:16 +0000319 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
320 return;
321
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000322 bool Handled = IndexingDeclVisitor(*this).Visit(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000323 if (!Handled && isa<DeclContext>(D))
324 indexDeclContext(cast<DeclContext>(D));
325}
326
327void IndexingContext::indexDeclContext(const DeclContext *DC) {
328 for (DeclContext::decl_iterator
329 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
330 indexDecl(*I);
331 }
332}
333
Argyrios Kyrtzidis3bed3d12012-09-10 22:58:04 +0000334void IndexingContext::indexTopLevelDecl(const Decl *D) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000335 if (isNotFromSourceFile(D->getLocation()))
336 return;
337
338 if (isa<ObjCMethodDecl>(D))
339 return; // Wait for the objc container.
340
341 indexDecl(D);
342}
343
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000344void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000345 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
346 indexTopLevelDecl(*I);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000347}
348
349void IndexingContext::indexTUDeclsInObjCContainer() {
Argyrios Kyrtzidis30a28052012-03-23 23:24:18 +0000350 while (!TUDeclsInObjCContainer.empty()) {
351 DeclGroupRef DG = TUDeclsInObjCContainer.front();
352 TUDeclsInObjCContainer.pop_front();
353 indexDeclGroupRef(DG);
354 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000355}