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