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 LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();
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
FriendDecl(DeclContext * DC,SourceLocation L,FriendUnion Friend,SourceLocation FriendL,SourceLocation EllipsisLoc,ArrayRef<TemplateParameterList * > FriendTypeTPLists)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 llvm::copy(FriendTypeTPLists, getTrailingObjects());
94 }
95
FriendDecl(EmptyShell Empty,unsigned NumFriendTypeTPLists)96 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
97 : Decl(Decl::Friend, Empty), UnsupportedFriend(false),
98 NumTPLists(NumFriendTypeTPLists) {}
99
getNextFriend()100 FriendDecl *getNextFriend() {
101 if (!NextFriend.isOffset())
102 return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
103 return getNextFriendSlowCase();
104 }
105
106 FriendDecl *getNextFriendSlowCase();
107
108 public:
109 friend class ASTDeclReader;
110 friend class ASTDeclWriter;
111 friend class ASTNodeImporter;
112 friend TrailingObjects;
113
114 static FriendDecl *
115 Create(ASTContext &C, DeclContext *DC, SourceLocation L, FriendUnion Friend_,
116 SourceLocation FriendL, SourceLocation EllipsisLoc = {},
117 ArrayRef<TemplateParameterList *> FriendTypeTPLists = {});
118 static FriendDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID,
119 unsigned FriendTypeNumTPLists);
120
121 /// If this friend declaration names an (untemplated but possibly
122 /// dependent) type, return the type; otherwise return null. This
123 /// is used for elaborated-type-specifiers and, in C++0x, for
124 /// arbitrary friend type declarations.
getFriendType()125 TypeSourceInfo *getFriendType() const {
126 return Friend.dyn_cast<TypeSourceInfo*>();
127 }
128
getFriendTypeNumTemplateParameterLists()129 unsigned getFriendTypeNumTemplateParameterLists() const {
130 return NumTPLists;
131 }
132
getFriendTypeTemplateParameterList(unsigned N)133 TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
134 return getTrailingObjects(NumTPLists)[N];
135 }
136
137 /// If this friend declaration doesn't name a type, return the inner
138 /// declaration.
getFriendDecl()139 NamedDecl *getFriendDecl() const {
140 return Friend.dyn_cast<NamedDecl *>();
141 }
142
143 /// Retrieves the location of the 'friend' keyword.
getFriendLoc()144 SourceLocation getFriendLoc() const {
145 return FriendLoc;
146 }
147
148 /// Retrieves the location of the '...', if present.
getEllipsisLoc()149 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
150
151 /// Retrieves the source range for the friend declaration.
getSourceRange()152 SourceRange getSourceRange() const override LLVM_READONLY {
153 if (TypeSourceInfo *TInfo = getFriendType()) {
154 SourceLocation StartL = (NumTPLists == 0)
155 ? getFriendLoc()
156 : getTrailingObjects()[0]->getTemplateLoc();
157 SourceLocation EndL = isPackExpansion() ? getEllipsisLoc()
158 : TInfo->getTypeLoc().getEndLoc();
159 return SourceRange(StartL, EndL);
160 }
161
162 if (isPackExpansion())
163 return SourceRange(getFriendLoc(), getEllipsisLoc());
164
165 if (NamedDecl *ND = getFriendDecl()) {
166 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
167 return FD->getSourceRange();
168 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(ND))
169 return FTD->getSourceRange();
170 if (const auto *CTD = dyn_cast<ClassTemplateDecl>(ND))
171 return CTD->getSourceRange();
172 if (const auto *DD = dyn_cast<DeclaratorDecl>(ND)) {
173 if (DD->getOuterLocStart() != DD->getInnerLocStart())
174 return DD->getSourceRange();
175 }
176 return SourceRange(getFriendLoc(), ND->getEndLoc());
177 }
178
179 return SourceRange(getFriendLoc(), getLocation());
180 }
181
182 /// Determines if this friend kind is unsupported.
isUnsupportedFriend()183 bool isUnsupportedFriend() const {
184 return UnsupportedFriend;
185 }
setUnsupportedFriend(bool Unsupported)186 void setUnsupportedFriend(bool Unsupported) {
187 UnsupportedFriend = Unsupported;
188 }
189
isPackExpansion()190 bool isPackExpansion() const { return EllipsisLoc.isValid(); }
191
192 // Implement isa/cast/dyncast/etc.
classof(const Decl * D)193 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
classofKind(Kind K)194 static bool classofKind(Kind K) { return K == Decl::Friend; }
195 };
196
197 /// An iterator over the friend declarations of a class.
198 class CXXRecordDecl::friend_iterator {
199 friend class CXXRecordDecl;
200
201 FriendDecl *Ptr;
202
friend_iterator(FriendDecl * Ptr)203 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
204
205 public:
206 friend_iterator() = default;
207
208 using value_type = FriendDecl *;
209 using reference = FriendDecl *;
210 using pointer = FriendDecl *;
211 using difference_type = int;
212 using iterator_category = std::forward_iterator_tag;
213
214 reference operator*() const { return Ptr; }
215
216 friend_iterator &operator++() {
217 assert(Ptr && "attempt to increment past end of friend list");
218 Ptr = Ptr->getNextFriend();
219 return *this;
220 }
221
222 friend_iterator operator++(int) {
223 friend_iterator tmp = *this;
224 ++*this;
225 return tmp;
226 }
227
228 bool operator==(const friend_iterator &Other) const {
229 return Ptr == Other.Ptr;
230 }
231
232 bool operator!=(const friend_iterator &Other) const {
233 return Ptr != Other.Ptr;
234 }
235
236 friend_iterator &operator+=(difference_type N) {
237 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
238 while (N--)
239 ++*this;
240 return *this;
241 }
242
243 friend_iterator operator+(difference_type N) const {
244 friend_iterator tmp = *this;
245 tmp += N;
246 return tmp;
247 }
248 };
249
friend_begin()250 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
251 return friend_iterator(getFirstFriend());
252 }
253
friend_end()254 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
255 return friend_iterator(nullptr);
256 }
257
friends()258 inline CXXRecordDecl::friend_range CXXRecordDecl::friends() const {
259 return friend_range(friend_begin(), friend_end());
260 }
261
pushFriendDecl(FriendDecl * FD)262 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
263 assert(!FD->NextFriend && "friend already has next friend?");
264 FD->NextFriend = data().FirstFriend;
265 data().FirstFriend = FD;
266 }
267
268 } // namespace clang
269
270 #endif // LLVM_CLANG_AST_DECLFRIEND_H
271