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),
63 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
64 SourceLocation L, FriendUnion Friend_,
65 SourceLocation FriendL);
67 /// If this friend declaration names an (untemplated but
68 /// possibly dependent) type, return the type; otherwise
69 /// return null. This is used only for C++0x's unelaborated
70 /// friend type declarations.
71 TypeSourceInfo *getFriendType() const {
72 return Friend.dyn_cast<TypeSourceInfo*>();
75 /// If this friend declaration doesn't name an unelaborated
76 /// type, return the inner declaration.
77 NamedDecl *getFriendDecl() const {
78 return Friend.dyn_cast<NamedDecl*>();
81 /// Retrieves the location of the 'friend' keyword.
82 SourceLocation getFriendLoc() const {
86 // Implement isa/cast/dyncast/etc.
87 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
88 static bool classof(const FriendDecl *D) { return true; }
89 static bool classofKind(Kind K) { return K == Decl::Friend; }
92 /// An iterator over the friend declarations of a class.
93 class CXXRecordDecl::friend_iterator {
96 friend class CXXRecordDecl;
97 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
101 typedef FriendDecl *value_type;
102 typedef FriendDecl *reference;
103 typedef FriendDecl *pointer;
104 typedef int difference_type;
105 typedef std::forward_iterator_tag iterator_category;
107 reference operator*() const { return Ptr; }
109 friend_iterator &operator++() {
110 assert(Ptr && "attempt to increment past end of friend list");
111 Ptr = Ptr->NextFriend;
115 friend_iterator operator++(int) {
116 friend_iterator tmp = *this;
121 bool operator==(const friend_iterator &Other) const {
122 return Ptr == Other.Ptr;
125 bool operator!=(const friend_iterator &Other) const {
126 return Ptr != Other.Ptr;
129 friend_iterator &operator+=(difference_type N) {
130 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
136 friend_iterator operator+(difference_type N) const {
137 friend_iterator tmp = *this;
143 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
144 return friend_iterator(data().FirstFriend);
147 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
148 return friend_iterator(0);
151 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
152 assert(FD->NextFriend == 0 && "friend already has next friend?");
153 FD->NextFriend = data().FirstFriend;
154 data().FirstFriend = FD;