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