blob: c6080df5928f523548623dfdbb3bb84103473e8e [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
29 if (!IndexCtx.indexFunctionLocalSymbols()) {
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 }
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000042 }
43
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000044 bool VisitFunctionDecl(FunctionDecl *D) {
45 IndexCtx.handleFunction(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000046 handleDeclarator(D);
Argyrios Kyrtzidis8818d452012-01-23 16:58:36 +000047
48 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
49 // Constructor initializers.
50 for (CXXConstructorDecl::init_iterator I = Ctor->init_begin(),
51 E = Ctor->init_end();
52 I != E; ++I) {
53 CXXCtorInitializer *Init = *I;
54 if (Init->isWritten()) {
55 IndexCtx.indexTypeSourceInfo(Init->getTypeSourceInfo(), D);
56 if (const FieldDecl *Member = Init->getAnyMember())
57 IndexCtx.handleReference(Member, Init->getMemberLocation(), D, D);
58 IndexCtx.indexBody(Init->getInit(), D, D);
59 }
60 }
61 }
62
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000063 if (D->isThisDeclarationADefinition()) {
64 const Stmt *Body = D->getBody();
65 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000066 IndexCtx.indexBody(Body, D, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000067 }
68 }
69 return true;
70 }
71
72 bool VisitVarDecl(VarDecl *D) {
73 IndexCtx.handleVar(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000074 handleDeclarator(D);
75 IndexCtx.indexBody(D->getInit(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000076 return true;
77 }
78
79 bool VisitFieldDecl(FieldDecl *D) {
80 IndexCtx.handleField(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000081 handleDeclarator(D);
82 if (D->isBitField())
83 IndexCtx.indexBody(D->getBitWidth(), D);
84 else if (D->hasInClassInitializer())
85 IndexCtx.indexBody(D->getInClassInitializer(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000086 return true;
87 }
88
89 bool VisitEnumConstantDecl(EnumConstantDecl *D) {
90 IndexCtx.handleEnumerator(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000091 IndexCtx.indexBody(D->getInitExpr(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000092 return true;
93 }
94
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +000095 bool VisitTypedefDecl(TypedefNameDecl *D) {
96 IndexCtx.handleTypedefName(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000097 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
98 return true;
99 }
100
101 bool VisitTagDecl(TagDecl *D) {
102 // Non-free standing tags are handled in indexTypeSourceInfo.
103 if (D->isFreeStanding())
104 IndexCtx.indexTagDecl(D);
105 return true;
106 }
107
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000108 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000109 IndexCtx.handleObjCInterface(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000110
Douglas Gregor375bb142011-12-27 22:43:10 +0000111 if (D->isThisDeclarationADefinition()) {
112 IndexCtx.indexTUDeclsInObjCContainer();
113 IndexCtx.indexDeclContext(D);
114 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000115 return true;
116 }
117
118 bool VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000119 IndexCtx.handleObjCProtocol(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000120
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000121 if (D->isThisDeclarationADefinition()) {
122 IndexCtx.indexTUDeclsInObjCContainer();
123 IndexCtx.indexDeclContext(D);
124 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000125 return true;
126 }
127
128 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000129 const ObjCInterfaceDecl *Class = D->getClassInterface();
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000130 if (!Class)
131 return true;
132
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000133 if (Class->isImplicitInterfaceDecl())
134 IndexCtx.handleObjCInterface(Class);
135
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000136 IndexCtx.handleObjCImplementation(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000137
138 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000139 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000140 return true;
141 }
142
143 bool VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000144 IndexCtx.handleObjCCategory(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000145
146 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000147 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000148 return true;
149 }
150
151 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000152 const ObjCCategoryDecl *Cat = D->getCategoryDecl();
153 if (!Cat)
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000154 return true;
155
156 IndexCtx.handleObjCCategoryImpl(D);
157
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000158 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000159 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000160 return true;
161 }
162
163 bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
164 IndexCtx.handleObjCMethod(D);
165 IndexCtx.indexTypeSourceInfo(D->getResultTypeSourceInfo(), D);
166 for (ObjCMethodDecl::param_iterator
167 I = D->param_begin(), E = D->param_end(); I != E; ++I)
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000168 handleDeclarator(*I, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000169
170 if (D->isThisDeclarationADefinition()) {
171 const Stmt *Body = D->getBody();
172 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000173 IndexCtx.indexBody(Body, D, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000174 }
175 }
176 return true;
177 }
178
179 bool VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
180 IndexCtx.handleObjCProperty(D);
181 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
182 return true;
183 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000184
185 bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
186 ObjCPropertyDecl *PD = D->getPropertyDecl();
187 IndexCtx.handleSynthesizedObjCProperty(D);
188
189 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
190 return true;
191 assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
192
193 if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
194 if (!IvarD->getSynthesize())
195 IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), 0,
196 D->getDeclContext());
197 }
198
199 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
200 if (MD->isSynthesized())
201 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
202 }
203 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
204 if (MD->isSynthesized())
205 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
206 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000207 return true;
208 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000209
Argyrios Kyrtzidis68478b02011-12-07 05:52:06 +0000210 bool VisitNamespaceDecl(NamespaceDecl *D) {
211 IndexCtx.handleNamespace(D);
212 IndexCtx.indexDeclContext(D);
213 return true;
214 }
215
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000216 bool VisitClassTemplateDecl(ClassTemplateDecl *D) {
217 IndexCtx.handleClassTemplate(D);
218 if (D->isThisDeclarationADefinition())
219 IndexCtx.indexDeclContext(D->getTemplatedDecl());
220 return true;
221 }
222
223 bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
224 IndexCtx.handleFunctionTemplate(D);
225 FunctionDecl *FD = D->getTemplatedDecl();
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000226 handleDeclarator(FD, D);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000227 if (FD->isThisDeclarationADefinition()) {
228 const Stmt *Body = FD->getBody();
229 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000230 IndexCtx.indexBody(Body, D, FD);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000231 }
232 }
233 return true;
234 }
235
236 bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
237 IndexCtx.handleTypeAliasTemplate(D);
238 IndexCtx.indexTypeSourceInfo(D->getTemplatedDecl()->getTypeSourceInfo(), D);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000239 return true;
240 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000241};
242
243} // anonymous namespace
244
245void IndexingContext::indexDecl(const Decl *D) {
246 bool Handled = IndexingDeclVisitor(*this).Visit(const_cast<Decl*>(D));
247 if (!Handled && isa<DeclContext>(D))
248 indexDeclContext(cast<DeclContext>(D));
249}
250
251void IndexingContext::indexDeclContext(const DeclContext *DC) {
252 for (DeclContext::decl_iterator
253 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
254 indexDecl(*I);
255 }
256}
257
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000258void IndexingContext::indexTopLevelDecl(Decl *D) {
259 if (isNotFromSourceFile(D->getLocation()))
260 return;
261
262 if (isa<ObjCMethodDecl>(D))
263 return; // Wait for the objc container.
264
265 indexDecl(D);
266}
267
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000268void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000269 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
270 indexTopLevelDecl(*I);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000271}
272
273void IndexingContext::indexTUDeclsInObjCContainer() {
274 for (unsigned i = 0, e = TUDeclsInObjCContainer.size(); i != e; ++i)
275 indexDeclGroupRef(TUDeclsInObjCContainer[i]);
276 TUDeclsInObjCContainer.clear();
277}