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 #include "clang/AST/DeclTemplate.h" 20 #include "llvm/Support/Compiler.h" 21 22 namespace clang { 23 24 /// FriendDecl - Represents the declaration of a friend entity, 25 /// which can be a function, a type, or a templated function or type. 26 // For example: 27 /// 28 /// @code 29 /// template <typename T> class A { 30 /// friend int foo(T); 31 /// friend class B; 32 /// friend T; // only in C++0x 33 /// template <typename U> friend class C; 34 /// template <typename U> friend A& operator+=(A&, const U&) { ... } 35 /// }; 36 /// @endcode 37 /// 38 /// The semantic context of a friend decl is its declaring class. 39 class FriendDecl : public Decl { 40 virtual void anchor(); 41 public: 42 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion; 43 44 private: 45 // The declaration that's a friend of this class. 46 FriendUnion Friend; 47 48 // A pointer to the next friend in the sequence. 49 LazyDeclPtr NextFriend; 50 51 // Location of the 'friend' specifier. 52 SourceLocation FriendLoc; 53 54 /// True if this 'friend' declaration is unsupported. Eventually we 55 /// will support every possible friend declaration, but for now we 56 /// silently ignore some and set this flag to authorize all access. 57 bool UnsupportedFriend : 1; 58 59 // The number of "outer" template parameter lists in non-templatic 60 // (currently unsupported) friend type declarations, such as 61 // template <class T> friend class A<T>::B; 62 unsigned NumTPLists : 31; 63 64 // The tail-allocated friend type template parameter lists (if any). 65 TemplateParameterList* const *getTPLists() const { 66 return reinterpret_cast<TemplateParameterList* const *>(this + 1); 67 } 68 TemplateParameterList **getTPLists() { 69 return reinterpret_cast<TemplateParameterList**>(this + 1); 70 } 71 72 friend class CXXRecordDecl::friend_iterator; 73 friend class CXXRecordDecl; 74 75 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend, 76 SourceLocation FriendL, 77 ArrayRef<TemplateParameterList*> FriendTypeTPLists) 78 : Decl(Decl::Friend, DC, L), 79 Friend(Friend), 80 NextFriend(), 81 FriendLoc(FriendL), 82 UnsupportedFriend(false), 83 NumTPLists(FriendTypeTPLists.size()) { 84 for (unsigned i = 0; i < NumTPLists; ++i) 85 getTPLists()[i] = FriendTypeTPLists[i]; 86 } 87 88 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists) 89 : Decl(Decl::Friend, Empty), NextFriend(), 90 NumTPLists(NumFriendTypeTPLists) { } 91 92 FriendDecl *getNextFriend() { 93 if (!NextFriend.isOffset()) 94 return cast_or_null<FriendDecl>(NextFriend.get(0)); 95 return getNextFriendSlowCase(); 96 } 97 FriendDecl *getNextFriendSlowCase(); 98 99 public: 100 static FriendDecl *Create(ASTContext &C, DeclContext *DC, 101 SourceLocation L, FriendUnion Friend_, 102 SourceLocation FriendL, 103 ArrayRef<TemplateParameterList*> FriendTypeTPLists 104 = None); 105 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID, 106 unsigned FriendTypeNumTPLists); 107 108 /// If this friend declaration names an (untemplated but possibly 109 /// dependent) type, return the type; otherwise return null. This 110 /// is used for elaborated-type-specifiers and, in C++0x, for 111 /// arbitrary friend type declarations. 112 TypeSourceInfo *getFriendType() const { 113 return Friend.dyn_cast<TypeSourceInfo*>(); 114 } 115 unsigned getFriendTypeNumTemplateParameterLists() const { 116 return NumTPLists; 117 } 118 TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const { 119 assert(N < NumTPLists); 120 return getTPLists()[N]; 121 } 122 123 /// If this friend declaration doesn't name a type, return the inner 124 /// declaration. 125 NamedDecl *getFriendDecl() const { 126 return Friend.dyn_cast<NamedDecl*>(); 127 } 128 129 /// Retrieves the location of the 'friend' keyword. 130 SourceLocation getFriendLoc() const { 131 return FriendLoc; 132 } 133 134 /// Retrieves the source range for the friend declaration. 135 SourceRange getSourceRange() const LLVM_READONLY { 136 if (NamedDecl *ND = getFriendDecl()) { 137 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) 138 return FTD->getSourceRange(); 139 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(ND)) { 140 if (DD->getOuterLocStart() != DD->getInnerLocStart()) 141 return DD->getSourceRange(); 142 } 143 return SourceRange(getFriendLoc(), ND->getLocEnd()); 144 } 145 else if (TypeSourceInfo *TInfo = getFriendType()) { 146 SourceLocation StartL = (NumTPLists == 0) 147 ? getFriendLoc() 148 : getTPLists()[0]->getTemplateLoc(); 149 return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc()); 150 } 151 else 152 return SourceRange(getFriendLoc(), getLocation()); 153 } 154 155 /// Determines if this friend kind is unsupported. 156 bool isUnsupportedFriend() const { 157 return UnsupportedFriend; 158 } 159 void setUnsupportedFriend(bool Unsupported) { 160 UnsupportedFriend = Unsupported; 161 } 162 163 // Implement isa/cast/dyncast/etc. 164 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 165 static bool classofKind(Kind K) { return K == Decl::Friend; } 166 167 friend class ASTDeclReader; 168 friend class ASTDeclWriter; 169 }; 170 171 /// An iterator over the friend declarations of a class. 172 class CXXRecordDecl::friend_iterator { 173 FriendDecl *Ptr; 174 175 friend class CXXRecordDecl; 176 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {} 177 public: 178 friend_iterator() {} 179 180 typedef FriendDecl *value_type; 181 typedef FriendDecl *reference; 182 typedef FriendDecl *pointer; 183 typedef int difference_type; 184 typedef std::forward_iterator_tag iterator_category; 185 186 reference operator*() const { return Ptr; } 187 188 friend_iterator &operator++() { 189 assert(Ptr && "attempt to increment past end of friend list"); 190 Ptr = Ptr->getNextFriend(); 191 return *this; 192 } 193 194 friend_iterator operator++(int) { 195 friend_iterator tmp = *this; 196 ++*this; 197 return tmp; 198 } 199 200 bool operator==(const friend_iterator &Other) const { 201 return Ptr == Other.Ptr; 202 } 203 204 bool operator!=(const friend_iterator &Other) const { 205 return Ptr != Other.Ptr; 206 } 207 208 friend_iterator &operator+=(difference_type N) { 209 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator"); 210 while (N--) 211 ++*this; 212 return *this; 213 } 214 215 friend_iterator operator+(difference_type N) const { 216 friend_iterator tmp = *this; 217 tmp += N; 218 return tmp; 219 } 220 }; 221 222 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const { 223 return friend_iterator(getFirstFriend()); 224 } 225 226 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const { 227 return friend_iterator(0); 228 } 229 230 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) { 231 assert(!FD->NextFriend && "friend already has next friend?"); 232 FD->NextFriend = data().FirstFriend; 233 data().FirstFriend = FD; 234 } 235 236 } 237 238 #endif 239