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 | using namespace clang; |
Argyrios Kyrtzidis | ccbcb70 | 2009-07-06 21:34:47 +0000 | [diff] [blame] | 19 | using namespace idx; |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 23 | class RefMapper : public ASTVisitor<RefMapper> { |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 24 | DeclReferenceMap::MapTy ⤅ |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 25 | |
| 26 | public: |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 27 | RefMapper(DeclReferenceMap::MapTy &map) : Map(map) { } |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 28 | |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 29 | void VisitDeclRefExpr(DeclRefExpr *Node); |
Argyrios Kyrtzidis | 9e6bc06 | 2009-07-14 03:18:09 +0000 | [diff] [blame] | 30 | void VisitMemberExpr(MemberExpr *Node); |
Argyrios Kyrtzidis | 80ede1d | 2009-07-21 00:05:38 +0000 | [diff] [blame] | 31 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node); |
Argyrios Kyrtzidis | b116884 | 2009-09-29 21:26:53 +0000 | [diff] [blame] | 32 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 33 | void VisitTypedefTypeLoc(TypedefTypeLoc TL); |
| 34 | void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | } // anonymous namespace |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 40 | // RefMapper Implementation |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 43 | void RefMapper::VisitDeclRefExpr(DeclRefExpr *Node) { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 44 | NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getCanonicalDecl()); |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 45 | Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node))); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 48 | void RefMapper::VisitMemberExpr(MemberExpr *Node) { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 49 | NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getCanonicalDecl()); |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 50 | Map.insert(std::make_pair(PrimD, ASTLocation(CurrentDecl, Node))); |
Argyrios Kyrtzidis | 9e6bc06 | 2009-07-14 03:18:09 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 53 | void RefMapper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
| 54 | Map.insert(std::make_pair(Node->getDecl(), ASTLocation(CurrentDecl, Node))); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 55 | } |
| 56 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 57 | void RefMapper::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 58 | NamedDecl *ND = TL.getTypedefNameDecl(); |
Argyrios Kyrtzidis | b116884 | 2009-09-29 21:26:53 +0000 | [diff] [blame] | 59 | Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc()))); |
| 60 | } |
| 61 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 62 | void RefMapper::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Argyrios Kyrtzidis | b116884 | 2009-09-29 21:26:53 +0000 | [diff] [blame] | 63 | NamedDecl *ND = TL.getIFaceDecl(); |
| 64 | Map.insert(std::make_pair(ND, ASTLocation(CurrentDecl, ND, TL.getNameLoc()))); |
| 65 | } |
| 66 | |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // DeclReferenceMap Implementation |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) { |
Argyrios Kyrtzidis | 81e8456 | 2009-07-29 23:40:39 +0000 | [diff] [blame] | 72 | RefMapper(Map).Visit(Ctx.getTranslationUnitDecl()); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 75 | DeclReferenceMap::astlocation_iterator |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 76 | DeclReferenceMap::refs_begin(NamedDecl *D) const { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 77 | NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | return astlocation_iterator(Map.lower_bound(Prim)); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Argyrios Kyrtzidis | 874012b | 2009-07-06 21:34:20 +0000 | [diff] [blame] | 81 | DeclReferenceMap::astlocation_iterator |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 82 | DeclReferenceMap::refs_end(NamedDecl *D) const { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 83 | NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | return astlocation_iterator(Map.upper_bound(Prim)); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | bool DeclReferenceMap::refs_empty(NamedDecl *D) const { |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 88 | NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | return refs_begin(Prim) == refs_end(Prim); |
Argyrios Kyrtzidis | 2c2ba3e | 2009-07-05 22:22:06 +0000 | [diff] [blame] | 90 | } |