[cfe-commits] r77541 - in /cfe/trunk: include/clang/Index/SelectorMap.h lib/Index/SelectorMap.cpp

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


Author: akirtzidis
Date: Wed Jul 29 18:40:58 2009
New Revision: 77541

URL: http://llvm.org/viewvc/llvm-project?rev=77541&view=rev
Log:
Introduce SelectorMap whose purpose is to map selectors to objc methods and message exprs,
inside a particular ASTContext.

Added:
    cfe/trunk/include/clang/Index/SelectorMap.h
    cfe/trunk/lib/Index/SelectorMap.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Index/SelectorMap.h (added)
+++ cfe/trunk/include/clang/Index/SelectorMap.h Wed Jul 29 18:40:58 2009
@@ -0,0 +1,95 @@
+//===--- SelectorMap.h - Maps selectors to methods and messages -*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  SelectorMap creates a mapping from selectors to ObjC method declarations
+//  and ObjC message expressions.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_INDEX_SELECTORMAP_H
+#define LLVM_CLANG_INDEX_SELECTORMAP_H
+
+#include "clang/Index/ASTLocation.h"
+#include "clang/Basic/IdentifierTable.h"
+#include <map>
+
+namespace clang {
+  class ASTContext;
+  class ObjCMethodDecl;
+
+namespace idx {
+  
+/// \brief Maps NamedDecls with the ASTLocations that reference them.
+///
+/// References are mapped and retrieved using the canonical decls.
+class SelectorMap {
+
+  template <typename iter_type>
+  class wrap_pair_iterator {
+    iter_type I;
+
+    wrap_pair_iterator(iter_type i) : I(i) { }
+    friend class SelectorMap;
+
+  public:
+    typedef typename iter_type::value_type::second_type value_type;
+    typedef value_type& reference;
+    typedef value_type* pointer;
+    typedef typename iter_type::iterator_category iterator_category;
+    typedef typename iter_type::difference_type   difference_type;
+
+    wrap_pair_iterator() { }
+
+    reference operator*() const { return I->second; }
+    pointer operator->() const { return &I->second; }
+
+    wrap_pair_iterator& operator++() {
+      ++I;
+      return *this;
+    }
+
+    wrap_pair_iterator operator++(int) {
+      wrap_pair_iterator tmp(*this);
+      ++(*this);
+      return tmp;
+    }
+
+    friend bool operator==(wrap_pair_iterator L, wrap_pair_iterator R) { 
+      return L.I == R.I;
+    }
+    friend bool operator!=(wrap_pair_iterator L, wrap_pair_iterator R) { 
+      return L.I != R.I;
+    }
+  };
+
+public:
+  explicit SelectorMap(ASTContext &Ctx);
+  
+  typedef std::multimap<Selector, ObjCMethodDecl *> SelMethMapTy;
+  typedef std::multimap<Selector, ASTLocation> SelRefMapTy;
+
+  typedef wrap_pair_iterator<SelMethMapTy::iterator> method_iterator;
+  typedef wrap_pair_iterator<SelRefMapTy::iterator> astlocation_iterator;
+
+  method_iterator methods_begin(Selector Sel) const;
+  method_iterator methods_end(Selector Sel) const;
+
+  astlocation_iterator refs_begin(Selector Sel) const;
+  astlocation_iterator refs_end(Selector Sel) const;
+  
+private:
+  mutable SelMethMapTy SelMethMap;
+  mutable SelRefMapTy SelRefMap;
+};
+
+} // end idx namespace
+  
+} // end clang namespace
+
+#endif

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

==============================================================================
--- cfe/trunk/lib/Index/SelectorMap.cpp (added)
+++ cfe/trunk/lib/Index/SelectorMap.cpp Wed Jul 29 18:40:58 2009
@@ -0,0 +1,85 @@
+//===- SelectorMap.cpp - Maps selectors to methods and messages -*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  SelectorMap creates a mapping from selectors to ObjC method declarations
+//  and ObjC message expressions.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Index/SelectorMap.h"
+#include "ASTVisitor.h"
+#include "llvm/Support/Compiler.h"
+using namespace clang;
+using namespace idx;
+
+namespace {
+
+class VISIBILITY_HIDDEN SelMapper : public ASTVisitor<SelMapper> {
+  SelectorMap::SelMethMapTy &SelMethMap;
+  SelectorMap::SelRefMapTy &SelRefMap;
+
+public:
+  SelMapper(SelectorMap::SelMethMapTy &MethMap,
+            SelectorMap::SelRefMapTy &RefMap)
+    : SelMethMap(MethMap), SelRefMap(RefMap) { }
+
+  void VisitObjCMethodDecl(ObjCMethodDecl *D);
+  void VisitObjCMessageExpr(ObjCMessageExpr *Node);
+  void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
+};
+
+} // anonymous namespace
+
+//===----------------------------------------------------------------------===//
+// SelMapper Implementation
+//===----------------------------------------------------------------------===//
+
+void SelMapper::VisitObjCMethodDecl(ObjCMethodDecl *D) {
+  if (D->getCanonicalDecl() == D)
+    SelMethMap.insert(std::make_pair(D->getSelector(), D));
+  Base::VisitObjCMethodDecl(D);
+}
+
+void SelMapper::VisitObjCMessageExpr(ObjCMessageExpr *Node) {
+  ASTLocation ASTLoc(CurrentDecl, Node);
+  SelRefMap.insert(std::make_pair(Node->getSelector(), ASTLoc));
+}
+
+void SelMapper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
+  ASTLocation ASTLoc(CurrentDecl, Node);
+  SelRefMap.insert(std::make_pair(Node->getSelector(), ASTLoc));
+}
+
+//===----------------------------------------------------------------------===//
+// SelectorMap Implementation
+//===----------------------------------------------------------------------===//
+
+SelectorMap::SelectorMap(ASTContext &Ctx) {
+  SelMapper(SelMethMap, SelRefMap).Visit(Ctx.getTranslationUnitDecl());
+}
+
+SelectorMap::method_iterator
+SelectorMap::methods_begin(Selector Sel) const {
+  return method_iterator(SelMethMap.lower_bound(Sel));
+}
+
+SelectorMap::method_iterator
+SelectorMap::methods_end(Selector Sel) const {
+  return method_iterator(SelMethMap.upper_bound(Sel));
+}
+
+SelectorMap::astlocation_iterator
+SelectorMap::refs_begin(Selector Sel) const {
+  return astlocation_iterator(SelRefMap.lower_bound(Sel));
+}
+
+SelectorMap::astlocation_iterator
+SelectorMap::refs_end(Selector Sel) const {
+  return astlocation_iterator(SelRefMap.upper_bound(Sel));
+}





More information about the cfe-commits mailing list