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