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