clang 8.0.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/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/ExternalASTSource.h"
23 #include "clang/AST/TypeLoc.h"
24 #include "clang/Basic/LLVM.h"
25 #include "clang/Basic/SourceLocation.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/None.h"
28 #include "llvm/ADT/PointerUnion.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/TrailingObjects.h"
32 #include <cassert>
33 #include <iterator>
34
35 namespace clang {
36
37 class ASTContext;
38
39 /// FriendDecl - Represents the declaration of a friend entity,
40 /// which can be a function, a type, or a templated function or type.
41 /// For example:
42 ///
43 /// @code
44 /// template <typename T> class A {
45 /// friend int foo(T);
46 /// friend class B;
47 /// friend T; // only in C++0x
48 /// template <typename U> friend class C;
49 /// template <typename U> friend A& operator+=(A&, const U&) { ... }
50 /// };
51 /// @endcode
52 ///
53 /// The semantic context of a friend decl is its declaring class.
54 class FriendDecl final
55 : public Decl,
56 private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> {
57 virtual void anchor();
58
59 public:
60 using FriendUnion = llvm::PointerUnion<NamedDecl *, TypeSourceInfo *>;
61
62 private:
63 friend class CXXRecordDecl;
64 friend class CXXRecordDecl::friend_iterator;
65
66 // The declaration that's a friend of this class.
67 FriendUnion Friend;
68
69 // A pointer to the next friend in the sequence.
70 LazyDeclPtr NextFriend;
71
72 // Location of the 'friend' specifier.
73 SourceLocation FriendLoc;
74
75 /// True if this 'friend' declaration is unsupported. Eventually we
76 /// will support every possible friend declaration, but for now we
77 /// silently ignore some and set this flag to authorize all access.
78 unsigned UnsupportedFriend : 1;
79
80 // The number of "outer" template parameter lists in non-templatic
81 // (currently unsupported) friend type declarations, such as
82 // template <class T> friend class A<T>::B;
83 unsigned NumTPLists : 31;
84
85 FriendDecl(DeclContext *DC, SourceLocation L, FriendUnion Friend,
86 SourceLocation FriendL,
87 ArrayRef<TemplateParameterList *> FriendTypeTPLists)
88 : Decl(Decl::Friend, DC, L), Friend(Friend), FriendLoc(FriendL),
89 UnsupportedFriend(false), NumTPLists(FriendTypeTPLists.size()) {
90 for (unsigned i = 0; i < NumTPLists; ++i)
91 getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i];
92 }
93
94 FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
95 : Decl(Decl::Friend, Empty), UnsupportedFriend(false),
96 NumTPLists(NumFriendTypeTPLists) {}
97
98 FriendDecl *getNextFriend() {
99 if (!NextFriend.isOffset())
100 return cast_or_null<FriendDecl>(NextFriend.get(nullptr));
101 return getNextFriendSlowCase();
102 }
103
104 FriendDecl *getNextFriendSlowCase();
105
106 public:
107 friend class ASTDeclReader;
108 friend class ASTDeclWriter;
109 friend class ASTNodeImporter;
110 friend TrailingObjects;
111
112 static FriendDecl *Create(ASTContext &C, DeclContext *DC,
113 SourceLocation L, FriendUnion Friend_,
114 SourceLocation FriendL,
115 ArrayRef<TemplateParameterList*> FriendTypeTPLists
116 = None);
117 static FriendDecl *CreateDeserialized(ASTContext &C, unsigned ID,
118 unsigned FriendTypeNumTPLists);
119
120 /// If this friend declaration names an (untemplated but possibly
121 /// dependent) type, return the type; otherwise return null. This
122 /// is used for elaborated-type-specifiers and, in C++0x, for
123 /// arbitrary friend type declarations.
124 TypeSourceInfo *getFriendType() const {
125 return Friend.dyn_cast<TypeSourceInfo*>();
126 }
127
128 unsigned getFriendTypeNumTemplateParameterLists() const {
129 return NumTPLists;
130 }
131
132 TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
133 assert(N < NumTPLists);
134 return getTrailingObjects<TemplateParameterList *>()[N];
135 }
136
137 /// If this friend declaration doesn't name a type, return the inner
138 /// declaration.
139 NamedDecl *getFriendDecl() const {
140 return Friend.dyn_cast<NamedDecl *>();
141 }
142
143 /// Retrieves the location of the 'friend' keyword.
144 SourceLocation getFriendLoc() const {
145 return FriendLoc;
146 }
147
148 /// Retrieves the source range for the friend declaration.
149 SourceRange getSourceRange() const override LLVM_READONLY {
150 if (NamedDecl *ND = getFriendDecl()) {
151 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
152 return FD->getSourceRange();
153 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(ND))
154 return FTD->getSourceRange();
155 if (const auto *CTD = dyn_cast<ClassTemplateDecl>(ND))
156 return CTD->getSourceRange();
157 if (const auto *DD = dyn_cast<DeclaratorDecl>(ND)) {
158 if (DD->getOuterLocStart() != DD->getInnerLocStart())
159 return DD->getSourceRange();
160 }
161 return SourceRange(getFriendLoc(), ND->getEndLoc());
162 }
163 else if (TypeSourceInfo *TInfo = getFriendType()) {
164 SourceLocation StartL =
165 (NumTPLists == 0) ? getFriendLoc()
166 : getTrailingObjects<TemplateParameterList *>()[0]
167 ->getTemplateLoc();
168 return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
169 }
170 else
171 return SourceRange(getFriendLoc(), getLocation());
172 }
173
174 /// Determines if this friend kind is unsupported.
175 bool isUnsupportedFriend() const {
176 return UnsupportedFriend;
177 }
178 void setUnsupportedFriend(bool Unsupported) {
179 UnsupportedFriend = Unsupported;
180 }
181
182 // Implement isa/cast/dyncast/etc.
183 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
184 static bool classofKind(Kind K) { return K == Decl::Friend; }
185 };
186
187 /// An iterator over the friend declarations of a class.
188 class CXXRecordDecl::friend_iterator {
189 friend class CXXRecordDecl;
190
191 FriendDecl *Ptr;
192
193 explicit friend_iterator(FriendDecl *Ptr) : Ptr(Ptr) {}
194
195 public:
196 friend_iterator() = default;
197
198 using value_type = FriendDecl *;
199 using reference = FriendDecl *;
200 using pointer = FriendDecl *;
201 using difference_type = int;
202 using iterator_category = std::forward_iterator_tag;
203
204 reference operator*() const { return Ptr; }
205
206 friend_iterator &operator++() {
207 assert(Ptr && "attempt to increment past end of friend list");
208 Ptr = Ptr->getNextFriend();
209 return *this;
210 }
211
212 friend_iterator operator++(int) {
213 friend_iterator tmp = *this;
214 ++*this;
215 return tmp;
216 }
217
218 bool operator==(const friend_iterator &Other) const {
219 return Ptr == Other.Ptr;
220 }
221
222 bool operator!=(const friend_iterator &Other) const {
223 return Ptr != Other.Ptr;
224 }
225
226 friend_iterator &operator+=(difference_type N) {
227 assert(N >= 0 && "cannot rewind a CXXRecordDecl::friend_iterator");
228 while (N--)
229 ++*this;
230 return *this;
231 }
232
233 friend_iterator operator+(difference_type N) const {
234 friend_iterator tmp = *this;
235 tmp += N;
236 return tmp;
237 }
238 };
239
240 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_begin() const {
241 return friend_iterator(getFirstFriend());
242 }
243
244 inline CXXRecordDecl::friend_iterator CXXRecordDecl::friend_end() const {
245 return friend_iterator(nullptr);
246 }
247
248 inline CXXRecordDecl::friend_range CXXRecordDecl::friends() const {
249 return friend_range(friend_begin(), friend_end());
250 }
251
252 inline void CXXRecordDecl::pushFriendDecl(FriendDecl *FD) {
253 assert(!FD->NextFriend && "friend already has next friend?");
254 FD->NextFriend = data().FirstFriend;
255 data().FirstFriend = FD;
256 }
257
258 } // namespace clang
259
260 #endif // LLVM_CLANG_AST_DECLFRIEND_H
clang::Builtin::ID
ID
Definition: Builtins.h:49
clang::FriendDecl::TrailingObjects
friend TrailingObjects
Definition: DeclFriend.h:110
clang::FriendDecl::getFriendTypeNumTemplateParameterLists
unsigned getFriendTypeNumTemplateParameterLists() const
Definition: DeclFriend.h:128
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
DeclTemplate.h
Defines the C++ template declaration subclasses.
clang::CXXRecordDecl::friend_iterator::operator+
friend_iterator operator+(difference_type N) const
Definition: DeclFriend.h:233
clang::FriendDecl::CreateDeserialized
static FriendDecl * CreateDeserialized(ASTContext &C, unsigned ID, unsigned FriendTypeNumTPLists)
Definition: DeclFriend.cpp:66
clang::TypeSourceInfo
A container of type source information.
Definition: Decl.h:87
clang::FriendDecl
FriendDecl - Represents the declaration of a friend entity, which can be a function, a type, or a templated function or type.
Definition: DeclFriend.h:54
clang::FriendDecl::FriendUnion
llvm::PointerUnion< NamedDecl *, TypeSourceInfo * > FriendUnion
Definition: DeclFriend.h:60
clang::TemplateParameterList
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:68
clang::CXXRecordDecl::friend_iterator
An iterator over the friend declarations of a class.
Definition: DeclFriend.h:188
clang::FriendDecl::getFriendType
TypeSourceInfo * getFriendType() const
If this friend declaration names an (untemplated but possibly dependent) type, return the type; other...
Definition: DeclFriend.h:124
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
clang::CXXRecordDecl::friend_iterator::difference_type
int difference_type
Definition: DeclFriend.h:201
clang::FriendDecl::getFriendDecl
NamedDecl * getFriendDecl() const
If this friend declaration doesn&#39;t name a type, return the inner declaration.
Definition: DeclFriend.h:139
clang::CXXRecordDecl::friend_iterator::operator++
friend_iterator operator++(int)
Definition: DeclFriend.h:212
clang::CXXRecordDecl::friend_range
llvm::iterator_range< friend_iterator > friend_range
Definition: DeclCXX.h:898
clang::CXXRecordDecl::friend_iterator::operator+=
friend_iterator & operator+=(difference_type N)
Definition: DeclFriend.h:226
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::CXXRecordDecl::friend_iterator::operator++
friend_iterator & operator++()
Definition: DeclFriend.h:206
clang::CXXRecordDecl::friend_end
friend_iterator friend_end() const
Definition: DeclFriend.h:244
clang::FriendDecl::getFriendTypeTemplateParameterList
TemplateParameterList * getFriendTypeTemplateParameterList(unsigned N) const
Definition: DeclFriend.h:132
llvm::ArrayRef
Definition: LLVM.h:32
ExternalASTSource.h
clang::Decl::EmptyShell
A placeholder type used to construct an empty shell of a decl-derived type that will be filled in lat...
Definition: DeclBase.h:103
DeclBase.h
TypeLoc.h
Defines the clang::TypeLoc interface and its subclasses.
clang::ASTNodeImporter
Definition: ASTImporter.cpp:157
clang::FriendDecl::classof
static bool classof(const Decl *D)
Definition: DeclFriend.h:183
false
#define false
Definition: stdbool.h:33
clang::FriendDecl::Create
static FriendDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation L, FriendUnion Friend_, SourceLocation FriendL, ArrayRef< TemplateParameterList *> FriendTypeTPLists=None)
Definition: DeclFriend.cpp:35
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::FriendDecl::getFriendLoc
SourceLocation getFriendLoc() const
Retrieves the location of the &#39;friend&#39; keyword.
Definition: DeclFriend.h:144
clang::CXXRecordDecl::friend_begin
friend_iterator friend_begin() const
Definition: DeclFriend.h:240
clang::CXXRecordDecl::friend_iterator::operator==
bool operator==(const friend_iterator &Other) const
Definition: DeclFriend.h:218
clang::CXXRecordDecl::friend_iterator::operator*
reference operator*() const
Definition: DeclFriend.h:204
clang::MultiVersionKind::None
clang::FriendDecl::classofKind
static bool classofKind(Kind K)
Definition: DeclFriend.h:184
clang::driver::options::Unsupported
Definition: Options.h:31
Decl.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1262
clang::LazyOffsetPtr< Decl, uint32_t, &ExternalASTSource::GetExternalDecl >
clang::Decl::getKind
Kind getKind() const
Definition: DeclBase.h:421
clang::CXXRecordDecl::friend_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: DeclFriend.h:202
clang::LazyOffsetPtr::get
T * get(ExternalASTSource *Source) const
Retrieve the pointer to the AST node that this lazy pointer points to.
Definition: ExternalASTSource.h:401
clang::CXXRecordDecl::friends
friend_range friends() const
Definition: DeclFriend.h:248
clang::ASTDeclReader
Definition: ASTReaderDecl.cpp:81
TrailingObjects
friend TrailingObjects
Definition: OpenMPClause.h:99
DeclCXX.h
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
SourceLocation.h
Defines the clang::SourceLocation class and associated facilities.
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
clang::AttrSyntax::C
clang::FriendDecl::setUnsupportedFriend
void setUnsupportedFriend(bool Unsupported)
Definition: DeclFriend.h:178
clang::LazyOffsetPtr::isOffset
bool isOffset() const
Whether this pointer is currently stored as an offset.
Definition: ExternalASTSource.h:394
clang::ASTDeclWriter
Definition: ASTWriterDecl.cpp:35
clang::Decl::Kind
Kind
Lists the kind of concrete classes of Decl.
Definition: DeclBase.h:90
clang::CXXRecordDecl::friend_iterator::operator!=
bool operator!=(const friend_iterator &Other) const
Definition: DeclFriend.h:222
clang::FriendDecl::getSourceRange
SourceRange getSourceRange() const override LLVM_READONLY
Retrieves the source range for the friend declaration.
Definition: DeclFriend.h:149
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:198
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
clang::FriendDecl::isUnsupportedFriend
bool isUnsupportedFriend() const
Determines if this friend kind is unsupported.
Definition: DeclFriend.h:175
clang::Decl::getLocation
SourceLocation getLocation() const
Definition: DeclBase.h:418
clang::CXXRecordDecl::pushFriendDecl
void pushFriendDecl(FriendDecl *FD)
Definition: DeclFriend.h:252