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