DeclFriend.h revision 99ba9e3bd70671f3441fb974895f226a83ce0e66
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 {
38  virtual void anchor();
39public:
40  typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
41
42private:
43  // The declaration that's a friend of this class.
44  FriendUnion Friend;
45
46  // A pointer to the next friend in the sequence.
47  LazyDeclPtr NextFriend;
48
49  // Location of the 'friend' specifier.
50  SourceLocation FriendLoc;
51
52  /// True if this 'friend' declaration is unsupported.  Eventually we
53  /// will support every possible friend declaration, but for now we
54  /// silently ignore some and set this flag to authorize all access.
55  bool UnsupportedFriend;
56
57  friend class CXXRecordDecl::friend_iterator;
58  friend class CXXRecordDecl;
59
60  FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
61             SourceLocation FriendL)
62    : Decl(Decl::Friend, DC, L),
63      Friend(Friend),
64      NextFriend(),
65      FriendLoc(FriendL),
66      UnsupportedFriend(false) {
67  }
68
69  explicit FriendDecl(EmptyShell Empty)
70    : Decl(Decl::Friend, Empty), NextFriend() { }
71
72  FriendDecl *getNextFriend() {
73    return cast_or_null<FriendDecl>(
74                          NextFriend.get(getASTContext().getExternalSource()));
75  }
76
77public:
78  static FriendDecl *Create(ASTContext &C, DeclContext *DC,
79                            SourceLocation L, FriendUnion Friend_,
80                            SourceLocation FriendL);
81  static FriendDecl *Create(ASTContext &C, EmptyShell Empty);
82
83  /// If this friend declaration names an (untemplated but possibly
84  /// dependent) type, return the type; otherwise return null.  This
85  /// is used for elaborated-type-specifiers and, in C++0x, for
86  /// arbitrary friend type declarations.
87  TypeSourceInfo *getFriendType() const {
88    return Friend.dyn_cast<TypeSourceInfo*>();
89  }
90
91  /// If this friend declaration doesn't name a type, return the inner
92  /// declaration.
93  NamedDecl *getFriendDecl() const {
94    return Friend.dyn_cast<NamedDecl*>();
95  }
96
97  /// Retrieves the location of the 'friend' keyword.
98  SourceLocation getFriendLoc() const {
99    return FriendLoc;
100  }
101
102  /// Retrieves the source range for the friend declaration.
103  SourceRange getSourceRange() const {
104    /* FIXME: consider the case of templates wrt start of range. */
105    if (NamedDecl *ND = getFriendDecl())
106      return SourceRange(getFriendLoc(), ND->getLocEnd());
107    else if (TypeSourceInfo *TInfo = getFriendType())
108      return SourceRange(getFriendLoc(), TInfo->getTypeLoc().getEndLoc());
109    else
110      return SourceRange(getFriendLoc(), getLocation());
111  }
112
113  /// Determines if this friend kind is unsupported.
114  bool isUnsupportedFriend() const {
115    return UnsupportedFriend;
116  }
117  void setUnsupportedFriend(bool Unsupported) {
118    UnsupportedFriend = Unsupported;
119  }
120
121  // Implement isa/cast/dyncast/etc.
122  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
123  static bool classof(const FriendDecl *D) { return true; }
124  static bool classofKind(Kind K) { return K == Decl::Friend; }
125
126  friend class ASTDeclReader;
127  friend class ASTDeclWriter;
128};
129
130/// An iterator over the friend declarations of a class.
131class CXXRecordDecl::friend_iterator {
132  FriendDecl *Ptr;
133
134  friend class CXXRecordDecl;
135  explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
136public:
137  friend_iterator() {}
138
139  typedef FriendDecl *value_type;
140  typedef FriendDecl *reference;
141  typedef FriendDecl *pointer;
142  typedef int difference_type;
143  typedef std::forward_iterator_tag iterator_category;
144
145  reference operator*() const { return Ptr; }
146
147  friend_iterator &operator++() {
148    assert(Ptr && "attempt to increment past end of friend list");
149    Ptr = Ptr->getNextFriend();
150    return *this;
151  }
152
153  friend_iterator operator++(int) {
154    friend_iterator tmp = *this;
155    ++*this;
156    return tmp;
157  }
158
159  bool operator==(const friend_iterator &Other) const {
160    return Ptr == Other.Ptr;
161  }
162
163  bool operator!=(const friend_iterator &Other) const {
164    return Ptr != Other.Ptr;
165  }
166
167  friend_iterator &operator+=(difference_type N) {
168    assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
169    while (N--)
170      ++*this;
171    return *this;
172  }
173
174  friend_iterator operator+(difference_type N) const {
175    friend_iterator tmp = *this;
176    tmp += N;
177    return tmp;
178  }
179};
180
181inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
182  return friend_iterator(data().FirstFriend);
183}
184
185inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
186  return friend_iterator(0);
187}
188
189inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
190  assert(FD->NextFriend == 0 && "friend already has next friend?");
191  FD->NextFriend = data().FirstFriend;
192  data().FirstFriend = FD;
193}
194
195}
196
197#endif
198