1 //===--- DeclReferenceMap.cpp - Map Decls to their references -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // DeclReferenceMap creates a mapping from Decls to the ASTLocations that
13 //===----------------------------------------------------------------------===//
15 #include "clang/Index/DeclReferenceMap.h"
16 #include "clang/Index/ASTLocation.h"
17 #include "ASTVisitor.h"
18 using namespace clang
;
23 class RefMapper
: public ASTVisitor
<RefMapper
> {
24 DeclReferenceMap::MapTy
&Map
;
27 RefMapper(DeclReferenceMap::MapTy
&map
) : Map(map
) { }
29 void VisitDeclRefExpr(DeclRefExpr
*Node
);
30 void VisitMemberExpr(MemberExpr
*Node
);
31 void VisitObjCIvarRefExpr(ObjCIvarRefExpr
*Node
);
33 void VisitTypedefTypeLoc(TypedefTypeLoc TL
);
34 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL
);
37 } // anonymous namespace
39 //===----------------------------------------------------------------------===//
40 // RefMapper Implementation
41 //===----------------------------------------------------------------------===//
43 void RefMapper::VisitDeclRefExpr(DeclRefExpr
*Node
) {
44 NamedDecl
*PrimD
= cast
<NamedDecl
>(Node
->getDecl()->getCanonicalDecl());
45 Map
.insert(std::make_pair(PrimD
, ASTLocation(CurrentDecl
, Node
)));
48 void RefMapper::VisitMemberExpr(MemberExpr
*Node
) {
49 NamedDecl
*PrimD
= cast
<NamedDecl
>(Node
->getMemberDecl()->getCanonicalDecl());
50 Map
.insert(std::make_pair(PrimD
, ASTLocation(CurrentDecl
, Node
)));
53 void RefMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr
*Node
) {
54 Map
.insert(std::make_pair(Node
->getDecl(), ASTLocation(CurrentDecl
, Node
)));
57 void RefMapper::VisitTypedefTypeLoc(TypedefTypeLoc TL
) {
58 NamedDecl
*ND
= TL
.getTypedefDecl();
59 Map
.insert(std::make_pair(ND
, ASTLocation(CurrentDecl
, ND
, TL
.getNameLoc())));
62 void RefMapper::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL
) {
63 NamedDecl
*ND
= TL
.getIFaceDecl();
64 Map
.insert(std::make_pair(ND
, ASTLocation(CurrentDecl
, ND
, TL
.getNameLoc())));
67 //===----------------------------------------------------------------------===//
68 // DeclReferenceMap Implementation
69 //===----------------------------------------------------------------------===//
71 DeclReferenceMap::DeclReferenceMap(ASTContext
&Ctx
) {
72 RefMapper(Map
).Visit(Ctx
.getTranslationUnitDecl());
75 DeclReferenceMap::astlocation_iterator
76 DeclReferenceMap::refs_begin(NamedDecl
*D
) const {
77 NamedDecl
*Prim
= cast
<NamedDecl
>(D
->getCanonicalDecl());
78 return astlocation_iterator(Map
.lower_bound(Prim
));
81 DeclReferenceMap::astlocation_iterator
82 DeclReferenceMap::refs_end(NamedDecl
*D
) const {
83 NamedDecl
*Prim
= cast
<NamedDecl
>(D
->getCanonicalDecl());
84 return astlocation_iterator(Map
.upper_bound(Prim
));
87 bool DeclReferenceMap::refs_empty(NamedDecl
*D
) const {
88 NamedDecl
*Prim
= cast
<NamedDecl
>(D
->getCanonicalDecl());
89 return refs_begin(Prim
) == refs_end(Prim
);