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/DeclCXX.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/TypeLoc.h"
21 #include "llvm/Support/Compiler.h"
25 /// FriendDecl - Represents the declaration of a friend entity,
26 /// which can be a function, a type, or a templated function or type.
30 /// template <typename T> class A {
31 /// friend int foo(T);
33 /// friend T; // only in C++0x
34 /// template <typename U> friend class C;
35 /// template <typename U> friend A& operator+=(A&, const U&) { ... }
39 /// The semantic context of a friend decl is its declaring class.
40 class FriendDecl final
42 private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> {
43 virtual void anchor();
45 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
48 // The declaration that's a friend of this class.
51 // A pointer to the next friend in the sequence.
52 LazyDeclPtr NextFriend;
54 // Location of the 'friend' specifier.
55 SourceLocation FriendLoc;
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;
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;
67 friend class CXXRecordDecl::friend_iterator;
68 friend class CXXRecordDecl;
70 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
71 SourceLocation FriendL,
72 ArrayRef<TemplateParameterList*> FriendTypeTPLists)
73 : Decl(Decl::Friend, DC, L),
77 UnsupportedFriend(false),
78 NumTPLists(FriendTypeTPLists.size()) {
79 for (unsigned i = 0; i < NumTPLists; ++i)
80 getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i];
83 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
84 : Decl(Decl::Friend, Empty), NextFriend(),
85 UnsupportedFriend(false),
86 NumTPLists(NumFriendTypeTPLists) { }
88 FriendDecl *getNextFriend() {
89 if (!NextFriend.isOffset())
90 return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
91 return getNextFriendSlowCase();
93 FriendDecl *getNextFriendSlowCase();
96 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
97 SourceLocation L, FriendUnion Friend_,
98 SourceLocation FriendL,
99 ArrayRef<TemplateParameterList*> FriendTypeTPLists
101 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
102 unsigned FriendTypeNumTPLists);
104 /// If this friend declaration names an (untemplated but possibly
105 /// dependent) type, return the type; otherwise return null. This
106 /// is used for elaborated-type-specifiers and, in C++0x, for
107 /// arbitrary friend type declarations.
108 TypeSourceInfo *getFriendType() const {
109 return Friend.dyn_cast<TypeSourceInfo*>();
111 unsigned getFriendTypeNumTemplateParameterLists() const {
114 TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
115 assert(N < NumTPLists);
116 return getTrailingObjects<TemplateParameterList *>()[N];
119 /// If this friend declaration doesn't name a type, return the inner
121 NamedDecl *getFriendDecl() const {
122 return Friend.dyn_cast<NamedDecl*>();
125 /// Retrieves the location of the 'friend' keyword.
126 SourceLocation getFriendLoc() const {
130 /// Retrieves the source range for the friend declaration.
131 SourceRange getSourceRange() const override LLVM_READONLY {
132 if (NamedDecl *ND = getFriendDecl()) {
133 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
134 return FD->getSourceRange();
135 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
136 return FTD->getSourceRange();
137 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
138 return CTD->getSourceRange();
139 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(ND)) {
140 if (DD->getOuterLocStart() != DD->getInnerLocStart())
141 return DD->getSourceRange();
143 return SourceRange(getFriendLoc(), ND->getLocEnd());
145 else if (TypeSourceInfo *TInfo = getFriendType()) {
146 SourceLocation StartL =
147 (NumTPLists == 0) ? getFriendLoc()
148 : getTrailingObjects<TemplateParameterList *>()[0]
150 return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
153 return SourceRange(getFriendLoc(), getLocation());
156 /// Determines if this friend kind is unsupported.
157 bool isUnsupportedFriend() const {
158 return UnsupportedFriend;
160 void setUnsupportedFriend(bool Unsupported) {
161 UnsupportedFriend = Unsupported;
164 // Implement isa/cast/dyncast/etc.
165 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
166 static bool classofKind(Kind K) { return K == Decl::Friend; }
168 friend class ASTDeclReader;
169 friend class ASTDeclWriter;
170 friend class ASTNodeImporter;
171 friend TrailingObjects;
174 /// An iterator over the friend declarations of a class.
175 class CXXRecordDecl::friend_iterator {
178 friend class CXXRecordDecl;
179 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
183 typedef FriendDecl *value_type;
184 typedef FriendDecl *reference;
185 typedef FriendDecl *pointer;
186 typedef int difference_type;
187 typedef std::forward_iterator_tag iterator_category;
189 reference operator*() const { return Ptr; }
191 friend_iterator &operator++() {
192 assert(Ptr && "attempt to increment past end of friend list");
193 Ptr = Ptr->getNextFriend();
197 friend_iterator operator++(int) {
198 friend_iterator tmp = *this;
203 bool operator==(const friend_iterator &Other) const {
204 return Ptr == Other.Ptr;
207 bool operator!=(const friend_iterator &Other) const {
208 return Ptr != Other.Ptr;
211 friend_iterator &operator+=(difference_type N) {
212 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
218 friend_iterator operator+(difference_type N) const {
219 friend_iterator tmp = *this;
225 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
226 return friend_iterator(getFirstFriend());
229 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
230 return friend_iterator(nullptr);
233 inline CXXRecordDecl::friend_range CXXRecordDecl::friends() const {
234 return friend_range(friend_begin(), friend_end());
237 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
238 assert(!FD->NextFriend && "friend already has next friend?");
239 FD->NextFriend = data().FirstFriend;
240 data().FirstFriend = FD;