aboutsummaryrefslogtreecommitdiff
path: root/lib/Index/GlobalSelector.cpp
blob: 2b2ca6d3b1cce2ada8acad0ecd4c41e34201baf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//===-- GlobalSelector.cpp - Cross-translation-unit "token" for selectors -===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  GlobalSelector is a ASTContext-independent way to refer to selectors.
//
//===----------------------------------------------------------------------===//

#include "clang/Index/GlobalSelector.h"
#include "ProgramImpl.h"
#include "clang/Index/Program.h"
#include "clang/AST/ASTContext.h"
using namespace clang;
using namespace idx;

/// \brief Get the ASTContext-specific selector.
Selector GlobalSelector::getSelector(ASTContext &AST) const {
  if (isInvalid())
    return Selector();

  Selector GlobSel = Selector(reinterpret_cast<uintptr_t>(Val));

  llvm::SmallVector<IdentifierInfo *, 8> Ids;
  for (unsigned i = 0, e = GlobSel.isUnarySelector() ? 1 : GlobSel.getNumArgs();
         i != e; ++i) {
    IdentifierInfo *GlobII = GlobSel.getIdentifierInfoForSlot(i);
    IdentifierInfo *II =
      &AST.Idents.get(GlobII->getNameStart(),
                      GlobII->getNameStart() + GlobII->getLength());
    Ids.push_back(II);
  }

  return AST.Selectors.getSelector(GlobSel.getNumArgs(), Ids.data());
}

/// \brief Get a printable name for debugging purpose.
std::string GlobalSelector::getPrintableName() const {
  if (isInvalid())
    return "<< Invalid >>";

  Selector GlobSel = Selector(reinterpret_cast<uintptr_t>(Val));
  return GlobSel.getAsString();
}

/// \brief Get a GlobalSelector for the ASTContext-specific selector.
GlobalSelector GlobalSelector::get(Selector Sel, Program &Prog) {
  if (Sel.isNull())
    return GlobalSelector();

  ProgramImpl &ProgImpl = *static_cast<ProgramImpl*>(Prog.Impl);

  llvm::SmallVector<IdentifierInfo *, 8> Ids;
  for (unsigned i = 0, e = Sel.isUnarySelector() ? 1 : Sel.getNumArgs();
         i != e; ++i) {
    IdentifierInfo *II = Sel.getIdentifierInfoForSlot(i);
    IdentifierInfo *GlobII =
      &ProgImpl.getIdents().get(II->getNameStart(),
                                II->getNameStart() + II->getLength());
    Ids.push_back(GlobII);
  }

  Selector GlobSel = ProgImpl.getSelectors().getSelector(Sel.getNumArgs(),
                                                         Ids.data());
  return GlobalSelector(GlobSel.getAsOpaquePtr());
}

unsigned
llvm::DenseMapInfo<GlobalSelector>::getHashValue(GlobalSelector Sel) {
  return DenseMapInfo<void*>::getHashValue(Sel.getAsOpaquePtr());
}