1 //===- DeclFriend.h - Classes for C++ friend declarations -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the section of the AST representing C++ friend
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_DECLFRIEND_H
16 #define LLVM_CLANG_AST_DECLFRIEND_H
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"
39 /// FriendDecl - Represents the declaration of a friend entity,
40 /// which can be a function, a type, or a templated function or type.
44 /// template <typename T> class A {
45 /// friend int foo(T);
47 /// friend T; // only in C++0x
48 /// template <typename U> friend class C;
49 /// template <typename U> friend A& operator+=(A&, const U&) { ... }
53 /// The semantic context of a friend decl is its declaring class.
54 class FriendDecl final
56 private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> {
57 virtual void anchor();
60 using FriendUnion = llvm::PointerUnion<NamedDecl *, TypeSourceInfo *>;
63 friend class CXXRecordDecl;
64 friend class CXXRecordDecl::friend_iterator;
66 // The declaration that's a friend of this class.
69 // A pointer to the next friend in the sequence.
70 LazyDeclPtr NextFriend;
72 // Location of the 'friend' specifier.
73 SourceLocation FriendLoc;
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;
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;
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];
94 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
95 : Decl(Decl::Friend, Empty), UnsupportedFriend(false),
96 NumTPLists(NumFriendTypeTPLists) {}
98 FriendDecl *getNextFriend() {
99 if (!NextFriend.isOffset())
100 return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
101 return getNextFriendSlowCase();
104 FriendDecl *getNextFriendSlowCase();
107 friend class ASTDeclReader;
108 friend class ASTDeclWriter;
109 friend class ASTNodeImporter;
110 friend TrailingObjects;
112 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
113 SourceLocation L, FriendUnion Friend_,
114 SourceLocation FriendL,
115 ArrayRef<TemplateParameterList*> FriendTypeTPLists
117 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
118 unsigned FriendTypeNumTPLists);
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.
124 TypeSourceInfo *getFriendType() const {
125 return Friend.dyn_cast<TypeSourceInfo*>();
128 unsigned getFriendTypeNumTemplateParameterLists() const {
132 TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
133 assert(N < NumTPLists);
134 return getTrailingObjects<TemplateParameterList *>()[N];
137 /// If this friend declaration doesn't name a type, return the inner
139 NamedDecl *getFriendDecl() const {
140 return Friend.dyn_cast<NamedDecl *>();
143 /// Retrieves the location of the 'friend' keyword.
144 SourceLocation getFriendLoc() const {
148 /// Retrieves the source range for the friend declaration.
149 SourceRange getSourceRange() const override LLVM_READONLY {
150 if (NamedDecl *ND = getFriendDecl()) {
151 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
152 return FD->getSourceRange();
153 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
154 return FTD->getSourceRange();
155 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
156 return CTD->getSourceRange();
157 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(ND)) {
158 if (DD->getOuterLocStart() != DD->getInnerLocStart())
159 return DD->getSourceRange();
161 return SourceRange(getFriendLoc(), ND->getLocEnd());
163 else if (TypeSourceInfo *TInfo = getFriendType()) {
164 SourceLocation StartL =
165 (NumTPLists == 0) ? getFriendLoc()
166 : getTrailingObjects<TemplateParameterList *>()[0]
168 return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
171 return SourceRange(getFriendLoc(), getLocation());
174 /// Determines if this friend kind is unsupported.
175 bool isUnsupportedFriend() const {
176 return UnsupportedFriend;
178 void setUnsupportedFriend(bool Unsupported) {
179 UnsupportedFriend = Unsupported;
182 // Implement isa/cast/dyncast/etc.
183 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
184 static bool classofKind(Kind K) { return K == Decl::Friend; }
187 /// An iterator over the friend declarations of a class.
188 class CXXRecordDecl::friend_iterator {
189 friend class CXXRecordDecl;
193 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
196 friend_iterator() = default;
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;
204 reference operator*() const { return Ptr; }
206 friend_iterator &operator++() {
207 assert(Ptr && "attempt to increment past end of friend list");
208 Ptr = Ptr->getNextFriend();
212 friend_iterator operator++(int) {
213 friend_iterator tmp = *this;
218 bool operator==(const friend_iterator &Other) const {
219 return Ptr == Other.Ptr;
222 bool operator!=(const friend_iterator &Other) const {
223 return Ptr != Other.Ptr;
226 friend_iterator &operator+=(difference_type N) {
227 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
233 friend_iterator operator+(difference_type N) const {
234 friend_iterator tmp = *this;
240 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
241 return friend_iterator(getFirstFriend());
244 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
245 return friend_iterator(nullptr);
248 inline CXXRecordDecl::friend_range CXXRecordDecl::friends() const {
249 return friend_range(friend_begin(), friend_end());
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;
260 #endif // LLVM_CLANG_AST_DECLFRIEND_H