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