clang 22.0.0git
DeclFriend.h
Go to the documentation of this file.
1//===- DeclFriend.h - Classes for C++ friend declarations -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the section of the AST representing C++ friend
10// declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_DECLFRIEND_H
15#define LLVM_CLANG_AST_DECLFRIEND_H
16
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclBase.h"
19#include "clang/AST/DeclCXX.h"
22#include "clang/AST/TypeLoc.h"
23#include "clang/Basic/LLVM.h"
25#include "llvm/ADT/ArrayRef.h"
26#include "llvm/ADT/PointerUnion.h"
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/Compiler.h"
29#include "llvm/Support/TrailingObjects.h"
30#include <cassert>
31#include <iterator>
32
33namespace clang {
34
35class ASTContext;
36
37/// FriendDecl - Represents the declaration of a friend entity,
38/// which can be a function, a type, or a templated function or type.
39/// For example:
40///
41/// @code
42/// template <typename T> class A {
43/// friend int foo(T);
44/// friend class B;
45/// friend T; // only in C++0x
46/// template <typename U> friend class C;
47/// template <typename U> friend A& operator+=(A&, const U&) { ... }
48/// };
49/// @endcode
50///
51/// The semantic context of a friend decl is its declaring class.
52class FriendDecl final
53 : public Decl,
54 private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> {
55 LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();
56
57public:
58 using FriendUnion = llvm::PointerUnion<NamedDecl *, TypeSourceInfo *>;
59
60private:
61 friend class CXXRecordDecl;
63
64 // The declaration that's a friend of this class.
65 FriendUnion Friend;
66
67 // A pointer to the next friend in the sequence.
68 LazyDeclPtr NextFriend;
69
70 // Location of the 'friend' specifier.
71 SourceLocation FriendLoc;
72
73 // Location of the '...', if present.
74 SourceLocation EllipsisLoc;
75
76 /// True if this 'friend' declaration is unsupported. Eventually we
77 /// will support every possible friend declaration, but for now we
78 /// silently ignore some and set this flag to authorize all access.
79 LLVM_PREFERRED_TYPE(bool)
80 unsigned UnsupportedFriend : 1;
81
82 // The number of "outer" template parameter lists in non-templatic
83 // (currently unsupported) friend type declarations, such as
84 // template <class T> friend class A<T>::B;
85 unsigned NumTPLists : 31;
86
88 SourceLocation FriendL, SourceLocation EllipsisLoc,
89 ArrayRef<TemplateParameterList *> FriendTypeTPLists)
90 : Decl(Decl::Friend, DC, L), Friend(Friend), FriendLoc(FriendL),
91 EllipsisLoc(EllipsisLoc), UnsupportedFriend(false),
92 NumTPLists(FriendTypeTPLists.size()) {
93 llvm::copy(FriendTypeTPLists, getTrailingObjects());
94 }
95
96 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
97 : Decl(Decl::Friend, Empty), UnsupportedFriend(false),
98 NumTPLists(NumFriendTypeTPLists) {}
99
100 FriendDecl *getNextFriend() {
101 if (!NextFriend.isOffset())
102 return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
103 return getNextFriendSlowCase();
104 }
105
106 FriendDecl *getNextFriendSlowCase();
107
108public:
109 friend class ASTDeclReader;
110 friend class ASTDeclWriter;
111 friend class ASTNodeImporter;
113
114 static FriendDecl *
116 SourceLocation FriendL, SourceLocation EllipsisLoc = {},
117 ArrayRef<TemplateParameterList *> FriendTypeTPLists = {});
118 static FriendDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID,
119 unsigned FriendTypeNumTPLists);
120
121 /// If this friend declaration names an (untemplated but possibly
122 /// dependent) type, return the type; otherwise return null. This
123 /// is used for elaborated-type-specifiers and, in C++0x, for
124 /// arbitrary friend type declarations.
126 return Friend.dyn_cast<TypeSourceInfo*>();
127 }
128
130 return NumTPLists;
131 }
132
134 return getTrailingObjects(NumTPLists)[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 location of the '...', if present.
149 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
150
151 /// Retrieves the source range for the friend declaration.
152 SourceRange getSourceRange() const override LLVM_READONLY {
153 if (TypeSourceInfo *TInfo = getFriendType()) {
154 SourceLocation StartL = (NumTPLists == 0)
155 ? getFriendLoc()
156 : getTrailingObjects()[0]->getTemplateLoc();
158 : TInfo->getTypeLoc().getEndLoc();
159 return SourceRange(StartL, EndL);
160 }
161
162 if (isPackExpansion())
164
165 if (NamedDecl *ND = getFriendDecl()) {
166 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
167 return FD->getSourceRange();
168 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(ND))
169 return FTD->getSourceRange();
170 if (const auto *CTD = dyn_cast<ClassTemplateDecl>(ND))
171 return CTD->getSourceRange();
172 if (const auto *DD = dyn_cast<DeclaratorDecl>(ND)) {
173 if (DD->getOuterLocStart() != DD->getInnerLocStart())
174 return DD->getSourceRange();
175 }
176 return SourceRange(getFriendLoc(), ND->getEndLoc());
177 }
178
180 }
181
182 /// Determines if this friend kind is unsupported.
183 bool isUnsupportedFriend() const {
184 return UnsupportedFriend;
185 }
186 void setUnsupportedFriend(bool Unsupported) {
187 UnsupportedFriend = Unsupported;
188 }
189
190 bool isPackExpansion() const { return EllipsisLoc.isValid(); }
191
192 // Implement isa/cast/dyncast/etc.
193 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
194 static bool classofKind(Kind K) { return K == Decl::Friend; }
195};
196
197/// An iterator over the friend declarations of a class.
199 friend class CXXRecordDecl;
200
201 FriendDecl *Ptr;
202
203 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
204
205public:
206 friend_iterator() = default;
207
212 using iterator_category = std::forward_iterator_tag;
213
214 reference operator*() const { return Ptr; }
215
217 assert(Ptr && "attempt to increment past end of friend list");
218 Ptr = Ptr->getNextFriend();
219 return *this;
220 }
221
223 friend_iterator tmp = *this;
224 ++*this;
225 return tmp;
226 }
227
228 bool operator==(const friend_iterator &Other) const {
229 return Ptr == Other.Ptr;
230 }
231
232 bool operator!=(const friend_iterator &Other) const {
233 return Ptr != Other.Ptr;
234 }
235
237 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
238 while (N--)
239 ++*this;
240 return *this;
241 }
242
244 friend_iterator tmp = *this;
245 tmp += N;
246 return tmp;
247 }
248};
249
251 return friend_iterator(getFirstFriend());
252}
253
255 return friend_iterator(nullptr);
256}
257
260}
261
263 assert(!FD->NextFriend && "friend already has next friend?");
264 FD->NextFriend = data().FirstFriend;
265 data().FirstFriend = FD;
266}
267
268} // namespace clang
269
270#endif // LLVM_CLANG_AST_DECLFRIEND_H
static char ID
Definition: Arena.cpp:183
const Decl * D
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::SourceLocation class and associated facilities.
Defines the clang::TypeLoc interface and its subclasses.
__device__ int
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
An iterator over the friend declarations of a class.
Definition: DeclFriend.h:198
std::forward_iterator_tag iterator_category
Definition: DeclFriend.h:212
friend_iterator operator+(difference_type N) const
Definition: DeclFriend.h:243
bool operator!=(const friend_iterator &Other) const
Definition: DeclFriend.h:232
bool operator==(const friend_iterator &Other) const
Definition: DeclFriend.h:228
friend_iterator & operator+=(difference_type N)
Definition: DeclFriend.h:236
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
friend_range friends() const
Definition: DeclFriend.h:258
friend_iterator friend_begin() const
Definition: DeclFriend.h:250
llvm::iterator_range< friend_iterator > friend_range
Definition: DeclCXX.h:683
void pushFriendDecl(FriendDecl *FD)
Definition: DeclFriend.h:262
friend_iterator friend_end() const
Definition: DeclFriend.h:254
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1449
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Kind
Lists the kind of concrete classes of Decl.
Definition: DeclBase.h:89
SourceLocation getLocation() const
Definition: DeclBase.h:439
FriendDecl - Represents the declaration of a friend entity, which can be a function,...
Definition: DeclFriend.h:54
bool isUnsupportedFriend() const
Determines if this friend kind is unsupported.
Definition: DeclFriend.h:183
llvm::PointerUnion< NamedDecl *, TypeSourceInfo * > FriendUnion
Definition: DeclFriend.h:58
static bool classofKind(Kind K)
Definition: DeclFriend.h:194
static FriendDecl * CreateDeserialized(ASTContext &C, GlobalDeclID ID, unsigned FriendTypeNumTPLists)
Definition: DeclFriend.cpp:63
static bool classof(const Decl *D)
Definition: DeclFriend.h:193
friend TrailingObjects
Definition: DeclFriend.h:112
unsigned getFriendTypeNumTemplateParameterLists() const
Definition: DeclFriend.h:129
SourceLocation getFriendLoc() const
Retrieves the location of the 'friend' keyword.
Definition: DeclFriend.h:144
void setUnsupportedFriend(bool Unsupported)
Definition: DeclFriend.h:186
SourceRange getSourceRange() const override LLVM_READONLY
Retrieves the source range for the friend declaration.
Definition: DeclFriend.h:152
SourceLocation getEllipsisLoc() const
Retrieves the location of the '...', if present.
Definition: DeclFriend.h:149
TemplateParameterList * getFriendTypeTemplateParameterList(unsigned N) const
Definition: DeclFriend.h:133
NamedDecl * getFriendDecl() const
If this friend declaration doesn't name a type, return the inner declaration.
Definition: DeclFriend.h:139
TypeSourceInfo * getFriendType() const
If this friend declaration names an (untemplated but possibly dependent) type, return the type; other...
Definition: DeclFriend.h:125
bool isPackExpansion() const
Definition: DeclFriend.h:190
This represents a decl that may have a name.
Definition: Decl.h:273
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
A trivial tuple used to represent a source range.
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:74
A container of type source information.
Definition: TypeBase.h:8314
The JSON file list parser is used to communicate input to InstallAPI.
@ Create
'create' clause, allowed on Compute and Combined constructs, plus 'data', 'enter data',...
@ Other
Other implicit parameter.
#define false
Definition: stdbool.h:26
bool isOffset() const
Whether this pointer is currently stored as an offset.
T * get(ExternalASTSource *Source) const
Retrieve the pointer to the AST node that this lazy pointer points to.