clang API Documentation
00001 //===-- DeclFriend.h - Classes for C++ friend declarations -*- C++ -*------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the section of the AST representing C++ friend 00011 // declarations. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CLANG_AST_DECLFRIEND_H 00016 #define LLVM_CLANG_AST_DECLFRIEND_H 00017 00018 #include "clang/AST/DeclCXX.h" 00019 #include "llvm/Support/Compiler.h" 00020 00021 namespace clang { 00022 00023 /// FriendDecl - Represents the declaration of a friend entity, 00024 /// which can be a function, a type, or a templated function or type. 00025 // For example: 00026 /// 00027 /// @code 00028 /// template <typename T> class A { 00029 /// friend int foo(T); 00030 /// friend class B; 00031 /// friend T; // only in C++0x 00032 /// template <typename U> friend class C; 00033 /// template <typename U> friend A& operator+=(A&, const U&) { ... } 00034 /// }; 00035 /// @endcode 00036 /// 00037 /// The semantic context of a friend decl is its declaring class. 00038 class FriendDecl : public Decl { 00039 virtual void anchor(); 00040 public: 00041 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion; 00042 00043 private: 00044 // The declaration that's a friend of this class. 00045 FriendUnion Friend; 00046 00047 // A pointer to the next friend in the sequence. 00048 LazyDeclPtr NextFriend; 00049 00050 // Location of the 'friend' specifier. 00051 SourceLocation FriendLoc; 00052 00053 /// True if this 'friend' declaration is unsupported. Eventually we 00054 /// will support every possible friend declaration, but for now we 00055 /// silently ignore some and set this flag to authorize all access. 00056 bool UnsupportedFriend; 00057 00058 friend class CXXRecordDecl::friend_iterator; 00059 friend class CXXRecordDecl; 00060 00061 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend, 00062 SourceLocation FriendL) 00063 : Decl(Decl::Friend, DC, L), 00064 Friend(Friend), 00065 NextFriend(), 00066 FriendLoc(FriendL), 00067 UnsupportedFriend(false) { 00068 } 00069 00070 explicit FriendDecl(EmptyShell Empty) 00071 : Decl(Decl::Friend, Empty), NextFriend() { } 00072 00073 FriendDecl *getNextFriend() { 00074 return cast_or_null<FriendDecl>( 00075 NextFriend.get(getASTContext().getExternalSource())); 00076 } 00077 00078 public: 00079 static FriendDecl *Create(ASTContext &C, DeclContext *DC, 00080 SourceLocation L, FriendUnion Friend_, 00081 SourceLocation FriendL); 00082 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID); 00083 00084 /// If this friend declaration names an (untemplated but possibly 00085 /// dependent) type, return the type; otherwise return null. This 00086 /// is used for elaborated-type-specifiers and, in C++0x, for 00087 /// arbitrary friend type declarations. 00088 TypeSourceInfo *getFriendType() const { 00089 return Friend.dyn_cast<TypeSourceInfo*>(); 00090 } 00091 00092 /// If this friend declaration doesn't name a type, return the inner 00093 /// declaration. 00094 NamedDecl *getFriendDecl() const { 00095 return Friend.dyn_cast<NamedDecl*>(); 00096 } 00097 00098 /// Retrieves the location of the 'friend' keyword. 00099 SourceLocation getFriendLoc() const { 00100 return FriendLoc; 00101 } 00102 00103 /// Retrieves the source range for the friend declaration. 00104 SourceRange getSourceRange() const LLVM_READONLY { 00105 /* FIXME: consider the case of templates wrt start of range. */ 00106 if (NamedDecl *ND = getFriendDecl()) 00107 return SourceRange(getFriendLoc(), ND->getLocEnd()); 00108 else if (TypeSourceInfo *TInfo = getFriendType()) 00109 return SourceRange(getFriendLoc(), TInfo->getTypeLoc().getEndLoc()); 00110 else 00111 return SourceRange(getFriendLoc(), getLocation()); 00112 } 00113 00114 /// Determines if this friend kind is unsupported. 00115 bool isUnsupportedFriend() const { 00116 return UnsupportedFriend; 00117 } 00118 void setUnsupportedFriend(bool Unsupported) { 00119 UnsupportedFriend = Unsupported; 00120 } 00121 00122 // Implement isa/cast/dyncast/etc. 00123 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 00124 static bool classof(const FriendDecl *D) { return true; } 00125 static bool classofKind(Kind K) { return K == Decl::Friend; } 00126 00127 friend class ASTDeclReader; 00128 friend class ASTDeclWriter; 00129 }; 00130 00131 /// An iterator over the friend declarations of a class. 00132 class CXXRecordDecl::friend_iterator { 00133 FriendDecl *Ptr; 00134 00135 friend class CXXRecordDecl; 00136 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {} 00137 public: 00138 friend_iterator() {} 00139 00140 typedef FriendDecl *value_type; 00141 typedef FriendDecl *reference; 00142 typedef FriendDecl *pointer; 00143 typedef int difference_type; 00144 typedef std::forward_iterator_tag iterator_category; 00145 00146 reference operator*() const { return Ptr; } 00147 00148 friend_iterator &operator++() { 00149 assert(Ptr && "attempt to increment past end of friend list"); 00150 Ptr = Ptr->getNextFriend(); 00151 return *this; 00152 } 00153 00154 friend_iterator operator++(int) { 00155 friend_iterator tmp = *this; 00156 ++*this; 00157 return tmp; 00158 } 00159 00160 bool operator==(const friend_iterator &Other) const { 00161 return Ptr == Other.Ptr; 00162 } 00163 00164 bool operator!=(const friend_iterator &Other) const { 00165 return Ptr != Other.Ptr; 00166 } 00167 00168 friend_iterator &operator+=(difference_type N) { 00169 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator"); 00170 while (N--) 00171 ++*this; 00172 return *this; 00173 } 00174 00175 friend_iterator operator+(difference_type N) const { 00176 friend_iterator tmp = *this; 00177 tmp += N; 00178 return tmp; 00179 } 00180 }; 00181 00182 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const { 00183 return friend_iterator(data().FirstFriend); 00184 } 00185 00186 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const { 00187 return friend_iterator(0); 00188 } 00189 00190 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) { 00191 assert(FD->NextFriend == 0 && "friend already has next friend?"); 00192 FD->NextFriend = data().FirstFriend; 00193 data().FirstFriend = FD; 00194 } 00195 00196 } 00197 00198 #endif