clang 24.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/DeclCXX.h"
19#include "clang/AST/TypeLoc.h"
20#include "clang/Basic/LLVM.h"
21#include "llvm/ADT/PointerUnion.h"
22#include "llvm/Support/Casting.h"
23#include "llvm/Support/Compiler.h"
24#include <cassert>
25#include <iterator>
26
27namespace clang {
28
29class ASTContext;
30
31/// FriendDecl - Represents the declaration of a friend entity,
32/// which can be a function, a type, or a templated function or type.
33/// For example:
34///
35/// @code
36/// template <typename T> class A {
37/// friend int foo(T);
38/// friend class B;
39/// friend T; // only in C++0x
40/// template <typename U> friend class C;
41/// template <typename U> friend A& operator+=(A&, const U&) { ... }
42/// };
43/// @endcode
44///
45/// The semantic context of a friend decl is its declaring class.
46class FriendDecl : public Decl {
47 LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();
48
49public:
50 using FriendUnion = llvm::PointerUnion<NamedDecl *, TypeSourceInfo *>;
51
52private:
53 friend class CXXRecordDecl;
55
56 // Location of the '...', if present.
57 SourceLocation EllipsisLoc;
58
59 SourceLocation FriendLoc;
60
61protected:
62 // The declaration that's a friend of this class.
64
66
68 SourceLocation FL, SourceLocation EllipsisLoc = {})
69 : Decl(K, DC, L), EllipsisLoc(EllipsisLoc), FriendLoc(FL), Friend(Friend),
70 NextFriend() {}
71
73
75 if (NextFriend.isOffset())
76 return getNextFriendSlowCase();
77 return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
78 }
79
81
82public:
83 friend class ASTDeclReader;
84 friend class ASTDeclWriter;
85 friend class ASTNodeImporter;
86
89 SourceLocation EllipsisLoc = {});
91
92 /// If this friend declaration names an (untemplated but possibly
93 /// dependent) type, return the type; otherwise return null. This
94 /// is used for elaborated-type-specifiers and, in C++0x, for
95 /// arbitrary friend type declarations.
97 return Friend.dyn_cast<TypeSourceInfo*>();
98 }
99
100 /// If this friend declaration doesn't name a type, return the inner
101 /// declaration.
102 virtual NamedDecl *getFriendDecl() const {
103 return Friend.dyn_cast<NamedDecl *>();
104 }
105
106 /// Retrieves the location of the '...', if present.
107 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
108
109 SourceLocation getFriendLoc() const { return FriendLoc; }
110
111 SourceRange getSourceRange() const override LLVM_READONLY;
112
113 bool isPackExpansion() const { return EllipsisLoc.isValid(); }
114
115 // Implement isa/cast/dyncast/etc.
116 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
117 static bool classofKind(Kind K) {
118 return K >= firstFriend && K <= lastFriend;
119 }
120};
121
122/// An iterator over the friend declarations of a class.
124 friend class CXXRecordDecl;
125
126 FriendDecl *Ptr;
127
128 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
129
130public:
131 friend_iterator() = default;
132
137 using iterator_category = std::forward_iterator_tag;
138
139 reference operator*() const { return Ptr; }
140
141 friend_iterator &operator++() {
142 assert(Ptr && "attempt to increment past end of friend list");
143 Ptr = Ptr->getNextFriend();
144 return *this;
145 }
146
147 friend_iterator operator++(int) {
148 friend_iterator tmp = *this;
149 ++*this;
150 return tmp;
151 }
152
153 bool operator==(const friend_iterator &Other) const {
154 return Ptr == Other.Ptr;
155 }
156
157 bool operator!=(const friend_iterator &Other) const {
158 return Ptr != Other.Ptr;
159 }
160
161 friend_iterator &operator+=(difference_type N) {
162 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
163 while (N--)
164 ++*this;
165 return *this;
166 }
167
168 friend_iterator operator+(difference_type N) const {
169 friend_iterator tmp = *this;
170 tmp += N;
171 return tmp;
172 }
173};
174
176 return friend_iterator(getFirstFriend());
177}
178
182
186
188 assert(!FD->NextFriend && "friend already has next friend?");
189 FD->NextFriend = data().FirstFriend;
190 data().FirstFriend = FD;
191}
192
193} // namespace clang
194
195#endif // LLVM_CLANG_AST_DECLFRIEND_H
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::TypeLoc interface and its subclasses.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
An iterator over the friend declarations of a class.
Definition DeclFriend.h:123
std::forward_iterator_tag iterator_category
Definition DeclFriend.h:137
friend_iterator operator+(difference_type N) const
Definition DeclFriend.h:168
bool operator!=(const friend_iterator &Other) const
Definition DeclFriend.h:157
bool operator==(const friend_iterator &Other) const
Definition DeclFriend.h:153
friend_iterator & operator+=(difference_type N)
Definition DeclFriend.h:161
friend_range friends() const
Definition DeclFriend.h:183
friend_iterator friend_begin() const
Definition DeclFriend.h:175
llvm::iterator_range< friend_iterator > friend_range
Definition DeclCXX.h:683
void pushFriendDecl(FriendDecl *FD)
Definition DeclFriend.h:187
friend_iterator friend_end() const
Definition DeclFriend.h:179
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Decl()=delete
Kind
Lists the kind of concrete classes of Decl.
Definition DeclBase.h:89
friend class DeclContext
Definition DeclBase.h:260
Kind getKind() const
Definition DeclBase.h:450
FriendDecl - Represents the declaration of a friend entity, which can be a function,...
Definition DeclFriend.h:46
LazyDeclPtr NextFriend
Definition DeclFriend.h:65
FriendDecl(Kind K, DeclContext *DC, SourceLocation L, FriendUnion Friend, SourceLocation FL, SourceLocation EllipsisLoc={})
Definition DeclFriend.h:67
llvm::PointerUnion< NamedDecl *, TypeSourceInfo * > FriendUnion
Definition DeclFriend.h:50
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
static bool classofKind(Kind K)
Definition DeclFriend.h:117
static bool classof(const Decl *D)
Definition DeclFriend.h:116
SourceLocation getFriendLoc() const
Definition DeclFriend.h:109
friend class ASTDeclReader
Definition DeclFriend.h:83
FriendUnion Friend
Definition DeclFriend.h:63
friend class ASTNodeImporter
Definition DeclFriend.h:85
friend class CXXRecordDecl
Definition DeclFriend.h:53
SourceLocation getEllipsisLoc() const
Retrieves the location of the '...', if present.
Definition DeclFriend.h:107
FriendDecl * getNextFriendSlowCase()
virtual NamedDecl * getFriendDecl() const
If this friend declaration doesn't name a type, return the inner declaration.
Definition DeclFriend.h:102
TypeSourceInfo * getFriendType() const
If this friend declaration names an (untemplated but possibly dependent) type, return the type; other...
Definition DeclFriend.h:96
friend class ASTDeclWriter
Definition DeclFriend.h:84
bool isPackExpansion() const
Definition DeclFriend.h:113
FriendDecl * getNextFriend()
Definition DeclFriend.h:74
FriendDecl(Kind K, EmptyShell Empty)
Definition DeclFriend.h:72
static FriendDecl * CreateDeserialized(ASTContext &C, GlobalDeclID ID)
This represents a decl that may have a name.
Definition Decl.h:274
Encodes a location in the source.
A trivial tuple used to represent a source range.
A container of type source information.
Definition TypeBase.h:8475
Top level wrappers for InstallAPI frontend operations.
@ Create
'create' clause, allowed on Compute and Combined constructs, plus 'data', 'enter data',...
LazyOffsetPtr< Decl, GlobalDeclID, &ExternalASTSource::GetExternalDecl > LazyDeclPtr
A lazy pointer to a declaration.
@ Other
Other implicit parameter.
Definition Decl.h:1774
A placeholder type used to construct an empty shell of a decl-derived type that will be filled in lat...
Definition DeclBase.h:102