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