1 //===- DeclFriend.h - Classes for C++ friend declarations -------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the section of the AST representing C++ friend 10 // declarations. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_AST_DECLFRIEND_H 15 #define LLVM_CLANG_AST_DECLFRIEND_H 16 17 #include "clang/AST/Decl.h" 18 #include "clang/AST/DeclBase.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclTemplate.h" 21 #include "clang/AST/ExternalASTSource.h" 22 #include "clang/AST/TypeLoc.h" 23 #include "clang/Basic/LLVM.h" 24 #include "clang/Basic/SourceLocation.h" 25 #include "llvm/ADT/ArrayRef.h" 26 #include "llvm/ADT/PointerUnion.h" 27 #include "llvm/Support/Casting.h" 28 #include "llvm/Support/Compiler.h" 29 #include "llvm/Support/TrailingObjects.h" 30 #include <cassert> 31 #include <iterator> 32 33 namespace clang { 34 35 class ASTContext; 36 37 /// FriendDecl - Represents the declaration of a friend entity, 38 /// which can be a function, a type, or a templated function or type. 39 /// For example: 40 /// 41 /// @code 42 /// template <typename T> class A { 43 /// friend int foo(T); 44 /// friend class B; 45 /// friend T; // only in C++0x 46 /// template <typename U> friend class C; 47 /// template <typename U> friend A& operator+=(A&, const U&) { ... } 48 /// }; 49 /// @endcode 50 /// 51 /// The semantic context of a friend decl is its declaring class. 52 class FriendDecl final 53 : public Decl, 54 private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> { 55 virtual void anchor(); 56 57 public: 58 using FriendUnion = llvm::PointerUnion<NamedDecl *, TypeSourceInfo *>; 59 60 private: 61 friend class CXXRecordDecl; 62 friend class CXXRecordDecl::friend_iterator; 63 64 // The declaration that's a friend of this class. 65 FriendUnion Friend; 66 67 // A pointer to the next friend in the sequence. 68 LazyDeclPtr NextFriend; 69 70 // Location of the 'friend' specifier. 71 SourceLocation FriendLoc; 72 73 // Location of the '...', if present. 74 SourceLocation EllipsisLoc; 75 76 /// True if this 'friend' declaration is unsupported. Eventually we 77 /// will support every possible friend declaration, but for now we 78 /// silently ignore some and set this flag to authorize all access. 79 LLVM_PREFERRED_TYPE(bool) 80 unsigned UnsupportedFriend : 1; 81 82 // The number of "outer" template parameter lists in non-templatic 83 // (currently unsupported) friend type declarations, such as 84 // template <class T> friend class A<T>::B; 85 unsigned NumTPLists : 31; 86 87 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend, 88 SourceLocation FriendL, SourceLocation EllipsisLoc, 89 ArrayRef<TemplateParameterList *> FriendTypeTPLists) 90 : Decl(Decl::Friend, DC, L), Friend(Friend), FriendLoc(FriendL), 91 EllipsisLoc(EllipsisLoc), UnsupportedFriend(false), 92 NumTPLists(FriendTypeTPLists.size()) { 93 for (unsigned i = 0; i < NumTPLists; ++i) 94 getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i]; 95 } 96 97 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists) 98 : Decl(Decl::Friend, Empty), UnsupportedFriend(false), 99 NumTPLists(NumFriendTypeTPLists) {} 100 101 FriendDecl *getNextFriend() { 102 if (!NextFriend.isOffset()) 103 return cast_or_null<FriendDecl>(NextFriend.get(nullptr)); 104 return getNextFriendSlowCase(); 105 } 106 107 FriendDecl *getNextFriendSlowCase(); 108 109 public: 110 friend class ASTDeclReader; 111 friend class ASTDeclWriter; 112 friend class ASTNodeImporter; 113 friend TrailingObjects; 114 115 static FriendDecl * 116 Create(ASTContext &C, DeclContext *DC, SourceLocation L, FriendUnion Friend_, 117 SourceLocation FriendL, SourceLocation EllipsisLoc = {}, 118 ArrayRef<TemplateParameterList *> FriendTypeTPLists = {}); 119 static FriendDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID, 120 unsigned FriendTypeNumTPLists); 121 122 /// If this friend declaration names an (untemplated but possibly 123 /// dependent) type, return the type; otherwise return null. This 124 /// is used for elaborated-type-specifiers and, in C++0x, for 125 /// arbitrary friend type declarations. 126 TypeSourceInfo *getFriendType() const { 127 return Friend.dyn_cast<TypeSourceInfo*>(); 128 } 129 130 unsigned getFriendTypeNumTemplateParameterLists() const { 131 return NumTPLists; 132 } 133 134 TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const { 135 assert(N < NumTPLists); 136 return getTrailingObjects<TemplateParameterList *>()[N]; 137 } 138 139 /// If this friend declaration doesn't name a type, return the inner 140 /// declaration. 141 NamedDecl *getFriendDecl() const { 142 return Friend.dyn_cast<NamedDecl *>(); 143 } 144 145 /// Retrieves the location of the 'friend' keyword. 146 SourceLocation getFriendLoc() const { 147 return FriendLoc; 148 } 149 150 /// Retrieves the location of the '...', if present. 151 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 152 153 /// Retrieves the source range for the friend declaration. 154 SourceRange getSourceRange() const override LLVM_READONLY { 155 if (TypeSourceInfo *TInfo = getFriendType()) { 156 SourceLocation StartL = 157 (NumTPLists == 0) ? getFriendLoc() 158 : getTrailingObjects<TemplateParameterList *>()[0] 159 ->getTemplateLoc(); 160 SourceLocation EndL = isPackExpansion() ? getEllipsisLoc() 161 : TInfo->getTypeLoc().getEndLoc(); 162 return SourceRange(StartL, EndL); 163 } 164 165 if (isPackExpansion()) 166 return SourceRange(getFriendLoc(), getEllipsisLoc()); 167 168 if (NamedDecl *ND = getFriendDecl()) { 169 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) 170 return FD->getSourceRange(); 171 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(ND)) 172 return FTD->getSourceRange(); 173 if (const auto *CTD = dyn_cast<ClassTemplateDecl>(ND)) 174 return CTD->getSourceRange(); 175 if (const auto *DD = dyn_cast<DeclaratorDecl>(ND)) { 176 if (DD->getOuterLocStart() != DD->getInnerLocStart()) 177 return DD->getSourceRange(); 178 } 179 return SourceRange(getFriendLoc(), ND->getEndLoc()); 180 } 181 182 return SourceRange(getFriendLoc(), getLocation()); 183 } 184 185 /// Determines if this friend kind is unsupported. 186 bool isUnsupportedFriend() const { 187 return UnsupportedFriend; 188 } 189 void setUnsupportedFriend(bool Unsupported) { 190 UnsupportedFriend = Unsupported; 191 } 192 193 bool isPackExpansion() const { return EllipsisLoc.isValid(); } 194 195 // Implement isa/cast/dyncast/etc. 196 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 197 static bool classofKind(Kind K) { return K == Decl::Friend; } 198 }; 199 200 /// An iterator over the friend declarations of a class. 201 class CXXRecordDecl::friend_iterator { 202 friend class CXXRecordDecl; 203 204 FriendDecl *Ptr; 205 206 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {} 207 208 public: 209 friend_iterator() = default; 210 211 using value_type = FriendDecl *; 212 using reference = FriendDecl *; 213 using pointer = FriendDecl *; 214 using difference_type = int; 215 using iterator_category = std::forward_iterator_tag; 216 217 reference operator*() const { return Ptr; } 218 219 friend_iterator &operator++() { 220 assert(Ptr && "attempt to increment past end of friend list"); 221 Ptr = Ptr->getNextFriend(); 222 return *this; 223 } 224 225 friend_iterator operator++(int) { 226 friend_iterator tmp = *this; 227 ++*this; 228 return tmp; 229 } 230 231 bool operator==(const friend_iterator &Other) const { 232 return Ptr == Other.Ptr; 233 } 234 235 bool operator!=(const friend_iterator &Other) const { 236 return Ptr != Other.Ptr; 237 } 238 239 friend_iterator &operator+=(difference_type N) { 240 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator"); 241 while (N--) 242 ++*this; 243 return *this; 244 } 245 246 friend_iterator operator+(difference_type N) const { 247 friend_iterator tmp = *this; 248 tmp += N; 249 return tmp; 250 } 251 }; 252 253 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const { 254 return friend_iterator(getFirstFriend()); 255 } 256 257 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const { 258 return friend_iterator(nullptr); 259 } 260 261 inline CXXRecordDecl::friend_range CXXRecordDecl::friends() const { 262 return friend_range(friend_begin(), friend_end()); 263 } 264 265 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) { 266 assert(!FD->NextFriend && "friend already has next friend?"); 267 FD->NextFriend = data().FirstFriend; 268 data().FirstFriend = FD; 269 } 270 271 } // namespace clang 272 273 #endif // LLVM_CLANG_AST_DECLFRIEND_H 274