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