Bug Summary

File:clang/lib/AST/DeclCXX.cpp
Warning:line 3052, column 3
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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