blob: 325b3711e8049d5854a10c772332fdbd83da364e [file] [log] [blame]
Argyrios Kyrtzidis557b1d22009-07-29 23:40:58 +00001//===- 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#include "llvm/Support/Compiler.h"
18using namespace clang;
19using namespace idx;
20
21namespace {
22
23class VISIBILITY_HIDDEN SelMapper : public ASTVisitor<SelMapper> {
24 SelectorMap::SelMethMapTy &SelMethMap;
25 SelectorMap::SelRefMapTy &SelRefMap;
26
27public:
28 SelMapper(SelectorMap::SelMethMapTy &MethMap,
29 SelectorMap::SelRefMapTy &RefMap)
30 : SelMethMap(MethMap), SelRefMap(RefMap) { }
31
32 void VisitObjCMethodDecl(ObjCMethodDecl *D);
33 void VisitObjCMessageExpr(ObjCMessageExpr *Node);
34 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
35};
36
37} // anonymous namespace
38
39//===----------------------------------------------------------------------===//
40// SelMapper Implementation
41//===----------------------------------------------------------------------===//
42
43void SelMapper::VisitObjCMethodDecl(ObjCMethodDecl *D) {
44 if (D->getCanonicalDecl() == D)
45 SelMethMap.insert(std::make_pair(D->getSelector(), D));
46 Base::VisitObjCMethodDecl(D);
47}
48
49void SelMapper::VisitObjCMessageExpr(ObjCMessageExpr *Node) {
50 ASTLocation ASTLoc(CurrentDecl, Node);
51 SelRefMap.insert(std::make_pair(Node->getSelector(), ASTLoc));
52}
53
54void SelMapper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
55 ASTLocation ASTLoc(CurrentDecl, Node);
56 SelRefMap.insert(std::make_pair(Node->getSelector(), ASTLoc));
57}
58
59//===----------------------------------------------------------------------===//
60// SelectorMap Implementation
61//===----------------------------------------------------------------------===//
62
63SelectorMap::SelectorMap(ASTContext &Ctx) {
64 SelMapper(SelMethMap, SelRefMap).Visit(Ctx.getTranslationUnitDecl());
65}
66
67SelectorMap::method_iterator
68SelectorMap::methods_begin(Selector Sel) const {
69 return method_iterator(SelMethMap.lower_bound(Sel));
70}
71
72SelectorMap::method_iterator
73SelectorMap::methods_end(Selector Sel) const {
74 return method_iterator(SelMethMap.upper_bound(Sel));
75}
76
77SelectorMap::astlocation_iterator
78SelectorMap::refs_begin(Selector Sel) const {
79 return astlocation_iterator(SelRefMap.lower_bound(Sel));
80}
81
82SelectorMap::astlocation_iterator
83SelectorMap::refs_end(Selector Sel) const {
84 return astlocation_iterator(SelRefMap.upper_bound(Sel));
85}