Chris Lattner | c3a6540 | 2009-07-12 22:33:12 +0000 | [diff] [blame] | 1 | //===--- DeclReferenceMap.cpp - Map Decls to their references -------------===// |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 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 | // |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 10 | // DeclReferenceMap creates a mapping from Decls to the ASTLocations that |
| 11 | // reference them. |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 15 | #include "clang/Index/DeclReferenceMap.h" |
| 16 | #include "clang/Index/ASTLocation.h" |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 17 | #include "ASTVisitor.h" |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compiler.h" |
| 19 | using namespace clang; |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 20 | using namespace idx; |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 24 | class VISIBILITY_HIDDEN RefMapper : public ASTVisitor<RefMapper> { |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 25 | DeclReferenceMap::MapTy ⤅ |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 26 | |
| 27 | public: |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 28 | RefMapper(DeclReferenceMap::MapTy &map) : Map(map) { } |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 29 | |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 30 | void VisitDeclRefExpr(DeclRefExpr *Node); |
Argyrios Kyrtzidis | 9e6bc06 | 2009-07-14 03:18:09 +0000 | [diff] [blame] | 31 | void VisitMemberExpr(MemberExpr *Node); |
Argyrios Kyrtzidis | 80ede1d | 2009-07-21 00:05:38 +0000 | [diff] [blame] | 32 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | } // anonymous namespace |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 38 | // RefMapper Implementation |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 41 | void RefMapper::VisitDeclRefExpr(DeclRefExpr *Node) { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 42 | NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getCanonicalDecl()); |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 43 | Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node))); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 46 | void RefMapper::VisitMemberExpr(MemberExpr *Node) { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 47 | NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getCanonicalDecl()); |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 48 | Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node))); |
Argyrios Kyrtzidis | 9e6bc06 | 2009-07-14 03:18:09 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 51 | void RefMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
| 52 | Map.insert(std::make_pair(Node->getDecl(), ASTLocation(CurrentDecl, Node))); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | // DeclReferenceMap Implementation |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
| 59 | DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) { |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 60 | RefMapper(Map).Visit(Ctx.getTranslationUnitDecl()); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 63 | DeclReferenceMap::astlocation_iterator |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 64 | DeclReferenceMap::refs_begin(NamedDecl *D) const { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 65 | NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 66 | return astlocation_iterator(Map.lower_bound(Prim)); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 69 | DeclReferenceMap::astlocation_iterator |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 70 | DeclReferenceMap::refs_end(NamedDecl *D) const { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 71 | NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 72 | return astlocation_iterator(Map.upper_bound(Prim)); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool DeclReferenceMap::refs_empty(NamedDecl *D) const { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 76 | NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 77 | return refs_begin(Prim) == refs_end(Prim); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 78 | } |