1 //===- SelectorMap.cpp - Maps selectors to methods and messages -*- C++ -*-===// 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 // SelectorMap creates a mapping from selectors to ObjC method declarations 11 // and ObjC message expressions. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/Index/SelectorMap.h" 16 #include "ASTVisitor.h" 17 using namespace clang; 18 using namespace idx; 19 20 namespace { 21 22 class SelMapper : public ASTVisitor<SelMapper> { 23 SelectorMap::SelMethMapTy &SelMethMap; 24 SelectorMap::SelRefMapTy &SelRefMap; 25 26 public: 27 SelMapper(SelectorMap::SelMethMapTy &MethMap, 28 SelectorMap::SelRefMapTy &RefMap) 29 : SelMethMap(MethMap), SelRefMap(RefMap) { } 30 31 void VisitObjCMethodDecl(ObjCMethodDecl *D); 32 void VisitObjCMessageExpr(ObjCMessageExpr *Node); 33 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node); 34 }; 35 36 } // anonymous namespace 37 38 //===----------------------------------------------------------------------===// 39 // SelMapper Implementation 40 //===----------------------------------------------------------------------===// 41 42 void SelMapper::VisitObjCMethodDecl(ObjCMethodDecl *D) { 43 if (D->getCanonicalDecl() == D) 44 SelMethMap.insert(std::make_pair(D->getSelector(), D)); 45 Base::VisitObjCMethodDecl(D); 46 } 47 48 void SelMapper::VisitObjCMessageExpr(ObjCMessageExpr *Node) { 49 ASTLocation ASTLoc(CurrentDecl, Node); 50 SelRefMap.insert(std::make_pair(Node->getSelector(), ASTLoc)); 51 } 52 53 void SelMapper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) { 54 ASTLocation ASTLoc(CurrentDecl, Node); 55 SelRefMap.insert(std::make_pair(Node->getSelector(), ASTLoc)); 56 } 57 58 //===----------------------------------------------------------------------===// 59 // SelectorMap Implementation 60 //===----------------------------------------------------------------------===// 61 62 SelectorMap::SelectorMap(ASTContext &Ctx) { 63 SelMapper(SelMethMap, SelRefMap).Visit(Ctx.getTranslationUnitDecl()); 64 } 65 66 SelectorMap::method_iterator 67 SelectorMap::methods_begin(Selector Sel) const { 68 return method_iterator(SelMethMap.lower_bound(Sel)); 69 } 70 71 SelectorMap::method_iterator 72 SelectorMap::methods_end(Selector Sel) const { 73 return method_iterator(SelMethMap.upper_bound(Sel)); 74 } 75 76 SelectorMap::astlocation_iterator 77 SelectorMap::refs_begin(Selector Sel) const { 78 return astlocation_iterator(SelRefMap.lower_bound(Sel)); 79 } 80 81 SelectorMap::astlocation_iterator 82 SelectorMap::refs_end(Selector Sel) const { 83 return astlocation_iterator(SelRefMap.upper_bound(Sel)); 84 } 85