DeclCXX.cpp revision 1.1.1.2 1 1.1 joerg //===- DeclCXX.cpp - C++ Declaration AST Node Implementation --------------===//
2 1.1 joerg //
3 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 1.1 joerg // See https://llvm.org/LICENSE.txt for license information.
5 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 1.1 joerg //
7 1.1 joerg //===----------------------------------------------------------------------===//
8 1.1 joerg //
9 1.1 joerg // This file implements the C++ related Decl classes.
10 1.1 joerg //
11 1.1 joerg //===----------------------------------------------------------------------===//
12 1.1 joerg
13 1.1 joerg #include "clang/AST/DeclCXX.h"
14 1.1 joerg #include "clang/AST/ASTContext.h"
15 1.1 joerg #include "clang/AST/ASTLambda.h"
16 1.1 joerg #include "clang/AST/ASTMutationListener.h"
17 1.1 joerg #include "clang/AST/ASTUnresolvedSet.h"
18 1.1.1.2 joerg #include "clang/AST/Attr.h"
19 1.1 joerg #include "clang/AST/CXXInheritance.h"
20 1.1 joerg #include "clang/AST/DeclBase.h"
21 1.1 joerg #include "clang/AST/DeclTemplate.h"
22 1.1 joerg #include "clang/AST/DeclarationName.h"
23 1.1 joerg #include "clang/AST/Expr.h"
24 1.1 joerg #include "clang/AST/ExprCXX.h"
25 1.1 joerg #include "clang/AST/LambdaCapture.h"
26 1.1 joerg #include "clang/AST/NestedNameSpecifier.h"
27 1.1 joerg #include "clang/AST/ODRHash.h"
28 1.1 joerg #include "clang/AST/Type.h"
29 1.1 joerg #include "clang/AST/TypeLoc.h"
30 1.1 joerg #include "clang/AST/UnresolvedSet.h"
31 1.1 joerg #include "clang/Basic/Diagnostic.h"
32 1.1 joerg #include "clang/Basic/IdentifierTable.h"
33 1.1 joerg #include "clang/Basic/LLVM.h"
34 1.1 joerg #include "clang/Basic/LangOptions.h"
35 1.1 joerg #include "clang/Basic/OperatorKinds.h"
36 1.1 joerg #include "clang/Basic/PartialDiagnostic.h"
37 1.1 joerg #include "clang/Basic/SourceLocation.h"
38 1.1 joerg #include "clang/Basic/Specifiers.h"
39 1.1 joerg #include "llvm/ADT/None.h"
40 1.1 joerg #include "llvm/ADT/SmallPtrSet.h"
41 1.1 joerg #include "llvm/ADT/SmallVector.h"
42 1.1 joerg #include "llvm/ADT/iterator_range.h"
43 1.1 joerg #include "llvm/Support/Casting.h"
44 1.1 joerg #include "llvm/Support/ErrorHandling.h"
45 1.1.1.2 joerg #include "llvm/Support/Format.h"
46 1.1 joerg #include "llvm/Support/raw_ostream.h"
47 1.1 joerg #include <algorithm>
48 1.1 joerg #include <cassert>
49 1.1 joerg #include <cstddef>
50 1.1 joerg #include <cstdint>
51 1.1 joerg
52 1.1 joerg using namespace clang;
53 1.1 joerg
54 1.1 joerg //===----------------------------------------------------------------------===//
55 1.1 joerg // Decl Allocation/Deallocation Method Implementations
56 1.1 joerg //===----------------------------------------------------------------------===//
57 1.1 joerg
58 1.1 joerg void AccessSpecDecl::anchor() {}
59 1.1 joerg
60 1.1 joerg AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
61 1.1 joerg return new (C, ID) AccessSpecDecl(EmptyShell());
62 1.1 joerg }
63 1.1 joerg
64 1.1 joerg void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
65 1.1 joerg ExternalASTSource *Source = C.getExternalSource();
66 1.1 joerg assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
67 1.1 joerg assert(Source && "getFromExternalSource with no external source");
68 1.1 joerg
69 1.1 joerg for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
70 1.1 joerg I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
71 1.1 joerg reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
72 1.1 joerg Impl.Decls.setLazy(false);
73 1.1 joerg }
74 1.1 joerg
75 1.1 joerg CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
76 1.1 joerg : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
77 1.1 joerg Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
78 1.1 joerg Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true),
79 1.1 joerg HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false),
80 1.1 joerg HasPrivateFields(false), HasProtectedFields(false),
81 1.1 joerg HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
82 1.1 joerg HasOnlyCMembers(true), HasInClassInitializer(false),
83 1.1 joerg HasUninitializedReferenceMember(false), HasUninitializedFields(false),
84 1.1.1.2 joerg HasInheritedConstructor(false),
85 1.1.1.2 joerg HasInheritedDefaultConstructor(false),
86 1.1.1.2 joerg HasInheritedAssignment(false),
87 1.1 joerg NeedOverloadResolutionForCopyConstructor(false),
88 1.1 joerg NeedOverloadResolutionForMoveConstructor(false),
89 1.1.1.2 joerg NeedOverloadResolutionForCopyAssignment(false),
90 1.1 joerg NeedOverloadResolutionForMoveAssignment(false),
91 1.1 joerg NeedOverloadResolutionForDestructor(false),
92 1.1 joerg DefaultedCopyConstructorIsDeleted(false),
93 1.1 joerg DefaultedMoveConstructorIsDeleted(false),
94 1.1.1.2 joerg DefaultedCopyAssignmentIsDeleted(false),
95 1.1 joerg DefaultedMoveAssignmentIsDeleted(false),
96 1.1 joerg DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
97 1.1 joerg HasTrivialSpecialMembersForCall(SMF_All),
98 1.1 joerg DeclaredNonTrivialSpecialMembers(0),
99 1.1 joerg DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true),
100 1.1 joerg HasConstexprNonCopyMoveConstructor(false),
101 1.1 joerg HasDefaultedDefaultConstructor(false),
102 1.1 joerg DefaultedDefaultConstructorIsConstexpr(true),
103 1.1 joerg HasConstexprDefaultConstructor(false),
104 1.1 joerg DefaultedDestructorIsConstexpr(true),
105 1.1.1.2 joerg HasNonLiteralTypeFieldsOrBases(false), StructuralIfLiteral(true),
106 1.1 joerg UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
107 1.1 joerg ImplicitCopyConstructorCanHaveConstParamForVBase(true),
108 1.1 joerg ImplicitCopyConstructorCanHaveConstParamForNonVBase(true),
109 1.1 joerg ImplicitCopyAssignmentHasConstParam(true),
110 1.1 joerg HasDeclaredCopyConstructorWithConstParam(false),
111 1.1 joerg HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
112 1.1 joerg IsParsingBaseSpecifiers(false), ComputedVisibleConversions(false),
113 1.1 joerg HasODRHash(false), Definition(D) {}
114 1.1 joerg
115 1.1 joerg CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
116 1.1 joerg return Bases.get(Definition->getASTContext().getExternalSource());
117 1.1 joerg }
118 1.1 joerg
119 1.1 joerg CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
120 1.1 joerg return VBases.get(Definition->getASTContext().getExternalSource());
121 1.1 joerg }
122 1.1 joerg
123 1.1 joerg CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
124 1.1 joerg DeclContext *DC, SourceLocation StartLoc,
125 1.1 joerg SourceLocation IdLoc, IdentifierInfo *Id,
126 1.1 joerg CXXRecordDecl *PrevDecl)
127 1.1 joerg : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
128 1.1 joerg DefinitionData(PrevDecl ? PrevDecl->DefinitionData
129 1.1 joerg : nullptr) {}
130 1.1 joerg
131 1.1 joerg CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
132 1.1 joerg DeclContext *DC, SourceLocation StartLoc,
133 1.1 joerg SourceLocation IdLoc, IdentifierInfo *Id,
134 1.1 joerg CXXRecordDecl *PrevDecl,
135 1.1 joerg bool DelayTypeCreation) {
136 1.1 joerg auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc, IdLoc, Id,
137 1.1 joerg PrevDecl);
138 1.1 joerg R->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
139 1.1 joerg
140 1.1 joerg // FIXME: DelayTypeCreation seems like such a hack
141 1.1 joerg if (!DelayTypeCreation)
142 1.1 joerg C.getTypeDeclType(R, PrevDecl);
143 1.1 joerg return R;
144 1.1 joerg }
145 1.1 joerg
146 1.1 joerg CXXRecordDecl *
147 1.1 joerg CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
148 1.1 joerg TypeSourceInfo *Info, SourceLocation Loc,
149 1.1 joerg bool Dependent, bool IsGeneric,
150 1.1 joerg LambdaCaptureDefault CaptureDefault) {
151 1.1 joerg auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
152 1.1 joerg nullptr, nullptr);
153 1.1 joerg R->setBeingDefined(true);
154 1.1 joerg R->DefinitionData =
155 1.1 joerg new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
156 1.1 joerg CaptureDefault);
157 1.1 joerg R->setMayHaveOutOfDateDef(false);
158 1.1 joerg R->setImplicit(true);
159 1.1 joerg C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
160 1.1 joerg return R;
161 1.1 joerg }
162 1.1 joerg
163 1.1 joerg CXXRecordDecl *
164 1.1 joerg CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
165 1.1 joerg auto *R = new (C, ID) CXXRecordDecl(
166 1.1 joerg CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
167 1.1 joerg nullptr, nullptr);
168 1.1 joerg R->setMayHaveOutOfDateDef(false);
169 1.1 joerg return R;
170 1.1 joerg }
171 1.1 joerg
172 1.1 joerg /// Determine whether a class has a repeated base class. This is intended for
173 1.1 joerg /// use when determining if a class is standard-layout, so makes no attempt to
174 1.1 joerg /// handle virtual bases.
175 1.1 joerg static bool hasRepeatedBaseClass(const CXXRecordDecl *StartRD) {
176 1.1 joerg llvm::SmallPtrSet<const CXXRecordDecl*, 8> SeenBaseTypes;
177 1.1 joerg SmallVector<const CXXRecordDecl*, 8> WorkList = {StartRD};
178 1.1 joerg while (!WorkList.empty()) {
179 1.1 joerg const CXXRecordDecl *RD = WorkList.pop_back_val();
180 1.1 joerg for (const CXXBaseSpecifier &BaseSpec : RD->bases()) {
181 1.1 joerg if (const CXXRecordDecl *B = BaseSpec.getType()->getAsCXXRecordDecl()) {
182 1.1 joerg if (!SeenBaseTypes.insert(B).second)
183 1.1 joerg return true;
184 1.1 joerg WorkList.push_back(B);
185 1.1 joerg }
186 1.1 joerg }
187 1.1 joerg }
188 1.1 joerg return false;
189 1.1 joerg }
190 1.1 joerg
191 1.1 joerg void
192 1.1 joerg CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
193 1.1 joerg unsigned NumBases) {
194 1.1 joerg ASTContext &C = getASTContext();
195 1.1 joerg
196 1.1 joerg if (!data().Bases.isOffset() && data().NumBases > 0)
197 1.1 joerg C.Deallocate(data().getBases());
198 1.1 joerg
199 1.1 joerg if (NumBases) {
200 1.1 joerg if (!C.getLangOpts().CPlusPlus17) {
201 1.1 joerg // C++ [dcl.init.aggr]p1:
202 1.1 joerg // An aggregate is [...] a class with [...] no base classes [...].
203 1.1 joerg data().Aggregate = false;
204 1.1 joerg }
205 1.1 joerg
206 1.1 joerg // C++ [class]p4:
207 1.1 joerg // A POD-struct is an aggregate class...
208 1.1 joerg data().PlainOldData = false;
209 1.1 joerg }
210 1.1 joerg
211 1.1 joerg // The set of seen virtual base types.
212 1.1 joerg llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
213 1.1 joerg
214 1.1 joerg // The virtual bases of this class.
215 1.1 joerg SmallVector<const CXXBaseSpecifier *, 8> VBases;
216 1.1 joerg
217 1.1 joerg data().Bases = new(C) CXXBaseSpecifier [NumBases];
218 1.1 joerg data().NumBases = NumBases;
219 1.1 joerg for (unsigned i = 0; i < NumBases; ++i) {
220 1.1 joerg data().getBases()[i] = *Bases[i];
221 1.1 joerg // Keep track of inherited vbases for this base class.
222 1.1 joerg const CXXBaseSpecifier *Base = Bases[i];
223 1.1 joerg QualType BaseType = Base->getType();
224 1.1 joerg // Skip dependent types; we can't do any checking on them now.
225 1.1 joerg if (BaseType->isDependentType())
226 1.1 joerg continue;
227 1.1 joerg auto *BaseClassDecl =
228 1.1 joerg cast<CXXRecordDecl>(BaseType->castAs<RecordType>()->getDecl());
229 1.1 joerg
230 1.1 joerg // C++2a [class]p7:
231 1.1 joerg // A standard-layout class is a class that:
232 1.1 joerg // [...]
233 1.1 joerg // -- has all non-static data members and bit-fields in the class and
234 1.1 joerg // its base classes first declared in the same class
235 1.1 joerg if (BaseClassDecl->data().HasBasesWithFields ||
236 1.1 joerg !BaseClassDecl->field_empty()) {
237 1.1 joerg if (data().HasBasesWithFields)
238 1.1 joerg // Two bases have members or bit-fields: not standard-layout.
239 1.1 joerg data().IsStandardLayout = false;
240 1.1 joerg data().HasBasesWithFields = true;
241 1.1 joerg }
242 1.1 joerg
243 1.1 joerg // C++11 [class]p7:
244 1.1 joerg // A standard-layout class is a class that:
245 1.1 joerg // -- [...] has [...] at most one base class with non-static data
246 1.1 joerg // members
247 1.1 joerg if (BaseClassDecl->data().HasBasesWithNonStaticDataMembers ||
248 1.1 joerg BaseClassDecl->hasDirectFields()) {
249 1.1 joerg if (data().HasBasesWithNonStaticDataMembers)
250 1.1 joerg data().IsCXX11StandardLayout = false;
251 1.1 joerg data().HasBasesWithNonStaticDataMembers = true;
252 1.1 joerg }
253 1.1 joerg
254 1.1 joerg if (!BaseClassDecl->isEmpty()) {
255 1.1 joerg // C++14 [meta.unary.prop]p4:
256 1.1 joerg // T is a class type [...] with [...] no base class B for which
257 1.1 joerg // is_empty<B>::value is false.
258 1.1 joerg data().Empty = false;
259 1.1 joerg }
260 1.1 joerg
261 1.1 joerg // C++1z [dcl.init.agg]p1:
262 1.1 joerg // An aggregate is a class with [...] no private or protected base classes
263 1.1.1.2 joerg if (Base->getAccessSpecifier() != AS_public) {
264 1.1 joerg data().Aggregate = false;
265 1.1 joerg
266 1.1.1.2 joerg // C++20 [temp.param]p7:
267 1.1.1.2 joerg // A structural type is [...] a literal class type with [...] all base
268 1.1.1.2 joerg // classes [...] public
269 1.1.1.2 joerg data().StructuralIfLiteral = false;
270 1.1.1.2 joerg }
271 1.1.1.2 joerg
272 1.1 joerg // C++ [class.virtual]p1:
273 1.1 joerg // A class that declares or inherits a virtual function is called a
274 1.1 joerg // polymorphic class.
275 1.1 joerg if (BaseClassDecl->isPolymorphic()) {
276 1.1 joerg data().Polymorphic = true;
277 1.1 joerg
278 1.1 joerg // An aggregate is a class with [...] no virtual functions.
279 1.1 joerg data().Aggregate = false;
280 1.1 joerg }
281 1.1 joerg
282 1.1 joerg // C++0x [class]p7:
283 1.1 joerg // A standard-layout class is a class that: [...]
284 1.1 joerg // -- has no non-standard-layout base classes
285 1.1 joerg if (!BaseClassDecl->isStandardLayout())
286 1.1 joerg data().IsStandardLayout = false;
287 1.1 joerg if (!BaseClassDecl->isCXX11StandardLayout())
288 1.1 joerg data().IsCXX11StandardLayout = false;
289 1.1 joerg
290 1.1 joerg // Record if this base is the first non-literal field or base.
291 1.1 joerg if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
292 1.1 joerg data().HasNonLiteralTypeFieldsOrBases = true;
293 1.1 joerg
294 1.1 joerg // Now go through all virtual bases of this base and add them.
295 1.1 joerg for (const auto &VBase : BaseClassDecl->vbases()) {
296 1.1 joerg // Add this base if it's not already in the list.
297 1.1 joerg if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
298 1.1 joerg VBases.push_back(&VBase);
299 1.1 joerg
300 1.1 joerg // C++11 [class.copy]p8:
301 1.1 joerg // The implicitly-declared copy constructor for a class X will have
302 1.1 joerg // the form 'X::X(const X&)' if each [...] virtual base class B of X
303 1.1 joerg // has a copy constructor whose first parameter is of type
304 1.1 joerg // 'const B&' or 'const volatile B&' [...]
305 1.1 joerg if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
306 1.1 joerg if (!VBaseDecl->hasCopyConstructorWithConstParam())
307 1.1 joerg data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
308 1.1 joerg
309 1.1 joerg // C++1z [dcl.init.agg]p1:
310 1.1 joerg // An aggregate is a class with [...] no virtual base classes
311 1.1 joerg data().Aggregate = false;
312 1.1 joerg }
313 1.1 joerg }
314 1.1 joerg
315 1.1 joerg if (Base->isVirtual()) {
316 1.1 joerg // Add this base if it's not already in the list.
317 1.1 joerg if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
318 1.1 joerg VBases.push_back(Base);
319 1.1 joerg
320 1.1 joerg // C++14 [meta.unary.prop] is_empty:
321 1.1 joerg // T is a class type, but not a union type, with ... no virtual base
322 1.1 joerg // classes
323 1.1 joerg data().Empty = false;
324 1.1 joerg
325 1.1 joerg // C++1z [dcl.init.agg]p1:
326 1.1 joerg // An aggregate is a class with [...] no virtual base classes
327 1.1 joerg data().Aggregate = false;
328 1.1 joerg
329 1.1 joerg // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
330 1.1 joerg // A [default constructor, copy/move constructor, or copy/move assignment
331 1.1 joerg // operator for a class X] is trivial [...] if:
332 1.1 joerg // -- class X has [...] no virtual base classes
333 1.1 joerg data().HasTrivialSpecialMembers &= SMF_Destructor;
334 1.1 joerg data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
335 1.1 joerg
336 1.1 joerg // C++0x [class]p7:
337 1.1 joerg // A standard-layout class is a class that: [...]
338 1.1 joerg // -- has [...] no virtual base classes
339 1.1 joerg data().IsStandardLayout = false;
340 1.1 joerg data().IsCXX11StandardLayout = false;
341 1.1 joerg
342 1.1 joerg // C++20 [dcl.constexpr]p3:
343 1.1 joerg // In the definition of a constexpr function [...]
344 1.1 joerg // -- if the function is a constructor or destructor,
345 1.1 joerg // its class shall not have any virtual base classes
346 1.1 joerg data().DefaultedDefaultConstructorIsConstexpr = false;
347 1.1 joerg data().DefaultedDestructorIsConstexpr = false;
348 1.1 joerg
349 1.1 joerg // C++1z [class.copy]p8:
350 1.1 joerg // The implicitly-declared copy constructor for a class X will have
351 1.1 joerg // the form 'X::X(const X&)' if each potentially constructed subobject
352 1.1 joerg // has a copy constructor whose first parameter is of type
353 1.1 joerg // 'const B&' or 'const volatile B&' [...]
354 1.1 joerg if (!BaseClassDecl->hasCopyConstructorWithConstParam())
355 1.1 joerg data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
356 1.1 joerg } else {
357 1.1 joerg // C++ [class.ctor]p5:
358 1.1 joerg // A default constructor is trivial [...] if:
359 1.1 joerg // -- all the direct base classes of its class have trivial default
360 1.1 joerg // constructors.
361 1.1 joerg if (!BaseClassDecl->hasTrivialDefaultConstructor())
362 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
363 1.1 joerg
364 1.1 joerg // C++0x [class.copy]p13:
365 1.1 joerg // A copy/move constructor for class X is trivial if [...]
366 1.1 joerg // [...]
367 1.1 joerg // -- the constructor selected to copy/move each direct base class
368 1.1 joerg // subobject is trivial, and
369 1.1 joerg if (!BaseClassDecl->hasTrivialCopyConstructor())
370 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
371 1.1 joerg
372 1.1 joerg if (!BaseClassDecl->hasTrivialCopyConstructorForCall())
373 1.1 joerg data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
374 1.1 joerg
375 1.1 joerg // If the base class doesn't have a simple move constructor, we'll eagerly
376 1.1 joerg // declare it and perform overload resolution to determine which function
377 1.1 joerg // it actually calls. If it does have a simple move constructor, this
378 1.1 joerg // check is correct.
379 1.1 joerg if (!BaseClassDecl->hasTrivialMoveConstructor())
380 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
381 1.1 joerg
382 1.1 joerg if (!BaseClassDecl->hasTrivialMoveConstructorForCall())
383 1.1 joerg data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
384 1.1 joerg
385 1.1 joerg // C++0x [class.copy]p27:
386 1.1 joerg // A copy/move assignment operator for class X is trivial if [...]
387 1.1 joerg // [...]
388 1.1 joerg // -- the assignment operator selected to copy/move each direct base
389 1.1 joerg // class subobject is trivial, and
390 1.1 joerg if (!BaseClassDecl->hasTrivialCopyAssignment())
391 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
392 1.1 joerg // If the base class doesn't have a simple move assignment, we'll eagerly
393 1.1 joerg // declare it and perform overload resolution to determine which function
394 1.1 joerg // it actually calls. If it does have a simple move assignment, this
395 1.1 joerg // check is correct.
396 1.1 joerg if (!BaseClassDecl->hasTrivialMoveAssignment())
397 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
398 1.1 joerg
399 1.1 joerg // C++11 [class.ctor]p6:
400 1.1 joerg // If that user-written default constructor would satisfy the
401 1.1 joerg // requirements of a constexpr constructor, the implicitly-defined
402 1.1 joerg // default constructor is constexpr.
403 1.1 joerg if (!BaseClassDecl->hasConstexprDefaultConstructor())
404 1.1 joerg data().DefaultedDefaultConstructorIsConstexpr = false;
405 1.1 joerg
406 1.1 joerg // C++1z [class.copy]p8:
407 1.1 joerg // The implicitly-declared copy constructor for a class X will have
408 1.1 joerg // the form 'X::X(const X&)' if each potentially constructed subobject
409 1.1 joerg // has a copy constructor whose first parameter is of type
410 1.1 joerg // 'const B&' or 'const volatile B&' [...]
411 1.1 joerg if (!BaseClassDecl->hasCopyConstructorWithConstParam())
412 1.1 joerg data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
413 1.1 joerg }
414 1.1 joerg
415 1.1 joerg // C++ [class.ctor]p3:
416 1.1 joerg // A destructor is trivial if all the direct base classes of its class
417 1.1 joerg // have trivial destructors.
418 1.1 joerg if (!BaseClassDecl->hasTrivialDestructor())
419 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_Destructor;
420 1.1 joerg
421 1.1 joerg if (!BaseClassDecl->hasTrivialDestructorForCall())
422 1.1 joerg data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
423 1.1 joerg
424 1.1 joerg if (!BaseClassDecl->hasIrrelevantDestructor())
425 1.1 joerg data().HasIrrelevantDestructor = false;
426 1.1 joerg
427 1.1 joerg // C++11 [class.copy]p18:
428 1.1.1.2 joerg // The implicitly-declared copy assignment operator for a class X will
429 1.1 joerg // have the form 'X& X::operator=(const X&)' if each direct base class B
430 1.1 joerg // of X has a copy assignment operator whose parameter is of type 'const
431 1.1 joerg // B&', 'const volatile B&', or 'B' [...]
432 1.1 joerg if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
433 1.1 joerg data().ImplicitCopyAssignmentHasConstParam = false;
434 1.1 joerg
435 1.1 joerg // A class has an Objective-C object member if... or any of its bases
436 1.1 joerg // has an Objective-C object member.
437 1.1 joerg if (BaseClassDecl->hasObjectMember())
438 1.1 joerg setHasObjectMember(true);
439 1.1 joerg
440 1.1 joerg if (BaseClassDecl->hasVolatileMember())
441 1.1 joerg setHasVolatileMember(true);
442 1.1 joerg
443 1.1 joerg if (BaseClassDecl->getArgPassingRestrictions() ==
444 1.1 joerg RecordDecl::APK_CanNeverPassInRegs)
445 1.1 joerg setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
446 1.1 joerg
447 1.1 joerg // Keep track of the presence of mutable fields.
448 1.1.1.2 joerg if (BaseClassDecl->hasMutableFields())
449 1.1 joerg data().HasMutableFields = true;
450 1.1 joerg
451 1.1 joerg if (BaseClassDecl->hasUninitializedReferenceMember())
452 1.1 joerg data().HasUninitializedReferenceMember = true;
453 1.1 joerg
454 1.1 joerg if (!BaseClassDecl->allowConstDefaultInit())
455 1.1 joerg data().HasUninitializedFields = true;
456 1.1 joerg
457 1.1 joerg addedClassSubobject(BaseClassDecl);
458 1.1 joerg }
459 1.1 joerg
460 1.1 joerg // C++2a [class]p7:
461 1.1 joerg // A class S is a standard-layout class if it:
462 1.1 joerg // -- has at most one base class subobject of any given type
463 1.1 joerg //
464 1.1 joerg // Note that we only need to check this for classes with more than one base
465 1.1 joerg // class. If there's only one base class, and it's standard layout, then
466 1.1 joerg // we know there are no repeated base classes.
467 1.1 joerg if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this))
468 1.1 joerg data().IsStandardLayout = false;
469 1.1 joerg
470 1.1 joerg if (VBases.empty()) {
471 1.1 joerg data().IsParsingBaseSpecifiers = false;
472 1.1 joerg return;
473 1.1 joerg }
474 1.1 joerg
475 1.1 joerg // Create base specifier for any direct or indirect virtual bases.
476 1.1 joerg data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
477 1.1 joerg data().NumVBases = VBases.size();
478 1.1 joerg for (int I = 0, E = VBases.size(); I != E; ++I) {
479 1.1 joerg QualType Type = VBases[I]->getType();
480 1.1 joerg if (!Type->isDependentType())
481 1.1 joerg addedClassSubobject(Type->getAsCXXRecordDecl());
482 1.1 joerg data().getVBases()[I] = *VBases[I];
483 1.1 joerg }
484 1.1 joerg
485 1.1 joerg data().IsParsingBaseSpecifiers = false;
486 1.1 joerg }
487 1.1 joerg
488 1.1 joerg unsigned CXXRecordDecl::getODRHash() const {
489 1.1 joerg assert(hasDefinition() && "ODRHash only for records with definitions");
490 1.1 joerg
491 1.1 joerg // Previously calculated hash is stored in DefinitionData.
492 1.1 joerg if (DefinitionData->HasODRHash)
493 1.1 joerg return DefinitionData->ODRHash;
494 1.1 joerg
495 1.1 joerg // Only calculate hash on first call of getODRHash per record.
496 1.1 joerg ODRHash Hash;
497 1.1 joerg Hash.AddCXXRecordDecl(getDefinition());
498 1.1 joerg DefinitionData->HasODRHash = true;
499 1.1 joerg DefinitionData->ODRHash = Hash.CalculateHash();
500 1.1 joerg
501 1.1 joerg return DefinitionData->ODRHash;
502 1.1 joerg }
503 1.1 joerg
504 1.1 joerg void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
505 1.1 joerg // C++11 [class.copy]p11:
506 1.1 joerg // A defaulted copy/move constructor for a class X is defined as
507 1.1 joerg // deleted if X has:
508 1.1 joerg // -- a direct or virtual base class B that cannot be copied/moved [...]
509 1.1 joerg // -- a non-static data member of class type M (or array thereof)
510 1.1 joerg // that cannot be copied or moved [...]
511 1.1 joerg if (!Subobj->hasSimpleCopyConstructor())
512 1.1 joerg data().NeedOverloadResolutionForCopyConstructor = true;
513 1.1 joerg if (!Subobj->hasSimpleMoveConstructor())
514 1.1 joerg data().NeedOverloadResolutionForMoveConstructor = true;
515 1.1 joerg
516 1.1 joerg // C++11 [class.copy]p23:
517 1.1 joerg // A defaulted copy/move assignment operator for a class X is defined as
518 1.1 joerg // deleted if X has:
519 1.1 joerg // -- a direct or virtual base class B that cannot be copied/moved [...]
520 1.1 joerg // -- a non-static data member of class type M (or array thereof)
521 1.1 joerg // that cannot be copied or moved [...]
522 1.1.1.2 joerg if (!Subobj->hasSimpleCopyAssignment())
523 1.1.1.2 joerg data().NeedOverloadResolutionForCopyAssignment = true;
524 1.1 joerg if (!Subobj->hasSimpleMoveAssignment())
525 1.1 joerg data().NeedOverloadResolutionForMoveAssignment = true;
526 1.1 joerg
527 1.1 joerg // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
528 1.1 joerg // A defaulted [ctor or dtor] for a class X is defined as
529 1.1 joerg // deleted if X has:
530 1.1 joerg // -- any direct or virtual base class [...] has a type with a destructor
531 1.1 joerg // that is deleted or inaccessible from the defaulted [ctor or dtor].
532 1.1 joerg // -- any non-static data member has a type with a destructor
533 1.1 joerg // that is deleted or inaccessible from the defaulted [ctor or dtor].
534 1.1 joerg if (!Subobj->hasSimpleDestructor()) {
535 1.1 joerg data().NeedOverloadResolutionForCopyConstructor = true;
536 1.1 joerg data().NeedOverloadResolutionForMoveConstructor = true;
537 1.1 joerg data().NeedOverloadResolutionForDestructor = true;
538 1.1 joerg }
539 1.1 joerg
540 1.1 joerg // C++2a [dcl.constexpr]p4:
541 1.1 joerg // The definition of a constexpr destructor [shall] satisfy the
542 1.1 joerg // following requirement:
543 1.1 joerg // -- for every subobject of class type or (possibly multi-dimensional)
544 1.1 joerg // array thereof, that class type shall have a constexpr destructor
545 1.1 joerg if (!Subobj->hasConstexprDestructor())
546 1.1 joerg data().DefaultedDestructorIsConstexpr = false;
547 1.1.1.2 joerg
548 1.1.1.2 joerg // C++20 [temp.param]p7:
549 1.1.1.2 joerg // A structural type is [...] a literal class type [for which] the types
550 1.1.1.2 joerg // of all base classes and non-static data members are structural types or
551 1.1.1.2 joerg // (possibly multi-dimensional) array thereof
552 1.1.1.2 joerg if (!Subobj->data().StructuralIfLiteral)
553 1.1.1.2 joerg data().StructuralIfLiteral = false;
554 1.1 joerg }
555 1.1 joerg
556 1.1 joerg bool CXXRecordDecl::hasConstexprDestructor() const {
557 1.1 joerg auto *Dtor = getDestructor();
558 1.1 joerg return Dtor ? Dtor->isConstexpr() : defaultedDestructorIsConstexpr();
559 1.1 joerg }
560 1.1 joerg
561 1.1 joerg bool CXXRecordDecl::hasAnyDependentBases() const {
562 1.1 joerg if (!isDependentContext())
563 1.1 joerg return false;
564 1.1 joerg
565 1.1 joerg return !forallBases([](const CXXRecordDecl *) { return true; });
566 1.1 joerg }
567 1.1 joerg
568 1.1 joerg bool CXXRecordDecl::isTriviallyCopyable() const {
569 1.1 joerg // C++0x [class]p5:
570 1.1 joerg // A trivially copyable class is a class that:
571 1.1 joerg // -- has no non-trivial copy constructors,
572 1.1 joerg if (hasNonTrivialCopyConstructor()) return false;
573 1.1 joerg // -- has no non-trivial move constructors,
574 1.1 joerg if (hasNonTrivialMoveConstructor()) return false;
575 1.1 joerg // -- has no non-trivial copy assignment operators,
576 1.1 joerg if (hasNonTrivialCopyAssignment()) return false;
577 1.1 joerg // -- has no non-trivial move assignment operators, and
578 1.1 joerg if (hasNonTrivialMoveAssignment()) return false;
579 1.1 joerg // -- has a trivial destructor.
580 1.1 joerg if (!hasTrivialDestructor()) return false;
581 1.1 joerg
582 1.1 joerg return true;
583 1.1 joerg }
584 1.1 joerg
585 1.1 joerg void CXXRecordDecl::markedVirtualFunctionPure() {
586 1.1 joerg // C++ [class.abstract]p2:
587 1.1 joerg // A class is abstract if it has at least one pure virtual function.
588 1.1 joerg data().Abstract = true;
589 1.1 joerg }
590 1.1 joerg
591 1.1 joerg bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
592 1.1 joerg ASTContext &Ctx, const CXXRecordDecl *XFirst) {
593 1.1 joerg if (!getNumBases())
594 1.1 joerg return false;
595 1.1 joerg
596 1.1 joerg llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases;
597 1.1 joerg llvm::SmallPtrSet<const CXXRecordDecl*, 8> M;
598 1.1 joerg SmallVector<const CXXRecordDecl*, 8> WorkList;
599 1.1 joerg
600 1.1 joerg // Visit a type that we have determined is an element of M(S).
601 1.1 joerg auto Visit = [&](const CXXRecordDecl *RD) -> bool {
602 1.1 joerg RD = RD->getCanonicalDecl();
603 1.1 joerg
604 1.1 joerg // C++2a [class]p8:
605 1.1 joerg // A class S is a standard-layout class if it [...] has no element of the
606 1.1 joerg // set M(S) of types as a base class.
607 1.1 joerg //
608 1.1 joerg // If we find a subobject of an empty type, it might also be a base class,
609 1.1 joerg // so we'll need to walk the base classes to check.
610 1.1 joerg if (!RD->data().HasBasesWithFields) {
611 1.1 joerg // Walk the bases the first time, stopping if we find the type. Build a
612 1.1 joerg // set of them so we don't need to walk them again.
613 1.1 joerg if (Bases.empty()) {
614 1.1 joerg bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool {
615 1.1 joerg Base = Base->getCanonicalDecl();
616 1.1 joerg if (RD == Base)
617 1.1 joerg return false;
618 1.1 joerg Bases.insert(Base);
619 1.1 joerg return true;
620 1.1 joerg });
621 1.1 joerg if (RDIsBase)
622 1.1 joerg return true;
623 1.1 joerg } else {
624 1.1 joerg if (Bases.count(RD))
625 1.1 joerg return true;
626 1.1 joerg }
627 1.1 joerg }
628 1.1 joerg
629 1.1 joerg if (M.insert(RD).second)
630 1.1 joerg WorkList.push_back(RD);
631 1.1 joerg return false;
632 1.1 joerg };
633 1.1 joerg
634 1.1 joerg if (Visit(XFirst))
635 1.1 joerg return true;
636 1.1 joerg
637 1.1 joerg while (!WorkList.empty()) {
638 1.1 joerg const CXXRecordDecl *X = WorkList.pop_back_val();
639 1.1 joerg
640 1.1 joerg // FIXME: We don't check the bases of X. That matches the standard, but
641 1.1 joerg // that sure looks like a wording bug.
642 1.1 joerg
643 1.1 joerg // -- If X is a non-union class type with a non-static data member
644 1.1 joerg // [recurse to each field] that is either of zero size or is the
645 1.1 joerg // first non-static data member of X
646 1.1 joerg // -- If X is a union type, [recurse to union members]
647 1.1 joerg bool IsFirstField = true;
648 1.1 joerg for (auto *FD : X->fields()) {
649 1.1 joerg // FIXME: Should we really care about the type of the first non-static
650 1.1 joerg // data member of a non-union if there are preceding unnamed bit-fields?
651 1.1 joerg if (FD->isUnnamedBitfield())
652 1.1 joerg continue;
653 1.1 joerg
654 1.1 joerg if (!IsFirstField && !FD->isZeroSize(Ctx))
655 1.1 joerg continue;
656 1.1 joerg
657 1.1 joerg // -- If X is n array type, [visit the element type]
658 1.1 joerg QualType T = Ctx.getBaseElementType(FD->getType());
659 1.1 joerg if (auto *RD = T->getAsCXXRecordDecl())
660 1.1 joerg if (Visit(RD))
661 1.1 joerg return true;
662 1.1 joerg
663 1.1 joerg if (!X->isUnion())
664 1.1 joerg IsFirstField = false;
665 1.1 joerg }
666 1.1 joerg }
667 1.1 joerg
668 1.1 joerg return false;
669 1.1 joerg }
670 1.1 joerg
671 1.1 joerg bool CXXRecordDecl::lambdaIsDefaultConstructibleAndAssignable() const {
672 1.1 joerg assert(isLambda() && "not a lambda");
673 1.1 joerg
674 1.1 joerg // C++2a [expr.prim.lambda.capture]p11:
675 1.1 joerg // The closure type associated with a lambda-expression has no default
676 1.1 joerg // constructor if the lambda-expression has a lambda-capture and a
677 1.1 joerg // defaulted default constructor otherwise. It has a deleted copy
678 1.1 joerg // assignment operator if the lambda-expression has a lambda-capture and
679 1.1 joerg // defaulted copy and move assignment operators otherwise.
680 1.1 joerg //
681 1.1 joerg // C++17 [expr.prim.lambda]p21:
682 1.1 joerg // The closure type associated with a lambda-expression has no default
683 1.1 joerg // constructor and a deleted copy assignment operator.
684 1.1.1.2 joerg if (getLambdaCaptureDefault() != LCD_None || capture_size() != 0)
685 1.1 joerg return false;
686 1.1.1.2 joerg return getASTContext().getLangOpts().CPlusPlus20;
687 1.1 joerg }
688 1.1 joerg
689 1.1 joerg void CXXRecordDecl::addedMember(Decl *D) {
690 1.1 joerg if (!D->isImplicit() &&
691 1.1 joerg !isa<FieldDecl>(D) &&
692 1.1 joerg !isa<IndirectFieldDecl>(D) &&
693 1.1 joerg (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
694 1.1 joerg cast<TagDecl>(D)->getTagKind() == TTK_Interface))
695 1.1 joerg data().HasOnlyCMembers = false;
696 1.1 joerg
697 1.1 joerg // Ignore friends and invalid declarations.
698 1.1 joerg if (D->getFriendObjectKind() || D->isInvalidDecl())
699 1.1 joerg return;
700 1.1 joerg
701 1.1 joerg auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
702 1.1 joerg if (FunTmpl)
703 1.1 joerg D = FunTmpl->getTemplatedDecl();
704 1.1 joerg
705 1.1 joerg // FIXME: Pass NamedDecl* to addedMember?
706 1.1 joerg Decl *DUnderlying = D;
707 1.1 joerg if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
708 1.1 joerg DUnderlying = ND->getUnderlyingDecl();
709 1.1 joerg if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying))
710 1.1 joerg DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
711 1.1 joerg }
712 1.1 joerg
713 1.1 joerg if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
714 1.1 joerg if (Method->isVirtual()) {
715 1.1 joerg // C++ [dcl.init.aggr]p1:
716 1.1 joerg // An aggregate is an array or a class with [...] no virtual functions.
717 1.1 joerg data().Aggregate = false;
718 1.1 joerg
719 1.1 joerg // C++ [class]p4:
720 1.1 joerg // A POD-struct is an aggregate class...
721 1.1 joerg data().PlainOldData = false;
722 1.1 joerg
723 1.1 joerg // C++14 [meta.unary.prop]p4:
724 1.1 joerg // T is a class type [...] with [...] no virtual member functions...
725 1.1 joerg data().Empty = false;
726 1.1 joerg
727 1.1 joerg // C++ [class.virtual]p1:
728 1.1 joerg // A class that declares or inherits a virtual function is called a
729 1.1 joerg // polymorphic class.
730 1.1 joerg data().Polymorphic = true;
731 1.1 joerg
732 1.1 joerg // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
733 1.1 joerg // A [default constructor, copy/move constructor, or copy/move
734 1.1 joerg // assignment operator for a class X] is trivial [...] if:
735 1.1 joerg // -- class X has no virtual functions [...]
736 1.1 joerg data().HasTrivialSpecialMembers &= SMF_Destructor;
737 1.1 joerg data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
738 1.1 joerg
739 1.1 joerg // C++0x [class]p7:
740 1.1 joerg // A standard-layout class is a class that: [...]
741 1.1 joerg // -- has no virtual functions
742 1.1 joerg data().IsStandardLayout = false;
743 1.1 joerg data().IsCXX11StandardLayout = false;
744 1.1 joerg }
745 1.1 joerg }
746 1.1 joerg
747 1.1 joerg // Notify the listener if an implicit member was added after the definition
748 1.1 joerg // was completed.
749 1.1 joerg if (!isBeingDefined() && D->isImplicit())
750 1.1 joerg if (ASTMutationListener *L = getASTMutationListener())
751 1.1 joerg L->AddedCXXImplicitMember(data().Definition, D);
752 1.1 joerg
753 1.1 joerg // The kind of special member this declaration is, if any.
754 1.1 joerg unsigned SMKind = 0;
755 1.1 joerg
756 1.1 joerg // Handle constructors.
757 1.1 joerg if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
758 1.1.1.2 joerg if (Constructor->isInheritingConstructor()) {
759 1.1.1.2 joerg // Ignore constructor shadow declarations. They are lazily created and
760 1.1.1.2 joerg // so shouldn't affect any properties of the class.
761 1.1.1.2 joerg } else {
762 1.1.1.2 joerg if (!Constructor->isImplicit()) {
763 1.1.1.2 joerg // Note that we have a user-declared constructor.
764 1.1.1.2 joerg data().UserDeclaredConstructor = true;
765 1.1.1.2 joerg
766 1.1.1.2 joerg // C++ [class]p4:
767 1.1.1.2 joerg // A POD-struct is an aggregate class [...]
768 1.1.1.2 joerg // Since the POD bit is meant to be C++03 POD-ness, clear it even if
769 1.1.1.2 joerg // the type is technically an aggregate in C++0x since it wouldn't be
770 1.1.1.2 joerg // in 03.
771 1.1.1.2 joerg data().PlainOldData = false;
772 1.1.1.2 joerg }
773 1.1 joerg
774 1.1.1.2 joerg if (Constructor->isDefaultConstructor()) {
775 1.1.1.2 joerg SMKind |= SMF_DefaultConstructor;
776 1.1 joerg
777 1.1.1.2 joerg if (Constructor->isUserProvided())
778 1.1.1.2 joerg data().UserProvidedDefaultConstructor = true;
779 1.1.1.2 joerg if (Constructor->isConstexpr())
780 1.1.1.2 joerg data().HasConstexprDefaultConstructor = true;
781 1.1.1.2 joerg if (Constructor->isDefaulted())
782 1.1.1.2 joerg data().HasDefaultedDefaultConstructor = true;
783 1.1.1.2 joerg }
784 1.1 joerg
785 1.1.1.2 joerg if (!FunTmpl) {
786 1.1.1.2 joerg unsigned Quals;
787 1.1.1.2 joerg if (Constructor->isCopyConstructor(Quals)) {
788 1.1.1.2 joerg SMKind |= SMF_CopyConstructor;
789 1.1.1.2 joerg
790 1.1.1.2 joerg if (Quals & Qualifiers::Const)
791 1.1.1.2 joerg data().HasDeclaredCopyConstructorWithConstParam = true;
792 1.1.1.2 joerg } else if (Constructor->isMoveConstructor())
793 1.1.1.2 joerg SMKind |= SMF_MoveConstructor;
794 1.1.1.2 joerg }
795 1.1 joerg
796 1.1.1.2 joerg // C++11 [dcl.init.aggr]p1: DR1518
797 1.1.1.2 joerg // An aggregate is an array or a class with no user-provided [or]
798 1.1.1.2 joerg // explicit [...] constructors
799 1.1.1.2 joerg // C++20 [dcl.init.aggr]p1:
800 1.1.1.2 joerg // An aggregate is an array or a class with no user-declared [...]
801 1.1.1.2 joerg // constructors
802 1.1.1.2 joerg if (getASTContext().getLangOpts().CPlusPlus20
803 1.1.1.2 joerg ? !Constructor->isImplicit()
804 1.1.1.2 joerg : (Constructor->isUserProvided() || Constructor->isExplicit()))
805 1.1.1.2 joerg data().Aggregate = false;
806 1.1.1.2 joerg }
807 1.1 joerg }
808 1.1 joerg
809 1.1 joerg // Handle constructors, including those inherited from base classes.
810 1.1 joerg if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) {
811 1.1 joerg // Record if we see any constexpr constructors which are neither copy
812 1.1 joerg // nor move constructors.
813 1.1 joerg // C++1z [basic.types]p10:
814 1.1 joerg // [...] has at least one constexpr constructor or constructor template
815 1.1 joerg // (possibly inherited from a base class) that is not a copy or move
816 1.1 joerg // constructor [...]
817 1.1 joerg if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
818 1.1 joerg data().HasConstexprNonCopyMoveConstructor = true;
819 1.1.1.2 joerg if (!isa<CXXConstructorDecl>(D) && Constructor->isDefaultConstructor())
820 1.1.1.2 joerg data().HasInheritedDefaultConstructor = true;
821 1.1 joerg }
822 1.1 joerg
823 1.1 joerg // Handle destructors.
824 1.1 joerg if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
825 1.1 joerg SMKind |= SMF_Destructor;
826 1.1 joerg
827 1.1 joerg if (DD->isUserProvided())
828 1.1 joerg data().HasIrrelevantDestructor = false;
829 1.1 joerg // If the destructor is explicitly defaulted and not trivial or not public
830 1.1 joerg // or if the destructor is deleted, we clear HasIrrelevantDestructor in
831 1.1 joerg // finishedDefaultedOrDeletedMember.
832 1.1 joerg
833 1.1 joerg // C++11 [class.dtor]p5:
834 1.1 joerg // A destructor is trivial if [...] the destructor is not virtual.
835 1.1 joerg if (DD->isVirtual()) {
836 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_Destructor;
837 1.1 joerg data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
838 1.1 joerg }
839 1.1 joerg }
840 1.1 joerg
841 1.1 joerg // Handle member functions.
842 1.1 joerg if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
843 1.1 joerg if (Method->isCopyAssignmentOperator()) {
844 1.1 joerg SMKind |= SMF_CopyAssignment;
845 1.1 joerg
846 1.1 joerg const auto *ParamTy =
847 1.1 joerg Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
848 1.1 joerg if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
849 1.1 joerg data().HasDeclaredCopyAssignmentWithConstParam = true;
850 1.1 joerg }
851 1.1 joerg
852 1.1 joerg if (Method->isMoveAssignmentOperator())
853 1.1 joerg SMKind |= SMF_MoveAssignment;
854 1.1 joerg
855 1.1 joerg // Keep the list of conversion functions up-to-date.
856 1.1 joerg if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) {
857 1.1 joerg // FIXME: We use the 'unsafe' accessor for the access specifier here,
858 1.1 joerg // because Sema may not have set it yet. That's really just a misdesign
859 1.1 joerg // in Sema. However, LLDB *will* have set the access specifier correctly,
860 1.1 joerg // and adds declarations after the class is technically completed,
861 1.1 joerg // so completeDefinition()'s overriding of the access specifiers doesn't
862 1.1 joerg // work.
863 1.1 joerg AccessSpecifier AS = Conversion->getAccessUnsafe();
864 1.1 joerg
865 1.1 joerg if (Conversion->getPrimaryTemplate()) {
866 1.1 joerg // We don't record specializations.
867 1.1 joerg } else {
868 1.1 joerg ASTContext &Ctx = getASTContext();
869 1.1 joerg ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
870 1.1 joerg NamedDecl *Primary =
871 1.1 joerg FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
872 1.1 joerg if (Primary->getPreviousDecl())
873 1.1 joerg Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
874 1.1 joerg Primary, AS);
875 1.1 joerg else
876 1.1 joerg Conversions.addDecl(Ctx, Primary, AS);
877 1.1 joerg }
878 1.1 joerg }
879 1.1 joerg
880 1.1 joerg if (SMKind) {
881 1.1 joerg // If this is the first declaration of a special member, we no longer have
882 1.1 joerg // an implicit trivial special member.
883 1.1 joerg data().HasTrivialSpecialMembers &=
884 1.1 joerg data().DeclaredSpecialMembers | ~SMKind;
885 1.1 joerg data().HasTrivialSpecialMembersForCall &=
886 1.1 joerg data().DeclaredSpecialMembers | ~SMKind;
887 1.1 joerg
888 1.1 joerg if (!Method->isImplicit() && !Method->isUserProvided()) {
889 1.1 joerg // This method is user-declared but not user-provided. We can't work out
890 1.1 joerg // whether it's trivial yet (not until we get to the end of the class).
891 1.1 joerg // We'll handle this method in finishedDefaultedOrDeletedMember.
892 1.1 joerg } else if (Method->isTrivial()) {
893 1.1 joerg data().HasTrivialSpecialMembers |= SMKind;
894 1.1 joerg data().HasTrivialSpecialMembersForCall |= SMKind;
895 1.1 joerg } else if (Method->isTrivialForCall()) {
896 1.1 joerg data().HasTrivialSpecialMembersForCall |= SMKind;
897 1.1 joerg data().DeclaredNonTrivialSpecialMembers |= SMKind;
898 1.1 joerg } else {
899 1.1 joerg data().DeclaredNonTrivialSpecialMembers |= SMKind;
900 1.1 joerg // If this is a user-provided function, do not set
901 1.1 joerg // DeclaredNonTrivialSpecialMembersForCall here since we don't know
902 1.1 joerg // yet whether the method would be considered non-trivial for the
903 1.1 joerg // purpose of calls (attribute "trivial_abi" can be dropped from the
904 1.1 joerg // class later, which can change the special method's triviality).
905 1.1 joerg if (!Method->isUserProvided())
906 1.1 joerg data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
907 1.1 joerg }
908 1.1 joerg
909 1.1 joerg // Note when we have declared a declared special member, and suppress the
910 1.1 joerg // implicit declaration of this special member.
911 1.1 joerg data().DeclaredSpecialMembers |= SMKind;
912 1.1 joerg
913 1.1 joerg if (!Method->isImplicit()) {
914 1.1 joerg data().UserDeclaredSpecialMembers |= SMKind;
915 1.1 joerg
916 1.1 joerg // C++03 [class]p4:
917 1.1 joerg // A POD-struct is an aggregate class that has [...] no user-defined
918 1.1 joerg // copy assignment operator and no user-defined destructor.
919 1.1 joerg //
920 1.1 joerg // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
921 1.1 joerg // aggregates could not have any constructors, clear it even for an
922 1.1 joerg // explicitly defaulted or deleted constructor.
923 1.1 joerg // type is technically an aggregate in C++0x since it wouldn't be in 03.
924 1.1 joerg //
925 1.1 joerg // Also, a user-declared move assignment operator makes a class non-POD.
926 1.1 joerg // This is an extension in C++03.
927 1.1 joerg data().PlainOldData = false;
928 1.1 joerg }
929 1.1 joerg }
930 1.1 joerg
931 1.1 joerg return;
932 1.1 joerg }
933 1.1 joerg
934 1.1 joerg // Handle non-static data members.
935 1.1 joerg if (const auto *Field = dyn_cast<FieldDecl>(D)) {
936 1.1 joerg ASTContext &Context = getASTContext();
937 1.1 joerg
938 1.1 joerg // C++2a [class]p7:
939 1.1 joerg // A standard-layout class is a class that:
940 1.1 joerg // [...]
941 1.1 joerg // -- has all non-static data members and bit-fields in the class and
942 1.1 joerg // its base classes first declared in the same class
943 1.1 joerg if (data().HasBasesWithFields)
944 1.1 joerg data().IsStandardLayout = false;
945 1.1 joerg
946 1.1 joerg // C++ [class.bit]p2:
947 1.1 joerg // A declaration for a bit-field that omits the identifier declares an
948 1.1 joerg // unnamed bit-field. Unnamed bit-fields are not members and cannot be
949 1.1 joerg // initialized.
950 1.1 joerg if (Field->isUnnamedBitfield()) {
951 1.1 joerg // C++ [meta.unary.prop]p4: [LWG2358]
952 1.1 joerg // T is a class type [...] with [...] no unnamed bit-fields of non-zero
953 1.1 joerg // length
954 1.1 joerg if (data().Empty && !Field->isZeroLengthBitField(Context) &&
955 1.1 joerg Context.getLangOpts().getClangABICompat() >
956 1.1 joerg LangOptions::ClangABI::Ver6)
957 1.1 joerg data().Empty = false;
958 1.1 joerg return;
959 1.1 joerg }
960 1.1 joerg
961 1.1 joerg // C++11 [class]p7:
962 1.1 joerg // A standard-layout class is a class that:
963 1.1 joerg // -- either has no non-static data members in the most derived class
964 1.1 joerg // [...] or has no base classes with non-static data members
965 1.1 joerg if (data().HasBasesWithNonStaticDataMembers)
966 1.1 joerg data().IsCXX11StandardLayout = false;
967 1.1 joerg
968 1.1 joerg // C++ [dcl.init.aggr]p1:
969 1.1 joerg // An aggregate is an array or a class (clause 9) with [...] no
970 1.1 joerg // private or protected non-static data members (clause 11).
971 1.1 joerg //
972 1.1 joerg // A POD must be an aggregate.
973 1.1 joerg if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
974 1.1 joerg data().Aggregate = false;
975 1.1 joerg data().PlainOldData = false;
976 1.1.1.2 joerg
977 1.1.1.2 joerg // C++20 [temp.param]p7:
978 1.1.1.2 joerg // A structural type is [...] a literal class type [for which] all
979 1.1.1.2 joerg // non-static data members are public
980 1.1.1.2 joerg data().StructuralIfLiteral = false;
981 1.1 joerg }
982 1.1 joerg
983 1.1 joerg // Track whether this is the first field. We use this when checking
984 1.1 joerg // whether the class is standard-layout below.
985 1.1 joerg bool IsFirstField = !data().HasPrivateFields &&
986 1.1 joerg !data().HasProtectedFields && !data().HasPublicFields;
987 1.1 joerg
988 1.1 joerg // C++0x [class]p7:
989 1.1 joerg // A standard-layout class is a class that:
990 1.1 joerg // [...]
991 1.1 joerg // -- has the same access control for all non-static data members,
992 1.1 joerg switch (D->getAccess()) {
993 1.1 joerg case AS_private: data().HasPrivateFields = true; break;
994 1.1 joerg case AS_protected: data().HasProtectedFields = true; break;
995 1.1 joerg case AS_public: data().HasPublicFields = true; break;
996 1.1 joerg case AS_none: llvm_unreachable("Invalid access specifier");
997 1.1 joerg };
998 1.1 joerg if ((data().HasPrivateFields + data().HasProtectedFields +
999 1.1 joerg data().HasPublicFields) > 1) {
1000 1.1 joerg data().IsStandardLayout = false;
1001 1.1 joerg data().IsCXX11StandardLayout = false;
1002 1.1 joerg }
1003 1.1 joerg
1004 1.1 joerg // Keep track of the presence of mutable fields.
1005 1.1 joerg if (Field->isMutable()) {
1006 1.1 joerg data().HasMutableFields = true;
1007 1.1.1.2 joerg
1008 1.1.1.2 joerg // C++20 [temp.param]p7:
1009 1.1.1.2 joerg // A structural type is [...] a literal class type [for which] all
1010 1.1.1.2 joerg // non-static data members are public
1011 1.1.1.2 joerg data().StructuralIfLiteral = false;
1012 1.1 joerg }
1013 1.1 joerg
1014 1.1 joerg // C++11 [class.union]p8, DR1460:
1015 1.1 joerg // If X is a union, a non-static data member of X that is not an anonymous
1016 1.1 joerg // union is a variant member of X.
1017 1.1 joerg if (isUnion() && !Field->isAnonymousStructOrUnion())
1018 1.1 joerg data().HasVariantMembers = true;
1019 1.1 joerg
1020 1.1 joerg // C++0x [class]p9:
1021 1.1 joerg // A POD struct is a class that is both a trivial class and a
1022 1.1 joerg // standard-layout class, and has no non-static data members of type
1023 1.1 joerg // non-POD struct, non-POD union (or array of such types).
1024 1.1 joerg //
1025 1.1 joerg // Automatic Reference Counting: the presence of a member of Objective-C pointer type
1026 1.1 joerg // that does not explicitly have no lifetime makes the class a non-POD.
1027 1.1 joerg QualType T = Context.getBaseElementType(Field->getType());
1028 1.1 joerg if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
1029 1.1 joerg if (T.hasNonTrivialObjCLifetime()) {
1030 1.1 joerg // Objective-C Automatic Reference Counting:
1031 1.1 joerg // If a class has a non-static data member of Objective-C pointer
1032 1.1 joerg // type (or array thereof), it is a non-POD type and its
1033 1.1 joerg // default constructor (if any), copy constructor, move constructor,
1034 1.1 joerg // copy assignment operator, move assignment operator, and destructor are
1035 1.1 joerg // non-trivial.
1036 1.1 joerg setHasObjectMember(true);
1037 1.1 joerg struct DefinitionData &Data = data();
1038 1.1 joerg Data.PlainOldData = false;
1039 1.1 joerg Data.HasTrivialSpecialMembers = 0;
1040 1.1 joerg
1041 1.1 joerg // __strong or __weak fields do not make special functions non-trivial
1042 1.1 joerg // for the purpose of calls.
1043 1.1 joerg Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime();
1044 1.1 joerg if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak)
1045 1.1 joerg data().HasTrivialSpecialMembersForCall = 0;
1046 1.1 joerg
1047 1.1 joerg // Structs with __weak fields should never be passed directly.
1048 1.1 joerg if (LT == Qualifiers::OCL_Weak)
1049 1.1 joerg setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
1050 1.1 joerg
1051 1.1 joerg Data.HasIrrelevantDestructor = false;
1052 1.1 joerg
1053 1.1 joerg if (isUnion()) {
1054 1.1 joerg data().DefaultedCopyConstructorIsDeleted = true;
1055 1.1 joerg data().DefaultedMoveConstructorIsDeleted = true;
1056 1.1.1.2 joerg data().DefaultedCopyAssignmentIsDeleted = true;
1057 1.1 joerg data().DefaultedMoveAssignmentIsDeleted = true;
1058 1.1 joerg data().DefaultedDestructorIsDeleted = true;
1059 1.1 joerg data().NeedOverloadResolutionForCopyConstructor = true;
1060 1.1 joerg data().NeedOverloadResolutionForMoveConstructor = true;
1061 1.1.1.2 joerg data().NeedOverloadResolutionForCopyAssignment = true;
1062 1.1 joerg data().NeedOverloadResolutionForMoveAssignment = true;
1063 1.1 joerg data().NeedOverloadResolutionForDestructor = true;
1064 1.1 joerg }
1065 1.1 joerg } else if (!Context.getLangOpts().ObjCAutoRefCount) {
1066 1.1 joerg setHasObjectMember(true);
1067 1.1 joerg }
1068 1.1 joerg } else if (!T.isCXX98PODType(Context))
1069 1.1 joerg data().PlainOldData = false;
1070 1.1 joerg
1071 1.1 joerg if (T->isReferenceType()) {
1072 1.1 joerg if (!Field->hasInClassInitializer())
1073 1.1 joerg data().HasUninitializedReferenceMember = true;
1074 1.1 joerg
1075 1.1 joerg // C++0x [class]p7:
1076 1.1 joerg // A standard-layout class is a class that:
1077 1.1 joerg // -- has no non-static data members of type [...] reference,
1078 1.1 joerg data().IsStandardLayout = false;
1079 1.1 joerg data().IsCXX11StandardLayout = false;
1080 1.1 joerg
1081 1.1 joerg // C++1z [class.copy.ctor]p10:
1082 1.1 joerg // A defaulted copy constructor for a class X is defined as deleted if X has:
1083 1.1 joerg // -- a non-static data member of rvalue reference type
1084 1.1 joerg if (T->isRValueReferenceType())
1085 1.1 joerg data().DefaultedCopyConstructorIsDeleted = true;
1086 1.1 joerg }
1087 1.1 joerg
1088 1.1 joerg if (!Field->hasInClassInitializer() && !Field->isMutable()) {
1089 1.1 joerg if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
1090 1.1 joerg if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
1091 1.1 joerg data().HasUninitializedFields = true;
1092 1.1 joerg } else {
1093 1.1 joerg data().HasUninitializedFields = true;
1094 1.1 joerg }
1095 1.1 joerg }
1096 1.1 joerg
1097 1.1 joerg // Record if this field is the first non-literal or volatile field or base.
1098 1.1 joerg if (!T->isLiteralType(Context) || T.isVolatileQualified())
1099 1.1 joerg data().HasNonLiteralTypeFieldsOrBases = true;
1100 1.1 joerg
1101 1.1 joerg if (Field->hasInClassInitializer() ||
1102 1.1 joerg (Field->isAnonymousStructOrUnion() &&
1103 1.1 joerg Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
1104 1.1 joerg data().HasInClassInitializer = true;
1105 1.1 joerg
1106 1.1 joerg // C++11 [class]p5:
1107 1.1 joerg // A default constructor is trivial if [...] no non-static data member
1108 1.1 joerg // of its class has a brace-or-equal-initializer.
1109 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
1110 1.1 joerg
1111 1.1 joerg // C++11 [dcl.init.aggr]p1:
1112 1.1 joerg // An aggregate is a [...] class with [...] no
1113 1.1 joerg // brace-or-equal-initializers for non-static data members.
1114 1.1 joerg //
1115 1.1 joerg // This rule was removed in C++14.
1116 1.1 joerg if (!getASTContext().getLangOpts().CPlusPlus14)
1117 1.1 joerg data().Aggregate = false;
1118 1.1 joerg
1119 1.1 joerg // C++11 [class]p10:
1120 1.1 joerg // A POD struct is [...] a trivial class.
1121 1.1 joerg data().PlainOldData = false;
1122 1.1 joerg }
1123 1.1 joerg
1124 1.1 joerg // C++11 [class.copy]p23:
1125 1.1 joerg // A defaulted copy/move assignment operator for a class X is defined
1126 1.1 joerg // as deleted if X has:
1127 1.1 joerg // -- a non-static data member of reference type
1128 1.1.1.2 joerg if (T->isReferenceType()) {
1129 1.1.1.2 joerg data().DefaultedCopyAssignmentIsDeleted = true;
1130 1.1 joerg data().DefaultedMoveAssignmentIsDeleted = true;
1131 1.1.1.2 joerg }
1132 1.1 joerg
1133 1.1 joerg // Bitfields of length 0 are also zero-sized, but we already bailed out for
1134 1.1 joerg // those because they are always unnamed.
1135 1.1 joerg bool IsZeroSize = Field->isZeroSize(Context);
1136 1.1 joerg
1137 1.1 joerg if (const auto *RecordTy = T->getAs<RecordType>()) {
1138 1.1 joerg auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
1139 1.1 joerg if (FieldRec->getDefinition()) {
1140 1.1 joerg addedClassSubobject(FieldRec);
1141 1.1 joerg
1142 1.1 joerg // We may need to perform overload resolution to determine whether a
1143 1.1 joerg // field can be moved if it's const or volatile qualified.
1144 1.1 joerg if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
1145 1.1 joerg // We need to care about 'const' for the copy constructor because an
1146 1.1 joerg // implicit copy constructor might be declared with a non-const
1147 1.1 joerg // parameter.
1148 1.1 joerg data().NeedOverloadResolutionForCopyConstructor = true;
1149 1.1 joerg data().NeedOverloadResolutionForMoveConstructor = true;
1150 1.1.1.2 joerg data().NeedOverloadResolutionForCopyAssignment = true;
1151 1.1 joerg data().NeedOverloadResolutionForMoveAssignment = true;
1152 1.1 joerg }
1153 1.1 joerg
1154 1.1 joerg // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1155 1.1 joerg // A defaulted [special member] for a class X is defined as
1156 1.1 joerg // deleted if:
1157 1.1 joerg // -- X is a union-like class that has a variant member with a
1158 1.1 joerg // non-trivial [corresponding special member]
1159 1.1 joerg if (isUnion()) {
1160 1.1 joerg if (FieldRec->hasNonTrivialCopyConstructor())
1161 1.1 joerg data().DefaultedCopyConstructorIsDeleted = true;
1162 1.1 joerg if (FieldRec->hasNonTrivialMoveConstructor())
1163 1.1 joerg data().DefaultedMoveConstructorIsDeleted = true;
1164 1.1.1.2 joerg if (FieldRec->hasNonTrivialCopyAssignment())
1165 1.1.1.2 joerg data().DefaultedCopyAssignmentIsDeleted = true;
1166 1.1 joerg if (FieldRec->hasNonTrivialMoveAssignment())
1167 1.1 joerg data().DefaultedMoveAssignmentIsDeleted = true;
1168 1.1 joerg if (FieldRec->hasNonTrivialDestructor())
1169 1.1 joerg data().DefaultedDestructorIsDeleted = true;
1170 1.1 joerg }
1171 1.1 joerg
1172 1.1 joerg // For an anonymous union member, our overload resolution will perform
1173 1.1 joerg // overload resolution for its members.
1174 1.1 joerg if (Field->isAnonymousStructOrUnion()) {
1175 1.1 joerg data().NeedOverloadResolutionForCopyConstructor |=
1176 1.1 joerg FieldRec->data().NeedOverloadResolutionForCopyConstructor;
1177 1.1 joerg data().NeedOverloadResolutionForMoveConstructor |=
1178 1.1 joerg FieldRec->data().NeedOverloadResolutionForMoveConstructor;
1179 1.1.1.2 joerg data().NeedOverloadResolutionForCopyAssignment |=
1180 1.1.1.2 joerg FieldRec->data().NeedOverloadResolutionForCopyAssignment;
1181 1.1 joerg data().NeedOverloadResolutionForMoveAssignment |=
1182 1.1 joerg FieldRec->data().NeedOverloadResolutionForMoveAssignment;
1183 1.1 joerg data().NeedOverloadResolutionForDestructor |=
1184 1.1 joerg FieldRec->data().NeedOverloadResolutionForDestructor;
1185 1.1 joerg }
1186 1.1 joerg
1187 1.1 joerg // C++0x [class.ctor]p5:
1188 1.1 joerg // A default constructor is trivial [...] if:
1189 1.1 joerg // -- for all the non-static data members of its class that are of
1190 1.1 joerg // class type (or array thereof), each such class has a trivial
1191 1.1 joerg // default constructor.
1192 1.1 joerg if (!FieldRec->hasTrivialDefaultConstructor())
1193 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
1194 1.1 joerg
1195 1.1 joerg // C++0x [class.copy]p13:
1196 1.1 joerg // A copy/move constructor for class X is trivial if [...]
1197 1.1 joerg // [...]
1198 1.1 joerg // -- for each non-static data member of X that is of class type (or
1199 1.1 joerg // an array thereof), the constructor selected to copy/move that
1200 1.1 joerg // member is trivial;
1201 1.1 joerg if (!FieldRec->hasTrivialCopyConstructor())
1202 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
1203 1.1 joerg
1204 1.1 joerg if (!FieldRec->hasTrivialCopyConstructorForCall())
1205 1.1 joerg data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
1206 1.1 joerg
1207 1.1 joerg // If the field doesn't have a simple move constructor, we'll eagerly
1208 1.1 joerg // declare the move constructor for this class and we'll decide whether
1209 1.1 joerg // it's trivial then.
1210 1.1 joerg if (!FieldRec->hasTrivialMoveConstructor())
1211 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
1212 1.1 joerg
1213 1.1 joerg if (!FieldRec->hasTrivialMoveConstructorForCall())
1214 1.1 joerg data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
1215 1.1 joerg
1216 1.1 joerg // C++0x [class.copy]p27:
1217 1.1 joerg // A copy/move assignment operator for class X is trivial if [...]
1218 1.1 joerg // [...]
1219 1.1 joerg // -- for each non-static data member of X that is of class type (or
1220 1.1 joerg // an array thereof), the assignment operator selected to
1221 1.1 joerg // copy/move that member is trivial;
1222 1.1 joerg if (!FieldRec->hasTrivialCopyAssignment())
1223 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
1224 1.1 joerg // If the field doesn't have a simple move assignment, we'll eagerly
1225 1.1 joerg // declare the move assignment for this class and we'll decide whether
1226 1.1 joerg // it's trivial then.
1227 1.1 joerg if (!FieldRec->hasTrivialMoveAssignment())
1228 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
1229 1.1 joerg
1230 1.1 joerg if (!FieldRec->hasTrivialDestructor())
1231 1.1 joerg data().HasTrivialSpecialMembers &= ~SMF_Destructor;
1232 1.1 joerg if (!FieldRec->hasTrivialDestructorForCall())
1233 1.1 joerg data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
1234 1.1 joerg if (!FieldRec->hasIrrelevantDestructor())
1235 1.1 joerg data().HasIrrelevantDestructor = false;
1236 1.1 joerg if (FieldRec->hasObjectMember())
1237 1.1 joerg setHasObjectMember(true);
1238 1.1 joerg if (FieldRec->hasVolatileMember())
1239 1.1 joerg setHasVolatileMember(true);
1240 1.1 joerg if (FieldRec->getArgPassingRestrictions() ==
1241 1.1 joerg RecordDecl::APK_CanNeverPassInRegs)
1242 1.1 joerg setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
1243 1.1 joerg
1244 1.1 joerg // C++0x [class]p7:
1245 1.1 joerg // A standard-layout class is a class that:
1246 1.1 joerg // -- has no non-static data members of type non-standard-layout
1247 1.1 joerg // class (or array of such types) [...]
1248 1.1 joerg if (!FieldRec->isStandardLayout())
1249 1.1 joerg data().IsStandardLayout = false;
1250 1.1 joerg if (!FieldRec->isCXX11StandardLayout())
1251 1.1 joerg data().IsCXX11StandardLayout = false;
1252 1.1 joerg
1253 1.1 joerg // C++2a [class]p7:
1254 1.1 joerg // A standard-layout class is a class that:
1255 1.1 joerg // [...]
1256 1.1 joerg // -- has no element of the set M(S) of types as a base class.
1257 1.1 joerg if (data().IsStandardLayout &&
1258 1.1 joerg (isUnion() || IsFirstField || IsZeroSize) &&
1259 1.1 joerg hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec))
1260 1.1 joerg data().IsStandardLayout = false;
1261 1.1 joerg
1262 1.1 joerg // C++11 [class]p7:
1263 1.1 joerg // A standard-layout class is a class that:
1264 1.1 joerg // -- has no base classes of the same type as the first non-static
1265 1.1 joerg // data member
1266 1.1 joerg if (data().IsCXX11StandardLayout && IsFirstField) {
1267 1.1 joerg // FIXME: We should check all base classes here, not just direct
1268 1.1 joerg // base classes.
1269 1.1 joerg for (const auto &BI : bases()) {
1270 1.1 joerg if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
1271 1.1 joerg data().IsCXX11StandardLayout = false;
1272 1.1 joerg break;
1273 1.1 joerg }
1274 1.1 joerg }
1275 1.1 joerg }
1276 1.1 joerg
1277 1.1 joerg // Keep track of the presence of mutable fields.
1278 1.1.1.2 joerg if (FieldRec->hasMutableFields())
1279 1.1 joerg data().HasMutableFields = true;
1280 1.1.1.2 joerg
1281 1.1.1.2 joerg if (Field->isMutable()) {
1282 1.1.1.2 joerg // Our copy constructor/assignment might call something other than
1283 1.1.1.2 joerg // the subobject's copy constructor/assignment if it's mutable and of
1284 1.1.1.2 joerg // class type.
1285 1.1 joerg data().NeedOverloadResolutionForCopyConstructor = true;
1286 1.1.1.2 joerg data().NeedOverloadResolutionForCopyAssignment = true;
1287 1.1 joerg }
1288 1.1 joerg
1289 1.1 joerg // C++11 [class.copy]p13:
1290 1.1 joerg // If the implicitly-defined constructor would satisfy the
1291 1.1 joerg // requirements of a constexpr constructor, the implicitly-defined
1292 1.1 joerg // constructor is constexpr.
1293 1.1 joerg // C++11 [dcl.constexpr]p4:
1294 1.1 joerg // -- every constructor involved in initializing non-static data
1295 1.1 joerg // members [...] shall be a constexpr constructor
1296 1.1 joerg if (!Field->hasInClassInitializer() &&
1297 1.1 joerg !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
1298 1.1 joerg // The standard requires any in-class initializer to be a constant
1299 1.1 joerg // expression. We consider this to be a defect.
1300 1.1 joerg data().DefaultedDefaultConstructorIsConstexpr = false;
1301 1.1 joerg
1302 1.1 joerg // C++11 [class.copy]p8:
1303 1.1 joerg // The implicitly-declared copy constructor for a class X will have
1304 1.1 joerg // the form 'X::X(const X&)' if each potentially constructed subobject
1305 1.1 joerg // of a class type M (or array thereof) has a copy constructor whose
1306 1.1 joerg // first parameter is of type 'const M&' or 'const volatile M&'.
1307 1.1 joerg if (!FieldRec->hasCopyConstructorWithConstParam())
1308 1.1 joerg data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
1309 1.1 joerg
1310 1.1 joerg // C++11 [class.copy]p18:
1311 1.1 joerg // The implicitly-declared copy assignment oeprator for a class X will
1312 1.1 joerg // have the form 'X& X::operator=(const X&)' if [...] for all the
1313 1.1 joerg // non-static data members of X that are of a class type M (or array
1314 1.1 joerg // thereof), each such class type has a copy assignment operator whose
1315 1.1 joerg // parameter is of type 'const M&', 'const volatile M&' or 'M'.
1316 1.1 joerg if (!FieldRec->hasCopyAssignmentWithConstParam())
1317 1.1 joerg data().ImplicitCopyAssignmentHasConstParam = false;
1318 1.1 joerg
1319 1.1 joerg if (FieldRec->hasUninitializedReferenceMember() &&
1320 1.1 joerg !Field->hasInClassInitializer())
1321 1.1 joerg data().HasUninitializedReferenceMember = true;
1322 1.1 joerg
1323 1.1 joerg // C++11 [class.union]p8, DR1460:
1324 1.1 joerg // a non-static data member of an anonymous union that is a member of
1325 1.1 joerg // X is also a variant member of X.
1326 1.1 joerg if (FieldRec->hasVariantMembers() &&
1327 1.1 joerg Field->isAnonymousStructOrUnion())
1328 1.1 joerg data().HasVariantMembers = true;
1329 1.1 joerg }
1330 1.1 joerg } else {
1331 1.1 joerg // Base element type of field is a non-class type.
1332 1.1 joerg if (!T->isLiteralType(Context) ||
1333 1.1 joerg (!Field->hasInClassInitializer() && !isUnion() &&
1334 1.1.1.2 joerg !Context.getLangOpts().CPlusPlus20))
1335 1.1 joerg data().DefaultedDefaultConstructorIsConstexpr = false;
1336 1.1 joerg
1337 1.1 joerg // C++11 [class.copy]p23:
1338 1.1 joerg // A defaulted copy/move assignment operator for a class X is defined
1339 1.1 joerg // as deleted if X has:
1340 1.1 joerg // -- a non-static data member of const non-class type (or array
1341 1.1 joerg // thereof)
1342 1.1.1.2 joerg if (T.isConstQualified()) {
1343 1.1.1.2 joerg data().DefaultedCopyAssignmentIsDeleted = true;
1344 1.1 joerg data().DefaultedMoveAssignmentIsDeleted = true;
1345 1.1.1.2 joerg }
1346 1.1.1.2 joerg
1347 1.1.1.2 joerg // C++20 [temp.param]p7:
1348 1.1.1.2 joerg // A structural type is [...] a literal class type [for which] the
1349 1.1.1.2 joerg // types of all non-static data members are structural types or
1350 1.1.1.2 joerg // (possibly multidimensional) array thereof
1351 1.1.1.2 joerg // We deal with class types elsewhere.
1352 1.1.1.2 joerg if (!T->isStructuralType())
1353 1.1.1.2 joerg data().StructuralIfLiteral = false;
1354 1.1 joerg }
1355 1.1 joerg
1356 1.1 joerg // C++14 [meta.unary.prop]p4:
1357 1.1 joerg // T is a class type [...] with [...] no non-static data members other
1358 1.1 joerg // than subobjects of zero size
1359 1.1 joerg if (data().Empty && !IsZeroSize)
1360 1.1 joerg data().Empty = false;
1361 1.1 joerg }
1362 1.1 joerg
1363 1.1 joerg // Handle using declarations of conversion functions.
1364 1.1 joerg if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) {
1365 1.1 joerg if (Shadow->getDeclName().getNameKind()
1366 1.1 joerg == DeclarationName::CXXConversionFunctionName) {
1367 1.1 joerg ASTContext &Ctx = getASTContext();
1368 1.1 joerg data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1369 1.1 joerg }
1370 1.1 joerg }
1371 1.1 joerg
1372 1.1 joerg if (const auto *Using = dyn_cast<UsingDecl>(D)) {
1373 1.1 joerg if (Using->getDeclName().getNameKind() ==
1374 1.1 joerg DeclarationName::CXXConstructorName) {
1375 1.1 joerg data().HasInheritedConstructor = true;
1376 1.1 joerg // C++1z [dcl.init.aggr]p1:
1377 1.1 joerg // An aggregate is [...] a class [...] with no inherited constructors
1378 1.1 joerg data().Aggregate = false;
1379 1.1 joerg }
1380 1.1 joerg
1381 1.1 joerg if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1382 1.1 joerg data().HasInheritedAssignment = true;
1383 1.1 joerg }
1384 1.1 joerg }
1385 1.1 joerg
1386 1.1 joerg void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1387 1.1 joerg assert(!D->isImplicit() && !D->isUserProvided());
1388 1.1 joerg
1389 1.1 joerg // The kind of special member this declaration is, if any.
1390 1.1 joerg unsigned SMKind = 0;
1391 1.1 joerg
1392 1.1 joerg if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1393 1.1 joerg if (Constructor->isDefaultConstructor()) {
1394 1.1 joerg SMKind |= SMF_DefaultConstructor;
1395 1.1 joerg if (Constructor->isConstexpr())
1396 1.1 joerg data().HasConstexprDefaultConstructor = true;
1397 1.1 joerg }
1398 1.1 joerg if (Constructor->isCopyConstructor())
1399 1.1 joerg SMKind |= SMF_CopyConstructor;
1400 1.1 joerg else if (Constructor->isMoveConstructor())
1401 1.1 joerg SMKind |= SMF_MoveConstructor;
1402 1.1 joerg else if (Constructor->isConstexpr())
1403 1.1 joerg // We may now know that the constructor is constexpr.
1404 1.1 joerg data().HasConstexprNonCopyMoveConstructor = true;
1405 1.1 joerg } else if (isa<CXXDestructorDecl>(D)) {
1406 1.1 joerg SMKind |= SMF_Destructor;
1407 1.1 joerg if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1408 1.1 joerg data().HasIrrelevantDestructor = false;
1409 1.1 joerg } else if (D->isCopyAssignmentOperator())
1410 1.1 joerg SMKind |= SMF_CopyAssignment;
1411 1.1 joerg else if (D->isMoveAssignmentOperator())
1412 1.1 joerg SMKind |= SMF_MoveAssignment;
1413 1.1 joerg
1414 1.1 joerg // Update which trivial / non-trivial special members we have.
1415 1.1 joerg // addedMember will have skipped this step for this member.
1416 1.1 joerg if (D->isTrivial())
1417 1.1 joerg data().HasTrivialSpecialMembers |= SMKind;
1418 1.1 joerg else
1419 1.1 joerg data().DeclaredNonTrivialSpecialMembers |= SMKind;
1420 1.1 joerg }
1421 1.1 joerg
1422 1.1.1.2 joerg void CXXRecordDecl::setCaptures(ASTContext &Context,
1423 1.1.1.2 joerg ArrayRef<LambdaCapture> Captures) {
1424 1.1.1.2 joerg CXXRecordDecl::LambdaDefinitionData &Data = getLambdaData();
1425 1.1.1.2 joerg
1426 1.1.1.2 joerg // Copy captures.
1427 1.1.1.2 joerg Data.NumCaptures = Captures.size();
1428 1.1.1.2 joerg Data.NumExplicitCaptures = 0;
1429 1.1.1.2 joerg Data.Captures = (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) *
1430 1.1.1.2 joerg Captures.size());
1431 1.1.1.2 joerg LambdaCapture *ToCapture = Data.Captures;
1432 1.1.1.2 joerg for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
1433 1.1.1.2 joerg if (Captures[I].isExplicit())
1434 1.1.1.2 joerg ++Data.NumExplicitCaptures;
1435 1.1.1.2 joerg
1436 1.1.1.2 joerg *ToCapture++ = Captures[I];
1437 1.1.1.2 joerg }
1438 1.1.1.2 joerg
1439 1.1.1.2 joerg if (!lambdaIsDefaultConstructibleAndAssignable())
1440 1.1.1.2 joerg Data.DefaultedCopyAssignmentIsDeleted = true;
1441 1.1.1.2 joerg }
1442 1.1.1.2 joerg
1443 1.1 joerg void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) {
1444 1.1 joerg unsigned SMKind = 0;
1445 1.1 joerg
1446 1.1 joerg if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1447 1.1 joerg if (Constructor->isCopyConstructor())
1448 1.1 joerg SMKind = SMF_CopyConstructor;
1449 1.1 joerg else if (Constructor->isMoveConstructor())
1450 1.1 joerg SMKind = SMF_MoveConstructor;
1451 1.1 joerg } else if (isa<CXXDestructorDecl>(D))
1452 1.1 joerg SMKind = SMF_Destructor;
1453 1.1 joerg
1454 1.1 joerg if (D->isTrivialForCall())
1455 1.1 joerg data().HasTrivialSpecialMembersForCall |= SMKind;
1456 1.1 joerg else
1457 1.1 joerg data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1458 1.1 joerg }
1459 1.1 joerg
1460 1.1 joerg bool CXXRecordDecl::isCLike() const {
1461 1.1 joerg if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1462 1.1 joerg !TemplateOrInstantiation.isNull())
1463 1.1 joerg return false;
1464 1.1 joerg if (!hasDefinition())
1465 1.1 joerg return true;
1466 1.1 joerg
1467 1.1 joerg return isPOD() && data().HasOnlyCMembers;
1468 1.1 joerg }
1469 1.1 joerg
1470 1.1 joerg bool CXXRecordDecl::isGenericLambda() const {
1471 1.1 joerg if (!isLambda()) return false;
1472 1.1 joerg return getLambdaData().IsGenericLambda;
1473 1.1 joerg }
1474 1.1 joerg
1475 1.1 joerg #ifndef NDEBUG
1476 1.1 joerg static bool allLookupResultsAreTheSame(const DeclContext::lookup_result &R) {
1477 1.1 joerg for (auto *D : R)
1478 1.1 joerg if (!declaresSameEntity(D, R.front()))
1479 1.1 joerg return false;
1480 1.1 joerg return true;
1481 1.1 joerg }
1482 1.1 joerg #endif
1483 1.1 joerg
1484 1.1 joerg static NamedDecl* getLambdaCallOperatorHelper(const CXXRecordDecl &RD) {
1485 1.1 joerg if (!RD.isLambda()) return nullptr;
1486 1.1 joerg DeclarationName Name =
1487 1.1 joerg RD.getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1488 1.1 joerg DeclContext::lookup_result Calls = RD.lookup(Name);
1489 1.1 joerg
1490 1.1 joerg assert(!Calls.empty() && "Missing lambda call operator!");
1491 1.1 joerg assert(allLookupResultsAreTheSame(Calls) &&
1492 1.1 joerg "More than one lambda call operator!");
1493 1.1 joerg return Calls.front();
1494 1.1 joerg }
1495 1.1 joerg
1496 1.1 joerg FunctionTemplateDecl* CXXRecordDecl::getDependentLambdaCallOperator() const {
1497 1.1 joerg NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
1498 1.1 joerg return dyn_cast_or_null<FunctionTemplateDecl>(CallOp);
1499 1.1 joerg }
1500 1.1 joerg
1501 1.1 joerg CXXMethodDecl *CXXRecordDecl::getLambdaCallOperator() const {
1502 1.1 joerg NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
1503 1.1 joerg
1504 1.1 joerg if (CallOp == nullptr)
1505 1.1 joerg return nullptr;
1506 1.1 joerg
1507 1.1 joerg if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
1508 1.1 joerg return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
1509 1.1 joerg
1510 1.1 joerg return cast<CXXMethodDecl>(CallOp);
1511 1.1 joerg }
1512 1.1 joerg
1513 1.1 joerg CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
1514 1.1.1.2 joerg CXXMethodDecl *CallOp = getLambdaCallOperator();
1515 1.1.1.2 joerg CallingConv CC = CallOp->getType()->castAs<FunctionType>()->getCallConv();
1516 1.1.1.2 joerg return getLambdaStaticInvoker(CC);
1517 1.1.1.2 joerg }
1518 1.1.1.2 joerg
1519 1.1.1.2 joerg static DeclContext::lookup_result
1520 1.1.1.2 joerg getLambdaStaticInvokers(const CXXRecordDecl &RD) {
1521 1.1.1.2 joerg assert(RD.isLambda() && "Must be a lambda");
1522 1.1 joerg DeclarationName Name =
1523 1.1.1.2 joerg &RD.getASTContext().Idents.get(getLambdaStaticInvokerName());
1524 1.1.1.2 joerg return RD.lookup(Name);
1525 1.1.1.2 joerg }
1526 1.1.1.2 joerg
1527 1.1.1.2 joerg static CXXMethodDecl *getInvokerAsMethod(NamedDecl *ND) {
1528 1.1.1.2 joerg if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(ND))
1529 1.1 joerg return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1530 1.1.1.2 joerg return cast<CXXMethodDecl>(ND);
1531 1.1.1.2 joerg }
1532 1.1.1.2 joerg
1533 1.1.1.2 joerg CXXMethodDecl *CXXRecordDecl::getLambdaStaticInvoker(CallingConv CC) const {
1534 1.1.1.2 joerg if (!isLambda())
1535 1.1.1.2 joerg return nullptr;
1536 1.1.1.2 joerg DeclContext::lookup_result Invoker = getLambdaStaticInvokers(*this);
1537 1.1 joerg
1538 1.1.1.2 joerg for (NamedDecl *ND : Invoker) {
1539 1.1.1.2 joerg const auto *FTy =
1540 1.1.1.2 joerg cast<ValueDecl>(ND->getAsFunction())->getType()->castAs<FunctionType>();
1541 1.1.1.2 joerg if (FTy->getCallConv() == CC)
1542 1.1.1.2 joerg return getInvokerAsMethod(ND);
1543 1.1.1.2 joerg }
1544 1.1.1.2 joerg
1545 1.1.1.2 joerg return nullptr;
1546 1.1 joerg }
1547 1.1 joerg
1548 1.1 joerg void CXXRecordDecl::getCaptureFields(
1549 1.1 joerg llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
1550 1.1 joerg FieldDecl *&ThisCapture) const {
1551 1.1 joerg Captures.clear();
1552 1.1 joerg ThisCapture = nullptr;
1553 1.1 joerg
1554 1.1 joerg LambdaDefinitionData &Lambda = getLambdaData();
1555 1.1 joerg RecordDecl::field_iterator Field = field_begin();
1556 1.1 joerg for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
1557 1.1 joerg C != CEnd; ++C, ++Field) {
1558 1.1 joerg if (C->capturesThis())
1559 1.1 joerg ThisCapture = *Field;
1560 1.1 joerg else if (C->capturesVariable())
1561 1.1 joerg Captures[C->getCapturedVar()] = *Field;
1562 1.1 joerg }
1563 1.1 joerg assert(Field == field_end());
1564 1.1 joerg }
1565 1.1 joerg
1566 1.1 joerg TemplateParameterList *
1567 1.1 joerg CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
1568 1.1 joerg if (!isGenericLambda()) return nullptr;
1569 1.1 joerg CXXMethodDecl *CallOp = getLambdaCallOperator();
1570 1.1 joerg if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1571 1.1 joerg return Tmpl->getTemplateParameters();
1572 1.1 joerg return nullptr;
1573 1.1 joerg }
1574 1.1 joerg
1575 1.1 joerg ArrayRef<NamedDecl *>
1576 1.1 joerg CXXRecordDecl::getLambdaExplicitTemplateParameters() const {
1577 1.1 joerg TemplateParameterList *List = getGenericLambdaTemplateParameterList();
1578 1.1 joerg if (!List)
1579 1.1 joerg return {};
1580 1.1 joerg
1581 1.1 joerg assert(std::is_partitioned(List->begin(), List->end(),
1582 1.1 joerg [](const NamedDecl *D) { return !D->isImplicit(); })
1583 1.1 joerg && "Explicit template params should be ordered before implicit ones");
1584 1.1 joerg
1585 1.1 joerg const auto ExplicitEnd = llvm::partition_point(
1586 1.1 joerg *List, [](const NamedDecl *D) { return !D->isImplicit(); });
1587 1.1 joerg return llvm::makeArrayRef(List->begin(), ExplicitEnd);
1588 1.1 joerg }
1589 1.1 joerg
1590 1.1 joerg Decl *CXXRecordDecl::getLambdaContextDecl() const {
1591 1.1 joerg assert(isLambda() && "Not a lambda closure type!");
1592 1.1 joerg ExternalASTSource *Source = getParentASTContext().getExternalSource();
1593 1.1 joerg return getLambdaData().ContextDecl.get(Source);
1594 1.1 joerg }
1595 1.1 joerg
1596 1.1.1.2 joerg void CXXRecordDecl::setDeviceLambdaManglingNumber(unsigned Num) const {
1597 1.1.1.2 joerg assert(isLambda() && "Not a lambda closure type!");
1598 1.1.1.2 joerg if (Num)
1599 1.1.1.2 joerg getASTContext().DeviceLambdaManglingNumbers[this] = Num;
1600 1.1.1.2 joerg }
1601 1.1.1.2 joerg
1602 1.1.1.2 joerg unsigned CXXRecordDecl::getDeviceLambdaManglingNumber() const {
1603 1.1.1.2 joerg assert(isLambda() && "Not a lambda closure type!");
1604 1.1.1.2 joerg auto I = getASTContext().DeviceLambdaManglingNumbers.find(this);
1605 1.1.1.2 joerg if (I != getASTContext().DeviceLambdaManglingNumbers.end())
1606 1.1.1.2 joerg return I->second;
1607 1.1.1.2 joerg return 0;
1608 1.1.1.2 joerg }
1609 1.1.1.2 joerg
1610 1.1 joerg static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1611 1.1 joerg QualType T =
1612 1.1 joerg cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1613 1.1 joerg ->getConversionType();
1614 1.1 joerg return Context.getCanonicalType(T);
1615 1.1 joerg }
1616 1.1 joerg
1617 1.1 joerg /// Collect the visible conversions of a base class.
1618 1.1 joerg ///
1619 1.1 joerg /// \param Record a base class of the class we're considering
1620 1.1 joerg /// \param InVirtual whether this base class is a virtual base (or a base
1621 1.1 joerg /// of a virtual base)
1622 1.1 joerg /// \param Access the access along the inheritance path to this base
1623 1.1 joerg /// \param ParentHiddenTypes the conversions provided by the inheritors
1624 1.1 joerg /// of this base
1625 1.1 joerg /// \param Output the set to which to add conversions from non-virtual bases
1626 1.1 joerg /// \param VOutput the set to which to add conversions from virtual bases
1627 1.1 joerg /// \param HiddenVBaseCs the set of conversions which were hidden in a
1628 1.1 joerg /// virtual base along some inheritance path
1629 1.1.1.2 joerg static void CollectVisibleConversions(
1630 1.1.1.2 joerg ASTContext &Context, const CXXRecordDecl *Record, bool InVirtual,
1631 1.1.1.2 joerg AccessSpecifier Access,
1632 1.1.1.2 joerg const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1633 1.1.1.2 joerg ASTUnresolvedSet &Output, UnresolvedSetImpl &VOutput,
1634 1.1.1.2 joerg llvm::SmallPtrSet<NamedDecl *, 8> &HiddenVBaseCs) {
1635 1.1 joerg // The set of types which have conversions in this class or its
1636 1.1 joerg // subclasses. As an optimization, we don't copy the derived set
1637 1.1 joerg // unless it might change.
1638 1.1 joerg const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1639 1.1 joerg llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1640 1.1 joerg
1641 1.1 joerg // Collect the direct conversions and figure out which conversions
1642 1.1 joerg // will be hidden in the subclasses.
1643 1.1 joerg CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1644 1.1 joerg CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1645 1.1 joerg if (ConvI != ConvE) {
1646 1.1 joerg HiddenTypesBuffer = ParentHiddenTypes;
1647 1.1 joerg HiddenTypes = &HiddenTypesBuffer;
1648 1.1 joerg
1649 1.1 joerg for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
1650 1.1 joerg CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1651 1.1 joerg bool Hidden = ParentHiddenTypes.count(ConvType);
1652 1.1 joerg if (!Hidden)
1653 1.1 joerg HiddenTypesBuffer.insert(ConvType);
1654 1.1 joerg
1655 1.1 joerg // If this conversion is hidden and we're in a virtual base,
1656 1.1 joerg // remember that it's hidden along some inheritance path.
1657 1.1 joerg if (Hidden && InVirtual)
1658 1.1 joerg HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1659 1.1 joerg
1660 1.1 joerg // If this conversion isn't hidden, add it to the appropriate output.
1661 1.1 joerg else if (!Hidden) {
1662 1.1 joerg AccessSpecifier IAccess
1663 1.1 joerg = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1664 1.1 joerg
1665 1.1 joerg if (InVirtual)
1666 1.1 joerg VOutput.addDecl(I.getDecl(), IAccess);
1667 1.1 joerg else
1668 1.1 joerg Output.addDecl(Context, I.getDecl(), IAccess);
1669 1.1 joerg }
1670 1.1 joerg }
1671 1.1 joerg }
1672 1.1 joerg
1673 1.1 joerg // Collect information recursively from any base classes.
1674 1.1 joerg for (const auto &I : Record->bases()) {
1675 1.1.1.2 joerg const auto *RT = I.getType()->getAs<RecordType>();
1676 1.1 joerg if (!RT) continue;
1677 1.1 joerg
1678 1.1 joerg AccessSpecifier BaseAccess
1679 1.1 joerg = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1680 1.1 joerg bool BaseInVirtual = InVirtual || I.isVirtual();
1681 1.1 joerg
1682 1.1 joerg auto *Base = cast<CXXRecordDecl>(RT->getDecl());
1683 1.1 joerg CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1684 1.1 joerg *HiddenTypes, Output, VOutput, HiddenVBaseCs);
1685 1.1 joerg }
1686 1.1 joerg }
1687 1.1 joerg
1688 1.1 joerg /// Collect the visible conversions of a class.
1689 1.1 joerg ///
1690 1.1 joerg /// This would be extremely straightforward if it weren't for virtual
1691 1.1 joerg /// bases. It might be worth special-casing that, really.
1692 1.1 joerg static void CollectVisibleConversions(ASTContext &Context,
1693 1.1.1.2 joerg const CXXRecordDecl *Record,
1694 1.1 joerg ASTUnresolvedSet &Output) {
1695 1.1 joerg // The collection of all conversions in virtual bases that we've
1696 1.1 joerg // found. These will be added to the output as long as they don't
1697 1.1 joerg // appear in the hidden-conversions set.
1698 1.1 joerg UnresolvedSet<8> VBaseCs;
1699 1.1 joerg
1700 1.1 joerg // The set of conversions in virtual bases that we've determined to
1701 1.1 joerg // be hidden.
1702 1.1 joerg llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1703 1.1 joerg
1704 1.1 joerg // The set of types hidden by classes derived from this one.
1705 1.1 joerg llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1706 1.1 joerg
1707 1.1 joerg // Go ahead and collect the direct conversions and add them to the
1708 1.1 joerg // hidden-types set.
1709 1.1 joerg CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1710 1.1 joerg CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1711 1.1 joerg Output.append(Context, ConvI, ConvE);
1712 1.1 joerg for (; ConvI != ConvE; ++ConvI)
1713 1.1 joerg HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
1714 1.1 joerg
1715 1.1 joerg // Recursively collect conversions from base classes.
1716 1.1 joerg for (const auto &I : Record->bases()) {
1717 1.1.1.2 joerg const auto *RT = I.getType()->getAs<RecordType>();
1718 1.1 joerg if (!RT) continue;
1719 1.1 joerg
1720 1.1 joerg CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1721 1.1 joerg I.isVirtual(), I.getAccessSpecifier(),
1722 1.1 joerg HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1723 1.1 joerg }
1724 1.1 joerg
1725 1.1 joerg // Add any unhidden conversions provided by virtual bases.
1726 1.1 joerg for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1727 1.1 joerg I != E; ++I) {
1728 1.1 joerg if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
1729 1.1 joerg Output.addDecl(Context, I.getDecl(), I.getAccess());
1730 1.1 joerg }
1731 1.1 joerg }
1732 1.1 joerg
1733 1.1 joerg /// getVisibleConversionFunctions - get all conversion functions visible
1734 1.1 joerg /// in current class; including conversion function templates.
1735 1.1 joerg llvm::iterator_range<CXXRecordDecl::conversion_iterator>
1736 1.1.1.2 joerg CXXRecordDecl::getVisibleConversionFunctions() const {
1737 1.1 joerg ASTContext &Ctx = getASTContext();
1738 1.1 joerg
1739 1.1 joerg ASTUnresolvedSet *Set;
1740 1.1 joerg if (bases_begin() == bases_end()) {
1741 1.1 joerg // If root class, all conversions are visible.
1742 1.1 joerg Set = &data().Conversions.get(Ctx);
1743 1.1 joerg } else {
1744 1.1 joerg Set = &data().VisibleConversions.get(Ctx);
1745 1.1 joerg // If visible conversion list is not evaluated, evaluate it.
1746 1.1 joerg if (!data().ComputedVisibleConversions) {
1747 1.1 joerg CollectVisibleConversions(Ctx, this, *Set);
1748 1.1 joerg data().ComputedVisibleConversions = true;
1749 1.1 joerg }
1750 1.1 joerg }
1751 1.1 joerg return llvm::make_range(Set->begin(), Set->end());
1752 1.1 joerg }
1753 1.1 joerg
1754 1.1 joerg void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1755 1.1 joerg // This operation is O(N) but extremely rare. Sema only uses it to
1756 1.1 joerg // remove UsingShadowDecls in a class that were followed by a direct
1757 1.1 joerg // declaration, e.g.:
1758 1.1 joerg // class A : B {
1759 1.1 joerg // using B::operator int;
1760 1.1 joerg // operator int();
1761 1.1 joerg // };
1762 1.1 joerg // This is uncommon by itself and even more uncommon in conjunction
1763 1.1 joerg // with sufficiently large numbers of directly-declared conversions
1764 1.1 joerg // that asymptotic behavior matters.
1765 1.1 joerg
1766 1.1 joerg ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
1767 1.1 joerg for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1768 1.1 joerg if (Convs[I].getDecl() == ConvDecl) {
1769 1.1 joerg Convs.erase(I);
1770 1.1 joerg assert(llvm::find(Convs, ConvDecl) == Convs.end() &&
1771 1.1 joerg "conversion was found multiple times in unresolved set");
1772 1.1 joerg return;
1773 1.1 joerg }
1774 1.1 joerg }
1775 1.1 joerg
1776 1.1 joerg llvm_unreachable("conversion not found in set!");
1777 1.1 joerg }
1778 1.1 joerg
1779 1.1 joerg CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
1780 1.1 joerg if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
1781 1.1 joerg return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1782 1.1 joerg
1783 1.1 joerg return nullptr;
1784 1.1 joerg }
1785 1.1 joerg
1786 1.1 joerg MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1787 1.1 joerg return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1788 1.1 joerg }
1789 1.1 joerg
1790 1.1 joerg void
1791 1.1 joerg CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1792 1.1 joerg TemplateSpecializationKind TSK) {
1793 1.1 joerg assert(TemplateOrInstantiation.isNull() &&
1794 1.1 joerg "Previous template or instantiation?");
1795 1.1 joerg assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
1796 1.1 joerg TemplateOrInstantiation
1797 1.1 joerg = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1798 1.1 joerg }
1799 1.1 joerg
1800 1.1 joerg ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1801 1.1 joerg return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1802 1.1 joerg }
1803 1.1 joerg
1804 1.1 joerg void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1805 1.1 joerg TemplateOrInstantiation = Template;
1806 1.1 joerg }
1807 1.1 joerg
1808 1.1 joerg TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1809 1.1 joerg if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this))
1810 1.1 joerg return Spec->getSpecializationKind();
1811 1.1 joerg
1812 1.1 joerg if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
1813 1.1 joerg return MSInfo->getTemplateSpecializationKind();
1814 1.1 joerg
1815 1.1 joerg return TSK_Undeclared;
1816 1.1 joerg }
1817 1.1 joerg
1818 1.1 joerg void
1819 1.1 joerg CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1820 1.1 joerg if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1821 1.1 joerg Spec->setSpecializationKind(TSK);
1822 1.1 joerg return;
1823 1.1 joerg }
1824 1.1 joerg
1825 1.1 joerg if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1826 1.1 joerg MSInfo->setTemplateSpecializationKind(TSK);
1827 1.1 joerg return;
1828 1.1 joerg }
1829 1.1 joerg
1830 1.1 joerg llvm_unreachable("Not a class template or member class specialization");
1831 1.1 joerg }
1832 1.1 joerg
1833 1.1 joerg const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1834 1.1 joerg auto GetDefinitionOrSelf =
1835 1.1 joerg [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1836 1.1 joerg if (auto *Def = D->getDefinition())
1837 1.1 joerg return Def;
1838 1.1 joerg return D;
1839 1.1 joerg };
1840 1.1 joerg
1841 1.1 joerg // If it's a class template specialization, find the template or partial
1842 1.1 joerg // specialization from which it was instantiated.
1843 1.1 joerg if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1844 1.1 joerg auto From = TD->getInstantiatedFrom();
1845 1.1 joerg if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1846 1.1 joerg while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1847 1.1 joerg if (NewCTD->isMemberSpecialization())
1848 1.1 joerg break;
1849 1.1 joerg CTD = NewCTD;
1850 1.1 joerg }
1851 1.1 joerg return GetDefinitionOrSelf(CTD->getTemplatedDecl());
1852 1.1 joerg }
1853 1.1 joerg if (auto *CTPSD =
1854 1.1 joerg From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1855 1.1 joerg while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1856 1.1 joerg if (NewCTPSD->isMemberSpecialization())
1857 1.1 joerg break;
1858 1.1 joerg CTPSD = NewCTPSD;
1859 1.1 joerg }
1860 1.1 joerg return GetDefinitionOrSelf(CTPSD);
1861 1.1 joerg }
1862 1.1 joerg }
1863 1.1 joerg
1864 1.1 joerg if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1865 1.1 joerg if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1866 1.1 joerg const CXXRecordDecl *RD = this;
1867 1.1 joerg while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1868 1.1 joerg RD = NewRD;
1869 1.1 joerg return GetDefinitionOrSelf(RD);
1870 1.1 joerg }
1871 1.1 joerg }
1872 1.1 joerg
1873 1.1 joerg assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1874 1.1 joerg "couldn't find pattern for class template instantiation");
1875 1.1 joerg return nullptr;
1876 1.1 joerg }
1877 1.1 joerg
1878 1.1 joerg CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1879 1.1 joerg ASTContext &Context = getASTContext();
1880 1.1 joerg QualType ClassType = Context.getTypeDeclType(this);
1881 1.1 joerg
1882 1.1 joerg DeclarationName Name
1883 1.1 joerg = Context.DeclarationNames.getCXXDestructorName(
1884 1.1 joerg Context.getCanonicalType(ClassType));
1885 1.1 joerg
1886 1.1 joerg DeclContext::lookup_result R = lookup(Name);
1887 1.1 joerg
1888 1.1 joerg return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
1889 1.1 joerg }
1890 1.1 joerg
1891 1.1 joerg bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1892 1.1 joerg // Destructor is noreturn.
1893 1.1 joerg if (const CXXDestructorDecl *Destructor = getDestructor())
1894 1.1 joerg if (Destructor->isNoReturn())
1895 1.1 joerg return true;
1896 1.1 joerg
1897 1.1 joerg // Check base classes destructor for noreturn.
1898 1.1 joerg for (const auto &Base : bases())
1899 1.1 joerg if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl())
1900 1.1 joerg if (RD->isAnyDestructorNoReturn())
1901 1.1 joerg return true;
1902 1.1 joerg
1903 1.1 joerg // Check fields for noreturn.
1904 1.1 joerg for (const auto *Field : fields())
1905 1.1 joerg if (const CXXRecordDecl *RD =
1906 1.1 joerg Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1907 1.1 joerg if (RD->isAnyDestructorNoReturn())
1908 1.1 joerg return true;
1909 1.1 joerg
1910 1.1 joerg // All destructors are not noreturn.
1911 1.1 joerg return false;
1912 1.1 joerg }
1913 1.1 joerg
1914 1.1 joerg static bool isDeclContextInNamespace(const DeclContext *DC) {
1915 1.1 joerg while (!DC->isTranslationUnit()) {
1916 1.1 joerg if (DC->isNamespace())
1917 1.1 joerg return true;
1918 1.1 joerg DC = DC->getParent();
1919 1.1 joerg }
1920 1.1 joerg return false;
1921 1.1 joerg }
1922 1.1 joerg
1923 1.1 joerg bool CXXRecordDecl::isInterfaceLike() const {
1924 1.1 joerg assert(hasDefinition() && "checking for interface-like without a definition");
1925 1.1 joerg // All __interfaces are inheritently interface-like.
1926 1.1 joerg if (isInterface())
1927 1.1 joerg return true;
1928 1.1 joerg
1929 1.1 joerg // Interface-like types cannot have a user declared constructor, destructor,
1930 1.1 joerg // friends, VBases, conversion functions, or fields. Additionally, lambdas
1931 1.1 joerg // cannot be interface types.
1932 1.1 joerg if (isLambda() || hasUserDeclaredConstructor() ||
1933 1.1 joerg hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1934 1.1 joerg getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1935 1.1 joerg return false;
1936 1.1 joerg
1937 1.1 joerg // No interface-like type can have a method with a definition.
1938 1.1 joerg for (const auto *const Method : methods())
1939 1.1 joerg if (Method->isDefined() && !Method->isImplicit())
1940 1.1 joerg return false;
1941 1.1 joerg
1942 1.1 joerg // Check "Special" types.
1943 1.1 joerg const auto *Uuid = getAttr<UuidAttr>();
1944 1.1 joerg // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1945 1.1 joerg // extern C++ block directly in the TU. These are only valid if in one
1946 1.1 joerg // of these two situations.
1947 1.1 joerg if (Uuid && isStruct() && !getDeclContext()->isExternCContext() &&
1948 1.1 joerg !isDeclContextInNamespace(getDeclContext()) &&
1949 1.1 joerg ((getName() == "IUnknown" &&
1950 1.1 joerg Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1951 1.1 joerg (getName() == "IDispatch" &&
1952 1.1 joerg Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1953 1.1 joerg if (getNumBases() > 0)
1954 1.1 joerg return false;
1955 1.1 joerg return true;
1956 1.1 joerg }
1957 1.1 joerg
1958 1.1 joerg // FIXME: Any access specifiers is supposed to make this no longer interface
1959 1.1 joerg // like.
1960 1.1 joerg
1961 1.1 joerg // If this isn't a 'special' type, it must have a single interface-like base.
1962 1.1 joerg if (getNumBases() != 1)
1963 1.1 joerg return false;
1964 1.1 joerg
1965 1.1 joerg const auto BaseSpec = *bases_begin();
1966 1.1 joerg if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public)
1967 1.1 joerg return false;
1968 1.1 joerg const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
1969 1.1 joerg if (Base->isInterface() || !Base->isInterfaceLike())
1970 1.1 joerg return false;
1971 1.1 joerg return true;
1972 1.1 joerg }
1973 1.1 joerg
1974 1.1 joerg void CXXRecordDecl::completeDefinition() {
1975 1.1 joerg completeDefinition(nullptr);
1976 1.1 joerg }
1977 1.1 joerg
1978 1.1 joerg void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1979 1.1 joerg RecordDecl::completeDefinition();
1980 1.1 joerg
1981 1.1 joerg // If the class may be abstract (but hasn't been marked as such), check for
1982 1.1 joerg // any pure final overriders.
1983 1.1 joerg if (mayBeAbstract()) {
1984 1.1 joerg CXXFinalOverriderMap MyFinalOverriders;
1985 1.1 joerg if (!FinalOverriders) {
1986 1.1 joerg getFinalOverriders(MyFinalOverriders);
1987 1.1 joerg FinalOverriders = &MyFinalOverriders;
1988 1.1 joerg }
1989 1.1 joerg
1990 1.1 joerg bool Done = false;
1991 1.1 joerg for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1992 1.1 joerg MEnd = FinalOverriders->end();
1993 1.1 joerg M != MEnd && !Done; ++M) {
1994 1.1 joerg for (OverridingMethods::iterator SO = M->second.begin(),
1995 1.1 joerg SOEnd = M->second.end();
1996 1.1 joerg SO != SOEnd && !Done; ++SO) {
1997 1.1 joerg assert(SO->second.size() > 0 &&
1998 1.1 joerg "All virtual functions have overriding virtual functions");
1999 1.1 joerg
2000 1.1 joerg // C++ [class.abstract]p4:
2001 1.1 joerg // A class is abstract if it contains or inherits at least one
2002 1.1 joerg // pure virtual function for which the final overrider is pure
2003 1.1 joerg // virtual.
2004 1.1 joerg if (SO->second.front().Method->isPure()) {
2005 1.1 joerg data().Abstract = true;
2006 1.1 joerg Done = true;
2007 1.1 joerg break;
2008 1.1 joerg }
2009 1.1 joerg }
2010 1.1 joerg }
2011 1.1 joerg }
2012 1.1 joerg
2013 1.1 joerg // Set access bits correctly on the directly-declared conversions.
2014 1.1 joerg for (conversion_iterator I = conversion_begin(), E = conversion_end();
2015 1.1 joerg I != E; ++I)
2016 1.1 joerg I.setAccess((*I)->getAccess());
2017 1.1 joerg }
2018 1.1 joerg
2019 1.1 joerg bool CXXRecordDecl::mayBeAbstract() const {
2020 1.1 joerg if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
2021 1.1 joerg isDependentContext())
2022 1.1 joerg return false;
2023 1.1 joerg
2024 1.1 joerg for (const auto &B : bases()) {
2025 1.1 joerg const auto *BaseDecl =
2026 1.1 joerg cast<CXXRecordDecl>(B.getType()->castAs<RecordType>()->getDecl());
2027 1.1 joerg if (BaseDecl->isAbstract())
2028 1.1 joerg return true;
2029 1.1 joerg }
2030 1.1 joerg
2031 1.1 joerg return false;
2032 1.1 joerg }
2033 1.1 joerg
2034 1.1.1.2 joerg bool CXXRecordDecl::isEffectivelyFinal() const {
2035 1.1.1.2 joerg auto *Def = getDefinition();
2036 1.1.1.2 joerg if (!Def)
2037 1.1.1.2 joerg return false;
2038 1.1.1.2 joerg if (Def->hasAttr<FinalAttr>())
2039 1.1.1.2 joerg return true;
2040 1.1.1.2 joerg if (const auto *Dtor = Def->getDestructor())
2041 1.1.1.2 joerg if (Dtor->hasAttr<FinalAttr>())
2042 1.1.1.2 joerg return true;
2043 1.1.1.2 joerg return false;
2044 1.1.1.2 joerg }
2045 1.1.1.2 joerg
2046 1.1 joerg void CXXDeductionGuideDecl::anchor() {}
2047 1.1 joerg
2048 1.1 joerg bool ExplicitSpecifier::isEquivalent(const ExplicitSpecifier Other) const {
2049 1.1 joerg if ((getKind() != Other.getKind() ||
2050 1.1 joerg getKind() == ExplicitSpecKind::Unresolved)) {
2051 1.1 joerg if (getKind() == ExplicitSpecKind::Unresolved &&
2052 1.1 joerg Other.getKind() == ExplicitSpecKind::Unresolved) {
2053 1.1 joerg ODRHash SelfHash, OtherHash;
2054 1.1 joerg SelfHash.AddStmt(getExpr());
2055 1.1 joerg OtherHash.AddStmt(Other.getExpr());
2056 1.1 joerg return SelfHash.CalculateHash() == OtherHash.CalculateHash();
2057 1.1 joerg } else
2058 1.1 joerg return false;
2059 1.1 joerg }
2060 1.1 joerg return true;
2061 1.1 joerg }
2062 1.1 joerg
2063 1.1 joerg ExplicitSpecifier ExplicitSpecifier::getFromDecl(FunctionDecl *Function) {
2064 1.1 joerg switch (Function->getDeclKind()) {
2065 1.1 joerg case Decl::Kind::CXXConstructor:
2066 1.1 joerg return cast<CXXConstructorDecl>(Function)->getExplicitSpecifier();
2067 1.1 joerg case Decl::Kind::CXXConversion:
2068 1.1 joerg return cast<CXXConversionDecl>(Function)->getExplicitSpecifier();
2069 1.1 joerg case Decl::Kind::CXXDeductionGuide:
2070 1.1 joerg return cast<CXXDeductionGuideDecl>(Function)->getExplicitSpecifier();
2071 1.1 joerg default:
2072 1.1 joerg return {};
2073 1.1 joerg }
2074 1.1 joerg }
2075 1.1 joerg
2076 1.1.1.2 joerg CXXDeductionGuideDecl *
2077 1.1.1.2 joerg CXXDeductionGuideDecl::Create(ASTContext &C, DeclContext *DC,
2078 1.1.1.2 joerg SourceLocation StartLoc, ExplicitSpecifier ES,
2079 1.1.1.2 joerg const DeclarationNameInfo &NameInfo, QualType T,
2080 1.1.1.2 joerg TypeSourceInfo *TInfo, SourceLocation EndLocation,
2081 1.1.1.2 joerg CXXConstructorDecl *Ctor) {
2082 1.1 joerg return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, ES, NameInfo, T,
2083 1.1.1.2 joerg TInfo, EndLocation, Ctor);
2084 1.1 joerg }
2085 1.1 joerg
2086 1.1 joerg CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
2087 1.1 joerg unsigned ID) {
2088 1.1 joerg return new (C, ID) CXXDeductionGuideDecl(
2089 1.1 joerg C, nullptr, SourceLocation(), ExplicitSpecifier(), DeclarationNameInfo(),
2090 1.1.1.2 joerg QualType(), nullptr, SourceLocation(), nullptr);
2091 1.1.1.2 joerg }
2092 1.1.1.2 joerg
2093 1.1.1.2 joerg RequiresExprBodyDecl *RequiresExprBodyDecl::Create(
2094 1.1.1.2 joerg ASTContext &C, DeclContext *DC, SourceLocation StartLoc) {
2095 1.1.1.2 joerg return new (C, DC) RequiresExprBodyDecl(C, DC, StartLoc);
2096 1.1.1.2 joerg }
2097 1.1.1.2 joerg
2098 1.1.1.2 joerg RequiresExprBodyDecl *RequiresExprBodyDecl::CreateDeserialized(ASTContext &C,
2099 1.1.1.2 joerg unsigned ID) {
2100 1.1.1.2 joerg return new (C, ID) RequiresExprBodyDecl(C, nullptr, SourceLocation());
2101 1.1 joerg }
2102 1.1 joerg
2103 1.1 joerg void CXXMethodDecl::anchor() {}
2104 1.1 joerg
2105 1.1 joerg bool CXXMethodDecl::isStatic() const {
2106 1.1 joerg const CXXMethodDecl *MD = getCanonicalDecl();
2107 1.1 joerg
2108 1.1 joerg if (MD->getStorageClass() == SC_Static)
2109 1.1 joerg return true;
2110 1.1 joerg
2111 1.1 joerg OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
2112 1.1 joerg return isStaticOverloadedOperator(OOK);
2113 1.1 joerg }
2114 1.1 joerg
2115 1.1 joerg static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
2116 1.1 joerg const CXXMethodDecl *BaseMD) {
2117 1.1 joerg for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) {
2118 1.1 joerg if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
2119 1.1 joerg return true;
2120 1.1 joerg if (recursivelyOverrides(MD, BaseMD))
2121 1.1 joerg return true;
2122 1.1 joerg }
2123 1.1 joerg return false;
2124 1.1 joerg }
2125 1.1 joerg
2126 1.1 joerg CXXMethodDecl *
2127 1.1 joerg CXXMethodDecl::getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD,
2128 1.1 joerg bool MayBeBase) {
2129 1.1 joerg if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
2130 1.1 joerg return this;
2131 1.1 joerg
2132 1.1 joerg // Lookup doesn't work for destructors, so handle them separately.
2133 1.1 joerg if (isa<CXXDestructorDecl>(this)) {
2134 1.1 joerg CXXMethodDecl *MD = RD->getDestructor();
2135 1.1 joerg if (MD) {
2136 1.1 joerg if (recursivelyOverrides(MD, this))
2137 1.1 joerg return MD;
2138 1.1 joerg if (MayBeBase && recursivelyOverrides(this, MD))
2139 1.1 joerg return MD;
2140 1.1 joerg }
2141 1.1 joerg return nullptr;
2142 1.1 joerg }
2143 1.1 joerg
2144 1.1 joerg for (auto *ND : RD->lookup(getDeclName())) {
2145 1.1 joerg auto *MD = dyn_cast<CXXMethodDecl>(ND);
2146 1.1 joerg if (!MD)
2147 1.1 joerg continue;
2148 1.1 joerg if (recursivelyOverrides(MD, this))
2149 1.1 joerg return MD;
2150 1.1 joerg if (MayBeBase && recursivelyOverrides(this, MD))
2151 1.1 joerg return MD;
2152 1.1 joerg }
2153 1.1 joerg
2154 1.1 joerg return nullptr;
2155 1.1 joerg }
2156 1.1 joerg
2157 1.1 joerg CXXMethodDecl *
2158 1.1 joerg CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
2159 1.1 joerg bool MayBeBase) {
2160 1.1 joerg if (auto *MD = getCorrespondingMethodDeclaredInClass(RD, MayBeBase))
2161 1.1 joerg return MD;
2162 1.1 joerg
2163 1.1.1.2 joerg llvm::SmallVector<CXXMethodDecl*, 4> FinalOverriders;
2164 1.1.1.2 joerg auto AddFinalOverrider = [&](CXXMethodDecl *D) {
2165 1.1.1.2 joerg // If this function is overridden by a candidate final overrider, it is not
2166 1.1.1.2 joerg // a final overrider.
2167 1.1.1.2 joerg for (CXXMethodDecl *OtherD : FinalOverriders) {
2168 1.1.1.2 joerg if (declaresSameEntity(D, OtherD) || recursivelyOverrides(OtherD, D))
2169 1.1.1.2 joerg return;
2170 1.1.1.2 joerg }
2171 1.1.1.2 joerg
2172 1.1.1.2 joerg // Other candidate final overriders might be overridden by this function.
2173 1.1.1.2 joerg FinalOverriders.erase(
2174 1.1.1.2 joerg std::remove_if(FinalOverriders.begin(), FinalOverriders.end(),
2175 1.1.1.2 joerg [&](CXXMethodDecl *OtherD) {
2176 1.1.1.2 joerg return recursivelyOverrides(D, OtherD);
2177 1.1.1.2 joerg }),
2178 1.1.1.2 joerg FinalOverriders.end());
2179 1.1.1.2 joerg
2180 1.1.1.2 joerg FinalOverriders.push_back(D);
2181 1.1.1.2 joerg };
2182 1.1.1.2 joerg
2183 1.1 joerg for (const auto &I : RD->bases()) {
2184 1.1 joerg const RecordType *RT = I.getType()->getAs<RecordType>();
2185 1.1 joerg if (!RT)
2186 1.1 joerg continue;
2187 1.1 joerg const auto *Base = cast<CXXRecordDecl>(RT->getDecl());
2188 1.1.1.2 joerg if (CXXMethodDecl *D = this->getCorrespondingMethodInClass(Base))
2189 1.1.1.2 joerg AddFinalOverrider(D);
2190 1.1 joerg }
2191 1.1 joerg
2192 1.1.1.2 joerg return FinalOverriders.size() == 1 ? FinalOverriders.front() : nullptr;
2193 1.1 joerg }
2194 1.1 joerg
2195 1.1 joerg CXXMethodDecl *CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
2196 1.1 joerg SourceLocation StartLoc,
2197 1.1 joerg const DeclarationNameInfo &NameInfo,
2198 1.1 joerg QualType T, TypeSourceInfo *TInfo,
2199 1.1 joerg StorageClass SC, bool isInline,
2200 1.1 joerg ConstexprSpecKind ConstexprKind,
2201 1.1.1.2 joerg SourceLocation EndLocation,
2202 1.1.1.2 joerg Expr *TrailingRequiresClause) {
2203 1.1 joerg return new (C, RD)
2204 1.1 joerg CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo, T, TInfo, SC,
2205 1.1.1.2 joerg isInline, ConstexprKind, EndLocation,
2206 1.1.1.2 joerg TrailingRequiresClause);
2207 1.1 joerg }
2208 1.1 joerg
2209 1.1 joerg CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2210 1.1.1.2 joerg return new (C, ID)
2211 1.1.1.2 joerg CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
2212 1.1.1.2 joerg DeclarationNameInfo(), QualType(), nullptr, SC_None, false,
2213 1.1.1.2 joerg ConstexprSpecKind::Unspecified, SourceLocation(), nullptr);
2214 1.1 joerg }
2215 1.1 joerg
2216 1.1 joerg CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
2217 1.1 joerg bool IsAppleKext) {
2218 1.1 joerg assert(isVirtual() && "this method is expected to be virtual");
2219 1.1 joerg
2220 1.1 joerg // When building with -fapple-kext, all calls must go through the vtable since
2221 1.1 joerg // the kernel linker can do runtime patching of vtables.
2222 1.1 joerg if (IsAppleKext)
2223 1.1 joerg return nullptr;
2224 1.1 joerg
2225 1.1 joerg // If the member function is marked 'final', we know that it can't be
2226 1.1 joerg // overridden and can therefore devirtualize it unless it's pure virtual.
2227 1.1 joerg if (hasAttr<FinalAttr>())
2228 1.1 joerg return isPure() ? nullptr : this;
2229 1.1 joerg
2230 1.1 joerg // If Base is unknown, we cannot devirtualize.
2231 1.1 joerg if (!Base)
2232 1.1 joerg return nullptr;
2233 1.1 joerg
2234 1.1 joerg // If the base expression (after skipping derived-to-base conversions) is a
2235 1.1 joerg // class prvalue, then we can devirtualize.
2236 1.1 joerg Base = Base->getBestDynamicClassTypeExpr();
2237 1.1 joerg if (Base->isRValue() && Base->getType()->isRecordType())
2238 1.1 joerg return this;
2239 1.1 joerg
2240 1.1 joerg // If we don't even know what we would call, we can't devirtualize.
2241 1.1 joerg const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
2242 1.1 joerg if (!BestDynamicDecl)
2243 1.1 joerg return nullptr;
2244 1.1 joerg
2245 1.1 joerg // There may be a method corresponding to MD in a derived class.
2246 1.1 joerg CXXMethodDecl *DevirtualizedMethod =
2247 1.1 joerg getCorrespondingMethodInClass(BestDynamicDecl);
2248 1.1 joerg
2249 1.1.1.2 joerg // If there final overrider in the dynamic type is ambiguous, we can't
2250 1.1.1.2 joerg // devirtualize this call.
2251 1.1.1.2 joerg if (!DevirtualizedMethod)
2252 1.1.1.2 joerg return nullptr;
2253 1.1.1.2 joerg
2254 1.1 joerg // If that method is pure virtual, we can't devirtualize. If this code is
2255 1.1 joerg // reached, the result would be UB, not a direct call to the derived class
2256 1.1 joerg // function, and we can't assume the derived class function is defined.
2257 1.1 joerg if (DevirtualizedMethod->isPure())
2258 1.1 joerg return nullptr;
2259 1.1 joerg
2260 1.1 joerg // If that method is marked final, we can devirtualize it.
2261 1.1 joerg if (DevirtualizedMethod->hasAttr<FinalAttr>())
2262 1.1 joerg return DevirtualizedMethod;
2263 1.1 joerg
2264 1.1 joerg // Similarly, if the class itself or its destructor is marked 'final',
2265 1.1.1.2 joerg // the class can't be derived from and we can therefore devirtualize the
2266 1.1 joerg // member function call.
2267 1.1.1.2 joerg if (BestDynamicDecl->isEffectivelyFinal())
2268 1.1 joerg return DevirtualizedMethod;
2269 1.1 joerg
2270 1.1 joerg if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
2271 1.1 joerg if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
2272 1.1 joerg if (VD->getType()->isRecordType())
2273 1.1 joerg // This is a record decl. We know the type and can devirtualize it.
2274 1.1 joerg return DevirtualizedMethod;
2275 1.1 joerg
2276 1.1 joerg return nullptr;
2277 1.1 joerg }
2278 1.1 joerg
2279 1.1 joerg // We can devirtualize calls on an object accessed by a class member access
2280 1.1 joerg // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2281 1.1 joerg // a derived class object constructed in the same location.
2282 1.1 joerg if (const auto *ME = dyn_cast<MemberExpr>(Base)) {
2283 1.1 joerg const ValueDecl *VD = ME->getMemberDecl();
2284 1.1 joerg return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr;
2285 1.1 joerg }
2286 1.1 joerg
2287 1.1 joerg // Likewise for calls on an object accessed by a (non-reference) pointer to
2288 1.1 joerg // member access.
2289 1.1 joerg if (auto *BO = dyn_cast<BinaryOperator>(Base)) {
2290 1.1 joerg if (BO->isPtrMemOp()) {
2291 1.1 joerg auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>();
2292 1.1 joerg if (MPT->getPointeeType()->isRecordType())
2293 1.1 joerg return DevirtualizedMethod;
2294 1.1 joerg }
2295 1.1 joerg }
2296 1.1 joerg
2297 1.1 joerg // We can't devirtualize the call.
2298 1.1 joerg return nullptr;
2299 1.1 joerg }
2300 1.1 joerg
2301 1.1 joerg bool CXXMethodDecl::isUsualDeallocationFunction(
2302 1.1 joerg SmallVectorImpl<const FunctionDecl *> &PreventedBy) const {
2303 1.1 joerg assert(PreventedBy.empty() && "PreventedBy is expected to be empty");
2304 1.1 joerg if (getOverloadedOperator() != OO_Delete &&
2305 1.1 joerg getOverloadedOperator() != OO_Array_Delete)
2306 1.1 joerg return false;
2307 1.1 joerg
2308 1.1 joerg // C++ [basic.stc.dynamic.deallocation]p2:
2309 1.1 joerg // A template instance is never a usual deallocation function,
2310 1.1 joerg // regardless of its signature.
2311 1.1 joerg if (getPrimaryTemplate())
2312 1.1 joerg return false;
2313 1.1 joerg
2314 1.1 joerg // C++ [basic.stc.dynamic.deallocation]p2:
2315 1.1 joerg // If a class T has a member deallocation function named operator delete
2316 1.1 joerg // with exactly one parameter, then that function is a usual (non-placement)
2317 1.1 joerg // deallocation function. [...]
2318 1.1 joerg if (getNumParams() == 1)
2319 1.1 joerg return true;
2320 1.1 joerg unsigned UsualParams = 1;
2321 1.1 joerg
2322 1.1 joerg // C++ P0722:
2323 1.1 joerg // A destroying operator delete is a usual deallocation function if
2324 1.1 joerg // removing the std::destroying_delete_t parameter and changing the
2325 1.1 joerg // first parameter type from T* to void* results in the signature of
2326 1.1 joerg // a usual deallocation function.
2327 1.1 joerg if (isDestroyingOperatorDelete())
2328 1.1 joerg ++UsualParams;
2329 1.1 joerg
2330 1.1 joerg // C++ <=14 [basic.stc.dynamic.deallocation]p2:
2331 1.1 joerg // [...] If class T does not declare such an operator delete but does
2332 1.1 joerg // declare a member deallocation function named operator delete with
2333 1.1 joerg // exactly two parameters, the second of which has type std::size_t (18.1),
2334 1.1 joerg // then this function is a usual deallocation function.
2335 1.1 joerg //
2336 1.1 joerg // C++17 says a usual deallocation function is one with the signature
2337 1.1 joerg // (void* [, size_t] [, std::align_val_t] [, ...])
2338 1.1 joerg // and all such functions are usual deallocation functions. It's not clear
2339 1.1 joerg // that allowing varargs functions was intentional.
2340 1.1 joerg ASTContext &Context = getASTContext();
2341 1.1 joerg if (UsualParams < getNumParams() &&
2342 1.1 joerg Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
2343 1.1 joerg Context.getSizeType()))
2344 1.1 joerg ++UsualParams;
2345 1.1 joerg
2346 1.1 joerg if (UsualParams < getNumParams() &&
2347 1.1 joerg getParamDecl(UsualParams)->getType()->isAlignValT())
2348 1.1 joerg ++UsualParams;
2349 1.1 joerg
2350 1.1 joerg if (UsualParams != getNumParams())
2351 1.1 joerg return false;
2352 1.1 joerg
2353 1.1 joerg // In C++17 onwards, all potential usual deallocation functions are actual
2354 1.1 joerg // usual deallocation functions. Honor this behavior when post-C++14
2355 1.1 joerg // deallocation functions are offered as extensions too.
2356 1.1 joerg // FIXME(EricWF): Destrying Delete should be a language option. How do we
2357 1.1 joerg // handle when destroying delete is used prior to C++17?
2358 1.1 joerg if (Context.getLangOpts().CPlusPlus17 ||
2359 1.1 joerg Context.getLangOpts().AlignedAllocation ||
2360 1.1 joerg isDestroyingOperatorDelete())
2361 1.1 joerg return true;
2362 1.1 joerg
2363 1.1 joerg // This function is a usual deallocation function if there are no
2364 1.1 joerg // single-parameter deallocation functions of the same kind.
2365 1.1 joerg DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
2366 1.1 joerg bool Result = true;
2367 1.1 joerg for (const auto *D : R) {
2368 1.1 joerg if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2369 1.1 joerg if (FD->getNumParams() == 1) {
2370 1.1 joerg PreventedBy.push_back(FD);
2371 1.1 joerg Result = false;
2372 1.1 joerg }
2373 1.1 joerg }
2374 1.1 joerg }
2375 1.1 joerg return Result;
2376 1.1 joerg }
2377 1.1 joerg
2378 1.1 joerg bool CXXMethodDecl::isCopyAssignmentOperator() const {
2379 1.1 joerg // C++0x [class.copy]p17:
2380 1.1 joerg // A user-declared copy assignment operator X::operator= is a non-static
2381 1.1 joerg // non-template member function of class X with exactly one parameter of
2382 1.1 joerg // type X, X&, const X&, volatile X& or const volatile X&.
2383 1.1 joerg if (/*operator=*/getOverloadedOperator() != OO_Equal ||
2384 1.1 joerg /*non-static*/ isStatic() ||
2385 1.1 joerg /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2386 1.1 joerg getNumParams() != 1)
2387 1.1 joerg return false;
2388 1.1 joerg
2389 1.1 joerg QualType ParamType = getParamDecl(0)->getType();
2390 1.1 joerg if (const auto *Ref = ParamType->getAs<LValueReferenceType>())
2391 1.1 joerg ParamType = Ref->getPointeeType();
2392 1.1 joerg
2393 1.1 joerg ASTContext &Context = getASTContext();
2394 1.1 joerg QualType ClassType
2395 1.1 joerg = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2396 1.1 joerg return Context.hasSameUnqualifiedType(ClassType, ParamType);
2397 1.1 joerg }
2398 1.1 joerg
2399 1.1 joerg bool CXXMethodDecl::isMoveAssignmentOperator() const {
2400 1.1 joerg // C++0x [class.copy]p19:
2401 1.1 joerg // A user-declared move assignment operator X::operator= is a non-static
2402 1.1 joerg // non-template member function of class X with exactly one parameter of type
2403 1.1 joerg // X&&, const X&&, volatile X&&, or const volatile X&&.
2404 1.1 joerg if (getOverloadedOperator() != OO_Equal || isStatic() ||
2405 1.1 joerg getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2406 1.1 joerg getNumParams() != 1)
2407 1.1 joerg return false;
2408 1.1 joerg
2409 1.1 joerg QualType ParamType = getParamDecl(0)->getType();
2410 1.1 joerg if (!isa<RValueReferenceType>(ParamType))
2411 1.1 joerg return false;
2412 1.1 joerg ParamType = ParamType->getPointeeType();
2413 1.1 joerg
2414 1.1 joerg ASTContext &Context = getASTContext();
2415 1.1 joerg QualType ClassType
2416 1.1 joerg = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2417 1.1 joerg return Context.hasSameUnqualifiedType(ClassType, ParamType);
2418 1.1 joerg }
2419 1.1 joerg
2420 1.1 joerg void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
2421 1.1 joerg assert(MD->isCanonicalDecl() && "Method is not canonical!");
2422 1.1 joerg assert(!MD->getParent()->isDependentContext() &&
2423 1.1 joerg "Can't add an overridden method to a class template!");
2424 1.1 joerg assert(MD->isVirtual() && "Method is not virtual!");
2425 1.1 joerg
2426 1.1 joerg getASTContext().addOverriddenMethod(this, MD);
2427 1.1 joerg }
2428 1.1 joerg
2429 1.1 joerg CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
2430 1.1 joerg if (isa<CXXConstructorDecl>(this)) return nullptr;
2431 1.1 joerg return getASTContext().overridden_methods_begin(this);
2432 1.1 joerg }
2433 1.1 joerg
2434 1.1 joerg CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
2435 1.1 joerg if (isa<CXXConstructorDecl>(this)) return nullptr;
2436 1.1 joerg return getASTContext().overridden_methods_end(this);
2437 1.1 joerg }
2438 1.1 joerg
2439 1.1 joerg unsigned CXXMethodDecl::size_overridden_methods() const {
2440 1.1 joerg if (isa<CXXConstructorDecl>(this)) return 0;
2441 1.1 joerg return getASTContext().overridden_methods_size(this);
2442 1.1 joerg }
2443 1.1 joerg
2444 1.1 joerg CXXMethodDecl::overridden_method_range
2445 1.1 joerg CXXMethodDecl::overridden_methods() const {
2446 1.1 joerg if (isa<CXXConstructorDecl>(this))
2447 1.1 joerg return overridden_method_range(nullptr, nullptr);
2448 1.1 joerg return getASTContext().overridden_methods(this);
2449 1.1 joerg }
2450 1.1 joerg
2451 1.1 joerg static QualType getThisObjectType(ASTContext &C, const FunctionProtoType *FPT,
2452 1.1 joerg const CXXRecordDecl *Decl) {
2453 1.1 joerg QualType ClassTy = C.getTypeDeclType(Decl);
2454 1.1 joerg return C.getQualifiedType(ClassTy, FPT->getMethodQuals());
2455 1.1 joerg }
2456 1.1 joerg
2457 1.1 joerg QualType CXXMethodDecl::getThisType(const FunctionProtoType *FPT,
2458 1.1 joerg const CXXRecordDecl *Decl) {
2459 1.1 joerg ASTContext &C = Decl->getASTContext();
2460 1.1 joerg QualType ObjectTy = ::getThisObjectType(C, FPT, Decl);
2461 1.1 joerg return C.getPointerType(ObjectTy);
2462 1.1 joerg }
2463 1.1 joerg
2464 1.1 joerg QualType CXXMethodDecl::getThisObjectType(const FunctionProtoType *FPT,
2465 1.1 joerg const CXXRecordDecl *Decl) {
2466 1.1 joerg ASTContext &C = Decl->getASTContext();
2467 1.1 joerg return ::getThisObjectType(C, FPT, Decl);
2468 1.1 joerg }
2469 1.1 joerg
2470 1.1 joerg QualType CXXMethodDecl::getThisType() const {
2471 1.1 joerg // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2472 1.1 joerg // If the member function is declared const, the type of this is const X*,
2473 1.1 joerg // if the member function is declared volatile, the type of this is
2474 1.1 joerg // volatile X*, and if the member function is declared const volatile,
2475 1.1 joerg // the type of this is const volatile X*.
2476 1.1 joerg assert(isInstance() && "No 'this' for static methods!");
2477 1.1.1.2 joerg return CXXMethodDecl::getThisType(getType()->castAs<FunctionProtoType>(),
2478 1.1 joerg getParent());
2479 1.1 joerg }
2480 1.1 joerg
2481 1.1 joerg QualType CXXMethodDecl::getThisObjectType() const {
2482 1.1 joerg // Ditto getThisType.
2483 1.1 joerg assert(isInstance() && "No 'this' for static methods!");
2484 1.1.1.2 joerg return CXXMethodDecl::getThisObjectType(
2485 1.1.1.2 joerg getType()->castAs<FunctionProtoType>(), getParent());
2486 1.1 joerg }
2487 1.1 joerg
2488 1.1 joerg bool CXXMethodDecl::hasInlineBody() const {
2489 1.1 joerg // If this function is a template instantiation, look at the template from
2490 1.1 joerg // which it was instantiated.
2491 1.1 joerg const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
2492 1.1 joerg if (!CheckFn)
2493 1.1 joerg CheckFn = this;
2494 1.1 joerg
2495 1.1 joerg const FunctionDecl *fn;
2496 1.1 joerg return CheckFn->isDefined(fn) && !fn->isOutOfLine() &&
2497 1.1 joerg (fn->doesThisDeclarationHaveABody() || fn->willHaveBody());
2498 1.1 joerg }
2499 1.1 joerg
2500 1.1 joerg bool CXXMethodDecl::isLambdaStaticInvoker() const {
2501 1.1 joerg const CXXRecordDecl *P = getParent();
2502 1.1.1.2 joerg return P->isLambda() && getDeclName().isIdentifier() &&
2503 1.1.1.2 joerg getName() == getLambdaStaticInvokerName();
2504 1.1 joerg }
2505 1.1 joerg
2506 1.1 joerg CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2507 1.1 joerg TypeSourceInfo *TInfo, bool IsVirtual,
2508 1.1 joerg SourceLocation L, Expr *Init,
2509 1.1 joerg SourceLocation R,
2510 1.1 joerg SourceLocation EllipsisLoc)
2511 1.1.1.2 joerg : Initializee(TInfo), Init(Init), MemberOrEllipsisLocation(EllipsisLoc),
2512 1.1 joerg LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
2513 1.1 joerg IsWritten(false), SourceOrder(0) {}
2514 1.1 joerg
2515 1.1.1.2 joerg CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, FieldDecl *Member,
2516 1.1 joerg SourceLocation MemberLoc,
2517 1.1 joerg SourceLocation L, Expr *Init,
2518 1.1 joerg SourceLocation R)
2519 1.1.1.2 joerg : Initializee(Member), Init(Init), MemberOrEllipsisLocation(MemberLoc),
2520 1.1 joerg LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2521 1.1 joerg IsWritten(false), SourceOrder(0) {}
2522 1.1 joerg
2523 1.1 joerg CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2524 1.1 joerg IndirectFieldDecl *Member,
2525 1.1 joerg SourceLocation MemberLoc,
2526 1.1 joerg SourceLocation L, Expr *Init,
2527 1.1 joerg SourceLocation R)
2528 1.1.1.2 joerg : Initializee(Member), Init(Init), MemberOrEllipsisLocation(MemberLoc),
2529 1.1 joerg LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2530 1.1 joerg IsWritten(false), SourceOrder(0) {}
2531 1.1 joerg
2532 1.1 joerg CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2533 1.1 joerg TypeSourceInfo *TInfo,
2534 1.1 joerg SourceLocation L, Expr *Init,
2535 1.1 joerg SourceLocation R)
2536 1.1 joerg : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R),
2537 1.1 joerg IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
2538 1.1 joerg
2539 1.1 joerg int64_t CXXCtorInitializer::getID(const ASTContext &Context) const {
2540 1.1 joerg return Context.getAllocator()
2541 1.1 joerg .identifyKnownAlignedObject<CXXCtorInitializer>(this);
2542 1.1 joerg }
2543 1.1 joerg
2544 1.1 joerg TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
2545 1.1 joerg if (isBaseInitializer())
2546 1.1 joerg return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
2547 1.1 joerg else
2548 1.1 joerg return {};
2549 1.1 joerg }
2550 1.1 joerg
2551 1.1 joerg const Type *CXXCtorInitializer::getBaseClass() const {
2552 1.1 joerg if (isBaseInitializer())
2553 1.1 joerg return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
2554 1.1 joerg else
2555 1.1 joerg return nullptr;
2556 1.1 joerg }
2557 1.1 joerg
2558 1.1 joerg SourceLocation CXXCtorInitializer::getSourceLocation() const {
2559 1.1 joerg if (isInClassMemberInitializer())
2560 1.1 joerg return getAnyMember()->getLocation();
2561 1.1 joerg
2562 1.1 joerg if (isAnyMemberInitializer())
2563 1.1 joerg return getMemberLocation();
2564 1.1 joerg
2565 1.1 joerg if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>())
2566 1.1 joerg return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
2567 1.1 joerg
2568 1.1 joerg return {};
2569 1.1 joerg }
2570 1.1 joerg
2571 1.1 joerg SourceRange CXXCtorInitializer::getSourceRange() const {
2572 1.1 joerg if (isInClassMemberInitializer()) {
2573 1.1 joerg FieldDecl *D = getAnyMember();
2574 1.1 joerg if (Expr *I = D->getInClassInitializer())
2575 1.1 joerg return I->getSourceRange();
2576 1.1 joerg return {};
2577 1.1 joerg }
2578 1.1 joerg
2579 1.1 joerg return SourceRange(getSourceLocation(), getRParenLoc());
2580 1.1 joerg }
2581 1.1 joerg
2582 1.1 joerg CXXConstructorDecl::CXXConstructorDecl(
2583 1.1 joerg ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2584 1.1 joerg const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2585 1.1 joerg ExplicitSpecifier ES, bool isInline, bool isImplicitlyDeclared,
2586 1.1.1.2 joerg ConstexprSpecKind ConstexprKind, InheritedConstructor Inherited,
2587 1.1.1.2 joerg Expr *TrailingRequiresClause)
2588 1.1 joerg : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
2589 1.1.1.2 joerg SC_None, isInline, ConstexprKind, SourceLocation(),
2590 1.1.1.2 joerg TrailingRequiresClause) {
2591 1.1 joerg setNumCtorInitializers(0);
2592 1.1 joerg setInheritingConstructor(static_cast<bool>(Inherited));
2593 1.1 joerg setImplicit(isImplicitlyDeclared);
2594 1.1 joerg CXXConstructorDeclBits.HasTrailingExplicitSpecifier = ES.getExpr() ? 1 : 0;
2595 1.1 joerg if (Inherited)
2596 1.1 joerg *getTrailingObjects<InheritedConstructor>() = Inherited;
2597 1.1 joerg setExplicitSpecifier(ES);
2598 1.1 joerg }
2599 1.1 joerg
2600 1.1 joerg void CXXConstructorDecl::anchor() {}
2601 1.1 joerg
2602 1.1 joerg CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
2603 1.1 joerg unsigned ID,
2604 1.1 joerg uint64_t AllocKind) {
2605 1.1.1.2 joerg bool hasTrailingExplicit = static_cast<bool>(AllocKind & TAKHasTailExplicit);
2606 1.1 joerg bool isInheritingConstructor =
2607 1.1 joerg static_cast<bool>(AllocKind & TAKInheritsConstructor);
2608 1.1 joerg unsigned Extra =
2609 1.1 joerg additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2610 1.1.1.2 joerg isInheritingConstructor, hasTrailingExplicit);
2611 1.1.1.2 joerg auto *Result = new (C, ID, Extra) CXXConstructorDecl(
2612 1.1.1.2 joerg C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2613 1.1.1.2 joerg ExplicitSpecifier(), false, false, ConstexprSpecKind::Unspecified,
2614 1.1.1.2 joerg InheritedConstructor(), nullptr);
2615 1.1 joerg Result->setInheritingConstructor(isInheritingConstructor);
2616 1.1 joerg Result->CXXConstructorDeclBits.HasTrailingExplicitSpecifier =
2617 1.1.1.2 joerg hasTrailingExplicit;
2618 1.1 joerg Result->setExplicitSpecifier(ExplicitSpecifier());
2619 1.1 joerg return Result;
2620 1.1 joerg }
2621 1.1 joerg
2622 1.1 joerg CXXConstructorDecl *CXXConstructorDecl::Create(
2623 1.1 joerg ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2624 1.1 joerg const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2625 1.1 joerg ExplicitSpecifier ES, bool isInline, bool isImplicitlyDeclared,
2626 1.1.1.2 joerg ConstexprSpecKind ConstexprKind, InheritedConstructor Inherited,
2627 1.1.1.2 joerg Expr *TrailingRequiresClause) {
2628 1.1 joerg assert(NameInfo.getName().getNameKind()
2629 1.1 joerg == DeclarationName::CXXConstructorName &&
2630 1.1 joerg "Name must refer to a constructor");
2631 1.1 joerg unsigned Extra =
2632 1.1 joerg additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2633 1.1 joerg Inherited ? 1 : 0, ES.getExpr() ? 1 : 0);
2634 1.1 joerg return new (C, RD, Extra)
2635 1.1 joerg CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo, ES, isInline,
2636 1.1.1.2 joerg isImplicitlyDeclared, ConstexprKind, Inherited,
2637 1.1.1.2 joerg TrailingRequiresClause);
2638 1.1 joerg }
2639 1.1 joerg
2640 1.1 joerg CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
2641 1.1 joerg return CtorInitializers.get(getASTContext().getExternalSource());
2642 1.1 joerg }
2643 1.1 joerg
2644 1.1 joerg CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
2645 1.1 joerg assert(isDelegatingConstructor() && "Not a delegating constructor!");
2646 1.1 joerg Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
2647 1.1 joerg if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
2648 1.1 joerg return Construct->getConstructor();
2649 1.1 joerg
2650 1.1 joerg return nullptr;
2651 1.1 joerg }
2652 1.1 joerg
2653 1.1 joerg bool CXXConstructorDecl::isDefaultConstructor() const {
2654 1.1.1.2 joerg // C++ [class.default.ctor]p1:
2655 1.1.1.2 joerg // A default constructor for a class X is a constructor of class X for
2656 1.1.1.2 joerg // which each parameter that is not a function parameter pack has a default
2657 1.1.1.2 joerg // argument (including the case of a constructor with no parameters)
2658 1.1.1.2 joerg return getMinRequiredArguments() == 0;
2659 1.1 joerg }
2660 1.1 joerg
2661 1.1 joerg bool
2662 1.1 joerg CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
2663 1.1 joerg return isCopyOrMoveConstructor(TypeQuals) &&
2664 1.1 joerg getParamDecl(0)->getType()->isLValueReferenceType();
2665 1.1 joerg }
2666 1.1 joerg
2667 1.1 joerg bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
2668 1.1 joerg return isCopyOrMoveConstructor(TypeQuals) &&
2669 1.1.1.2 joerg getParamDecl(0)->getType()->isRValueReferenceType();
2670 1.1 joerg }
2671 1.1 joerg
2672 1.1 joerg /// Determine whether this is a copy or move constructor.
2673 1.1 joerg bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
2674 1.1 joerg // C++ [class.copy]p2:
2675 1.1 joerg // A non-template constructor for class X is a copy constructor
2676 1.1 joerg // if its first parameter is of type X&, const X&, volatile X& or
2677 1.1 joerg // const volatile X&, and either there are no other parameters
2678 1.1 joerg // or else all other parameters have default arguments (8.3.6).
2679 1.1 joerg // C++0x [class.copy]p3:
2680 1.1 joerg // A non-template constructor for class X is a move constructor if its
2681 1.1 joerg // first parameter is of type X&&, const X&&, volatile X&&, or
2682 1.1 joerg // const volatile X&&, and either there are no other parameters or else
2683 1.1 joerg // all other parameters have default arguments.
2684 1.1.1.2 joerg if (!hasOneParamOrDefaultArgs() || getPrimaryTemplate() != nullptr ||
2685 1.1.1.2 joerg getDescribedFunctionTemplate() != nullptr)
2686 1.1 joerg return false;
2687 1.1 joerg
2688 1.1 joerg const ParmVarDecl *Param = getParamDecl(0);
2689 1.1 joerg
2690 1.1 joerg // Do we have a reference type?
2691 1.1 joerg const auto *ParamRefType = Param->getType()->getAs<ReferenceType>();
2692 1.1 joerg if (!ParamRefType)
2693 1.1 joerg return false;
2694 1.1 joerg
2695 1.1 joerg // Is it a reference to our class type?
2696 1.1 joerg ASTContext &Context = getASTContext();
2697 1.1 joerg
2698 1.1 joerg CanQualType PointeeType
2699 1.1 joerg = Context.getCanonicalType(ParamRefType->getPointeeType());
2700 1.1 joerg CanQualType ClassTy
2701 1.1 joerg = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2702 1.1 joerg if (PointeeType.getUnqualifiedType() != ClassTy)
2703 1.1 joerg return false;
2704 1.1 joerg
2705 1.1 joerg // FIXME: other qualifiers?
2706 1.1 joerg
2707 1.1 joerg // We have a copy or move constructor.
2708 1.1 joerg TypeQuals = PointeeType.getCVRQualifiers();
2709 1.1 joerg return true;
2710 1.1 joerg }
2711 1.1 joerg
2712 1.1 joerg bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
2713 1.1 joerg // C++ [class.conv.ctor]p1:
2714 1.1 joerg // A constructor declared without the function-specifier explicit
2715 1.1 joerg // that can be called with a single parameter specifies a
2716 1.1 joerg // conversion from the type of its first parameter to the type of
2717 1.1 joerg // its class. Such a constructor is called a converting
2718 1.1 joerg // constructor.
2719 1.1 joerg if (isExplicit() && !AllowExplicit)
2720 1.1 joerg return false;
2721 1.1 joerg
2722 1.1.1.2 joerg // FIXME: This has nothing to do with the definition of converting
2723 1.1.1.2 joerg // constructor, but is convenient for how we use this function in overload
2724 1.1.1.2 joerg // resolution.
2725 1.1.1.2 joerg return getNumParams() == 0
2726 1.1.1.2 joerg ? getType()->castAs<FunctionProtoType>()->isVariadic()
2727 1.1.1.2 joerg : getMinRequiredArguments() <= 1;
2728 1.1 joerg }
2729 1.1 joerg
2730 1.1 joerg bool CXXConstructorDecl::isSpecializationCopyingObject() const {
2731 1.1.1.2 joerg if (!hasOneParamOrDefaultArgs() || getDescribedFunctionTemplate() != nullptr)
2732 1.1 joerg return false;
2733 1.1 joerg
2734 1.1 joerg const ParmVarDecl *Param = getParamDecl(0);
2735 1.1 joerg
2736 1.1 joerg ASTContext &Context = getASTContext();
2737 1.1 joerg CanQualType ParamType = Context.getCanonicalType(Param->getType());
2738 1.1 joerg
2739 1.1 joerg // Is it the same as our class type?
2740 1.1 joerg CanQualType ClassTy
2741 1.1 joerg = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2742 1.1 joerg if (ParamType.getUnqualifiedType() != ClassTy)
2743 1.1 joerg return false;
2744 1.1 joerg
2745 1.1 joerg return true;
2746 1.1 joerg }
2747 1.1 joerg
2748 1.1 joerg void CXXDestructorDecl::anchor() {}
2749 1.1 joerg
2750 1.1 joerg CXXDestructorDecl *
2751 1.1 joerg CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2752 1.1.1.2 joerg return new (C, ID) CXXDestructorDecl(
2753 1.1.1.2 joerg C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2754 1.1.1.2 joerg false, false, ConstexprSpecKind::Unspecified, nullptr);
2755 1.1 joerg }
2756 1.1 joerg
2757 1.1 joerg CXXDestructorDecl *CXXDestructorDecl::Create(
2758 1.1 joerg ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2759 1.1 joerg const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2760 1.1.1.2 joerg bool isInline, bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind,
2761 1.1.1.2 joerg Expr *TrailingRequiresClause) {
2762 1.1 joerg assert(NameInfo.getName().getNameKind()
2763 1.1 joerg == DeclarationName::CXXDestructorName &&
2764 1.1 joerg "Name must refer to a destructor");
2765 1.1 joerg return new (C, RD)
2766 1.1 joerg CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo, isInline,
2767 1.1.1.2 joerg isImplicitlyDeclared, ConstexprKind,
2768 1.1.1.2 joerg TrailingRequiresClause);
2769 1.1 joerg }
2770 1.1 joerg
2771 1.1 joerg void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
2772 1.1 joerg auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2773 1.1 joerg if (OD && !First->OperatorDelete) {
2774 1.1 joerg First->OperatorDelete = OD;
2775 1.1 joerg First->OperatorDeleteThisArg = ThisArg;
2776 1.1 joerg if (auto *L = getASTMutationListener())
2777 1.1 joerg L->ResolvedOperatorDelete(First, OD, ThisArg);
2778 1.1 joerg }
2779 1.1 joerg }
2780 1.1 joerg
2781 1.1 joerg void CXXConversionDecl::anchor() {}
2782 1.1 joerg
2783 1.1 joerg CXXConversionDecl *
2784 1.1 joerg CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2785 1.1 joerg return new (C, ID) CXXConversionDecl(
2786 1.1 joerg C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2787 1.1.1.2 joerg false, ExplicitSpecifier(), ConstexprSpecKind::Unspecified,
2788 1.1.1.2 joerg SourceLocation(), nullptr);
2789 1.1 joerg }
2790 1.1 joerg
2791 1.1 joerg CXXConversionDecl *CXXConversionDecl::Create(
2792 1.1 joerg ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2793 1.1 joerg const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2794 1.1 joerg bool isInline, ExplicitSpecifier ES, ConstexprSpecKind ConstexprKind,
2795 1.1.1.2 joerg SourceLocation EndLocation, Expr *TrailingRequiresClause) {
2796 1.1 joerg assert(NameInfo.getName().getNameKind()
2797 1.1 joerg == DeclarationName::CXXConversionFunctionName &&
2798 1.1 joerg "Name must refer to a conversion function");
2799 1.1 joerg return new (C, RD)
2800 1.1 joerg CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo, isInline, ES,
2801 1.1.1.2 joerg ConstexprKind, EndLocation, TrailingRequiresClause);
2802 1.1 joerg }
2803 1.1 joerg
2804 1.1 joerg bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2805 1.1 joerg return isImplicit() && getParent()->isLambda() &&
2806 1.1 joerg getConversionType()->isBlockPointerType();
2807 1.1 joerg }
2808 1.1 joerg
2809 1.1 joerg LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2810 1.1 joerg SourceLocation LangLoc, LanguageIDs lang,
2811 1.1 joerg bool HasBraces)
2812 1.1 joerg : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2813 1.1 joerg ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) {
2814 1.1 joerg setLanguage(lang);
2815 1.1 joerg LinkageSpecDeclBits.HasBraces = HasBraces;
2816 1.1 joerg }
2817 1.1 joerg
2818 1.1 joerg void LinkageSpecDecl::anchor() {}
2819 1.1 joerg
2820 1.1 joerg LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
2821 1.1 joerg DeclContext *DC,
2822 1.1 joerg SourceLocation ExternLoc,
2823 1.1 joerg SourceLocation LangLoc,
2824 1.1 joerg LanguageIDs Lang,
2825 1.1 joerg bool HasBraces) {
2826 1.1 joerg return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
2827 1.1 joerg }
2828 1.1 joerg
2829 1.1 joerg LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2830 1.1 joerg unsigned ID) {
2831 1.1 joerg return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2832 1.1 joerg SourceLocation(), lang_c, false);
2833 1.1 joerg }
2834 1.1 joerg
2835 1.1 joerg void UsingDirectiveDecl::anchor() {}
2836 1.1 joerg
2837 1.1 joerg UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2838 1.1 joerg SourceLocation L,
2839 1.1 joerg SourceLocation NamespaceLoc,
2840 1.1 joerg NestedNameSpecifierLoc QualifierLoc,
2841 1.1 joerg SourceLocation IdentLoc,
2842 1.1 joerg NamedDecl *Used,
2843 1.1 joerg DeclContext *CommonAncestor) {
2844 1.1 joerg if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used))
2845 1.1 joerg Used = NS->getOriginalNamespace();
2846 1.1 joerg return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2847 1.1 joerg IdentLoc, Used, CommonAncestor);
2848 1.1 joerg }
2849 1.1 joerg
2850 1.1 joerg UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2851 1.1 joerg unsigned ID) {
2852 1.1 joerg return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2853 1.1 joerg SourceLocation(),
2854 1.1 joerg NestedNameSpecifierLoc(),
2855 1.1 joerg SourceLocation(), nullptr, nullptr);
2856 1.1 joerg }
2857 1.1 joerg
2858 1.1 joerg NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2859 1.1 joerg if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2860 1.1 joerg return NA->getNamespace();
2861 1.1 joerg return cast_or_null<NamespaceDecl>(NominatedNamespace);
2862 1.1 joerg }
2863 1.1 joerg
2864 1.1 joerg NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2865 1.1 joerg SourceLocation StartLoc, SourceLocation IdLoc,
2866 1.1 joerg IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2867 1.1 joerg : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2868 1.1 joerg redeclarable_base(C), LocStart(StartLoc),
2869 1.1 joerg AnonOrFirstNamespaceAndInline(nullptr, Inline) {
2870 1.1 joerg setPreviousDecl(PrevDecl);
2871 1.1 joerg
2872 1.1 joerg if (PrevDecl)
2873 1.1 joerg AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2874 1.1 joerg }
2875 1.1 joerg
2876 1.1 joerg NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2877 1.1 joerg bool Inline, SourceLocation StartLoc,
2878 1.1 joerg SourceLocation IdLoc, IdentifierInfo *Id,
2879 1.1 joerg NamespaceDecl *PrevDecl) {
2880 1.1 joerg return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2881 1.1 joerg PrevDecl);
2882 1.1 joerg }
2883 1.1 joerg
2884 1.1 joerg NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2885 1.1 joerg return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
2886 1.1 joerg SourceLocation(), nullptr, nullptr);
2887 1.1 joerg }
2888 1.1 joerg
2889 1.1 joerg NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2890 1.1 joerg if (isFirstDecl())
2891 1.1 joerg return this;
2892 1.1 joerg
2893 1.1 joerg return AnonOrFirstNamespaceAndInline.getPointer();
2894 1.1 joerg }
2895 1.1 joerg
2896 1.1 joerg const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2897 1.1 joerg if (isFirstDecl())
2898 1.1 joerg return this;
2899 1.1 joerg
2900 1.1 joerg return AnonOrFirstNamespaceAndInline.getPointer();
2901 1.1 joerg }
2902 1.1 joerg
2903 1.1 joerg bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2904 1.1 joerg
2905 1.1 joerg NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2906 1.1 joerg return getNextRedeclaration();
2907 1.1 joerg }
2908 1.1 joerg
2909 1.1 joerg NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2910 1.1 joerg return getPreviousDecl();
2911 1.1 joerg }
2912 1.1 joerg
2913 1.1 joerg NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2914 1.1 joerg return getMostRecentDecl();
2915 1.1 joerg }
2916 1.1 joerg
2917 1.1 joerg void NamespaceAliasDecl::anchor() {}
2918 1.1 joerg
2919 1.1 joerg NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2920 1.1 joerg return getNextRedeclaration();
2921 1.1 joerg }
2922 1.1 joerg
2923 1.1 joerg NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2924 1.1 joerg return getPreviousDecl();
2925 1.1 joerg }
2926 1.1 joerg
2927 1.1 joerg NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2928 1.1 joerg return getMostRecentDecl();
2929 1.1 joerg }
2930 1.1 joerg
2931 1.1 joerg NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
2932 1.1 joerg SourceLocation UsingLoc,
2933 1.1 joerg SourceLocation AliasLoc,
2934 1.1 joerg IdentifierInfo *Alias,
2935 1.1 joerg NestedNameSpecifierLoc QualifierLoc,
2936 1.1 joerg SourceLocation IdentLoc,
2937 1.1 joerg NamedDecl *Namespace) {
2938 1.1 joerg // FIXME: Preserve the aliased namespace as written.
2939 1.1 joerg if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2940 1.1 joerg Namespace = NS->getOriginalNamespace();
2941 1.1 joerg return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
2942 1.1 joerg QualifierLoc, IdentLoc, Namespace);
2943 1.1 joerg }
2944 1.1 joerg
2945 1.1 joerg NamespaceAliasDecl *
2946 1.1 joerg NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2947 1.1 joerg return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
2948 1.1 joerg SourceLocation(), nullptr,
2949 1.1 joerg NestedNameSpecifierLoc(),
2950 1.1 joerg SourceLocation(), nullptr);
2951 1.1 joerg }
2952 1.1 joerg
2953 1.1.1.2 joerg void LifetimeExtendedTemporaryDecl::anchor() {}
2954 1.1.1.2 joerg
2955 1.1.1.2 joerg /// Retrieve the storage duration for the materialized temporary.
2956 1.1.1.2 joerg StorageDuration LifetimeExtendedTemporaryDecl::getStorageDuration() const {
2957 1.1.1.2 joerg const ValueDecl *ExtendingDecl = getExtendingDecl();
2958 1.1.1.2 joerg if (!ExtendingDecl)
2959 1.1.1.2 joerg return SD_FullExpression;
2960 1.1.1.2 joerg // FIXME: This is not necessarily correct for a temporary materialized
2961 1.1.1.2 joerg // within a default initializer.
2962 1.1.1.2 joerg if (isa<FieldDecl>(ExtendingDecl))
2963 1.1.1.2 joerg return SD_Automatic;
2964 1.1.1.2 joerg // FIXME: This only works because storage class specifiers are not allowed
2965 1.1.1.2 joerg // on decomposition declarations.
2966 1.1.1.2 joerg if (isa<BindingDecl>(ExtendingDecl))
2967 1.1.1.2 joerg return ExtendingDecl->getDeclContext()->isFunctionOrMethod() ? SD_Automatic
2968 1.1.1.2 joerg : SD_Static;
2969 1.1.1.2 joerg return cast<VarDecl>(ExtendingDecl)->getStorageDuration();
2970 1.1.1.2 joerg }
2971 1.1.1.2 joerg
2972 1.1.1.2 joerg APValue *LifetimeExtendedTemporaryDecl::getOrCreateValue(bool MayCreate) const {
2973 1.1.1.2 joerg assert(getStorageDuration() == SD_Static &&
2974 1.1.1.2 joerg "don't need to cache the computed value for this temporary");
2975 1.1.1.2 joerg if (MayCreate && !Value) {
2976 1.1.1.2 joerg Value = (new (getASTContext()) APValue);
2977 1.1.1.2 joerg getASTContext().addDestruction(Value);
2978 1.1.1.2 joerg }
2979 1.1.1.2 joerg assert(Value && "may not be null");
2980 1.1.1.2 joerg return Value;
2981 1.1.1.2 joerg }
2982 1.1.1.2 joerg
2983 1.1 joerg void UsingShadowDecl::anchor() {}
2984 1.1 joerg
2985 1.1 joerg UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2986 1.1 joerg SourceLocation Loc, UsingDecl *Using,
2987 1.1 joerg NamedDecl *Target)
2988 1.1 joerg : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
2989 1.1 joerg redeclarable_base(C), UsingOrNextShadow(cast<NamedDecl>(Using)) {
2990 1.1 joerg if (Target)
2991 1.1 joerg setTargetDecl(Target);
2992 1.1 joerg setImplicit();
2993 1.1 joerg }
2994 1.1 joerg
2995 1.1 joerg UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2996 1.1 joerg : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
2997 1.1 joerg redeclarable_base(C) {}
2998 1.1 joerg
2999 1.1 joerg UsingShadowDecl *
3000 1.1 joerg UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3001 1.1 joerg return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
3002 1.1 joerg }
3003 1.1 joerg
3004 1.1 joerg UsingDecl *UsingShadowDecl::getUsingDecl() const {
3005 1.1 joerg const UsingShadowDecl *Shadow = this;
3006 1.1 joerg while (const auto *NextShadow =
3007 1.1 joerg dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
3008 1.1 joerg Shadow = NextShadow;
3009 1.1 joerg return cast<UsingDecl>(Shadow->UsingOrNextShadow);
3010 1.1 joerg }
3011 1.1 joerg
3012 1.1 joerg void ConstructorUsingShadowDecl::anchor() {}
3013 1.1 joerg
3014 1.1 joerg ConstructorUsingShadowDecl *
3015 1.1 joerg ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
3016 1.1 joerg SourceLocation Loc, UsingDecl *Using,
3017 1.1 joerg NamedDecl *Target, bool IsVirtual) {
3018 1.1 joerg return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
3019 1.1 joerg IsVirtual);
3020 1.1 joerg }
3021 1.1 joerg
3022 1.1 joerg ConstructorUsingShadowDecl *
3023 1.1 joerg ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3024 1.1 joerg return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
3025 1.1 joerg }
3026 1.1 joerg
3027 1.1 joerg CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
3028 1.1 joerg return getUsingDecl()->getQualifier()->getAsRecordDecl();
3029 1.1 joerg }
3030 1.1 joerg
3031 1.1 joerg void UsingDecl::anchor() {}
3032 1.1 joerg
3033 1.1 joerg void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
3034 1.1 joerg assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
3035 1.1 joerg "declaration already in set");
3036 1.1 joerg assert(S->getUsingDecl() == this);
3037 1.1 joerg
3038 1.1 joerg if (FirstUsingShadow.getPointer())
3039 1.1 joerg S->UsingOrNextShadow = FirstUsingShadow.getPointer();
3040 1.1 joerg FirstUsingShadow.setPointer(S);
3041 1.1 joerg }
3042 1.1 joerg
3043 1.1 joerg void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
3044 1.1 joerg assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
3045 1.1 joerg "declaration not in set");
3046 1.1 joerg assert(S->getUsingDecl() == this);
3047 1.1 joerg
3048 1.1 joerg // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
3049 1.1 joerg
3050 1.1 joerg if (FirstUsingShadow.getPointer() == S) {
3051 1.1 joerg FirstUsingShadow.setPointer(
3052 1.1 joerg dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
3053 1.1 joerg S->UsingOrNextShadow = this;
3054 1.1 joerg return;
3055 1.1 joerg }
3056 1.1 joerg
3057 1.1 joerg UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
3058 1.1 joerg while (Prev->UsingOrNextShadow != S)
3059 1.1 joerg Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
3060 1.1 joerg Prev->UsingOrNextShadow = S->UsingOrNextShadow;
3061 1.1 joerg S->UsingOrNextShadow = this;
3062 1.1 joerg }
3063 1.1 joerg
3064 1.1 joerg UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
3065 1.1 joerg NestedNameSpecifierLoc QualifierLoc,
3066 1.1 joerg const DeclarationNameInfo &NameInfo,
3067 1.1 joerg bool HasTypename) {
3068 1.1 joerg return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
3069 1.1 joerg }
3070 1.1 joerg
3071 1.1 joerg UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3072 1.1 joerg return new (C, ID) UsingDecl(nullptr, SourceLocation(),
3073 1.1 joerg NestedNameSpecifierLoc(), DeclarationNameInfo(),
3074 1.1 joerg false);
3075 1.1 joerg }
3076 1.1 joerg
3077 1.1 joerg SourceRange UsingDecl::getSourceRange() const {
3078 1.1 joerg SourceLocation Begin = isAccessDeclaration()
3079 1.1 joerg ? getQualifierLoc().getBeginLoc() : UsingLocation;
3080 1.1 joerg return SourceRange(Begin, getNameInfo().getEndLoc());
3081 1.1 joerg }
3082 1.1 joerg
3083 1.1 joerg void UsingPackDecl::anchor() {}
3084 1.1 joerg
3085 1.1 joerg UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
3086 1.1 joerg NamedDecl *InstantiatedFrom,
3087 1.1 joerg ArrayRef<NamedDecl *> UsingDecls) {
3088 1.1 joerg size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
3089 1.1 joerg return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
3090 1.1 joerg }
3091 1.1 joerg
3092 1.1 joerg UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3093 1.1 joerg unsigned NumExpansions) {
3094 1.1 joerg size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
3095 1.1 joerg auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
3096 1.1 joerg Result->NumExpansions = NumExpansions;
3097 1.1 joerg auto *Trail = Result->getTrailingObjects<NamedDecl *>();
3098 1.1 joerg for (unsigned I = 0; I != NumExpansions; ++I)
3099 1.1 joerg new (Trail + I) NamedDecl*(nullptr);
3100 1.1 joerg return Result;
3101 1.1 joerg }
3102 1.1 joerg
3103 1.1 joerg void UnresolvedUsingValueDecl::anchor() {}
3104 1.1 joerg
3105 1.1 joerg UnresolvedUsingValueDecl *
3106 1.1 joerg UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
3107 1.1 joerg SourceLocation UsingLoc,
3108 1.1 joerg NestedNameSpecifierLoc QualifierLoc,
3109 1.1 joerg const DeclarationNameInfo &NameInfo,
3110 1.1 joerg SourceLocation EllipsisLoc) {
3111 1.1 joerg return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
3112 1.1 joerg QualifierLoc, NameInfo,
3113 1.1 joerg EllipsisLoc);
3114 1.1 joerg }
3115 1.1 joerg
3116 1.1 joerg UnresolvedUsingValueDecl *
3117 1.1 joerg UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3118 1.1 joerg return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
3119 1.1 joerg SourceLocation(),
3120 1.1 joerg NestedNameSpecifierLoc(),
3121 1.1 joerg DeclarationNameInfo(),
3122 1.1 joerg SourceLocation());
3123 1.1 joerg }
3124 1.1 joerg
3125 1.1 joerg SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
3126 1.1 joerg SourceLocation Begin = isAccessDeclaration()
3127 1.1 joerg ? getQualifierLoc().getBeginLoc() : UsingLocation;
3128 1.1 joerg return SourceRange(Begin, getNameInfo().getEndLoc());
3129 1.1 joerg }
3130 1.1 joerg
3131 1.1 joerg void UnresolvedUsingTypenameDecl::anchor() {}
3132 1.1 joerg
3133 1.1 joerg UnresolvedUsingTypenameDecl *
3134 1.1 joerg UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
3135 1.1 joerg SourceLocation UsingLoc,
3136 1.1 joerg SourceLocation TypenameLoc,
3137 1.1 joerg NestedNameSpecifierLoc QualifierLoc,
3138 1.1 joerg SourceLocation TargetNameLoc,
3139 1.1 joerg DeclarationName TargetName,
3140 1.1 joerg SourceLocation EllipsisLoc) {
3141 1.1 joerg return new (C, DC) UnresolvedUsingTypenameDecl(
3142 1.1 joerg DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
3143 1.1 joerg TargetName.getAsIdentifierInfo(), EllipsisLoc);
3144 1.1 joerg }
3145 1.1 joerg
3146 1.1 joerg UnresolvedUsingTypenameDecl *
3147 1.1 joerg UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3148 1.1 joerg return new (C, ID) UnresolvedUsingTypenameDecl(
3149 1.1 joerg nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
3150 1.1 joerg SourceLocation(), nullptr, SourceLocation());
3151 1.1 joerg }
3152 1.1 joerg
3153 1.1 joerg void StaticAssertDecl::anchor() {}
3154 1.1 joerg
3155 1.1 joerg StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
3156 1.1 joerg SourceLocation StaticAssertLoc,
3157 1.1 joerg Expr *AssertExpr,
3158 1.1 joerg StringLiteral *Message,
3159 1.1 joerg SourceLocation RParenLoc,
3160 1.1 joerg bool Failed) {
3161 1.1 joerg return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
3162 1.1 joerg RParenLoc, Failed);
3163 1.1 joerg }
3164 1.1 joerg
3165 1.1 joerg StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
3166 1.1 joerg unsigned ID) {
3167 1.1 joerg return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
3168 1.1 joerg nullptr, SourceLocation(), false);
3169 1.1 joerg }
3170 1.1 joerg
3171 1.1 joerg void BindingDecl::anchor() {}
3172 1.1 joerg
3173 1.1 joerg BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
3174 1.1 joerg SourceLocation IdLoc, IdentifierInfo *Id) {
3175 1.1 joerg return new (C, DC) BindingDecl(DC, IdLoc, Id);
3176 1.1 joerg }
3177 1.1 joerg
3178 1.1 joerg BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3179 1.1 joerg return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
3180 1.1 joerg }
3181 1.1 joerg
3182 1.1 joerg VarDecl *BindingDecl::getHoldingVar() const {
3183 1.1 joerg Expr *B = getBinding();
3184 1.1 joerg if (!B)
3185 1.1 joerg return nullptr;
3186 1.1 joerg auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
3187 1.1 joerg if (!DRE)
3188 1.1 joerg return nullptr;
3189 1.1 joerg
3190 1.1.1.2 joerg auto *VD = cast<VarDecl>(DRE->getDecl());
3191 1.1 joerg assert(VD->isImplicit() && "holding var for binding decl not implicit");
3192 1.1 joerg return VD;
3193 1.1 joerg }
3194 1.1 joerg
3195 1.1 joerg void DecompositionDecl::anchor() {}
3196 1.1 joerg
3197 1.1 joerg DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
3198 1.1 joerg SourceLocation StartLoc,
3199 1.1 joerg SourceLocation LSquareLoc,
3200 1.1 joerg QualType T, TypeSourceInfo *TInfo,
3201 1.1 joerg StorageClass SC,
3202 1.1 joerg ArrayRef<BindingDecl *> Bindings) {
3203 1.1 joerg size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
3204 1.1 joerg return new (C, DC, Extra)
3205 1.1 joerg DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
3206 1.1 joerg }
3207 1.1 joerg
3208 1.1 joerg DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
3209 1.1 joerg unsigned ID,
3210 1.1 joerg unsigned NumBindings) {
3211 1.1 joerg size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
3212 1.1 joerg auto *Result = new (C, ID, Extra)
3213 1.1 joerg DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
3214 1.1 joerg QualType(), nullptr, StorageClass(), None);
3215 1.1 joerg // Set up and clean out the bindings array.
3216 1.1 joerg Result->NumBindings = NumBindings;
3217 1.1 joerg auto *Trail = Result->getTrailingObjects<BindingDecl *>();
3218 1.1 joerg for (unsigned I = 0; I != NumBindings; ++I)
3219 1.1 joerg new (Trail + I) BindingDecl*(nullptr);
3220 1.1 joerg return Result;
3221 1.1 joerg }
3222 1.1 joerg
3223 1.1 joerg void DecompositionDecl::printName(llvm::raw_ostream &os) const {
3224 1.1 joerg os << '[';
3225 1.1 joerg bool Comma = false;
3226 1.1 joerg for (const auto *B : bindings()) {
3227 1.1 joerg if (Comma)
3228 1.1 joerg os << ", ";
3229 1.1 joerg B->printName(os);
3230 1.1 joerg Comma = true;
3231 1.1 joerg }
3232 1.1 joerg os << ']';
3233 1.1 joerg }
3234 1.1 joerg
3235 1.1 joerg void MSPropertyDecl::anchor() {}
3236 1.1 joerg
3237 1.1 joerg MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
3238 1.1 joerg SourceLocation L, DeclarationName N,
3239 1.1 joerg QualType T, TypeSourceInfo *TInfo,
3240 1.1 joerg SourceLocation StartL,
3241 1.1 joerg IdentifierInfo *Getter,
3242 1.1 joerg IdentifierInfo *Setter) {
3243 1.1 joerg return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
3244 1.1 joerg }
3245 1.1 joerg
3246 1.1 joerg MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3247 1.1 joerg unsigned ID) {
3248 1.1 joerg return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
3249 1.1 joerg DeclarationName(), QualType(), nullptr,
3250 1.1 joerg SourceLocation(), nullptr, nullptr);
3251 1.1 joerg }
3252 1.1 joerg
3253 1.1.1.2 joerg void MSGuidDecl::anchor() {}
3254 1.1.1.2 joerg
3255 1.1.1.2 joerg MSGuidDecl::MSGuidDecl(DeclContext *DC, QualType T, Parts P)
3256 1.1.1.2 joerg : ValueDecl(Decl::MSGuid, DC, SourceLocation(), DeclarationName(), T),
3257 1.1.1.2 joerg PartVal(P), APVal() {}
3258 1.1.1.2 joerg
3259 1.1.1.2 joerg MSGuidDecl *MSGuidDecl::Create(const ASTContext &C, QualType T, Parts P) {
3260 1.1.1.2 joerg DeclContext *DC = C.getTranslationUnitDecl();
3261 1.1.1.2 joerg return new (C, DC) MSGuidDecl(DC, T, P);
3262 1.1.1.2 joerg }
3263 1.1.1.2 joerg
3264 1.1.1.2 joerg MSGuidDecl *MSGuidDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3265 1.1.1.2 joerg return new (C, ID) MSGuidDecl(nullptr, QualType(), Parts());
3266 1.1.1.2 joerg }
3267 1.1.1.2 joerg
3268 1.1.1.2 joerg void MSGuidDecl::printName(llvm::raw_ostream &OS) const {
3269 1.1.1.2 joerg OS << llvm::format("GUID{%08" PRIx32 "-%04" PRIx16 "-%04" PRIx16 "-",
3270 1.1.1.2 joerg PartVal.Part1, PartVal.Part2, PartVal.Part3);
3271 1.1.1.2 joerg unsigned I = 0;
3272 1.1.1.2 joerg for (uint8_t Byte : PartVal.Part4And5) {
3273 1.1.1.2 joerg OS << llvm::format("%02" PRIx8, Byte);
3274 1.1.1.2 joerg if (++I == 2)
3275 1.1.1.2 joerg OS << '-';
3276 1.1.1.2 joerg }
3277 1.1.1.2 joerg OS << '}';
3278 1.1.1.2 joerg }
3279 1.1.1.2 joerg
3280 1.1.1.2 joerg /// Determine if T is a valid 'struct _GUID' of the shape that we expect.
3281 1.1.1.2 joerg static bool isValidStructGUID(ASTContext &Ctx, QualType T) {
3282 1.1.1.2 joerg // FIXME: We only need to check this once, not once each time we compute a
3283 1.1.1.2 joerg // GUID APValue.
3284 1.1.1.2 joerg using MatcherRef = llvm::function_ref<bool(QualType)>;
3285 1.1.1.2 joerg
3286 1.1.1.2 joerg auto IsInt = [&Ctx](unsigned N) {
3287 1.1.1.2 joerg return [&Ctx, N](QualType T) {
3288 1.1.1.2 joerg return T->isUnsignedIntegerOrEnumerationType() &&
3289 1.1.1.2 joerg Ctx.getIntWidth(T) == N;
3290 1.1.1.2 joerg };
3291 1.1.1.2 joerg };
3292 1.1.1.2 joerg
3293 1.1.1.2 joerg auto IsArray = [&Ctx](MatcherRef Elem, unsigned N) {
3294 1.1.1.2 joerg return [&Ctx, Elem, N](QualType T) {
3295 1.1.1.2 joerg const ConstantArrayType *CAT = Ctx.getAsConstantArrayType(T);
3296 1.1.1.2 joerg return CAT && CAT->getSize() == N && Elem(CAT->getElementType());
3297 1.1.1.2 joerg };
3298 1.1.1.2 joerg };
3299 1.1.1.2 joerg
3300 1.1.1.2 joerg auto IsStruct = [](std::initializer_list<MatcherRef> Fields) {
3301 1.1.1.2 joerg return [Fields](QualType T) {
3302 1.1.1.2 joerg const RecordDecl *RD = T->getAsRecordDecl();
3303 1.1.1.2 joerg if (!RD || RD->isUnion())
3304 1.1.1.2 joerg return false;
3305 1.1.1.2 joerg RD = RD->getDefinition();
3306 1.1.1.2 joerg if (!RD)
3307 1.1.1.2 joerg return false;
3308 1.1.1.2 joerg if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
3309 1.1.1.2 joerg if (CXXRD->getNumBases())
3310 1.1.1.2 joerg return false;
3311 1.1.1.2 joerg auto MatcherIt = Fields.begin();
3312 1.1.1.2 joerg for (const FieldDecl *FD : RD->fields()) {
3313 1.1.1.2 joerg if (FD->isUnnamedBitfield()) continue;
3314 1.1.1.2 joerg if (FD->isBitField() || MatcherIt == Fields.end() ||
3315 1.1.1.2 joerg !(*MatcherIt)(FD->getType()))
3316 1.1.1.2 joerg return false;
3317 1.1.1.2 joerg ++MatcherIt;
3318 1.1.1.2 joerg }
3319 1.1.1.2 joerg return MatcherIt == Fields.end();
3320 1.1.1.2 joerg };
3321 1.1.1.2 joerg };
3322 1.1.1.2 joerg
3323 1.1.1.2 joerg // We expect an {i32, i16, i16, [8 x i8]}.
3324 1.1.1.2 joerg return IsStruct({IsInt(32), IsInt(16), IsInt(16), IsArray(IsInt(8), 8)})(T);
3325 1.1.1.2 joerg }
3326 1.1.1.2 joerg
3327 1.1.1.2 joerg APValue &MSGuidDecl::getAsAPValue() const {
3328 1.1.1.2 joerg if (APVal.isAbsent() && isValidStructGUID(getASTContext(), getType())) {
3329 1.1.1.2 joerg using llvm::APInt;
3330 1.1.1.2 joerg using llvm::APSInt;
3331 1.1.1.2 joerg APVal = APValue(APValue::UninitStruct(), 0, 4);
3332 1.1.1.2 joerg APVal.getStructField(0) = APValue(APSInt(APInt(32, PartVal.Part1), true));
3333 1.1.1.2 joerg APVal.getStructField(1) = APValue(APSInt(APInt(16, PartVal.Part2), true));
3334 1.1.1.2 joerg APVal.getStructField(2) = APValue(APSInt(APInt(16, PartVal.Part3), true));
3335 1.1.1.2 joerg APValue &Arr = APVal.getStructField(3) =
3336 1.1.1.2 joerg APValue(APValue::UninitArray(), 8, 8);
3337 1.1.1.2 joerg for (unsigned I = 0; I != 8; ++I) {
3338 1.1.1.2 joerg Arr.getArrayInitializedElt(I) =
3339 1.1.1.2 joerg APValue(APSInt(APInt(8, PartVal.Part4And5[I]), true));
3340 1.1.1.2 joerg }
3341 1.1.1.2 joerg // Register this APValue to be destroyed if necessary. (Note that the
3342 1.1.1.2 joerg // MSGuidDecl destructor is never run.)
3343 1.1.1.2 joerg getASTContext().addDestruction(&APVal);
3344 1.1.1.2 joerg }
3345 1.1.1.2 joerg
3346 1.1.1.2 joerg return APVal;
3347 1.1.1.2 joerg }
3348 1.1.1.2 joerg
3349 1.1 joerg static const char *getAccessName(AccessSpecifier AS) {
3350 1.1 joerg switch (AS) {
3351 1.1 joerg case AS_none:
3352 1.1 joerg llvm_unreachable("Invalid access specifier!");
3353 1.1 joerg case AS_public:
3354 1.1 joerg return "public";
3355 1.1 joerg case AS_private:
3356 1.1 joerg return "private";
3357 1.1 joerg case AS_protected:
3358 1.1 joerg return "protected";
3359 1.1 joerg }
3360 1.1 joerg llvm_unreachable("Invalid access specifier!");
3361 1.1 joerg }
3362 1.1 joerg
3363 1.1.1.2 joerg const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
3364 1.1.1.2 joerg AccessSpecifier AS) {
3365 1.1 joerg return DB << getAccessName(AS);
3366 1.1 joerg }
3367