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 "llvm/Support/Compiler.h"
23 /// FriendDecl - Represents the declaration of a friend entity,
24 /// which can be a function, a type, or a templated function or type.
28 /// template <typename T> class A {
29 /// friend int foo(T);
31 /// friend T; // only in C++0x
32 /// template <typename U> friend class C;
33 /// template <typename U> friend A& operator+=(A&, const U&) { ... }
37 /// The semantic context of a friend decl is its declaring class.
38 class FriendDecl : public Decl {
39 virtual void anchor();
41 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
44 // The declaration that's a friend of this class.
47 // A pointer to the next friend in the sequence.
48 LazyDeclPtr NextFriend;
50 // Location of the 'friend' specifier.
51 SourceLocation FriendLoc;
53 /// True if this 'friend' declaration is unsupported. Eventually we
54 /// will support every possible friend declaration, but for now we
55 /// silently ignore some and set this flag to authorize all access.
56 bool UnsupportedFriend;
58 friend class CXXRecordDecl::friend_iterator;
59 friend class CXXRecordDecl;
61 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
62 SourceLocation FriendL)
63 : Decl(Decl::Friend, DC, L),
67 UnsupportedFriend(false) {
70 explicit FriendDecl(EmptyShell Empty)
71 : Decl(Decl::Friend, Empty), NextFriend() { }
73 FriendDecl *getNextFriend() {
74 return cast_or_null<FriendDecl>(
75 NextFriend.get(getASTContext().getExternalSource()));
79 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
80 SourceLocation L, FriendUnion Friend_,
81 SourceLocation FriendL);
82 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID);
84 /// If this friend declaration names an (untemplated but possibly
85 /// dependent) type, return the type; otherwise return null. This
86 /// is used for elaborated-type-specifiers and, in C++0x, for
87 /// arbitrary friend type declarations.
88 TypeSourceInfo *getFriendType() const {
89 return Friend.dyn_cast<TypeSourceInfo*>();
92 /// If this friend declaration doesn't name a type, return the inner
94 NamedDecl *getFriendDecl() const {
95 return Friend.dyn_cast<NamedDecl*>();
98 /// Retrieves the location of the 'friend' keyword.
99 SourceLocation getFriendLoc() const {
103 /// Retrieves the source range for the friend declaration.
104 SourceRange getSourceRange() const LLVM_READONLY {
105 /* FIXME: consider the case of templates wrt start of range. */
106 if (NamedDecl *ND = getFriendDecl())
107 return SourceRange(getFriendLoc(), ND->getLocEnd());
108 else if (TypeSourceInfo *TInfo = getFriendType())
109 return SourceRange(getFriendLoc(), TInfo->getTypeLoc().getEndLoc());
111 return SourceRange(getFriendLoc(), getLocation());
114 /// Determines if this friend kind is unsupported.
115 bool isUnsupportedFriend() const {
116 return UnsupportedFriend;
118 void setUnsupportedFriend(bool Unsupported) {
119 UnsupportedFriend = Unsupported;
122 // Implement isa/cast/dyncast/etc.
123 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
124 static bool classof(const FriendDecl *D) { return true; }
125 static bool classofKind(Kind K) { return K == Decl::Friend; }
127 friend class ASTDeclReader;
128 friend class ASTDeclWriter;
131 /// An iterator over the friend declarations of a class.
132 class CXXRecordDecl::friend_iterator {
135 friend class CXXRecordDecl;
136 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
140 typedef FriendDecl *value_type;
141 typedef FriendDecl *reference;
142 typedef FriendDecl *pointer;
143 typedef int difference_type;
144 typedef std::forward_iterator_tag iterator_category;
146 reference operator*() const { return Ptr; }
148 friend_iterator &operator++() {
149 assert(Ptr && "attempt to increment past end of friend list");
150 Ptr = Ptr->getNextFriend();
154 friend_iterator operator++(int) {
155 friend_iterator tmp = *this;
160 bool operator==(const friend_iterator &Other) const {
161 return Ptr == Other.Ptr;
164 bool operator!=(const friend_iterator &Other) const {
165 return Ptr != Other.Ptr;
168 friend_iterator &operator+=(difference_type N) {
169 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
175 friend_iterator operator+(difference_type N) const {
176 friend_iterator tmp = *this;
182 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
183 return friend_iterator(data().FirstFriend);
186 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
187 return friend_iterator(0);
190 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
191 assert(FD->NextFriend == 0 && "friend already has next friend?");
192 FD->NextFriend = data().FirstFriend;
193 data().FirstFriend = FD;