1 //===-- DeclFriend.h - Classes for C++ friend declarations -*- 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 // This file defines the section of the AST representing C++ friend 11 // declarations. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_AST_DECLFRIEND_H 16 #define LLVM_CLANG_AST_DECLFRIEND_H 17 18 #include "clang/AST/DeclCXX.h" 19 20 namespace clang { 21 22 /// FriendDecl - Represents the declaration of a friend entity, 23 /// which can be a function, a type, or a templated function or type. 24 // For example: 25 /// 26 /// @code 27 /// template <typename T> class A { 28 /// friend int foo(T); 29 /// friend class B; 30 /// friend T; // only in C++0x 31 /// template <typename U> friend class C; 32 /// template <typename U> friend A& operator+=(A&, const U&) { ... } 33 /// }; 34 /// @endcode 35 /// 36 /// The semantic context of a friend decl is its declaring class. 37 class FriendDecl : public Decl { 38 public: 39 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion; 40 41 private: 42 // The declaration that's a friend of this class. 43 FriendUnion Friend; 44 45 // A pointer to the next friend in the sequence. 46 LazyDeclPtr NextFriend; 47 48 // Location of the 'friend' specifier. 49 SourceLocation FriendLoc; 50 51 /// True if this 'friend' declaration is unsupported. Eventually we 52 /// will support every possible friend declaration, but for now we 53 /// silently ignore some and set this flag to authorize all access. 54 bool UnsupportedFriend; 55 56 friend class CXXRecordDecl::friend_iterator; 57 friend class CXXRecordDecl; 58 59 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend, 60 SourceLocation FriendL) 61 : Decl(Decl::Friend, DC, L), 62 Friend(Friend), 63 NextFriend(), 64 FriendLoc(FriendL), 65 UnsupportedFriend(false) { 66 } 67 68 explicit FriendDecl(EmptyShell Empty) 69 : Decl(Decl::Friend, Empty), NextFriend() { } 70 71 FriendDecl *getNextFriend() { 72 return cast_or_null<FriendDecl>( 73 NextFriend.get(getASTContext().getExternalSource())); 74 } 75 76 public: 77 static FriendDecl *Create(ASTContext &C, DeclContext *DC, 78 SourceLocation L, FriendUnion Friend_, 79 SourceLocation FriendL); 80 static FriendDecl *Create(ASTContext &C, EmptyShell Empty); 81 82 /// If this friend declaration names an (untemplated but possibly 83 /// dependent) type, return the type; otherwise return null. This 84 /// is used for elaborated-type-specifiers and, in C++0x, for 85 /// arbitrary friend type declarations. 86 TypeSourceInfo *getFriendType() const { 87 return Friend.dyn_cast<TypeSourceInfo*>(); 88 } 89 90 /// If this friend declaration doesn't name a type, return the inner 91 /// declaration. 92 NamedDecl *getFriendDecl() const { 93 return Friend.dyn_cast<NamedDecl*>(); 94 } 95 96 /// Retrieves the location of the 'friend' keyword. 97 SourceLocation getFriendLoc() const { 98 return FriendLoc; 99 } 100 101 /// Retrieves the source range for the friend declaration. 102 SourceRange getSourceRange() const { 103 /* FIXME: consider the case of templates wrt start of range. */ 104 if (NamedDecl *ND = getFriendDecl()) 105 return SourceRange(getFriendLoc(), ND->getLocEnd()); 106 else if (TypeSourceInfo *TInfo = getFriendType()) 107 return SourceRange(getFriendLoc(), TInfo->getTypeLoc().getEndLoc()); 108 else 109 return SourceRange(getFriendLoc(), getLocation()); 110 } 111 112 /// Determines if this friend kind is unsupported. 113 bool isUnsupportedFriend() const { 114 return UnsupportedFriend; 115 } 116 void setUnsupportedFriend(bool Unsupported) { 117 UnsupportedFriend = Unsupported; 118 } 119 120 // Implement isa/cast/dyncast/etc. 121 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 122 static bool classof(const FriendDecl *D) { return true; } 123 static bool classofKind(Kind K) { return K == Decl::Friend; } 124 125 friend class ASTDeclReader; 126 friend class ASTDeclWriter; 127 }; 128 129 /// An iterator over the friend declarations of a class. 130 class CXXRecordDecl::friend_iterator { 131 FriendDecl *Ptr; 132 133 friend class CXXRecordDecl; 134 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {} 135 public: 136 friend_iterator() {} 137 138 typedef FriendDecl *value_type; 139 typedef FriendDecl *reference; 140 typedef FriendDecl *pointer; 141 typedef int difference_type; 142 typedef std::forward_iterator_tag iterator_category; 143 144 reference operator*() const { return Ptr; } 145 146 friend_iterator &operator++() { 147 assert(Ptr && "attempt to increment past end of friend list"); 148 Ptr = Ptr->getNextFriend(); 149 return *this; 150 } 151 152 friend_iterator operator++(int) { 153 friend_iterator tmp = *this; 154 ++*this; 155 return tmp; 156 } 157 158 bool operator==(const friend_iterator &Other) const { 159 return Ptr == Other.Ptr; 160 } 161 162 bool operator!=(const friend_iterator &Other) const { 163 return Ptr != Other.Ptr; 164 } 165 166 friend_iterator &operator+=(difference_type N) { 167 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator"); 168 while (N--) 169 ++*this; 170 return *this; 171 } 172 173 friend_iterator operator+(difference_type N) const { 174 friend_iterator tmp = *this; 175 tmp += N; 176 return tmp; 177 } 178 }; 179 180 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const { 181 return friend_iterator(data().FirstFriend); 182 } 183 184 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const { 185 return friend_iterator(0); 186 } 187 188 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) { 189 assert(FD->NextFriend == 0 && "friend already has next friend?"); 190 FD->NextFriend = data().FirstFriend; 191 data().FirstFriend = FD; 192 } 193 194 } 195 196 #endif 197