Coverage Report

Created: 2021-01-23 06:44

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