blob: 0d6dacb77528cf8781a093ce8a868b2021182b83 [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;
28 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), Parent);
29 IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
30 }
31
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000032 bool VisitFunctionDecl(FunctionDecl *D) {
33 IndexCtx.handleFunction(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000034 handleDeclarator(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000035 if (D->isThisDeclarationADefinition()) {
36 const Stmt *Body = D->getBody();
37 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000038 IndexCtx.indexBody(Body, D, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000039 }
40 }
41 return true;
42 }
43
44 bool VisitVarDecl(VarDecl *D) {
45 IndexCtx.handleVar(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000046 handleDeclarator(D);
47 IndexCtx.indexBody(D->getInit(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000048 return true;
49 }
50
51 bool VisitFieldDecl(FieldDecl *D) {
52 IndexCtx.handleField(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000053 handleDeclarator(D);
54 if (D->isBitField())
55 IndexCtx.indexBody(D->getBitWidth(), D);
56 else if (D->hasInClassInitializer())
57 IndexCtx.indexBody(D->getInClassInitializer(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000058 return true;
59 }
60
61 bool VisitEnumConstantDecl(EnumConstantDecl *D) {
62 IndexCtx.handleEnumerator(D);
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +000063 IndexCtx.indexBody(D->getInitExpr(), D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000064 return true;
65 }
66
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +000067 bool VisitTypedefDecl(TypedefNameDecl *D) {
68 IndexCtx.handleTypedefName(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000069 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
70 return true;
71 }
72
73 bool VisitTagDecl(TagDecl *D) {
74 // Non-free standing tags are handled in indexTypeSourceInfo.
75 if (D->isFreeStanding())
76 IndexCtx.indexTagDecl(D);
77 return true;
78 }
79
80 bool VisitObjCClassDecl(ObjCClassDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000081 IndexCtx.handleObjCClass(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000082 return true;
83 }
84
85 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
86 ObjCForwardProtocolDecl::protocol_loc_iterator LI = D->protocol_loc_begin();
87 for (ObjCForwardProtocolDecl::protocol_iterator
88 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I, ++LI) {
89 SourceLocation Loc = *LI;
90 ObjCProtocolDecl *PD = *I;
91
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000092 bool isRedeclaration = PD->getLocation() != Loc;
93 IndexCtx.handleObjCForwardProtocol(PD, Loc, isRedeclaration);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000094 }
95 return true;
96 }
97
98 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000099 // Forward decls are handled at VisitObjCClassDecl.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000100 if (D->isForwardDecl())
101 return true;
102
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000103 IndexCtx.handleObjCInterface(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000104
105 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000106 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000107 return true;
108 }
109
110 bool VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000111 // Forward decls are handled at VisitObjCForwardProtocolDecl.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000112 if (D->isForwardDecl())
113 return true;
114
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000115 IndexCtx.handleObjCProtocol(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000116
117 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000118 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000119 return true;
120 }
121
122 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000123 const ObjCInterfaceDecl *Class = D->getClassInterface();
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000124 if (!Class)
125 return true;
126
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000127 if (Class->isImplicitInterfaceDecl())
128 IndexCtx.handleObjCInterface(Class);
129
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000130 IndexCtx.handleObjCImplementation(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000131
132 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000133 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000134 return true;
135 }
136
137 bool VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000138 IndexCtx.handleObjCCategory(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000139
140 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000141 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000142 return true;
143 }
144
145 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000146 const ObjCCategoryDecl *Cat = D->getCategoryDecl();
147 if (!Cat)
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000148 return true;
149
150 IndexCtx.handleObjCCategoryImpl(D);
151
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000152 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000153 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000154 return true;
155 }
156
157 bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
158 IndexCtx.handleObjCMethod(D);
159 IndexCtx.indexTypeSourceInfo(D->getResultTypeSourceInfo(), D);
160 for (ObjCMethodDecl::param_iterator
161 I = D->param_begin(), E = D->param_end(); I != E; ++I)
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000162 handleDeclarator(*I, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000163
164 if (D->isThisDeclarationADefinition()) {
165 const Stmt *Body = D->getBody();
166 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000167 IndexCtx.indexBody(Body, D, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000168 }
169 }
170 return true;
171 }
172
173 bool VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
174 IndexCtx.handleObjCProperty(D);
175 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
176 return true;
177 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000178
179 bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
180 ObjCPropertyDecl *PD = D->getPropertyDecl();
181 IndexCtx.handleSynthesizedObjCProperty(D);
182
183 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
184 return true;
185 assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
186
187 if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
188 if (!IvarD->getSynthesize())
189 IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), 0,
190 D->getDeclContext());
191 }
192
193 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
194 if (MD->isSynthesized())
195 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
196 }
197 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
198 if (MD->isSynthesized())
199 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
200 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000201 return true;
202 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000203
Argyrios Kyrtzidis68478b02011-12-07 05:52:06 +0000204 bool VisitNamespaceDecl(NamespaceDecl *D) {
205 IndexCtx.handleNamespace(D);
206 IndexCtx.indexDeclContext(D);
207 return true;
208 }
209
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000210 bool VisitClassTemplateDecl(ClassTemplateDecl *D) {
211 IndexCtx.handleClassTemplate(D);
212 if (D->isThisDeclarationADefinition())
213 IndexCtx.indexDeclContext(D->getTemplatedDecl());
214 return true;
215 }
216
217 bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
218 IndexCtx.handleFunctionTemplate(D);
219 FunctionDecl *FD = D->getTemplatedDecl();
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000220 handleDeclarator(FD, D);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000221 if (FD->isThisDeclarationADefinition()) {
222 const Stmt *Body = FD->getBody();
223 if (Body) {
Argyrios Kyrtzidise422e452011-12-13 18:47:41 +0000224 IndexCtx.indexBody(Body, D, FD);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000225 }
226 }
227 return true;
228 }
229
230 bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
231 IndexCtx.handleTypeAliasTemplate(D);
232 IndexCtx.indexTypeSourceInfo(D->getTemplatedDecl()->getTypeSourceInfo(), D);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000233 return true;
234 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000235};
236
237} // anonymous namespace
238
239void IndexingContext::indexDecl(const Decl *D) {
240 bool Handled = IndexingDeclVisitor(*this).Visit(const_cast<Decl*>(D));
241 if (!Handled && isa<DeclContext>(D))
242 indexDeclContext(cast<DeclContext>(D));
243}
244
245void IndexingContext::indexDeclContext(const DeclContext *DC) {
246 for (DeclContext::decl_iterator
247 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
248 indexDecl(*I);
249 }
250}
251
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000252void IndexingContext::indexTopLevelDecl(Decl *D) {
253 if (isNotFromSourceFile(D->getLocation()))
254 return;
255
256 if (isa<ObjCMethodDecl>(D))
257 return; // Wait for the objc container.
258
259 indexDecl(D);
260}
261
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000262void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000263 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
264 indexTopLevelDecl(*I);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000265}
266
267void IndexingContext::indexTUDeclsInObjCContainer() {
268 for (unsigned i = 0, e = TUDeclsInObjCContainer.size(); i != e; ++i)
269 indexDeclGroupRef(TUDeclsInObjCContainer[i]);
270 TUDeclsInObjCContainer.clear();
271}