blob: 84c1134501143c8598c32996fd20b2b0a9f1a4dc [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
26 bool VisitFunctionDecl(FunctionDecl *D) {
27 IndexCtx.handleFunction(D);
28 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
29 if (D->isThisDeclarationADefinition()) {
30 const Stmt *Body = D->getBody();
31 if (Body) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000032 IndexCtx.indexBody(Body, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000033 }
34 }
35 return true;
36 }
37
38 bool VisitVarDecl(VarDecl *D) {
39 IndexCtx.handleVar(D);
40 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
41 return true;
42 }
43
44 bool VisitFieldDecl(FieldDecl *D) {
45 IndexCtx.handleField(D);
46 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
47 return true;
48 }
49
50 bool VisitEnumConstantDecl(EnumConstantDecl *D) {
51 IndexCtx.handleEnumerator(D);
52 return true;
53 }
54
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +000055 bool VisitTypedefDecl(TypedefNameDecl *D) {
56 IndexCtx.handleTypedefName(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000057 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
58 return true;
59 }
60
61 bool VisitTagDecl(TagDecl *D) {
62 // Non-free standing tags are handled in indexTypeSourceInfo.
63 if (D->isFreeStanding())
64 IndexCtx.indexTagDecl(D);
65 return true;
66 }
67
68 bool VisitObjCClassDecl(ObjCClassDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000069 IndexCtx.handleObjCClass(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000070 return true;
71 }
72
73 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
74 ObjCForwardProtocolDecl::protocol_loc_iterator LI = D->protocol_loc_begin();
75 for (ObjCForwardProtocolDecl::protocol_iterator
76 I = D->protocol_begin(), E = D->protocol_end(); I != E; ++I, ++LI) {
77 SourceLocation Loc = *LI;
78 ObjCProtocolDecl *PD = *I;
79
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000080 bool isRedeclaration = PD->getLocation() != Loc;
81 IndexCtx.handleObjCForwardProtocol(PD, Loc, isRedeclaration);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000082 }
83 return true;
84 }
85
86 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000087 // Forward decls are handled at VisitObjCClassDecl.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000088 if (D->isForwardDecl())
89 return true;
90
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000091 IndexCtx.handleObjCInterface(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000092
93 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000094 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000095 return true;
96 }
97
98 bool VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000099 // Forward decls are handled at VisitObjCForwardProtocolDecl.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000100 if (D->isForwardDecl())
101 return true;
102
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000103 IndexCtx.handleObjCProtocol(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 VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000111 const ObjCInterfaceDecl *Class = D->getClassInterface();
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000112 if (!Class)
113 return true;
114
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000115 if (Class->isImplicitInterfaceDecl())
116 IndexCtx.handleObjCInterface(Class);
117
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000118 IndexCtx.handleObjCImplementation(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000119
120 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000121 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000122 return true;
123 }
124
125 bool VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000126 IndexCtx.handleObjCCategory(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000127
128 IndexCtx.indexTUDeclsInObjCContainer();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000129 IndexCtx.indexDeclContext(D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000130 return true;
131 }
132
133 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000134 const ObjCCategoryDecl *Cat = D->getCategoryDecl();
135 if (!Cat)
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000136 return true;
137
138 IndexCtx.handleObjCCategoryImpl(D);
139
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000140 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 VisitObjCMethodDecl(ObjCMethodDecl *D) {
146 IndexCtx.handleObjCMethod(D);
147 IndexCtx.indexTypeSourceInfo(D->getResultTypeSourceInfo(), D);
148 for (ObjCMethodDecl::param_iterator
149 I = D->param_begin(), E = D->param_end(); I != E; ++I)
150 IndexCtx.indexTypeSourceInfo((*I)->getTypeSourceInfo(), D);
151
152 if (D->isThisDeclarationADefinition()) {
153 const Stmt *Body = D->getBody();
154 if (Body) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000155 IndexCtx.indexBody(Body, D);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000156 }
157 }
158 return true;
159 }
160
161 bool VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
162 IndexCtx.handleObjCProperty(D);
163 IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
164 return true;
165 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000166
167 bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
168 ObjCPropertyDecl *PD = D->getPropertyDecl();
169 IndexCtx.handleSynthesizedObjCProperty(D);
170
171 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
172 return true;
173 assert(D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize);
174
175 if (ObjCIvarDecl *IvarD = D->getPropertyIvarDecl()) {
176 if (!IvarD->getSynthesize())
177 IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), 0,
178 D->getDeclContext());
179 }
180
181 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) {
182 if (MD->isSynthesized())
183 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
184 }
185 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) {
186 if (MD->isSynthesized())
187 IndexCtx.handleSynthesizedObjCMethod(MD, D->getLocation());
188 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000189 return true;
190 }
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000191
Argyrios Kyrtzidis68478b02011-12-07 05:52:06 +0000192 bool VisitNamespaceDecl(NamespaceDecl *D) {
193 IndexCtx.handleNamespace(D);
194 IndexCtx.indexDeclContext(D);
195 return true;
196 }
197
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000198 bool VisitClassTemplateDecl(ClassTemplateDecl *D) {
199 IndexCtx.handleClassTemplate(D);
200 if (D->isThisDeclarationADefinition())
201 IndexCtx.indexDeclContext(D->getTemplatedDecl());
202 return true;
203 }
204
205 bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
206 IndexCtx.handleFunctionTemplate(D);
207 FunctionDecl *FD = D->getTemplatedDecl();
208 IndexCtx.indexTypeSourceInfo(FD->getTypeSourceInfo(), D);
209 if (FD->isThisDeclarationADefinition()) {
210 const Stmt *Body = FD->getBody();
211 if (Body) {
212 IndexCtx.indexBody(Body, FD);
213 }
214 }
215 return true;
216 }
217
218 bool VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
219 IndexCtx.handleTypeAliasTemplate(D);
220 IndexCtx.indexTypeSourceInfo(D->getTemplatedDecl()->getTypeSourceInfo(), D);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000221 return true;
222 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000223};
224
225} // anonymous namespace
226
227void IndexingContext::indexDecl(const Decl *D) {
228 bool Handled = IndexingDeclVisitor(*this).Visit(const_cast<Decl*>(D));
229 if (!Handled && isa<DeclContext>(D))
230 indexDeclContext(cast<DeclContext>(D));
231}
232
233void IndexingContext::indexDeclContext(const DeclContext *DC) {
234 for (DeclContext::decl_iterator
235 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
236 indexDecl(*I);
237 }
238}
239
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000240void IndexingContext::indexTopLevelDecl(Decl *D) {
241 if (isNotFromSourceFile(D->getLocation()))
242 return;
243
244 if (isa<ObjCMethodDecl>(D))
245 return; // Wait for the objc container.
246
247 indexDecl(D);
248}
249
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000250void IndexingContext::indexDeclGroupRef(DeclGroupRef DG) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000251 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
252 indexTopLevelDecl(*I);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000253}
254
255void IndexingContext::indexTUDeclsInObjCContainer() {
256 for (unsigned i = 0, e = TUDeclsInObjCContainer.size(); i != e; ++i)
257 indexDeclGroupRef(TUDeclsInObjCContainer[i]);
258 TUDeclsInObjCContainer.clear();
259}