blob: 15fcde174de2b5531b7ef4565f4736eb0ee2ac33 [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#include "CXTranslationUnit.h"
12#include "CIndexDiagnostic.h"
13
14#include "clang/Frontend/ASTUnit.h"
15#include "clang/AST/DeclObjC.h"
16
17using namespace clang;
18using namespace cxindex;
19using namespace cxcursor;
20
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000021IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo(
22 const ObjCProtocolList &ProtList,
23 IndexingContext &IdxCtx,
24 StrAdapter &SA) {
25 ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
26 for (ObjCInterfaceDecl::protocol_iterator
27 I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) {
28 SourceLocation Loc = *LI;
29 ObjCProtocolDecl *PD = *I;
30 ProtEntities.push_back(CXIdxEntityInfo());
31 IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA);
32 CXIdxObjCProtocolRefInfo ProtInfo = { 0,
33 MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU),
34 IdxCtx.getIndexLoc(Loc) };
35 ProtInfos.push_back(ProtInfo);
36 }
37
38 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
39 ProtInfos[i].protocol = &ProtEntities[i];
40
41 for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
42 Prots.push_back(&ProtInfos[i]);
43}
44
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000045const char *IndexingContext::StrAdapter::toCStr(StringRef Str) {
46 if (Str.empty())
47 return "";
48 if (Str.data()[Str.size()] == '\0')
49 return Str.data();
50 Scratch += Str;
51 Scratch.push_back('\0');
52 return Scratch.data() + (Scratch.size() - Str.size() - 1);
53}
54
55void IndexingContext::setASTContext(ASTContext &ctx) {
56 Ctx = &ctx;
57 static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx);
58}
59
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000060void IndexingContext::enteredMainFile(const FileEntry *File) {
61 if (File && CB.enteredMainFile) {
62 CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0);
63 FileMap[File] = idxFile;
64 }
65}
66
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000067void IndexingContext::ppIncludedFile(SourceLocation hashLoc,
68 StringRef filename,
69 const FileEntry *File,
70 bool isImport, bool isAngled) {
71 if (!CB.ppIncludedFile)
72 return;
73
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000074 StrAdapter SA(*this);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000075 CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
76 SA.toCStr(filename),
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000077 (CXFile)File,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000078 isImport, isAngled };
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000079 CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
80 FileMap[File] = idxFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000081}
82
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +000083void IndexingContext::startedTranslationUnit() {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +000084 CXIdxClientContainer idxCont = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000085 if (CB.startedTranslationUnit)
86 idxCont = CB.startedTranslationUnit(ClientData, 0);
87 addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont);
88}
89
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000090void IndexingContext::handleDiagnostic(const StoredDiagnostic &StoredDiag) {
91 if (!CB.diagnostic)
92 return;
93
94 CXStoredDiagnostic CXDiag(StoredDiag, Ctx->getLangOptions());
95 CB.diagnostic(ClientData, &CXDiag, 0);
96}
97
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +000098void IndexingContext::handleDiagnostic(CXDiagnostic CXDiag) {
99 if (!CB.diagnostic)
100 return;
101
102 CB.diagnostic(ClientData, CXDiag, 0);
103}
104
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000105void IndexingContext::handleDecl(const NamedDecl *D,
106 SourceLocation Loc, CXCursor Cursor,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000107 DeclInfo &DInfo) {
108 if (!CB.indexDeclaration)
109 return;
110
111 StrAdapter SA(*this);
112 getEntityInfo(D, DInfo.CXEntInfo, SA);
113 DInfo.entityInfo = &DInfo.CXEntInfo;
114 DInfo.cursor = Cursor;
115 DInfo.loc = getIndexLoc(Loc);
116 DInfo.container = getIndexContainer(D);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000117 DInfo.isImplicit = D->isImplicit();
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000118
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000119 CXIdxClientContainer clientCont = 0;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000120 CXIdxDeclOut DeclOut = { DInfo.isContainer ? &clientCont : 0 };
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000121 CB.indexDeclaration(ClientData, &DInfo, &DeclOut);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000122
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000123 if (DInfo.isContainer)
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000124 addContainerInMap(cast<DeclContext>(D), clientCont);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000125}
126
127void IndexingContext::handleObjCContainer(const ObjCContainerDecl *D,
128 SourceLocation Loc, CXCursor Cursor,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000129 ObjCContainerDeclInfo &ContDInfo) {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000130 ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000131 handleDecl(D, Loc, Cursor, ContDInfo);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000132}
133
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000134void IndexingContext::handleFunction(const FunctionDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000135 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
136 D->isThisDeclarationADefinition());
137 handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000138}
139
140void IndexingContext::handleVar(const VarDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000141 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
142 /*isContainer=*/false);
143 handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000144}
145
146void IndexingContext::handleField(const FieldDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000147 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
148 /*isContainer=*/false);
149 handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000150}
151
152void IndexingContext::handleEnumerator(const EnumConstantDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000153 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
154 /*isContainer=*/false);
155 handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000156}
157
158void IndexingContext::handleTagDecl(const TagDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000159 DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
160 D->isThisDeclarationADefinition());
161 handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000162}
163
164void IndexingContext::handleTypedef(const TypedefDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000165 DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true,
166 /*isContainer=*/false);
167 handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000168}
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000169
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000170void IndexingContext::handleObjCClass(const ObjCClassDecl *D) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000171 const ObjCClassDecl::ObjCClassRef *Ref = D->getForwardDecl();
172 ObjCInterfaceDecl *IFaceD = Ref->getInterface();
173 SourceLocation Loc = Ref->getLocation();
174 bool isRedeclaration = IFaceD->getLocation() != Loc;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000175
176 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration,
177 /*isImplementation=*/false);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000178 handleObjCContainer(IFaceD, Loc, MakeCursorObjCClassRef(IFaceD, Loc, CXTU),
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000179 ContDInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000180}
181
182void IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000183 StrAdapter SA(*this);
184
185 CXIdxBaseClassInfo BaseClass;
186 CXIdxEntityInfo BaseEntity;
187 BaseClass.cursor = clang_getNullCursor();
188 if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
189 getEntityInfo(SuperD, BaseEntity, SA);
190 SourceLocation SuperLoc = D->getSuperClassLoc();
191 BaseClass.base = &BaseEntity;
192 BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
193 BaseClass.loc = getIndexLoc(SuperLoc);
194 }
195
196 ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
197
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000198 ObjCInterfaceDeclInfo InterInfo(D);
199 InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
200 InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000201 InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000202 InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000203
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000204 handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000205}
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000206
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000207void IndexingContext::handleObjCImplementation(
208 const ObjCImplementationDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000209 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false,
Argyrios Kyrtzidise7bbab92011-11-15 06:20:24 +0000210 /*isRedeclaration=*/true,
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000211 /*isImplementation=*/true);
212 handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000213}
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000214
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000215void IndexingContext::handleObjCForwardProtocol(const ObjCProtocolDecl *D,
216 SourceLocation Loc,
217 bool isRedeclaration) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000218 ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true,
219 isRedeclaration,
220 /*isImplementation=*/false);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000221 handleObjCContainer(D, Loc, MakeCursorObjCProtocolRef(D, Loc, CXTU),
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000222 ContDInfo);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000223}
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000224
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000225void IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000226 StrAdapter SA(*this);
227 ObjCProtocolListInfo ProtListInfo(D->getReferencedProtocols(), *this, SA);
228
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000229 ObjCProtocolDeclInfo ProtInfo(D);
230 ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo();
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000231
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000232 handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000233}
234
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000235void IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000236 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000237 CXIdxEntityInfo ClassEntity;
238 StrAdapter SA(*this);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000239 const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
240 SourceLocation ClassLoc = D->getLocation();
241 SourceLocation CategoryLoc = D->getCategoryNameLoc();
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000242 getEntityInfo(IFaceD, ClassEntity, SA);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000243
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000244 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000245 if (IFaceD) {
246 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
247 CatDInfo.ObjCCatDeclInfo.classCursor =
248 MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
249 } else {
250 CatDInfo.ObjCCatDeclInfo.objcClass = 0;
251 CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
252 }
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +0000253 CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
254 handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000255}
256
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000257void IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
258 const ObjCCategoryDecl *CatD = D->getCategoryDecl();
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000259 ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000260 CXIdxEntityInfo ClassEntity;
261 StrAdapter SA(*this);
262 getEntityInfo(CatD->getClassInterface(), ClassEntity, SA);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000263
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +0000264 CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
265 CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000266 handleObjCContainer(D, D->getLocation(), getCursor(D), CatDInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000267}
268
269void IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000270 DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(),
271 D->isThisDeclarationADefinition());
272 handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000273}
274
275void IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000276 DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/false,
277 /*isContainer=*/false);
278 handleDecl(D, D->getLocation(), getCursor(D), DInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000279}
280
281void IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
282 const NamedDecl *Parent,
283 const DeclContext *DC,
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +0000284 const Expr *E,
285 CXIdxEntityRefKind Kind) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000286 if (Loc.isInvalid())
287 return;
288 if (!CB.indexEntityReference)
289 return;
290 if (isNotFromSourceFile(D->getLocation()))
291 return;
292
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000293 D = getEntityDecl(D);
294
295 if (onlyOneRefPerFile()) {
296 SourceManager &SM = Ctx->getSourceManager();
297 SourceLocation FileLoc = SM.getFileLoc(Loc);
298
299 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
300 FileID FID = LocInfo.first;
301 if (FID.isInvalid())
302 return;
303
304 const FileEntry *FE = SM.getFileEntryForID(FID);
305 if (!FE)
306 return;
307 RefFileOccurence RefOccur(FE, D);
308 std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool>
309 res = RefFileOccurences.insert(RefOccur);
310 if (!res.second)
311 return; // already in map.
312 }
313
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000314 StrAdapter SA(*this);
Benjamin Kramer854625f2011-10-28 13:37:11 +0000315 CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E),
316 const_cast<Decl*>(cast<Decl>(DC)), CXTU)
317 : getRefCursor(D, Loc);
318
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000319 CXIdxEntityInfo RefEntity, ParentEntity;
320 getEntityInfo(D, RefEntity, SA);
321 getEntityInfo(Parent, ParentEntity, SA);
Benjamin Kramer854625f2011-10-28 13:37:11 +0000322 CXIdxEntityRefInfo Info = { Cursor,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000323 getIndexLoc(Loc),
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000324 &RefEntity,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000325 Parent ? &ParentEntity : 0,
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +0000326 getIndexContainerForDC(DC),
327 Kind };
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000328 CB.indexEntityReference(ClientData, &Info);
329}
330
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000331bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const {
332 if (Loc.isInvalid())
333 return true;
334 SourceManager &SM = Ctx->getSourceManager();
335 SourceLocation FileLoc = SM.getFileLoc(Loc);
336 FileID FID = SM.getFileID(FileLoc);
337 return SM.getFileEntryForID(FID) == 0;
338}
339
340void IndexingContext::addContainerInMap(const DeclContext *DC,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000341 CXIdxClientContainer container) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000342 assert(getScopedContext(DC) == DC);
343 ContainerMapTy::iterator I = ContainerMap.find(DC);
344 if (I == ContainerMap.end()) {
345 if (container)
346 ContainerMap[DC] = container;
347 return;
348 }
349 // Allow changing the container of a previously seen DeclContext so we
350 // can handle invalid user code, like a function re-definition.
351 if (container)
352 I->second = container;
353 else
354 ContainerMap.erase(I);
355}
356
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000357const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const {
358 assert(D);
359 D = cast<NamedDecl>(D->getCanonicalDecl());
360
361 if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(D)) {
362 if (Cat->IsClassExtension())
363 return getEntityDecl(Cat->getClassInterface());
364
365 } else if (const ObjCImplementationDecl *
366 ImplD = dyn_cast<ObjCImplementationDecl>(D)) {
367 return getEntityDecl(ImplD->getClassInterface());
368
369 } else if (const ObjCCategoryImplDecl *
370 CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) {
371 return getEntityDecl(CatImplD->getCategoryDecl());
372 }
373
374 return D;
375}
376
377const DeclContext *
378IndexingContext::getScopedContext(const DeclContext *DC) const {
379 // Local contexts are ignored for indexing.
380 const DeclContext *FuncCtx = cast<Decl>(DC)->getParentFunctionOrMethod();
381 if (FuncCtx)
382 return FuncCtx;
383
384 // We consider enums always scoped for indexing.
385 if (isa<TagDecl>(DC))
386 return DC;
387
388 if (const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) {
389 if (NS->isAnonymousNamespace())
390 return getScopedContext(NS->getParent());
391 return NS;
392 }
393
394 return DC->getRedeclContext();
395}
396
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000397CXIdxClientContainer
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000398IndexingContext::getIndexContainerForDC(const DeclContext *DC) const {
399 DC = getScopedContext(DC);
400 ContainerMapTy::const_iterator I = ContainerMap.find(DC);
401// assert(I != ContainerMap.end() &&
402// "Failed to include a scoped context in the container map");
403 return I->second;
404}
405
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000406CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000407 if (!File)
408 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000409
410 FileMapTy::iterator FI = FileMap.find(File);
411 if (FI != FileMap.end())
412 return FI->second;
413
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000414 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000415}
416
417CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const {
418 CXIdxLoc idxLoc = { {0, 0}, 0 };
419 if (Loc.isInvalid())
420 return idxLoc;
421
422 idxLoc.ptr_data[0] = (void*)this;
423 idxLoc.int_data = Loc.getRawEncoding();
424 return idxLoc;
425}
426
427void IndexingContext::translateLoc(SourceLocation Loc,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000428 CXIdxClientFile *indexFile, CXFile *file,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000429 unsigned *line, unsigned *column,
430 unsigned *offset) {
431 if (Loc.isInvalid())
432 return;
433
434 SourceManager &SM = Ctx->getSourceManager();
435 Loc = SM.getFileLoc(Loc);
436
437 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
438 FileID FID = LocInfo.first;
439 unsigned FileOffset = LocInfo.second;
440
441 if (FID.isInvalid())
442 return;
443
444 const FileEntry *FE = SM.getFileEntryForID(FID);
445 if (indexFile)
446 *indexFile = getIndexFile(FE);
447 if (file)
448 *file = (void *)FE;
449 if (line)
450 *line = SM.getLineNumber(FID, FileOffset);
451 if (column)
452 *column = SM.getColumnNumber(FID, FileOffset);
453 if (offset)
454 *offset = FileOffset;
455}
456
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000457void IndexingContext::getEntityInfo(const NamedDecl *D,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000458 CXIdxEntityInfo &EntityInfo,
459 StrAdapter &SA) {
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +0000460 if (!D)
461 return;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000462 D = getEntityDecl(D);
463 EntityInfo.kind = CXIdxEntity_Unexposed;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000464
465 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
466 switch (TD->getTagKind()) {
467 case TTK_Struct:
468 EntityInfo.kind = CXIdxEntity_Struct; break;
469 case TTK_Union:
470 EntityInfo.kind = CXIdxEntity_Union; break;
471 case TTK_Class:
472 EntityInfo.kind = CXIdxEntity_CXXClass; break;
473 case TTK_Enum:
474 EntityInfo.kind = CXIdxEntity_Enum; break;
475 }
476
477 } else {
478 switch (D->getKind()) {
479 case Decl::Typedef:
480 EntityInfo.kind = CXIdxEntity_Typedef; break;
481 case Decl::Function:
482 EntityInfo.kind = CXIdxEntity_Function; break;
483 case Decl::Var:
484 EntityInfo.kind = CXIdxEntity_Variable; break;
485 case Decl::Field:
486 EntityInfo.kind = CXIdxEntity_Field; break;
487 case Decl::EnumConstant:
488 EntityInfo.kind = CXIdxEntity_EnumConstant; break;
489 case Decl::ObjCInterface:
490 EntityInfo.kind = CXIdxEntity_ObjCClass; break;
491 case Decl::ObjCProtocol:
492 EntityInfo.kind = CXIdxEntity_ObjCProtocol; break;
493 case Decl::ObjCCategory:
494 EntityInfo.kind = CXIdxEntity_ObjCCategory; break;
495 case Decl::ObjCMethod:
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +0000496 if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
497 EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod;
498 else
499 EntityInfo.kind = CXIdxEntity_ObjCClassMethod;
500 break;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000501 case Decl::ObjCProperty:
502 EntityInfo.kind = CXIdxEntity_ObjCProperty; break;
503 case Decl::ObjCIvar:
504 EntityInfo.kind = CXIdxEntity_ObjCIvar; break;
505 default:
506 break;
507 }
508 }
509
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000510 if (IdentifierInfo *II = D->getIdentifier()) {
511 EntityInfo.name = SA.toCStr(II->getName());
512
513 } else if (isa<RecordDecl>(D) || isa<NamespaceDecl>(D)) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000514 EntityInfo.name = 0; // anonymous record/namespace.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000515
516 } else {
517 unsigned Begin = SA.getCurSize();
518 {
519 llvm::raw_svector_ostream OS(SA.getBuffer());
520 D->printName(OS);
521 }
522 EntityInfo.name = SA.getCStr(Begin);
523 }
524
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +0000525 {
526 unsigned Begin = SA.getCurSize();
527 bool Ignore = getDeclCursorUSR(D, SA.getBuffer());
528 if (Ignore) {
529 EntityInfo.USR = "";
530 } else {
531 EntityInfo.USR = SA.getCStr(Begin);
532 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000533 }
534}
535
536CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) {
537 if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
538 return MakeCursorTypeRef(TD, Loc, CXTU);
539 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
540 return MakeCursorObjCClassRef(ID, Loc, CXTU);
541 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
542 return MakeCursorObjCProtocolRef(PD, Loc, CXTU);
543
544 //assert(0 && "not yet");
545 return clang_getNullCursor();
546}