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 FriendDecl *NextFriend;
48 // Location of the 'friend' specifier.
49 SourceLocation FriendLoc;
51 friend class CXXRecordDecl::friend_iterator;
52 friend class CXXRecordDecl;
54 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
55 SourceLocation FriendL)
56 : Decl(Decl::Friend, DC, L),
62 explicit FriendDecl(EmptyShell Empty)
63 : Decl(Decl::Friend, Empty), NextFriend(0) { }
66 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
67 SourceLocation L, FriendUnion Friend_,
68 SourceLocation FriendL);
69 static FriendDecl *Create(ASTContext &C, EmptyShell Empty);
71 /// If this friend declaration names an (untemplated but possibly
72 /// dependent) type, return the type; otherwise return null. This
73 /// is used for elaborated-type-specifiers and, in C++0x, for
74 /// arbitrary friend type declarations.
75 TypeSourceInfo *getFriendType() const {
76 return Friend.dyn_cast<TypeSourceInfo*>();
79 /// If this friend declaration doesn't name a type, return the inner
81 NamedDecl *getFriendDecl() const {
82 return Friend.dyn_cast<NamedDecl*>();
85 /// Retrieves the location of the 'friend' keyword.
86 SourceLocation getFriendLoc() const {
90 // Implement isa/cast/dyncast/etc.
91 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
92 static bool classof(const FriendDecl *D) { return true; }
93 static bool classofKind(Kind K) { return K == Decl::Friend; }
95 friend class ASTDeclReader;
96 friend class ASTDeclWriter;
99 /// An iterator over the friend declarations of a class.
100 class CXXRecordDecl::friend_iterator {
103 friend class CXXRecordDecl;
104 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
108 typedef FriendDecl *value_type;
109 typedef FriendDecl *reference;
110 typedef FriendDecl *pointer;
111 typedef int difference_type;
112 typedef std::forward_iterator_tag iterator_category;
114 reference operator*() const { return Ptr; }
116 friend_iterator &operator++() {
117 assert(Ptr && "attempt to increment past end of friend list");
118 Ptr = Ptr->NextFriend;
122 friend_iterator operator++(int) {
123 friend_iterator tmp = *this;
128 bool operator==(const friend_iterator &Other) const {
129 return Ptr == Other.Ptr;
132 bool operator!=(const friend_iterator &Other) const {
133 return Ptr != Other.Ptr;
136 friend_iterator &operator+=(difference_type N) {
137 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
143 friend_iterator operator+(difference_type N) const {
144 friend_iterator tmp = *this;
150 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
151 return friend_iterator(data().FirstFriend);
154 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
155 return friend_iterator(0);
158 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
159 assert(FD->NextFriend == 0 && "friend already has next friend?");
160 FD->NextFriend = data().FirstFriend;
161 data().FirstFriend = FD;