clang  3.7.0
DeclFriend.h
Go to the documentation of this file.
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 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/TypeLoc.h"
21 #include "llvm/Support/Compiler.h"
22 
23 namespace clang {
24 
25 /// FriendDecl - Represents the declaration of a friend entity,
26 /// which can be a function, a type, or a templated function or type.
27 // For example:
28 ///
29 /// @code
30 /// template <typename T> class A {
31 /// friend int foo(T);
32 /// friend class B;
33 /// friend T; // only in C++0x
34 /// template <typename U> friend class C;
35 /// template <typename U> friend A& operator+=(A&, const U&) { ... }
36 /// };
37 /// @endcode
38 ///
39 /// The semantic context of a friend decl is its declaring class.
40 class FriendDecl : public Decl {
41  virtual void anchor();
42 public:
43  typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
44 
45 private:
46  // The declaration that's a friend of this class.
47  FriendUnion Friend;
48 
49  // A pointer to the next friend in the sequence.
50  LazyDeclPtr NextFriend;
51 
52  // Location of the 'friend' specifier.
53  SourceLocation FriendLoc;
54 
55  /// True if this 'friend' declaration is unsupported. Eventually we
56  /// will support every possible friend declaration, but for now we
57  /// silently ignore some and set this flag to authorize all access.
58  bool UnsupportedFriend : 1;
59 
60  // The number of "outer" template parameter lists in non-templatic
61  // (currently unsupported) friend type declarations, such as
62  // template <class T> friend class A<T>::B;
63  unsigned NumTPLists : 31;
64 
65  // The tail-allocated friend type template parameter lists (if any).
66  TemplateParameterList* const *getTPLists() const {
67  return reinterpret_cast<TemplateParameterList* const *>(this + 1);
68  }
69  TemplateParameterList **getTPLists() {
70  return reinterpret_cast<TemplateParameterList**>(this + 1);
71  }
72 
74  friend class CXXRecordDecl;
75 
77  SourceLocation FriendL,
78  ArrayRef<TemplateParameterList*> FriendTypeTPLists)
79  : Decl(Decl::Friend, DC, L),
80  Friend(Friend),
81  NextFriend(),
82  FriendLoc(FriendL),
83  UnsupportedFriend(false),
84  NumTPLists(FriendTypeTPLists.size()) {
85  for (unsigned i = 0; i < NumTPLists; ++i)
86  getTPLists()[i] = FriendTypeTPLists[i];
87  }
88 
89  FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
90  : Decl(Decl::Friend, Empty), NextFriend(),
91  NumTPLists(NumFriendTypeTPLists) { }
92 
93  FriendDecl *getNextFriend() {
94  if (!NextFriend.isOffset())
95  return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
96  return getNextFriendSlowCase();
97  }
98  FriendDecl *getNextFriendSlowCase();
99 
100 public:
101  static FriendDecl *Create(ASTContext &C, DeclContext *DC,
102  SourceLocation L, FriendUnion Friend_,
103  SourceLocation FriendL,
104  ArrayRef<TemplateParameterList*> FriendTypeTPLists
105  = None);
106  static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
107  unsigned FriendTypeNumTPLists);
108 
109  /// If this friend declaration names an (untemplated but possibly
110  /// dependent) type, return the type; otherwise return null. This
111  /// is used for elaborated-type-specifiers and, in C++0x, for
112  /// arbitrary friend type declarations.
114  return Friend.dyn_cast<TypeSourceInfo*>();
115  }
117  return NumTPLists;
118  }
120  assert(N < NumTPLists);
121  return getTPLists()[N];
122  }
123 
124  /// If this friend declaration doesn't name a type, return the inner
125  /// declaration.
127  return Friend.dyn_cast<NamedDecl*>();
128  }
129 
130  /// Retrieves the location of the 'friend' keyword.
132  return FriendLoc;
133  }
134 
135  /// Retrieves the source range for the friend declaration.
136  SourceRange getSourceRange() const override LLVM_READONLY {
137  if (NamedDecl *ND = getFriendDecl()) {
138  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
139  return FD->getSourceRange();
140  if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
141  return FTD->getSourceRange();
142  if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
143  return CTD->getSourceRange();
144  if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(ND)) {
145  if (DD->getOuterLocStart() != DD->getInnerLocStart())
146  return DD->getSourceRange();
147  }
148  return SourceRange(getFriendLoc(), ND->getLocEnd());
149  }
150  else if (TypeSourceInfo *TInfo = getFriendType()) {
151  SourceLocation StartL = (NumTPLists == 0)
152  ? getFriendLoc()
153  : getTPLists()[0]->getTemplateLoc();
154  return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
155  }
156  else
157  return SourceRange(getFriendLoc(), getLocation());
158  }
159 
160  /// Determines if this friend kind is unsupported.
161  bool isUnsupportedFriend() const {
162  return UnsupportedFriend;
163  }
165  UnsupportedFriend = Unsupported;
166  }
167 
168  // Implement isa/cast/dyncast/etc.
169  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
170  static bool classofKind(Kind K) { return K == Decl::Friend; }
171 
172  friend class ASTDeclReader;
173  friend class ASTDeclWriter;
174 };
175 
176 /// An iterator over the friend declarations of a class.
178  FriendDecl *Ptr;
179 
180  friend class CXXRecordDecl;
181  explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
182 public:
184 
187  typedef FriendDecl *pointer;
188  typedef int difference_type;
189  typedef std::forward_iterator_tag iterator_category;
190 
191  reference operator*() const { return Ptr; }
192 
194  assert(Ptr && "attempt to increment past end of friend list");
195  Ptr = Ptr->getNextFriend();
196  return *this;
197  }
198 
200  friend_iterator tmp = *this;
201  ++*this;
202  return tmp;
203  }
204 
205  bool operator==(const friend_iterator &Other) const {
206  return Ptr == Other.Ptr;
207  }
208 
209  bool operator!=(const friend_iterator &Other) const {
210  return Ptr != Other.Ptr;
211  }
212 
214  assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
215  while (N--)
216  ++*this;
217  return *this;
218  }
219 
221  friend_iterator tmp = *this;
222  tmp += N;
223  return tmp;
224  }
225 };
226 
228  return friend_iterator(getFirstFriend());
229 }
230 
232  return friend_iterator(nullptr);
233 }
234 
236  return friend_range(friend_begin(), friend_end());
237 }
238 
240  assert(!FD->NextFriend && "friend already has next friend?");
241  FD->NextFriend = data().FirstFriend;
242  data().FirstFriend = FD;
243 }
244 
245 }
246 
247 #endif
TemplateParameterList * getFriendTypeTemplateParameterList(unsigned N) const
Definition: DeclFriend.h:119
bool isOffset() const
Whether this pointer is currently stored as an offset.
Defines the C++ template declaration subclasses.
friend_range friends() const
Definition: DeclFriend.h:235
static FriendDecl * CreateDeserialized(ASTContext &C, unsigned ID, unsigned FriendTypeNumTPLists)
Definition: DeclFriend.cpp:56
A container of type source information.
Definition: Decl.h:60
static FriendDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation L, FriendUnion Friend_, SourceLocation FriendL, ArrayRef< TemplateParameterList * > FriendTypeTPLists=None)
Definition: DeclFriend.cpp:27
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:46
llvm::iterator_range< friend_iterator > friend_range
Definition: DeclCXX.h:785
TypeSourceInfo * getFriendType() const
Definition: DeclFriend.h:113
An iterator over the friend declarations of a class.
Definition: DeclFriend.h:177
unsigned getFriendTypeNumTemplateParameterLists() const
Definition: DeclFriend.h:116
friend class DeclContext
Definition: DeclBase.h:211
llvm::PointerUnion< NamedDecl *, TypeSourceInfo * > FriendUnion
Definition: DeclFriend.h:43
friend_iterator & operator+=(difference_type N)
Definition: DeclFriend.h:213
friend_iterator friend_end() const
Definition: DeclFriend.h:231
std::forward_iterator_tag iterator_category
Definition: DeclFriend.h:189
Represents a ValueDecl that came out of a declarator. Contains type source information through TypeSo...
Definition: Decl.h:586
ID
Defines the set of possible language-specific address spaces.
Definition: AddressSpaces.h:27
friend_iterator friend_begin() const
Definition: DeclFriend.h:227
Kind getKind() const
Definition: DeclBase.h:375
Defines the clang::TypeLoc interface and its subclasses.
static bool classof(const Decl *D)
Definition: DeclFriend.h:169
SourceLocation getFriendLoc() const
Retrieves the location of the 'friend' keyword.
Definition: DeclFriend.h:131
#define false
Definition: stdbool.h:33
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
bool operator==(const friend_iterator &Other) const
Definition: DeclFriend.h:205
T * get(ExternalASTSource *Source) const
Retrieve the pointer to the AST node that this lazy pointer.
static bool classofKind(Kind K)
Definition: DeclFriend.h:170
bool isUnsupportedFriend() const
Determines if this friend kind is unsupported.
Definition: DeclFriend.h:161
NamedDecl * getFriendDecl() const
Definition: DeclFriend.h:126
friend_iterator operator+(difference_type N) const
Definition: DeclFriend.h:220
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Represents a C++ struct/union/class.
Definition: DeclCXX.h:285
void setUnsupportedFriend(bool Unsupported)
Definition: DeclFriend.h:164
Declaration of a class template.
Kind
Lists the kind of concrete classes of Decl.
Definition: DeclBase.h:76
SourceRange getSourceRange() const override LLVM_READONLY
Retrieves the source range for the friend declaration.
Definition: DeclFriend.h:136
A trivial tuple used to represent a source range.
SourceLocation getLocation() const
Definition: DeclBase.h:372
bool operator!=(const friend_iterator &Other) const
Definition: DeclFriend.h:209
Declaration of a template function.
Definition: DeclTemplate.h:821
void pushFriendDecl(FriendDecl *FD)
Definition: DeclFriend.h:239
SourceLocation getTemplateLoc() const
Definition: DeclTemplate.h:126