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 "llvm/Support/Compiler.h"
24 /// FriendDecl - Represents the declaration of a friend entity,
25 /// which can be a function, a type, or a templated function or type.
29 /// template <typename T> class A {
30 /// friend int foo(T);
32 /// friend T; // only in C++0x
33 /// template <typename U> friend class C;
34 /// template <typename U> friend A& operator+=(A&, const U&) { ... }
38 /// The semantic context of a friend decl is its declaring class.
39 class FriendDecl : public Decl {
40 virtual void anchor();
42 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
45 // The declaration that's a friend of this class.
48 // A pointer to the next friend in the sequence.
49 LazyDeclPtr NextFriend;
51 // Location of the 'friend' specifier.
52 SourceLocation FriendLoc;
54 /// True if this 'friend' declaration is unsupported. Eventually we
55 /// will support every possible friend declaration, but for now we
56 /// silently ignore some and set this flag to authorize all access.
57 bool UnsupportedFriend : 1;
59 // The number of "outer" template parameter lists in non-templatic
60 // (currently unsupported) friend type declarations, such as
61 // template <class T> friend class A<T>::B;
62 unsigned NumTPLists : 31;
64 // The tail-allocated friend type template parameter lists (if any).
65 TemplateParameterList* const *getTPLists() const {
66 return reinterpret_cast<TemplateParameterList* const *>(this + 1);
68 TemplateParameterList **getTPLists() {
69 return reinterpret_cast<TemplateParameterList**>(this + 1);
72 friend class CXXRecordDecl::friend_iterator;
73 friend class CXXRecordDecl;
75 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
76 SourceLocation FriendL,
77 ArrayRef<TemplateParameterList*> FriendTypeTPLists)
78 : Decl(Decl::Friend, DC, L),
82 UnsupportedFriend(false),
83 NumTPLists(FriendTypeTPLists.size()) {
84 for (unsigned i = 0; i < NumTPLists; ++i)
85 getTPLists()[i] = FriendTypeTPLists[i];
88 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
89 : Decl(Decl::Friend, Empty), NextFriend(),
90 NumTPLists(NumFriendTypeTPLists) { }
92 FriendDecl *getNextFriend() {
93 if (!NextFriend.isOffset())
94 return cast_or_null<FriendDecl>(NextFriend.get(0));
95 return getNextFriendSlowCase();
97 FriendDecl *getNextFriendSlowCase();
100 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
101 SourceLocation L, FriendUnion Friend_,
102 SourceLocation FriendL,
103 ArrayRef<TemplateParameterList*> FriendTypeTPLists
104 = ArrayRef<TemplateParameterList*>());
105 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
106 unsigned FriendTypeNumTPLists);
108 /// If this friend declaration names an (untemplated but possibly
109 /// dependent) type, return the type; otherwise return null. This
110 /// is used for elaborated-type-specifiers and, in C++0x, for
111 /// arbitrary friend type declarations.
112 TypeSourceInfo *getFriendType() const {
113 return Friend.dyn_cast<TypeSourceInfo*>();
115 unsigned getFriendTypeNumTemplateParameterLists() const {
118 TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
119 assert(N < NumTPLists);
120 return getTPLists()[N];
123 /// If this friend declaration doesn't name a type, return the inner
125 NamedDecl *getFriendDecl() const {
126 return Friend.dyn_cast<NamedDecl*>();
129 /// Retrieves the location of the 'friend' keyword.
130 SourceLocation getFriendLoc() const {
134 /// Retrieves the source range for the friend declaration.
135 SourceRange getSourceRange() const LLVM_READONLY {
136 if (NamedDecl *ND = getFriendDecl()) {
137 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
138 return FTD->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 = (NumTPLists == 0)
148 : getTPLists()[0]->getTemplateLoc();
149 return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
152 return SourceRange(getFriendLoc(), getLocation());
155 /// Determines if this friend kind is unsupported.
156 bool isUnsupportedFriend() const {
157 return UnsupportedFriend;
159 void setUnsupportedFriend(bool Unsupported) {
160 UnsupportedFriend = Unsupported;
163 // Implement isa/cast/dyncast/etc.
164 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
165 static bool classofKind(Kind K) { return K == Decl::Friend; }
167 friend class ASTDeclReader;
168 friend class ASTDeclWriter;
171 /// An iterator over the friend declarations of a class.
172 class CXXRecordDecl::friend_iterator {
175 friend class CXXRecordDecl;
176 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
180 typedef FriendDecl *value_type;
181 typedef FriendDecl *reference;
182 typedef FriendDecl *pointer;
183 typedef int difference_type;
184 typedef std::forward_iterator_tag iterator_category;
186 reference operator*() const { return Ptr; }
188 friend_iterator &operator++() {
189 assert(Ptr && "attempt to increment past end of friend list");
190 Ptr = Ptr->getNextFriend();
194 friend_iterator operator++(int) {
195 friend_iterator tmp = *this;
200 bool operator==(const friend_iterator &Other) const {
201 return Ptr == Other.Ptr;
204 bool operator!=(const friend_iterator &Other) const {
205 return Ptr != Other.Ptr;
208 friend_iterator &operator+=(difference_type N) {
209 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
215 friend_iterator operator+(difference_type N) const {
216 friend_iterator tmp = *this;
222 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
223 return friend_iterator(data().FirstFriend);
226 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
227 return friend_iterator(0);
230 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
231 assert(FD->NextFriend == 0 && "friend already has next friend?");
232 FD->NextFriend = data().FirstFriend;
233 data().FirstFriend = FD;