clang 20.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 virtual void anchor();
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 for (unsigned i = 0; i < NumTPLists; ++i)
94 getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i];
95 }
96
97 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
98 : Decl(Decl::Friend, Empty), UnsupportedFriend(false),
99 NumTPLists(NumFriendTypeTPLists) {}
100
101 FriendDecl *getNextFriend() {
102 if (!NextFriend.isOffset())
103 return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
104 return getNextFriendSlowCase();
105 }
106
107 FriendDecl *getNextFriendSlowCase();
108
109public:
110 friend class ASTDeclReader;
111 friend class ASTDeclWriter;
112 friend class ASTNodeImporter;
114
115 static FriendDecl *
117 SourceLocation FriendL, SourceLocation EllipsisLoc = {},
118 ArrayRef<TemplateParameterList *> FriendTypeTPLists = {});
119 static FriendDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID,
120 unsigned FriendTypeNumTPLists);
121
122 /// If this friend declaration names an (untemplated but possibly
123 /// dependent) type, return the type; otherwise return null. This
124 /// is used for elaborated-type-specifiers and, in C++0x, for
125 /// arbitrary friend type declarations.
127 return Friend.dyn_cast<TypeSourceInfo*>();
128 }
129
131 return NumTPLists;
132 }
133
135 assert(N < NumTPLists);
136 return getTrailingObjects<TemplateParameterList *>()[N];
137 }
138
139 /// If this friend declaration doesn't name a type, return the inner
140 /// declaration.
142 return Friend.dyn_cast<NamedDecl *>();
143 }
144
145 /// Retrieves the location of the 'friend' keyword.
147 return FriendLoc;
148 }
149
150 /// Retrieves the location of the '...', if present.
151 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
152
153 /// Retrieves the source range for the friend declaration.
154 SourceRange getSourceRange() const override LLVM_READONLY {
155 if (TypeSourceInfo *TInfo = getFriendType()) {
156 SourceLocation StartL =
157 (NumTPLists == 0) ? getFriendLoc()
158 : getTrailingObjects<TemplateParameterList *>()[0]
159 ->getTemplateLoc();
161 : TInfo->getTypeLoc().getEndLoc();
162 return SourceRange(StartL, EndL);
163 }
164
165 if (isPackExpansion())
167
168 if (NamedDecl *ND = getFriendDecl()) {
169 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
170 return FD->getSourceRange();
171 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(ND))
172 return FTD->getSourceRange();
173 if (const auto *CTD = dyn_cast<ClassTemplateDecl>(ND))
174 return CTD->getSourceRange();
175 if (const auto *DD = dyn_cast<DeclaratorDecl>(ND)) {
176 if (DD->getOuterLocStart() != DD->getInnerLocStart())
177 return DD->getSourceRange();
178 }
179 return SourceRange(getFriendLoc(), ND->getEndLoc());
180 }
181
183 }
184
185 /// Determines if this friend kind is unsupported.
186 bool isUnsupportedFriend() const {
187 return UnsupportedFriend;
188 }
189 void setUnsupportedFriend(bool Unsupported) {
190 UnsupportedFriend = Unsupported;
191 }
192
193 bool isPackExpansion() const { return EllipsisLoc.isValid(); }
194
195 // Implement isa/cast/dyncast/etc.
196 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
197 static bool classofKind(Kind K) { return K == Decl::Friend; }
198};
199
200/// An iterator over the friend declarations of a class.
202 friend class CXXRecordDecl;
203
204 FriendDecl *Ptr;
205
206 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
207
208public:
209 friend_iterator() = default;
210
215 using iterator_category = std::forward_iterator_tag;
216
217 reference operator*() const { return Ptr; }
218
220 assert(Ptr && "attempt to increment past end of friend list");
221 Ptr = Ptr->getNextFriend();
222 return *this;
223 }
224
226 friend_iterator tmp = *this;
227 ++*this;
228 return tmp;
229 }
230
231 bool operator==(const friend_iterator &Other) const {
232 return Ptr == Other.Ptr;
233 }
234
235 bool operator!=(const friend_iterator &Other) const {
236 return Ptr != Other.Ptr;
237 }
238
240 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
241 while (N--)
242 ++*this;
243 return *this;
244 }
245
247 friend_iterator tmp = *this;
248 tmp += N;
249 return tmp;
250 }
251};
252
254 return friend_iterator(getFirstFriend());
255}
256
258 return friend_iterator(nullptr);
259}
260
263}
264
266 assert(!FD->NextFriend && "friend already has next friend?");
267 FD->NextFriend = data().FirstFriend;
268 data().FirstFriend = FD;
269}
270
271} // namespace clang
272
273#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:201
std::forward_iterator_tag iterator_category
Definition: DeclFriend.h:215
friend_iterator operator+(difference_type N) const
Definition: DeclFriend.h:246
bool operator!=(const friend_iterator &Other) const
Definition: DeclFriend.h:235
bool operator==(const friend_iterator &Other) const
Definition: DeclFriend.h:231
friend_iterator & operator+=(difference_type N)
Definition: DeclFriend.h:239
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
friend_range friends() const
Definition: DeclFriend.h:261
friend_iterator friend_begin() const
Definition: DeclFriend.h:253
llvm::iterator_range< friend_iterator > friend_range
Definition: DeclCXX.h:695
void pushFriendDecl(FriendDecl *FD)
Definition: DeclFriend.h:265
friend_iterator friend_end() const
Definition: DeclFriend.h:257
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1435
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:442
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:186
llvm::PointerUnion< NamedDecl *, TypeSourceInfo * > FriendUnion
Definition: DeclFriend.h:58
static bool classofKind(Kind K)
Definition: DeclFriend.h:197
static FriendDecl * CreateDeserialized(ASTContext &C, GlobalDeclID ID, unsigned FriendTypeNumTPLists)
Definition: DeclFriend.cpp:63
static bool classof(const Decl *D)
Definition: DeclFriend.h:196
friend TrailingObjects
Definition: DeclFriend.h:113
unsigned getFriendTypeNumTemplateParameterLists() const
Definition: DeclFriend.h:130
SourceLocation getFriendLoc() const
Retrieves the location of the 'friend' keyword.
Definition: DeclFriend.h:146
void setUnsupportedFriend(bool Unsupported)
Definition: DeclFriend.h:189
SourceRange getSourceRange() const override LLVM_READONLY
Retrieves the source range for the friend declaration.
Definition: DeclFriend.h:154
SourceLocation getEllipsisLoc() const
Retrieves the location of the '...', if present.
Definition: DeclFriend.h:151
TemplateParameterList * getFriendTypeTemplateParameterList(unsigned N) const
Definition: DeclFriend.h:134
NamedDecl * getFriendDecl() const
If this friend declaration doesn't name a type, return the inner declaration.
Definition: DeclFriend.h:141
TypeSourceInfo * getFriendType() const
If this friend declaration names an (untemplated but possibly dependent) type, return the type; other...
Definition: DeclFriend.h:126
bool isPackExpansion() const
Definition: DeclFriend.h:193
This represents a decl that may have a name.
Definition: Decl.h:253
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:73
A container of type source information.
Definition: Type.h:7902
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.