CIndex.cpp revision 5f688f4b15d02aa7ad159c46b1f78fe59d412f12
1//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
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// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CIndexer.h"
16#include "CXCursor.h"
17#include "CXTranslationUnit.h"
18#include "CXString.h"
19#include "CXType.h"
20#include "CXSourceLocation.h"
21#include "CIndexDiagnostic.h"
22#include "CursorVisitor.h"
23
24#include "clang/Basic/Version.h"
25
26#include "clang/AST/StmtVisitor.h"
27#include "clang/Basic/Diagnostic.h"
28#include "clang/Frontend/ASTUnit.h"
29#include "clang/Frontend/CompilerInstance.h"
30#include "clang/Frontend/FrontendDiagnostic.h"
31#include "clang/Lex/Lexer.h"
32#include "clang/Lex/HeaderSearch.h"
33#include "clang/Lex/PreprocessingRecord.h"
34#include "clang/Lex/Preprocessor.h"
35#include "llvm/ADT/STLExtras.h"
36#include "llvm/ADT/Optional.h"
37#include "llvm/ADT/StringSwitch.h"
38#include "clang/Analysis/Support/SaveAndRestore.h"
39#include "llvm/Support/CrashRecoveryContext.h"
40#include "llvm/Support/PrettyStackTrace.h"
41#include "llvm/Support/MemoryBuffer.h"
42#include "llvm/Support/raw_ostream.h"
43#include "llvm/Support/Timer.h"
44#include "llvm/Support/Mutex.h"
45#include "llvm/Support/Program.h"
46#include "llvm/Support/Signals.h"
47#include "llvm/Support/Threading.h"
48#include "llvm/Support/Compiler.h"
49
50using namespace clang;
51using namespace clang::cxcursor;
52using namespace clang::cxstring;
53using namespace clang::cxtu;
54
55CXTranslationUnit cxtu::MakeCXTranslationUnit(ASTUnit *TU) {
56  if (!TU)
57    return 0;
58  CXTranslationUnit D = new CXTranslationUnitImpl();
59  D->TUData = TU;
60  D->StringPool = createCXStringPool();
61  D->Diagnostics = 0;
62  return D;
63}
64
65cxtu::CXTUOwner::~CXTUOwner() {
66  if (TU)
67    clang_disposeTranslationUnit(TU);
68}
69
70/// \brief Compare two source ranges to determine their relative position in
71/// the translation unit.
72static RangeComparisonResult RangeCompare(SourceManager &SM,
73                                          SourceRange R1,
74                                          SourceRange R2) {
75  assert(R1.isValid() && "First range is invalid?");
76  assert(R2.isValid() && "Second range is invalid?");
77  if (R1.getEnd() != R2.getBegin() &&
78      SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
79    return RangeBefore;
80  if (R2.getEnd() != R1.getBegin() &&
81      SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
82    return RangeAfter;
83  return RangeOverlap;
84}
85
86/// \brief Determine if a source location falls within, before, or after a
87///   a given source range.
88static RangeComparisonResult LocationCompare(SourceManager &SM,
89                                             SourceLocation L, SourceRange R) {
90  assert(R.isValid() && "First range is invalid?");
91  assert(L.isValid() && "Second range is invalid?");
92  if (L == R.getBegin() || L == R.getEnd())
93    return RangeOverlap;
94  if (SM.isBeforeInTranslationUnit(L, R.getBegin()))
95    return RangeBefore;
96  if (SM.isBeforeInTranslationUnit(R.getEnd(), L))
97    return RangeAfter;
98  return RangeOverlap;
99}
100
101/// \brief Translate a Clang source range into a CIndex source range.
102///
103/// Clang internally represents ranges where the end location points to the
104/// start of the token at the end. However, for external clients it is more
105/// useful to have a CXSourceRange be a proper half-open interval. This routine
106/// does the appropriate translation.
107CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
108                                          const LangOptions &LangOpts,
109                                          const CharSourceRange &R) {
110  // We want the last character in this location, so we will adjust the
111  // location accordingly.
112  SourceLocation EndLoc = R.getEnd();
113  if (EndLoc.isValid() && EndLoc.isMacroID())
114    EndLoc = SM.getExpansionRange(EndLoc).second;
115  if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) {
116    unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
117    EndLoc = EndLoc.getLocWithOffset(Length);
118  }
119
120  CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
121                           R.getBegin().getRawEncoding(),
122                           EndLoc.getRawEncoding() };
123  return Result;
124}
125
126//===----------------------------------------------------------------------===//
127// Cursor visitor.
128//===----------------------------------------------------------------------===//
129
130static SourceRange getRawCursorExtent(CXCursor C);
131static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
132
133
134RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
135  return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
136}
137
138/// \brief Visit the given cursor and, if requested by the visitor,
139/// its children.
140///
141/// \param Cursor the cursor to visit.
142///
143/// \param CheckRegionOfInterest if true, then the caller already checked that
144/// this cursor is within the region of interest.
145///
146/// \returns true if the visitation should be aborted, false if it
147/// should continue.
148bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
149  if (clang_isInvalid(Cursor.kind))
150    return false;
151
152  if (clang_isDeclaration(Cursor.kind)) {
153    Decl *D = getCursorDecl(Cursor);
154    if (!D) {
155      assert(0 && "Invalid declaration cursor");
156      return true; // abort.
157    }
158
159    // Ignore implicit declarations, unless it's an objc method because
160    // currently we should report implicit methods for properties when indexing.
161    if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
162      return false;
163  }
164
165  // If we have a range of interest, and this cursor doesn't intersect with it,
166  // we're done.
167  if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
168    SourceRange Range = getRawCursorExtent(Cursor);
169    if (Range.isInvalid() || CompareRegionOfInterest(Range))
170      return false;
171  }
172
173  switch (Visitor(Cursor, Parent, ClientData)) {
174  case CXChildVisit_Break:
175    return true;
176
177  case CXChildVisit_Continue:
178    return false;
179
180  case CXChildVisit_Recurse:
181    return VisitChildren(Cursor);
182  }
183
184  llvm_unreachable("Invalid CXChildVisitResult!");
185}
186
187static bool visitPreprocessedEntitiesInRange(SourceRange R,
188                                             PreprocessingRecord &PPRec,
189                                             CursorVisitor &Visitor) {
190  SourceManager &SM = Visitor.getASTUnit()->getSourceManager();
191  FileID FID;
192
193  if (!Visitor.shouldVisitIncludedEntities()) {
194    // If the begin/end of the range lie in the same FileID, do the optimization
195    // where we skip preprocessed entities that do not come from the same FileID.
196    FID = SM.getFileID(SM.getFileLoc(R.getBegin()));
197    if (FID != SM.getFileID(SM.getFileLoc(R.getEnd())))
198      FID = FileID();
199  }
200
201  std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
202    Entities = PPRec.getPreprocessedEntitiesInRange(R);
203  return Visitor.visitPreprocessedEntities(Entities.first, Entities.second,
204                                           PPRec, FID);
205}
206
207void CursorVisitor::visitFileRegion() {
208  if (RegionOfInterest.isInvalid())
209    return;
210
211  ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData);
212  SourceManager &SM = Unit->getSourceManager();
213
214  std::pair<FileID, unsigned>
215    Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())),
216    End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd()));
217
218  if (End.first != Begin.first) {
219    // If the end does not reside in the same file, try to recover by
220    // picking the end of the file of begin location.
221    End.first = Begin.first;
222    End.second = SM.getFileIDSize(Begin.first);
223  }
224
225  assert(Begin.first == End.first);
226  if (Begin.second > End.second)
227    return;
228
229  FileID File = Begin.first;
230  unsigned Offset = Begin.second;
231  unsigned Length = End.second - Begin.second;
232
233  if (!VisitDeclsOnly && !VisitPreprocessorLast)
234    if (visitPreprocessedEntitiesInRegion())
235      return; // visitation break.
236
237  visitDeclsFromFileRegion(File, Offset, Length);
238
239  if (!VisitDeclsOnly && VisitPreprocessorLast)
240    visitPreprocessedEntitiesInRegion();
241}
242
243static bool isInLexicalContext(Decl *D, DeclContext *DC) {
244  if (!DC)
245    return false;
246
247  for (DeclContext *DeclDC = D->getLexicalDeclContext();
248         DeclDC; DeclDC = DeclDC->getLexicalParent()) {
249    if (DeclDC == DC)
250      return true;
251  }
252  return false;
253}
254
255void CursorVisitor::visitDeclsFromFileRegion(FileID File,
256                                             unsigned Offset, unsigned Length) {
257  ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData);
258  SourceManager &SM = Unit->getSourceManager();
259  SourceRange Range = RegionOfInterest;
260
261  SmallVector<Decl *, 16> Decls;
262  Unit->findFileRegionDecls(File, Offset, Length, Decls);
263
264  // If we didn't find any file level decls for the file, try looking at the
265  // file that it was included from.
266  while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) {
267    bool Invalid = false;
268    const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid);
269    if (Invalid)
270      return;
271
272    SourceLocation Outer;
273    if (SLEntry.isFile())
274      Outer = SLEntry.getFile().getIncludeLoc();
275    else
276      Outer = SLEntry.getExpansion().getExpansionLocStart();
277    if (Outer.isInvalid())
278      return;
279
280    llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer);
281    Length = 0;
282    Unit->findFileRegionDecls(File, Offset, Length, Decls);
283  }
284
285  assert(!Decls.empty());
286
287  bool VisitedAtLeastOnce = false;
288  DeclContext *CurDC = 0;
289  SmallVector<Decl *, 16>::iterator DIt = Decls.begin();
290  for (SmallVector<Decl *, 16>::iterator DE = Decls.end(); DIt != DE; ++DIt) {
291    Decl *D = *DIt;
292    if (D->getSourceRange().isInvalid())
293      continue;
294
295    if (isInLexicalContext(D, CurDC))
296      continue;
297
298    CurDC = dyn_cast<DeclContext>(D);
299
300    if (TagDecl *TD = dyn_cast<TagDecl>(D))
301      if (!TD->isFreeStanding())
302        continue;
303
304    RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range);
305    if (CompRes == RangeBefore)
306      continue;
307    if (CompRes == RangeAfter)
308      break;
309
310    assert(CompRes == RangeOverlap);
311    VisitedAtLeastOnce = true;
312
313    if (isa<ObjCContainerDecl>(D)) {
314      FileDI_current = &DIt;
315      FileDE_current = DE;
316    } else {
317      FileDI_current = 0;
318    }
319
320    if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
321      break;
322  }
323
324  if (VisitedAtLeastOnce)
325    return;
326
327  // No Decls overlapped with the range. Move up the lexical context until there
328  // is a context that contains the range or we reach the translation unit
329  // level.
330  DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext()
331                                         : (*(DIt-1))->getLexicalDeclContext();
332
333  while (DC && !DC->isTranslationUnit()) {
334    Decl *D = cast<Decl>(DC);
335    SourceRange CurDeclRange = D->getSourceRange();
336    if (CurDeclRange.isInvalid())
337      break;
338
339    if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) {
340      Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true);
341      break;
342    }
343
344    DC = D->getLexicalDeclContext();
345  }
346}
347
348bool CursorVisitor::visitPreprocessedEntitiesInRegion() {
349  if (!AU->getPreprocessor().getPreprocessingRecord())
350    return false;
351
352  PreprocessingRecord &PPRec
353    = *AU->getPreprocessor().getPreprocessingRecord();
354  SourceManager &SM = AU->getSourceManager();
355
356  if (RegionOfInterest.isValid()) {
357    SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest);
358    SourceLocation B = MappedRange.getBegin();
359    SourceLocation E = MappedRange.getEnd();
360
361    if (AU->isInPreambleFileID(B)) {
362      if (SM.isLoadedSourceLocation(E))
363        return visitPreprocessedEntitiesInRange(SourceRange(B, E),
364                                                 PPRec, *this);
365
366      // Beginning of range lies in the preamble but it also extends beyond
367      // it into the main file. Split the range into 2 parts, one covering
368      // the preamble and another covering the main file. This allows subsequent
369      // calls to visitPreprocessedEntitiesInRange to accept a source range that
370      // lies in the same FileID, allowing it to skip preprocessed entities that
371      // do not come from the same FileID.
372      bool breaked =
373        visitPreprocessedEntitiesInRange(
374                                   SourceRange(B, AU->getEndOfPreambleFileID()),
375                                          PPRec, *this);
376      if (breaked) return true;
377      return visitPreprocessedEntitiesInRange(
378                                    SourceRange(AU->getStartOfMainFileID(), E),
379                                        PPRec, *this);
380    }
381
382    return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this);
383  }
384
385  bool OnlyLocalDecls
386    = !AU->isMainFileAST() && AU->getOnlyLocalDecls();
387
388  if (OnlyLocalDecls)
389    return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(),
390                                     PPRec);
391
392  return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec);
393}
394
395template<typename InputIterator>
396bool CursorVisitor::visitPreprocessedEntities(InputIterator First,
397                                              InputIterator Last,
398                                              PreprocessingRecord &PPRec,
399                                              FileID FID) {
400  for (; First != Last; ++First) {
401    if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID))
402      continue;
403
404    PreprocessedEntity *PPE = *First;
405    if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) {
406      if (Visit(MakeMacroExpansionCursor(ME, TU)))
407        return true;
408
409      continue;
410    }
411
412    if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) {
413      if (Visit(MakeMacroDefinitionCursor(MD, TU)))
414        return true;
415
416      continue;
417    }
418
419    if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
420      if (Visit(MakeInclusionDirectiveCursor(ID, TU)))
421        return true;
422
423      continue;
424    }
425  }
426
427  return false;
428}
429
430/// \brief Visit the children of the given cursor.
431///
432/// \returns true if the visitation should be aborted, false if it
433/// should continue.
434bool CursorVisitor::VisitChildren(CXCursor Cursor) {
435  if (clang_isReference(Cursor.kind) &&
436      Cursor.kind != CXCursor_CXXBaseSpecifier) {
437    // By definition, references have no children.
438    return false;
439  }
440
441  // Set the Parent field to Cursor, then back to its old value once we're
442  // done.
443  SetParentRAII SetParent(Parent, StmtParent, Cursor);
444
445  if (clang_isDeclaration(Cursor.kind)) {
446    Decl *D = getCursorDecl(Cursor);
447    if (!D)
448      return false;
449
450    return VisitAttributes(D) || Visit(D);
451  }
452
453  if (clang_isStatement(Cursor.kind)) {
454    if (Stmt *S = getCursorStmt(Cursor))
455      return Visit(S);
456
457    return false;
458  }
459
460  if (clang_isExpression(Cursor.kind)) {
461    if (Expr *E = getCursorExpr(Cursor))
462      return Visit(E);
463
464    return false;
465  }
466
467  if (clang_isTranslationUnit(Cursor.kind)) {
468    CXTranslationUnit tu = getCursorTU(Cursor);
469    ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData);
470
471    int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast };
472    for (unsigned I = 0; I != 2; ++I) {
473      if (VisitOrder[I]) {
474        if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
475            RegionOfInterest.isInvalid()) {
476          for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
477                                        TLEnd = CXXUnit->top_level_end();
478               TL != TLEnd; ++TL) {
479            if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true))
480              return true;
481          }
482        } else if (VisitDeclContext(
483                                CXXUnit->getASTContext().getTranslationUnitDecl()))
484          return true;
485        continue;
486      }
487
488      // Walk the preprocessing record.
489      if (CXXUnit->getPreprocessor().getPreprocessingRecord())
490        visitPreprocessedEntitiesInRegion();
491    }
492
493    return false;
494  }
495
496  if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
497    if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) {
498      if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) {
499        return Visit(BaseTSInfo->getTypeLoc());
500      }
501    }
502  }
503
504  if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
505    IBOutletCollectionAttr *A =
506      cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor));
507    if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>())
508      return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(),
509                                                    A->getInterfaceLoc(), TU));
510  }
511
512  // Nothing to visit at the moment.
513  return false;
514}
515
516bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
517  if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten())
518    if (Visit(TSInfo->getTypeLoc()))
519        return true;
520
521  if (Stmt *Body = B->getBody())
522    return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest));
523
524  return false;
525}
526
527llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
528  if (RegionOfInterest.isValid()) {
529    SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
530    if (Range.isInvalid())
531      return llvm::Optional<bool>();
532
533    switch (CompareRegionOfInterest(Range)) {
534    case RangeBefore:
535      // This declaration comes before the region of interest; skip it.
536      return llvm::Optional<bool>();
537
538    case RangeAfter:
539      // This declaration comes after the region of interest; we're done.
540      return false;
541
542    case RangeOverlap:
543      // This declaration overlaps the region of interest; visit it.
544      break;
545    }
546  }
547  return true;
548}
549
550bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
551  DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
552
553  // FIXME: Eventually remove.  This part of a hack to support proper
554  // iteration over all Decls contained lexically within an ObjC container.
555  SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I);
556  SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E);
557
558  for ( ; I != E; ++I) {
559    Decl *D = *I;
560    if (D->getLexicalDeclContext() != DC)
561      continue;
562    CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest);
563
564    // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol
565    // declarations is a mismatch with the compiler semantics.
566    if (Cursor.kind == CXCursor_ObjCInterfaceDecl) {
567      ObjCInterfaceDecl *ID = cast<ObjCInterfaceDecl>(D);
568      if (!ID->isThisDeclarationADefinition())
569        Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU);
570
571    } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) {
572      ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
573      if (!PD->isThisDeclarationADefinition())
574        Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU);
575    }
576
577    const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
578    if (!V.hasValue())
579      continue;
580    if (!V.getValue())
581      return false;
582    if (Visit(Cursor, true))
583      return true;
584  }
585  return false;
586}
587
588bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
589  llvm_unreachable("Translation units are visited directly by Visit()");
590}
591
592bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) {
593  if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
594    return Visit(TSInfo->getTypeLoc());
595
596  return false;
597}
598
599bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
600  if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
601    return Visit(TSInfo->getTypeLoc());
602
603  return false;
604}
605
606bool CursorVisitor::VisitTagDecl(TagDecl *D) {
607  return VisitDeclContext(D);
608}
609
610bool CursorVisitor::VisitClassTemplateSpecializationDecl(
611                                          ClassTemplateSpecializationDecl *D) {
612  bool ShouldVisitBody = false;
613  switch (D->getSpecializationKind()) {
614  case TSK_Undeclared:
615  case TSK_ImplicitInstantiation:
616    // Nothing to visit
617    return false;
618
619  case TSK_ExplicitInstantiationDeclaration:
620  case TSK_ExplicitInstantiationDefinition:
621    break;
622
623  case TSK_ExplicitSpecialization:
624    ShouldVisitBody = true;
625    break;
626  }
627
628  // Visit the template arguments used in the specialization.
629  if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) {
630    TypeLoc TL = SpecType->getTypeLoc();
631    if (TemplateSpecializationTypeLoc *TSTLoc
632          = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) {
633      for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I)
634        if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I)))
635          return true;
636    }
637  }
638
639  if (ShouldVisitBody && VisitCXXRecordDecl(D))
640    return true;
641
642  return false;
643}
644
645bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl(
646                                   ClassTemplatePartialSpecializationDecl *D) {
647  // FIXME: Visit the "outer" template parameter lists on the TagDecl
648  // before visiting these template parameters.
649  if (VisitTemplateParameters(D->getTemplateParameters()))
650    return true;
651
652  // Visit the partial specialization arguments.
653  const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten();
654  for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I)
655    if (VisitTemplateArgumentLoc(TemplateArgs[I]))
656      return true;
657
658  return VisitCXXRecordDecl(D);
659}
660
661bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
662  // Visit the default argument.
663  if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
664    if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo())
665      if (Visit(DefArg->getTypeLoc()))
666        return true;
667
668  return false;
669}
670
671bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
672  if (Expr *Init = D->getInitExpr())
673    return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
674  return false;
675}
676
677bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
678  if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
679    if (Visit(TSInfo->getTypeLoc()))
680      return true;
681
682  // Visit the nested-name-specifier, if present.
683  if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc())
684    if (VisitNestedNameSpecifierLoc(QualifierLoc))
685      return true;
686
687  return false;
688}
689
690/// \brief Compare two base or member initializers based on their source order.
691static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) {
692  CXXCtorInitializer const * const *X
693    = static_cast<CXXCtorInitializer const * const *>(Xp);
694  CXXCtorInitializer const * const *Y
695    = static_cast<CXXCtorInitializer const * const *>(Yp);
696
697  if ((*X)->getSourceOrder() < (*Y)->getSourceOrder())
698    return -1;
699  else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder())
700    return 1;
701  else
702    return 0;
703}
704
705bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
706  if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
707    // Visit the function declaration's syntactic components in the order
708    // written. This requires a bit of work.
709    TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
710    FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL);
711
712    // If we have a function declared directly (without the use of a typedef),
713    // visit just the return type. Otherwise, just visit the function's type
714    // now.
715    if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) ||
716        (!FTL && Visit(TL)))
717      return true;
718
719    // Visit the nested-name-specifier, if present.
720    if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc())
721      if (VisitNestedNameSpecifierLoc(QualifierLoc))
722        return true;
723
724    // Visit the declaration name.
725    if (VisitDeclarationNameInfo(ND->getNameInfo()))
726      return true;
727
728    // FIXME: Visit explicitly-specified template arguments!
729
730    // Visit the function parameters, if we have a function type.
731    if (FTL && VisitFunctionTypeLoc(*FTL, true))
732      return true;
733
734    // FIXME: Attributes?
735  }
736
737  if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) {
738    if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
739      // Find the initializers that were written in the source.
740      SmallVector<CXXCtorInitializer *, 4> WrittenInits;
741      for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(),
742                                          IEnd = Constructor->init_end();
743           I != IEnd; ++I) {
744        if (!(*I)->isWritten())
745          continue;
746
747        WrittenInits.push_back(*I);
748      }
749
750      // Sort the initializers in source order
751      llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
752                           &CompareCXXCtorInitializers);
753
754      // Visit the initializers in source order
755      for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
756        CXXCtorInitializer *Init = WrittenInits[I];
757        if (Init->isAnyMemberInitializer()) {
758          if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
759                                        Init->getMemberLocation(), TU)))
760            return true;
761        } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) {
762          if (Visit(TInfo->getTypeLoc()))
763            return true;
764        }
765
766        // Visit the initializer value.
767        if (Expr *Initializer = Init->getInit())
768          if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest)))
769            return true;
770      }
771    }
772
773    if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
774      return true;
775  }
776
777  return false;
778}
779
780bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
781  if (VisitDeclaratorDecl(D))
782    return true;
783
784  if (Expr *BitWidth = D->getBitWidth())
785    return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest));
786
787  return false;
788}
789
790bool CursorVisitor::VisitVarDecl(VarDecl *D) {
791  if (VisitDeclaratorDecl(D))
792    return true;
793
794  if (Expr *Init = D->getInit())
795    return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest));
796
797  return false;
798}
799
800bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
801  if (VisitDeclaratorDecl(D))
802    return true;
803
804  if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited())
805    if (Expr *DefArg = D->getDefaultArgument())
806      return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest));
807
808  return false;
809}
810
811bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
812  // FIXME: Visit the "outer" template parameter lists on the FunctionDecl
813  // before visiting these template parameters.
814  if (VisitTemplateParameters(D->getTemplateParameters()))
815    return true;
816
817  return VisitFunctionDecl(D->getTemplatedDecl());
818}
819
820bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) {
821  // FIXME: Visit the "outer" template parameter lists on the TagDecl
822  // before visiting these template parameters.
823  if (VisitTemplateParameters(D->getTemplateParameters()))
824    return true;
825
826  return VisitCXXRecordDecl(D->getTemplatedDecl());
827}
828
829bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
830  if (VisitTemplateParameters(D->getTemplateParameters()))
831    return true;
832
833  if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() &&
834      VisitTemplateArgumentLoc(D->getDefaultArgument()))
835    return true;
836
837  return false;
838}
839
840bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
841  if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo())
842    if (Visit(TSInfo->getTypeLoc()))
843      return true;
844
845  for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
846       PEnd = ND->param_end();
847       P != PEnd; ++P) {
848    if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
849      return true;
850  }
851
852  if (ND->isThisDeclarationADefinition() &&
853      Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)))
854    return true;
855
856  return false;
857}
858
859template <typename DeclIt>
860static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current,
861                                      SourceManager &SM, SourceLocation EndLoc,
862                                      SmallVectorImpl<Decl *> &Decls) {
863  DeclIt next = *DI_current;
864  while (++next != DE_current) {
865    Decl *D_next = *next;
866    if (!D_next)
867      break;
868    SourceLocation L = D_next->getLocStart();
869    if (!L.isValid())
870      break;
871    if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
872      *DI_current = next;
873      Decls.push_back(D_next);
874      continue;
875    }
876    break;
877  }
878}
879
880namespace {
881  struct ContainerDeclsSort {
882    SourceManager &SM;
883    ContainerDeclsSort(SourceManager &sm) : SM(sm) {}
884    bool operator()(Decl *A, Decl *B) {
885      SourceLocation L_A = A->getLocStart();
886      SourceLocation L_B = B->getLocStart();
887      assert(L_A.isValid() && L_B.isValid());
888      return SM.isBeforeInTranslationUnit(L_A, L_B);
889    }
890  };
891}
892
893bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
894  // FIXME: Eventually convert back to just 'VisitDeclContext()'.  Essentially
895  // an @implementation can lexically contain Decls that are not properly
896  // nested in the AST.  When we identify such cases, we need to retrofit
897  // this nesting here.
898  if (!DI_current && !FileDI_current)
899    return VisitDeclContext(D);
900
901  // Scan the Decls that immediately come after the container
902  // in the current DeclContext.  If any fall within the
903  // container's lexical region, stash them into a vector
904  // for later processing.
905  SmallVector<Decl *, 24> DeclsInContainer;
906  SourceLocation EndLoc = D->getSourceRange().getEnd();
907  SourceManager &SM = AU->getSourceManager();
908  if (EndLoc.isValid()) {
909    if (DI_current) {
910      addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc,
911                                DeclsInContainer);
912    } else {
913      addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc,
914                                DeclsInContainer);
915    }
916  }
917
918  // The common case.
919  if (DeclsInContainer.empty())
920    return VisitDeclContext(D);
921
922  // Get all the Decls in the DeclContext, and sort them with the
923  // additional ones we've collected.  Then visit them.
924  for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
925       I!=E; ++I) {
926    Decl *subDecl = *I;
927    if (!subDecl || subDecl->getLexicalDeclContext() != D ||
928        subDecl->getLocStart().isInvalid())
929      continue;
930    DeclsInContainer.push_back(subDecl);
931  }
932
933  // Now sort the Decls so that they appear in lexical order.
934  std::sort(DeclsInContainer.begin(), DeclsInContainer.end(),
935            ContainerDeclsSort(SM));
936
937  // Now visit the decls.
938  for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(),
939         E = DeclsInContainer.end(); I != E; ++I) {
940    CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest);
941    const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
942    if (!V.hasValue())
943      continue;
944    if (!V.getValue())
945      return false;
946    if (Visit(Cursor, true))
947      return true;
948  }
949  return false;
950}
951
952bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
953  if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
954                                   TU)))
955    return true;
956
957  ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
958  for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
959         E = ND->protocol_end(); I != E; ++I, ++PL)
960    if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
961      return true;
962
963  return VisitObjCContainerDecl(ND);
964}
965
966bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
967  if (!PID->isThisDeclarationADefinition())
968    return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU));
969
970  ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
971  for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
972       E = PID->protocol_end(); I != E; ++I, ++PL)
973    if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
974      return true;
975
976  return VisitObjCContainerDecl(PID);
977}
978
979bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
980  if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
981    return true;
982
983  // FIXME: This implements a workaround with @property declarations also being
984  // installed in the DeclContext for the @interface.  Eventually this code
985  // should be removed.
986  ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext());
987  if (!CDecl || !CDecl->IsClassExtension())
988    return false;
989
990  ObjCInterfaceDecl *ID = CDecl->getClassInterface();
991  if (!ID)
992    return false;
993
994  IdentifierInfo *PropertyId = PD->getIdentifier();
995  ObjCPropertyDecl *prevDecl =
996    ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId);
997
998  if (!prevDecl)
999    return false;
1000
1001  // Visit synthesized methods since they will be skipped when visiting
1002  // the @interface.
1003  if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
1004    if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
1005      if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1006        return true;
1007
1008  if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
1009    if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
1010      if (Visit(MakeCXCursor(MD, TU, RegionOfInterest)))
1011        return true;
1012
1013  return false;
1014}
1015
1016bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
1017  if (!D->isThisDeclarationADefinition()) {
1018    // Forward declaration is treated like a reference.
1019    return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU));
1020  }
1021
1022  // Issue callbacks for super class.
1023  if (D->getSuperClass() &&
1024      Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
1025                                        D->getSuperClassLoc(),
1026                                        TU)))
1027    return true;
1028
1029  ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
1030  for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
1031         E = D->protocol_end(); I != E; ++I, ++PL)
1032    if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
1033      return true;
1034
1035  return VisitObjCContainerDecl(D);
1036}
1037
1038bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
1039  return VisitObjCContainerDecl(D);
1040}
1041
1042bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1043  // 'ID' could be null when dealing with invalid code.
1044  if (ObjCInterfaceDecl *ID = D->getClassInterface())
1045    if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU)))
1046      return true;
1047
1048  return VisitObjCImplDecl(D);
1049}
1050
1051bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1052#if 0
1053  // Issue callbacks for super class.
1054  // FIXME: No source location information!
1055  if (D->getSuperClass() &&
1056      Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
1057                                        D->getSuperClassLoc(),
1058                                        TU)))
1059    return true;
1060#endif
1061
1062  return VisitObjCImplDecl(D);
1063}
1064
1065bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
1066  if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
1067    return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
1068
1069  return false;
1070}
1071
1072bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
1073  return VisitDeclContext(D);
1074}
1075
1076bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1077  // Visit nested-name-specifier.
1078  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1079    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1080      return true;
1081
1082  return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
1083                                      D->getTargetNameLoc(), TU));
1084}
1085
1086bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
1087  // Visit nested-name-specifier.
1088  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1089    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1090      return true;
1091  }
1092
1093  if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
1094    return true;
1095
1096  return VisitDeclarationNameInfo(D->getNameInfo());
1097}
1098
1099bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1100  // Visit nested-name-specifier.
1101  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1102    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1103      return true;
1104
1105  return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
1106                                      D->getIdentLocation(), TU));
1107}
1108
1109bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1110  // Visit nested-name-specifier.
1111  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) {
1112    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1113      return true;
1114  }
1115
1116  return VisitDeclarationNameInfo(D->getNameInfo());
1117}
1118
1119bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
1120                                               UnresolvedUsingTypenameDecl *D) {
1121  // Visit nested-name-specifier.
1122  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1123    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1124      return true;
1125
1126  return false;
1127}
1128
1129bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
1130  switch (Name.getName().getNameKind()) {
1131  case clang::DeclarationName::Identifier:
1132  case clang::DeclarationName::CXXLiteralOperatorName:
1133  case clang::DeclarationName::CXXOperatorName:
1134  case clang::DeclarationName::CXXUsingDirective:
1135    return false;
1136
1137  case clang::DeclarationName::CXXConstructorName:
1138  case clang::DeclarationName::CXXDestructorName:
1139  case clang::DeclarationName::CXXConversionFunctionName:
1140    if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo())
1141      return Visit(TSInfo->getTypeLoc());
1142    return false;
1143
1144  case clang::DeclarationName::ObjCZeroArgSelector:
1145  case clang::DeclarationName::ObjCOneArgSelector:
1146  case clang::DeclarationName::ObjCMultiArgSelector:
1147    // FIXME: Per-identifier location info?
1148    return false;
1149  }
1150
1151  llvm_unreachable("Invalid DeclarationName::Kind!");
1152}
1153
1154bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS,
1155                                             SourceRange Range) {
1156  // FIXME: This whole routine is a hack to work around the lack of proper
1157  // source information in nested-name-specifiers (PR5791). Since we do have
1158  // a beginning source location, we can visit the first component of the
1159  // nested-name-specifier, if it's a single-token component.
1160  if (!NNS)
1161    return false;
1162
1163  // Get the first component in the nested-name-specifier.
1164  while (NestedNameSpecifier *Prefix = NNS->getPrefix())
1165    NNS = Prefix;
1166
1167  switch (NNS->getKind()) {
1168  case NestedNameSpecifier::Namespace:
1169    return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(),
1170                                        TU));
1171
1172  case NestedNameSpecifier::NamespaceAlias:
1173    return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1174                                        Range.getBegin(), TU));
1175
1176  case NestedNameSpecifier::TypeSpec: {
1177    // If the type has a form where we know that the beginning of the source
1178    // range matches up with a reference cursor. Visit the appropriate reference
1179    // cursor.
1180    const Type *T = NNS->getAsType();
1181    if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
1182      return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
1183    if (const TagType *Tag = dyn_cast<TagType>(T))
1184      return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU));
1185    if (const TemplateSpecializationType *TST
1186                                      = dyn_cast<TemplateSpecializationType>(T))
1187      return VisitTemplateName(TST->getTemplateName(), Range.getBegin());
1188    break;
1189  }
1190
1191  case NestedNameSpecifier::TypeSpecWithTemplate:
1192  case NestedNameSpecifier::Global:
1193  case NestedNameSpecifier::Identifier:
1194    break;
1195  }
1196
1197  return false;
1198}
1199
1200bool
1201CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1202  SmallVector<NestedNameSpecifierLoc, 4> Qualifiers;
1203  for (; Qualifier; Qualifier = Qualifier.getPrefix())
1204    Qualifiers.push_back(Qualifier);
1205
1206  while (!Qualifiers.empty()) {
1207    NestedNameSpecifierLoc Q = Qualifiers.pop_back_val();
1208    NestedNameSpecifier *NNS = Q.getNestedNameSpecifier();
1209    switch (NNS->getKind()) {
1210    case NestedNameSpecifier::Namespace:
1211      if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(),
1212                                       Q.getLocalBeginLoc(),
1213                                       TU)))
1214        return true;
1215
1216      break;
1217
1218    case NestedNameSpecifier::NamespaceAlias:
1219      if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(),
1220                                       Q.getLocalBeginLoc(),
1221                                       TU)))
1222        return true;
1223
1224      break;
1225
1226    case NestedNameSpecifier::TypeSpec:
1227    case NestedNameSpecifier::TypeSpecWithTemplate:
1228      if (Visit(Q.getTypeLoc()))
1229        return true;
1230
1231      break;
1232
1233    case NestedNameSpecifier::Global:
1234    case NestedNameSpecifier::Identifier:
1235      break;
1236    }
1237  }
1238
1239  return false;
1240}
1241
1242bool CursorVisitor::VisitTemplateParameters(
1243                                          const TemplateParameterList *Params) {
1244  if (!Params)
1245    return false;
1246
1247  for (TemplateParameterList::const_iterator P = Params->begin(),
1248                                          PEnd = Params->end();
1249       P != PEnd; ++P) {
1250    if (Visit(MakeCXCursor(*P, TU, RegionOfInterest)))
1251      return true;
1252  }
1253
1254  return false;
1255}
1256
1257bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
1258  switch (Name.getKind()) {
1259  case TemplateName::Template:
1260    return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
1261
1262  case TemplateName::OverloadedTemplate:
1263    // Visit the overloaded template set.
1264    if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
1265      return true;
1266
1267    return false;
1268
1269  case TemplateName::DependentTemplate:
1270    // FIXME: Visit nested-name-specifier.
1271    return false;
1272
1273  case TemplateName::QualifiedTemplate:
1274    // FIXME: Visit nested-name-specifier.
1275    return Visit(MakeCursorTemplateRef(
1276                                  Name.getAsQualifiedTemplateName()->getDecl(),
1277                                       Loc, TU));
1278
1279  case TemplateName::SubstTemplateTemplateParm:
1280    return Visit(MakeCursorTemplateRef(
1281                         Name.getAsSubstTemplateTemplateParm()->getParameter(),
1282                                       Loc, TU));
1283
1284  case TemplateName::SubstTemplateTemplateParmPack:
1285    return Visit(MakeCursorTemplateRef(
1286                  Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(),
1287                                       Loc, TU));
1288  }
1289
1290  llvm_unreachable("Invalid TemplateName::Kind!");
1291}
1292
1293bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
1294  switch (TAL.getArgument().getKind()) {
1295  case TemplateArgument::Null:
1296  case TemplateArgument::Integral:
1297  case TemplateArgument::Pack:
1298    return false;
1299
1300  case TemplateArgument::Type:
1301    if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
1302      return Visit(TSInfo->getTypeLoc());
1303    return false;
1304
1305  case TemplateArgument::Declaration:
1306    if (Expr *E = TAL.getSourceDeclExpression())
1307      return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1308    return false;
1309
1310  case TemplateArgument::Expression:
1311    if (Expr *E = TAL.getSourceExpression())
1312      return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest));
1313    return false;
1314
1315  case TemplateArgument::Template:
1316  case TemplateArgument::TemplateExpansion:
1317    if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc()))
1318      return true;
1319
1320    return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(),
1321                             TAL.getTemplateNameLoc());
1322  }
1323
1324  llvm_unreachable("Invalid TemplateArgument::Kind!");
1325}
1326
1327bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1328  return VisitDeclContext(D);
1329}
1330
1331bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1332  return Visit(TL.getUnqualifiedLoc());
1333}
1334
1335bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1336  ASTContext &Context = AU->getASTContext();
1337
1338  // Some builtin types (such as Objective-C's "id", "sel", and
1339  // "Class") have associated declarations. Create cursors for those.
1340  QualType VisitType;
1341  switch (TL.getTypePtr()->getKind()) {
1342
1343  case BuiltinType::Void:
1344  case BuiltinType::NullPtr:
1345  case BuiltinType::Dependent:
1346#define BUILTIN_TYPE(Id, SingletonId)
1347#define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1348#define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id:
1349#define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id:
1350#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
1351#include "clang/AST/BuiltinTypes.def"
1352    break;
1353
1354  case BuiltinType::ObjCId:
1355    VisitType = Context.getObjCIdType();
1356    break;
1357
1358  case BuiltinType::ObjCClass:
1359    VisitType = Context.getObjCClassType();
1360    break;
1361
1362  case BuiltinType::ObjCSel:
1363    VisitType = Context.getObjCSelType();
1364    break;
1365  }
1366
1367  if (!VisitType.isNull()) {
1368    if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
1369      return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
1370                                     TU));
1371  }
1372
1373  return false;
1374}
1375
1376bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1377  return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU));
1378}
1379
1380bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
1381  return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1382}
1383
1384bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
1385  if (TL.isDefinition())
1386    return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest));
1387
1388  return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1389}
1390
1391bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
1392  return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1393}
1394
1395bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1396  if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
1397    return true;
1398
1399  return false;
1400}
1401
1402bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1403  if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc()))
1404    return true;
1405
1406  for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
1407    if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
1408                                        TU)))
1409      return true;
1410  }
1411
1412  return false;
1413}
1414
1415bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1416  return Visit(TL.getPointeeLoc());
1417}
1418
1419bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
1420  return Visit(TL.getInnerLoc());
1421}
1422
1423bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
1424  return Visit(TL.getPointeeLoc());
1425}
1426
1427bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1428  return Visit(TL.getPointeeLoc());
1429}
1430
1431bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1432  return Visit(TL.getPointeeLoc());
1433}
1434
1435bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1436  return Visit(TL.getPointeeLoc());
1437}
1438
1439bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1440  return Visit(TL.getPointeeLoc());
1441}
1442
1443bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
1444  return Visit(TL.getModifiedLoc());
1445}
1446
1447bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL,
1448                                         bool SkipResultType) {
1449  if (!SkipResultType && Visit(TL.getResultLoc()))
1450    return true;
1451
1452  for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1453    if (Decl *D = TL.getArg(I))
1454      if (Visit(MakeCXCursor(D, TU, RegionOfInterest)))
1455        return true;
1456
1457  return false;
1458}
1459
1460bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
1461  if (Visit(TL.getElementLoc()))
1462    return true;
1463
1464  if (Expr *Size = TL.getSizeExpr())
1465    return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest));
1466
1467  return false;
1468}
1469
1470bool CursorVisitor::VisitTemplateSpecializationTypeLoc(
1471                                             TemplateSpecializationTypeLoc TL) {
1472  // Visit the template name.
1473  if (VisitTemplateName(TL.getTypePtr()->getTemplateName(),
1474                        TL.getTemplateNameLoc()))
1475    return true;
1476
1477  // Visit the template arguments.
1478  for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1479    if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1480      return true;
1481
1482  return false;
1483}
1484
1485bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1486  return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
1487}
1488
1489bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1490  if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1491    return Visit(TSInfo->getTypeLoc());
1492
1493  return false;
1494}
1495
1496bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
1497  if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
1498    return Visit(TSInfo->getTypeLoc());
1499
1500  return false;
1501}
1502
1503bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1504  if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1505    return true;
1506
1507  return false;
1508}
1509
1510bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc(
1511                                    DependentTemplateSpecializationTypeLoc TL) {
1512  // Visit the nested-name-specifier, if there is one.
1513  if (TL.getQualifierLoc() &&
1514      VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1515    return true;
1516
1517  // Visit the template arguments.
1518  for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
1519    if (VisitTemplateArgumentLoc(TL.getArgLoc(I)))
1520      return true;
1521
1522  return false;
1523}
1524
1525bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1526  if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc()))
1527    return true;
1528
1529  return Visit(TL.getNamedTypeLoc());
1530}
1531
1532bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
1533  return Visit(TL.getPatternLoc());
1534}
1535
1536bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
1537  if (Expr *E = TL.getUnderlyingExpr())
1538    return Visit(MakeCXCursor(E, StmtParent, TU));
1539
1540  return false;
1541}
1542
1543bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
1544  return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
1545}
1546
1547bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
1548  return Visit(TL.getValueLoc());
1549}
1550
1551#define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \
1552bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \
1553  return Visit##PARENT##Loc(TL); \
1554}
1555
1556DEFAULT_TYPELOC_IMPL(Complex, Type)
1557DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType)
1558DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType)
1559DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType)
1560DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType)
1561DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type)
1562DEFAULT_TYPELOC_IMPL(Vector, Type)
1563DEFAULT_TYPELOC_IMPL(ExtVector, VectorType)
1564DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType)
1565DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType)
1566DEFAULT_TYPELOC_IMPL(Record, TagType)
1567DEFAULT_TYPELOC_IMPL(Enum, TagType)
1568DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type)
1569DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type)
1570DEFAULT_TYPELOC_IMPL(Auto, Type)
1571
1572bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
1573  // Visit the nested-name-specifier, if present.
1574  if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc())
1575    if (VisitNestedNameSpecifierLoc(QualifierLoc))
1576      return true;
1577
1578  if (D->isCompleteDefinition()) {
1579    for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
1580         E = D->bases_end(); I != E; ++I) {
1581      if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU)))
1582        return true;
1583    }
1584  }
1585
1586  return VisitTagDecl(D);
1587}
1588
1589bool CursorVisitor::VisitAttributes(Decl *D) {
1590  for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end();
1591       i != e; ++i)
1592    if (Visit(MakeCXCursor(*i, D, TU)))
1593        return true;
1594
1595  return false;
1596}
1597
1598//===----------------------------------------------------------------------===//
1599// Data-recursive visitor methods.
1600//===----------------------------------------------------------------------===//
1601
1602namespace {
1603#define DEF_JOB(NAME, DATA, KIND)\
1604class NAME : public VisitorJob {\
1605public:\
1606  NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \
1607  static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
1608  DATA *get() const { return static_cast<DATA*>(data[0]); }\
1609};
1610
1611DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
1612DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
1613DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
1614DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
1615DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo,
1616        ExplicitTemplateArgsVisitKind)
1617DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
1618DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind)
1619#undef DEF_JOB
1620
1621class DeclVisit : public VisitorJob {
1622public:
1623  DeclVisit(Decl *d, CXCursor parent, bool isFirst) :
1624    VisitorJob(parent, VisitorJob::DeclVisitKind,
1625               d, isFirst ? (void*) 1 : (void*) 0) {}
1626  static bool classof(const VisitorJob *VJ) {
1627    return VJ->getKind() == DeclVisitKind;
1628  }
1629  Decl *get() const { return static_cast<Decl*>(data[0]); }
1630  bool isFirst() const { return data[1] ? true : false; }
1631};
1632class TypeLocVisit : public VisitorJob {
1633public:
1634  TypeLocVisit(TypeLoc tl, CXCursor parent) :
1635    VisitorJob(parent, VisitorJob::TypeLocVisitKind,
1636               tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
1637
1638  static bool classof(const VisitorJob *VJ) {
1639    return VJ->getKind() == TypeLocVisitKind;
1640  }
1641
1642  TypeLoc get() const {
1643    QualType T = QualType::getFromOpaquePtr(data[0]);
1644    return TypeLoc(T, data[1]);
1645  }
1646};
1647
1648class LabelRefVisit : public VisitorJob {
1649public:
1650  LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
1651    : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
1652                 labelLoc.getPtrEncoding()) {}
1653
1654  static bool classof(const VisitorJob *VJ) {
1655    return VJ->getKind() == VisitorJob::LabelRefVisitKind;
1656  }
1657  LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); }
1658  SourceLocation getLoc() const {
1659    return SourceLocation::getFromPtrEncoding(data[1]); }
1660};
1661
1662class NestedNameSpecifierLocVisit : public VisitorJob {
1663public:
1664  NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent)
1665    : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind,
1666                 Qualifier.getNestedNameSpecifier(),
1667                 Qualifier.getOpaqueData()) { }
1668
1669  static bool classof(const VisitorJob *VJ) {
1670    return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind;
1671  }
1672
1673  NestedNameSpecifierLoc get() const {
1674    return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]),
1675                                  data[1]);
1676  }
1677};
1678
1679class DeclarationNameInfoVisit : public VisitorJob {
1680public:
1681  DeclarationNameInfoVisit(Stmt *S, CXCursor parent)
1682    : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
1683  static bool classof(const VisitorJob *VJ) {
1684    return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
1685  }
1686  DeclarationNameInfo get() const {
1687    Stmt *S = static_cast<Stmt*>(data[0]);
1688    switch (S->getStmtClass()) {
1689    default:
1690      llvm_unreachable("Unhandled Stmt");
1691    case clang::Stmt::MSDependentExistsStmtClass:
1692      return cast<MSDependentExistsStmt>(S)->getNameInfo();
1693    case Stmt::CXXDependentScopeMemberExprClass:
1694      return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
1695    case Stmt::DependentScopeDeclRefExprClass:
1696      return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
1697    }
1698  }
1699};
1700class MemberRefVisit : public VisitorJob {
1701public:
1702  MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent)
1703    : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
1704                 L.getPtrEncoding()) {}
1705  static bool classof(const VisitorJob *VJ) {
1706    return VJ->getKind() == VisitorJob::MemberRefVisitKind;
1707  }
1708  FieldDecl *get() const {
1709    return static_cast<FieldDecl*>(data[0]);
1710  }
1711  SourceLocation getLoc() const {
1712    return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
1713  }
1714};
1715class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> {
1716  VisitorWorkList &WL;
1717  CXCursor Parent;
1718public:
1719  EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
1720    : WL(wl), Parent(parent) {}
1721
1722  void VisitAddrLabelExpr(AddrLabelExpr *E);
1723  void VisitBlockExpr(BlockExpr *B);
1724  void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
1725  void VisitCompoundStmt(CompoundStmt *S);
1726  void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ }
1727  void VisitMSDependentExistsStmt(MSDependentExistsStmt *S);
1728  void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
1729  void VisitCXXNewExpr(CXXNewExpr *E);
1730  void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
1731  void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
1732  void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
1733  void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
1734  void VisitCXXTypeidExpr(CXXTypeidExpr *E);
1735  void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
1736  void VisitCXXUuidofExpr(CXXUuidofExpr *E);
1737  void VisitCXXCatchStmt(CXXCatchStmt *S);
1738  void VisitDeclRefExpr(DeclRefExpr *D);
1739  void VisitDeclStmt(DeclStmt *S);
1740  void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
1741  void VisitDesignatedInitExpr(DesignatedInitExpr *E);
1742  void VisitExplicitCastExpr(ExplicitCastExpr *E);
1743  void VisitForStmt(ForStmt *FS);
1744  void VisitGotoStmt(GotoStmt *GS);
1745  void VisitIfStmt(IfStmt *If);
1746  void VisitInitListExpr(InitListExpr *IE);
1747  void VisitMemberExpr(MemberExpr *M);
1748  void VisitOffsetOfExpr(OffsetOfExpr *E);
1749  void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
1750  void VisitObjCMessageExpr(ObjCMessageExpr *M);
1751  void VisitOverloadExpr(OverloadExpr *E);
1752  void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
1753  void VisitStmt(Stmt *S);
1754  void VisitSwitchStmt(SwitchStmt *S);
1755  void VisitWhileStmt(WhileStmt *W);
1756  void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E);
1757  void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E);
1758  void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
1759  void VisitExpressionTraitExpr(ExpressionTraitExpr *E);
1760  void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U);
1761  void VisitVAArgExpr(VAArgExpr *E);
1762  void VisitSizeOfPackExpr(SizeOfPackExpr *E);
1763  void VisitPseudoObjectExpr(PseudoObjectExpr *E);
1764  void VisitOpaqueValueExpr(OpaqueValueExpr *E);
1765  void VisitLambdaExpr(LambdaExpr *E);
1766
1767private:
1768  void AddDeclarationNameInfo(Stmt *S);
1769  void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier);
1770  void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A);
1771  void AddMemberRef(FieldDecl *D, SourceLocation L);
1772  void AddStmt(Stmt *S);
1773  void AddDecl(Decl *D, bool isFirst = true);
1774  void AddTypeLoc(TypeSourceInfo *TI);
1775  void EnqueueChildren(Stmt *S);
1776};
1777} // end anonyous namespace
1778
1779void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) {
1780  // 'S' should always be non-null, since it comes from the
1781  // statement we are visiting.
1782  WL.push_back(DeclarationNameInfoVisit(S, Parent));
1783}
1784
1785void
1786EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) {
1787  if (Qualifier)
1788    WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent));
1789}
1790
1791void EnqueueVisitor::AddStmt(Stmt *S) {
1792  if (S)
1793    WL.push_back(StmtVisit(S, Parent));
1794}
1795void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) {
1796  if (D)
1797    WL.push_back(DeclVisit(D, Parent, isFirst));
1798}
1799void EnqueueVisitor::
1800  AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) {
1801  if (A)
1802    WL.push_back(ExplicitTemplateArgsVisit(
1803                        const_cast<ASTTemplateArgumentListInfo*>(A), Parent));
1804}
1805void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) {
1806  if (D)
1807    WL.push_back(MemberRefVisit(D, L, Parent));
1808}
1809void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
1810  if (TI)
1811    WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
1812 }
1813void EnqueueVisitor::EnqueueChildren(Stmt *S) {
1814  unsigned size = WL.size();
1815  for (Stmt::child_range Child = S->children(); Child; ++Child) {
1816    AddStmt(*Child);
1817  }
1818  if (size == WL.size())
1819    return;
1820  // Now reverse the entries we just added.  This will match the DFS
1821  // ordering performed by the worklist.
1822  VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
1823  std::reverse(I, E);
1824}
1825void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) {
1826  WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
1827}
1828void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) {
1829  AddDecl(B->getBlockDecl());
1830}
1831void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1832  EnqueueChildren(E);
1833  AddTypeLoc(E->getTypeSourceInfo());
1834}
1835void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) {
1836  for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(),
1837        E = S->body_rend(); I != E; ++I) {
1838    AddStmt(*I);
1839  }
1840}
1841void EnqueueVisitor::
1842VisitMSDependentExistsStmt(MSDependentExistsStmt *S) {
1843  AddStmt(S->getSubStmt());
1844  AddDeclarationNameInfo(S);
1845  if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc())
1846    AddNestedNameSpecifierLoc(QualifierLoc);
1847}
1848
1849void EnqueueVisitor::
1850VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
1851  AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
1852  AddDeclarationNameInfo(E);
1853  if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
1854    AddNestedNameSpecifierLoc(QualifierLoc);
1855  if (!E->isImplicitAccess())
1856    AddStmt(E->getBase());
1857}
1858void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) {
1859  // Enqueue the initializer , if any.
1860  AddStmt(E->getInitializer());
1861  // Enqueue the array size, if any.
1862  AddStmt(E->getArraySize());
1863  // Enqueue the allocated type.
1864  AddTypeLoc(E->getAllocatedTypeSourceInfo());
1865  // Enqueue the placement arguments.
1866  for (unsigned I = E->getNumPlacementArgs(); I > 0; --I)
1867    AddStmt(E->getPlacementArg(I-1));
1868}
1869void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) {
1870  for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
1871    AddStmt(CE->getArg(I-1));
1872  AddStmt(CE->getCallee());
1873  AddStmt(CE->getArg(0));
1874}
1875void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1876  // Visit the name of the type being destroyed.
1877  AddTypeLoc(E->getDestroyedTypeInfo());
1878  // Visit the scope type that looks disturbingly like the nested-name-specifier
1879  // but isn't.
1880  AddTypeLoc(E->getScopeTypeInfo());
1881  // Visit the nested-name-specifier.
1882  if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc())
1883    AddNestedNameSpecifierLoc(QualifierLoc);
1884  // Visit base expression.
1885  AddStmt(E->getBase());
1886}
1887void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1888  AddTypeLoc(E->getTypeSourceInfo());
1889}
1890void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
1891  EnqueueChildren(E);
1892  AddTypeLoc(E->getTypeSourceInfo());
1893}
1894void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
1895  EnqueueChildren(E);
1896  if (E->isTypeOperand())
1897    AddTypeLoc(E->getTypeOperandSourceInfo());
1898}
1899
1900void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr
1901                                                     *E) {
1902  EnqueueChildren(E);
1903  AddTypeLoc(E->getTypeSourceInfo());
1904}
1905void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
1906  EnqueueChildren(E);
1907  if (E->isTypeOperand())
1908    AddTypeLoc(E->getTypeOperandSourceInfo());
1909}
1910
1911void EnqueueVisitor::VisitCXXCatchStmt(CXXCatchStmt *S) {
1912  EnqueueChildren(S);
1913  AddDecl(S->getExceptionDecl());
1914}
1915
1916void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) {
1917  if (DR->hasExplicitTemplateArgs()) {
1918    AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs());
1919  }
1920  WL.push_back(DeclRefExprParts(DR, Parent));
1921}
1922void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
1923  AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
1924  AddDeclarationNameInfo(E);
1925  AddNestedNameSpecifierLoc(E->getQualifierLoc());
1926}
1927void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) {
1928  unsigned size = WL.size();
1929  bool isFirst = true;
1930  for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
1931       D != DEnd; ++D) {
1932    AddDecl(*D, isFirst);
1933    isFirst = false;
1934  }
1935  if (size == WL.size())
1936    return;
1937  // Now reverse the entries we just added.  This will match the DFS
1938  // ordering performed by the worklist.
1939  VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
1940  std::reverse(I, E);
1941}
1942void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1943  AddStmt(E->getInit());
1944  typedef DesignatedInitExpr::Designator Designator;
1945  for (DesignatedInitExpr::reverse_designators_iterator
1946         D = E->designators_rbegin(), DEnd = E->designators_rend();
1947         D != DEnd; ++D) {
1948    if (D->isFieldDesignator()) {
1949      if (FieldDecl *Field = D->getField())
1950        AddMemberRef(Field, D->getFieldLoc());
1951      continue;
1952    }
1953    if (D->isArrayDesignator()) {
1954      AddStmt(E->getArrayIndex(*D));
1955      continue;
1956    }
1957    assert(D->isArrayRangeDesignator() && "Unknown designator kind");
1958    AddStmt(E->getArrayRangeEnd(*D));
1959    AddStmt(E->getArrayRangeStart(*D));
1960  }
1961}
1962void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1963  EnqueueChildren(E);
1964  AddTypeLoc(E->getTypeInfoAsWritten());
1965}
1966void EnqueueVisitor::VisitForStmt(ForStmt *FS) {
1967  AddStmt(FS->getBody());
1968  AddStmt(FS->getInc());
1969  AddStmt(FS->getCond());
1970  AddDecl(FS->getConditionVariable());
1971  AddStmt(FS->getInit());
1972}
1973void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) {
1974  WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
1975}
1976void EnqueueVisitor::VisitIfStmt(IfStmt *If) {
1977  AddStmt(If->getElse());
1978  AddStmt(If->getThen());
1979  AddStmt(If->getCond());
1980  AddDecl(If->getConditionVariable());
1981}
1982void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) {
1983  // We care about the syntactic form of the initializer list, only.
1984  if (InitListExpr *Syntactic = IE->getSyntacticForm())
1985    IE = Syntactic;
1986  EnqueueChildren(IE);
1987}
1988void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) {
1989  WL.push_back(MemberExprParts(M, Parent));
1990
1991  // If the base of the member access expression is an implicit 'this', don't
1992  // visit it.
1993  // FIXME: If we ever want to show these implicit accesses, this will be
1994  // unfortunate. However, clang_getCursor() relies on this behavior.
1995  if (!M->isImplicitAccess())
1996    AddStmt(M->getBase());
1997}
1998void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1999  AddTypeLoc(E->getEncodedTypeSourceInfo());
2000}
2001void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) {
2002  EnqueueChildren(M);
2003  AddTypeLoc(M->getClassReceiverTypeInfo());
2004}
2005void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) {
2006  // Visit the components of the offsetof expression.
2007  for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) {
2008    typedef OffsetOfExpr::OffsetOfNode OffsetOfNode;
2009    const OffsetOfNode &Node = E->getComponent(I-1);
2010    switch (Node.getKind()) {
2011    case OffsetOfNode::Array:
2012      AddStmt(E->getIndexExpr(Node.getArrayExprIndex()));
2013      break;
2014    case OffsetOfNode::Field:
2015      AddMemberRef(Node.getField(), Node.getSourceRange().getEnd());
2016      break;
2017    case OffsetOfNode::Identifier:
2018    case OffsetOfNode::Base:
2019      continue;
2020    }
2021  }
2022  // Visit the type into which we're computing the offset.
2023  AddTypeLoc(E->getTypeSourceInfo());
2024}
2025void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) {
2026  AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
2027  WL.push_back(OverloadExprParts(E, Parent));
2028}
2029void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr(
2030                                              UnaryExprOrTypeTraitExpr *E) {
2031  EnqueueChildren(E);
2032  if (E->isArgumentType())
2033    AddTypeLoc(E->getArgumentTypeInfo());
2034}
2035void EnqueueVisitor::VisitStmt(Stmt *S) {
2036  EnqueueChildren(S);
2037}
2038void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) {
2039  AddStmt(S->getBody());
2040  AddStmt(S->getCond());
2041  AddDecl(S->getConditionVariable());
2042}
2043
2044void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) {
2045  AddStmt(W->getBody());
2046  AddStmt(W->getCond());
2047  AddDecl(W->getConditionVariable());
2048}
2049
2050void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
2051  AddTypeLoc(E->getQueriedTypeSourceInfo());
2052}
2053
2054void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
2055  AddTypeLoc(E->getRhsTypeSourceInfo());
2056  AddTypeLoc(E->getLhsTypeSourceInfo());
2057}
2058
2059void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
2060  AddTypeLoc(E->getQueriedTypeSourceInfo());
2061}
2062
2063void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
2064  EnqueueChildren(E);
2065}
2066
2067void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) {
2068  VisitOverloadExpr(U);
2069  if (!U->isImplicitAccess())
2070    AddStmt(U->getBase());
2071}
2072void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) {
2073  AddStmt(E->getSubExpr());
2074  AddTypeLoc(E->getWrittenTypeInfo());
2075}
2076void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
2077  WL.push_back(SizeOfPackExprParts(E, Parent));
2078}
2079void EnqueueVisitor::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
2080  // If the opaque value has a source expression, just transparently
2081  // visit that.  This is useful for (e.g.) pseudo-object expressions.
2082  if (Expr *SourceExpr = E->getSourceExpr())
2083    return Visit(SourceExpr);
2084}
2085void EnqueueVisitor::VisitLambdaExpr(LambdaExpr *E) {
2086  AddStmt(E->getBody());
2087  WL.push_back(LambdaExprParts(E, Parent));
2088}
2089void EnqueueVisitor::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
2090  // Treat the expression like its syntactic form.
2091  Visit(E->getSyntacticForm());
2092}
2093
2094void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) {
2095  EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S);
2096}
2097
2098bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
2099  if (RegionOfInterest.isValid()) {
2100    SourceRange Range = getRawCursorExtent(C);
2101    if (Range.isInvalid() || CompareRegionOfInterest(Range))
2102      return false;
2103  }
2104  return true;
2105}
2106
2107bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
2108  while (!WL.empty()) {
2109    // Dequeue the worklist item.
2110    VisitorJob LI = WL.back();
2111    WL.pop_back();
2112
2113    // Set the Parent field, then back to its old value once we're done.
2114    SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
2115
2116    switch (LI.getKind()) {
2117      case VisitorJob::DeclVisitKind: {
2118        Decl *D = cast<DeclVisit>(&LI)->get();
2119        if (!D)
2120          continue;
2121
2122        // For now, perform default visitation for Decls.
2123        if (Visit(MakeCXCursor(D, TU, RegionOfInterest,
2124                               cast<DeclVisit>(&LI)->isFirst())))
2125            return true;
2126
2127        continue;
2128      }
2129      case VisitorJob::ExplicitTemplateArgsVisitKind: {
2130        const ASTTemplateArgumentListInfo *ArgList =
2131          cast<ExplicitTemplateArgsVisit>(&LI)->get();
2132        for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
2133               *ArgEnd = Arg + ArgList->NumTemplateArgs;
2134               Arg != ArgEnd; ++Arg) {
2135          if (VisitTemplateArgumentLoc(*Arg))
2136            return true;
2137        }
2138        continue;
2139      }
2140      case VisitorJob::TypeLocVisitKind: {
2141        // Perform default visitation for TypeLocs.
2142        if (Visit(cast<TypeLocVisit>(&LI)->get()))
2143          return true;
2144        continue;
2145      }
2146      case VisitorJob::LabelRefVisitKind: {
2147        LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
2148        if (LabelStmt *stmt = LS->getStmt()) {
2149          if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(),
2150                                       TU))) {
2151            return true;
2152          }
2153        }
2154        continue;
2155      }
2156
2157      case VisitorJob::NestedNameSpecifierLocVisitKind: {
2158        NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI);
2159        if (VisitNestedNameSpecifierLoc(V->get()))
2160          return true;
2161        continue;
2162      }
2163
2164      case VisitorJob::DeclarationNameInfoVisitKind: {
2165        if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)
2166                                     ->get()))
2167          return true;
2168        continue;
2169      }
2170      case VisitorJob::MemberRefVisitKind: {
2171        MemberRefVisit *V = cast<MemberRefVisit>(&LI);
2172        if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
2173          return true;
2174        continue;
2175      }
2176      case VisitorJob::StmtVisitKind: {
2177        Stmt *S = cast<StmtVisit>(&LI)->get();
2178        if (!S)
2179          continue;
2180
2181        // Update the current cursor.
2182        CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest);
2183        if (!IsInRegionOfInterest(Cursor))
2184          continue;
2185        switch (Visitor(Cursor, Parent, ClientData)) {
2186          case CXChildVisit_Break: return true;
2187          case CXChildVisit_Continue: break;
2188          case CXChildVisit_Recurse:
2189            EnqueueWorkList(WL, S);
2190            break;
2191        }
2192        continue;
2193      }
2194      case VisitorJob::MemberExprPartsKind: {
2195        // Handle the other pieces in the MemberExpr besides the base.
2196        MemberExpr *M = cast<MemberExprParts>(&LI)->get();
2197
2198        // Visit the nested-name-specifier
2199        if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc())
2200          if (VisitNestedNameSpecifierLoc(QualifierLoc))
2201            return true;
2202
2203        // Visit the declaration name.
2204        if (VisitDeclarationNameInfo(M->getMemberNameInfo()))
2205          return true;
2206
2207        // Visit the explicitly-specified template arguments, if any.
2208        if (M->hasExplicitTemplateArgs()) {
2209          for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
2210               *ArgEnd = Arg + M->getNumTemplateArgs();
2211               Arg != ArgEnd; ++Arg) {
2212            if (VisitTemplateArgumentLoc(*Arg))
2213              return true;
2214          }
2215        }
2216        continue;
2217      }
2218      case VisitorJob::DeclRefExprPartsKind: {
2219        DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
2220        // Visit nested-name-specifier, if present.
2221        if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc())
2222          if (VisitNestedNameSpecifierLoc(QualifierLoc))
2223            return true;
2224        // Visit declaration name.
2225        if (VisitDeclarationNameInfo(DR->getNameInfo()))
2226          return true;
2227        continue;
2228      }
2229      case VisitorJob::OverloadExprPartsKind: {
2230        OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
2231        // Visit the nested-name-specifier.
2232        if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc())
2233          if (VisitNestedNameSpecifierLoc(QualifierLoc))
2234            return true;
2235        // Visit the declaration name.
2236        if (VisitDeclarationNameInfo(O->getNameInfo()))
2237          return true;
2238        // Visit the overloaded declaration reference.
2239        if (Visit(MakeCursorOverloadedDeclRef(O, TU)))
2240          return true;
2241        continue;
2242      }
2243      case VisitorJob::SizeOfPackExprPartsKind: {
2244        SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
2245        NamedDecl *Pack = E->getPack();
2246        if (isa<TemplateTypeParmDecl>(Pack)) {
2247          if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack),
2248                                      E->getPackLoc(), TU)))
2249            return true;
2250
2251          continue;
2252        }
2253
2254        if (isa<TemplateTemplateParmDecl>(Pack)) {
2255          if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack),
2256                                          E->getPackLoc(), TU)))
2257            return true;
2258
2259          continue;
2260        }
2261
2262        // Non-type template parameter packs and function parameter packs are
2263        // treated like DeclRefExpr cursors.
2264        continue;
2265      }
2266
2267      case VisitorJob::LambdaExprPartsKind: {
2268        // Visit captures.
2269        LambdaExpr *E = cast<LambdaExprParts>(&LI)->get();
2270        for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(),
2271                                       CEnd = E->explicit_capture_end();
2272             C != CEnd; ++C) {
2273          if (C->capturesThis())
2274            continue;
2275
2276          if (Visit(MakeCursorVariableRef(C->getCapturedVar(),
2277                                          C->getLocation(),
2278                                          TU)))
2279            return true;
2280        }
2281
2282        // Visit parameters and return type, if present.
2283        if (E->hasExplicitParameters() || E->hasExplicitResultType()) {
2284          TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc();
2285          if (E->hasExplicitParameters() && E->hasExplicitResultType()) {
2286            // Visit the whole type.
2287            if (Visit(TL))
2288              return true;
2289          } else if (isa<FunctionProtoTypeLoc>(TL)) {
2290            FunctionProtoTypeLoc Proto = cast<FunctionProtoTypeLoc>(TL);
2291            if (E->hasExplicitParameters()) {
2292              // Visit parameters.
2293              for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I)
2294                if (Visit(MakeCXCursor(Proto.getArg(I), TU)))
2295                  return true;
2296            } else {
2297              // Visit result type.
2298              if (Visit(Proto.getResultLoc()))
2299                return true;
2300            }
2301          }
2302        }
2303        break;
2304      }
2305    }
2306  }
2307  return false;
2308}
2309
2310bool CursorVisitor::Visit(Stmt *S) {
2311  VisitorWorkList *WL = 0;
2312  if (!WorkListFreeList.empty()) {
2313    WL = WorkListFreeList.back();
2314    WL->clear();
2315    WorkListFreeList.pop_back();
2316  }
2317  else {
2318    WL = new VisitorWorkList();
2319    WorkListCache.push_back(WL);
2320  }
2321  EnqueueWorkList(*WL, S);
2322  bool result = RunVisitorWorkList(*WL);
2323  WorkListFreeList.push_back(WL);
2324  return result;
2325}
2326
2327namespace {
2328typedef llvm::SmallVector<SourceRange, 4> RefNamePieces;
2329RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
2330                          const DeclarationNameInfo &NI,
2331                          const SourceRange &QLoc,
2332                          const ASTTemplateArgumentListInfo *TemplateArgs = 0){
2333  const bool WantQualifier = NameFlags & CXNameRange_WantQualifier;
2334  const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs;
2335  const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece;
2336
2337  const DeclarationName::NameKind Kind = NI.getName().getNameKind();
2338
2339  RefNamePieces Pieces;
2340
2341  if (WantQualifier && QLoc.isValid())
2342    Pieces.push_back(QLoc);
2343
2344  if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr)
2345    Pieces.push_back(NI.getLoc());
2346
2347  if (WantTemplateArgs && TemplateArgs)
2348    Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc,
2349                                 TemplateArgs->RAngleLoc));
2350
2351  if (Kind == DeclarationName::CXXOperatorName) {
2352    Pieces.push_back(SourceLocation::getFromRawEncoding(
2353                       NI.getInfo().CXXOperatorName.BeginOpNameLoc));
2354    Pieces.push_back(SourceLocation::getFromRawEncoding(
2355                       NI.getInfo().CXXOperatorName.EndOpNameLoc));
2356  }
2357
2358  if (WantSinglePiece) {
2359    SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd());
2360    Pieces.clear();
2361    Pieces.push_back(R);
2362  }
2363
2364  return Pieces;
2365}
2366}
2367
2368//===----------------------------------------------------------------------===//
2369// Misc. API hooks.
2370//===----------------------------------------------------------------------===//
2371
2372static llvm::sys::Mutex EnableMultithreadingMutex;
2373static bool EnabledMultithreading;
2374
2375static void fatal_error_handler(void *user_data, const std::string& reason) {
2376  // Write the result out to stderr avoiding errs() because raw_ostreams can
2377  // call report_fatal_error.
2378  fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str());
2379  ::abort();
2380}
2381
2382extern "C" {
2383CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
2384                          int displayDiagnostics) {
2385  // Disable pretty stack trace functionality, which will otherwise be a very
2386  // poor citizen of the world and set up all sorts of signal handlers.
2387  llvm::DisablePrettyStackTrace = true;
2388
2389  // We use crash recovery to make some of our APIs more reliable, implicitly
2390  // enable it.
2391  llvm::CrashRecoveryContext::Enable();
2392
2393  // Enable support for multithreading in LLVM.
2394  {
2395    llvm::sys::ScopedLock L(EnableMultithreadingMutex);
2396    if (!EnabledMultithreading) {
2397      llvm::install_fatal_error_handler(fatal_error_handler, 0);
2398      llvm::llvm_start_multithreaded();
2399      EnabledMultithreading = true;
2400    }
2401  }
2402
2403  CIndexer *CIdxr = new CIndexer();
2404  if (excludeDeclarationsFromPCH)
2405    CIdxr->setOnlyLocalDecls();
2406  if (displayDiagnostics)
2407    CIdxr->setDisplayDiagnostics();
2408  return CIdxr;
2409}
2410
2411void clang_disposeIndex(CXIndex CIdx) {
2412  if (CIdx)
2413    delete static_cast<CIndexer *>(CIdx);
2414}
2415
2416void clang_toggleCrashRecovery(unsigned isEnabled) {
2417  if (isEnabled)
2418    llvm::CrashRecoveryContext::Enable();
2419  else
2420    llvm::CrashRecoveryContext::Disable();
2421}
2422
2423CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
2424                                              const char *ast_filename) {
2425  if (!CIdx)
2426    return 0;
2427
2428  CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
2429  FileSystemOptions FileSystemOpts;
2430  FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory();
2431
2432  llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
2433  ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts,
2434                                  CXXIdx->getOnlyLocalDecls(),
2435                                  0, 0, true);
2436  return MakeCXTranslationUnit(TU);
2437}
2438
2439unsigned clang_defaultEditingTranslationUnitOptions() {
2440  return CXTranslationUnit_PrecompiledPreamble |
2441         CXTranslationUnit_CacheCompletionResults;
2442}
2443
2444CXTranslationUnit
2445clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
2446                                          const char *source_filename,
2447                                          int num_command_line_args,
2448                                          const char * const *command_line_args,
2449                                          unsigned num_unsaved_files,
2450                                          struct CXUnsavedFile *unsaved_files) {
2451  unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord |
2452                     CXTranslationUnit_NestedMacroExpansions;
2453  return clang_parseTranslationUnit(CIdx, source_filename,
2454                                    command_line_args, num_command_line_args,
2455                                    unsaved_files, num_unsaved_files,
2456                                    Options);
2457}
2458
2459struct ParseTranslationUnitInfo {
2460  CXIndex CIdx;
2461  const char *source_filename;
2462  const char *const *command_line_args;
2463  int num_command_line_args;
2464  struct CXUnsavedFile *unsaved_files;
2465  unsigned num_unsaved_files;
2466  unsigned options;
2467  CXTranslationUnit result;
2468};
2469static void clang_parseTranslationUnit_Impl(void *UserData) {
2470  ParseTranslationUnitInfo *PTUI =
2471    static_cast<ParseTranslationUnitInfo*>(UserData);
2472  CXIndex CIdx = PTUI->CIdx;
2473  const char *source_filename = PTUI->source_filename;
2474  const char * const *command_line_args = PTUI->command_line_args;
2475  int num_command_line_args = PTUI->num_command_line_args;
2476  struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files;
2477  unsigned num_unsaved_files = PTUI->num_unsaved_files;
2478  unsigned options = PTUI->options;
2479  PTUI->result = 0;
2480
2481  if (!CIdx)
2482    return;
2483
2484  CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
2485
2486  bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
2487  // FIXME: Add a flag for modules.
2488  TranslationUnitKind TUKind
2489    = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete;
2490  bool CacheCodeCompetionResults
2491    = options & CXTranslationUnit_CacheCompletionResults;
2492
2493  // Configure the diagnostics.
2494  DiagnosticOptions DiagOpts;
2495  llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
2496    Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args,
2497                                                command_line_args));
2498
2499  // Recover resources if we crash before exiting this function.
2500  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
2501    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
2502    DiagCleanup(Diags.getPtr());
2503
2504  OwningPtr<std::vector<ASTUnit::RemappedFile> >
2505    RemappedFiles(new std::vector<ASTUnit::RemappedFile>());
2506
2507  // Recover resources if we crash before exiting this function.
2508  llvm::CrashRecoveryContextCleanupRegistrar<
2509    std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
2510
2511  for (unsigned I = 0; I != num_unsaved_files; ++I) {
2512    StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
2513    const llvm::MemoryBuffer *Buffer
2514      = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
2515    RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename,
2516                                            Buffer));
2517  }
2518
2519  OwningPtr<std::vector<const char *> >
2520    Args(new std::vector<const char*>());
2521
2522  // Recover resources if we crash before exiting this method.
2523  llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
2524    ArgsCleanup(Args.get());
2525
2526  // Since the Clang C library is primarily used by batch tools dealing with
2527  // (often very broken) source code, where spell-checking can have a
2528  // significant negative impact on performance (particularly when
2529  // precompiled headers are involved), we disable it by default.
2530  // Only do this if we haven't found a spell-checking-related argument.
2531  bool FoundSpellCheckingArgument = false;
2532  for (int I = 0; I != num_command_line_args; ++I) {
2533    if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
2534        strcmp(command_line_args[I], "-fspell-checking") == 0) {
2535      FoundSpellCheckingArgument = true;
2536      break;
2537    }
2538  }
2539  if (!FoundSpellCheckingArgument)
2540    Args->push_back("-fno-spell-checking");
2541
2542  Args->insert(Args->end(), command_line_args,
2543               command_line_args + num_command_line_args);
2544
2545  // The 'source_filename' argument is optional.  If the caller does not
2546  // specify it then it is assumed that the source file is specified
2547  // in the actual argument list.
2548  // Put the source file after command_line_args otherwise if '-x' flag is
2549  // present it will be unused.
2550  if (source_filename)
2551    Args->push_back(source_filename);
2552
2553  // Do we need the detailed preprocessing record?
2554  bool NestedMacroExpansions = false;
2555  if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
2556    Args->push_back("-Xclang");
2557    Args->push_back("-detailed-preprocessing-record");
2558    NestedMacroExpansions
2559      = (options & CXTranslationUnit_NestedMacroExpansions);
2560  }
2561
2562  unsigned NumErrors = Diags->getClient()->getNumErrors();
2563  OwningPtr<ASTUnit> Unit(
2564    ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0
2565                                 /* vector::data() not portable */,
2566                                 Args->size() ? (&(*Args)[0] + Args->size()) :0,
2567                                 Diags,
2568                                 CXXIdx->getClangResourcesPath(),
2569                                 CXXIdx->getOnlyLocalDecls(),
2570                                 /*CaptureDiagnostics=*/true,
2571                                 RemappedFiles->size() ? &(*RemappedFiles)[0]:0,
2572                                 RemappedFiles->size(),
2573                                 /*RemappedFilesKeepOriginalName=*/true,
2574                                 PrecompilePreamble,
2575                                 TUKind,
2576                                 CacheCodeCompetionResults,
2577                                 NestedMacroExpansions));
2578
2579  if (NumErrors != Diags->getClient()->getNumErrors()) {
2580    // Make sure to check that 'Unit' is non-NULL.
2581    if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
2582      for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
2583                                      DEnd = Unit->stored_diag_end();
2584           D != DEnd; ++D) {
2585        CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
2586        CXString Msg = clang_formatDiagnostic(&Diag,
2587                                    clang_defaultDiagnosticDisplayOptions());
2588        fprintf(stderr, "%s\n", clang_getCString(Msg));
2589        clang_disposeString(Msg);
2590      }
2591#ifdef LLVM_ON_WIN32
2592      // On Windows, force a flush, since there may be multiple copies of
2593      // stderr and stdout in the file system, all with different buffers
2594      // but writing to the same device.
2595      fflush(stderr);
2596#endif
2597    }
2598  }
2599
2600  PTUI->result = MakeCXTranslationUnit(Unit.take());
2601}
2602CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx,
2603                                             const char *source_filename,
2604                                         const char * const *command_line_args,
2605                                             int num_command_line_args,
2606                                            struct CXUnsavedFile *unsaved_files,
2607                                             unsigned num_unsaved_files,
2608                                             unsigned options) {
2609  ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args,
2610                                    num_command_line_args, unsaved_files,
2611                                    num_unsaved_files, options, 0 };
2612  llvm::CrashRecoveryContext CRC;
2613
2614  if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) {
2615    fprintf(stderr, "libclang: crash detected during parsing: {\n");
2616    fprintf(stderr, "  'source_filename' : '%s'\n", source_filename);
2617    fprintf(stderr, "  'command_line_args' : [");
2618    for (int i = 0; i != num_command_line_args; ++i) {
2619      if (i)
2620        fprintf(stderr, ", ");
2621      fprintf(stderr, "'%s'", command_line_args[i]);
2622    }
2623    fprintf(stderr, "],\n");
2624    fprintf(stderr, "  'unsaved_files' : [");
2625    for (unsigned i = 0; i != num_unsaved_files; ++i) {
2626      if (i)
2627        fprintf(stderr, ", ");
2628      fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
2629              unsaved_files[i].Length);
2630    }
2631    fprintf(stderr, "],\n");
2632    fprintf(stderr, "  'options' : %d,\n", options);
2633    fprintf(stderr, "}\n");
2634
2635    return 0;
2636  } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
2637    PrintLibclangResourceUsage(PTUI.result);
2638  }
2639
2640  return PTUI.result;
2641}
2642
2643unsigned clang_defaultSaveOptions(CXTranslationUnit TU) {
2644  return CXSaveTranslationUnit_None;
2645}
2646
2647int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
2648                              unsigned options) {
2649  if (!TU)
2650    return CXSaveError_InvalidTU;
2651
2652  CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName);
2653  if (getenv("LIBCLANG_RESOURCE_USAGE"))
2654    PrintLibclangResourceUsage(TU);
2655  return result;
2656}
2657
2658void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
2659  if (CTUnit) {
2660    // If the translation unit has been marked as unsafe to free, just discard
2661    // it.
2662    if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree())
2663      return;
2664
2665    delete static_cast<ASTUnit *>(CTUnit->TUData);
2666    disposeCXStringPool(CTUnit->StringPool);
2667    delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);
2668    delete CTUnit;
2669  }
2670}
2671
2672unsigned clang_defaultReparseOptions(CXTranslationUnit TU) {
2673  return CXReparse_None;
2674}
2675
2676struct ReparseTranslationUnitInfo {
2677  CXTranslationUnit TU;
2678  unsigned num_unsaved_files;
2679  struct CXUnsavedFile *unsaved_files;
2680  unsigned options;
2681  int result;
2682};
2683
2684static void clang_reparseTranslationUnit_Impl(void *UserData) {
2685  ReparseTranslationUnitInfo *RTUI =
2686    static_cast<ReparseTranslationUnitInfo*>(UserData);
2687  CXTranslationUnit TU = RTUI->TU;
2688
2689  // Reset the associated diagnostics.
2690  delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
2691  TU->Diagnostics = 0;
2692
2693  unsigned num_unsaved_files = RTUI->num_unsaved_files;
2694  struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files;
2695  unsigned options = RTUI->options;
2696  (void) options;
2697  RTUI->result = 1;
2698
2699  if (!TU)
2700    return;
2701
2702  ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
2703  ASTUnit::ConcurrencyCheck Check(*CXXUnit);
2704
2705  OwningPtr<std::vector<ASTUnit::RemappedFile> >
2706    RemappedFiles(new std::vector<ASTUnit::RemappedFile>());
2707
2708  // Recover resources if we crash before exiting this function.
2709  llvm::CrashRecoveryContextCleanupRegistrar<
2710    std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get());
2711
2712  for (unsigned I = 0; I != num_unsaved_files; ++I) {
2713    StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
2714    const llvm::MemoryBuffer *Buffer
2715      = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
2716    RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename,
2717                                            Buffer));
2718  }
2719
2720  if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0,
2721                        RemappedFiles->size()))
2722    RTUI->result = 0;
2723}
2724
2725int clang_reparseTranslationUnit(CXTranslationUnit TU,
2726                                 unsigned num_unsaved_files,
2727                                 struct CXUnsavedFile *unsaved_files,
2728                                 unsigned options) {
2729  ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files,
2730                                      options, 0 };
2731
2732  if (getenv("LIBCLANG_NOTHREADS")) {
2733    clang_reparseTranslationUnit_Impl(&RTUI);
2734    return RTUI.result;
2735  }
2736
2737  llvm::CrashRecoveryContext CRC;
2738
2739  if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) {
2740    fprintf(stderr, "libclang: crash detected during reparsing\n");
2741    static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
2742    return 1;
2743  } else if (getenv("LIBCLANG_RESOURCE_USAGE"))
2744    PrintLibclangResourceUsage(TU);
2745
2746  return RTUI.result;
2747}
2748
2749
2750CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
2751  if (!CTUnit)
2752    return createCXString("");
2753
2754  ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData);
2755  return createCXString(CXXUnit->getOriginalSourceFileName(), true);
2756}
2757
2758CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
2759  CXCursor Result = { CXCursor_TranslationUnit, 0, { 0, 0, TU } };
2760  return Result;
2761}
2762
2763} // end: extern "C"
2764
2765//===----------------------------------------------------------------------===//
2766// CXFile Operations.
2767//===----------------------------------------------------------------------===//
2768
2769extern "C" {
2770CXString clang_getFileName(CXFile SFile) {
2771  if (!SFile)
2772    return createCXString((const char*)NULL);
2773
2774  FileEntry *FEnt = static_cast<FileEntry *>(SFile);
2775  return createCXString(FEnt->getName());
2776}
2777
2778time_t clang_getFileTime(CXFile SFile) {
2779  if (!SFile)
2780    return 0;
2781
2782  FileEntry *FEnt = static_cast<FileEntry *>(SFile);
2783  return FEnt->getModificationTime();
2784}
2785
2786CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
2787  if (!tu)
2788    return 0;
2789
2790  ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
2791
2792  FileManager &FMgr = CXXUnit->getFileManager();
2793  return const_cast<FileEntry *>(FMgr.getFile(file_name));
2794}
2795
2796unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) {
2797  if (!tu || !file)
2798    return 0;
2799
2800  ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
2801  FileEntry *FEnt = static_cast<FileEntry *>(file);
2802  return CXXUnit->getPreprocessor().getHeaderSearchInfo()
2803                                          .isFileMultipleIncludeGuarded(FEnt);
2804}
2805
2806} // end: extern "C"
2807
2808//===----------------------------------------------------------------------===//
2809// CXCursor Operations.
2810//===----------------------------------------------------------------------===//
2811
2812static Decl *getDeclFromExpr(Stmt *E) {
2813  if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
2814    return getDeclFromExpr(CE->getSubExpr());
2815
2816  if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
2817    return RefExpr->getDecl();
2818  if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
2819    return RefExpr->getDecl();
2820  if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
2821    return ME->getMemberDecl();
2822  if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
2823    return RE->getDecl();
2824  if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E))
2825    return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0;
2826  if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
2827    return getDeclFromExpr(POE->getSyntacticForm());
2828  if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
2829    if (Expr *Src = OVE->getSourceExpr())
2830      return getDeclFromExpr(Src);
2831
2832  if (CallExpr *CE = dyn_cast<CallExpr>(E))
2833    return getDeclFromExpr(CE->getCallee());
2834  if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E))
2835    if (!CE->isElidable())
2836    return CE->getConstructor();
2837  if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
2838    return OME->getMethodDecl();
2839
2840  if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
2841    return PE->getProtocol();
2842  if (SubstNonTypeTemplateParmPackExpr *NTTP
2843                              = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
2844    return NTTP->getParameterPack();
2845  if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
2846    if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) ||
2847        isa<ParmVarDecl>(SizeOfPack->getPack()))
2848      return SizeOfPack->getPack();
2849
2850  return 0;
2851}
2852
2853static SourceLocation getLocationFromExpr(Expr *E) {
2854  if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
2855    return getLocationFromExpr(CE->getSubExpr());
2856
2857  if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
2858    return /*FIXME:*/Msg->getLeftLoc();
2859  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
2860    return DRE->getLocation();
2861  if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
2862    return RefExpr->getLocation();
2863  if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
2864    return Member->getMemberLoc();
2865  if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
2866    return Ivar->getLocation();
2867  if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
2868    return SizeOfPack->getPackLoc();
2869
2870  return E->getLocStart();
2871}
2872
2873extern "C" {
2874
2875unsigned clang_visitChildren(CXCursor parent,
2876                             CXCursorVisitor visitor,
2877                             CXClientData client_data) {
2878  CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
2879                          /*VisitPreprocessorLast=*/false);
2880  return CursorVis.VisitChildren(parent);
2881}
2882
2883#ifndef __has_feature
2884#define __has_feature(x) 0
2885#endif
2886#if __has_feature(blocks)
2887typedef enum CXChildVisitResult
2888     (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent);
2889
2890static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
2891    CXClientData client_data) {
2892  CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
2893  return block(cursor, parent);
2894}
2895#else
2896// If we are compiled with a compiler that doesn't have native blocks support,
2897// define and call the block manually, so the
2898typedef struct _CXChildVisitResult
2899{
2900	void *isa;
2901	int flags;
2902	int reserved;
2903	enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor,
2904                                         CXCursor);
2905} *CXCursorVisitorBlock;
2906
2907static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
2908    CXClientData client_data) {
2909  CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
2910  return block->invoke(block, cursor, parent);
2911}
2912#endif
2913
2914
2915unsigned clang_visitChildrenWithBlock(CXCursor parent,
2916                                      CXCursorVisitorBlock block) {
2917  return clang_visitChildren(parent, visitWithBlock, block);
2918}
2919
2920static CXString getDeclSpelling(Decl *D) {
2921  if (!D)
2922    return createCXString("");
2923
2924  NamedDecl *ND = dyn_cast<NamedDecl>(D);
2925  if (!ND) {
2926    if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D))
2927      if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
2928        return createCXString(Property->getIdentifier()->getName());
2929
2930    return createCXString("");
2931  }
2932
2933  if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
2934    return createCXString(OMD->getSelector().getAsString());
2935
2936  if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
2937    // No, this isn't the same as the code below. getIdentifier() is non-virtual
2938    // and returns different names. NamedDecl returns the class name and
2939    // ObjCCategoryImplDecl returns the category name.
2940    return createCXString(CIMP->getIdentifier()->getNameStart());
2941
2942  if (isa<UsingDirectiveDecl>(D))
2943    return createCXString("");
2944
2945  SmallString<1024> S;
2946  llvm::raw_svector_ostream os(S);
2947  ND->printName(os);
2948
2949  return createCXString(os.str());
2950}
2951
2952CXString clang_getCursorSpelling(CXCursor C) {
2953  if (clang_isTranslationUnit(C.kind))
2954    return clang_getTranslationUnitSpelling(
2955                            static_cast<CXTranslationUnit>(C.data[2]));
2956
2957  if (clang_isReference(C.kind)) {
2958    switch (C.kind) {
2959    case CXCursor_ObjCSuperClassRef: {
2960      ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
2961      return createCXString(Super->getIdentifier()->getNameStart());
2962    }
2963    case CXCursor_ObjCClassRef: {
2964      ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
2965      return createCXString(Class->getIdentifier()->getNameStart());
2966    }
2967    case CXCursor_ObjCProtocolRef: {
2968      ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
2969      assert(OID && "getCursorSpelling(): Missing protocol decl");
2970      return createCXString(OID->getIdentifier()->getNameStart());
2971    }
2972    case CXCursor_CXXBaseSpecifier: {
2973      CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C);
2974      return createCXString(B->getType().getAsString());
2975    }
2976    case CXCursor_TypeRef: {
2977      TypeDecl *Type = getCursorTypeRef(C).first;
2978      assert(Type && "Missing type decl");
2979
2980      return createCXString(getCursorContext(C).getTypeDeclType(Type).
2981                              getAsString());
2982    }
2983    case CXCursor_TemplateRef: {
2984      TemplateDecl *Template = getCursorTemplateRef(C).first;
2985      assert(Template && "Missing template decl");
2986
2987      return createCXString(Template->getNameAsString());
2988    }
2989
2990    case CXCursor_NamespaceRef: {
2991      NamedDecl *NS = getCursorNamespaceRef(C).first;
2992      assert(NS && "Missing namespace decl");
2993
2994      return createCXString(NS->getNameAsString());
2995    }
2996
2997    case CXCursor_MemberRef: {
2998      FieldDecl *Field = getCursorMemberRef(C).first;
2999      assert(Field && "Missing member decl");
3000
3001      return createCXString(Field->getNameAsString());
3002    }
3003
3004    case CXCursor_LabelRef: {
3005      LabelStmt *Label = getCursorLabelRef(C).first;
3006      assert(Label && "Missing label");
3007
3008      return createCXString(Label->getName());
3009    }
3010
3011    case CXCursor_OverloadedDeclRef: {
3012      OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
3013      if (Decl *D = Storage.dyn_cast<Decl *>()) {
3014        if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
3015          return createCXString(ND->getNameAsString());
3016        return createCXString("");
3017      }
3018      if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
3019        return createCXString(E->getName().getAsString());
3020      OverloadedTemplateStorage *Ovl
3021        = Storage.get<OverloadedTemplateStorage*>();
3022      if (Ovl->size() == 0)
3023        return createCXString("");
3024      return createCXString((*Ovl->begin())->getNameAsString());
3025    }
3026
3027    case CXCursor_VariableRef: {
3028      VarDecl *Var = getCursorVariableRef(C).first;
3029      assert(Var && "Missing variable decl");
3030
3031      return createCXString(Var->getNameAsString());
3032    }
3033
3034    default:
3035      return createCXString("<not implemented>");
3036    }
3037  }
3038
3039  if (clang_isExpression(C.kind)) {
3040    Decl *D = getDeclFromExpr(getCursorExpr(C));
3041    if (D)
3042      return getDeclSpelling(D);
3043    return createCXString("");
3044  }
3045
3046  if (clang_isStatement(C.kind)) {
3047    Stmt *S = getCursorStmt(C);
3048    if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
3049      return createCXString(Label->getName());
3050
3051    return createCXString("");
3052  }
3053
3054  if (C.kind == CXCursor_MacroExpansion)
3055    return createCXString(getCursorMacroExpansion(C)->getName()
3056                                                           ->getNameStart());
3057
3058  if (C.kind == CXCursor_MacroDefinition)
3059    return createCXString(getCursorMacroDefinition(C)->getName()
3060                                                           ->getNameStart());
3061
3062  if (C.kind == CXCursor_InclusionDirective)
3063    return createCXString(getCursorInclusionDirective(C)->getFileName());
3064
3065  if (clang_isDeclaration(C.kind))
3066    return getDeclSpelling(getCursorDecl(C));
3067
3068  if (C.kind == CXCursor_AnnotateAttr) {
3069    AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C));
3070    return createCXString(AA->getAnnotation());
3071  }
3072
3073  if (C.kind == CXCursor_AsmLabelAttr) {
3074    AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C));
3075    return createCXString(AA->getLabel());
3076  }
3077
3078  return createCXString("");
3079}
3080
3081CXString clang_getCursorDisplayName(CXCursor C) {
3082  if (!clang_isDeclaration(C.kind))
3083    return clang_getCursorSpelling(C);
3084
3085  Decl *D = getCursorDecl(C);
3086  if (!D)
3087    return createCXString("");
3088
3089  PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy();
3090  if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
3091    D = FunTmpl->getTemplatedDecl();
3092
3093  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
3094    SmallString<64> Str;
3095    llvm::raw_svector_ostream OS(Str);
3096    OS << *Function;
3097    if (Function->getPrimaryTemplate())
3098      OS << "<>";
3099    OS << "(";
3100    for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
3101      if (I)
3102        OS << ", ";
3103      OS << Function->getParamDecl(I)->getType().getAsString(Policy);
3104    }
3105
3106    if (Function->isVariadic()) {
3107      if (Function->getNumParams())
3108        OS << ", ";
3109      OS << "...";
3110    }
3111    OS << ")";
3112    return createCXString(OS.str());
3113  }
3114
3115  if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
3116    SmallString<64> Str;
3117    llvm::raw_svector_ostream OS(Str);
3118    OS << *ClassTemplate;
3119    OS << "<";
3120    TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
3121    for (unsigned I = 0, N = Params->size(); I != N; ++I) {
3122      if (I)
3123        OS << ", ";
3124
3125      NamedDecl *Param = Params->getParam(I);
3126      if (Param->getIdentifier()) {
3127        OS << Param->getIdentifier()->getName();
3128        continue;
3129      }
3130
3131      // There is no parameter name, which makes this tricky. Try to come up
3132      // with something useful that isn't too long.
3133      if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
3134        OS << (TTP->wasDeclaredWithTypename()? "typename" : "class");
3135      else if (NonTypeTemplateParmDecl *NTTP
3136                                    = dyn_cast<NonTypeTemplateParmDecl>(Param))
3137        OS << NTTP->getType().getAsString(Policy);
3138      else
3139        OS << "template<...> class";
3140    }
3141
3142    OS << ">";
3143    return createCXString(OS.str());
3144  }
3145
3146  if (ClassTemplateSpecializationDecl *ClassSpec
3147                              = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
3148    // If the type was explicitly written, use that.
3149    if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
3150      return createCXString(TSInfo->getType().getAsString(Policy));
3151
3152    SmallString<64> Str;
3153    llvm::raw_svector_ostream OS(Str);
3154    OS << *ClassSpec;
3155    OS << TemplateSpecializationType::PrintTemplateArgumentList(
3156                                      ClassSpec->getTemplateArgs().data(),
3157                                      ClassSpec->getTemplateArgs().size(),
3158                                                                Policy);
3159    return createCXString(OS.str());
3160  }
3161
3162  return clang_getCursorSpelling(C);
3163}
3164
3165CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
3166  switch (Kind) {
3167  case CXCursor_FunctionDecl:
3168      return createCXString("FunctionDecl");
3169  case CXCursor_TypedefDecl:
3170      return createCXString("TypedefDecl");
3171  case CXCursor_EnumDecl:
3172      return createCXString("EnumDecl");
3173  case CXCursor_EnumConstantDecl:
3174      return createCXString("EnumConstantDecl");
3175  case CXCursor_StructDecl:
3176      return createCXString("StructDecl");
3177  case CXCursor_UnionDecl:
3178      return createCXString("UnionDecl");
3179  case CXCursor_ClassDecl:
3180      return createCXString("ClassDecl");
3181  case CXCursor_FieldDecl:
3182      return createCXString("FieldDecl");
3183  case CXCursor_VarDecl:
3184      return createCXString("VarDecl");
3185  case CXCursor_ParmDecl:
3186      return createCXString("ParmDecl");
3187  case CXCursor_ObjCInterfaceDecl:
3188      return createCXString("ObjCInterfaceDecl");
3189  case CXCursor_ObjCCategoryDecl:
3190      return createCXString("ObjCCategoryDecl");
3191  case CXCursor_ObjCProtocolDecl:
3192      return createCXString("ObjCProtocolDecl");
3193  case CXCursor_ObjCPropertyDecl:
3194      return createCXString("ObjCPropertyDecl");
3195  case CXCursor_ObjCIvarDecl:
3196      return createCXString("ObjCIvarDecl");
3197  case CXCursor_ObjCInstanceMethodDecl:
3198      return createCXString("ObjCInstanceMethodDecl");
3199  case CXCursor_ObjCClassMethodDecl:
3200      return createCXString("ObjCClassMethodDecl");
3201  case CXCursor_ObjCImplementationDecl:
3202      return createCXString("ObjCImplementationDecl");
3203  case CXCursor_ObjCCategoryImplDecl:
3204      return createCXString("ObjCCategoryImplDecl");
3205  case CXCursor_CXXMethod:
3206      return createCXString("CXXMethod");
3207  case CXCursor_UnexposedDecl:
3208      return createCXString("UnexposedDecl");
3209  case CXCursor_ObjCSuperClassRef:
3210      return createCXString("ObjCSuperClassRef");
3211  case CXCursor_ObjCProtocolRef:
3212      return createCXString("ObjCProtocolRef");
3213  case CXCursor_ObjCClassRef:
3214      return createCXString("ObjCClassRef");
3215  case CXCursor_TypeRef:
3216      return createCXString("TypeRef");
3217  case CXCursor_TemplateRef:
3218      return createCXString("TemplateRef");
3219  case CXCursor_NamespaceRef:
3220    return createCXString("NamespaceRef");
3221  case CXCursor_MemberRef:
3222    return createCXString("MemberRef");
3223  case CXCursor_LabelRef:
3224    return createCXString("LabelRef");
3225  case CXCursor_OverloadedDeclRef:
3226    return createCXString("OverloadedDeclRef");
3227  case CXCursor_VariableRef:
3228    return createCXString("VariableRef");
3229  case CXCursor_IntegerLiteral:
3230      return createCXString("IntegerLiteral");
3231  case CXCursor_FloatingLiteral:
3232      return createCXString("FloatingLiteral");
3233  case CXCursor_ImaginaryLiteral:
3234      return createCXString("ImaginaryLiteral");
3235  case CXCursor_StringLiteral:
3236      return createCXString("StringLiteral");
3237  case CXCursor_CharacterLiteral:
3238      return createCXString("CharacterLiteral");
3239  case CXCursor_ParenExpr:
3240      return createCXString("ParenExpr");
3241  case CXCursor_UnaryOperator:
3242      return createCXString("UnaryOperator");
3243  case CXCursor_ArraySubscriptExpr:
3244      return createCXString("ArraySubscriptExpr");
3245  case CXCursor_BinaryOperator:
3246      return createCXString("BinaryOperator");
3247  case CXCursor_CompoundAssignOperator:
3248      return createCXString("CompoundAssignOperator");
3249  case CXCursor_ConditionalOperator:
3250      return createCXString("ConditionalOperator");
3251  case CXCursor_CStyleCastExpr:
3252      return createCXString("CStyleCastExpr");
3253  case CXCursor_CompoundLiteralExpr:
3254      return createCXString("CompoundLiteralExpr");
3255  case CXCursor_InitListExpr:
3256      return createCXString("InitListExpr");
3257  case CXCursor_AddrLabelExpr:
3258      return createCXString("AddrLabelExpr");
3259  case CXCursor_StmtExpr:
3260      return createCXString("StmtExpr");
3261  case CXCursor_GenericSelectionExpr:
3262      return createCXString("GenericSelectionExpr");
3263  case CXCursor_GNUNullExpr:
3264      return createCXString("GNUNullExpr");
3265  case CXCursor_CXXStaticCastExpr:
3266      return createCXString("CXXStaticCastExpr");
3267  case CXCursor_CXXDynamicCastExpr:
3268      return createCXString("CXXDynamicCastExpr");
3269  case CXCursor_CXXReinterpretCastExpr:
3270      return createCXString("CXXReinterpretCastExpr");
3271  case CXCursor_CXXConstCastExpr:
3272      return createCXString("CXXConstCastExpr");
3273  case CXCursor_CXXFunctionalCastExpr:
3274      return createCXString("CXXFunctionalCastExpr");
3275  case CXCursor_CXXTypeidExpr:
3276      return createCXString("CXXTypeidExpr");
3277  case CXCursor_CXXBoolLiteralExpr:
3278      return createCXString("CXXBoolLiteralExpr");
3279  case CXCursor_CXXNullPtrLiteralExpr:
3280      return createCXString("CXXNullPtrLiteralExpr");
3281  case CXCursor_CXXThisExpr:
3282      return createCXString("CXXThisExpr");
3283  case CXCursor_CXXThrowExpr:
3284      return createCXString("CXXThrowExpr");
3285  case CXCursor_CXXNewExpr:
3286      return createCXString("CXXNewExpr");
3287  case CXCursor_CXXDeleteExpr:
3288      return createCXString("CXXDeleteExpr");
3289  case CXCursor_UnaryExpr:
3290      return createCXString("UnaryExpr");
3291  case CXCursor_ObjCStringLiteral:
3292      return createCXString("ObjCStringLiteral");
3293  case CXCursor_ObjCEncodeExpr:
3294      return createCXString("ObjCEncodeExpr");
3295  case CXCursor_ObjCSelectorExpr:
3296      return createCXString("ObjCSelectorExpr");
3297  case CXCursor_ObjCProtocolExpr:
3298      return createCXString("ObjCProtocolExpr");
3299  case CXCursor_ObjCBridgedCastExpr:
3300      return createCXString("ObjCBridgedCastExpr");
3301  case CXCursor_BlockExpr:
3302      return createCXString("BlockExpr");
3303  case CXCursor_PackExpansionExpr:
3304      return createCXString("PackExpansionExpr");
3305  case CXCursor_SizeOfPackExpr:
3306      return createCXString("SizeOfPackExpr");
3307  case CXCursor_LambdaExpr:
3308    return createCXString("LambdaExpr");
3309  case CXCursor_UnexposedExpr:
3310      return createCXString("UnexposedExpr");
3311  case CXCursor_DeclRefExpr:
3312      return createCXString("DeclRefExpr");
3313  case CXCursor_MemberRefExpr:
3314      return createCXString("MemberRefExpr");
3315  case CXCursor_CallExpr:
3316      return createCXString("CallExpr");
3317  case CXCursor_ObjCMessageExpr:
3318      return createCXString("ObjCMessageExpr");
3319  case CXCursor_UnexposedStmt:
3320      return createCXString("UnexposedStmt");
3321  case CXCursor_DeclStmt:
3322      return createCXString("DeclStmt");
3323  case CXCursor_LabelStmt:
3324      return createCXString("LabelStmt");
3325  case CXCursor_CompoundStmt:
3326      return createCXString("CompoundStmt");
3327  case CXCursor_CaseStmt:
3328      return createCXString("CaseStmt");
3329  case CXCursor_DefaultStmt:
3330      return createCXString("DefaultStmt");
3331  case CXCursor_IfStmt:
3332      return createCXString("IfStmt");
3333  case CXCursor_SwitchStmt:
3334      return createCXString("SwitchStmt");
3335  case CXCursor_WhileStmt:
3336      return createCXString("WhileStmt");
3337  case CXCursor_DoStmt:
3338      return createCXString("DoStmt");
3339  case CXCursor_ForStmt:
3340      return createCXString("ForStmt");
3341  case CXCursor_GotoStmt:
3342      return createCXString("GotoStmt");
3343  case CXCursor_IndirectGotoStmt:
3344      return createCXString("IndirectGotoStmt");
3345  case CXCursor_ContinueStmt:
3346      return createCXString("ContinueStmt");
3347  case CXCursor_BreakStmt:
3348      return createCXString("BreakStmt");
3349  case CXCursor_ReturnStmt:
3350      return createCXString("ReturnStmt");
3351  case CXCursor_AsmStmt:
3352      return createCXString("AsmStmt");
3353  case CXCursor_ObjCAtTryStmt:
3354      return createCXString("ObjCAtTryStmt");
3355  case CXCursor_ObjCAtCatchStmt:
3356      return createCXString("ObjCAtCatchStmt");
3357  case CXCursor_ObjCAtFinallyStmt:
3358      return createCXString("ObjCAtFinallyStmt");
3359  case CXCursor_ObjCAtThrowStmt:
3360      return createCXString("ObjCAtThrowStmt");
3361  case CXCursor_ObjCAtSynchronizedStmt:
3362      return createCXString("ObjCAtSynchronizedStmt");
3363  case CXCursor_ObjCAutoreleasePoolStmt:
3364      return createCXString("ObjCAutoreleasePoolStmt");
3365  case CXCursor_ObjCForCollectionStmt:
3366      return createCXString("ObjCForCollectionStmt");
3367  case CXCursor_CXXCatchStmt:
3368      return createCXString("CXXCatchStmt");
3369  case CXCursor_CXXTryStmt:
3370      return createCXString("CXXTryStmt");
3371  case CXCursor_CXXForRangeStmt:
3372      return createCXString("CXXForRangeStmt");
3373  case CXCursor_SEHTryStmt:
3374      return createCXString("SEHTryStmt");
3375  case CXCursor_SEHExceptStmt:
3376      return createCXString("SEHExceptStmt");
3377  case CXCursor_SEHFinallyStmt:
3378      return createCXString("SEHFinallyStmt");
3379  case CXCursor_NullStmt:
3380      return createCXString("NullStmt");
3381  case CXCursor_InvalidFile:
3382      return createCXString("InvalidFile");
3383  case CXCursor_InvalidCode:
3384    return createCXString("InvalidCode");
3385  case CXCursor_NoDeclFound:
3386      return createCXString("NoDeclFound");
3387  case CXCursor_NotImplemented:
3388      return createCXString("NotImplemented");
3389  case CXCursor_TranslationUnit:
3390      return createCXString("TranslationUnit");
3391  case CXCursor_UnexposedAttr:
3392      return createCXString("UnexposedAttr");
3393  case CXCursor_IBActionAttr:
3394      return createCXString("attribute(ibaction)");
3395  case CXCursor_IBOutletAttr:
3396     return createCXString("attribute(iboutlet)");
3397  case CXCursor_IBOutletCollectionAttr:
3398      return createCXString("attribute(iboutletcollection)");
3399  case CXCursor_CXXFinalAttr:
3400      return createCXString("attribute(final)");
3401  case CXCursor_CXXOverrideAttr:
3402      return createCXString("attribute(override)");
3403  case CXCursor_AnnotateAttr:
3404    return createCXString("attribute(annotate)");
3405  case CXCursor_AsmLabelAttr:
3406    return createCXString("asm label");
3407  case CXCursor_PreprocessingDirective:
3408    return createCXString("preprocessing directive");
3409  case CXCursor_MacroDefinition:
3410    return createCXString("macro definition");
3411  case CXCursor_MacroExpansion:
3412    return createCXString("macro expansion");
3413  case CXCursor_InclusionDirective:
3414    return createCXString("inclusion directive");
3415  case CXCursor_Namespace:
3416    return createCXString("Namespace");
3417  case CXCursor_LinkageSpec:
3418    return createCXString("LinkageSpec");
3419  case CXCursor_CXXBaseSpecifier:
3420    return createCXString("C++ base class specifier");
3421  case CXCursor_Constructor:
3422    return createCXString("CXXConstructor");
3423  case CXCursor_Destructor:
3424    return createCXString("CXXDestructor");
3425  case CXCursor_ConversionFunction:
3426    return createCXString("CXXConversion");
3427  case CXCursor_TemplateTypeParameter:
3428    return createCXString("TemplateTypeParameter");
3429  case CXCursor_NonTypeTemplateParameter:
3430    return createCXString("NonTypeTemplateParameter");
3431  case CXCursor_TemplateTemplateParameter:
3432    return createCXString("TemplateTemplateParameter");
3433  case CXCursor_FunctionTemplate:
3434    return createCXString("FunctionTemplate");
3435  case CXCursor_ClassTemplate:
3436    return createCXString("ClassTemplate");
3437  case CXCursor_ClassTemplatePartialSpecialization:
3438    return createCXString("ClassTemplatePartialSpecialization");
3439  case CXCursor_NamespaceAlias:
3440    return createCXString("NamespaceAlias");
3441  case CXCursor_UsingDirective:
3442    return createCXString("UsingDirective");
3443  case CXCursor_UsingDeclaration:
3444    return createCXString("UsingDeclaration");
3445  case CXCursor_TypeAliasDecl:
3446    return createCXString("TypeAliasDecl");
3447  case CXCursor_ObjCSynthesizeDecl:
3448    return createCXString("ObjCSynthesizeDecl");
3449  case CXCursor_ObjCDynamicDecl:
3450    return createCXString("ObjCDynamicDecl");
3451  case CXCursor_CXXAccessSpecifier:
3452    return createCXString("CXXAccessSpecifier");
3453  }
3454
3455  llvm_unreachable("Unhandled CXCursorKind");
3456}
3457
3458struct GetCursorData {
3459  SourceLocation TokenBeginLoc;
3460  bool PointsAtMacroArgExpansion;
3461  CXCursor &BestCursor;
3462
3463  GetCursorData(SourceManager &SM,
3464                SourceLocation tokenBegin, CXCursor &outputCursor)
3465    : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) {
3466    PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin);
3467  }
3468};
3469
3470static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
3471                                                CXCursor parent,
3472                                                CXClientData client_data) {
3473  GetCursorData *Data = static_cast<GetCursorData *>(client_data);
3474  CXCursor *BestCursor = &Data->BestCursor;
3475
3476  // If we point inside a macro argument we should provide info of what the
3477  // token is so use the actual cursor, don't replace it with a macro expansion
3478  // cursor.
3479  if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion)
3480    return CXChildVisit_Recurse;
3481
3482  if (clang_isDeclaration(cursor.kind)) {
3483    // Avoid having the implicit methods override the property decls.
3484    if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor)))
3485      if (MD->isImplicit())
3486        return CXChildVisit_Break;
3487  }
3488
3489  if (clang_isExpression(cursor.kind) &&
3490      clang_isDeclaration(BestCursor->kind)) {
3491    if (Decl *D = getCursorDecl(*BestCursor)) {
3492      // Avoid having the cursor of an expression replace the declaration cursor
3493      // when the expression source range overlaps the declaration range.
3494      // This can happen for C++ constructor expressions whose range generally
3495      // include the variable declaration, e.g.:
3496      //  MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor.
3497      if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() &&
3498          D->getLocation() == Data->TokenBeginLoc)
3499        return CXChildVisit_Break;
3500    }
3501  }
3502
3503  // If our current best cursor is the construction of a temporary object,
3504  // don't replace that cursor with a type reference, because we want
3505  // clang_getCursor() to point at the constructor.
3506  if (clang_isExpression(BestCursor->kind) &&
3507      isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) &&
3508      cursor.kind == CXCursor_TypeRef) {
3509    // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it
3510    // as having the actual point on the type reference.
3511    *BestCursor = getTypeRefedCallExprCursor(*BestCursor);
3512    return CXChildVisit_Recurse;
3513  }
3514
3515  *BestCursor = cursor;
3516  return CXChildVisit_Recurse;
3517}
3518
3519CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
3520  if (!TU)
3521    return clang_getNullCursor();
3522
3523  ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
3524  ASTUnit::ConcurrencyCheck Check(*CXXUnit);
3525
3526  SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
3527  CXCursor Result = cxcursor::getCursor(TU, SLoc);
3528
3529  bool Logging = getenv("LIBCLANG_LOGGING");
3530  if (Logging) {
3531    CXFile SearchFile;
3532    unsigned SearchLine, SearchColumn;
3533    CXFile ResultFile;
3534    unsigned ResultLine, ResultColumn;
3535    CXString SearchFileName, ResultFileName, KindSpelling, USR;
3536    const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : "";
3537    CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
3538
3539    clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0);
3540    clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine,
3541                               &ResultColumn, 0);
3542    SearchFileName = clang_getFileName(SearchFile);
3543    ResultFileName = clang_getFileName(ResultFile);
3544    KindSpelling = clang_getCursorKindSpelling(Result.kind);
3545    USR = clang_getCursorUSR(Result);
3546    fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d):%s%s\n",
3547            clang_getCString(SearchFileName), SearchLine, SearchColumn,
3548            clang_getCString(KindSpelling),
3549            clang_getCString(ResultFileName), ResultLine, ResultColumn,
3550            clang_getCString(USR), IsDef);
3551    clang_disposeString(SearchFileName);
3552    clang_disposeString(ResultFileName);
3553    clang_disposeString(KindSpelling);
3554    clang_disposeString(USR);
3555
3556    CXCursor Definition = clang_getCursorDefinition(Result);
3557    if (!clang_equalCursors(Definition, clang_getNullCursor())) {
3558      CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition);
3559      CXString DefinitionKindSpelling
3560                                = clang_getCursorKindSpelling(Definition.kind);
3561      CXFile DefinitionFile;
3562      unsigned DefinitionLine, DefinitionColumn;
3563      clang_getExpansionLocation(DefinitionLoc, &DefinitionFile,
3564                                 &DefinitionLine, &DefinitionColumn, 0);
3565      CXString DefinitionFileName = clang_getFileName(DefinitionFile);
3566      fprintf(stderr, "  -> %s(%s:%d:%d)\n",
3567              clang_getCString(DefinitionKindSpelling),
3568              clang_getCString(DefinitionFileName),
3569              DefinitionLine, DefinitionColumn);
3570      clang_disposeString(DefinitionFileName);
3571      clang_disposeString(DefinitionKindSpelling);
3572    }
3573  }
3574
3575  return Result;
3576}
3577
3578CXCursor clang_getNullCursor(void) {
3579  return MakeCXCursorInvalid(CXCursor_InvalidFile);
3580}
3581
3582unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
3583  return X == Y;
3584}
3585
3586unsigned clang_hashCursor(CXCursor C) {
3587  unsigned Index = 0;
3588  if (clang_isExpression(C.kind) || clang_isStatement(C.kind))
3589    Index = 1;
3590
3591  return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue(
3592                                        std::make_pair(C.kind, C.data[Index]));
3593}
3594
3595unsigned clang_isInvalid(enum CXCursorKind K) {
3596  return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
3597}
3598
3599unsigned clang_isDeclaration(enum CXCursorKind K) {
3600  return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
3601}
3602
3603unsigned clang_isReference(enum CXCursorKind K) {
3604  return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
3605}
3606
3607unsigned clang_isExpression(enum CXCursorKind K) {
3608  return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
3609}
3610
3611unsigned clang_isStatement(enum CXCursorKind K) {
3612  return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
3613}
3614
3615unsigned clang_isAttribute(enum CXCursorKind K) {
3616    return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr;
3617}
3618
3619unsigned clang_isTranslationUnit(enum CXCursorKind K) {
3620  return K == CXCursor_TranslationUnit;
3621}
3622
3623unsigned clang_isPreprocessing(enum CXCursorKind K) {
3624  return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing;
3625}
3626
3627unsigned clang_isUnexposed(enum CXCursorKind K) {
3628  switch (K) {
3629    case CXCursor_UnexposedDecl:
3630    case CXCursor_UnexposedExpr:
3631    case CXCursor_UnexposedStmt:
3632    case CXCursor_UnexposedAttr:
3633      return true;
3634    default:
3635      return false;
3636  }
3637}
3638
3639CXCursorKind clang_getCursorKind(CXCursor C) {
3640  return C.kind;
3641}
3642
3643CXSourceLocation clang_getCursorLocation(CXCursor C) {
3644  if (clang_isReference(C.kind)) {
3645    switch (C.kind) {
3646    case CXCursor_ObjCSuperClassRef: {
3647      std::pair<ObjCInterfaceDecl *, SourceLocation> P
3648        = getCursorObjCSuperClassRef(C);
3649      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3650    }
3651
3652    case CXCursor_ObjCProtocolRef: {
3653      std::pair<ObjCProtocolDecl *, SourceLocation> P
3654        = getCursorObjCProtocolRef(C);
3655      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3656    }
3657
3658    case CXCursor_ObjCClassRef: {
3659      std::pair<ObjCInterfaceDecl *, SourceLocation> P
3660        = getCursorObjCClassRef(C);
3661      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3662    }
3663
3664    case CXCursor_TypeRef: {
3665      std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
3666      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3667    }
3668
3669    case CXCursor_TemplateRef: {
3670      std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C);
3671      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3672    }
3673
3674    case CXCursor_NamespaceRef: {
3675      std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C);
3676      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3677    }
3678
3679    case CXCursor_MemberRef: {
3680      std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
3681      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3682    }
3683
3684    case CXCursor_VariableRef: {
3685      std::pair<VarDecl *, SourceLocation> P = getCursorVariableRef(C);
3686      return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
3687    }
3688
3689    case CXCursor_CXXBaseSpecifier: {
3690      CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
3691      if (!BaseSpec)
3692        return clang_getNullLocation();
3693
3694      if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
3695        return cxloc::translateSourceLocation(getCursorContext(C),
3696                                            TSInfo->getTypeLoc().getBeginLoc());
3697
3698      return cxloc::translateSourceLocation(getCursorContext(C),
3699                                        BaseSpec->getSourceRange().getBegin());
3700    }
3701
3702    case CXCursor_LabelRef: {
3703      std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
3704      return cxloc::translateSourceLocation(getCursorContext(C), P.second);
3705    }
3706
3707    case CXCursor_OverloadedDeclRef:
3708      return cxloc::translateSourceLocation(getCursorContext(C),
3709                                          getCursorOverloadedDeclRef(C).second);
3710
3711    default:
3712      // FIXME: Need a way to enumerate all non-reference cases.
3713      llvm_unreachable("Missed a reference kind");
3714    }
3715  }
3716
3717  if (clang_isExpression(C.kind))
3718    return cxloc::translateSourceLocation(getCursorContext(C),
3719                                   getLocationFromExpr(getCursorExpr(C)));
3720
3721  if (clang_isStatement(C.kind))
3722    return cxloc::translateSourceLocation(getCursorContext(C),
3723                                          getCursorStmt(C)->getLocStart());
3724
3725  if (C.kind == CXCursor_PreprocessingDirective) {
3726    SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
3727    return cxloc::translateSourceLocation(getCursorContext(C), L);
3728  }
3729
3730  if (C.kind == CXCursor_MacroExpansion) {
3731    SourceLocation L
3732      = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin();
3733    return cxloc::translateSourceLocation(getCursorContext(C), L);
3734  }
3735
3736  if (C.kind == CXCursor_MacroDefinition) {
3737    SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
3738    return cxloc::translateSourceLocation(getCursorContext(C), L);
3739  }
3740
3741  if (C.kind == CXCursor_InclusionDirective) {
3742    SourceLocation L
3743      = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
3744    return cxloc::translateSourceLocation(getCursorContext(C), L);
3745  }
3746
3747  if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl)
3748    return clang_getNullLocation();
3749
3750  Decl *D = getCursorDecl(C);
3751  if (!D)
3752    return clang_getNullLocation();
3753
3754  SourceLocation Loc = D->getLocation();
3755  // FIXME: Multiple variables declared in a single declaration
3756  // currently lack the information needed to correctly determine their
3757  // ranges when accounting for the type-specifier.  We use context
3758  // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3759  // and if so, whether it is the first decl.
3760  if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3761    if (!cxcursor::isFirstInDeclGroup(C))
3762      Loc = VD->getLocation();
3763  }
3764
3765  return cxloc::translateSourceLocation(getCursorContext(C), Loc);
3766}
3767
3768} // end extern "C"
3769
3770CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) {
3771  assert(TU);
3772
3773  // Guard against an invalid SourceLocation, or we may assert in one
3774  // of the following calls.
3775  if (SLoc.isInvalid())
3776    return clang_getNullCursor();
3777
3778  ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
3779
3780  // Translate the given source location to make it point at the beginning of
3781  // the token under the cursor.
3782  SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(),
3783                                    CXXUnit->getASTContext().getLangOptions());
3784
3785  CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
3786  if (SLoc.isValid()) {
3787    GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result);
3788    CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData,
3789                            /*VisitPreprocessorLast=*/true,
3790                            /*VisitIncludedEntities=*/false,
3791                            SourceLocation(SLoc));
3792    CursorVis.visitFileRegion();
3793  }
3794
3795  return Result;
3796}
3797
3798static SourceRange getRawCursorExtent(CXCursor C) {
3799  if (clang_isReference(C.kind)) {
3800    switch (C.kind) {
3801    case CXCursor_ObjCSuperClassRef:
3802      return  getCursorObjCSuperClassRef(C).second;
3803
3804    case CXCursor_ObjCProtocolRef:
3805      return getCursorObjCProtocolRef(C).second;
3806
3807    case CXCursor_ObjCClassRef:
3808      return getCursorObjCClassRef(C).second;
3809
3810    case CXCursor_TypeRef:
3811      return getCursorTypeRef(C).second;
3812
3813    case CXCursor_TemplateRef:
3814      return getCursorTemplateRef(C).second;
3815
3816    case CXCursor_NamespaceRef:
3817      return getCursorNamespaceRef(C).second;
3818
3819    case CXCursor_MemberRef:
3820      return getCursorMemberRef(C).second;
3821
3822    case CXCursor_CXXBaseSpecifier:
3823      return getCursorCXXBaseSpecifier(C)->getSourceRange();
3824
3825    case CXCursor_LabelRef:
3826      return getCursorLabelRef(C).second;
3827
3828    case CXCursor_OverloadedDeclRef:
3829      return getCursorOverloadedDeclRef(C).second;
3830
3831    case CXCursor_VariableRef:
3832      return getCursorVariableRef(C).second;
3833
3834    default:
3835      // FIXME: Need a way to enumerate all non-reference cases.
3836      llvm_unreachable("Missed a reference kind");
3837    }
3838  }
3839
3840  if (clang_isExpression(C.kind))
3841    return getCursorExpr(C)->getSourceRange();
3842
3843  if (clang_isStatement(C.kind))
3844    return getCursorStmt(C)->getSourceRange();
3845
3846  if (clang_isAttribute(C.kind))
3847    return getCursorAttr(C)->getRange();
3848
3849  if (C.kind == CXCursor_PreprocessingDirective)
3850    return cxcursor::getCursorPreprocessingDirective(C);
3851
3852  if (C.kind == CXCursor_MacroExpansion) {
3853    ASTUnit *TU = getCursorASTUnit(C);
3854    SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange();
3855    return TU->mapRangeFromPreamble(Range);
3856  }
3857
3858  if (C.kind == CXCursor_MacroDefinition) {
3859    ASTUnit *TU = getCursorASTUnit(C);
3860    SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
3861    return TU->mapRangeFromPreamble(Range);
3862  }
3863
3864  if (C.kind == CXCursor_InclusionDirective) {
3865    ASTUnit *TU = getCursorASTUnit(C);
3866    SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange();
3867    return TU->mapRangeFromPreamble(Range);
3868  }
3869
3870  if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
3871    Decl *D = cxcursor::getCursorDecl(C);
3872    if (!D)
3873      return SourceRange();
3874
3875    SourceRange R = D->getSourceRange();
3876    // FIXME: Multiple variables declared in a single declaration
3877    // currently lack the information needed to correctly determine their
3878    // ranges when accounting for the type-specifier.  We use context
3879    // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3880    // and if so, whether it is the first decl.
3881    if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3882      if (!cxcursor::isFirstInDeclGroup(C))
3883        R.setBegin(VD->getLocation());
3884    }
3885    return R;
3886  }
3887  return SourceRange();
3888}
3889
3890/// \brief Retrieves the "raw" cursor extent, which is then extended to include
3891/// the decl-specifier-seq for declarations.
3892static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) {
3893  if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
3894    Decl *D = cxcursor::getCursorDecl(C);
3895    if (!D)
3896      return SourceRange();
3897
3898    SourceRange R = D->getSourceRange();
3899
3900    // Adjust the start of the location for declarations preceded by
3901    // declaration specifiers.
3902    SourceLocation StartLoc;
3903    if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
3904      if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
3905        StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
3906    } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) {
3907      if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
3908        StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
3909    }
3910
3911    if (StartLoc.isValid() && R.getBegin().isValid() &&
3912        SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin()))
3913      R.setBegin(StartLoc);
3914
3915    // FIXME: Multiple variables declared in a single declaration
3916    // currently lack the information needed to correctly determine their
3917    // ranges when accounting for the type-specifier.  We use context
3918    // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
3919    // and if so, whether it is the first decl.
3920    if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
3921      if (!cxcursor::isFirstInDeclGroup(C))
3922        R.setBegin(VD->getLocation());
3923    }
3924
3925    return R;
3926  }
3927
3928  return getRawCursorExtent(C);
3929}
3930
3931extern "C" {
3932
3933CXSourceRange clang_getCursorExtent(CXCursor C) {
3934  SourceRange R = getRawCursorExtent(C);
3935  if (R.isInvalid())
3936    return clang_getNullRange();
3937
3938  return cxloc::translateSourceRange(getCursorContext(C), R);
3939}
3940
3941CXCursor clang_getCursorReferenced(CXCursor C) {
3942  if (clang_isInvalid(C.kind))
3943    return clang_getNullCursor();
3944
3945  CXTranslationUnit tu = getCursorTU(C);
3946  if (clang_isDeclaration(C.kind)) {
3947    Decl *D = getCursorDecl(C);
3948    if (!D)
3949      return clang_getNullCursor();
3950    if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
3951      return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu);
3952    if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D))
3953      if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
3954        return MakeCXCursor(Property, tu);
3955
3956    return C;
3957  }
3958
3959  if (clang_isExpression(C.kind)) {
3960    Expr *E = getCursorExpr(C);
3961    Decl *D = getDeclFromExpr(E);
3962    if (D) {
3963      CXCursor declCursor = MakeCXCursor(D, tu);
3964      declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C),
3965                                               declCursor);
3966      return declCursor;
3967    }
3968
3969    if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E))
3970      return MakeCursorOverloadedDeclRef(Ovl, tu);
3971
3972    return clang_getNullCursor();
3973  }
3974
3975  if (clang_isStatement(C.kind)) {
3976    Stmt *S = getCursorStmt(C);
3977    if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S))
3978      if (LabelDecl *label = Goto->getLabel())
3979        if (LabelStmt *labelS = label->getStmt())
3980        return MakeCXCursor(labelS, getCursorDecl(C), tu);
3981
3982    return clang_getNullCursor();
3983  }
3984
3985  if (C.kind == CXCursor_MacroExpansion) {
3986    if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition())
3987      return MakeMacroDefinitionCursor(Def, tu);
3988  }
3989
3990  if (!clang_isReference(C.kind))
3991    return clang_getNullCursor();
3992
3993  switch (C.kind) {
3994    case CXCursor_ObjCSuperClassRef:
3995      return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu);
3996
3997    case CXCursor_ObjCProtocolRef: {
3998      ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first;
3999      if (ObjCProtocolDecl *Def = Prot->getDefinition())
4000        return MakeCXCursor(Def, tu);
4001
4002      return MakeCXCursor(Prot, tu);
4003    }
4004
4005    case CXCursor_ObjCClassRef: {
4006      ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
4007      if (ObjCInterfaceDecl *Def = Class->getDefinition())
4008        return MakeCXCursor(Def, tu);
4009
4010      return MakeCXCursor(Class, tu);
4011    }
4012
4013    case CXCursor_TypeRef:
4014      return MakeCXCursor(getCursorTypeRef(C).first, tu );
4015
4016    case CXCursor_TemplateRef:
4017      return MakeCXCursor(getCursorTemplateRef(C).first, tu );
4018
4019    case CXCursor_NamespaceRef:
4020      return MakeCXCursor(getCursorNamespaceRef(C).first, tu );
4021
4022    case CXCursor_MemberRef:
4023      return MakeCXCursor(getCursorMemberRef(C).first, tu );
4024
4025    case CXCursor_CXXBaseSpecifier: {
4026      CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
4027      return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(),
4028                                                         tu ));
4029    }
4030
4031    case CXCursor_LabelRef:
4032      // FIXME: We end up faking the "parent" declaration here because we
4033      // don't want to make CXCursor larger.
4034      return MakeCXCursor(getCursorLabelRef(C).first,
4035               static_cast<ASTUnit*>(tu->TUData)->getASTContext()
4036                          .getTranslationUnitDecl(),
4037                          tu);
4038
4039    case CXCursor_OverloadedDeclRef:
4040      return C;
4041
4042    case CXCursor_VariableRef:
4043      return MakeCXCursor(getCursorVariableRef(C).first, tu);
4044
4045    default:
4046      // We would prefer to enumerate all non-reference cursor kinds here.
4047      llvm_unreachable("Unhandled reference cursor kind");
4048  }
4049}
4050
4051CXCursor clang_getCursorDefinition(CXCursor C) {
4052  if (clang_isInvalid(C.kind))
4053    return clang_getNullCursor();
4054
4055  CXTranslationUnit TU = getCursorTU(C);
4056
4057  bool WasReference = false;
4058  if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
4059    C = clang_getCursorReferenced(C);
4060    WasReference = true;
4061  }
4062
4063  if (C.kind == CXCursor_MacroExpansion)
4064    return clang_getCursorReferenced(C);
4065
4066  if (!clang_isDeclaration(C.kind))
4067    return clang_getNullCursor();
4068
4069  Decl *D = getCursorDecl(C);
4070  if (!D)
4071    return clang_getNullCursor();
4072
4073  switch (D->getKind()) {
4074  // Declaration kinds that don't really separate the notions of
4075  // declaration and definition.
4076  case Decl::Namespace:
4077  case Decl::Typedef:
4078  case Decl::TypeAlias:
4079  case Decl::TypeAliasTemplate:
4080  case Decl::TemplateTypeParm:
4081  case Decl::EnumConstant:
4082  case Decl::Field:
4083  case Decl::IndirectField:
4084  case Decl::ObjCIvar:
4085  case Decl::ObjCAtDefsField:
4086  case Decl::ImplicitParam:
4087  case Decl::ParmVar:
4088  case Decl::NonTypeTemplateParm:
4089  case Decl::TemplateTemplateParm:
4090  case Decl::ObjCCategoryImpl:
4091  case Decl::ObjCImplementation:
4092  case Decl::AccessSpec:
4093  case Decl::LinkageSpec:
4094  case Decl::ObjCPropertyImpl:
4095  case Decl::FileScopeAsm:
4096  case Decl::StaticAssert:
4097  case Decl::Block:
4098  case Decl::Label:  // FIXME: Is this right??
4099  case Decl::ClassScopeFunctionSpecialization:
4100  case Decl::Import:
4101    return C;
4102
4103  // Declaration kinds that don't make any sense here, but are
4104  // nonetheless harmless.
4105  case Decl::TranslationUnit:
4106    break;
4107
4108  // Declaration kinds for which the definition is not resolvable.
4109  case Decl::UnresolvedUsingTypename:
4110  case Decl::UnresolvedUsingValue:
4111    break;
4112
4113  case Decl::UsingDirective:
4114    return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
4115                        TU);
4116
4117  case Decl::NamespaceAlias:
4118    return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU);
4119
4120  case Decl::Enum:
4121  case Decl::Record:
4122  case Decl::CXXRecord:
4123  case Decl::ClassTemplateSpecialization:
4124  case Decl::ClassTemplatePartialSpecialization:
4125    if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
4126      return MakeCXCursor(Def, TU);
4127    return clang_getNullCursor();
4128
4129  case Decl::Function:
4130  case Decl::CXXMethod:
4131  case Decl::CXXConstructor:
4132  case Decl::CXXDestructor:
4133  case Decl::CXXConversion: {
4134    const FunctionDecl *Def = 0;
4135    if (cast<FunctionDecl>(D)->getBody(Def))
4136      return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU);
4137    return clang_getNullCursor();
4138  }
4139
4140  case Decl::Var: {
4141    // Ask the variable if it has a definition.
4142    if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
4143      return MakeCXCursor(Def, TU);
4144    return clang_getNullCursor();
4145  }
4146
4147  case Decl::FunctionTemplate: {
4148    const FunctionDecl *Def = 0;
4149    if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
4150      return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU);
4151    return clang_getNullCursor();
4152  }
4153
4154  case Decl::ClassTemplate: {
4155    if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
4156                                                            ->getDefinition())
4157      return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
4158                          TU);
4159    return clang_getNullCursor();
4160  }
4161
4162  case Decl::Using:
4163    return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D),
4164                                       D->getLocation(), TU);
4165
4166  case Decl::UsingShadow:
4167    return clang_getCursorDefinition(
4168                       MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
4169                                    TU));
4170
4171  case Decl::ObjCMethod: {
4172    ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
4173    if (Method->isThisDeclarationADefinition())
4174      return C;
4175
4176    // Dig out the method definition in the associated
4177    // @implementation, if we have it.
4178    // FIXME: The ASTs should make finding the definition easier.
4179    if (ObjCInterfaceDecl *Class
4180                       = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
4181      if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
4182        if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
4183                                                  Method->isInstanceMethod()))
4184          if (Def->isThisDeclarationADefinition())
4185            return MakeCXCursor(Def, TU);
4186
4187    return clang_getNullCursor();
4188  }
4189
4190  case Decl::ObjCCategory:
4191    if (ObjCCategoryImplDecl *Impl
4192                               = cast<ObjCCategoryDecl>(D)->getImplementation())
4193      return MakeCXCursor(Impl, TU);
4194    return clang_getNullCursor();
4195
4196  case Decl::ObjCProtocol:
4197    if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition())
4198      return MakeCXCursor(Def, TU);
4199    return clang_getNullCursor();
4200
4201  case Decl::ObjCInterface: {
4202    // There are two notions of a "definition" for an Objective-C
4203    // class: the interface and its implementation. When we resolved a
4204    // reference to an Objective-C class, produce the @interface as
4205    // the definition; when we were provided with the interface,
4206    // produce the @implementation as the definition.
4207    ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D);
4208    if (WasReference) {
4209      if (ObjCInterfaceDecl *Def = IFace->getDefinition())
4210        return MakeCXCursor(Def, TU);
4211    } else if (ObjCImplementationDecl *Impl = IFace->getImplementation())
4212      return MakeCXCursor(Impl, TU);
4213    return clang_getNullCursor();
4214  }
4215
4216  case Decl::ObjCProperty:
4217    // FIXME: We don't really know where to find the
4218    // ObjCPropertyImplDecls that implement this property.
4219    return clang_getNullCursor();
4220
4221  case Decl::ObjCCompatibleAlias:
4222    if (ObjCInterfaceDecl *Class
4223          = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
4224      if (ObjCInterfaceDecl *Def = Class->getDefinition())
4225        return MakeCXCursor(Def, TU);
4226
4227    return clang_getNullCursor();
4228
4229  case Decl::Friend:
4230    if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
4231      return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
4232    return clang_getNullCursor();
4233
4234  case Decl::FriendTemplate:
4235    if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
4236      return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
4237    return clang_getNullCursor();
4238  }
4239
4240  return clang_getNullCursor();
4241}
4242
4243unsigned clang_isCursorDefinition(CXCursor C) {
4244  if (!clang_isDeclaration(C.kind))
4245    return 0;
4246
4247  return clang_getCursorDefinition(C) == C;
4248}
4249
4250CXCursor clang_getCanonicalCursor(CXCursor C) {
4251  if (!clang_isDeclaration(C.kind))
4252    return C;
4253
4254  if (Decl *D = getCursorDecl(C)) {
4255    if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D))
4256      if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl())
4257        return MakeCXCursor(CatD, getCursorTU(C));
4258
4259    if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
4260      if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
4261        return MakeCXCursor(IFD, getCursorTU(C));
4262
4263    return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C));
4264  }
4265
4266  return C;
4267}
4268
4269unsigned clang_getNumOverloadedDecls(CXCursor C) {
4270  if (C.kind != CXCursor_OverloadedDeclRef)
4271    return 0;
4272
4273  OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
4274  if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
4275    return E->getNumDecls();
4276
4277  if (OverloadedTemplateStorage *S
4278                              = Storage.dyn_cast<OverloadedTemplateStorage*>())
4279    return S->size();
4280
4281  Decl *D = Storage.get<Decl*>();
4282  if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
4283    return Using->shadow_size();
4284
4285  return 0;
4286}
4287
4288CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
4289  if (cursor.kind != CXCursor_OverloadedDeclRef)
4290    return clang_getNullCursor();
4291
4292  if (index >= clang_getNumOverloadedDecls(cursor))
4293    return clang_getNullCursor();
4294
4295  CXTranslationUnit TU = getCursorTU(cursor);
4296  OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first;
4297  if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
4298    return MakeCXCursor(E->decls_begin()[index], TU);
4299
4300  if (OverloadedTemplateStorage *S
4301                              = Storage.dyn_cast<OverloadedTemplateStorage*>())
4302    return MakeCXCursor(S->begin()[index], TU);
4303
4304  Decl *D = Storage.get<Decl*>();
4305  if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
4306    // FIXME: This is, unfortunately, linear time.
4307    UsingDecl::shadow_iterator Pos = Using->shadow_begin();
4308    std::advance(Pos, index);
4309    return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU);
4310  }
4311
4312  return clang_getNullCursor();
4313}
4314
4315void clang_getDefinitionSpellingAndExtent(CXCursor C,
4316                                          const char **startBuf,
4317                                          const char **endBuf,
4318                                          unsigned *startLine,
4319                                          unsigned *startColumn,
4320                                          unsigned *endLine,
4321                                          unsigned *endColumn) {
4322  assert(getCursorDecl(C) && "CXCursor has null decl");
4323  NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
4324  FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
4325  CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
4326
4327  SourceManager &SM = FD->getASTContext().getSourceManager();
4328  *startBuf = SM.getCharacterData(Body->getLBracLoc());
4329  *endBuf = SM.getCharacterData(Body->getRBracLoc());
4330  *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
4331  *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
4332  *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
4333  *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
4334}
4335
4336
4337CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags,
4338                                                unsigned PieceIndex) {
4339  RefNamePieces Pieces;
4340
4341  switch (C.kind) {
4342  case CXCursor_MemberRefExpr:
4343    if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C)))
4344      Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(),
4345                           E->getQualifierLoc().getSourceRange());
4346    break;
4347
4348  case CXCursor_DeclRefExpr:
4349    if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C)))
4350      Pieces = buildPieces(NameFlags, false, E->getNameInfo(),
4351                           E->getQualifierLoc().getSourceRange(),
4352                           E->getOptionalExplicitTemplateArgs());
4353    break;
4354
4355  case CXCursor_CallExpr:
4356    if (CXXOperatorCallExpr *OCE =
4357        dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) {
4358      Expr *Callee = OCE->getCallee();
4359      if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee))
4360        Callee = ICE->getSubExpr();
4361
4362      if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee))
4363        Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(),
4364                             DRE->getQualifierLoc().getSourceRange());
4365    }
4366    break;
4367
4368  default:
4369    break;
4370  }
4371
4372  if (Pieces.empty()) {
4373    if (PieceIndex == 0)
4374      return clang_getCursorExtent(C);
4375  } else if (PieceIndex < Pieces.size()) {
4376      SourceRange R = Pieces[PieceIndex];
4377      if (R.isValid())
4378        return cxloc::translateSourceRange(getCursorContext(C), R);
4379  }
4380
4381  return clang_getNullRange();
4382}
4383
4384void clang_enableStackTraces(void) {
4385  llvm::sys::PrintStackTraceOnErrorSignal();
4386}
4387
4388void clang_executeOnThread(void (*fn)(void*), void *user_data,
4389                           unsigned stack_size) {
4390  llvm::llvm_execute_on_thread(fn, user_data, stack_size);
4391}
4392
4393} // end: extern "C"
4394
4395//===----------------------------------------------------------------------===//
4396// Token-based Operations.
4397//===----------------------------------------------------------------------===//
4398
4399/* CXToken layout:
4400 *   int_data[0]: a CXTokenKind
4401 *   int_data[1]: starting token location
4402 *   int_data[2]: token length
4403 *   int_data[3]: reserved
4404 *   ptr_data: for identifiers and keywords, an IdentifierInfo*.
4405 *   otherwise unused.
4406 */
4407extern "C" {
4408
4409CXTokenKind clang_getTokenKind(CXToken CXTok) {
4410  return static_cast<CXTokenKind>(CXTok.int_data[0]);
4411}
4412
4413CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
4414  switch (clang_getTokenKind(CXTok)) {
4415  case CXToken_Identifier:
4416  case CXToken_Keyword:
4417    // We know we have an IdentifierInfo*, so use that.
4418    return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
4419                            ->getNameStart());
4420
4421  case CXToken_Literal: {
4422    // We have stashed the starting pointer in the ptr_data field. Use it.
4423    const char *Text = static_cast<const char *>(CXTok.ptr_data);
4424    return createCXString(StringRef(Text, CXTok.int_data[2]));
4425  }
4426
4427  case CXToken_Punctuation:
4428  case CXToken_Comment:
4429    break;
4430  }
4431
4432  // We have to find the starting buffer pointer the hard way, by
4433  // deconstructing the source location.
4434  ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
4435  if (!CXXUnit)
4436    return createCXString("");
4437
4438  SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
4439  std::pair<FileID, unsigned> LocInfo
4440    = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc);
4441  bool Invalid = false;
4442  StringRef Buffer
4443    = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
4444  if (Invalid)
4445    return createCXString("");
4446
4447  return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2]));
4448}
4449
4450CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
4451  ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
4452  if (!CXXUnit)
4453    return clang_getNullLocation();
4454
4455  return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
4456                        SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
4457}
4458
4459CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
4460  ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
4461  if (!CXXUnit)
4462    return clang_getNullRange();
4463
4464  return cxloc::translateSourceRange(CXXUnit->getASTContext(),
4465                        SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
4466}
4467
4468static void getTokens(ASTUnit *CXXUnit, SourceRange Range,
4469                      SmallVectorImpl<CXToken> &CXTokens) {
4470  SourceManager &SourceMgr = CXXUnit->getSourceManager();
4471  std::pair<FileID, unsigned> BeginLocInfo
4472    = SourceMgr.getDecomposedLoc(Range.getBegin());
4473  std::pair<FileID, unsigned> EndLocInfo
4474    = SourceMgr.getDecomposedLoc(Range.getEnd());
4475
4476  // Cannot tokenize across files.
4477  if (BeginLocInfo.first != EndLocInfo.first)
4478    return;
4479
4480  // Create a lexer
4481  bool Invalid = false;
4482  StringRef Buffer
4483    = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
4484  if (Invalid)
4485    return;
4486
4487  Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
4488            CXXUnit->getASTContext().getLangOptions(),
4489            Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end());
4490  Lex.SetCommentRetentionState(true);
4491
4492  // Lex tokens until we hit the end of the range.
4493  const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
4494  Token Tok;
4495  bool previousWasAt = false;
4496  do {
4497    // Lex the next token
4498    Lex.LexFromRawLexer(Tok);
4499    if (Tok.is(tok::eof))
4500      break;
4501
4502    // Initialize the CXToken.
4503    CXToken CXTok;
4504
4505    //   - Common fields
4506    CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
4507    CXTok.int_data[2] = Tok.getLength();
4508    CXTok.int_data[3] = 0;
4509
4510    //   - Kind-specific fields
4511    if (Tok.isLiteral()) {
4512      CXTok.int_data[0] = CXToken_Literal;
4513      CXTok.ptr_data = (void *)Tok.getLiteralData();
4514    } else if (Tok.is(tok::raw_identifier)) {
4515      // Lookup the identifier to determine whether we have a keyword.
4516      IdentifierInfo *II
4517        = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok);
4518
4519      if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
4520        CXTok.int_data[0] = CXToken_Keyword;
4521      }
4522      else {
4523        CXTok.int_data[0] = Tok.is(tok::identifier)
4524          ? CXToken_Identifier
4525          : CXToken_Keyword;
4526      }
4527      CXTok.ptr_data = II;
4528    } else if (Tok.is(tok::comment)) {
4529      CXTok.int_data[0] = CXToken_Comment;
4530      CXTok.ptr_data = 0;
4531    } else {
4532      CXTok.int_data[0] = CXToken_Punctuation;
4533      CXTok.ptr_data = 0;
4534    }
4535    CXTokens.push_back(CXTok);
4536    previousWasAt = Tok.is(tok::at);
4537  } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
4538}
4539
4540void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
4541                    CXToken **Tokens, unsigned *NumTokens) {
4542  if (Tokens)
4543    *Tokens = 0;
4544  if (NumTokens)
4545    *NumTokens = 0;
4546
4547  ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
4548  if (!CXXUnit || !Tokens || !NumTokens)
4549    return;
4550
4551  ASTUnit::ConcurrencyCheck Check(*CXXUnit);
4552
4553  SourceRange R = cxloc::translateCXSourceRange(Range);
4554  if (R.isInvalid())
4555    return;
4556
4557  SmallVector<CXToken, 32> CXTokens;
4558  getTokens(CXXUnit, R, CXTokens);
4559
4560  if (CXTokens.empty())
4561    return;
4562
4563  *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
4564  memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
4565  *NumTokens = CXTokens.size();
4566}
4567
4568void clang_disposeTokens(CXTranslationUnit TU,
4569                         CXToken *Tokens, unsigned NumTokens) {
4570  free(Tokens);
4571}
4572
4573} // end: extern "C"
4574
4575//===----------------------------------------------------------------------===//
4576// Token annotation APIs.
4577//===----------------------------------------------------------------------===//
4578
4579typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
4580static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
4581                                                     CXCursor parent,
4582                                                     CXClientData client_data);
4583namespace {
4584class AnnotateTokensWorker {
4585  AnnotateTokensData &Annotated;
4586  CXToken *Tokens;
4587  CXCursor *Cursors;
4588  unsigned NumTokens;
4589  unsigned TokIdx;
4590  unsigned PreprocessingTokIdx;
4591  CursorVisitor AnnotateVis;
4592  SourceManager &SrcMgr;
4593  bool HasContextSensitiveKeywords;
4594
4595  bool MoreTokens() const { return TokIdx < NumTokens; }
4596  unsigned NextToken() const { return TokIdx; }
4597  void AdvanceToken() { ++TokIdx; }
4598  SourceLocation GetTokenLoc(unsigned tokI) {
4599    return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]);
4600  }
4601  bool isFunctionMacroToken(unsigned tokI) const {
4602    return Tokens[tokI].int_data[3] != 0;
4603  }
4604  SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const {
4605    return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]);
4606  }
4607
4608  void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange);
4609  void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult,
4610                                             SourceRange);
4611
4612public:
4613  AnnotateTokensWorker(AnnotateTokensData &annotated,
4614                       CXToken *tokens, CXCursor *cursors, unsigned numTokens,
4615                       CXTranslationUnit tu, SourceRange RegionOfInterest)
4616    : Annotated(annotated), Tokens(tokens), Cursors(cursors),
4617      NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
4618      AnnotateVis(tu,
4619                  AnnotateTokensVisitor, this,
4620                  /*VisitPreprocessorLast=*/true,
4621                  /*VisitIncludedEntities=*/false,
4622                  RegionOfInterest),
4623      SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()),
4624      HasContextSensitiveKeywords(false) { }
4625
4626  void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
4627  enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
4628  void AnnotateTokens();
4629
4630  /// \brief Determine whether the annotator saw any cursors that have
4631  /// context-sensitive keywords.
4632  bool hasContextSensitiveKeywords() const {
4633    return HasContextSensitiveKeywords;
4634  }
4635};
4636}
4637
4638void AnnotateTokensWorker::AnnotateTokens() {
4639  // Walk the AST within the region of interest, annotating tokens
4640  // along the way.
4641  AnnotateVis.visitFileRegion();
4642
4643  for (unsigned I = 0 ; I < TokIdx ; ++I) {
4644    AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
4645    if (Pos != Annotated.end() &&
4646        (clang_isInvalid(Cursors[I].kind) ||
4647         Pos->second.kind != CXCursor_PreprocessingDirective))
4648      Cursors[I] = Pos->second;
4649  }
4650
4651  // Finish up annotating any tokens left.
4652  if (!MoreTokens())
4653    return;
4654
4655  const CXCursor &C = clang_getNullCursor();
4656  for (unsigned I = TokIdx ; I < NumTokens ; ++I) {
4657    if (I < PreprocessingTokIdx && clang_isPreprocessing(Cursors[I].kind))
4658      continue;
4659
4660    AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
4661    Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second;
4662  }
4663}
4664
4665/// \brief It annotates and advances tokens with a cursor until the comparison
4666//// between the cursor location and the source range is the same as
4667/// \arg compResult.
4668///
4669/// Pass RangeBefore to annotate tokens with a cursor until a range is reached.
4670/// Pass RangeOverlap to annotate tokens inside a range.
4671void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC,
4672                                               RangeComparisonResult compResult,
4673                                               SourceRange range) {
4674  while (MoreTokens()) {
4675    const unsigned I = NextToken();
4676    if (isFunctionMacroToken(I))
4677      return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range);
4678
4679    SourceLocation TokLoc = GetTokenLoc(I);
4680    if (LocationCompare(SrcMgr, TokLoc, range) == compResult) {
4681      Cursors[I] = updateC;
4682      AdvanceToken();
4683      continue;
4684    }
4685    break;
4686  }
4687}
4688
4689/// \brief Special annotation handling for macro argument tokens.
4690void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens(
4691                                               CXCursor updateC,
4692                                               RangeComparisonResult compResult,
4693                                               SourceRange range) {
4694  assert(MoreTokens());
4695  assert(isFunctionMacroToken(NextToken()) &&
4696         "Should be called only for macro arg tokens");
4697
4698  // This works differently than annotateAndAdvanceTokens; because expanded
4699  // macro arguments can have arbitrary translation-unit source order, we do not
4700  // advance the token index one by one until a token fails the range test.
4701  // We only advance once past all of the macro arg tokens if all of them
4702  // pass the range test. If one of them fails we keep the token index pointing
4703  // at the start of the macro arg tokens so that the failing token will be
4704  // annotated by a subsequent annotation try.
4705
4706  bool atLeastOneCompFail = false;
4707
4708  unsigned I = NextToken();
4709  for (; I < NumTokens && isFunctionMacroToken(I); ++I) {
4710    SourceLocation TokLoc = getFunctionMacroTokenLoc(I);
4711    if (TokLoc.isFileID())
4712      continue; // not macro arg token, it's parens or comma.
4713    if (LocationCompare(SrcMgr, TokLoc, range) == compResult) {
4714      if (clang_isInvalid(clang_getCursorKind(Cursors[I])))
4715        Cursors[I] = updateC;
4716    } else
4717      atLeastOneCompFail = true;
4718  }
4719
4720  if (!atLeastOneCompFail)
4721    TokIdx = I; // All of the tokens were handled, advance beyond all of them.
4722}
4723
4724enum CXChildVisitResult
4725AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
4726  CXSourceLocation Loc = clang_getCursorLocation(cursor);
4727  SourceRange cursorRange = getRawCursorExtent(cursor);
4728  if (cursorRange.isInvalid())
4729    return CXChildVisit_Recurse;
4730
4731  if (!HasContextSensitiveKeywords) {
4732    // Objective-C properties can have context-sensitive keywords.
4733    if (cursor.kind == CXCursor_ObjCPropertyDecl) {
4734      if (ObjCPropertyDecl *Property
4735                  = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor)))
4736        HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0;
4737    }
4738    // Objective-C methods can have context-sensitive keywords.
4739    else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl ||
4740             cursor.kind == CXCursor_ObjCClassMethodDecl) {
4741      if (ObjCMethodDecl *Method
4742            = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) {
4743        if (Method->getObjCDeclQualifier())
4744          HasContextSensitiveKeywords = true;
4745        else {
4746          for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
4747                                           PEnd = Method->param_end();
4748               P != PEnd; ++P) {
4749            if ((*P)->getObjCDeclQualifier()) {
4750              HasContextSensitiveKeywords = true;
4751              break;
4752            }
4753          }
4754        }
4755      }
4756    }
4757    // C++ methods can have context-sensitive keywords.
4758    else if (cursor.kind == CXCursor_CXXMethod) {
4759      if (CXXMethodDecl *Method
4760                  = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) {
4761        if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>())
4762          HasContextSensitiveKeywords = true;
4763      }
4764    }
4765    // C++ classes can have context-sensitive keywords.
4766    else if (cursor.kind == CXCursor_StructDecl ||
4767             cursor.kind == CXCursor_ClassDecl ||
4768             cursor.kind == CXCursor_ClassTemplate ||
4769             cursor.kind == CXCursor_ClassTemplatePartialSpecialization) {
4770      if (Decl *D = getCursorDecl(cursor))
4771        if (D->hasAttr<FinalAttr>())
4772          HasContextSensitiveKeywords = true;
4773    }
4774  }
4775
4776  if (clang_isPreprocessing(cursor.kind)) {
4777    // For macro expansions, just note where the beginning of the macro
4778    // expansion occurs.
4779    if (cursor.kind == CXCursor_MacroExpansion) {
4780      Annotated[Loc.int_data] = cursor;
4781      return CXChildVisit_Recurse;
4782    }
4783
4784    // Items in the preprocessing record are kept separate from items in
4785    // declarations, so we keep a separate token index.
4786    unsigned SavedTokIdx = TokIdx;
4787    TokIdx = PreprocessingTokIdx;
4788
4789    // Skip tokens up until we catch up to the beginning of the preprocessing
4790    // entry.
4791    while (MoreTokens()) {
4792      const unsigned I = NextToken();
4793      SourceLocation TokLoc = GetTokenLoc(I);
4794      switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4795      case RangeBefore:
4796        AdvanceToken();
4797        continue;
4798      case RangeAfter:
4799      case RangeOverlap:
4800        break;
4801      }
4802      break;
4803    }
4804
4805    // Look at all of the tokens within this range.
4806    while (MoreTokens()) {
4807      const unsigned I = NextToken();
4808      SourceLocation TokLoc = GetTokenLoc(I);
4809      switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
4810      case RangeBefore:
4811        llvm_unreachable("Infeasible");
4812      case RangeAfter:
4813        break;
4814      case RangeOverlap:
4815        Cursors[I] = cursor;
4816        AdvanceToken();
4817        continue;
4818      }
4819      break;
4820    }
4821
4822    // Save the preprocessing token index; restore the non-preprocessing
4823    // token index.
4824    PreprocessingTokIdx = TokIdx;
4825    TokIdx = SavedTokIdx;
4826    return CXChildVisit_Recurse;
4827  }
4828
4829  if (cursorRange.isInvalid())
4830    return CXChildVisit_Continue;
4831
4832  SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data);
4833
4834  // Adjust the annotated range based specific declarations.
4835  const enum CXCursorKind cursorK = clang_getCursorKind(cursor);
4836  if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) {
4837    Decl *D = cxcursor::getCursorDecl(cursor);
4838
4839    SourceLocation StartLoc;
4840    if (const DeclaratorDecl *DD = dyn_cast_or_null<DeclaratorDecl>(D)) {
4841      if (TypeSourceInfo *TI = DD->getTypeSourceInfo())
4842        StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
4843    } else if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(D)) {
4844      if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo())
4845        StartLoc = TI->getTypeLoc().getSourceRange().getBegin();
4846    }
4847
4848    if (StartLoc.isValid() && L.isValid() &&
4849        SrcMgr.isBeforeInTranslationUnit(StartLoc, L))
4850      cursorRange.setBegin(StartLoc);
4851  }
4852
4853  // If the location of the cursor occurs within a macro instantiation, record
4854  // the spelling location of the cursor in our annotation map.  We can then
4855  // paper over the token labelings during a post-processing step to try and
4856  // get cursor mappings for tokens that are the *arguments* of a macro
4857  // instantiation.
4858  if (L.isMacroID()) {
4859    unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding();
4860    // Only invalidate the old annotation if it isn't part of a preprocessing
4861    // directive.  Here we assume that the default construction of CXCursor
4862    // results in CXCursor.kind being an initialized value (i.e., 0).  If
4863    // this isn't the case, we can fix by doing lookup + insertion.
4864
4865    CXCursor &oldC = Annotated[rawEncoding];
4866    if (!clang_isPreprocessing(oldC.kind))
4867      oldC = cursor;
4868  }
4869
4870  const enum CXCursorKind K = clang_getCursorKind(parent);
4871  const CXCursor updateC =
4872    (clang_isInvalid(K) || K == CXCursor_TranslationUnit)
4873     ? clang_getNullCursor() : parent;
4874
4875  annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange);
4876
4877  // Avoid having the cursor of an expression "overwrite" the annotation of the
4878  // variable declaration that it belongs to.
4879  // This can happen for C++ constructor expressions whose range generally
4880  // include the variable declaration, e.g.:
4881  //  MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor.
4882  if (clang_isExpression(cursorK)) {
4883    Expr *E = getCursorExpr(cursor);
4884    if (Decl *D = getCursorParentDecl(cursor)) {
4885      const unsigned I = NextToken();
4886      if (E->getLocStart().isValid() && D->getLocation().isValid() &&
4887          E->getLocStart() == D->getLocation() &&
4888          E->getLocStart() == GetTokenLoc(I)) {
4889        Cursors[I] = updateC;
4890        AdvanceToken();
4891      }
4892    }
4893  }
4894
4895  // Visit children to get their cursor information.
4896  const unsigned BeforeChildren = NextToken();
4897  VisitChildren(cursor);
4898  const unsigned AfterChildren = NextToken();
4899
4900  // Scan the tokens that are at the end of the cursor, but are not captured
4901  // but the child cursors.
4902  annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange);
4903
4904  // Scan the tokens that are at the beginning of the cursor, but are not
4905  // capture by the child cursors.
4906  for (unsigned I = BeforeChildren; I != AfterChildren; ++I) {
4907    if (!clang_isInvalid(clang_getCursorKind(Cursors[I])))
4908      break;
4909
4910    Cursors[I] = cursor;
4911  }
4912
4913  return CXChildVisit_Continue;
4914}
4915
4916static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
4917                                                     CXCursor parent,
4918                                                     CXClientData client_data) {
4919  return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
4920}
4921
4922namespace {
4923
4924/// \brief Uses the macro expansions in the preprocessing record to find
4925/// and mark tokens that are macro arguments. This info is used by the
4926/// AnnotateTokensWorker.
4927class MarkMacroArgTokensVisitor {
4928  SourceManager &SM;
4929  CXToken *Tokens;
4930  unsigned NumTokens;
4931  unsigned CurIdx;
4932
4933public:
4934  MarkMacroArgTokensVisitor(SourceManager &SM,
4935                            CXToken *tokens, unsigned numTokens)
4936    : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { }
4937
4938  CXChildVisitResult visit(CXCursor cursor, CXCursor parent) {
4939    if (cursor.kind != CXCursor_MacroExpansion)
4940      return CXChildVisit_Continue;
4941
4942    SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange();
4943    if (macroRange.getBegin() == macroRange.getEnd())
4944      return CXChildVisit_Continue; // it's not a function macro.
4945
4946    for (; CurIdx < NumTokens; ++CurIdx) {
4947      if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx),
4948                                        macroRange.getBegin()))
4949        break;
4950    }
4951
4952    if (CurIdx == NumTokens)
4953      return CXChildVisit_Break;
4954
4955    for (; CurIdx < NumTokens; ++CurIdx) {
4956      SourceLocation tokLoc = getTokenLoc(CurIdx);
4957      if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd()))
4958        break;
4959
4960      setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc));
4961    }
4962
4963    if (CurIdx == NumTokens)
4964      return CXChildVisit_Break;
4965
4966    return CXChildVisit_Continue;
4967  }
4968
4969private:
4970  SourceLocation getTokenLoc(unsigned tokI) {
4971    return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]);
4972  }
4973
4974  void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) {
4975    // The third field is reserved and currently not used. Use it here
4976    // to mark macro arg expanded tokens with their expanded locations.
4977    Tokens[tokI].int_data[3] = loc.getRawEncoding();
4978  }
4979};
4980
4981} // end anonymous namespace
4982
4983static CXChildVisitResult
4984MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent,
4985                                  CXClientData client_data) {
4986  return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor,
4987                                                                     parent);
4988}
4989
4990namespace {
4991  struct clang_annotateTokens_Data {
4992    CXTranslationUnit TU;
4993    ASTUnit *CXXUnit;
4994    CXToken *Tokens;
4995    unsigned NumTokens;
4996    CXCursor *Cursors;
4997  };
4998}
4999
5000static void annotatePreprocessorTokens(CXTranslationUnit TU,
5001                                       SourceRange RegionOfInterest,
5002                                       AnnotateTokensData &Annotated) {
5003  ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
5004
5005  SourceManager &SourceMgr = CXXUnit->getSourceManager();
5006  std::pair<FileID, unsigned> BeginLocInfo
5007    = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin());
5008  std::pair<FileID, unsigned> EndLocInfo
5009    = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd());
5010
5011  if (BeginLocInfo.first != EndLocInfo.first)
5012    return;
5013
5014  StringRef Buffer;
5015  bool Invalid = false;
5016  Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid);
5017  if (Buffer.empty() || Invalid)
5018    return;
5019
5020  Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
5021            CXXUnit->getASTContext().getLangOptions(),
5022            Buffer.begin(), Buffer.data() + BeginLocInfo.second,
5023            Buffer.end());
5024  Lex.SetCommentRetentionState(true);
5025
5026  // Lex tokens in raw mode until we hit the end of the range, to avoid
5027  // entering #includes or expanding macros.
5028  while (true) {
5029    Token Tok;
5030    Lex.LexFromRawLexer(Tok);
5031
5032  reprocess:
5033    if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
5034      // We have found a preprocessing directive. Gobble it up so that we
5035      // don't see it while preprocessing these tokens later, but keep track
5036      // of all of the token locations inside this preprocessing directive so
5037      // that we can annotate them appropriately.
5038      //
5039      // FIXME: Some simple tests here could identify macro definitions and
5040      // #undefs, to provide specific cursor kinds for those.
5041      SmallVector<SourceLocation, 32> Locations;
5042      do {
5043        Locations.push_back(Tok.getLocation());
5044        Lex.LexFromRawLexer(Tok);
5045      } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof));
5046
5047      using namespace cxcursor;
5048      CXCursor Cursor
5049      = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(),
5050                                                     Locations.back()),
5051                                         TU);
5052      for (unsigned I = 0, N = Locations.size(); I != N; ++I) {
5053        Annotated[Locations[I].getRawEncoding()] = Cursor;
5054      }
5055
5056      if (Tok.isAtStartOfLine())
5057        goto reprocess;
5058
5059      continue;
5060    }
5061
5062    if (Tok.is(tok::eof))
5063      break;
5064  }
5065}
5066
5067// This gets run a separate thread to avoid stack blowout.
5068static void clang_annotateTokensImpl(void *UserData) {
5069  CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU;
5070  ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit;
5071  CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens;
5072  const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens;
5073  CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors;
5074
5075  // Determine the region of interest, which contains all of the tokens.
5076  SourceRange RegionOfInterest;
5077  RegionOfInterest.setBegin(
5078    cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
5079  RegionOfInterest.setEnd(
5080    cxloc::translateSourceLocation(clang_getTokenLocation(TU,
5081                                                         Tokens[NumTokens-1])));
5082
5083  // A mapping from the source locations found when re-lexing or traversing the
5084  // region of interest to the corresponding cursors.
5085  AnnotateTokensData Annotated;
5086
5087  // Relex the tokens within the source range to look for preprocessing
5088  // directives.
5089  annotatePreprocessorTokens(TU, RegionOfInterest, Annotated);
5090
5091  if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
5092    // Search and mark tokens that are macro argument expansions.
5093    MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(),
5094                                      Tokens, NumTokens);
5095    CursorVisitor MacroArgMarker(TU,
5096                                 MarkMacroArgTokensVisitorDelegate, &Visitor,
5097                                 /*VisitPreprocessorLast=*/true,
5098                                 /*VisitIncludedEntities=*/false,
5099                                 RegionOfInterest);
5100    MacroArgMarker.visitPreprocessedEntitiesInRegion();
5101  }
5102
5103  // Annotate all of the source locations in the region of interest that map to
5104  // a specific cursor.
5105  AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens,
5106                         TU, RegionOfInterest);
5107
5108  // FIXME: We use a ridiculous stack size here because the data-recursion
5109  // algorithm uses a large stack frame than the non-data recursive version,
5110  // and AnnotationTokensWorker currently transforms the data-recursion
5111  // algorithm back into a traditional recursion by explicitly calling
5112  // VisitChildren().  We will need to remove this explicit recursive call.
5113  W.AnnotateTokens();
5114
5115  // If we ran into any entities that involve context-sensitive keywords,
5116  // take another pass through the tokens to mark them as such.
5117  if (W.hasContextSensitiveKeywords()) {
5118    for (unsigned I = 0; I != NumTokens; ++I) {
5119      if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier)
5120        continue;
5121
5122      if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) {
5123        IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
5124        if (ObjCPropertyDecl *Property
5125            = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) {
5126          if (Property->getPropertyAttributesAsWritten() != 0 &&
5127              llvm::StringSwitch<bool>(II->getName())
5128              .Case("readonly", true)
5129              .Case("assign", true)
5130              .Case("unsafe_unretained", true)
5131              .Case("readwrite", true)
5132              .Case("retain", true)
5133              .Case("copy", true)
5134              .Case("nonatomic", true)
5135              .Case("atomic", true)
5136              .Case("getter", true)
5137              .Case("setter", true)
5138              .Case("strong", true)
5139              .Case("weak", true)
5140              .Default(false))
5141            Tokens[I].int_data[0] = CXToken_Keyword;
5142        }
5143        continue;
5144      }
5145
5146      if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl ||
5147          Cursors[I].kind == CXCursor_ObjCClassMethodDecl) {
5148        IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data);
5149        if (llvm::StringSwitch<bool>(II->getName())
5150            .Case("in", true)
5151            .Case("out", true)
5152            .Case("inout", true)
5153            .Case("oneway", true)
5154            .Case("bycopy", true)
5155            .Case("byref", true)
5156            .Default(false))
5157          Tokens[I].int_data[0] = CXToken_Keyword;
5158        continue;
5159      }
5160
5161      if (Cursors[I].kind == CXCursor_CXXFinalAttr ||
5162          Cursors[I].kind == CXCursor_CXXOverrideAttr) {
5163        Tokens[I].int_data[0] = CXToken_Keyword;
5164        continue;
5165      }
5166    }
5167  }
5168}
5169
5170extern "C" {
5171
5172void clang_annotateTokens(CXTranslationUnit TU,
5173                          CXToken *Tokens, unsigned NumTokens,
5174                          CXCursor *Cursors) {
5175
5176  if (NumTokens == 0 || !Tokens || !Cursors)
5177    return;
5178
5179  // Any token we don't specifically annotate will have a NULL cursor.
5180  CXCursor C = clang_getNullCursor();
5181  for (unsigned I = 0; I != NumTokens; ++I)
5182    Cursors[I] = C;
5183
5184  ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
5185  if (!CXXUnit)
5186    return;
5187
5188  ASTUnit::ConcurrencyCheck Check(*CXXUnit);
5189
5190  clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors };
5191  llvm::CrashRecoveryContext CRC;
5192  if (!RunSafely(CRC, clang_annotateTokensImpl, &data,
5193                 GetSafetyThreadStackSize() * 2)) {
5194    fprintf(stderr, "libclang: crash detected while annotating tokens\n");
5195  }
5196}
5197
5198} // end: extern "C"
5199
5200//===----------------------------------------------------------------------===//
5201// Operations for querying linkage of a cursor.
5202//===----------------------------------------------------------------------===//
5203
5204extern "C" {
5205CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
5206  if (!clang_isDeclaration(cursor.kind))
5207    return CXLinkage_Invalid;
5208
5209  Decl *D = cxcursor::getCursorDecl(cursor);
5210  if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
5211    switch (ND->getLinkage()) {
5212      case NoLinkage: return CXLinkage_NoLinkage;
5213      case InternalLinkage: return CXLinkage_Internal;
5214      case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
5215      case ExternalLinkage: return CXLinkage_External;
5216    };
5217
5218  return CXLinkage_Invalid;
5219}
5220} // end: extern "C"
5221
5222//===----------------------------------------------------------------------===//
5223// Operations for querying language of a cursor.
5224//===----------------------------------------------------------------------===//
5225
5226static CXLanguageKind getDeclLanguage(const Decl *D) {
5227  if (!D)
5228    return CXLanguage_C;
5229
5230  switch (D->getKind()) {
5231    default:
5232      break;
5233    case Decl::ImplicitParam:
5234    case Decl::ObjCAtDefsField:
5235    case Decl::ObjCCategory:
5236    case Decl::ObjCCategoryImpl:
5237    case Decl::ObjCCompatibleAlias:
5238    case Decl::ObjCImplementation:
5239    case Decl::ObjCInterface:
5240    case Decl::ObjCIvar:
5241    case Decl::ObjCMethod:
5242    case Decl::ObjCProperty:
5243    case Decl::ObjCPropertyImpl:
5244    case Decl::ObjCProtocol:
5245      return CXLanguage_ObjC;
5246    case Decl::CXXConstructor:
5247    case Decl::CXXConversion:
5248    case Decl::CXXDestructor:
5249    case Decl::CXXMethod:
5250    case Decl::CXXRecord:
5251    case Decl::ClassTemplate:
5252    case Decl::ClassTemplatePartialSpecialization:
5253    case Decl::ClassTemplateSpecialization:
5254    case Decl::Friend:
5255    case Decl::FriendTemplate:
5256    case Decl::FunctionTemplate:
5257    case Decl::LinkageSpec:
5258    case Decl::Namespace:
5259    case Decl::NamespaceAlias:
5260    case Decl::NonTypeTemplateParm:
5261    case Decl::StaticAssert:
5262    case Decl::TemplateTemplateParm:
5263    case Decl::TemplateTypeParm:
5264    case Decl::UnresolvedUsingTypename:
5265    case Decl::UnresolvedUsingValue:
5266    case Decl::Using:
5267    case Decl::UsingDirective:
5268    case Decl::UsingShadow:
5269      return CXLanguage_CPlusPlus;
5270  }
5271
5272  return CXLanguage_C;
5273}
5274
5275extern "C" {
5276
5277enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) {
5278  if (clang_isDeclaration(cursor.kind))
5279    if (Decl *D = cxcursor::getCursorDecl(cursor)) {
5280      if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted())
5281        return CXAvailability_Available;
5282
5283      switch (D->getAvailability()) {
5284      case AR_Available:
5285      case AR_NotYetIntroduced:
5286        return CXAvailability_Available;
5287
5288      case AR_Deprecated:
5289        return CXAvailability_Deprecated;
5290
5291      case AR_Unavailable:
5292        return CXAvailability_NotAvailable;
5293      }
5294    }
5295
5296  return CXAvailability_Available;
5297}
5298
5299CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
5300  if (clang_isDeclaration(cursor.kind))
5301    return getDeclLanguage(cxcursor::getCursorDecl(cursor));
5302
5303  return CXLanguage_Invalid;
5304}
5305
5306 /// \brief If the given cursor is the "templated" declaration
5307 /// descibing a class or function template, return the class or
5308 /// function template.
5309static Decl *maybeGetTemplateCursor(Decl *D) {
5310  if (!D)
5311    return 0;
5312
5313  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5314    if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate())
5315      return FunTmpl;
5316
5317  if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
5318    if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate())
5319      return ClassTmpl;
5320
5321  return D;
5322}
5323
5324CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
5325  if (clang_isDeclaration(cursor.kind)) {
5326    if (Decl *D = getCursorDecl(cursor)) {
5327      DeclContext *DC = D->getDeclContext();
5328      if (!DC)
5329        return clang_getNullCursor();
5330
5331      return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
5332                          getCursorTU(cursor));
5333    }
5334  }
5335
5336  if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) {
5337    if (Decl *D = getCursorDecl(cursor))
5338      return MakeCXCursor(D, getCursorTU(cursor));
5339  }
5340
5341  return clang_getNullCursor();
5342}
5343
5344CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
5345  if (clang_isDeclaration(cursor.kind)) {
5346    if (Decl *D = getCursorDecl(cursor)) {
5347      DeclContext *DC = D->getLexicalDeclContext();
5348      if (!DC)
5349        return clang_getNullCursor();
5350
5351      return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
5352                          getCursorTU(cursor));
5353    }
5354  }
5355
5356  // FIXME: Note that we can't easily compute the lexical context of a
5357  // statement or expression, so we return nothing.
5358  return clang_getNullCursor();
5359}
5360
5361void clang_getOverriddenCursors(CXCursor cursor,
5362                                CXCursor **overridden,
5363                                unsigned *num_overridden) {
5364  if (overridden)
5365    *overridden = 0;
5366  if (num_overridden)
5367    *num_overridden = 0;
5368  if (!overridden || !num_overridden)
5369    return;
5370
5371  SmallVector<CXCursor, 8> Overridden;
5372  cxcursor::getOverriddenCursors(cursor, Overridden);
5373
5374  // Don't allocate memory if we have no overriden cursors.
5375  if (Overridden.size() == 0)
5376    return;
5377
5378  *num_overridden = Overridden.size();
5379  *overridden = new CXCursor [Overridden.size()];
5380  std::copy(Overridden.begin(), Overridden.end(), *overridden);
5381}
5382
5383void clang_disposeOverriddenCursors(CXCursor *overridden) {
5384  delete [] overridden;
5385}
5386
5387CXFile clang_getIncludedFile(CXCursor cursor) {
5388  if (cursor.kind != CXCursor_InclusionDirective)
5389    return 0;
5390
5391  InclusionDirective *ID = getCursorInclusionDirective(cursor);
5392  return (void *)ID->getFile();
5393}
5394
5395} // end: extern "C"
5396
5397
5398//===----------------------------------------------------------------------===//
5399// C++ AST instrospection.
5400//===----------------------------------------------------------------------===//
5401
5402extern "C" {
5403unsigned clang_CXXMethod_isStatic(CXCursor C) {
5404  if (!clang_isDeclaration(C.kind))
5405    return 0;
5406
5407  CXXMethodDecl *Method = 0;
5408  Decl *D = cxcursor::getCursorDecl(C);
5409  if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
5410    Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
5411  else
5412    Method = dyn_cast_or_null<CXXMethodDecl>(D);
5413  return (Method && Method->isStatic()) ? 1 : 0;
5414}
5415
5416unsigned clang_CXXMethod_isVirtual(CXCursor C) {
5417  if (!clang_isDeclaration(C.kind))
5418    return 0;
5419
5420  CXXMethodDecl *Method = 0;
5421  Decl *D = cxcursor::getCursorDecl(C);
5422  if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
5423    Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
5424  else
5425    Method = dyn_cast_or_null<CXXMethodDecl>(D);
5426  return (Method && Method->isVirtual()) ? 1 : 0;
5427}
5428} // end: extern "C"
5429
5430//===----------------------------------------------------------------------===//
5431// Attribute introspection.
5432//===----------------------------------------------------------------------===//
5433
5434extern "C" {
5435CXType clang_getIBOutletCollectionType(CXCursor C) {
5436  if (C.kind != CXCursor_IBOutletCollectionAttr)
5437    return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C));
5438
5439  IBOutletCollectionAttr *A =
5440    cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C));
5441
5442  return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C));
5443}
5444} // end: extern "C"
5445
5446//===----------------------------------------------------------------------===//
5447// Inspecting memory usage.
5448//===----------------------------------------------------------------------===//
5449
5450typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries;
5451
5452static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries,
5453                                              enum CXTUResourceUsageKind k,
5454                                              unsigned long amount) {
5455  CXTUResourceUsageEntry entry = { k, amount };
5456  entries.push_back(entry);
5457}
5458
5459extern "C" {
5460
5461const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) {
5462  const char *str = "";
5463  switch (kind) {
5464    case CXTUResourceUsage_AST:
5465      str = "ASTContext: expressions, declarations, and types";
5466      break;
5467    case CXTUResourceUsage_Identifiers:
5468      str = "ASTContext: identifiers";
5469      break;
5470    case CXTUResourceUsage_Selectors:
5471      str = "ASTContext: selectors";
5472      break;
5473    case CXTUResourceUsage_GlobalCompletionResults:
5474      str = "Code completion: cached global results";
5475      break;
5476    case CXTUResourceUsage_SourceManagerContentCache:
5477      str = "SourceManager: content cache allocator";
5478      break;
5479    case CXTUResourceUsage_AST_SideTables:
5480      str = "ASTContext: side tables";
5481      break;
5482    case CXTUResourceUsage_SourceManager_Membuffer_Malloc:
5483      str = "SourceManager: malloc'ed memory buffers";
5484      break;
5485    case CXTUResourceUsage_SourceManager_Membuffer_MMap:
5486      str = "SourceManager: mmap'ed memory buffers";
5487      break;
5488    case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc:
5489      str = "ExternalASTSource: malloc'ed memory buffers";
5490      break;
5491    case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap:
5492      str = "ExternalASTSource: mmap'ed memory buffers";
5493      break;
5494    case CXTUResourceUsage_Preprocessor:
5495      str = "Preprocessor: malloc'ed memory";
5496      break;
5497    case CXTUResourceUsage_PreprocessingRecord:
5498      str = "Preprocessor: PreprocessingRecord";
5499      break;
5500    case CXTUResourceUsage_SourceManager_DataStructures:
5501      str = "SourceManager: data structures and tables";
5502      break;
5503    case CXTUResourceUsage_Preprocessor_HeaderSearch:
5504      str = "Preprocessor: header search tables";
5505      break;
5506  }
5507  return str;
5508}
5509
5510CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) {
5511  if (!TU) {
5512    CXTUResourceUsage usage = { (void*) 0, 0, 0 };
5513    return usage;
5514  }
5515
5516  ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData);
5517  OwningPtr<MemUsageEntries> entries(new MemUsageEntries());
5518  ASTContext &astContext = astUnit->getASTContext();
5519
5520  // How much memory is used by AST nodes and types?
5521  createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST,
5522    (unsigned long) astContext.getASTAllocatedMemory());
5523
5524  // How much memory is used by identifiers?
5525  createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers,
5526    (unsigned long) astContext.Idents.getAllocator().getTotalMemory());
5527
5528  // How much memory is used for selectors?
5529  createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors,
5530    (unsigned long) astContext.Selectors.getTotalMemory());
5531
5532  // How much memory is used by ASTContext's side tables?
5533  createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables,
5534    (unsigned long) astContext.getSideTableAllocatedMemory());
5535
5536  // How much memory is used for caching global code completion results?
5537  unsigned long completionBytes = 0;
5538  if (GlobalCodeCompletionAllocator *completionAllocator =
5539      astUnit->getCachedCompletionAllocator().getPtr()) {
5540    completionBytes = completionAllocator->getTotalMemory();
5541  }
5542  createCXTUResourceUsageEntry(*entries,
5543                               CXTUResourceUsage_GlobalCompletionResults,
5544                               completionBytes);
5545
5546  // How much memory is being used by SourceManager's content cache?
5547  createCXTUResourceUsageEntry(*entries,
5548          CXTUResourceUsage_SourceManagerContentCache,
5549          (unsigned long) astContext.getSourceManager().getContentCacheSize());
5550
5551  // How much memory is being used by the MemoryBuffer's in SourceManager?
5552  const SourceManager::MemoryBufferSizes &srcBufs =
5553    astUnit->getSourceManager().getMemoryBufferSizes();
5554
5555  createCXTUResourceUsageEntry(*entries,
5556                               CXTUResourceUsage_SourceManager_Membuffer_Malloc,
5557                               (unsigned long) srcBufs.malloc_bytes);
5558  createCXTUResourceUsageEntry(*entries,
5559                               CXTUResourceUsage_SourceManager_Membuffer_MMap,
5560                               (unsigned long) srcBufs.mmap_bytes);
5561  createCXTUResourceUsageEntry(*entries,
5562                               CXTUResourceUsage_SourceManager_DataStructures,
5563                               (unsigned long) astContext.getSourceManager()
5564                                .getDataStructureSizes());
5565
5566  // How much memory is being used by the ExternalASTSource?
5567  if (ExternalASTSource *esrc = astContext.getExternalSource()) {
5568    const ExternalASTSource::MemoryBufferSizes &sizes =
5569      esrc->getMemoryBufferSizes();
5570
5571    createCXTUResourceUsageEntry(*entries,
5572      CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc,
5573                                 (unsigned long) sizes.malloc_bytes);
5574    createCXTUResourceUsageEntry(*entries,
5575      CXTUResourceUsage_ExternalASTSource_Membuffer_MMap,
5576                                 (unsigned long) sizes.mmap_bytes);
5577  }
5578
5579  // How much memory is being used by the Preprocessor?
5580  Preprocessor &pp = astUnit->getPreprocessor();
5581  createCXTUResourceUsageEntry(*entries,
5582                               CXTUResourceUsage_Preprocessor,
5583                               pp.getTotalMemory());
5584
5585  if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) {
5586    createCXTUResourceUsageEntry(*entries,
5587                                 CXTUResourceUsage_PreprocessingRecord,
5588                                 pRec->getTotalMemory());
5589  }
5590
5591  createCXTUResourceUsageEntry(*entries,
5592                               CXTUResourceUsage_Preprocessor_HeaderSearch,
5593                               pp.getHeaderSearchInfo().getTotalMemory());
5594
5595  CXTUResourceUsage usage = { (void*) entries.get(),
5596                            (unsigned) entries->size(),
5597                            entries->size() ? &(*entries)[0] : 0 };
5598  entries.take();
5599  return usage;
5600}
5601
5602void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) {
5603  if (usage.data)
5604    delete (MemUsageEntries*) usage.data;
5605}
5606
5607} // end extern "C"
5608
5609void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) {
5610  CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU);
5611  for (unsigned I = 0; I != Usage.numEntries; ++I)
5612    fprintf(stderr, "  %s: %lu\n",
5613            clang_getTUResourceUsageName(Usage.entries[I].kind),
5614            Usage.entries[I].amount);
5615
5616  clang_disposeCXTUResourceUsage(Usage);
5617}
5618
5619//===----------------------------------------------------------------------===//
5620// Misc. utility functions.
5621//===----------------------------------------------------------------------===//
5622
5623/// Default to using an 8 MB stack size on "safety" threads.
5624static unsigned SafetyStackThreadSize = 8 << 20;
5625
5626namespace clang {
5627
5628bool RunSafely(llvm::CrashRecoveryContext &CRC,
5629               void (*Fn)(void*), void *UserData,
5630               unsigned Size) {
5631  if (!Size)
5632    Size = GetSafetyThreadStackSize();
5633  if (Size)
5634    return CRC.RunSafelyOnThread(Fn, UserData, Size);
5635  return CRC.RunSafely(Fn, UserData);
5636}
5637
5638unsigned GetSafetyThreadStackSize() {
5639  return SafetyStackThreadSize;
5640}
5641
5642void SetSafetyThreadStackSize(unsigned Value) {
5643  SafetyStackThreadSize = Value;
5644}
5645
5646}
5647
5648extern "C" {
5649
5650CXString clang_getClangVersion() {
5651  return createCXString(getClangFullVersion());
5652}
5653
5654} // end: extern "C"
5655
5656