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"
22 /// FriendDecl - Represents the declaration of a friend entity,
23 /// which can be a function, a type, or a templated function or type.
27 /// template <typename T> class A {
28 /// friend int foo(T);
30 /// friend T; // only in C++0x
31 /// template <typename U> friend class C;
32 /// template <typename U> friend A& operator+=(A&, const U&) { ... }
36 /// The semantic context of a friend decl is its declaring class.
37 class FriendDecl : public Decl {
39 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
42 // The declaration that's a friend of this class.
45 // A pointer to the next friend in the sequence.
46 LazyDeclPtr NextFriend;
48 // Location of the 'friend' specifier.
49 SourceLocation FriendLoc;
51 /// True if this 'friend' declaration is unsupported. Eventually we
52 /// will support every possible friend declaration, but for now we
53 /// silently ignore some and set this flag to authorize all access.
54 bool UnsupportedFriend;
56 friend class CXXRecordDecl::friend_iterator;
57 friend class CXXRecordDecl;
59 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
60 SourceLocation FriendL)
61 : Decl(Decl::Friend, DC, L),
65 UnsupportedFriend(false) {
68 explicit FriendDecl(EmptyShell Empty)
69 : Decl(Decl::Friend, Empty), NextFriend() { }
71 FriendDecl *getNextFriend() {
72 return cast_or_null<FriendDecl>(
73 NextFriend.get(getASTContext().getExternalSource()));
77 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
78 SourceLocation L, FriendUnion Friend_,
79 SourceLocation FriendL);
80 static FriendDecl *Create(ASTContext &C, EmptyShell Empty);
82 /// If this friend declaration names an (untemplated but possibly
83 /// dependent) type, return the type; otherwise return null. This
84 /// is used for elaborated-type-specifiers and, in C++0x, for
85 /// arbitrary friend type declarations.
86 TypeSourceInfo *getFriendType() const {
87 return Friend.dyn_cast<TypeSourceInfo*>();
90 /// If this friend declaration doesn't name a type, return the inner
92 NamedDecl *getFriendDecl() const {
93 return Friend.dyn_cast<NamedDecl*>();
96 /// Retrieves the location of the 'friend' keyword.
97 SourceLocation getFriendLoc() const {
101 /// Retrieves the source range for the friend declaration.
102 SourceRange getSourceRange() const {
103 /* FIXME: consider the case of templates wrt start of range. */
104 if (NamedDecl *ND = getFriendDecl())
105 return SourceRange(getFriendLoc(), ND->getLocEnd());
106 else if (TypeSourceInfo *TInfo = getFriendType())
107 return SourceRange(getFriendLoc(), TInfo->getTypeLoc().getEndLoc());
109 return SourceRange(getFriendLoc(), getLocation());
112 /// Determines if this friend kind is unsupported.
113 bool isUnsupportedFriend() const {
114 return UnsupportedFriend;
116 void setUnsupportedFriend(bool Unsupported) {
117 UnsupportedFriend = Unsupported;
120 // Implement isa/cast/dyncast/etc.
121 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
122 static bool classof(const FriendDecl *D) { return true; }
123 static bool classofKind(Kind K) { return K == Decl::Friend; }
125 friend class ASTDeclReader;
126 friend class ASTDeclWriter;
129 /// An iterator over the friend declarations of a class.
130 class CXXRecordDecl::friend_iterator {
133 friend class CXXRecordDecl;
134 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
138 typedef FriendDecl *value_type;
139 typedef FriendDecl *reference;
140 typedef FriendDecl *pointer;
141 typedef int difference_type;
142 typedef std::forward_iterator_tag iterator_category;
144 reference operator*() const { return Ptr; }
146 friend_iterator &operator++() {
147 assert(Ptr && "attempt to increment past end of friend list");
148 Ptr = Ptr->getNextFriend();
152 friend_iterator operator++(int) {
153 friend_iterator tmp = *this;
158 bool operator==(const friend_iterator &Other) const {
159 return Ptr == Other.Ptr;
162 bool operator!=(const friend_iterator &Other) const {
163 return Ptr != Other.Ptr;
166 friend_iterator &operator+=(difference_type N) {
167 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
173 friend_iterator operator+(difference_type N) const {
174 friend_iterator tmp = *this;
180 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
181 return friend_iterator(data().FirstFriend);
184 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
185 return friend_iterator(0);
188 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
189 assert(FD->NextFriend == 0 && "friend already has next friend?");
190 FD->NextFriend = data().FirstFriend;
191 data().FirstFriend = FD;