DeclFriend.h revision 6102ca1d490836096678d7d934f0b2b78f9293ec
1//===-- DeclFriend.h - Classes for C++ friend declarations -*- C++ -*------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the section of the AST representing C++ friend
11// declarations.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_DECLFRIEND_H
16#define LLVM_CLANG_AST_DECLFRIEND_H
17
18#include "clang/AST/DeclCXX.h"
19
20namespace clang {
21
22/// FriendDecl - Represents the declaration of a friend entity,
23/// which can be a function, a type, or a templated function or type.
24//  For example:
25///
26/// @code
27/// template <typename T> class A {
28///   friend int foo(T);
29///   friend class B;
30///   friend T; // only in C++0x
31///   template <typename U> friend class C;
32///   template <typename U> friend A& operator+=(A&, const U&) { ... }
33/// };
34/// @endcode
35///
36/// The semantic context of a friend decl is its declaring class.
37class FriendDecl : public Decl {
38public:
39  typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
40
41private:
42  // The declaration that's a friend of this class.
43  FriendUnion Friend;
44
45  // A pointer to the next friend in the sequence.
46  FriendDecl *NextFriend;
47
48  // Location of the 'friend' specifier.
49  SourceLocation FriendLoc;
50
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;
55
56  friend class CXXRecordDecl::friend_iterator;
57  friend class CXXRecordDecl;
58
59  FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
60             SourceLocation FriendL)
61    : Decl(Decl::Friend, DC, L),
62      Friend(Friend),
63      NextFriend(0),
64      FriendLoc(FriendL),
65      UnsupportedFriend(false) {
66  }
67
68  explicit FriendDecl(EmptyShell Empty)
69    : Decl(Decl::Friend, Empty), NextFriend(0) { }
70
71public:
72  static FriendDecl *Create(ASTContext &C, DeclContext *DC,
73                            SourceLocation L, FriendUnion Friend_,
74                            SourceLocation FriendL);
75  static FriendDecl *Create(ASTContext &C, EmptyShell Empty);
76
77  /// If this friend declaration names an (untemplated but possibly
78  /// dependent) type, return the type; otherwise return null.  This
79  /// is used for elaborated-type-specifiers and, in C++0x, for
80  /// arbitrary friend type declarations.
81  TypeSourceInfo *getFriendType() const {
82    return Friend.dyn_cast<TypeSourceInfo*>();
83  }
84
85  /// If this friend declaration doesn't name a type, return the inner
86  /// declaration.
87  NamedDecl *getFriendDecl() const {
88    return Friend.dyn_cast<NamedDecl*>();
89  }
90
91  /// Retrieves the location of the 'friend' keyword.
92  SourceLocation getFriendLoc() const {
93    return FriendLoc;
94  }
95
96  /// Determines if this friend kind is unsupported.
97  bool isUnsupportedFriend() const {
98    return UnsupportedFriend;
99  }
100  void setUnsupportedFriend(bool Unsupported) {
101    UnsupportedFriend = Unsupported;
102  }
103
104  // Implement isa/cast/dyncast/etc.
105  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
106  static bool classof(const FriendDecl *D) { return true; }
107  static bool classofKind(Kind K) { return K == Decl::Friend; }
108
109  friend class ASTDeclReader;
110  friend class ASTDeclWriter;
111};
112
113/// An iterator over the friend declarations of a class.
114class CXXRecordDecl::friend_iterator {
115  FriendDecl *Ptr;
116
117  friend class CXXRecordDecl;
118  explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
119public:
120  friend_iterator() {}
121
122  typedef FriendDecl *value_type;
123  typedef FriendDecl *reference;
124  typedef FriendDecl *pointer;
125  typedef int difference_type;
126  typedef std::forward_iterator_tag iterator_category;
127
128  reference operator*() const { return Ptr; }
129
130  friend_iterator &operator++() {
131    assert(Ptr && "attempt to increment past end of friend list");
132    Ptr = Ptr->NextFriend;
133    return *this;
134  }
135
136  friend_iterator operator++(int) {
137    friend_iterator tmp = *this;
138    ++*this;
139    return tmp;
140  }
141
142  bool operator==(const friend_iterator &Other) const {
143    return Ptr == Other.Ptr;
144  }
145
146  bool operator!=(const friend_iterator &Other) const {
147    return Ptr != Other.Ptr;
148  }
149
150  friend_iterator &operator+=(difference_type N) {
151    assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
152    while (N--)
153      ++*this;
154    return *this;
155  }
156
157  friend_iterator operator+(difference_type N) const {
158    friend_iterator tmp = *this;
159    tmp += N;
160    return tmp;
161  }
162};
163
164inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
165  return friend_iterator(data().FirstFriend);
166}
167
168inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
169  return friend_iterator(0);
170}
171
172inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
173  assert(FD->NextFriend == 0 && "friend already has next friend?");
174  FD->NextFriend = data().FirstFriend;
175  data().FirstFriend = FD;
176}
177
178}
179
180#endif
181