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