clang 17.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 /// True if this 'friend' declaration is unsupported. Eventually we
74 /// will support every possible friend declaration, but for now we
75 /// silently ignore some and set this flag to authorize all access.
76 unsigned UnsupportedFriend : 1;
77
78 // The number of "outer" template parameter lists in non-templatic
79 // (currently unsupported) friend type declarations, such as
80 // template <class T> friend class A<T>::B;
81 unsigned NumTPLists : 31;
82
84 SourceLocation FriendL,
85 ArrayRef<TemplateParameterList *> FriendTypeTPLists)
86 : Decl(Decl::Friend, DC, L), Friend(Friend), FriendLoc(FriendL),
87 UnsupportedFriend(false), NumTPLists(FriendTypeTPLists.size()) {
88 for (unsigned i = 0; i < NumTPLists; ++i)
89 getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i];
90 }
91
92 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
93 : Decl(Decl::Friend, Empty), UnsupportedFriend(false),
94 NumTPLists(NumFriendTypeTPLists) {}
95
96 FriendDecl *getNextFriend() {
97 if (!NextFriend.isOffset())
98 return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
99 return getNextFriendSlowCase();
100 }
101
102 FriendDecl *getNextFriendSlowCase();
103
104public:
105 friend class ASTDeclReader;
106 friend class ASTDeclWriter;
107 friend class ASTNodeImporter;
109
110 static FriendDecl *
112 SourceLocation FriendL,
113 ArrayRef<TemplateParameterList *> FriendTypeTPLists = std::nullopt);
114 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
115 unsigned FriendTypeNumTPLists);
116
117 /// If this friend declaration names an (untemplated but possibly
118 /// dependent) type, return the type; otherwise return null. This
119 /// is used for elaborated-type-specifiers and, in C++0x, for
120 /// arbitrary friend type declarations.
122 return Friend.dyn_cast<TypeSourceInfo*>();
123 }
124
126 return NumTPLists;
127 }
128
130 assert(N < NumTPLists);
131 return getTrailingObjects<TemplateParameterList *>()[N];
132 }
133
134 /// If this friend declaration doesn't name a type, return the inner
135 /// declaration.
137 return Friend.dyn_cast<NamedDecl *>();
138 }
139
140 /// Retrieves the location of the 'friend' keyword.
142 return FriendLoc;
143 }
144
145 /// Retrieves the source range for the friend declaration.
146 SourceRange getSourceRange() const override LLVM_READONLY {
147 if (NamedDecl *ND = getFriendDecl()) {
148 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
149 return FD->getSourceRange();
150 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(ND))
151 return FTD->getSourceRange();
152 if (const auto *CTD = dyn_cast<ClassTemplateDecl>(ND))
153 return CTD->getSourceRange();
154 if (const auto *DD = dyn_cast<DeclaratorDecl>(ND)) {
155 if (DD->getOuterLocStart() != DD->getInnerLocStart())
156 return DD->getSourceRange();
157 }
158 return SourceRange(getFriendLoc(), ND->getEndLoc());
159 }
160 else if (TypeSourceInfo *TInfo = getFriendType()) {
161 SourceLocation StartL =
162 (NumTPLists == 0) ? getFriendLoc()
163 : getTrailingObjects<TemplateParameterList *>()[0]
164 ->getTemplateLoc();
165 return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
166 }
167 else
169 }
170
171 /// Determines if this friend kind is unsupported.
172 bool isUnsupportedFriend() const {
173 return UnsupportedFriend;
174 }
175 void setUnsupportedFriend(bool Unsupported) {
176 UnsupportedFriend = Unsupported;
177 }
178
179 // Implement isa/cast/dyncast/etc.
180 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
181 static bool classofKind(Kind K) { return K == Decl::Friend; }
182};
183
184/// An iterator over the friend declarations of a class.
186 friend class CXXRecordDecl;
187
188 FriendDecl *Ptr;
189
190 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
191
192public:
193 friend_iterator() = default;
194
199 using iterator_category = std::forward_iterator_tag;
200
201 reference operator*() const { return Ptr; }
202
204 assert(Ptr && "attempt to increment past end of friend list");
205 Ptr = Ptr->getNextFriend();
206 return *this;
207 }
208
210 friend_iterator tmp = *this;
211 ++*this;
212 return tmp;
213 }
214
215 bool operator==(const friend_iterator &Other) const {
216 return Ptr == Other.Ptr;
217 }
218
219 bool operator!=(const friend_iterator &Other) const {
220 return Ptr != Other.Ptr;
221 }
222
224 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
225 while (N--)
226 ++*this;
227 return *this;
228 }
229
231 friend_iterator tmp = *this;
232 tmp += N;
233 return tmp;
234 }
235};
236
238 return friend_iterator(getFirstFriend());
239}
240
242 return friend_iterator(nullptr);
243}
244
247}
248
250 assert(!FD->NextFriend && "friend already has next friend?");
251 FD->NextFriend = data().FirstFriend;
252 data().FirstFriend = FD;
253}
254
255} // namespace clang
256
257#endif // LLVM_CLANG_AST_DECLFRIEND_H
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:182
An iterator over the friend declarations of a class.
Definition: DeclFriend.h:185
std::forward_iterator_tag iterator_category
Definition: DeclFriend.h:199
friend_iterator operator+(difference_type N) const
Definition: DeclFriend.h:230
bool operator!=(const friend_iterator &Other) const
Definition: DeclFriend.h:219
bool operator==(const friend_iterator &Other) const
Definition: DeclFriend.h:215
friend_iterator & operator+=(difference_type N)
Definition: DeclFriend.h:223
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
friend_range friends() const
Definition: DeclFriend.h:245
friend_iterator friend_begin() const
Definition: DeclFriend.h:237
llvm::iterator_range< friend_iterator > friend_range
Definition: DeclCXX.h:677
void pushFriendDecl(FriendDecl *FD)
Definition: DeclFriend.h:249
friend_iterator friend_end() const
Definition: DeclFriend.h:241
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1393
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
Kind
Lists the kind of concrete classes of Decl.
Definition: DeclBase.h:86
SourceLocation getLocation() const
Definition: DeclBase.h:432
Kind getKind() const
Definition: DeclBase.h:435
FriendDecl - Represents the declaration of a friend entity, which can be a function,...
Definition: DeclFriend.h:54
static FriendDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation L, FriendUnion Friend_, SourceLocation FriendL, ArrayRef< TemplateParameterList * > FriendTypeTPLists=std::nullopt)
Definition: DeclFriend.cpp:34
bool isUnsupportedFriend() const
Determines if this friend kind is unsupported.
Definition: DeclFriend.h:172
llvm::PointerUnion< NamedDecl *, TypeSourceInfo * > FriendUnion
Definition: DeclFriend.h:58
static bool classofKind(Kind K)
Definition: DeclFriend.h:181
static bool classof(const Decl *D)
Definition: DeclFriend.h:180
friend TrailingObjects
Definition: DeclFriend.h:108
unsigned getFriendTypeNumTemplateParameterLists() const
Definition: DeclFriend.h:125
SourceLocation getFriendLoc() const
Retrieves the location of the 'friend' keyword.
Definition: DeclFriend.h:141
void setUnsupportedFriend(bool Unsupported)
Definition: DeclFriend.h:175
SourceRange getSourceRange() const override LLVM_READONLY
Retrieves the source range for the friend declaration.
Definition: DeclFriend.h:146
TemplateParameterList * getFriendTypeTemplateParameterList(unsigned N) const
Definition: DeclFriend.h:129
NamedDecl * getFriendDecl() const
If this friend declaration doesn't name a type, return the inner declaration.
Definition: DeclFriend.h:136
TypeSourceInfo * getFriendType() const
If this friend declaration names an (untemplated but possibly dependent) type, return the type; other...
Definition: DeclFriend.h:121
static FriendDecl * CreateDeserialized(ASTContext &C, unsigned ID, unsigned FriendTypeNumTPLists)
Definition: DeclFriend.cpp:65
This represents a decl that may have a name.
Definition: Decl.h:247
Encodes a location in the source.
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:6620
@ C
Languages that the frontend can parse and compile.
#define false
Definition: stdbool.h:22
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.