Argyrios Kyrtzidis | 27bd0dc | 2009-07-29 23:40:32 +0000 | [diff] [blame] | 1 | //===-- 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" |
| 18 | using namespace clang; |
| 19 | using namespace idx; |
| 20 | |
| 21 | /// \brief Get the ASTContext-specific selector. |
| 22 | Selector 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); |
| 32 | IdentifierInfo *II = &AST.Idents.get(GlobII->getName(), |
| 33 | GlobII->getName() + GlobII->getLength()); |
| 34 | Ids.push_back(II); |
| 35 | } |
| 36 | |
Argyrios Kyrtzidis | ab5f311 | 2009-07-30 03:42:08 +0000 | [diff] [blame] | 37 | return AST.Selectors.getSelector(GlobSel.getNumArgs(), Ids.data()); |
Argyrios Kyrtzidis | 27bd0dc | 2009-07-29 23:40:32 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /// \brief Get a printable name for debugging purpose. |
| 41 | std::string GlobalSelector::getPrintableName() const { |
| 42 | if (isInvalid()) |
| 43 | return "<< Invalid >>"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Argyrios Kyrtzidis | 27bd0dc | 2009-07-29 23:40:32 +0000 | [diff] [blame] | 45 | Selector GlobSel = Selector(reinterpret_cast<uintptr_t>(Val)); |
| 46 | return GlobSel.getAsString(); |
| 47 | } |
| 48 | |
| 49 | /// \brief Get a GlobalSelector for the ASTContext-specific selector. |
| 50 | GlobalSelector GlobalSelector::get(Selector Sel, Program &Prog) { |
| 51 | if (Sel.isNull()) |
| 52 | return GlobalSelector(); |
| 53 | |
| 54 | ProgramImpl &ProgImpl = *static_cast<ProgramImpl*>(Prog.Impl); |
| 55 | |
| 56 | llvm::SmallVector<IdentifierInfo *, 8> Ids; |
| 57 | for (unsigned i = 0, e = Sel.isUnarySelector() ? 1 : Sel.getNumArgs(); |
| 58 | i != e; ++i) { |
| 59 | IdentifierInfo *II = Sel.getIdentifierInfoForSlot(i); |
| 60 | IdentifierInfo *GlobII = &ProgImpl.getIdents().get(II->getName(), |
| 61 | II->getName() + II->getLength()); |
| 62 | Ids.push_back(GlobII); |
| 63 | } |
| 64 | |
Argyrios Kyrtzidis | ab5f311 | 2009-07-30 03:42:08 +0000 | [diff] [blame] | 65 | Selector GlobSel = ProgImpl.getSelectors().getSelector(Sel.getNumArgs(), |
| 66 | Ids.data()); |
Argyrios Kyrtzidis | 27bd0dc | 2009-07-29 23:40:32 +0000 | [diff] [blame] | 67 | return GlobalSelector(GlobSel.getAsOpaquePtr()); |
| 68 | } |
| 69 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | unsigned |
Argyrios Kyrtzidis | 27bd0dc | 2009-07-29 23:40:32 +0000 | [diff] [blame] | 71 | llvm::DenseMapInfo<GlobalSelector>::getHashValue(GlobalSelector Sel) { |
| 72 | return DenseMapInfo<void*>::getHashValue(Sel.getAsOpaquePtr()); |
| 73 | } |