blob: 756001c3c124fd7956e30ef9fc833d88b47aacba [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
John McCall76da55d2013-04-16 07:28:30 +0000108 bool VisitMSPropertyDecl(const MSPropertyDecl *D) {
109 handleDeclarator(D);
110 return true;
111 }
112
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000113 bool VisitEnumConstantDecl(const EnumConstantDecl *D) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000114 IndexCtx.handleEnumerator(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000115 IndexCtx.indexBody(D->getInitExpr(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000116 return true;
117 }
118
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000119 bool VisitTypedefNameDecl(const TypedefNameDecl *D) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000120 IndexCtx.handleTypedefName(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000121 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
122 return true;
123 }
124
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000125 bool VisitTagDecl(const TagDecl *D) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000126 // Non-free standing tags are handled in indexTypeSourceInfo.
127 if (D->isFreeStanding())
128 IndexCtx.indexTagDecl(D);
129 return true;
130 }
131
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000132 bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000133 IndexCtx.handleObjCInterface(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000134
Douglas Gregor375bb142011-12-27 22:43:10 +0000135 if (D->isThisDeclarationADefinition()) {
136 IndexCtx.indexTUDeclsInObjCContainer();
137 IndexCtx.indexDeclContext(D);
138 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000139 return true;
140 }
141
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000142 bool VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000143 IndexCtx.handleObjCProtocol(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000144
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000145 if (D->isThisDeclarationADefinition()) {
146 IndexCtx.indexTUDeclsInObjCContainer();
147 IndexCtx.indexDeclContext(D);
148 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000149 return true;
150 }
151
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000152 bool VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000153 const ObjCInterfaceDecl *Class = D->getClassInterface();
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000154 if (!Class)
155 return true;
156
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000157 if (Class->isImplicitInterfaceDecl())
158 IndexCtx.handleObjCInterface(Class);
159
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000160 IndexCtx.handleObjCImplementation(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000161
162 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +0000163
164 // Index the ivars first to make sure the synthesized ivars are indexed
165 // before indexing the methods that can reference them.
166 for (ObjCImplementationDecl::ivar_iterator
167 IvarI = D->ivar_begin(),
168 IvarE = D->ivar_end(); IvarI != IvarE; ++IvarI) {
169 IndexCtx.indexDecl(*IvarI);
170 }
171 for (DeclContext::decl_iterator
172 I = D->decls_begin(), E = D->decls_end(); I != E; ++I) {
173 if (!isa<ObjCIvarDecl>(*I))
174 IndexCtx.indexDecl(*I);
175 }
176
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000177 return true;
178 }
179
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000180 bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000181 IndexCtx.handleObjCCategory(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000182
183 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000184 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000185 return true;
186 }
187
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000188 bool VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000189 const ObjCCategoryDecl *Cat = D->getCategoryDecl();
190 if (!Cat)
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000191 return true;
192
193 IndexCtx.handleObjCCategoryImpl(D);
194
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000195 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000196 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000197 return true;
198 }
199
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000200 bool VisitObjCMethodDecl(const ObjCMethodDecl *D) {
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000201 // Methods associated with a property, even user-declared ones, are
202 // handled when we handle the property.
Jordan Rose1e4691b2012-10-10 16:42:25 +0000203 if (D->isPropertyAccessor())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000204 return true;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000205
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000206 handleObjCMethod(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000207 return true;
208 }
209
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000210 bool VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000211 if (ObjCMethodDecl *MD = D->getGetterMethodDecl())
212 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
213 handleObjCMethod(MD);
214 if (ObjCMethodDecl *MD = D->getSetterMethodDecl())
215 if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
216 handleObjCMethod(MD);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000217 IndexCtx.handleObjCProperty(D);
218 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
219 return true;
220 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000221
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000222 bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000223 ObjCPropertyDecl *PD = D->getPropertyDecl();
224 IndexCtx.handleSynthesizedObjCProperty(D);
225
226 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
227 return true;
228 assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
229
230 if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
231 if (!IvarD->getSynthesize())
232 IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), 0,
233 D->getDeclContext());
234 }
235
236 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
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 }
241 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
Jordan Rose1e4691b2012-10-10 16:42:25 +0000242 if (MD->isPropertyAccessor())
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000243 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation(),
244 D->getLexicalDeclContext());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000245 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000246 return true;
247 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000248
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000249 bool VisitNamespaceDecl(const NamespaceDecl *D) {
Argyrios Kyrtzidis68478b02011-12-07 05:52:06 +0000250 IndexCtx.handleNamespace(D);
251 IndexCtx.indexDeclContext(D);
252 return true;
253 }
254
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000255 bool VisitUsingDecl(const UsingDecl *D) {
Argyrios Kyrtzidis911d7172012-02-10 20:10:48 +0000256 // FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
257 // we should do better.
258
259 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
260 for (UsingDecl::shadow_iterator
261 I = D->shadow_begin(), E = D->shadow_end(); I != E; ++I) {
262 IndexCtx.handleReference((*I)->getUnderlyingDecl(), D->getLocation(),
263 D, D->getLexicalDeclContext());
264 }
265 return true;
266 }
267
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000268 bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Argyrios Kyrtzidis911d7172012-02-10 20:10:48 +0000269 // FIXME: Parent for the following is CXIdxEntity_Unexposed with no USR,
270 // we should do better.
271
272 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), D);
273 IndexCtx.handleReference(D->getNominatedNamespaceAsWritten(),
274 D->getLocation(), D, D->getLexicalDeclContext());
275 return true;
276 }
277
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000278 bool VisitClassTemplateDecl(const ClassTemplateDecl *D) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000279 IndexCtx.handleClassTemplate(D);
280 if (D->isThisDeclarationADefinition())
281 IndexCtx.indexDeclContext(D->getTemplatedDecl());
282 return true;
283 }
284
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000285 bool VisitClassTemplateSpecializationDecl(const
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000286 ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +0000287 // FIXME: Notify subsequent callbacks if info comes from implicit
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000288 // instantiation.
Argyrios Kyrtzidis58d2dbe2012-02-14 22:23:11 +0000289 if (D->isThisDeclarationADefinition() &&
290 (IndexCtx.shouldIndexImplicitTemplateInsts() ||
291 !IndexCtx.isTemplateImplicitInstantiation(D)))
Argyrios Kyrtzidis6d968362012-02-10 20:10:44 +0000292 IndexCtx.indexTagDecl(D);
293 return true;
294 }
295
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000296 bool VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000297 IndexCtx.handleFunctionTemplate(D);
298 FunctionDecl *FD = D->getTemplatedDecl();
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000299 handleDeclarator(FD, D);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000300 if (FD->isThisDeclarationADefinition()) {
301 const Stmt *Body = FD->getBody();
302 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000303 IndexCtx.indexBody(Body, D, FD);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000304 }
305 }
306 return true;
307 }
308
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000309 bool VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000310 IndexCtx.handleTypeAliasTemplate(D);
311 IndexCtx.indexTypeSourceInfo(D->getTemplatedDecl()->getTypeSourceInfo(), D);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000312 return true;
313 }
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +0000314
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000315 bool VisitImportDecl(const ImportDecl *D) {
Argyrios Kyrtzidis37f2f522012-10-03 21:05:44 +0000316 IndexCtx.importedModule(D);
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +0000317 return true;
318 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000319};
320
321} // anonymous namespace
322
323void IndexingContext::indexDecl(const Decl *D) {
Argyrios Kyrtzidisd0890082012-02-07 22:46:16 +0000324 if (D->isImplicit() && shouldIgnoreIfImplicit(D))
325 return;
326
Dmitri Gribenko361d79c2013-02-03 13:42:20 +0000327 bool Handled = IndexingDeclVisitor(*this).Visit(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000328 if (!Handled && isa<DeclContext>(D))
329 indexDeclContext(cast<DeclContext>(D));
330}
331
332void IndexingContext::indexDeclContext(const DeclContext *DC) {
333 for (DeclContext::decl_iterator
334 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
335 indexDecl(*I);
336 }
337}
338
Argyrios Kyrtzidis3bed3d12012-09-10 22:58:04 +0000339void IndexingContext::indexTopLevelDecl(const Decl *D) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000340 if (isNotFromSourceFile(D->getLocation()))
341 return;
342
343 if (isa<ObjCMethodDecl>(D))
344 return; // Wait for the objc container.
345
346 indexDecl(D);
347}
348
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000349void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000350 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
351 indexTopLevelDecl(*I);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000352}
353
354void IndexingContext::indexTUDeclsInObjCContainer() {
Argyrios Kyrtzidis30a28052012-03-23 23:24:18 +0000355 while (!TUDeclsInObjCContainer.empty()) {
356 DeclGroupRef DG = TUDeclsInObjCContainer.front();
357 TUDeclsInObjCContainer.pop_front();
358 indexDeclGroupRef(DG);
359 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000360}