1 //===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the C++ related Decl classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/DeclCXX.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/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/TypeLoc.h" 22 #include "clang/Basic/IdentifierTable.h" 23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/ADT/SmallPtrSet.h" 25 using namespace clang; 26 27 //===----------------------------------------------------------------------===// 28 // Decl Allocation/Deallocation Method Implementations 29 //===----------------------------------------------------------------------===// 30 31 void AccessSpecDecl::anchor() { } 32 33 AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl)); 35 return new (Mem) 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), HasPublicFields(false), 54 HasMutableFields(false), HasOnlyCMembers(true), 55 HasInClassInitializer(false), HasUninitializedReferenceMember(false), 56 NeedOverloadResolutionForMoveConstructor(false), 57 NeedOverloadResolutionForMoveAssignment(false), 58 NeedOverloadResolutionForDestructor(false), 59 DefaultedMoveConstructorIsDeleted(false), 60 DefaultedMoveAssignmentIsDeleted(false), 61 DefaultedDestructorIsDeleted(false), 62 HasTrivialSpecialMembers(SMF_All), 63 DeclaredNonTrivialSpecialMembers(0), 64 HasIrrelevantDestructor(true), 65 HasConstexprNonCopyMoveConstructor(false), 66 DefaultedDefaultConstructorIsConstexpr(true), 67 HasConstexprDefaultConstructor(false), 68 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false), 69 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0), 70 ImplicitCopyConstructorHasConstParam(true), 71 ImplicitCopyAssignmentHasConstParam(true), 72 HasDeclaredCopyConstructorWithConstParam(false), 73 HasDeclaredCopyAssignmentWithConstParam(false), 74 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false), 75 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(), 76 Definition(D), FirstFriend() { 77 } 78 79 CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const { 80 return Bases.get(Definition->getASTContext().getExternalSource()); 81 } 82 83 CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const { 84 return VBases.get(Definition->getASTContext().getExternalSource()); 85 } 86 87 CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 88 SourceLocation StartLoc, SourceLocation IdLoc, 89 IdentifierInfo *Id, CXXRecordDecl *PrevDecl) 90 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl), 91 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0), 92 TemplateOrInstantiation() { } 93 94 CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK, 95 DeclContext *DC, SourceLocation StartLoc, 96 SourceLocation IdLoc, IdentifierInfo *Id, 97 CXXRecordDecl* PrevDecl, 98 bool DelayTypeCreation) { 99 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc, 100 Id, PrevDecl); 101 R->MayHaveOutOfDateDef = C.getLangOpts().Modules; 102 103 // FIXME: DelayTypeCreation seems like such a hack 104 if (!DelayTypeCreation) 105 C.getTypeDeclType(R, PrevDecl); 106 return R; 107 } 108 109 CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC, 110 TypeSourceInfo *Info, SourceLocation Loc, 111 bool Dependent) { 112 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc, 113 0, 0); 114 R->IsBeingDefined = true; 115 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent); 116 R->MayHaveOutOfDateDef = false; 117 R->setImplicit(true); 118 C.getTypeDeclType(R, /*PrevDecl=*/0); 119 return R; 120 } 121 122 CXXRecordDecl * 123 CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { 124 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl)); 125 CXXRecordDecl *R = new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, 126 SourceLocation(), SourceLocation(), 127 0, 0); 128 R->MayHaveOutOfDateDef = false; 129 return R; 130 } 131 132 void 133 CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, 134 unsigned NumBases) { 135 ASTContext &C = getASTContext(); 136 137 if (!data().Bases.isOffset() && data().NumBases > 0) 138 C.Deallocate(data().getBases()); 139 140 if (NumBases) { 141 // C++ [dcl.init.aggr]p1: 142 // An aggregate is [...] a class with [...] no base classes [...]. 143 data().Aggregate = false; 144 145 // C++ [class]p4: 146 // A POD-struct is an aggregate class... 147 data().PlainOldData = false; 148 } 149 150 // The set of seen virtual base types. 151 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes; 152 153 // The virtual bases of this class. 154 SmallVector<const CXXBaseSpecifier *, 8> VBases; 155 156 data().Bases = new(C) CXXBaseSpecifier [NumBases]; 157 data().NumBases = NumBases; 158 for (unsigned i = 0; i < NumBases; ++i) { 159 data().getBases()[i] = *Bases[i]; 160 // Keep track of inherited vbases for this base class. 161 const CXXBaseSpecifier *Base = Bases[i]; 162 QualType BaseType = Base->getType(); 163 // Skip dependent types; we can't do any checking on them now. 164 if (BaseType->isDependentType()) 165 continue; 166 CXXRecordDecl *BaseClassDecl 167 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); 168 169 // A class with a non-empty base class is not empty. 170 // FIXME: Standard ref? 171 if (!BaseClassDecl->isEmpty()) { 172 if (!data().Empty) { 173 // C++0x [class]p7: 174 // A standard-layout class is a class that: 175 // [...] 176 // -- either has no non-static data members in the most derived 177 // class and at most one base class with non-static data members, 178 // or has no base classes with non-static data members, and 179 // If this is the second non-empty base, then neither of these two 180 // clauses can be true. 181 data().IsStandardLayout = false; 182 } 183 184 data().Empty = false; 185 data().HasNoNonEmptyBases = false; 186 } 187 188 // C++ [class.virtual]p1: 189 // A class that declares or inherits a virtual function is called a 190 // polymorphic class. 191 if (BaseClassDecl->isPolymorphic()) 192 data().Polymorphic = true; 193 194 // C++0x [class]p7: 195 // A standard-layout class is a class that: [...] 196 // -- has no non-standard-layout base classes 197 if (!BaseClassDecl->isStandardLayout()) 198 data().IsStandardLayout = false; 199 200 // Record if this base is the first non-literal field or base. 201 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C)) 202 data().HasNonLiteralTypeFieldsOrBases = true; 203 204 // Now go through all virtual bases of this base and add them. 205 for (CXXRecordDecl::base_class_iterator VBase = 206 BaseClassDecl->vbases_begin(), 207 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { 208 // Add this base if it's not already in the list. 209 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) { 210 VBases.push_back(VBase); 211 212 // C++11 [class.copy]p8: 213 // The implicitly-declared copy constructor for a class X will have 214 // the form 'X::X(const X&)' if each [...] virtual base class B of X 215 // has a copy constructor whose first parameter is of type 216 // 'const B&' or 'const volatile B&' [...] 217 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl()) 218 if (!VBaseDecl->hasCopyConstructorWithConstParam()) 219 data().ImplicitCopyConstructorHasConstParam = false; 220 } 221 } 222 223 if (Base->isVirtual()) { 224 // Add this base if it's not already in the list. 225 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType))) 226 VBases.push_back(Base); 227 228 // C++0x [meta.unary.prop] is_empty: 229 // T is a class type, but not a union type, with ... no virtual base 230 // classes 231 data().Empty = false; 232 233 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: 234 // A [default constructor, copy/move constructor, or copy/move assignment 235 // operator for a class X] is trivial [...] if: 236 // -- class X has [...] no virtual base classes 237 data().HasTrivialSpecialMembers &= SMF_Destructor; 238 239 // C++0x [class]p7: 240 // A standard-layout class is a class that: [...] 241 // -- has [...] no virtual base classes 242 data().IsStandardLayout = false; 243 244 // C++11 [dcl.constexpr]p4: 245 // In the definition of a constexpr constructor [...] 246 // -- the class shall not have any virtual base classes 247 data().DefaultedDefaultConstructorIsConstexpr = false; 248 } else { 249 // C++ [class.ctor]p5: 250 // A default constructor is trivial [...] if: 251 // -- all the direct base classes of its class have trivial default 252 // constructors. 253 if (!BaseClassDecl->hasTrivialDefaultConstructor()) 254 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor; 255 256 // C++0x [class.copy]p13: 257 // A copy/move constructor for class X is trivial if [...] 258 // [...] 259 // -- the constructor selected to copy/move each direct base class 260 // subobject is trivial, and 261 if (!BaseClassDecl->hasTrivialCopyConstructor()) 262 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor; 263 // If the base class doesn't have a simple move constructor, we'll eagerly 264 // declare it and perform overload resolution to determine which function 265 // it actually calls. If it does have a simple move constructor, this 266 // check is correct. 267 if (!BaseClassDecl->hasTrivialMoveConstructor()) 268 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor; 269 270 // C++0x [class.copy]p27: 271 // A copy/move assignment operator for class X is trivial if [...] 272 // [...] 273 // -- the assignment operator selected to copy/move each direct base 274 // class subobject is trivial, and 275 if (!BaseClassDecl->hasTrivialCopyAssignment()) 276 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment; 277 // If the base class doesn't have a simple move assignment, we'll eagerly 278 // declare it and perform overload resolution to determine which function 279 // it actually calls. If it does have a simple move assignment, this 280 // check is correct. 281 if (!BaseClassDecl->hasTrivialMoveAssignment()) 282 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment; 283 284 // C++11 [class.ctor]p6: 285 // If that user-written default constructor would satisfy the 286 // requirements of a constexpr constructor, the implicitly-defined 287 // default constructor is constexpr. 288 if (!BaseClassDecl->hasConstexprDefaultConstructor()) 289 data().DefaultedDefaultConstructorIsConstexpr = false; 290 } 291 292 // C++ [class.ctor]p3: 293 // A destructor is trivial if all the direct base classes of its class 294 // have trivial destructors. 295 if (!BaseClassDecl->hasTrivialDestructor()) 296 data().HasTrivialSpecialMembers &= ~SMF_Destructor; 297 298 if (!BaseClassDecl->hasIrrelevantDestructor()) 299 data().HasIrrelevantDestructor = false; 300 301 // C++11 [class.copy]p18: 302 // The implicitly-declared copy assignment oeprator for a class X will 303 // have the form 'X& X::operator=(const X&)' if each direct base class B 304 // of X has a copy assignment operator whose parameter is of type 'const 305 // B&', 'const volatile B&', or 'B' [...] 306 if (!BaseClassDecl->hasCopyAssignmentWithConstParam()) 307 data().ImplicitCopyAssignmentHasConstParam = false; 308 309 // C++11 [class.copy]p8: 310 // The implicitly-declared copy constructor for a class X will have 311 // the form 'X::X(const X&)' if each direct [...] base class B of X 312 // has a copy constructor whose first parameter is of type 313 // 'const B&' or 'const volatile B&' [...] 314 if (!BaseClassDecl->hasCopyConstructorWithConstParam()) 315 data().ImplicitCopyConstructorHasConstParam = false; 316 317 // A class has an Objective-C object member if... or any of its bases 318 // has an Objective-C object member. 319 if (BaseClassDecl->hasObjectMember()) 320 setHasObjectMember(true); 321 322 if (BaseClassDecl->hasVolatileMember()) 323 setHasVolatileMember(true); 324 325 // Keep track of the presence of mutable fields. 326 if (BaseClassDecl->hasMutableFields()) 327 data().HasMutableFields = true; 328 329 if (BaseClassDecl->hasUninitializedReferenceMember()) 330 data().HasUninitializedReferenceMember = true; 331 332 addedClassSubobject(BaseClassDecl); 333 } 334 335 if (VBases.empty()) 336 return; 337 338 // Create base specifier for any direct or indirect virtual bases. 339 data().VBases = new (C) CXXBaseSpecifier[VBases.size()]; 340 data().NumVBases = VBases.size(); 341 for (int I = 0, E = VBases.size(); I != E; ++I) { 342 QualType Type = VBases[I]->getType(); 343 if (!Type->isDependentType()) 344 addedClassSubobject(Type->getAsCXXRecordDecl()); 345 data().getVBases()[I] = *VBases[I]; 346 } 347 } 348 349 void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) { 350 // C++11 [class.copy]p11: 351 // A defaulted copy/move constructor for a class X is defined as 352 // deleted if X has: 353 // -- a direct or virtual base class B that cannot be copied/moved [...] 354 // -- a non-static data member of class type M (or array thereof) 355 // that cannot be copied or moved [...] 356 if (!Subobj->hasSimpleMoveConstructor()) 357 data().NeedOverloadResolutionForMoveConstructor = true; 358 359 // C++11 [class.copy]p23: 360 // A defaulted copy/move assignment operator for a class X is defined as 361 // deleted if X has: 362 // -- a direct or virtual base class B that cannot be copied/moved [...] 363 // -- a non-static data member of class type M (or array thereof) 364 // that cannot be copied or moved [...] 365 if (!Subobj->hasSimpleMoveAssignment()) 366 data().NeedOverloadResolutionForMoveAssignment = true; 367 368 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5: 369 // A defaulted [ctor or dtor] for a class X is defined as 370 // deleted if X has: 371 // -- any direct or virtual base class [...] has a type with a destructor 372 // that is deleted or inaccessible from the defaulted [ctor or dtor]. 373 // -- any non-static data member has a type with a destructor 374 // that is deleted or inaccessible from the defaulted [ctor or dtor]. 375 if (!Subobj->hasSimpleDestructor()) { 376 data().NeedOverloadResolutionForMoveConstructor = true; 377 data().NeedOverloadResolutionForDestructor = true; 378 } 379 } 380 381 /// Callback function for CXXRecordDecl::forallBases that acknowledges 382 /// that it saw a base class. 383 static bool SawBase(const CXXRecordDecl *, void *) { 384 return true; 385 } 386 387 bool CXXRecordDecl::hasAnyDependentBases() const { 388 if (!isDependentContext()) 389 return false; 390 391 return !forallBases(SawBase, 0); 392 } 393 394 bool CXXRecordDecl::isTriviallyCopyable() const { 395 // C++0x [class]p5: 396 // A trivially copyable class is a class that: 397 // -- has no non-trivial copy constructors, 398 if (hasNonTrivialCopyConstructor()) return false; 399 // -- has no non-trivial move constructors, 400 if (hasNonTrivialMoveConstructor()) return false; 401 // -- has no non-trivial copy assignment operators, 402 if (hasNonTrivialCopyAssignment()) return false; 403 // -- has no non-trivial move assignment operators, and 404 if (hasNonTrivialMoveAssignment()) return false; 405 // -- has a trivial destructor. 406 if (!hasTrivialDestructor()) return false; 407 408 return true; 409 } 410 411 void CXXRecordDecl::markedVirtualFunctionPure() { 412 // C++ [class.abstract]p2: 413 // A class is abstract if it has at least one pure virtual function. 414 data().Abstract = true; 415 } 416 417 void CXXRecordDecl::addedMember(Decl *D) { 418 if (!D->isImplicit() && 419 !isa<FieldDecl>(D) && 420 !isa<IndirectFieldDecl>(D) && 421 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class || 422 cast<TagDecl>(D)->getTagKind() == TTK_Interface)) 423 data().HasOnlyCMembers = false; 424 425 // Ignore friends and invalid declarations. 426 if (D->getFriendObjectKind() || D->isInvalidDecl()) 427 return; 428 429 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); 430 if (FunTmpl) 431 D = FunTmpl->getTemplatedDecl(); 432 433 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 434 if (Method->isVirtual()) { 435 // C++ [dcl.init.aggr]p1: 436 // An aggregate is an array or a class with [...] no virtual functions. 437 data().Aggregate = false; 438 439 // C++ [class]p4: 440 // A POD-struct is an aggregate class... 441 data().PlainOldData = false; 442 443 // Virtual functions make the class non-empty. 444 // FIXME: Standard ref? 445 data().Empty = false; 446 447 // C++ [class.virtual]p1: 448 // A class that declares or inherits a virtual function is called a 449 // polymorphic class. 450 data().Polymorphic = true; 451 452 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: 453 // A [default constructor, copy/move constructor, or copy/move 454 // assignment operator for a class X] is trivial [...] if: 455 // -- class X has no virtual functions [...] 456 data().HasTrivialSpecialMembers &= SMF_Destructor; 457 458 // C++0x [class]p7: 459 // A standard-layout class is a class that: [...] 460 // -- has no virtual functions 461 data().IsStandardLayout = false; 462 } 463 } 464 465 // Notify the listener if an implicit member was added after the definition 466 // was completed. 467 if (!isBeingDefined() && D->isImplicit()) 468 if (ASTMutationListener *L = getASTMutationListener()) 469 L->AddedCXXImplicitMember(data().Definition, D); 470 471 // The kind of special member this declaration is, if any. 472 unsigned SMKind = 0; 473 474 // Handle constructors. 475 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { 476 if (!Constructor->isImplicit()) { 477 // Note that we have a user-declared constructor. 478 data().UserDeclaredConstructor = true; 479 480 // C++ [class]p4: 481 // A POD-struct is an aggregate class [...] 482 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the 483 // type is technically an aggregate in C++0x since it wouldn't be in 03. 484 data().PlainOldData = false; 485 } 486 487 // Technically, "user-provided" is only defined for special member 488 // functions, but the intent of the standard is clearly that it should apply 489 // to all functions. 490 bool UserProvided = Constructor->isUserProvided(); 491 492 if (Constructor->isDefaultConstructor()) { 493 SMKind |= SMF_DefaultConstructor; 494 495 if (UserProvided) 496 data().UserProvidedDefaultConstructor = true; 497 if (Constructor->isConstexpr()) 498 data().HasConstexprDefaultConstructor = true; 499 } 500 501 if (!FunTmpl) { 502 unsigned Quals; 503 if (Constructor->isCopyConstructor(Quals)) { 504 SMKind |= SMF_CopyConstructor; 505 506 if (Quals & Qualifiers::Const) 507 data().HasDeclaredCopyConstructorWithConstParam = true; 508 } else if (Constructor->isMoveConstructor()) 509 SMKind |= SMF_MoveConstructor; 510 } 511 512 // Record if we see any constexpr constructors which are neither copy 513 // nor move constructors. 514 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) 515 data().HasConstexprNonCopyMoveConstructor = true; 516 517 // C++ [dcl.init.aggr]p1: 518 // An aggregate is an array or a class with no user-declared 519 // constructors [...]. 520 // C++11 [dcl.init.aggr]p1: 521 // An aggregate is an array or a class with no user-provided 522 // constructors [...]. 523 if (getASTContext().getLangOpts().CPlusPlus11 524 ? UserProvided : !Constructor->isImplicit()) 525 data().Aggregate = false; 526 } 527 528 // Handle destructors. 529 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) { 530 SMKind |= SMF_Destructor; 531 532 if (!DD->isImplicit()) 533 data().HasIrrelevantDestructor = false; 534 535 // C++11 [class.dtor]p5: 536 // A destructor is trivial if [...] the destructor is not virtual. 537 if (DD->isVirtual()) 538 data().HasTrivialSpecialMembers &= ~SMF_Destructor; 539 } 540 541 // Handle member functions. 542 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 543 if (Method->isCopyAssignmentOperator()) { 544 SMKind |= SMF_CopyAssignment; 545 546 const ReferenceType *ParamTy = 547 Method->getParamDecl(0)->getType()->getAs<ReferenceType>(); 548 if (!ParamTy || ParamTy->getPointeeType().isConstQualified()) 549 data().HasDeclaredCopyAssignmentWithConstParam = true; 550 } 551 552 if (Method->isMoveAssignmentOperator()) 553 SMKind |= SMF_MoveAssignment; 554 555 // Keep the list of conversion functions up-to-date. 556 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { 557 // FIXME: We use the 'unsafe' accessor for the access specifier here, 558 // because Sema may not have set it yet. That's really just a misdesign 559 // in Sema. However, LLDB *will* have set the access specifier correctly, 560 // and adds declarations after the class is technically completed, 561 // so completeDefinition()'s overriding of the access specifiers doesn't 562 // work. 563 AccessSpecifier AS = Conversion->getAccessUnsafe(); 564 565 if (Conversion->getPrimaryTemplate()) { 566 // We don't record specializations. 567 } else { 568 ASTContext &Ctx = getASTContext(); 569 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx); 570 NamedDecl *Primary = 571 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion); 572 if (Primary->getPreviousDecl()) 573 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()), 574 Primary, AS); 575 else 576 Conversions.addDecl(Ctx, Primary, AS); 577 } 578 } 579 580 if (SMKind) { 581 // If this is the first declaration of a special member, we no longer have 582 // an implicit trivial special member. 583 data().HasTrivialSpecialMembers &= 584 data().DeclaredSpecialMembers | ~SMKind; 585 586 if (!Method->isImplicit() && !Method->isUserProvided()) { 587 // This method is user-declared but not user-provided. We can't work out 588 // whether it's trivial yet (not until we get to the end of the class). 589 // We'll handle this method in finishedDefaultedOrDeletedMember. 590 } else if (Method->isTrivial()) 591 data().HasTrivialSpecialMembers |= SMKind; 592 else 593 data().DeclaredNonTrivialSpecialMembers |= SMKind; 594 595 // Note when we have declared a declared special member, and suppress the 596 // implicit declaration of this special member. 597 data().DeclaredSpecialMembers |= SMKind; 598 599 if (!Method->isImplicit()) { 600 data().UserDeclaredSpecialMembers |= SMKind; 601 602 // C++03 [class]p4: 603 // A POD-struct is an aggregate class that has [...] no user-defined 604 // copy assignment operator and no user-defined destructor. 605 // 606 // Since the POD bit is meant to be C++03 POD-ness, and in C++03, 607 // aggregates could not have any constructors, clear it even for an 608 // explicitly defaulted or deleted constructor. 609 // type is technically an aggregate in C++0x since it wouldn't be in 03. 610 // 611 // Also, a user-declared move assignment operator makes a class non-POD. 612 // This is an extension in C++03. 613 data().PlainOldData = false; 614 } 615 } 616 617 return; 618 } 619 620 // Handle non-static data members. 621 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) { 622 // C++ [class.bit]p2: 623 // A declaration for a bit-field that omits the identifier declares an 624 // unnamed bit-field. Unnamed bit-fields are not members and cannot be 625 // initialized. 626 if (Field->isUnnamedBitfield()) 627 return; 628 629 // C++ [dcl.init.aggr]p1: 630 // An aggregate is an array or a class (clause 9) with [...] no 631 // private or protected non-static data members (clause 11). 632 // 633 // A POD must be an aggregate. 634 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) { 635 data().Aggregate = false; 636 data().PlainOldData = false; 637 } 638 639 // C++0x [class]p7: 640 // A standard-layout class is a class that: 641 // [...] 642 // -- has the same access control for all non-static data members, 643 switch (D->getAccess()) { 644 case AS_private: data().HasPrivateFields = true; break; 645 case AS_protected: data().HasProtectedFields = true; break; 646 case AS_public: data().HasPublicFields = true; break; 647 case AS_none: llvm_unreachable("Invalid access specifier"); 648 }; 649 if ((data().HasPrivateFields + data().HasProtectedFields + 650 data().HasPublicFields) > 1) 651 data().IsStandardLayout = false; 652 653 // Keep track of the presence of mutable fields. 654 if (Field->isMutable()) 655 data().HasMutableFields = true; 656 657 // C++0x [class]p9: 658 // A POD struct is a class that is both a trivial class and a 659 // standard-layout class, and has no non-static data members of type 660 // non-POD struct, non-POD union (or array of such types). 661 // 662 // Automatic Reference Counting: the presence of a member of Objective-C pointer type 663 // that does not explicitly have no lifetime makes the class a non-POD. 664 // However, we delay setting PlainOldData to false in this case so that 665 // Sema has a chance to diagnostic causes where the same class will be 666 // non-POD with Automatic Reference Counting but a POD without ARC. 667 // In this case, the class will become a non-POD class when we complete 668 // the definition. 669 ASTContext &Context = getASTContext(); 670 QualType T = Context.getBaseElementType(Field->getType()); 671 if (T->isObjCRetainableType() || T.isObjCGCStrong()) { 672 if (!Context.getLangOpts().ObjCAutoRefCount || 673 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) 674 setHasObjectMember(true); 675 } else if (!T.isCXX98PODType(Context)) 676 data().PlainOldData = false; 677 678 if (T->isReferenceType()) { 679 if (!Field->hasInClassInitializer()) 680 data().HasUninitializedReferenceMember = true; 681 682 // C++0x [class]p7: 683 // A standard-layout class is a class that: 684 // -- has no non-static data members of type [...] reference, 685 data().IsStandardLayout = false; 686 } 687 688 // Record if this field is the first non-literal or volatile field or base. 689 if (!T->isLiteralType(Context) || T.isVolatileQualified()) 690 data().HasNonLiteralTypeFieldsOrBases = true; 691 692 if (Field->hasInClassInitializer()) { 693 data().HasInClassInitializer = true; 694 695 // C++11 [class]p5: 696 // A default constructor is trivial if [...] no non-static data member 697 // of its class has a brace-or-equal-initializer. 698 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor; 699 700 // C++11 [dcl.init.aggr]p1: 701 // An aggregate is a [...] class with [...] no 702 // brace-or-equal-initializers for non-static data members. 703 // 704 // This rule was removed in C++1y. 705 if (!getASTContext().getLangOpts().CPlusPlus1y) 706 data().Aggregate = false; 707 708 // C++11 [class]p10: 709 // A POD struct is [...] a trivial class. 710 data().PlainOldData = false; 711 } 712 713 // C++11 [class.copy]p23: 714 // A defaulted copy/move assignment operator for a class X is defined 715 // as deleted if X has: 716 // -- a non-static data member of reference type 717 if (T->isReferenceType()) 718 data().DefaultedMoveAssignmentIsDeleted = true; 719 720 if (const RecordType *RecordTy = T->getAs<RecordType>()) { 721 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl()); 722 if (FieldRec->getDefinition()) { 723 addedClassSubobject(FieldRec); 724 725 // C++11 [class.ctor]p5, C++11 [class.copy]p11: 726 // A defaulted [special member] for a class X is defined as 727 // deleted if: 728 // -- X is a union-like class that has a variant member with a 729 // non-trivial [corresponding special member] 730 if (isUnion()) { 731 if (FieldRec->hasNonTrivialMoveConstructor()) 732 data().DefaultedMoveConstructorIsDeleted = true; 733 if (FieldRec->hasNonTrivialMoveAssignment()) 734 data().DefaultedMoveAssignmentIsDeleted = true; 735 if (FieldRec->hasNonTrivialDestructor()) 736 data().DefaultedDestructorIsDeleted = true; 737 } 738 739 // C++0x [class.ctor]p5: 740 // A default constructor is trivial [...] if: 741 // -- for all the non-static data members of its class that are of 742 // class type (or array thereof), each such class has a trivial 743 // default constructor. 744 if (!FieldRec->hasTrivialDefaultConstructor()) 745 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor; 746 747 // C++0x [class.copy]p13: 748 // A copy/move constructor for class X is trivial if [...] 749 // [...] 750 // -- for each non-static data member of X that is of class type (or 751 // an array thereof), the constructor selected to copy/move that 752 // member is trivial; 753 if (!FieldRec->hasTrivialCopyConstructor()) 754 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor; 755 // If the field doesn't have a simple move constructor, we'll eagerly 756 // declare the move constructor for this class and we'll decide whether 757 // it's trivial then. 758 if (!FieldRec->hasTrivialMoveConstructor()) 759 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor; 760 761 // C++0x [class.copy]p27: 762 // A copy/move assignment operator for class X is trivial if [...] 763 // [...] 764 // -- for each non-static data member of X that is of class type (or 765 // an array thereof), the assignment operator selected to 766 // copy/move that member is trivial; 767 if (!FieldRec->hasTrivialCopyAssignment()) 768 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment; 769 // If the field doesn't have a simple move assignment, we'll eagerly 770 // declare the move assignment for this class and we'll decide whether 771 // it's trivial then. 772 if (!FieldRec->hasTrivialMoveAssignment()) 773 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment; 774 775 if (!FieldRec->hasTrivialDestructor()) 776 data().HasTrivialSpecialMembers &= ~SMF_Destructor; 777 if (!FieldRec->hasIrrelevantDestructor()) 778 data().HasIrrelevantDestructor = false; 779 if (FieldRec->hasObjectMember()) 780 setHasObjectMember(true); 781 if (FieldRec->hasVolatileMember()) 782 setHasVolatileMember(true); 783 784 // C++0x [class]p7: 785 // A standard-layout class is a class that: 786 // -- has no non-static data members of type non-standard-layout 787 // class (or array of such types) [...] 788 if (!FieldRec->isStandardLayout()) 789 data().IsStandardLayout = false; 790 791 // C++0x [class]p7: 792 // A standard-layout class is a class that: 793 // [...] 794 // -- has no base classes of the same type as the first non-static 795 // data member. 796 // We don't want to expend bits in the state of the record decl 797 // tracking whether this is the first non-static data member so we 798 // cheat a bit and use some of the existing state: the empty bit. 799 // Virtual bases and virtual methods make a class non-empty, but they 800 // also make it non-standard-layout so we needn't check here. 801 // A non-empty base class may leave the class standard-layout, but not 802 // if we have arrived here, and have at least on non-static data 803 // member. If IsStandardLayout remains true, then the first non-static 804 // data member must come through here with Empty still true, and Empty 805 // will subsequently be set to false below. 806 if (data().IsStandardLayout && data().Empty) { 807 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(), 808 BE = bases_end(); 809 BI != BE; ++BI) { 810 if (Context.hasSameUnqualifiedType(BI->getType(), T)) { 811 data().IsStandardLayout = false; 812 break; 813 } 814 } 815 } 816 817 // Keep track of the presence of mutable fields. 818 if (FieldRec->hasMutableFields()) 819 data().HasMutableFields = true; 820 821 // C++11 [class.copy]p13: 822 // If the implicitly-defined constructor would satisfy the 823 // requirements of a constexpr constructor, the implicitly-defined 824 // constructor is constexpr. 825 // C++11 [dcl.constexpr]p4: 826 // -- every constructor involved in initializing non-static data 827 // members [...] shall be a constexpr constructor 828 if (!Field->hasInClassInitializer() && 829 !FieldRec->hasConstexprDefaultConstructor() && !isUnion()) 830 // The standard requires any in-class initializer to be a constant 831 // expression. We consider this to be a defect. 832 data().DefaultedDefaultConstructorIsConstexpr = false; 833 834 // C++11 [class.copy]p8: 835 // The implicitly-declared copy constructor for a class X will have 836 // the form 'X::X(const X&)' if [...] for all the non-static data 837 // members of X that are of a class type M (or array thereof), each 838 // such class type has a copy constructor whose first parameter is 839 // of type 'const M&' or 'const volatile M&'. 840 if (!FieldRec->hasCopyConstructorWithConstParam()) 841 data().ImplicitCopyConstructorHasConstParam = false; 842 843 // C++11 [class.copy]p18: 844 // The implicitly-declared copy assignment oeprator for a class X will 845 // have the form 'X& X::operator=(const X&)' if [...] for all the 846 // non-static data members of X that are of a class type M (or array 847 // thereof), each such class type has a copy assignment operator whose 848 // parameter is of type 'const M&', 'const volatile M&' or 'M'. 849 if (!FieldRec->hasCopyAssignmentWithConstParam()) 850 data().ImplicitCopyAssignmentHasConstParam = false; 851 852 if (FieldRec->hasUninitializedReferenceMember() && 853 !Field->hasInClassInitializer()) 854 data().HasUninitializedReferenceMember = true; 855 } 856 } else { 857 // Base element type of field is a non-class type. 858 if (!T->isLiteralType(Context) || 859 (!Field->hasInClassInitializer() && !isUnion())) 860 data().DefaultedDefaultConstructorIsConstexpr = false; 861 862 // C++11 [class.copy]p23: 863 // A defaulted copy/move assignment operator for a class X is defined 864 // as deleted if X has: 865 // -- a non-static data member of const non-class type (or array 866 // thereof) 867 if (T.isConstQualified()) 868 data().DefaultedMoveAssignmentIsDeleted = true; 869 } 870 871 // C++0x [class]p7: 872 // A standard-layout class is a class that: 873 // [...] 874 // -- either has no non-static data members in the most derived 875 // class and at most one base class with non-static data members, 876 // or has no base classes with non-static data members, and 877 // At this point we know that we have a non-static data member, so the last 878 // clause holds. 879 if (!data().HasNoNonEmptyBases) 880 data().IsStandardLayout = false; 881 882 // If this is not a zero-length bit-field, then the class is not empty. 883 if (data().Empty) { 884 if (!Field->isBitField() || 885 (!Field->getBitWidth()->isTypeDependent() && 886 !Field->getBitWidth()->isValueDependent() && 887 Field->getBitWidthValue(Context) != 0)) 888 data().Empty = false; 889 } 890 } 891 892 // Handle using declarations of conversion functions. 893 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) { 894 if (Shadow->getDeclName().getNameKind() 895 == DeclarationName::CXXConversionFunctionName) { 896 ASTContext &Ctx = getASTContext(); 897 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess()); 898 } 899 } 900 } 901 902 void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) { 903 assert(!D->isImplicit() && !D->isUserProvided()); 904 905 // The kind of special member this declaration is, if any. 906 unsigned SMKind = 0; 907 908 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { 909 if (Constructor->isDefaultConstructor()) { 910 SMKind |= SMF_DefaultConstructor; 911 if (Constructor->isConstexpr()) 912 data().HasConstexprDefaultConstructor = true; 913 } 914 if (Constructor->isCopyConstructor()) 915 SMKind |= SMF_CopyConstructor; 916 else if (Constructor->isMoveConstructor()) 917 SMKind |= SMF_MoveConstructor; 918 else if (Constructor->isConstexpr()) 919 // We may now know that the constructor is constexpr. 920 data().HasConstexprNonCopyMoveConstructor = true; 921 } else if (isa<CXXDestructorDecl>(D)) 922 SMKind |= SMF_Destructor; 923 else if (D->isCopyAssignmentOperator()) 924 SMKind |= SMF_CopyAssignment; 925 else if (D->isMoveAssignmentOperator()) 926 SMKind |= SMF_MoveAssignment; 927 928 // Update which trivial / non-trivial special members we have. 929 // addedMember will have skipped this step for this member. 930 if (D->isTrivial()) 931 data().HasTrivialSpecialMembers |= SMKind; 932 else 933 data().DeclaredNonTrivialSpecialMembers |= SMKind; 934 } 935 936 bool CXXRecordDecl::isCLike() const { 937 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface || 938 !TemplateOrInstantiation.isNull()) 939 return false; 940 if (!hasDefinition()) 941 return true; 942 943 return isPOD() && data().HasOnlyCMembers; 944 } 945 946 void CXXRecordDecl::getCaptureFields( 947 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures, 948 FieldDecl *&ThisCapture) const { 949 Captures.clear(); 950 ThisCapture = 0; 951 952 LambdaDefinitionData &Lambda = getLambdaData(); 953 RecordDecl::field_iterator Field = field_begin(); 954 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures; 955 C != CEnd; ++C, ++Field) { 956 if (C->capturesThis()) 957 ThisCapture = *Field; 958 else if (C->capturesVariable()) 959 Captures[C->getCapturedVar()] = *Field; 960 } 961 } 962 963 964 static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { 965 QualType T; 966 if (isa<UsingShadowDecl>(Conv)) 967 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl(); 968 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv)) 969 T = ConvTemp->getTemplatedDecl()->getResultType(); 970 else 971 T = cast<CXXConversionDecl>(Conv)->getConversionType(); 972 return Context.getCanonicalType(T); 973 } 974 975 /// Collect the visible conversions of a base class. 976 /// 977 /// \param Record a base class of the class we're considering 978 /// \param InVirtual whether this base class is a virtual base (or a base 979 /// of a virtual base) 980 /// \param Access the access along the inheritance path to this base 981 /// \param ParentHiddenTypes the conversions provided by the inheritors 982 /// of this base 983 /// \param Output the set to which to add conversions from non-virtual bases 984 /// \param VOutput the set to which to add conversions from virtual bases 985 /// \param HiddenVBaseCs the set of conversions which were hidden in a 986 /// virtual base along some inheritance path 987 static void CollectVisibleConversions(ASTContext &Context, 988 CXXRecordDecl *Record, 989 bool InVirtual, 990 AccessSpecifier Access, 991 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, 992 ASTUnresolvedSet &Output, 993 UnresolvedSetImpl &VOutput, 994 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { 995 // The set of types which have conversions in this class or its 996 // subclasses. As an optimization, we don't copy the derived set 997 // unless it might change. 998 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; 999 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; 1000 1001 // Collect the direct conversions and figure out which conversions 1002 // will be hidden in the subclasses. 1003 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin(); 1004 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end(); 1005 if (ConvI != ConvE) { 1006 HiddenTypesBuffer = ParentHiddenTypes; 1007 HiddenTypes = &HiddenTypesBuffer; 1008 1009 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) { 1010 CanQualType ConvType(GetConversionType(Context, I.getDecl())); 1011 bool Hidden = ParentHiddenTypes.count(ConvType); 1012 if (!Hidden) 1013 HiddenTypesBuffer.insert(ConvType); 1014 1015 // If this conversion is hidden and we're in a virtual base, 1016 // remember that it's hidden along some inheritance path. 1017 if (Hidden && InVirtual) 1018 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); 1019 1020 // If this conversion isn't hidden, add it to the appropriate output. 1021 else if (!Hidden) { 1022 AccessSpecifier IAccess 1023 = CXXRecordDecl::MergeAccess(Access, I.getAccess()); 1024 1025 if (InVirtual) 1026 VOutput.addDecl(I.getDecl(), IAccess); 1027 else 1028 Output.addDecl(Context, I.getDecl(), IAccess); 1029 } 1030 } 1031 } 1032 1033 // Collect information recursively from any base classes. 1034 for (CXXRecordDecl::base_class_iterator 1035 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { 1036 const RecordType *RT = I->getType()->getAs<RecordType>(); 1037 if (!RT) continue; 1038 1039 AccessSpecifier BaseAccess 1040 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier()); 1041 bool BaseInVirtual = InVirtual || I->isVirtual(); 1042 1043 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); 1044 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, 1045 *HiddenTypes, Output, VOutput, HiddenVBaseCs); 1046 } 1047 } 1048 1049 /// Collect the visible conversions of a class. 1050 /// 1051 /// This would be extremely straightforward if it weren't for virtual 1052 /// bases. It might be worth special-casing that, really. 1053 static void CollectVisibleConversions(ASTContext &Context, 1054 CXXRecordDecl *Record, 1055 ASTUnresolvedSet &Output) { 1056 // The collection of all conversions in virtual bases that we've 1057 // found. These will be added to the output as long as they don't 1058 // appear in the hidden-conversions set. 1059 UnresolvedSet<8> VBaseCs; 1060 1061 // The set of conversions in virtual bases that we've determined to 1062 // be hidden. 1063 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; 1064 1065 // The set of types hidden by classes derived from this one. 1066 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; 1067 1068 // Go ahead and collect the direct conversions and add them to the 1069 // hidden-types set. 1070 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin(); 1071 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end(); 1072 Output.append(Context, ConvI, ConvE); 1073 for (; ConvI != ConvE; ++ConvI) 1074 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl())); 1075 1076 // Recursively collect conversions from base classes. 1077 for (CXXRecordDecl::base_class_iterator 1078 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { 1079 const RecordType *RT = I->getType()->getAs<RecordType>(); 1080 if (!RT) continue; 1081 1082 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), 1083 I->isVirtual(), I->getAccessSpecifier(), 1084 HiddenTypes, Output, VBaseCs, HiddenVBaseCs); 1085 } 1086 1087 // Add any unhidden conversions provided by virtual bases. 1088 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); 1089 I != E; ++I) { 1090 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) 1091 Output.addDecl(Context, I.getDecl(), I.getAccess()); 1092 } 1093 } 1094 1095 /// getVisibleConversionFunctions - get all conversion functions visible 1096 /// in current class; including conversion function templates. 1097 std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator> 1098 CXXRecordDecl::getVisibleConversionFunctions() { 1099 ASTContext &Ctx = getASTContext(); 1100 1101 ASTUnresolvedSet *Set; 1102 if (bases_begin() == bases_end()) { 1103 // If root class, all conversions are visible. 1104 Set = &data().Conversions.get(Ctx); 1105 } else { 1106 Set = &data().VisibleConversions.get(Ctx); 1107 // If visible conversion list is not evaluated, evaluate it. 1108 if (!data().ComputedVisibleConversions) { 1109 CollectVisibleConversions(Ctx, this, *Set); 1110 data().ComputedVisibleConversions = true; 1111 } 1112 } 1113 return std::make_pair(Set->begin(), Set->end()); 1114 } 1115 1116 void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) { 1117 // This operation is O(N) but extremely rare. Sema only uses it to 1118 // remove UsingShadowDecls in a class that were followed by a direct 1119 // declaration, e.g.: 1120 // class A : B { 1121 // using B::operator int; 1122 // operator int(); 1123 // }; 1124 // This is uncommon by itself and even more uncommon in conjunction 1125 // with sufficiently large numbers of directly-declared conversions 1126 // that asymptotic behavior matters. 1127 1128 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext()); 1129 for (unsigned I = 0, E = Convs.size(); I != E; ++I) { 1130 if (Convs[I].getDecl() == ConvDecl) { 1131 Convs.erase(I); 1132 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end() 1133 && "conversion was found multiple times in unresolved set"); 1134 return; 1135 } 1136 } 1137 1138 llvm_unreachable("conversion not found in set!"); 1139 } 1140 1141 CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { 1142 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 1143 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); 1144 1145 return 0; 1146 } 1147 1148 void 1149 CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, 1150 TemplateSpecializationKind TSK) { 1151 assert(TemplateOrInstantiation.isNull() && 1152 "Previous template or instantiation?"); 1153 assert(!isa<ClassTemplateSpecializationDecl>(this)); 1154 TemplateOrInstantiation 1155 = new (getASTContext()) MemberSpecializationInfo(RD, TSK); 1156 } 1157 1158 TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ 1159 if (const ClassTemplateSpecializationDecl *Spec 1160 = dyn_cast<ClassTemplateSpecializationDecl>(this)) 1161 return Spec->getSpecializationKind(); 1162 1163 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 1164 return MSInfo->getTemplateSpecializationKind(); 1165 1166 return TSK_Undeclared; 1167 } 1168 1169 void 1170 CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { 1171 if (ClassTemplateSpecializationDecl *Spec 1172 = dyn_cast<ClassTemplateSpecializationDecl>(this)) { 1173 Spec->setSpecializationKind(TSK); 1174 return; 1175 } 1176 1177 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { 1178 MSInfo->setTemplateSpecializationKind(TSK); 1179 return; 1180 } 1181 1182 llvm_unreachable("Not a class template or member class specialization"); 1183 } 1184 1185 CXXDestructorDecl *CXXRecordDecl::getDestructor() const { 1186 ASTContext &Context = getASTContext(); 1187 QualType ClassType = Context.getTypeDeclType(this); 1188 1189 DeclarationName Name 1190 = Context.DeclarationNames.getCXXDestructorName( 1191 Context.getCanonicalType(ClassType)); 1192 1193 DeclContext::lookup_const_result R = lookup(Name); 1194 if (R.empty()) 1195 return 0; 1196 1197 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front()); 1198 return Dtor; 1199 } 1200 1201 void CXXRecordDecl::completeDefinition() { 1202 completeDefinition(0); 1203 } 1204 1205 void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) { 1206 RecordDecl::completeDefinition(); 1207 1208 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) { 1209 // Objective-C Automatic Reference Counting: 1210 // If a class has a non-static data member of Objective-C pointer 1211 // type (or array thereof), it is a non-POD type and its 1212 // default constructor (if any), copy constructor, move constructor, 1213 // copy assignment operator, move assignment operator, and destructor are 1214 // non-trivial. 1215 struct DefinitionData &Data = data(); 1216 Data.PlainOldData = false; 1217 Data.HasTrivialSpecialMembers = 0; 1218 Data.HasIrrelevantDestructor = false; 1219 } 1220 1221 // If the class may be abstract (but hasn't been marked as such), check for 1222 // any pure final overriders. 1223 if (mayBeAbstract()) { 1224 CXXFinalOverriderMap MyFinalOverriders; 1225 if (!FinalOverriders) { 1226 getFinalOverriders(MyFinalOverriders); 1227 FinalOverriders = &MyFinalOverriders; 1228 } 1229 1230 bool Done = false; 1231 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(), 1232 MEnd = FinalOverriders->end(); 1233 M != MEnd && !Done; ++M) { 1234 for (OverridingMethods::iterator SO = M->second.begin(), 1235 SOEnd = M->second.end(); 1236 SO != SOEnd && !Done; ++SO) { 1237 assert(SO->second.size() > 0 && 1238 "All virtual functions have overridding virtual functions"); 1239 1240 // C++ [class.abstract]p4: 1241 // A class is abstract if it contains or inherits at least one 1242 // pure virtual function for which the final overrider is pure 1243 // virtual. 1244 if (SO->second.front().Method->isPure()) { 1245 data().Abstract = true; 1246 Done = true; 1247 break; 1248 } 1249 } 1250 } 1251 } 1252 1253 // Set access bits correctly on the directly-declared conversions. 1254 for (conversion_iterator I = conversion_begin(), E = conversion_end(); 1255 I != E; ++I) 1256 I.setAccess((*I)->getAccess()); 1257 } 1258 1259 bool CXXRecordDecl::mayBeAbstract() const { 1260 if (data().Abstract || isInvalidDecl() || !data().Polymorphic || 1261 isDependentContext()) 1262 return false; 1263 1264 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(), 1265 BEnd = bases_end(); 1266 B != BEnd; ++B) { 1267 CXXRecordDecl *BaseDecl 1268 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); 1269 if (BaseDecl->isAbstract()) 1270 return true; 1271 } 1272 1273 return false; 1274 } 1275 1276 void CXXMethodDecl::anchor() { } 1277 1278 bool CXXMethodDecl::isStatic() const { 1279 const CXXMethodDecl *MD = getCanonicalDecl(); 1280 1281 if (MD->getStorageClass() == SC_Static) 1282 return true; 1283 1284 DeclarationName Name = getDeclName(); 1285 // [class.free]p1: 1286 // Any allocation function for a class T is a static member 1287 // (even if not explicitly declared static). 1288 if (Name.getCXXOverloadedOperator() == OO_New || 1289 Name.getCXXOverloadedOperator() == OO_Array_New) 1290 return true; 1291 1292 // [class.free]p6 Any deallocation function for a class X is a static member 1293 // (even if not explicitly declared static). 1294 if (Name.getCXXOverloadedOperator() == OO_Delete || 1295 Name.getCXXOverloadedOperator() == OO_Array_Delete) 1296 return true; 1297 1298 return false; 1299 } 1300 1301 static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD, 1302 const CXXMethodDecl *BaseMD) { 1303 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(), 1304 E = DerivedMD->end_overridden_methods(); I != E; ++I) { 1305 const CXXMethodDecl *MD = *I; 1306 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl()) 1307 return true; 1308 if (recursivelyOverrides(MD, BaseMD)) 1309 return true; 1310 } 1311 return false; 1312 } 1313 1314 CXXMethodDecl * 1315 CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD, 1316 bool MayBeBase) { 1317 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl()) 1318 return this; 1319 1320 // Lookup doesn't work for destructors, so handle them separately. 1321 if (isa<CXXDestructorDecl>(this)) { 1322 CXXMethodDecl *MD = RD->getDestructor(); 1323 if (MD) { 1324 if (recursivelyOverrides(MD, this)) 1325 return MD; 1326 if (MayBeBase && recursivelyOverrides(this, MD)) 1327 return MD; 1328 } 1329 return NULL; 1330 } 1331 1332 lookup_const_result Candidates = RD->lookup(getDeclName()); 1333 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) { 1334 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I); 1335 if (!MD) 1336 continue; 1337 if (recursivelyOverrides(MD, this)) 1338 return MD; 1339 if (MayBeBase && recursivelyOverrides(this, MD)) 1340 return MD; 1341 } 1342 1343 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 1344 E = RD->bases_end(); I != E; ++I) { 1345 const RecordType *RT = I->getType()->getAs<RecordType>(); 1346 if (!RT) 1347 continue; 1348 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); 1349 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base); 1350 if (T) 1351 return T; 1352 } 1353 1354 return NULL; 1355 } 1356 1357 CXXMethodDecl * 1358 CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1359 SourceLocation StartLoc, 1360 const DeclarationNameInfo &NameInfo, 1361 QualType T, TypeSourceInfo *TInfo, 1362 StorageClass SC, bool isInline, 1363 bool isConstexpr, SourceLocation EndLocation) { 1364 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo, 1365 SC, isInline, isConstexpr, 1366 EndLocation); 1367 } 1368 1369 CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1370 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl)); 1371 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(), 1372 DeclarationNameInfo(), QualType(), 1373 0, SC_None, false, false, 1374 SourceLocation()); 1375 } 1376 1377 bool CXXMethodDecl::isUsualDeallocationFunction() const { 1378 if (getOverloadedOperator() != OO_Delete && 1379 getOverloadedOperator() != OO_Array_Delete) 1380 return false; 1381 1382 // C++ [basic.stc.dynamic.deallocation]p2: 1383 // A template instance is never a usual deallocation function, 1384 // regardless of its signature. 1385 if (getPrimaryTemplate()) 1386 return false; 1387 1388 // C++ [basic.stc.dynamic.deallocation]p2: 1389 // If a class T has a member deallocation function named operator delete 1390 // with exactly one parameter, then that function is a usual (non-placement) 1391 // deallocation function. [...] 1392 if (getNumParams() == 1) 1393 return true; 1394 1395 // C++ [basic.stc.dynamic.deallocation]p2: 1396 // [...] If class T does not declare such an operator delete but does 1397 // declare a member deallocation function named operator delete with 1398 // exactly two parameters, the second of which has type std::size_t (18.1), 1399 // then this function is a usual deallocation function. 1400 ASTContext &Context = getASTContext(); 1401 if (getNumParams() != 2 || 1402 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(), 1403 Context.getSizeType())) 1404 return false; 1405 1406 // This function is a usual deallocation function if there are no 1407 // single-parameter deallocation functions of the same kind. 1408 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); 1409 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end(); 1410 I != E; ++I) { 1411 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) 1412 if (FD->getNumParams() == 1) 1413 return false; 1414 } 1415 1416 return true; 1417 } 1418 1419 bool CXXMethodDecl::isCopyAssignmentOperator() const { 1420 // C++0x [class.copy]p17: 1421 // A user-declared copy assignment operator X::operator= is a non-static 1422 // non-template member function of class X with exactly one parameter of 1423 // type X, X&, const X&, volatile X& or const volatile X&. 1424 if (/*operator=*/getOverloadedOperator() != OO_Equal || 1425 /*non-static*/ isStatic() || 1426 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() || 1427 getNumParams() != 1) 1428 return false; 1429 1430 QualType ParamType = getParamDecl(0)->getType(); 1431 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>()) 1432 ParamType = Ref->getPointeeType(); 1433 1434 ASTContext &Context = getASTContext(); 1435 QualType ClassType 1436 = Context.getCanonicalType(Context.getTypeDeclType(getParent())); 1437 return Context.hasSameUnqualifiedType(ClassType, ParamType); 1438 } 1439 1440 bool CXXMethodDecl::isMoveAssignmentOperator() const { 1441 // C++0x [class.copy]p19: 1442 // A user-declared move assignment operator X::operator= is a non-static 1443 // non-template member function of class X with exactly one parameter of type 1444 // X&&, const X&&, volatile X&&, or const volatile X&&. 1445 if (getOverloadedOperator() != OO_Equal || isStatic() || 1446 getPrimaryTemplate() || getDescribedFunctionTemplate() || 1447 getNumParams() != 1) 1448 return false; 1449 1450 QualType ParamType = getParamDecl(0)->getType(); 1451 if (!isa<RValueReferenceType>(ParamType)) 1452 return false; 1453 ParamType = ParamType->getPointeeType(); 1454 1455 ASTContext &Context = getASTContext(); 1456 QualType ClassType 1457 = Context.getCanonicalType(Context.getTypeDeclType(getParent())); 1458 return Context.hasSameUnqualifiedType(ClassType, ParamType); 1459 } 1460 1461 void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { 1462 assert(MD->isCanonicalDecl() && "Method is not canonical!"); 1463 assert(!MD->getParent()->isDependentContext() && 1464 "Can't add an overridden method to a class template!"); 1465 assert(MD->isVirtual() && "Method is not virtual!"); 1466 1467 getASTContext().addOverriddenMethod(this, MD); 1468 } 1469 1470 CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { 1471 if (isa<CXXConstructorDecl>(this)) return 0; 1472 return getASTContext().overridden_methods_begin(this); 1473 } 1474 1475 CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { 1476 if (isa<CXXConstructorDecl>(this)) return 0; 1477 return getASTContext().overridden_methods_end(this); 1478 } 1479 1480 unsigned CXXMethodDecl::size_overridden_methods() const { 1481 if (isa<CXXConstructorDecl>(this)) return 0; 1482 return getASTContext().overridden_methods_size(this); 1483 } 1484 1485 QualType CXXMethodDecl::getThisType(ASTContext &C) const { 1486 // C++ 9.3.2p1: The type of this in a member function of a class X is X*. 1487 // If the member function is declared const, the type of this is const X*, 1488 // if the member function is declared volatile, the type of this is 1489 // volatile X*, and if the member function is declared const volatile, 1490 // the type of this is const volatile X*. 1491 1492 assert(isInstance() && "No 'this' for static methods!"); 1493 1494 QualType ClassTy = C.getTypeDeclType(getParent()); 1495 ClassTy = C.getQualifiedType(ClassTy, 1496 Qualifiers::fromCVRMask(getTypeQualifiers())); 1497 return C.getPointerType(ClassTy); 1498 } 1499 1500 bool CXXMethodDecl::hasInlineBody() const { 1501 // If this function is a template instantiation, look at the template from 1502 // which it was instantiated. 1503 const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); 1504 if (!CheckFn) 1505 CheckFn = this; 1506 1507 const FunctionDecl *fn; 1508 return CheckFn->hasBody(fn) && !fn->isOutOfLine(); 1509 } 1510 1511 bool CXXMethodDecl::isLambdaStaticInvoker() const { 1512 return getParent()->isLambda() && 1513 getIdentifier() && getIdentifier()->getName() == "__invoke"; 1514 } 1515 1516 1517 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1518 TypeSourceInfo *TInfo, bool IsVirtual, 1519 SourceLocation L, Expr *Init, 1520 SourceLocation R, 1521 SourceLocation EllipsisLoc) 1522 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init), 1523 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual), 1524 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1525 { 1526 } 1527 1528 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1529 FieldDecl *Member, 1530 SourceLocation MemberLoc, 1531 SourceLocation L, Expr *Init, 1532 SourceLocation R) 1533 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 1534 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false), 1535 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1536 { 1537 } 1538 1539 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1540 IndirectFieldDecl *Member, 1541 SourceLocation MemberLoc, 1542 SourceLocation L, Expr *Init, 1543 SourceLocation R) 1544 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 1545 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false), 1546 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1547 { 1548 } 1549 1550 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1551 TypeSourceInfo *TInfo, 1552 SourceLocation L, Expr *Init, 1553 SourceLocation R) 1554 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init), 1555 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false), 1556 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1557 { 1558 } 1559 1560 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1561 FieldDecl *Member, 1562 SourceLocation MemberLoc, 1563 SourceLocation L, Expr *Init, 1564 SourceLocation R, 1565 VarDecl **Indices, 1566 unsigned NumIndices) 1567 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 1568 LParenLoc(L), RParenLoc(R), IsVirtual(false), 1569 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices) 1570 { 1571 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1); 1572 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *)); 1573 } 1574 1575 CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context, 1576 FieldDecl *Member, 1577 SourceLocation MemberLoc, 1578 SourceLocation L, Expr *Init, 1579 SourceLocation R, 1580 VarDecl **Indices, 1581 unsigned NumIndices) { 1582 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) + 1583 sizeof(VarDecl *) * NumIndices, 1584 llvm::alignOf<CXXCtorInitializer>()); 1585 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R, 1586 Indices, NumIndices); 1587 } 1588 1589 TypeLoc CXXCtorInitializer::getBaseClassLoc() const { 1590 if (isBaseInitializer()) 1591 return Initializee.get<TypeSourceInfo*>()->getTypeLoc(); 1592 else 1593 return TypeLoc(); 1594 } 1595 1596 const Type *CXXCtorInitializer::getBaseClass() const { 1597 if (isBaseInitializer()) 1598 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr(); 1599 else 1600 return 0; 1601 } 1602 1603 SourceLocation CXXCtorInitializer::getSourceLocation() const { 1604 if (isAnyMemberInitializer()) 1605 return getMemberLocation(); 1606 1607 if (isInClassMemberInitializer()) 1608 return getAnyMember()->getLocation(); 1609 1610 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>()) 1611 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin(); 1612 1613 return SourceLocation(); 1614 } 1615 1616 SourceRange CXXCtorInitializer::getSourceRange() const { 1617 if (isInClassMemberInitializer()) { 1618 FieldDecl *D = getAnyMember(); 1619 if (Expr *I = D->getInClassInitializer()) 1620 return I->getSourceRange(); 1621 return SourceRange(); 1622 } 1623 1624 return SourceRange(getSourceLocation(), getRParenLoc()); 1625 } 1626 1627 void CXXConstructorDecl::anchor() { } 1628 1629 CXXConstructorDecl * 1630 CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1631 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl)); 1632 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(), 1633 QualType(), 0, false, false, false,false); 1634 } 1635 1636 CXXConstructorDecl * 1637 CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1638 SourceLocation StartLoc, 1639 const DeclarationNameInfo &NameInfo, 1640 QualType T, TypeSourceInfo *TInfo, 1641 bool isExplicit, bool isInline, 1642 bool isImplicitlyDeclared, bool isConstexpr) { 1643 assert(NameInfo.getName().getNameKind() 1644 == DeclarationName::CXXConstructorName && 1645 "Name must refer to a constructor"); 1646 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo, 1647 isExplicit, isInline, isImplicitlyDeclared, 1648 isConstexpr); 1649 } 1650 1651 CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const { 1652 assert(isDelegatingConstructor() && "Not a delegating constructor!"); 1653 Expr *E = (*init_begin())->getInit()->IgnoreImplicit(); 1654 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E)) 1655 return Construct->getConstructor(); 1656 1657 return 0; 1658 } 1659 1660 bool CXXConstructorDecl::isDefaultConstructor() const { 1661 // C++ [class.ctor]p5: 1662 // A default constructor for a class X is a constructor of class 1663 // X that can be called without an argument. 1664 return (getNumParams() == 0) || 1665 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); 1666 } 1667 1668 bool 1669 CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { 1670 return isCopyOrMoveConstructor(TypeQuals) && 1671 getParamDecl(0)->getType()->isLValueReferenceType(); 1672 } 1673 1674 bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const { 1675 return isCopyOrMoveConstructor(TypeQuals) && 1676 getParamDecl(0)->getType()->isRValueReferenceType(); 1677 } 1678 1679 /// \brief Determine whether this is a copy or move constructor. 1680 bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const { 1681 // C++ [class.copy]p2: 1682 // A non-template constructor for class X is a copy constructor 1683 // if its first parameter is of type X&, const X&, volatile X& or 1684 // const volatile X&, and either there are no other parameters 1685 // or else all other parameters have default arguments (8.3.6). 1686 // C++0x [class.copy]p3: 1687 // A non-template constructor for class X is a move constructor if its 1688 // first parameter is of type X&&, const X&&, volatile X&&, or 1689 // const volatile X&&, and either there are no other parameters or else 1690 // all other parameters have default arguments. 1691 if ((getNumParams() < 1) || 1692 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 1693 (getPrimaryTemplate() != 0) || 1694 (getDescribedFunctionTemplate() != 0)) 1695 return false; 1696 1697 const ParmVarDecl *Param = getParamDecl(0); 1698 1699 // Do we have a reference type? 1700 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>(); 1701 if (!ParamRefType) 1702 return false; 1703 1704 // Is it a reference to our class type? 1705 ASTContext &Context = getASTContext(); 1706 1707 CanQualType PointeeType 1708 = Context.getCanonicalType(ParamRefType->getPointeeType()); 1709 CanQualType ClassTy 1710 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 1711 if (PointeeType.getUnqualifiedType() != ClassTy) 1712 return false; 1713 1714 // FIXME: other qualifiers? 1715 1716 // We have a copy or move constructor. 1717 TypeQuals = PointeeType.getCVRQualifiers(); 1718 return true; 1719 } 1720 1721 bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { 1722 // C++ [class.conv.ctor]p1: 1723 // A constructor declared without the function-specifier explicit 1724 // that can be called with a single parameter specifies a 1725 // conversion from the type of its first parameter to the type of 1726 // its class. Such a constructor is called a converting 1727 // constructor. 1728 if (isExplicit() && !AllowExplicit) 1729 return false; 1730 1731 return (getNumParams() == 0 && 1732 getType()->getAs<FunctionProtoType>()->isVariadic()) || 1733 (getNumParams() == 1) || 1734 (getNumParams() > 1 && 1735 (getParamDecl(1)->hasDefaultArg() || 1736 getParamDecl(1)->isParameterPack())); 1737 } 1738 1739 bool CXXConstructorDecl::isSpecializationCopyingObject() const { 1740 if ((getNumParams() < 1) || 1741 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 1742 (getPrimaryTemplate() == 0) || 1743 (getDescribedFunctionTemplate() != 0)) 1744 return false; 1745 1746 const ParmVarDecl *Param = getParamDecl(0); 1747 1748 ASTContext &Context = getASTContext(); 1749 CanQualType ParamType = Context.getCanonicalType(Param->getType()); 1750 1751 // Is it the same as our our class type? 1752 CanQualType ClassTy 1753 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 1754 if (ParamType.getUnqualifiedType() != ClassTy) 1755 return false; 1756 1757 return true; 1758 } 1759 1760 const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const { 1761 // Hack: we store the inherited constructor in the overridden method table 1762 method_iterator It = getASTContext().overridden_methods_begin(this); 1763 if (It == getASTContext().overridden_methods_end(this)) 1764 return 0; 1765 1766 return cast<CXXConstructorDecl>(*It); 1767 } 1768 1769 void 1770 CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){ 1771 // Hack: we store the inherited constructor in the overridden method table 1772 assert(getASTContext().overridden_methods_size(this) == 0 && 1773 "Base ctor already set."); 1774 getASTContext().addOverriddenMethod(this, BaseCtor); 1775 } 1776 1777 void CXXDestructorDecl::anchor() { } 1778 1779 CXXDestructorDecl * 1780 CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1781 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl)); 1782 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(), 1783 QualType(), 0, false, false); 1784 } 1785 1786 CXXDestructorDecl * 1787 CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1788 SourceLocation StartLoc, 1789 const DeclarationNameInfo &NameInfo, 1790 QualType T, TypeSourceInfo *TInfo, 1791 bool isInline, bool isImplicitlyDeclared) { 1792 assert(NameInfo.getName().getNameKind() 1793 == DeclarationName::CXXDestructorName && 1794 "Name must refer to a destructor"); 1795 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline, 1796 isImplicitlyDeclared); 1797 } 1798 1799 void CXXConversionDecl::anchor() { } 1800 1801 CXXConversionDecl * 1802 CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1803 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl)); 1804 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(), 1805 QualType(), 0, false, false, false, 1806 SourceLocation()); 1807 } 1808 1809 CXXConversionDecl * 1810 CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1811 SourceLocation StartLoc, 1812 const DeclarationNameInfo &NameInfo, 1813 QualType T, TypeSourceInfo *TInfo, 1814 bool isInline, bool isExplicit, 1815 bool isConstexpr, SourceLocation EndLocation) { 1816 assert(NameInfo.getName().getNameKind() 1817 == DeclarationName::CXXConversionFunctionName && 1818 "Name must refer to a conversion function"); 1819 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo, 1820 isInline, isExplicit, isConstexpr, 1821 EndLocation); 1822 } 1823 1824 bool CXXConversionDecl::isLambdaToBlockPointerConversion() const { 1825 return isImplicit() && getParent()->isLambda() && 1826 getConversionType()->isBlockPointerType(); 1827 } 1828 1829 void LinkageSpecDecl::anchor() { } 1830 1831 LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, 1832 DeclContext *DC, 1833 SourceLocation ExternLoc, 1834 SourceLocation LangLoc, 1835 LanguageIDs Lang, 1836 bool HasBraces) { 1837 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces); 1838 } 1839 1840 LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1841 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl)); 1842 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(), 1843 lang_c, false); 1844 } 1845 1846 void UsingDirectiveDecl::anchor() { } 1847 1848 UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, 1849 SourceLocation L, 1850 SourceLocation NamespaceLoc, 1851 NestedNameSpecifierLoc QualifierLoc, 1852 SourceLocation IdentLoc, 1853 NamedDecl *Used, 1854 DeclContext *CommonAncestor) { 1855 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) 1856 Used = NS->getOriginalNamespace(); 1857 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc, 1858 IdentLoc, Used, CommonAncestor); 1859 } 1860 1861 UsingDirectiveDecl * 1862 UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1863 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl)); 1864 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(), 1865 NestedNameSpecifierLoc(), 1866 SourceLocation(), 0, 0); 1867 } 1868 1869 NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { 1870 if (NamespaceAliasDecl *NA = 1871 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) 1872 return NA->getNamespace(); 1873 return cast_or_null<NamespaceDecl>(NominatedNamespace); 1874 } 1875 1876 void NamespaceDecl::anchor() { } 1877 1878 NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline, 1879 SourceLocation StartLoc, 1880 SourceLocation IdLoc, IdentifierInfo *Id, 1881 NamespaceDecl *PrevDecl) 1882 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace), 1883 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline) 1884 { 1885 setPreviousDeclaration(PrevDecl); 1886 1887 if (PrevDecl) 1888 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace()); 1889 } 1890 1891 NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, 1892 bool Inline, SourceLocation StartLoc, 1893 SourceLocation IdLoc, IdentifierInfo *Id, 1894 NamespaceDecl *PrevDecl) { 1895 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl); 1896 } 1897 1898 NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1899 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl)); 1900 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(), 1901 0, 0); 1902 } 1903 1904 void NamespaceAliasDecl::anchor() { } 1905 1906 NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, 1907 SourceLocation UsingLoc, 1908 SourceLocation AliasLoc, 1909 IdentifierInfo *Alias, 1910 NestedNameSpecifierLoc QualifierLoc, 1911 SourceLocation IdentLoc, 1912 NamedDecl *Namespace) { 1913 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) 1914 Namespace = NS->getOriginalNamespace(); 1915 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, 1916 QualifierLoc, IdentLoc, Namespace); 1917 } 1918 1919 NamespaceAliasDecl * 1920 NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1921 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl)); 1922 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0, 1923 NestedNameSpecifierLoc(), 1924 SourceLocation(), 0); 1925 } 1926 1927 void UsingShadowDecl::anchor() { } 1928 1929 UsingShadowDecl * 1930 UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1931 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl)); 1932 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0); 1933 } 1934 1935 UsingDecl *UsingShadowDecl::getUsingDecl() const { 1936 const UsingShadowDecl *Shadow = this; 1937 while (const UsingShadowDecl *NextShadow = 1938 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow)) 1939 Shadow = NextShadow; 1940 return cast<UsingDecl>(Shadow->UsingOrNextShadow); 1941 } 1942 1943 void UsingDecl::anchor() { } 1944 1945 void UsingDecl::addShadowDecl(UsingShadowDecl *S) { 1946 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() && 1947 "declaration already in set"); 1948 assert(S->getUsingDecl() == this); 1949 1950 if (FirstUsingShadow.getPointer()) 1951 S->UsingOrNextShadow = FirstUsingShadow.getPointer(); 1952 FirstUsingShadow.setPointer(S); 1953 } 1954 1955 void UsingDecl::removeShadowDecl(UsingShadowDecl *S) { 1956 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() && 1957 "declaration not in set"); 1958 assert(S->getUsingDecl() == this); 1959 1960 // Remove S from the shadow decl chain. This is O(n) but hopefully rare. 1961 1962 if (FirstUsingShadow.getPointer() == S) { 1963 FirstUsingShadow.setPointer( 1964 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow)); 1965 S->UsingOrNextShadow = this; 1966 return; 1967 } 1968 1969 UsingShadowDecl *Prev = FirstUsingShadow.getPointer(); 1970 while (Prev->UsingOrNextShadow != S) 1971 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow); 1972 Prev->UsingOrNextShadow = S->UsingOrNextShadow; 1973 S->UsingOrNextShadow = this; 1974 } 1975 1976 UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL, 1977 NestedNameSpecifierLoc QualifierLoc, 1978 const DeclarationNameInfo &NameInfo, 1979 bool HasTypename) { 1980 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename); 1981 } 1982 1983 UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 1984 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl)); 1985 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(), 1986 DeclarationNameInfo(), false); 1987 } 1988 1989 SourceRange UsingDecl::getSourceRange() const { 1990 SourceLocation Begin = isAccessDeclaration() 1991 ? getQualifierLoc().getBeginLoc() : UsingLocation; 1992 return SourceRange(Begin, getNameInfo().getEndLoc()); 1993 } 1994 1995 void UnresolvedUsingValueDecl::anchor() { } 1996 1997 UnresolvedUsingValueDecl * 1998 UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, 1999 SourceLocation UsingLoc, 2000 NestedNameSpecifierLoc QualifierLoc, 2001 const DeclarationNameInfo &NameInfo) { 2002 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, 2003 QualifierLoc, NameInfo); 2004 } 2005 2006 UnresolvedUsingValueDecl * 2007 UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 2008 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl)); 2009 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(), 2010 NestedNameSpecifierLoc(), 2011 DeclarationNameInfo()); 2012 } 2013 2014 SourceRange UnresolvedUsingValueDecl::getSourceRange() const { 2015 SourceLocation Begin = isAccessDeclaration() 2016 ? getQualifierLoc().getBeginLoc() : UsingLocation; 2017 return SourceRange(Begin, getNameInfo().getEndLoc()); 2018 } 2019 2020 void UnresolvedUsingTypenameDecl::anchor() { } 2021 2022 UnresolvedUsingTypenameDecl * 2023 UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, 2024 SourceLocation UsingLoc, 2025 SourceLocation TypenameLoc, 2026 NestedNameSpecifierLoc QualifierLoc, 2027 SourceLocation TargetNameLoc, 2028 DeclarationName TargetName) { 2029 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, 2030 QualifierLoc, TargetNameLoc, 2031 TargetName.getAsIdentifierInfo()); 2032 } 2033 2034 UnresolvedUsingTypenameDecl * 2035 UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) { 2036 void *Mem = AllocateDeserializedDecl(C, ID, 2037 sizeof(UnresolvedUsingTypenameDecl)); 2038 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(), 2039 SourceLocation(), 2040 NestedNameSpecifierLoc(), 2041 SourceLocation(), 2042 0); 2043 } 2044 2045 void StaticAssertDecl::anchor() { } 2046 2047 StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, 2048 SourceLocation StaticAssertLoc, 2049 Expr *AssertExpr, 2050 StringLiteral *Message, 2051 SourceLocation RParenLoc, 2052 bool Failed) { 2053 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message, 2054 RParenLoc, Failed); 2055 } 2056 2057 StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C, 2058 unsigned ID) { 2059 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl)); 2060 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0, 2061 SourceLocation(), false); 2062 } 2063 2064 static const char *getAccessName(AccessSpecifier AS) { 2065 switch (AS) { 2066 case AS_none: 2067 llvm_unreachable("Invalid access specifier!"); 2068 case AS_public: 2069 return "public"; 2070 case AS_private: 2071 return "private"; 2072 case AS_protected: 2073 return "protected"; 2074 } 2075 llvm_unreachable("Invalid access specifier!"); 2076 } 2077 2078 const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, 2079 AccessSpecifier AS) { 2080 return DB << getAccessName(AS); 2081 } 2082 2083 const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB, 2084 AccessSpecifier AS) { 2085 return DB << getAccessName(AS); 2086 } 2087