]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/libclang/IndexingContext.cpp
Vendor import of clang release_32 branch r168974 (effectively, 3.2 RC2):
[FreeBSD/FreeBSD.git] / tools / libclang / IndexingContext.cpp
1 //===- IndexingContext.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/DeclCXX.h"
16 #include "clang/AST/DeclTemplate.h"
17
18 using namespace clang;
19 using namespace cxindex;
20 using namespace cxcursor;
21
22 IndexingContext::ObjCProtocolListInfo::ObjCProtocolListInfo(
23                                     const ObjCProtocolList &ProtList,
24                                     IndexingContext &IdxCtx,
25                                     ScratchAlloc &SA) {
26   ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
27   for (ObjCInterfaceDecl::protocol_iterator
28          I = ProtList.begin(), E = ProtList.end(); I != E; ++I, ++LI) {
29     SourceLocation Loc = *LI;
30     ObjCProtocolDecl *PD = *I;
31     ProtEntities.push_back(EntityInfo());
32     IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA);
33     CXIdxObjCProtocolRefInfo ProtInfo = { 0,
34                                 MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU),
35                                 IdxCtx.getIndexLoc(Loc) };
36     ProtInfos.push_back(ProtInfo);
37
38     if (IdxCtx.shouldSuppressRefs())
39       IdxCtx.markEntityOccurrenceInFile(PD, Loc);
40   }
41
42   for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
43     ProtInfos[i].protocol = &ProtEntities[i];
44
45   for (unsigned i = 0, e = ProtInfos.size(); i != e; ++i)
46     Prots.push_back(&ProtInfos[i]);
47 }
48
49
50 IBOutletCollectionInfo::IBOutletCollectionInfo(
51                                           const IBOutletCollectionInfo &other)
52   : AttrInfo(CXIdxAttr_IBOutletCollection, other.cursor, other.loc, other.A) {
53
54   IBCollInfo.attrInfo = this;
55   IBCollInfo.classCursor = other.IBCollInfo.classCursor;
56   IBCollInfo.classLoc = other.IBCollInfo.classLoc;
57   if (other.IBCollInfo.objcClass) {
58     ClassInfo = other.ClassInfo;
59     IBCollInfo.objcClass = &ClassInfo;
60   } else
61     IBCollInfo.objcClass = 0;
62 }
63
64 AttrListInfo::AttrListInfo(const Decl *D, IndexingContext &IdxCtx)
65   : SA(IdxCtx), ref_cnt(0) {
66
67   if (!D->hasAttrs())
68     return;
69
70   for (AttrVec::const_iterator AttrI = D->attr_begin(), AttrE = D->attr_end();
71          AttrI != AttrE; ++AttrI) {
72     const Attr *A = *AttrI;
73     CXCursor C = MakeCXCursor(A, const_cast<Decl *>(D), IdxCtx.CXTU);
74     CXIdxLoc Loc =  IdxCtx.getIndexLoc(A->getLocation());
75     switch (C.kind) {
76     default:
77       Attrs.push_back(AttrInfo(CXIdxAttr_Unexposed, C, Loc, A));
78       break;
79     case CXCursor_IBActionAttr:
80       Attrs.push_back(AttrInfo(CXIdxAttr_IBAction, C, Loc, A));
81       break;
82     case CXCursor_IBOutletAttr:
83       Attrs.push_back(AttrInfo(CXIdxAttr_IBOutlet, C, Loc, A));
84       break;
85     case CXCursor_IBOutletCollectionAttr:
86       IBCollAttrs.push_back(IBOutletCollectionInfo(C, Loc, A));
87       break;
88     }
89   }
90
91   for (unsigned i = 0, e = IBCollAttrs.size(); i != e; ++i) {
92     IBOutletCollectionInfo &IBInfo = IBCollAttrs[i];
93     CXAttrs.push_back(&IBInfo);
94
95     const IBOutletCollectionAttr *
96       IBAttr = cast<IBOutletCollectionAttr>(IBInfo.A);
97     IBInfo.IBCollInfo.attrInfo = &IBInfo;
98     IBInfo.IBCollInfo.classLoc = IdxCtx.getIndexLoc(IBAttr->getInterfaceLoc());
99     IBInfo.IBCollInfo.objcClass = 0;
100     IBInfo.IBCollInfo.classCursor = clang_getNullCursor();
101     QualType Ty = IBAttr->getInterface();
102     if (const ObjCInterfaceType *InterTy = Ty->getAs<ObjCInterfaceType>()) {
103       if (const ObjCInterfaceDecl *InterD = InterTy->getInterface()) {
104         IdxCtx.getEntityInfo(InterD, IBInfo.ClassInfo, SA);
105         IBInfo.IBCollInfo.objcClass = &IBInfo.ClassInfo;
106         IBInfo.IBCollInfo.classCursor = MakeCursorObjCClassRef(InterD,
107                                         IBAttr->getInterfaceLoc(), IdxCtx.CXTU);
108       }
109     }
110   }
111
112   for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
113     CXAttrs.push_back(&Attrs[i]);
114 }
115
116 IntrusiveRefCntPtr<AttrListInfo>
117 AttrListInfo::create(const Decl *D, IndexingContext &IdxCtx) {
118   ScratchAlloc SA(IdxCtx);
119   AttrListInfo *attrs = SA.allocate<AttrListInfo>();
120   return new (attrs) AttrListInfo(D, IdxCtx);
121 }
122
123 IndexingContext::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D,
124                                    IndexingContext &IdxCtx,
125                                    ScratchAlloc &SA) {
126   for (CXXRecordDecl::base_class_const_iterator
127          I = D->bases_begin(), E = D->bases_end(); I != E; ++I) {
128     const CXXBaseSpecifier &Base = *I;
129     BaseEntities.push_back(EntityInfo());
130     const NamedDecl *BaseD = 0;
131     QualType T = Base.getType();
132     SourceLocation Loc = getBaseLoc(Base);
133
134     if (const TypedefType *TDT = T->getAs<TypedefType>()) {
135       BaseD = TDT->getDecl();
136     } else if (const TemplateSpecializationType *
137           TST = T->getAs<TemplateSpecializationType>()) {
138       BaseD = TST->getTemplateName().getAsTemplateDecl();
139     } else if (const RecordType *RT = T->getAs<RecordType>()) {
140       BaseD = RT->getDecl();
141     }
142
143     if (BaseD)
144       IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA);
145     CXIdxBaseClassInfo BaseInfo = { 0,
146                          MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU),
147                          IdxCtx.getIndexLoc(Loc) };
148     BaseInfos.push_back(BaseInfo);
149   }
150
151   for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i) {
152     if (BaseEntities[i].name && BaseEntities[i].USR)
153       BaseInfos[i].base = &BaseEntities[i];
154   }
155
156   for (unsigned i = 0, e = BaseInfos.size(); i != e; ++i)
157     CXBases.push_back(&BaseInfos[i]);
158 }
159
160 SourceLocation IndexingContext::CXXBasesListInfo::getBaseLoc(
161                                            const CXXBaseSpecifier &Base) const {
162   SourceLocation Loc = Base.getSourceRange().getBegin();
163   TypeLoc TL;
164   if (Base.getTypeSourceInfo())
165     TL = Base.getTypeSourceInfo()->getTypeLoc();
166   if (TL.isNull())
167     return Loc;
168
169   if (const QualifiedTypeLoc *QL = dyn_cast<QualifiedTypeLoc>(&TL))
170     TL = QL->getUnqualifiedLoc();
171
172   if (const ElaboratedTypeLoc *EL = dyn_cast<ElaboratedTypeLoc>(&TL))
173     return EL->getNamedTypeLoc().getBeginLoc();
174   if (const DependentNameTypeLoc *DL = dyn_cast<DependentNameTypeLoc>(&TL))
175     return DL->getNameLoc();
176   if (const DependentTemplateSpecializationTypeLoc *
177         DTL = dyn_cast<DependentTemplateSpecializationTypeLoc>(&TL))
178     return DTL->getTemplateNameLoc();
179
180   return Loc;
181 }
182
183 const char *ScratchAlloc::toCStr(StringRef Str) {
184   if (Str.empty())
185     return "";
186   if (Str.data()[Str.size()] == '\0')
187     return Str.data();
188   return copyCStr(Str);
189 }
190
191 const char *ScratchAlloc::copyCStr(StringRef Str) {
192   char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1);
193   std::uninitialized_copy(Str.begin(), Str.end(), buf);
194   buf[Str.size()] = '\0';
195   return buf;
196 }
197
198 void IndexingContext::setASTContext(ASTContext &ctx) {
199   Ctx = &ctx;
200   static_cast<ASTUnit*>(CXTU->TUData)->setASTContext(&ctx);
201 }
202
203 void IndexingContext::setPreprocessor(Preprocessor &PP) {
204   static_cast<ASTUnit*>(CXTU->TUData)->setPreprocessor(&PP);
205 }
206
207 bool IndexingContext::isFunctionLocalDecl(const Decl *D) {
208   assert(D);
209
210   if (!D->getParentFunctionOrMethod())
211     return false;
212
213   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
214     switch (ND->getLinkage()) {
215     case NoLinkage:
216     case InternalLinkage:
217       return true;
218     case UniqueExternalLinkage:
219     case ExternalLinkage:
220       return false;
221     }
222   }
223
224   return true;
225 }
226
227 bool IndexingContext::shouldAbort() {
228   if (!CB.abortQuery)
229     return false;
230   return CB.abortQuery(ClientData, 0);
231 }
232
233 void IndexingContext::enteredMainFile(const FileEntry *File) {
234   if (File && CB.enteredMainFile) {
235     CXIdxClientFile idxFile = CB.enteredMainFile(ClientData, (CXFile)File, 0);
236     FileMap[File] = idxFile;
237   }
238 }
239
240 void IndexingContext::ppIncludedFile(SourceLocation hashLoc,
241                                      StringRef filename,
242                                      const FileEntry *File,
243                                      bool isImport, bool isAngled,
244                                      bool isModuleImport) {
245   if (!CB.ppIncludedFile)
246     return;
247
248   ScratchAlloc SA(*this);
249   CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
250                                  SA.toCStr(filename),
251                                  (CXFile)File,
252                                  isImport, isAngled, isModuleImport };
253   CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
254   FileMap[File] = idxFile;
255 }
256
257 void IndexingContext::importedModule(const ImportDecl *ImportD) {
258   if (!CB.importedASTFile)
259     return;
260
261   Module *Mod = ImportD->getImportedModule();
262   if (!Mod)
263     return;
264   std::string ModuleName = Mod->getFullModuleName();
265
266   CXIdxImportedASTFileInfo Info = {
267                                     (CXFile)Mod->getASTFile(),
268                                     Mod,
269                                     getIndexLoc(ImportD->getLocation()),
270                                     ImportD->isImplicit()
271                                   };
272   CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
273   (void)astFile;
274 }
275
276 void IndexingContext::importedPCH(const FileEntry *File) {
277   if (!CB.importedASTFile)
278     return;
279
280   CXIdxImportedASTFileInfo Info = {
281                                     (CXFile)File,
282                                     /*module=*/NULL,
283                                     getIndexLoc(SourceLocation()),
284                                     /*isImplicit=*/false
285                                   };
286   CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
287   (void)astFile;
288 }
289
290 void IndexingContext::startedTranslationUnit() {
291   CXIdxClientContainer idxCont = 0;
292   if (CB.startedTranslationUnit)
293     idxCont = CB.startedTranslationUnit(ClientData, 0);
294   addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont);
295 }
296
297 void IndexingContext::handleDiagnosticSet(CXDiagnostic CXDiagSet) {
298   if (!CB.diagnostic)
299     return;
300
301   CB.diagnostic(ClientData, CXDiagSet, 0);
302 }
303
304 bool IndexingContext::handleDecl(const NamedDecl *D,
305                                  SourceLocation Loc, CXCursor Cursor,
306                                  DeclInfo &DInfo,
307                                  const DeclContext *LexicalDC) {
308   if (!CB.indexDeclaration || !D)
309     return false;
310   if (D->isImplicit() && shouldIgnoreIfImplicit(D))
311     return false;
312
313   ScratchAlloc SA(*this);
314   getEntityInfo(D, DInfo.EntInfo, SA);
315   if ((!shouldIndexFunctionLocalSymbols() && !DInfo.EntInfo.USR)
316       || Loc.isInvalid())
317     return false;
318
319   if (!LexicalDC)
320     LexicalDC = D->getLexicalDeclContext();
321
322   if (shouldSuppressRefs())
323     markEntityOccurrenceInFile(D, Loc);
324   
325   DInfo.entityInfo = &DInfo.EntInfo;
326   DInfo.cursor = Cursor;
327   DInfo.loc = getIndexLoc(Loc);
328   DInfo.isImplicit = D->isImplicit();
329
330   DInfo.attributes = DInfo.EntInfo.attributes;
331   DInfo.numAttributes = DInfo.EntInfo.numAttributes;
332
333   getContainerInfo(D->getDeclContext(), DInfo.SemanticContainer);
334   DInfo.semanticContainer = &DInfo.SemanticContainer;
335
336   if (LexicalDC == D->getDeclContext()) {
337     DInfo.lexicalContainer = &DInfo.SemanticContainer;
338   } else if (isTemplateImplicitInstantiation(D)) {
339     // Implicit instantiations have the lexical context of where they were
340     // instantiated first. We choose instead the semantic context because:
341     // 1) at the time that we see the instantiation we have not seen the
342     //   function where it occurred yet.
343     // 2) the lexical context of the first instantiation is not useful
344     //   information anyway.
345     DInfo.lexicalContainer = &DInfo.SemanticContainer;
346   } else {
347     getContainerInfo(LexicalDC, DInfo.LexicalContainer);
348     DInfo.lexicalContainer = &DInfo.LexicalContainer;
349   }
350
351   if (DInfo.isContainer) {
352     getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer);
353     DInfo.declAsContainer = &DInfo.DeclAsContainer;
354   }
355
356   CB.indexDeclaration(ClientData, &DInfo);
357   return true;
358 }
359
360 bool IndexingContext::handleObjCContainer(const ObjCContainerDecl *D,
361                                           SourceLocation Loc, CXCursor Cursor,
362                                           ObjCContainerDeclInfo &ContDInfo) {
363   ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo;
364   return handleDecl(D, Loc, Cursor, ContDInfo);
365 }
366
367 bool IndexingContext::handleFunction(const FunctionDecl *D) {
368   DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
369                  D->isThisDeclarationADefinition());
370   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
371 }
372
373 bool IndexingContext::handleVar(const VarDecl *D) {
374   DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
375                  /*isContainer=*/false);
376   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
377 }
378
379 bool IndexingContext::handleField(const FieldDecl *D) {
380   DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
381                  /*isContainer=*/false);
382   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
383 }
384
385 bool IndexingContext::handleEnumerator(const EnumConstantDecl *D) {
386   DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
387                  /*isContainer=*/false);
388   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
389 }
390
391 bool IndexingContext::handleTagDecl(const TagDecl *D) {
392   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D))
393     return handleCXXRecordDecl(CXXRD, D);
394
395   DeclInfo DInfo(!D->isFirstDeclaration(), D->isThisDeclarationADefinition(),
396                  D->isThisDeclarationADefinition());
397   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
398 }
399
400 bool IndexingContext::handleTypedefName(const TypedefNameDecl *D) {
401   DeclInfo DInfo(!D->isFirstDeclaration(), /*isDefinition=*/true,
402                  /*isContainer=*/false);
403   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
404 }
405
406 bool IndexingContext::handleObjCInterface(const ObjCInterfaceDecl *D) {
407   // For @class forward declarations, suppress them the same way as references.
408   if (!D->isThisDeclarationADefinition()) {
409     if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
410       return false; // already occurred.
411
412     // FIXME: This seems like the wrong definition for redeclaration.
413     bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
414     ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration,
415                                     /*isImplementation=*/false);
416     return handleObjCContainer(D, D->getLocation(),
417                                MakeCursorObjCClassRef(D, D->getLocation(),
418                                                       CXTU), 
419                                ContDInfo);
420   }
421
422   ScratchAlloc SA(*this);
423
424   CXIdxBaseClassInfo BaseClass;
425   EntityInfo BaseEntity;
426   BaseClass.cursor = clang_getNullCursor();
427   if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
428     getEntityInfo(SuperD, BaseEntity, SA);
429     SourceLocation SuperLoc = D->getSuperClassLoc();
430     BaseClass.base = &BaseEntity;
431     BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
432     BaseClass.loc = getIndexLoc(SuperLoc);
433
434     if (shouldSuppressRefs())
435       markEntityOccurrenceInFile(SuperD, SuperLoc);
436   }
437   
438   ObjCProtocolList EmptyProtoList;
439   ObjCProtocolListInfo ProtInfo(D->isThisDeclarationADefinition() 
440                                   ? D->getReferencedProtocols()
441                                   : EmptyProtoList, 
442                                 *this, SA);
443   
444   ObjCInterfaceDeclInfo InterInfo(D);
445   InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
446   InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo;
447   InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass : 0;
448   InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo;
449
450   return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo);
451 }
452
453 bool IndexingContext::handleObjCImplementation(
454                                               const ObjCImplementationDecl *D) {
455   ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false,
456                       /*isRedeclaration=*/true,
457                       /*isImplementation=*/true);
458   return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo);
459 }
460
461 bool IndexingContext::handleObjCProtocol(const ObjCProtocolDecl *D) {
462   if (!D->isThisDeclarationADefinition()) {
463     if (shouldSuppressRefs() && markEntityOccurrenceInFile(D, D->getLocation()))
464       return false; // already occurred.
465     
466     // FIXME: This seems like the wrong definition for redeclaration.
467     bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
468     ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true,
469                                     isRedeclaration,
470                                     /*isImplementation=*/false);
471     return handleObjCContainer(D, D->getLocation(), 
472                                MakeCursorObjCProtocolRef(D, D->getLocation(),
473                                                          CXTU),
474                                ContDInfo);    
475   }
476   
477   ScratchAlloc SA(*this);
478   ObjCProtocolList EmptyProtoList;
479   ObjCProtocolListInfo ProtListInfo(D->isThisDeclarationADefinition()
480                                       ? D->getReferencedProtocols()
481                                       : EmptyProtoList,
482                                     *this, SA);
483   
484   ObjCProtocolDeclInfo ProtInfo(D);
485   ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo();
486
487   return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo);
488 }
489
490 bool IndexingContext::handleObjCCategory(const ObjCCategoryDecl *D) {
491   ScratchAlloc SA(*this);
492
493   ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false);
494   EntityInfo ClassEntity;
495   const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
496   SourceLocation ClassLoc = D->getLocation();
497   SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc
498                                                      : D->getCategoryNameLoc();
499   getEntityInfo(IFaceD, ClassEntity, SA);
500
501   if (shouldSuppressRefs())
502     markEntityOccurrenceInFile(IFaceD, ClassLoc);
503
504   ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
505   
506   CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
507   if (IFaceD) {
508     CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
509     CatDInfo.ObjCCatDeclInfo.classCursor =
510         MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
511   } else {
512     CatDInfo.ObjCCatDeclInfo.objcClass = 0;
513     CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
514   }
515   CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
516   CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
517   CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo;
518
519   return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
520 }
521
522 bool IndexingContext::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
523   ScratchAlloc SA(*this);
524
525   const ObjCCategoryDecl *CatD = D->getCategoryDecl();
526   ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true);
527   EntityInfo ClassEntity;
528   const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface();
529   SourceLocation ClassLoc = D->getLocation();
530   SourceLocation CategoryLoc = D->getCategoryNameLoc();
531   getEntityInfo(IFaceD, ClassEntity, SA);
532
533   if (shouldSuppressRefs())
534     markEntityOccurrenceInFile(IFaceD, ClassLoc);
535
536   CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
537   if (IFaceD) {
538     CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
539     CatDInfo.ObjCCatDeclInfo.classCursor =
540         MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
541   } else {
542     CatDInfo.ObjCCatDeclInfo.objcClass = 0;
543     CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
544   }
545   CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
546   CatDInfo.ObjCCatDeclInfo.protocols = 0;
547
548   return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
549 }
550
551 bool IndexingContext::handleObjCMethod(const ObjCMethodDecl *D) {
552   DeclInfo DInfo(!D->isCanonicalDecl(), D->isThisDeclarationADefinition(),
553                  D->isThisDeclarationADefinition());
554   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
555 }
556
557 bool IndexingContext::handleSynthesizedObjCProperty(
558                                                 const ObjCPropertyImplDecl *D) {
559   ObjCPropertyDecl *PD = D->getPropertyDecl();
560   return handleReference(PD, D->getLocation(), getCursor(D), 0, D->getDeclContext());
561 }
562
563 bool IndexingContext::handleSynthesizedObjCMethod(const ObjCMethodDecl *D,
564                                                   SourceLocation Loc,
565                                                  const DeclContext *LexicalDC) {
566   DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true,
567                  /*isContainer=*/false);
568   return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC);
569 }
570
571 bool IndexingContext::handleObjCProperty(const ObjCPropertyDecl *D) {
572   ScratchAlloc SA(*this);
573
574   ObjCPropertyDeclInfo DInfo;
575   EntityInfo GetterEntity;
576   EntityInfo SetterEntity;
577
578   DInfo.ObjCPropDeclInfo.declInfo = &DInfo;
579
580   if (ObjCMethodDecl *Getter = D->getGetterMethodDecl()) {
581     getEntityInfo(Getter, GetterEntity, SA);
582     DInfo.ObjCPropDeclInfo.getter = &GetterEntity;
583   } else {
584     DInfo.ObjCPropDeclInfo.getter = 0;
585   }
586   if (ObjCMethodDecl *Setter = D->getSetterMethodDecl()) {
587     getEntityInfo(Setter, SetterEntity, SA);
588     DInfo.ObjCPropDeclInfo.setter = &SetterEntity;
589   } else {
590     DInfo.ObjCPropDeclInfo.setter = 0;
591   }
592
593   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
594 }
595
596 bool IndexingContext::handleNamespace(const NamespaceDecl *D) {
597   DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(),
598                  /*isDefinition=*/true,
599                  /*isContainer=*/true);
600   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
601 }
602
603 bool IndexingContext::handleClassTemplate(const ClassTemplateDecl *D) {
604   return handleCXXRecordDecl(D->getTemplatedDecl(), D);
605 }
606
607 bool IndexingContext::handleFunctionTemplate(const FunctionTemplateDecl *D) {
608   DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
609                  /*isDefinition=*/D->isThisDeclarationADefinition(),
610                  /*isContainer=*/D->isThisDeclarationADefinition());
611   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
612 }
613
614 bool IndexingContext::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) {
615   DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
616                  /*isDefinition=*/true, /*isContainer=*/false);
617   return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
618 }
619
620 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
621                                       const NamedDecl *Parent,
622                                       const DeclContext *DC,
623                                       const Expr *E,
624                                       CXIdxEntityRefKind Kind) {
625   if (!D)
626     return false;
627
628   CXCursor Cursor = E ? MakeCXCursor(const_cast<Expr*>(E),
629                                      const_cast<Decl*>(cast<Decl>(DC)), CXTU)
630                       : getRefCursor(D, Loc);
631   return handleReference(D, Loc, Cursor, Parent, DC, E, Kind);
632 }
633
634 bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
635                                       CXCursor Cursor,
636                                       const NamedDecl *Parent,
637                                       const DeclContext *DC,
638                                       const Expr *E,
639                                       CXIdxEntityRefKind Kind) {
640   if (!CB.indexEntityReference)
641     return false;
642
643   if (!D)
644     return false;
645   if (Loc.isInvalid())
646     return false;
647   if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalDecl(D))
648     return false;
649   if (isNotFromSourceFile(D->getLocation()))
650     return false;
651   if (D->isImplicit() && shouldIgnoreIfImplicit(D))
652     return false;
653
654   if (shouldSuppressRefs()) {
655     if (markEntityOccurrenceInFile(D, Loc))
656       return false; // already occurred.
657   }
658
659   ScratchAlloc SA(*this);
660   EntityInfo RefEntity, ParentEntity;
661   getEntityInfo(D, RefEntity, SA);
662   if (!RefEntity.USR)
663     return false;
664
665   getEntityInfo(Parent, ParentEntity, SA);
666
667   ContainerInfo Container;
668   getContainerInfo(DC, Container);
669
670   CXIdxEntityRefInfo Info = { Kind,
671                               Cursor,
672                               getIndexLoc(Loc),
673                               &RefEntity,
674                               Parent ? &ParentEntity : 0,
675                               &Container };
676   CB.indexEntityReference(ClientData, &Info);
677   return true;
678 }
679
680 bool IndexingContext::isNotFromSourceFile(SourceLocation Loc) const {
681   if (Loc.isInvalid())
682     return true;
683   SourceManager &SM = Ctx->getSourceManager();
684   SourceLocation FileLoc = SM.getFileLoc(Loc);
685   FileID FID = SM.getFileID(FileLoc);
686   return SM.getFileEntryForID(FID) == 0;
687 }
688
689 void IndexingContext::addContainerInMap(const DeclContext *DC,
690                                         CXIdxClientContainer container) {
691   if (!DC)
692     return;
693
694   ContainerMapTy::iterator I = ContainerMap.find(DC);
695   if (I == ContainerMap.end()) {
696     if (container)
697       ContainerMap[DC] = container;
698     return;
699   }
700   // Allow changing the container of a previously seen DeclContext so we
701   // can handle invalid user code, like a function re-definition.
702   if (container)
703     I->second = container;
704   else
705     ContainerMap.erase(I);
706 }
707
708 CXIdxClientEntity IndexingContext::getClientEntity(const Decl *D) const {
709   if (!D)
710     return 0;
711   EntityMapTy::const_iterator I = EntityMap.find(D);
712   if (I == EntityMap.end())
713     return 0;
714   return I->second;
715 }
716
717 void IndexingContext::setClientEntity(const Decl *D, CXIdxClientEntity client) {
718   if (!D)
719     return;
720   EntityMap[D] = client;
721 }
722
723 bool IndexingContext::handleCXXRecordDecl(const CXXRecordDecl *RD,
724                                           const NamedDecl *OrigD) {
725   if (RD->isThisDeclarationADefinition()) {
726     ScratchAlloc SA(*this);
727     CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
728                            /*isDefinition=*/RD->isThisDeclarationADefinition());
729     CXXBasesListInfo BaseList(RD, *this, SA);
730     CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo;
731     CXXDInfo.CXXClassInfo.bases = BaseList.getBases();
732     CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases();
733
734     if (shouldSuppressRefs()) {
735       // Go through bases and mark them as referenced.
736       for (unsigned i = 0, e = BaseList.getNumBases(); i != e; ++i) {
737         const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i];
738         if (baseInfo->base) {
739           const NamedDecl *BaseD = BaseList.BaseEntities[i].Dcl;
740           SourceLocation
741             Loc = SourceLocation::getFromRawEncoding(baseInfo->loc.int_data);
742           markEntityOccurrenceInFile(BaseD, Loc);
743         }
744       }
745     }
746
747     return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo);
748   }
749
750   DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
751                  /*isDefinition=*/RD->isThisDeclarationADefinition(),
752                  /*isContainer=*/RD->isThisDeclarationADefinition());
753   return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo);
754 }
755
756 bool IndexingContext::markEntityOccurrenceInFile(const NamedDecl *D,
757                                                  SourceLocation Loc) {
758   if (!D || Loc.isInvalid())
759     return true;
760
761   SourceManager &SM = Ctx->getSourceManager();
762   D = getEntityDecl(D);
763   
764   std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc));
765   FileID FID = LocInfo.first;
766   if (FID.isInvalid())
767     return true;
768   
769   const FileEntry *FE = SM.getFileEntryForID(FID);
770   if (!FE)
771     return true;
772   RefFileOccurence RefOccur(FE, D);
773   std::pair<llvm::DenseSet<RefFileOccurence>::iterator, bool>
774   res = RefFileOccurences.insert(RefOccur);
775   if (!res.second)
776     return true; // already in map.
777
778   return false;
779 }
780
781 const NamedDecl *IndexingContext::getEntityDecl(const NamedDecl *D) const {
782   assert(D);
783   D = cast<NamedDecl>(D->getCanonicalDecl());
784
785   if (const ObjCImplementationDecl *
786                ImplD = dyn_cast<ObjCImplementationDecl>(D)) {
787     return getEntityDecl(ImplD->getClassInterface());
788
789   } else if (const ObjCCategoryImplDecl *
790                CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) {
791     return getEntityDecl(CatImplD->getCategoryDecl());
792   } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
793     if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate())
794       return getEntityDecl(TemplD);
795   } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
796     if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate())
797       return getEntityDecl(TemplD);
798   }
799
800   return D;
801 }
802
803 const DeclContext *
804 IndexingContext::getEntityContainer(const Decl *D) const {
805   const DeclContext *DC = dyn_cast<DeclContext>(D);
806   if (DC)
807     return DC;
808
809   if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) {
810     DC = ClassTempl->getTemplatedDecl();
811   } if (const FunctionTemplateDecl *
812           FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) {
813     DC = FuncTempl->getTemplatedDecl();
814   }
815
816   return DC;
817 }
818
819 CXIdxClientContainer
820 IndexingContext::getClientContainerForDC(const DeclContext *DC) const {
821   if (!DC)
822     return 0;
823
824   ContainerMapTy::const_iterator I = ContainerMap.find(DC);
825   if (I == ContainerMap.end())
826     return 0;
827
828   return I->second;
829 }
830
831 CXIdxClientFile IndexingContext::getIndexFile(const FileEntry *File) {
832   if (!File)
833     return 0;
834
835   FileMapTy::iterator FI = FileMap.find(File);
836   if (FI != FileMap.end())
837     return FI->second;
838
839   return 0;
840 }
841
842 CXIdxLoc IndexingContext::getIndexLoc(SourceLocation Loc) const {
843   CXIdxLoc idxLoc =  { {0, 0}, 0 };
844   if (Loc.isInvalid())
845     return idxLoc;
846
847   idxLoc.ptr_data[0] = (void*)this;
848   idxLoc.int_data = Loc.getRawEncoding();
849   return idxLoc;
850 }
851
852 void IndexingContext::translateLoc(SourceLocation Loc,
853                                    CXIdxClientFile *indexFile, CXFile *file,
854                                    unsigned *line, unsigned *column,
855                                    unsigned *offset) {
856   if (Loc.isInvalid())
857     return;
858
859   SourceManager &SM = Ctx->getSourceManager();
860   Loc = SM.getFileLoc(Loc);
861
862   std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
863   FileID FID = LocInfo.first;
864   unsigned FileOffset = LocInfo.second;
865
866   if (FID.isInvalid())
867     return;
868   
869   const FileEntry *FE = SM.getFileEntryForID(FID);
870   if (indexFile)
871     *indexFile = getIndexFile(FE);
872   if (file)
873     *file = (void *)FE;
874   if (line)
875     *line = SM.getLineNumber(FID, FileOffset);
876   if (column)
877     *column = SM.getColumnNumber(FID, FileOffset);
878   if (offset)
879     *offset = FileOffset;
880 }
881
882 void IndexingContext::getEntityInfo(const NamedDecl *D,
883                                     EntityInfo &EntityInfo,
884                                     ScratchAlloc &SA) {
885   if (!D)
886     return;
887
888   D = getEntityDecl(D);
889   EntityInfo.cursor = getCursor(D);
890   EntityInfo.Dcl = D;
891   EntityInfo.IndexCtx = this;
892   EntityInfo.kind = CXIdxEntity_Unexposed;
893   EntityInfo.templateKind = CXIdxEntity_NonTemplate;
894   EntityInfo.lang = CXIdxEntityLang_C;
895
896   if (D->hasAttrs()) {
897     EntityInfo.AttrList = AttrListInfo::create(D, *this);
898     EntityInfo.attributes = EntityInfo.AttrList->getAttrs();
899     EntityInfo.numAttributes = EntityInfo.AttrList->getNumAttrs();
900   }
901
902   if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
903     switch (TD->getTagKind()) {
904     case TTK_Struct:
905       EntityInfo.kind = CXIdxEntity_Struct; break;
906     case TTK_Union:
907       EntityInfo.kind = CXIdxEntity_Union; break;
908     case TTK_Class:
909       EntityInfo.kind = CXIdxEntity_CXXClass;
910       EntityInfo.lang = CXIdxEntityLang_CXX;
911       break;
912     case TTK_Interface:
913       EntityInfo.kind = CXIdxEntity_CXXInterface;
914       EntityInfo.lang = CXIdxEntityLang_CXX;
915       break;
916     case TTK_Enum:
917       EntityInfo.kind = CXIdxEntity_Enum; break;
918     }
919
920     if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D))
921       if (!CXXRec->isCLike())
922         EntityInfo.lang = CXIdxEntityLang_CXX;
923
924     if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
925       EntityInfo.templateKind = CXIdxEntity_TemplatePartialSpecialization;
926     } else if (isa<ClassTemplateSpecializationDecl>(D)) {
927       EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
928     }
929
930   } else {
931     switch (D->getKind()) {
932     case Decl::Typedef:
933       EntityInfo.kind = CXIdxEntity_Typedef; break;
934     case Decl::Function:
935       EntityInfo.kind = CXIdxEntity_Function;
936       break;
937     case Decl::ParmVar:
938       EntityInfo.kind = CXIdxEntity_Variable;
939       break;
940     case Decl::Var:
941       EntityInfo.kind = CXIdxEntity_Variable;
942       if (isa<CXXRecordDecl>(D->getDeclContext())) {
943         EntityInfo.kind = CXIdxEntity_CXXStaticVariable;
944         EntityInfo.lang = CXIdxEntityLang_CXX;
945       }
946       break;
947     case Decl::Field:
948       EntityInfo.kind = CXIdxEntity_Field;
949       if (const CXXRecordDecl *
950             CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
951         // FIXME: isPOD check is not sufficient, a POD can contain methods,
952         // we want a isCStructLike check.
953         if (!CXXRec->isPOD())
954           EntityInfo.lang = CXIdxEntityLang_CXX;
955       }
956       break;
957     case Decl::EnumConstant:
958       EntityInfo.kind = CXIdxEntity_EnumConstant; break;
959     case Decl::ObjCInterface:
960       EntityInfo.kind = CXIdxEntity_ObjCClass;
961       EntityInfo.lang = CXIdxEntityLang_ObjC;
962       break;
963     case Decl::ObjCProtocol:
964       EntityInfo.kind = CXIdxEntity_ObjCProtocol;
965       EntityInfo.lang = CXIdxEntityLang_ObjC;
966       break;
967     case Decl::ObjCCategory:
968       EntityInfo.kind = CXIdxEntity_ObjCCategory;
969       EntityInfo.lang = CXIdxEntityLang_ObjC;
970       break;
971     case Decl::ObjCMethod:
972       if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
973         EntityInfo.kind = CXIdxEntity_ObjCInstanceMethod;
974       else
975         EntityInfo.kind = CXIdxEntity_ObjCClassMethod;
976       EntityInfo.lang = CXIdxEntityLang_ObjC;
977       break;
978     case Decl::ObjCProperty:
979       EntityInfo.kind = CXIdxEntity_ObjCProperty;
980       EntityInfo.lang = CXIdxEntityLang_ObjC;
981       break;
982     case Decl::ObjCIvar:
983       EntityInfo.kind = CXIdxEntity_ObjCIvar;
984       EntityInfo.lang = CXIdxEntityLang_ObjC;
985       break;
986     case Decl::Namespace:
987       EntityInfo.kind = CXIdxEntity_CXXNamespace;
988       EntityInfo.lang = CXIdxEntityLang_CXX;
989       break;
990     case Decl::NamespaceAlias:
991       EntityInfo.kind = CXIdxEntity_CXXNamespaceAlias;
992       EntityInfo.lang = CXIdxEntityLang_CXX;
993       break;
994     case Decl::CXXConstructor:
995       EntityInfo.kind = CXIdxEntity_CXXConstructor;
996       EntityInfo.lang = CXIdxEntityLang_CXX;
997       break;
998     case Decl::CXXDestructor:
999       EntityInfo.kind = CXIdxEntity_CXXDestructor;
1000       EntityInfo.lang = CXIdxEntityLang_CXX;
1001       break;
1002     case Decl::CXXConversion:
1003       EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
1004       EntityInfo.lang = CXIdxEntityLang_CXX;
1005       break;
1006     case Decl::CXXMethod: {
1007       const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
1008       if (MD->isStatic())
1009         EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
1010       else
1011         EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
1012       EntityInfo.lang = CXIdxEntityLang_CXX;
1013       break;
1014     }
1015     case Decl::ClassTemplate:
1016       EntityInfo.kind = CXIdxEntity_CXXClass;
1017       EntityInfo.templateKind = CXIdxEntity_Template;
1018       break;
1019     case Decl::FunctionTemplate:
1020       EntityInfo.kind = CXIdxEntity_Function;
1021       EntityInfo.templateKind = CXIdxEntity_Template;
1022       if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
1023                            cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
1024         if (isa<CXXConstructorDecl>(MD))
1025           EntityInfo.kind = CXIdxEntity_CXXConstructor;
1026         else if (isa<CXXDestructorDecl>(MD))
1027           EntityInfo.kind = CXIdxEntity_CXXDestructor;
1028         else if (isa<CXXConversionDecl>(MD))
1029           EntityInfo.kind = CXIdxEntity_CXXConversionFunction;
1030         else {
1031           if (MD->isStatic())
1032             EntityInfo.kind = CXIdxEntity_CXXStaticMethod;
1033           else
1034             EntityInfo.kind = CXIdxEntity_CXXInstanceMethod;
1035         }
1036       }
1037       break;
1038     case Decl::TypeAliasTemplate:
1039       EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
1040       EntityInfo.templateKind = CXIdxEntity_Template;
1041       break;
1042     case Decl::TypeAlias:
1043       EntityInfo.kind = CXIdxEntity_CXXTypeAlias;
1044       EntityInfo.lang = CXIdxEntityLang_CXX;
1045       break;
1046     default:
1047       break;
1048     }
1049   }
1050
1051   if (EntityInfo.kind == CXIdxEntity_Unexposed)
1052     return;
1053
1054   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1055     if (FD->getTemplatedKind() ==
1056           FunctionDecl::TK_FunctionTemplateSpecialization)
1057       EntityInfo.templateKind = CXIdxEntity_TemplateSpecialization;
1058   }
1059
1060   if (EntityInfo.templateKind != CXIdxEntity_NonTemplate)
1061     EntityInfo.lang = CXIdxEntityLang_CXX;
1062
1063   if (IdentifierInfo *II = D->getIdentifier()) {
1064     EntityInfo.name = SA.toCStr(II->getName());
1065
1066   } else if (isa<TagDecl>(D) || isa<FieldDecl>(D) || isa<NamespaceDecl>(D)) {
1067     EntityInfo.name = 0; // anonymous tag/field/namespace.
1068
1069   } else {
1070     SmallString<256> StrBuf;
1071     {
1072       llvm::raw_svector_ostream OS(StrBuf);
1073       D->printName(OS);
1074     }
1075     EntityInfo.name = SA.copyCStr(StrBuf.str());
1076   }
1077
1078   {
1079     SmallString<512> StrBuf;
1080     bool Ignore = getDeclCursorUSR(D, StrBuf);
1081     if (Ignore) {
1082       EntityInfo.USR = 0;
1083     } else {
1084       EntityInfo.USR = SA.copyCStr(StrBuf.str());
1085     }
1086   }
1087 }
1088
1089 void IndexingContext::getContainerInfo(const DeclContext *DC,
1090                                        ContainerInfo &ContInfo) {
1091   ContInfo.cursor = getCursor(cast<Decl>(DC));
1092   ContInfo.DC = DC;
1093   ContInfo.IndexCtx = this;
1094 }
1095
1096 CXCursor IndexingContext::getRefCursor(const NamedDecl *D, SourceLocation Loc) {
1097   if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
1098     return MakeCursorTypeRef(TD, Loc, CXTU);
1099   if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
1100     return MakeCursorObjCClassRef(ID, Loc, CXTU);
1101   if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
1102     return MakeCursorObjCProtocolRef(PD, Loc, CXTU);
1103   if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D))
1104     return MakeCursorTemplateRef(Template, Loc, CXTU);
1105   if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D))
1106     return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1107   if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D))
1108     return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1109   if (const FieldDecl *Field = dyn_cast<FieldDecl>(D))
1110     return MakeCursorMemberRef(Field, Loc, CXTU);
1111   if (const VarDecl *Var = dyn_cast<VarDecl>(D))
1112     return MakeCursorVariableRef(Var, Loc, CXTU);
1113   
1114   return clang_getNullCursor();
1115 }
1116
1117 bool IndexingContext::shouldIgnoreIfImplicit(const Decl *D) {
1118   if (isa<ObjCInterfaceDecl>(D))
1119     return false;
1120   if (isa<ObjCCategoryDecl>(D))
1121     return false;
1122   if (isa<ObjCIvarDecl>(D))
1123     return false;
1124   if (isa<ObjCMethodDecl>(D))
1125     return false;
1126   if (isa<ImportDecl>(D))
1127     return false;
1128   return true;
1129 }
1130
1131 bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
1132   if (const ClassTemplateSpecializationDecl *
1133         SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1134     return SD->getSpecializationKind() == TSK_ImplicitInstantiation;
1135   }
1136   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1137     return FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation;
1138   }
1139   return false;
1140 }