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