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