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