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