clang  3.9.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  NumTPLists(NumFriendTypeTPLists) { }
86 
87  FriendDecl *getNextFriend() {
88  if (!NextFriend.isOffset())
89  return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
90  return getNextFriendSlowCase();
91  }
92  FriendDecl *getNextFriendSlowCase();
93 
94 public:
95  static FriendDecl *Create(ASTContext &C, DeclContext *DC,
96  SourceLocation L, FriendUnion Friend_,
97  SourceLocation FriendL,
98  ArrayRef<TemplateParameterList*> FriendTypeTPLists
99  = None);
100  static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
101  unsigned FriendTypeNumTPLists);
102 
103  /// If this friend declaration names an (untemplated but possibly
104  /// dependent) type, return the type; otherwise return null. This
105  /// is used for elaborated-type-specifiers and, in C++0x, for
106  /// arbitrary friend type declarations.
108  return Friend.dyn_cast<TypeSourceInfo*>();
109  }
111  return NumTPLists;
112  }
114  assert(N < NumTPLists);
115  return getTrailingObjects<TemplateParameterList *>()[N];
116  }
117 
118  /// If this friend declaration doesn't name a type, return the inner
119  /// declaration.
121  return Friend.dyn_cast<NamedDecl*>();
122  }
123 
124  /// Retrieves the location of the 'friend' keyword.
126  return FriendLoc;
127  }
128 
129  /// Retrieves the source range for the friend declaration.
130  SourceRange getSourceRange() const override LLVM_READONLY {
131  if (NamedDecl *ND = getFriendDecl()) {
132  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
133  return FD->getSourceRange();
134  if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
135  return FTD->getSourceRange();
136  if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
137  return CTD->getSourceRange();
138  if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(ND)) {
139  if (DD->getOuterLocStart() != DD->getInnerLocStart())
140  return DD->getSourceRange();
141  }
142  return SourceRange(getFriendLoc(), ND->getLocEnd());
143  }
144  else if (TypeSourceInfo *TInfo = getFriendType()) {
145  SourceLocation StartL =
146  (NumTPLists == 0) ? getFriendLoc()
147  : getTrailingObjects<TemplateParameterList *>()[0]
148  ->getTemplateLoc();
149  return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
150  }
151  else
152  return SourceRange(getFriendLoc(), getLocation());
153  }
154 
155  /// Determines if this friend kind is unsupported.
156  bool isUnsupportedFriend() const {
157  return UnsupportedFriend;
158  }
160  UnsupportedFriend = Unsupported;
161  }
162 
163  // Implement isa/cast/dyncast/etc.
164  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
165  static bool classofKind(Kind K) { return K == Decl::Friend; }
166 
167  friend class ASTDeclReader;
168  friend class ASTDeclWriter;
170 };
171 
172 /// An iterator over the friend declarations of a class.
174  FriendDecl *Ptr;
175 
176  friend class CXXRecordDecl;
177  explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
178 public:
180 
183  typedef FriendDecl *pointer;
184  typedef int difference_type;
185  typedef std::forward_iterator_tag iterator_category;
186 
187  reference operator*() const { return Ptr; }
188 
190  assert(Ptr && "attempt to increment past end of friend list");
191  Ptr = Ptr->getNextFriend();
192  return *this;
193  }
194 
196  friend_iterator tmp = *this;
197  ++*this;
198  return tmp;
199  }
200 
201  bool operator==(const friend_iterator &Other) const {
202  return Ptr == Other.Ptr;
203  }
204 
205  bool operator!=(const friend_iterator &Other) const {
206  return Ptr != Other.Ptr;
207  }
208 
210  assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
211  while (N--)
212  ++*this;
213  return *this;
214  }
215 
217  friend_iterator tmp = *this;
218  tmp += N;
219  return tmp;
220  }
221 };
222 
224  return friend_iterator(getFirstFriend());
225 }
226 
228  return friend_iterator(nullptr);
229 }
230 
232  return friend_range(friend_begin(), friend_end());
233 }
234 
236  assert(!FD->NextFriend && "friend already has next friend?");
237  FD->NextFriend = data().FirstFriend;
238  data().FirstFriend = FD;
239 }
240 
241 }
242 
243 #endif
TemplateParameterList * getFriendTypeTemplateParameterList(unsigned N) const
Definition: DeclFriend.h:113
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
friend TrailingObjects
Definition: DeclFriend.h:169
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:231
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:49
llvm::iterator_range< friend_iterator > friend_range
Definition: DeclCXX.h:790
TypeSourceInfo * getFriendType() const
If this friend declaration names an (untemplated but possibly dependent) type, return the type; other...
Definition: DeclFriend.h:107
An iterator over the friend declarations of a class.
Definition: DeclFriend.h:173
unsigned getFriendTypeNumTemplateParameterLists() const
Definition: DeclFriend.h:110
llvm::PointerUnion< NamedDecl *, TypeSourceInfo * > FriendUnion
Definition: DeclFriend.h:45
friend_iterator & operator+=(difference_type N)
Definition: DeclFriend.h:209
friend_iterator friend_end() const
Definition: DeclFriend.h:227
std::forward_iterator_tag iterator_category
Definition: DeclFriend.h:185
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:646
friend class ASTContext
Definition: Type.h:4178
friend_iterator friend_begin() const
Definition: DeclFriend.h:223
Defines the clang::TypeLoc interface and its subclasses.
static bool classof(const Decl *D)
Definition: DeclFriend.h:164
SourceLocation getFriendLoc() const
Retrieves the location of the 'friend' keyword.
Definition: DeclFriend.h:125
#define false
Definition: stdbool.h:33
Kind
Encodes a location in the source.
const std::string ID
bool operator==(const friend_iterator &Other) const
Definition: DeclFriend.h:201
T * get(ExternalASTSource *Source) const
Retrieve the pointer to the AST node that this lazy pointer.
static bool classofKind(Kind K)
Definition: DeclFriend.h:165
bool isUnsupportedFriend() const
Determines if this friend kind is unsupported.
Definition: DeclFriend.h:156
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1135
NamedDecl * getFriendDecl() const
If this friend declaration doesn't name a type, return the inner declaration.
Definition: DeclFriend.h:120
friend TrailingObjects
Definition: OpenMPClause.h:258
friend_iterator operator+(difference_type N) const
Definition: DeclFriend.h:216
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:263
void setUnsupportedFriend(bool Unsupported)
Definition: DeclFriend.h:159
Declaration of a class template.
SourceRange getSourceRange() const override LLVM_READONLY
Retrieves the source range for the friend declaration.
Definition: DeclFriend.h:130
A trivial tuple used to represent a source range.
NamedDecl - This represents a decl with a name.
Definition: Decl.h:213
bool operator!=(const friend_iterator &Other) const
Definition: DeclFriend.h:205
Declaration of a template function.
Definition: DeclTemplate.h:838
void pushFriendDecl(FriendDecl *FD)
Definition: DeclFriend.h:235