Coverage Report

Created: 2022-05-17 06:19

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