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 : public Decl {
41 virtual void anchor();
42 public:
43 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
44
45 private:
46 // The declaration that's a friend of this class.
47 FriendUnion Friend;
48
49 // A pointer to the next friend in the sequence.
50 LazyDeclPtr NextFriend;
51
52 // Location of the 'friend' specifier.
53 SourceLocation FriendLoc;
54
55 /// True if this 'friend' declaration is unsupported. Eventually we
56 /// will support every possible friend declaration, but for now we
57 /// silently ignore some and set this flag to authorize all access.
58 bool UnsupportedFriend : 1;
59
60 // The number of "outer" template parameter lists in non-templatic
61 // (currently unsupported) friend type declarations, such as
62 // template <class T> friend class A<T>::B;
63 unsigned NumTPLists : 31;
64
65 // The tail-allocated friend type template parameter lists (if any).
getTPLists()66 TemplateParameterList* const *getTPLists() const {
67 return reinterpret_cast<TemplateParameterList* const *>(this + 1);
68 }
getTPLists()69 TemplateParameterList **getTPLists() {
70 return reinterpret_cast<TemplateParameterList**>(this + 1);
71 }
72
73 friend class CXXRecordDecl::friend_iterator;
74 friend class CXXRecordDecl;
75
FriendDecl(DeclContext * DC,SourceLocation L,FriendUnion Friend,SourceLocation FriendL,ArrayRef<TemplateParameterList * > FriendTypeTPLists)76 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
77 SourceLocation FriendL,
78 ArrayRef<TemplateParameterList*> FriendTypeTPLists)
79 : Decl(Decl::Friend, DC, L),
80 Friend(Friend),
81 NextFriend(),
82 FriendLoc(FriendL),
83 UnsupportedFriend(false),
84 NumTPLists(FriendTypeTPLists.size()) {
85 for (unsigned i = 0; i < NumTPLists; ++i)
86 getTPLists()[i] = FriendTypeTPLists[i];
87 }
88
FriendDecl(EmptyShell Empty,unsigned NumFriendTypeTPLists)89 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
90 : Decl(Decl::Friend, Empty), NextFriend(),
91 NumTPLists(NumFriendTypeTPLists) { }
92
getNextFriend()93 FriendDecl *getNextFriend() {
94 if (!NextFriend.isOffset())
95 return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
96 return getNextFriendSlowCase();
97 }
98 FriendDecl *getNextFriendSlowCase();
99
100 public:
101 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
102 SourceLocation L, FriendUnion Friend_,
103 SourceLocation FriendL,
104 ArrayRef<TemplateParameterList*> FriendTypeTPLists
105 = None);
106 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
107 unsigned FriendTypeNumTPLists);
108
109 /// If this friend declaration names an (untemplated but possibly
110 /// dependent) type, return the type; otherwise return null. This
111 /// is used for elaborated-type-specifiers and, in C++0x, for
112 /// arbitrary friend type declarations.
getFriendType()113 TypeSourceInfo *getFriendType() const {
114 return Friend.dyn_cast<TypeSourceInfo*>();
115 }
getFriendTypeNumTemplateParameterLists()116 unsigned getFriendTypeNumTemplateParameterLists() const {
117 return NumTPLists;
118 }
getFriendTypeTemplateParameterList(unsigned N)119 TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
120 assert(N < NumTPLists);
121 return getTPLists()[N];
122 }
123
124 /// If this friend declaration doesn't name a type, return the inner
125 /// declaration.
getFriendDecl()126 NamedDecl *getFriendDecl() const {
127 return Friend.dyn_cast<NamedDecl*>();
128 }
129
130 /// Retrieves the location of the 'friend' keyword.
getFriendLoc()131 SourceLocation getFriendLoc() const {
132 return FriendLoc;
133 }
134
135 /// Retrieves the source range for the friend declaration.
getSourceRange()136 SourceRange getSourceRange() const override LLVM_READONLY {
137 if (NamedDecl *ND = getFriendDecl()) {
138 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
139 return FTD->getSourceRange();
140 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(ND)) {
141 if (DD->getOuterLocStart() != DD->getInnerLocStart())
142 return DD->getSourceRange();
143 }
144 return SourceRange(getFriendLoc(), ND->getLocEnd());
145 }
146 else if (TypeSourceInfo *TInfo = getFriendType()) {
147 SourceLocation StartL = (NumTPLists == 0)
148 ? getFriendLoc()
149 : getTPLists()[0]->getTemplateLoc();
150 return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
151 }
152 else
153 return SourceRange(getFriendLoc(), getLocation());
154 }
155
156 /// Determines if this friend kind is unsupported.
isUnsupportedFriend()157 bool isUnsupportedFriend() const {
158 return UnsupportedFriend;
159 }
setUnsupportedFriend(bool Unsupported)160 void setUnsupportedFriend(bool Unsupported) {
161 UnsupportedFriend = Unsupported;
162 }
163
164 // Implement isa/cast/dyncast/etc.
classof(const Decl * D)165 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
classofKind(Kind K)166 static bool classofKind(Kind K) { return K == Decl::Friend; }
167
168 friend class ASTDeclReader;
169 friend class ASTDeclWriter;
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