[cfe-commits] r77538 - in /cfe/trunk: include/clang/Index/GlobalSelector.h include/clang/Index/Program.h lib/Index/GlobalSelector.cpp lib/Index/ProgramImpl.h

Argiris Kirtzidis akyrtzi at gmail.com
Wed Jul 29 16:40:32 PDT 2009


Author: akirtzidis
Date: Wed Jul 29 18:40:32 2009
New Revision: 77538

URL: http://llvm.org/viewvc/llvm-project?rev=77538&view=rev
Log:
Introduce the GlobalSelector class in the Indexing library.

GlobalSelector is an ASTContext-independent way to refer to Objective C selectors.

Added:
    cfe/trunk/include/clang/Index/GlobalSelector.h
    cfe/trunk/lib/Index/GlobalSelector.cpp
Modified:
    cfe/trunk/include/clang/Index/Program.h
    cfe/trunk/lib/Index/ProgramImpl.h

Added: cfe/trunk/include/clang/Index/GlobalSelector.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/GlobalSelector.h?rev=77538&view=auto

==============================================================================
--- cfe/trunk/include/clang/Index/GlobalSelector.h (added)
+++ cfe/trunk/include/clang/Index/GlobalSelector.h Wed Jul 29 18:40:32 2009
@@ -0,0 +1,95 @@
+//===--- GlobalSelector.h - 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.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_INDEX_GLOBALSELECTOR_H
+#define LLVM_CLANG_INDEX_GLOBALSELECTOR_H
+
+#include "llvm/ADT/DenseMap.h"
+#include <string>
+
+namespace clang {
+  class ASTContext;
+  class Selector;
+
+namespace idx {
+  class Program;
+
+/// \brief A ASTContext-independent way to refer to selectors.
+class GlobalSelector {
+  void *Val;
+
+  explicit GlobalSelector(void *val) : Val(val) { }
+
+public:
+  GlobalSelector() : Val(0) { }
+
+  /// \brief Get the ASTContext-specific selector.
+  Selector getSelector(ASTContext &AST) const;
+
+  bool isValid() const { return Val != 0; }
+  bool isInvalid() const { return !isValid(); }
+
+  /// \brief Get a printable name for debugging purpose.
+  std::string getPrintableName() const;
+
+  /// \brief Get a GlobalSelector for the ASTContext-specific selector.
+  static GlobalSelector get(Selector Sel, Program &Prog);
+  
+  void *getAsOpaquePtr() const { return Val; }
+
+  friend bool operator==(const GlobalSelector &LHS, const GlobalSelector &RHS) { 
+    return LHS.getAsOpaquePtr() == RHS.getAsOpaquePtr();
+  }
+  
+  // For use in a std::map.
+  friend bool operator< (const GlobalSelector &LHS, const GlobalSelector &RHS) { 
+    return LHS.getAsOpaquePtr() < RHS.getAsOpaquePtr();
+  }
+
+  // For use in DenseMap/DenseSet.
+  static GlobalSelector getEmptyMarker() { return GlobalSelector((void*)-1); }
+  static GlobalSelector getTombstoneMarker() {
+    return GlobalSelector((void*)-2);
+  }
+};
+  
+} // namespace idx
+
+} // namespace clang
+
+namespace llvm {
+/// Define DenseMapInfo so that GlobalSelectors can be used as keys in DenseMap
+/// and DenseSets.
+template<>
+struct DenseMapInfo<clang::idx::GlobalSelector> {
+  static inline clang::idx::GlobalSelector getEmptyKey() {
+    return clang::idx::GlobalSelector::getEmptyMarker();
+  }
+
+  static inline clang::idx::GlobalSelector getTombstoneKey() {
+    return clang::idx::GlobalSelector::getTombstoneMarker();
+  }
+
+  static unsigned getHashValue(clang::idx::GlobalSelector);
+
+  static inline bool 
+  isEqual(clang::idx::GlobalSelector LHS, clang::idx::GlobalSelector RHS) {
+    return LHS == RHS;
+  }
+
+  static inline bool isPod() { return true; }
+};
+
+}  // end namespace llvm
+
+#endif

Modified: cfe/trunk/include/clang/Index/Program.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/Program.h?rev=77538&r1=77537&r2=77538&view=diff

==============================================================================
--- cfe/trunk/include/clang/Index/Program.h (original)
+++ cfe/trunk/include/clang/Index/Program.h Wed Jul 29 18:40:32 2009
@@ -28,6 +28,7 @@
   Program(const Program&); // do not implement
   Program &operator=(const Program &); // do not implement
   friend class Entity;
+  friend class GlobalSelector;
   
 public:
   Program();

Added: cfe/trunk/lib/Index/GlobalSelector.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/GlobalSelector.cpp?rev=77538&view=auto

==============================================================================
--- cfe/trunk/lib/Index/GlobalSelector.cpp (added)
+++ cfe/trunk/lib/Index/GlobalSelector.cpp Wed Jul 29 18:40:32 2009
@@ -0,0 +1,72 @@
+//===-- 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->getName(),
+                                       GlobII->getName() + GlobII->getLength());
+    Ids.push_back(II);
+  }
+
+  return AST.Selectors.getSelector(Ids.size(), 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->getName(),
+                                               II->getName() + II->getLength());
+    Ids.push_back(GlobII);
+  }
+
+  Selector GlobSel = ProgImpl.getSelectors().getSelector(Ids.size(),Ids.data());
+  return GlobalSelector(GlobSel.getAsOpaquePtr());
+}
+
+unsigned 
+llvm::DenseMapInfo<GlobalSelector>::getHashValue(GlobalSelector Sel) {
+  return DenseMapInfo<void*>::getHashValue(Sel.getAsOpaquePtr());
+}

Modified: cfe/trunk/lib/Index/ProgramImpl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/ProgramImpl.h?rev=77538&r1=77537&r2=77538&view=diff

==============================================================================
--- cfe/trunk/lib/Index/ProgramImpl.h (original)
+++ cfe/trunk/lib/Index/ProgramImpl.h Wed Jul 29 18:40:32 2009
@@ -32,6 +32,7 @@
   llvm::BumpPtrAllocator BumpAlloc;
   
   IdentifierTable Identifiers;
+  SelectorTable Selectors;
 
   ProgramImpl(const ProgramImpl&); // do not implement
   ProgramImpl &operator=(const ProgramImpl &); // do not implement
@@ -41,6 +42,7 @@
   
   EntitySetTy &getEntities() { return Entities; }
   IdentifierTable &getIdents() { return Identifiers; }
+  SelectorTable &getSelectors() { return Selectors; }
 
   void *Allocate(unsigned Size, unsigned Align = 8) {
     return BumpAlloc.Allocate(Size, Align);





More information about the cfe-commits mailing list