blob: 2b2ca6d3b1cce2ada8acad0ecd4c41e34201baf2 [file] [log] [blame]
Argyrios Kyrtzidis27bd0dc2009-07-29 23:40:32 +00001//===-- GlobalSelector.cpp - Cross-translation-unit "token" for selectors -===//
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// GlobalSelector is a ASTContext-independent way to refer to selectors.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Index/GlobalSelector.h"
15#include "ProgramImpl.h"
16#include "clang/Index/Program.h"
17#include "clang/AST/ASTContext.h"
18using namespace clang;
19using namespace idx;
20
21/// \brief Get the ASTContext-specific selector.
22Selector GlobalSelector::getSelector(ASTContext &AST) const {
23 if (isInvalid())
24 return Selector();
25
26 Selector GlobSel = Selector(reinterpret_cast<uintptr_t>(Val));
27
28 llvm::SmallVector<IdentifierInfo *, 8> Ids;
29 for (unsigned i = 0, e = GlobSel.isUnarySelector() ? 1 : GlobSel.getNumArgs();
30 i != e; ++i) {
31 IdentifierInfo *GlobII = GlobSel.getIdentifierInfoForSlot(i);
Daniel Dunbare013d682009-10-18 20:26:12 +000032 IdentifierInfo *II =
33 &AST.Idents.get(GlobII->getNameStart(),
34 GlobII->getNameStart() + GlobII->getLength());
Argyrios Kyrtzidis27bd0dc2009-07-29 23:40:32 +000035 Ids.push_back(II);
36 }
37
Argyrios Kyrtzidisab5f3112009-07-30 03:42:08 +000038 return AST.Selectors.getSelector(GlobSel.getNumArgs(), Ids.data());
Argyrios Kyrtzidis27bd0dc2009-07-29 23:40:32 +000039}
40
41/// \brief Get a printable name for debugging purpose.
42std::string GlobalSelector::getPrintableName() const {
43 if (isInvalid())
44 return "<< Invalid >>";
Mike Stump1eb44332009-09-09 15:08:12 +000045
Argyrios Kyrtzidis27bd0dc2009-07-29 23:40:32 +000046 Selector GlobSel = Selector(reinterpret_cast<uintptr_t>(Val));
47 return GlobSel.getAsString();
48}
49
50/// \brief Get a GlobalSelector for the ASTContext-specific selector.
51GlobalSelector GlobalSelector::get(Selector Sel, Program &Prog) {
52 if (Sel.isNull())
53 return GlobalSelector();
54
55 ProgramImpl &ProgImpl = *static_cast<ProgramImpl*>(Prog.Impl);
56
57 llvm::SmallVector<IdentifierInfo *, 8> Ids;
58 for (unsigned i = 0, e = Sel.isUnarySelector() ? 1 : Sel.getNumArgs();
59 i != e; ++i) {
60 IdentifierInfo *II = Sel.getIdentifierInfoForSlot(i);
Daniel Dunbare013d682009-10-18 20:26:12 +000061 IdentifierInfo *GlobII =
62 &ProgImpl.getIdents().get(II->getNameStart(),
63 II->getNameStart() + II->getLength());
Argyrios Kyrtzidis27bd0dc2009-07-29 23:40:32 +000064 Ids.push_back(GlobII);
65 }
66
Argyrios Kyrtzidisab5f3112009-07-30 03:42:08 +000067 Selector GlobSel = ProgImpl.getSelectors().getSelector(Sel.getNumArgs(),
68 Ids.data());
Argyrios Kyrtzidis27bd0dc2009-07-29 23:40:32 +000069 return GlobalSelector(GlobSel.getAsOpaquePtr());
70}
71
Mike Stump1eb44332009-09-09 15:08:12 +000072unsigned
Argyrios Kyrtzidis27bd0dc2009-07-29 23:40:32 +000073llvm::DenseMapInfo<GlobalSelector>::getHashValue(GlobalSelector Sel) {
74 return DenseMapInfo<void*>::getHashValue(Sel.getAsOpaquePtr());
75}