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;
59 friend class CXXRecordDecl::friend_iterator;
60 friend class CXXRecordDecl;
62 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
63 SourceLocation FriendL)
64 : Decl(Decl::Friend, DC, L),
68 UnsupportedFriend(false) {
71 explicit FriendDecl(EmptyShell Empty)
72 : Decl(Decl::Friend, Empty), NextFriend() { }
74 FriendDecl *getNextFriend() {
75 if (!NextFriend.isOffset())
76 return cast_or_null<FriendDecl>(NextFriend.get(0));
77 return getNextFriendSlowCase();
79 FriendDecl *getNextFriendSlowCase();
82 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
83 SourceLocation L, FriendUnion Friend_,
84 SourceLocation FriendL);
85 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID);
87 /// If this friend declaration names an (untemplated but possibly
88 /// dependent) type, return the type; otherwise return null. This
89 /// is used for elaborated-type-specifiers and, in C++0x, for
90 /// arbitrary friend type declarations.
91 TypeSourceInfo *getFriendType() const {
92 return Friend.dyn_cast<TypeSourceInfo*>();
95 /// If this friend declaration doesn't name a type, return the inner
97 NamedDecl *getFriendDecl() const {
98 return Friend.dyn_cast<NamedDecl*>();
101 /// Retrieves the location of the 'friend' keyword.
102 SourceLocation getFriendLoc() const {
106 /// Retrieves the source range for the friend declaration.
107 SourceRange getSourceRange() const LLVM_READONLY {
108 if (NamedDecl *ND = getFriendDecl()) {
109 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
110 return FTD->getSourceRange();
111 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(ND)) {
112 if (DD->getOuterLocStart() != DD->getInnerLocStart())
113 return DD->getSourceRange();
115 return SourceRange(getFriendLoc(), ND->getLocEnd());
117 else if (TypeSourceInfo *TInfo = getFriendType())
118 return SourceRange(getFriendLoc(), TInfo->getTypeLoc().getEndLoc());
120 return SourceRange(getFriendLoc(), getLocation());
123 /// Determines if this friend kind is unsupported.
124 bool isUnsupportedFriend() const {
125 return UnsupportedFriend;
127 void setUnsupportedFriend(bool Unsupported) {
128 UnsupportedFriend = Unsupported;
131 // Implement isa/cast/dyncast/etc.
132 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
133 static bool classofKind(Kind K) { return K == Decl::Friend; }
135 friend class ASTDeclReader;
136 friend class ASTDeclWriter;
139 /// An iterator over the friend declarations of a class.
140 class CXXRecordDecl::friend_iterator {
143 friend class CXXRecordDecl;
144 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
148 typedef FriendDecl *value_type;
149 typedef FriendDecl *reference;
150 typedef FriendDecl *pointer;
151 typedef int difference_type;
152 typedef std::forward_iterator_tag iterator_category;
154 reference operator*() const { return Ptr; }
156 friend_iterator &operator++() {
157 assert(Ptr && "attempt to increment past end of friend list");
158 Ptr = Ptr->getNextFriend();
162 friend_iterator operator++(int) {
163 friend_iterator tmp = *this;
168 bool operator==(const friend_iterator &Other) const {
169 return Ptr == Other.Ptr;
172 bool operator!=(const friend_iterator &Other) const {
173 return Ptr != Other.Ptr;
176 friend_iterator &operator+=(difference_type N) {
177 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
183 friend_iterator operator+(difference_type N) const {
184 friend_iterator tmp = *this;
190 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
191 return friend_iterator(data().FirstFriend);
194 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
195 return friend_iterator(0);
198 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
199 assert(FD->NextFriend == 0 && "friend already has next friend?");
200 FD->NextFriend = data().FirstFriend;
201 data().FirstFriend = FD;