DeclCXX.cpp revision 226890
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/DeclTemplate.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/ASTMutationListener.h" 18#include "clang/AST/CXXInheritance.h" 19#include "clang/AST/Expr.h" 20#include "clang/AST/TypeLoc.h" 21#include "clang/Basic/IdentifierTable.h" 22#include "llvm/ADT/STLExtras.h" 23#include "llvm/ADT/SmallPtrSet.h" 24using namespace clang; 25 26//===----------------------------------------------------------------------===// 27// Decl Allocation/Deallocation Method Implementations 28//===----------------------------------------------------------------------===// 29 30CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D) 31 : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), 32 UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false), 33 UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false), 34 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false), 35 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true), 36 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false), 37 HasMutableFields(false), HasTrivialDefaultConstructor(true), 38 HasConstexprNonCopyMoveConstructor(false), HasTrivialCopyConstructor(true), 39 HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true), 40 HasTrivialMoveAssignment(true), HasTrivialDestructor(true), 41 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false), 42 UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false), 43 DeclaredCopyConstructor(false), DeclaredMoveConstructor(false), 44 DeclaredCopyAssignment(false), DeclaredMoveAssignment(false), 45 DeclaredDestructor(false), FailedImplicitMoveConstructor(false), 46 FailedImplicitMoveAssignment(false), NumBases(0), NumVBases(0), Bases(), 47 VBases(), Definition(D), FirstFriend(0) { 48} 49 50CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 51 SourceLocation StartLoc, SourceLocation IdLoc, 52 IdentifierInfo *Id, CXXRecordDecl *PrevDecl) 53 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl), 54 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0), 55 TemplateOrInstantiation() { } 56 57CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK, 58 DeclContext *DC, SourceLocation StartLoc, 59 SourceLocation IdLoc, IdentifierInfo *Id, 60 CXXRecordDecl* PrevDecl, 61 bool DelayTypeCreation) { 62 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc, 63 Id, PrevDecl); 64 65 // FIXME: DelayTypeCreation seems like such a hack 66 if (!DelayTypeCreation) 67 C.getTypeDeclType(R, PrevDecl); 68 return R; 69} 70 71CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) { 72 return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 73 SourceLocation(), 0, 0); 74} 75 76void 77CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, 78 unsigned NumBases) { 79 ASTContext &C = getASTContext(); 80 81 // C++ [dcl.init.aggr]p1: 82 // An aggregate is an array or a class (clause 9) with [...] 83 // no base classes [...]. 84 data().Aggregate = false; 85 86 if (!data().Bases.isOffset() && data().NumBases > 0) 87 C.Deallocate(data().getBases()); 88 89 // The set of seen virtual base types. 90 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes; 91 92 // The virtual bases of this class. 93 SmallVector<const CXXBaseSpecifier *, 8> VBases; 94 95 data().Bases = new(C) CXXBaseSpecifier [NumBases]; 96 data().NumBases = NumBases; 97 for (unsigned i = 0; i < NumBases; ++i) { 98 data().getBases()[i] = *Bases[i]; 99 // Keep track of inherited vbases for this base class. 100 const CXXBaseSpecifier *Base = Bases[i]; 101 QualType BaseType = Base->getType(); 102 // Skip dependent types; we can't do any checking on them now. 103 if (BaseType->isDependentType()) 104 continue; 105 CXXRecordDecl *BaseClassDecl 106 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); 107 108 // C++ [dcl.init.aggr]p1: 109 // An aggregate is [...] a class with [...] no base classes [...]. 110 data().Aggregate = false; 111 112 // C++ [class]p4: 113 // A POD-struct is an aggregate class... 114 data().PlainOldData = false; 115 116 // A class with a non-empty base class is not empty. 117 // FIXME: Standard ref? 118 if (!BaseClassDecl->isEmpty()) { 119 if (!data().Empty) { 120 // C++0x [class]p7: 121 // A standard-layout class is a class that: 122 // [...] 123 // -- either has no non-static data members in the most derived 124 // class and at most one base class with non-static data members, 125 // or has no base classes with non-static data members, and 126 // If this is the second non-empty base, then neither of these two 127 // clauses can be true. 128 data().IsStandardLayout = false; 129 } 130 131 data().Empty = false; 132 data().HasNoNonEmptyBases = false; 133 } 134 135 // C++ [class.virtual]p1: 136 // A class that declares or inherits a virtual function is called a 137 // polymorphic class. 138 if (BaseClassDecl->isPolymorphic()) 139 data().Polymorphic = true; 140 141 // C++0x [class]p7: 142 // A standard-layout class is a class that: [...] 143 // -- has no non-standard-layout base classes 144 if (!BaseClassDecl->isStandardLayout()) 145 data().IsStandardLayout = false; 146 147 // Record if this base is the first non-literal field or base. 148 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType()) 149 data().HasNonLiteralTypeFieldsOrBases = true; 150 151 // Now go through all virtual bases of this base and add them. 152 for (CXXRecordDecl::base_class_iterator VBase = 153 BaseClassDecl->vbases_begin(), 154 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { 155 // Add this base if it's not already in the list. 156 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) 157 VBases.push_back(VBase); 158 } 159 160 if (Base->isVirtual()) { 161 // Add this base if it's not already in the list. 162 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType))) 163 VBases.push_back(Base); 164 165 // C++0x [meta.unary.prop] is_empty: 166 // T is a class type, but not a union type, with ... no virtual base 167 // classes 168 data().Empty = false; 169 170 // C++ [class.ctor]p5: 171 // A default constructor is trivial [...] if: 172 // -- its class has [...] no virtual bases 173 data().HasTrivialDefaultConstructor = false; 174 175 // C++0x [class.copy]p13: 176 // A copy/move constructor for class X is trivial if it is neither 177 // user-provided nor deleted and if 178 // -- class X has no virtual functions and no virtual base classes, and 179 data().HasTrivialCopyConstructor = false; 180 data().HasTrivialMoveConstructor = false; 181 182 // C++0x [class.copy]p27: 183 // A copy/move assignment operator for class X is trivial if it is 184 // neither user-provided nor deleted and if 185 // -- class X has no virtual functions and no virtual base classes, and 186 data().HasTrivialCopyAssignment = false; 187 data().HasTrivialMoveAssignment = false; 188 189 // C++0x [class]p7: 190 // A standard-layout class is a class that: [...] 191 // -- has [...] no virtual base classes 192 data().IsStandardLayout = false; 193 } else { 194 // C++ [class.ctor]p5: 195 // A default constructor is trivial [...] if: 196 // -- all the direct base classes of its class have trivial default 197 // constructors. 198 if (!BaseClassDecl->hasTrivialDefaultConstructor()) 199 data().HasTrivialDefaultConstructor = false; 200 201 // C++0x [class.copy]p13: 202 // A copy/move constructor for class X is trivial if [...] 203 // [...] 204 // -- the constructor selected to copy/move each direct base class 205 // subobject is trivial, and 206 // FIXME: C++0x: We need to only consider the selected constructor 207 // instead of all of them. 208 if (!BaseClassDecl->hasTrivialCopyConstructor()) 209 data().HasTrivialCopyConstructor = false; 210 if (!BaseClassDecl->hasTrivialMoveConstructor()) 211 data().HasTrivialMoveConstructor = false; 212 213 // C++0x [class.copy]p27: 214 // A copy/move assignment operator for class X is trivial if [...] 215 // [...] 216 // -- the assignment operator selected to copy/move each direct base 217 // class subobject is trivial, and 218 // FIXME: C++0x: We need to only consider the selected operator instead 219 // of all of them. 220 if (!BaseClassDecl->hasTrivialCopyAssignment()) 221 data().HasTrivialCopyAssignment = false; 222 if (!BaseClassDecl->hasTrivialMoveAssignment()) 223 data().HasTrivialMoveAssignment = false; 224 } 225 226 // C++ [class.ctor]p3: 227 // A destructor is trivial if all the direct base classes of its class 228 // have trivial destructors. 229 if (!BaseClassDecl->hasTrivialDestructor()) 230 data().HasTrivialDestructor = false; 231 232 // A class has an Objective-C object member if... or any of its bases 233 // has an Objective-C object member. 234 if (BaseClassDecl->hasObjectMember()) 235 setHasObjectMember(true); 236 237 // Keep track of the presence of mutable fields. 238 if (BaseClassDecl->hasMutableFields()) 239 data().HasMutableFields = true; 240 } 241 242 if (VBases.empty()) 243 return; 244 245 // Create base specifier for any direct or indirect virtual bases. 246 data().VBases = new (C) CXXBaseSpecifier[VBases.size()]; 247 data().NumVBases = VBases.size(); 248 for (int I = 0, E = VBases.size(); I != E; ++I) 249 data().getVBases()[I] = *VBases[I]; 250} 251 252/// Callback function for CXXRecordDecl::forallBases that acknowledges 253/// that it saw a base class. 254static bool SawBase(const CXXRecordDecl *, void *) { 255 return true; 256} 257 258bool CXXRecordDecl::hasAnyDependentBases() const { 259 if (!isDependentContext()) 260 return false; 261 262 return !forallBases(SawBase, 0); 263} 264 265bool CXXRecordDecl::hasConstCopyConstructor() const { 266 return getCopyConstructor(Qualifiers::Const) != 0; 267} 268 269bool CXXRecordDecl::isTriviallyCopyable() const { 270 // C++0x [class]p5: 271 // A trivially copyable class is a class that: 272 // -- has no non-trivial copy constructors, 273 if (!hasTrivialCopyConstructor()) return false; 274 // -- has no non-trivial move constructors, 275 if (!hasTrivialMoveConstructor()) return false; 276 // -- has no non-trivial copy assignment operators, 277 if (!hasTrivialCopyAssignment()) return false; 278 // -- has no non-trivial move assignment operators, and 279 if (!hasTrivialMoveAssignment()) return false; 280 // -- has a trivial destructor. 281 if (!hasTrivialDestructor()) return false; 282 283 return true; 284} 285 286/// \brief Perform a simplistic form of overload resolution that only considers 287/// cv-qualifiers on a single parameter, and return the best overload candidate 288/// (if there is one). 289static CXXMethodDecl * 290GetBestOverloadCandidateSimple( 291 const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) { 292 if (Cands.empty()) 293 return 0; 294 if (Cands.size() == 1) 295 return Cands[0].first; 296 297 unsigned Best = 0, N = Cands.size(); 298 for (unsigned I = 1; I != N; ++I) 299 if (Cands[Best].second.compatiblyIncludes(Cands[I].second)) 300 Best = I; 301 302 for (unsigned I = 1; I != N; ++I) 303 if (Cands[Best].second.compatiblyIncludes(Cands[I].second)) 304 return 0; 305 306 return Cands[Best].first; 307} 308 309CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{ 310 ASTContext &Context = getASTContext(); 311 QualType ClassType 312 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); 313 DeclarationName ConstructorName 314 = Context.DeclarationNames.getCXXConstructorName( 315 Context.getCanonicalType(ClassType)); 316 unsigned FoundTQs; 317 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; 318 DeclContext::lookup_const_iterator Con, ConEnd; 319 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName); 320 Con != ConEnd; ++Con) { 321 // C++ [class.copy]p2: 322 // A non-template constructor for class X is a copy constructor if [...] 323 if (isa<FunctionTemplateDecl>(*Con)) 324 continue; 325 326 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); 327 if (Constructor->isCopyConstructor(FoundTQs)) { 328 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) || 329 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const))) 330 Found.push_back(std::make_pair( 331 const_cast<CXXConstructorDecl *>(Constructor), 332 Qualifiers::fromCVRMask(FoundTQs))); 333 } 334 } 335 336 return cast_or_null<CXXConstructorDecl>( 337 GetBestOverloadCandidateSimple(Found)); 338} 339 340CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const { 341 for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I) 342 if (I->isMoveConstructor()) 343 return *I; 344 345 return 0; 346} 347 348CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const { 349 ASTContext &Context = getASTContext(); 350 QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this)); 351 DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); 352 353 SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; 354 DeclContext::lookup_const_iterator Op, OpEnd; 355 for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) { 356 // C++ [class.copy]p9: 357 // A user-declared copy assignment operator is a non-static non-template 358 // member function of class X with exactly one parameter of type X, X&, 359 // const X&, volatile X& or const volatile X&. 360 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); 361 if (!Method || Method->isStatic() || Method->getPrimaryTemplate()) 362 continue; 363 364 const FunctionProtoType *FnType 365 = Method->getType()->getAs<FunctionProtoType>(); 366 assert(FnType && "Overloaded operator has no prototype."); 367 // Don't assert on this; an invalid decl might have been left in the AST. 368 if (FnType->getNumArgs() != 1 || FnType->isVariadic()) 369 continue; 370 371 QualType ArgType = FnType->getArgType(0); 372 Qualifiers Quals; 373 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) { 374 ArgType = Ref->getPointeeType(); 375 // If we have a const argument and we have a reference to a non-const, 376 // this function does not match. 377 if (ArgIsConst && !ArgType.isConstQualified()) 378 continue; 379 380 Quals = ArgType.getQualifiers(); 381 } else { 382 // By-value copy-assignment operators are treated like const X& 383 // copy-assignment operators. 384 Quals = Qualifiers::fromCVRMask(Qualifiers::Const); 385 } 386 387 if (!Context.hasSameUnqualifiedType(ArgType, Class)) 388 continue; 389 390 // Save this copy-assignment operator. It might be "the one". 391 Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals)); 392 } 393 394 // Use a simplistic form of overload resolution to find the candidate. 395 return GetBestOverloadCandidateSimple(Found); 396} 397 398CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const { 399 for (method_iterator I = method_begin(), E = method_end(); I != E; ++I) 400 if (I->isMoveAssignmentOperator()) 401 return *I; 402 403 return 0; 404} 405 406void CXXRecordDecl::markedVirtualFunctionPure() { 407 // C++ [class.abstract]p2: 408 // A class is abstract if it has at least one pure virtual function. 409 data().Abstract = true; 410} 411 412void CXXRecordDecl::addedMember(Decl *D) { 413 // Ignore friends and invalid declarations. 414 if (D->getFriendObjectKind() || D->isInvalidDecl()) 415 return; 416 417 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); 418 if (FunTmpl) 419 D = FunTmpl->getTemplatedDecl(); 420 421 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 422 if (Method->isVirtual()) { 423 // C++ [dcl.init.aggr]p1: 424 // An aggregate is an array or a class with [...] no virtual functions. 425 data().Aggregate = false; 426 427 // C++ [class]p4: 428 // A POD-struct is an aggregate class... 429 data().PlainOldData = false; 430 431 // Virtual functions make the class non-empty. 432 // FIXME: Standard ref? 433 data().Empty = false; 434 435 // C++ [class.virtual]p1: 436 // A class that declares or inherits a virtual function is called a 437 // polymorphic class. 438 data().Polymorphic = true; 439 440 // C++0x [class.ctor]p5 441 // A default constructor is trivial [...] if: 442 // -- its class has no virtual functions [...] 443 data().HasTrivialDefaultConstructor = false; 444 445 // C++0x [class.copy]p13: 446 // A copy/move constructor for class X is trivial if [...] 447 // -- class X has no virtual functions [...] 448 data().HasTrivialCopyConstructor = false; 449 data().HasTrivialMoveConstructor = false; 450 451 // C++0x [class.copy]p27: 452 // A copy/move assignment operator for class X is trivial if [...] 453 // -- class X has no virtual functions [...] 454 data().HasTrivialCopyAssignment = false; 455 data().HasTrivialMoveAssignment = false; 456 // FIXME: 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 if (D->isImplicit()) { 466 // Notify that an implicit member was added after the definition 467 // was completed. 468 if (!isBeingDefined()) 469 if (ASTMutationListener *L = getASTMutationListener()) 470 L->AddedCXXImplicitMember(data().Definition, D); 471 472 // If this is a special member function, note that it was added and then 473 // return early. 474 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { 475 if (Constructor->isDefaultConstructor()) 476 data().DeclaredDefaultConstructor = true; 477 else if (Constructor->isCopyConstructor()) 478 data().DeclaredCopyConstructor = true; 479 else if (Constructor->isMoveConstructor()) 480 data().DeclaredMoveConstructor = true; 481 else 482 goto NotASpecialMember; 483 return; 484 } else if (isa<CXXDestructorDecl>(D)) { 485 data().DeclaredDestructor = true; 486 return; 487 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 488 if (Method->isCopyAssignmentOperator()) 489 data().DeclaredCopyAssignment = true; 490 else if (Method->isMoveAssignmentOperator()) 491 data().DeclaredMoveAssignment = true; 492 else 493 goto NotASpecialMember; 494 return; 495 } 496 497NotASpecialMember:; 498 // Any other implicit declarations are handled like normal declarations. 499 } 500 501 // Handle (user-declared) constructors. 502 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { 503 // Note that we have a user-declared constructor. 504 data().UserDeclaredConstructor = true; 505 506 // Technically, "user-provided" is only defined for special member 507 // functions, but the intent of the standard is clearly that it should apply 508 // to all functions. 509 bool UserProvided = Constructor->isUserProvided(); 510 511 // C++0x [class.ctor]p5: 512 // A default constructor is trivial if it is not user-provided [...] 513 if (Constructor->isDefaultConstructor()) { 514 data().DeclaredDefaultConstructor = true; 515 if (UserProvided) { 516 data().HasTrivialDefaultConstructor = false; 517 data().UserProvidedDefaultConstructor = true; 518 } 519 } 520 521 // Note when we have a user-declared copy or move constructor, which will 522 // suppress the implicit declaration of those constructors. 523 if (!FunTmpl) { 524 if (Constructor->isCopyConstructor()) { 525 data().UserDeclaredCopyConstructor = true; 526 data().DeclaredCopyConstructor = true; 527 528 // C++0x [class.copy]p13: 529 // A copy/move constructor for class X is trivial if it is not 530 // user-provided [...] 531 if (UserProvided) 532 data().HasTrivialCopyConstructor = false; 533 } else if (Constructor->isMoveConstructor()) { 534 data().UserDeclaredMoveConstructor = true; 535 data().DeclaredMoveConstructor = true; 536 537 // C++0x [class.copy]p13: 538 // A copy/move constructor for class X is trivial if it is not 539 // user-provided [...] 540 if (UserProvided) 541 data().HasTrivialMoveConstructor = false; 542 } 543 } 544 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) { 545 // Record if we see any constexpr constructors which are neither copy 546 // nor move constructors. 547 data().HasConstexprNonCopyMoveConstructor = true; 548 } 549 550 // C++ [dcl.init.aggr]p1: 551 // An aggregate is an array or a class with no user-declared 552 // constructors [...]. 553 // C++0x [dcl.init.aggr]p1: 554 // An aggregate is an array or a class with no user-provided 555 // constructors [...]. 556 if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided) 557 data().Aggregate = false; 558 559 // C++ [class]p4: 560 // A POD-struct is an aggregate class [...] 561 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the 562 // type is technically an aggregate in C++0x since it wouldn't be in 03. 563 data().PlainOldData = false; 564 565 return; 566 } 567 568 // Handle (user-declared) destructors. 569 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) { 570 data().DeclaredDestructor = true; 571 data().UserDeclaredDestructor = true; 572 573 // C++ [class]p4: 574 // A POD-struct is an aggregate class that has [...] no user-defined 575 // destructor. 576 // This bit is the C++03 POD bit, not the 0x one. 577 data().PlainOldData = false; 578 579 // C++0x [class.dtor]p5: 580 // A destructor is trivial if it is not user-provided and [...] 581 if (DD->isUserProvided()) 582 data().HasTrivialDestructor = false; 583 584 return; 585 } 586 587 // Handle (user-declared) member functions. 588 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 589 if (Method->isCopyAssignmentOperator()) { 590 // C++ [class]p4: 591 // A POD-struct is an aggregate class that [...] has no user-defined 592 // copy assignment operator [...]. 593 // This is the C++03 bit only. 594 data().PlainOldData = false; 595 596 // This is a copy assignment operator. 597 598 // Suppress the implicit declaration of a copy constructor. 599 data().UserDeclaredCopyAssignment = true; 600 data().DeclaredCopyAssignment = true; 601 602 // C++0x [class.copy]p27: 603 // A copy/move assignment operator for class X is trivial if it is 604 // neither user-provided nor deleted [...] 605 if (Method->isUserProvided()) 606 data().HasTrivialCopyAssignment = false; 607 608 return; 609 } 610 611 if (Method->isMoveAssignmentOperator()) { 612 // This is an extension in C++03 mode, but we'll keep consistency by 613 // taking a move assignment operator to induce non-POD-ness 614 data().PlainOldData = false; 615 616 // This is a move assignment operator. 617 data().UserDeclaredMoveAssignment = true; 618 data().DeclaredMoveAssignment = true; 619 620 // C++0x [class.copy]p27: 621 // A copy/move assignment operator for class X is trivial if it is 622 // neither user-provided nor deleted [...] 623 if (Method->isUserProvided()) 624 data().HasTrivialMoveAssignment = false; 625 } 626 627 // Keep the list of conversion functions up-to-date. 628 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { 629 // We don't record specializations. 630 if (Conversion->getPrimaryTemplate()) 631 return; 632 633 // FIXME: We intentionally don't use the decl's access here because it 634 // hasn't been set yet. That's really just a misdesign in Sema. 635 636 if (FunTmpl) { 637 if (FunTmpl->getPreviousDeclaration()) 638 data().Conversions.replace(FunTmpl->getPreviousDeclaration(), 639 FunTmpl); 640 else 641 data().Conversions.addDecl(FunTmpl); 642 } else { 643 if (Conversion->getPreviousDeclaration()) 644 data().Conversions.replace(Conversion->getPreviousDeclaration(), 645 Conversion); 646 else 647 data().Conversions.addDecl(Conversion); 648 } 649 } 650 651 return; 652 } 653 654 // Handle non-static data members. 655 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) { 656 // C++ [class.bit]p2: 657 // A declaration for a bit-field that omits the identifier declares an 658 // unnamed bit-field. Unnamed bit-fields are not members and cannot be 659 // initialized. 660 if (Field->isUnnamedBitfield()) 661 return; 662 663 // C++ [dcl.init.aggr]p1: 664 // An aggregate is an array or a class (clause 9) with [...] no 665 // private or protected non-static data members (clause 11). 666 // 667 // A POD must be an aggregate. 668 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) { 669 data().Aggregate = false; 670 data().PlainOldData = false; 671 } 672 673 // C++0x [class]p7: 674 // A standard-layout class is a class that: 675 // [...] 676 // -- has the same access control for all non-static data members, 677 switch (D->getAccess()) { 678 case AS_private: data().HasPrivateFields = true; break; 679 case AS_protected: data().HasProtectedFields = true; break; 680 case AS_public: data().HasPublicFields = true; break; 681 case AS_none: llvm_unreachable("Invalid access specifier"); 682 }; 683 if ((data().HasPrivateFields + data().HasProtectedFields + 684 data().HasPublicFields) > 1) 685 data().IsStandardLayout = false; 686 687 // Keep track of the presence of mutable fields. 688 if (Field->isMutable()) 689 data().HasMutableFields = true; 690 691 // C++0x [class]p9: 692 // A POD struct is a class that is both a trivial class and a 693 // standard-layout class, and has no non-static data members of type 694 // non-POD struct, non-POD union (or array of such types). 695 // 696 // Automatic Reference Counting: the presence of a member of Objective-C pointer type 697 // that does not explicitly have no lifetime makes the class a non-POD. 698 // However, we delay setting PlainOldData to false in this case so that 699 // Sema has a chance to diagnostic causes where the same class will be 700 // non-POD with Automatic Reference Counting but a POD without Instant Objects. 701 // In this case, the class will become a non-POD class when we complete 702 // the definition. 703 ASTContext &Context = getASTContext(); 704 QualType T = Context.getBaseElementType(Field->getType()); 705 if (T->isObjCRetainableType() || T.isObjCGCStrong()) { 706 if (!Context.getLangOptions().ObjCAutoRefCount || 707 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) 708 setHasObjectMember(true); 709 } else if (!T.isPODType(Context)) 710 data().PlainOldData = false; 711 712 if (T->isReferenceType()) { 713 data().HasTrivialDefaultConstructor = false; 714 715 // C++0x [class]p7: 716 // A standard-layout class is a class that: 717 // -- has no non-static data members of type [...] reference, 718 data().IsStandardLayout = false; 719 } 720 721 // Record if this field is the first non-literal field or base. 722 // As a slight variation on the standard, we regard mutable members as being 723 // non-literal, since mutating a constexpr variable would break C++11 724 // constant expression semantics. 725 if ((!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) || 726 Field->isMutable()) 727 data().HasNonLiteralTypeFieldsOrBases = true; 728 729 if (Field->hasInClassInitializer()) { 730 // C++0x [class]p5: 731 // A default constructor is trivial if [...] no non-static data member 732 // of its class has a brace-or-equal-initializer. 733 data().HasTrivialDefaultConstructor = false; 734 735 // C++0x [dcl.init.aggr]p1: 736 // An aggregate is a [...] class with [...] no 737 // brace-or-equal-initializers for non-static data members. 738 data().Aggregate = false; 739 740 // C++0x [class]p10: 741 // A POD struct is [...] a trivial class. 742 data().PlainOldData = false; 743 } 744 745 if (const RecordType *RecordTy = T->getAs<RecordType>()) { 746 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl()); 747 if (FieldRec->getDefinition()) { 748 // C++0x [class.ctor]p5: 749 // A defulat constructor is trivial [...] if: 750 // -- for all the non-static data members of its class that are of 751 // class type (or array thereof), each such class has a trivial 752 // default constructor. 753 if (!FieldRec->hasTrivialDefaultConstructor()) 754 data().HasTrivialDefaultConstructor = false; 755 756 // C++0x [class.copy]p13: 757 // A copy/move constructor for class X is trivial if [...] 758 // [...] 759 // -- for each non-static data member of X that is of class type (or 760 // an array thereof), the constructor selected to copy/move that 761 // member is trivial; 762 // FIXME: C++0x: We don't correctly model 'selected' constructors. 763 if (!FieldRec->hasTrivialCopyConstructor()) 764 data().HasTrivialCopyConstructor = false; 765 if (!FieldRec->hasTrivialMoveConstructor()) 766 data().HasTrivialMoveConstructor = false; 767 768 // C++0x [class.copy]p27: 769 // A copy/move assignment operator for class X is trivial if [...] 770 // [...] 771 // -- for each non-static data member of X that is of class type (or 772 // an array thereof), the assignment operator selected to 773 // copy/move that member is trivial; 774 // FIXME: C++0x: We don't correctly model 'selected' operators. 775 if (!FieldRec->hasTrivialCopyAssignment()) 776 data().HasTrivialCopyAssignment = false; 777 if (!FieldRec->hasTrivialMoveAssignment()) 778 data().HasTrivialMoveAssignment = false; 779 780 if (!FieldRec->hasTrivialDestructor()) 781 data().HasTrivialDestructor = false; 782 if (FieldRec->hasObjectMember()) 783 setHasObjectMember(true); 784 785 // C++0x [class]p7: 786 // A standard-layout class is a class that: 787 // -- has no non-static data members of type non-standard-layout 788 // class (or array of such types) [...] 789 if (!FieldRec->isStandardLayout()) 790 data().IsStandardLayout = false; 791 792 // C++0x [class]p7: 793 // A standard-layout class is a class that: 794 // [...] 795 // -- has no base classes of the same type as the first non-static 796 // data member. 797 // We don't want to expend bits in the state of the record decl 798 // tracking whether this is the first non-static data member so we 799 // cheat a bit and use some of the existing state: the empty bit. 800 // Virtual bases and virtual methods make a class non-empty, but they 801 // also make it non-standard-layout so we needn't check here. 802 // A non-empty base class may leave the class standard-layout, but not 803 // if we have arrived here, and have at least on non-static data 804 // member. If IsStandardLayout remains true, then the first non-static 805 // data member must come through here with Empty still true, and Empty 806 // will subsequently be set to false below. 807 if (data().IsStandardLayout && data().Empty) { 808 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(), 809 BE = bases_end(); 810 BI != BE; ++BI) { 811 if (Context.hasSameUnqualifiedType(BI->getType(), T)) { 812 data().IsStandardLayout = false; 813 break; 814 } 815 } 816 } 817 818 // Keep track of the presence of mutable fields. 819 if (FieldRec->hasMutableFields()) 820 data().HasMutableFields = true; 821 } 822 } 823 824 // C++0x [class]p7: 825 // A standard-layout class is a class that: 826 // [...] 827 // -- either has no non-static data members in the most derived 828 // class and at most one base class with non-static data members, 829 // or has no base classes with non-static data members, and 830 // At this point we know that we have a non-static data member, so the last 831 // clause holds. 832 if (!data().HasNoNonEmptyBases) 833 data().IsStandardLayout = false; 834 835 // If this is not a zero-length bit-field, then the class is not empty. 836 if (data().Empty) { 837 if (!Field->isBitField() || 838 (!Field->getBitWidth()->isTypeDependent() && 839 !Field->getBitWidth()->isValueDependent() && 840 Field->getBitWidthValue(Context) != 0)) 841 data().Empty = false; 842 } 843 } 844 845 // Handle using declarations of conversion functions. 846 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) 847 if (Shadow->getDeclName().getNameKind() 848 == DeclarationName::CXXConversionFunctionName) 849 data().Conversions.addDecl(Shadow, Shadow->getAccess()); 850} 851 852static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { 853 QualType T; 854 if (isa<UsingShadowDecl>(Conv)) 855 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl(); 856 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv)) 857 T = ConvTemp->getTemplatedDecl()->getResultType(); 858 else 859 T = cast<CXXConversionDecl>(Conv)->getConversionType(); 860 return Context.getCanonicalType(T); 861} 862 863/// Collect the visible conversions of a base class. 864/// 865/// \param Base a base class of the class we're considering 866/// \param InVirtual whether this base class is a virtual base (or a base 867/// of a virtual base) 868/// \param Access the access along the inheritance path to this base 869/// \param ParentHiddenTypes the conversions provided by the inheritors 870/// of this base 871/// \param Output the set to which to add conversions from non-virtual bases 872/// \param VOutput the set to which to add conversions from virtual bases 873/// \param HiddenVBaseCs the set of conversions which were hidden in a 874/// virtual base along some inheritance path 875static void CollectVisibleConversions(ASTContext &Context, 876 CXXRecordDecl *Record, 877 bool InVirtual, 878 AccessSpecifier Access, 879 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, 880 UnresolvedSetImpl &Output, 881 UnresolvedSetImpl &VOutput, 882 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { 883 // The set of types which have conversions in this class or its 884 // subclasses. As an optimization, we don't copy the derived set 885 // unless it might change. 886 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; 887 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; 888 889 // Collect the direct conversions and figure out which conversions 890 // will be hidden in the subclasses. 891 UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); 892 if (!Cs.empty()) { 893 HiddenTypesBuffer = ParentHiddenTypes; 894 HiddenTypes = &HiddenTypesBuffer; 895 896 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) { 897 bool Hidden = 898 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl())); 899 900 // If this conversion is hidden and we're in a virtual base, 901 // remember that it's hidden along some inheritance path. 902 if (Hidden && InVirtual) 903 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); 904 905 // If this conversion isn't hidden, add it to the appropriate output. 906 else if (!Hidden) { 907 AccessSpecifier IAccess 908 = CXXRecordDecl::MergeAccess(Access, I.getAccess()); 909 910 if (InVirtual) 911 VOutput.addDecl(I.getDecl(), IAccess); 912 else 913 Output.addDecl(I.getDecl(), IAccess); 914 } 915 } 916 } 917 918 // Collect information recursively from any base classes. 919 for (CXXRecordDecl::base_class_iterator 920 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { 921 const RecordType *RT = I->getType()->getAs<RecordType>(); 922 if (!RT) continue; 923 924 AccessSpecifier BaseAccess 925 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier()); 926 bool BaseInVirtual = InVirtual || I->isVirtual(); 927 928 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); 929 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, 930 *HiddenTypes, Output, VOutput, HiddenVBaseCs); 931 } 932} 933 934/// Collect the visible conversions of a class. 935/// 936/// This would be extremely straightforward if it weren't for virtual 937/// bases. It might be worth special-casing that, really. 938static void CollectVisibleConversions(ASTContext &Context, 939 CXXRecordDecl *Record, 940 UnresolvedSetImpl &Output) { 941 // The collection of all conversions in virtual bases that we've 942 // found. These will be added to the output as long as they don't 943 // appear in the hidden-conversions set. 944 UnresolvedSet<8> VBaseCs; 945 946 // The set of conversions in virtual bases that we've determined to 947 // be hidden. 948 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; 949 950 // The set of types hidden by classes derived from this one. 951 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; 952 953 // Go ahead and collect the direct conversions and add them to the 954 // hidden-types set. 955 UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); 956 Output.append(Cs.begin(), Cs.end()); 957 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) 958 HiddenTypes.insert(GetConversionType(Context, I.getDecl())); 959 960 // Recursively collect conversions from base classes. 961 for (CXXRecordDecl::base_class_iterator 962 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { 963 const RecordType *RT = I->getType()->getAs<RecordType>(); 964 if (!RT) continue; 965 966 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), 967 I->isVirtual(), I->getAccessSpecifier(), 968 HiddenTypes, Output, VBaseCs, HiddenVBaseCs); 969 } 970 971 // Add any unhidden conversions provided by virtual bases. 972 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); 973 I != E; ++I) { 974 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) 975 Output.addDecl(I.getDecl(), I.getAccess()); 976 } 977} 978 979/// getVisibleConversionFunctions - get all conversion functions visible 980/// in current class; including conversion function templates. 981const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() { 982 // If root class, all conversions are visible. 983 if (bases_begin() == bases_end()) 984 return &data().Conversions; 985 // If visible conversion list is already evaluated, return it. 986 if (data().ComputedVisibleConversions) 987 return &data().VisibleConversions; 988 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions); 989 data().ComputedVisibleConversions = true; 990 return &data().VisibleConversions; 991} 992 993void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) { 994 // This operation is O(N) but extremely rare. Sema only uses it to 995 // remove UsingShadowDecls in a class that were followed by a direct 996 // declaration, e.g.: 997 // class A : B { 998 // using B::operator int; 999 // operator int(); 1000 // }; 1001 // This is uncommon by itself and even more uncommon in conjunction 1002 // with sufficiently large numbers of directly-declared conversions 1003 // that asymptotic behavior matters. 1004 1005 UnresolvedSetImpl &Convs = *getConversionFunctions(); 1006 for (unsigned I = 0, E = Convs.size(); I != E; ++I) { 1007 if (Convs[I].getDecl() == ConvDecl) { 1008 Convs.erase(I); 1009 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end() 1010 && "conversion was found multiple times in unresolved set"); 1011 return; 1012 } 1013 } 1014 1015 llvm_unreachable("conversion not found in set!"); 1016} 1017 1018CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { 1019 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 1020 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); 1021 1022 return 0; 1023} 1024 1025MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { 1026 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); 1027} 1028 1029void 1030CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, 1031 TemplateSpecializationKind TSK) { 1032 assert(TemplateOrInstantiation.isNull() && 1033 "Previous template or instantiation?"); 1034 assert(!isa<ClassTemplateSpecializationDecl>(this)); 1035 TemplateOrInstantiation 1036 = new (getASTContext()) MemberSpecializationInfo(RD, TSK); 1037} 1038 1039TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ 1040 if (const ClassTemplateSpecializationDecl *Spec 1041 = dyn_cast<ClassTemplateSpecializationDecl>(this)) 1042 return Spec->getSpecializationKind(); 1043 1044 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 1045 return MSInfo->getTemplateSpecializationKind(); 1046 1047 return TSK_Undeclared; 1048} 1049 1050void 1051CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { 1052 if (ClassTemplateSpecializationDecl *Spec 1053 = dyn_cast<ClassTemplateSpecializationDecl>(this)) { 1054 Spec->setSpecializationKind(TSK); 1055 return; 1056 } 1057 1058 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { 1059 MSInfo->setTemplateSpecializationKind(TSK); 1060 return; 1061 } 1062 1063 llvm_unreachable("Not a class template or member class specialization"); 1064} 1065 1066CXXDestructorDecl *CXXRecordDecl::getDestructor() const { 1067 ASTContext &Context = getASTContext(); 1068 QualType ClassType = Context.getTypeDeclType(this); 1069 1070 DeclarationName Name 1071 = Context.DeclarationNames.getCXXDestructorName( 1072 Context.getCanonicalType(ClassType)); 1073 1074 DeclContext::lookup_const_iterator I, E; 1075 llvm::tie(I, E) = lookup(Name); 1076 if (I == E) 1077 return 0; 1078 1079 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); 1080 return Dtor; 1081} 1082 1083void CXXRecordDecl::completeDefinition() { 1084 completeDefinition(0); 1085} 1086 1087void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) { 1088 RecordDecl::completeDefinition(); 1089 1090 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) { 1091 // Objective-C Automatic Reference Counting: 1092 // If a class has a non-static data member of Objective-C pointer 1093 // type (or array thereof), it is a non-POD type and its 1094 // default constructor (if any), copy constructor, copy assignment 1095 // operator, and destructor are non-trivial. 1096 struct DefinitionData &Data = data(); 1097 Data.PlainOldData = false; 1098 Data.HasTrivialDefaultConstructor = false; 1099 Data.HasTrivialCopyConstructor = false; 1100 Data.HasTrivialCopyAssignment = false; 1101 Data.HasTrivialDestructor = false; 1102 } 1103 1104 // If the class may be abstract (but hasn't been marked as such), check for 1105 // any pure final overriders. 1106 if (mayBeAbstract()) { 1107 CXXFinalOverriderMap MyFinalOverriders; 1108 if (!FinalOverriders) { 1109 getFinalOverriders(MyFinalOverriders); 1110 FinalOverriders = &MyFinalOverriders; 1111 } 1112 1113 bool Done = false; 1114 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(), 1115 MEnd = FinalOverriders->end(); 1116 M != MEnd && !Done; ++M) { 1117 for (OverridingMethods::iterator SO = M->second.begin(), 1118 SOEnd = M->second.end(); 1119 SO != SOEnd && !Done; ++SO) { 1120 assert(SO->second.size() > 0 && 1121 "All virtual functions have overridding virtual functions"); 1122 1123 // C++ [class.abstract]p4: 1124 // A class is abstract if it contains or inherits at least one 1125 // pure virtual function for which the final overrider is pure 1126 // virtual. 1127 if (SO->second.front().Method->isPure()) { 1128 data().Abstract = true; 1129 Done = true; 1130 break; 1131 } 1132 } 1133 } 1134 } 1135 1136 // Set access bits correctly on the directly-declared conversions. 1137 for (UnresolvedSetIterator I = data().Conversions.begin(), 1138 E = data().Conversions.end(); 1139 I != E; ++I) 1140 data().Conversions.setAccess(I, (*I)->getAccess()); 1141} 1142 1143bool CXXRecordDecl::mayBeAbstract() const { 1144 if (data().Abstract || isInvalidDecl() || !data().Polymorphic || 1145 isDependentContext()) 1146 return false; 1147 1148 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(), 1149 BEnd = bases_end(); 1150 B != BEnd; ++B) { 1151 CXXRecordDecl *BaseDecl 1152 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); 1153 if (BaseDecl->isAbstract()) 1154 return true; 1155 } 1156 1157 return false; 1158} 1159 1160CXXMethodDecl * 1161CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1162 SourceLocation StartLoc, 1163 const DeclarationNameInfo &NameInfo, 1164 QualType T, TypeSourceInfo *TInfo, 1165 bool isStatic, StorageClass SCAsWritten, bool isInline, 1166 bool isConstexpr, SourceLocation EndLocation) { 1167 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo, 1168 isStatic, SCAsWritten, isInline, isConstexpr, 1169 EndLocation); 1170} 1171 1172bool CXXMethodDecl::isUsualDeallocationFunction() const { 1173 if (getOverloadedOperator() != OO_Delete && 1174 getOverloadedOperator() != OO_Array_Delete) 1175 return false; 1176 1177 // C++ [basic.stc.dynamic.deallocation]p2: 1178 // A template instance is never a usual deallocation function, 1179 // regardless of its signature. 1180 if (getPrimaryTemplate()) 1181 return false; 1182 1183 // C++ [basic.stc.dynamic.deallocation]p2: 1184 // If a class T has a member deallocation function named operator delete 1185 // with exactly one parameter, then that function is a usual (non-placement) 1186 // deallocation function. [...] 1187 if (getNumParams() == 1) 1188 return true; 1189 1190 // C++ [basic.stc.dynamic.deallocation]p2: 1191 // [...] If class T does not declare such an operator delete but does 1192 // declare a member deallocation function named operator delete with 1193 // exactly two parameters, the second of which has type std::size_t (18.1), 1194 // then this function is a usual deallocation function. 1195 ASTContext &Context = getASTContext(); 1196 if (getNumParams() != 2 || 1197 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(), 1198 Context.getSizeType())) 1199 return false; 1200 1201 // This function is a usual deallocation function if there are no 1202 // single-parameter deallocation functions of the same kind. 1203 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); 1204 R.first != R.second; ++R.first) { 1205 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) 1206 if (FD->getNumParams() == 1) 1207 return false; 1208 } 1209 1210 return true; 1211} 1212 1213bool CXXMethodDecl::isCopyAssignmentOperator() const { 1214 // C++0x [class.copy]p17: 1215 // A user-declared copy assignment operator X::operator= is a non-static 1216 // non-template member function of class X with exactly one parameter of 1217 // type X, X&, const X&, volatile X& or const volatile X&. 1218 if (/*operator=*/getOverloadedOperator() != OO_Equal || 1219 /*non-static*/ isStatic() || 1220 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate()) 1221 return false; 1222 1223 QualType ParamType = getParamDecl(0)->getType(); 1224 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>()) 1225 ParamType = Ref->getPointeeType(); 1226 1227 ASTContext &Context = getASTContext(); 1228 QualType ClassType 1229 = Context.getCanonicalType(Context.getTypeDeclType(getParent())); 1230 return Context.hasSameUnqualifiedType(ClassType, ParamType); 1231} 1232 1233bool CXXMethodDecl::isMoveAssignmentOperator() const { 1234 // C++0x [class.copy]p19: 1235 // A user-declared move assignment operator X::operator= is a non-static 1236 // non-template member function of class X with exactly one parameter of type 1237 // X&&, const X&&, volatile X&&, or const volatile X&&. 1238 if (getOverloadedOperator() != OO_Equal || isStatic() || 1239 getPrimaryTemplate() || getDescribedFunctionTemplate()) 1240 return false; 1241 1242 QualType ParamType = getParamDecl(0)->getType(); 1243 if (!isa<RValueReferenceType>(ParamType)) 1244 return false; 1245 ParamType = ParamType->getPointeeType(); 1246 1247 ASTContext &Context = getASTContext(); 1248 QualType ClassType 1249 = Context.getCanonicalType(Context.getTypeDeclType(getParent())); 1250 return Context.hasSameUnqualifiedType(ClassType, ParamType); 1251} 1252 1253void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { 1254 assert(MD->isCanonicalDecl() && "Method is not canonical!"); 1255 assert(!MD->getParent()->isDependentContext() && 1256 "Can't add an overridden method to a class template!"); 1257 1258 getASTContext().addOverriddenMethod(this, MD); 1259} 1260 1261CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { 1262 return getASTContext().overridden_methods_begin(this); 1263} 1264 1265CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { 1266 return getASTContext().overridden_methods_end(this); 1267} 1268 1269unsigned CXXMethodDecl::size_overridden_methods() const { 1270 return getASTContext().overridden_methods_size(this); 1271} 1272 1273QualType CXXMethodDecl::getThisType(ASTContext &C) const { 1274 // C++ 9.3.2p1: The type of this in a member function of a class X is X*. 1275 // If the member function is declared const, the type of this is const X*, 1276 // if the member function is declared volatile, the type of this is 1277 // volatile X*, and if the member function is declared const volatile, 1278 // the type of this is const volatile X*. 1279 1280 assert(isInstance() && "No 'this' for static methods!"); 1281 1282 QualType ClassTy = C.getTypeDeclType(getParent()); 1283 ClassTy = C.getQualifiedType(ClassTy, 1284 Qualifiers::fromCVRMask(getTypeQualifiers())); 1285 return C.getPointerType(ClassTy); 1286} 1287 1288bool CXXMethodDecl::hasInlineBody() const { 1289 // If this function is a template instantiation, look at the template from 1290 // which it was instantiated. 1291 const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); 1292 if (!CheckFn) 1293 CheckFn = this; 1294 1295 const FunctionDecl *fn; 1296 return CheckFn->hasBody(fn) && !fn->isOutOfLine(); 1297} 1298 1299CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1300 TypeSourceInfo *TInfo, bool IsVirtual, 1301 SourceLocation L, Expr *Init, 1302 SourceLocation R, 1303 SourceLocation EllipsisLoc) 1304 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init), 1305 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false), 1306 SourceOrderOrNumArrayIndices(0) 1307{ 1308} 1309 1310CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1311 FieldDecl *Member, 1312 SourceLocation MemberLoc, 1313 SourceLocation L, Expr *Init, 1314 SourceLocation R) 1315 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 1316 LParenLoc(L), RParenLoc(R), IsVirtual(false), 1317 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1318{ 1319} 1320 1321CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1322 IndirectFieldDecl *Member, 1323 SourceLocation MemberLoc, 1324 SourceLocation L, Expr *Init, 1325 SourceLocation R) 1326 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 1327 LParenLoc(L), RParenLoc(R), IsVirtual(false), 1328 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1329{ 1330} 1331 1332CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1333 SourceLocation D, SourceLocation L, 1334 CXXConstructorDecl *Target, Expr *Init, 1335 SourceLocation R) 1336 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init), 1337 LParenLoc(L), RParenLoc(R), IsVirtual(false), 1338 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1339{ 1340} 1341 1342CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1343 FieldDecl *Member, 1344 SourceLocation MemberLoc, 1345 SourceLocation L, Expr *Init, 1346 SourceLocation R, 1347 VarDecl **Indices, 1348 unsigned NumIndices) 1349 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 1350 LParenLoc(L), RParenLoc(R), IsVirtual(false), 1351 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices) 1352{ 1353 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1); 1354 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *)); 1355} 1356 1357CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context, 1358 FieldDecl *Member, 1359 SourceLocation MemberLoc, 1360 SourceLocation L, Expr *Init, 1361 SourceLocation R, 1362 VarDecl **Indices, 1363 unsigned NumIndices) { 1364 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) + 1365 sizeof(VarDecl *) * NumIndices, 1366 llvm::alignOf<CXXCtorInitializer>()); 1367 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R, 1368 Indices, NumIndices); 1369} 1370 1371TypeLoc CXXCtorInitializer::getBaseClassLoc() const { 1372 if (isBaseInitializer()) 1373 return Initializee.get<TypeSourceInfo*>()->getTypeLoc(); 1374 else 1375 return TypeLoc(); 1376} 1377 1378const Type *CXXCtorInitializer::getBaseClass() const { 1379 if (isBaseInitializer()) 1380 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr(); 1381 else 1382 return 0; 1383} 1384 1385SourceLocation CXXCtorInitializer::getSourceLocation() const { 1386 if (isAnyMemberInitializer() || isDelegatingInitializer()) 1387 return getMemberLocation(); 1388 1389 if (isInClassMemberInitializer()) 1390 return getAnyMember()->getLocation(); 1391 1392 return getBaseClassLoc().getLocalSourceRange().getBegin(); 1393} 1394 1395SourceRange CXXCtorInitializer::getSourceRange() const { 1396 if (isInClassMemberInitializer()) { 1397 FieldDecl *D = getAnyMember(); 1398 if (Expr *I = D->getInClassInitializer()) 1399 return I->getSourceRange(); 1400 return SourceRange(); 1401 } 1402 1403 return SourceRange(getSourceLocation(), getRParenLoc()); 1404} 1405 1406CXXConstructorDecl * 1407CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) { 1408 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(), 1409 QualType(), 0, false, false, false, false); 1410} 1411 1412CXXConstructorDecl * 1413CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1414 SourceLocation StartLoc, 1415 const DeclarationNameInfo &NameInfo, 1416 QualType T, TypeSourceInfo *TInfo, 1417 bool isExplicit, bool isInline, 1418 bool isImplicitlyDeclared, bool isConstexpr) { 1419 assert(NameInfo.getName().getNameKind() 1420 == DeclarationName::CXXConstructorName && 1421 "Name must refer to a constructor"); 1422 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo, 1423 isExplicit, isInline, isImplicitlyDeclared, 1424 isConstexpr); 1425} 1426 1427bool CXXConstructorDecl::isDefaultConstructor() const { 1428 // C++ [class.ctor]p5: 1429 // A default constructor for a class X is a constructor of class 1430 // X that can be called without an argument. 1431 return (getNumParams() == 0) || 1432 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); 1433} 1434 1435bool 1436CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { 1437 return isCopyOrMoveConstructor(TypeQuals) && 1438 getParamDecl(0)->getType()->isLValueReferenceType(); 1439} 1440 1441bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const { 1442 return isCopyOrMoveConstructor(TypeQuals) && 1443 getParamDecl(0)->getType()->isRValueReferenceType(); 1444} 1445 1446/// \brief Determine whether this is a copy or move constructor. 1447bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const { 1448 // C++ [class.copy]p2: 1449 // A non-template constructor for class X is a copy constructor 1450 // if its first parameter is of type X&, const X&, volatile X& or 1451 // const volatile X&, and either there are no other parameters 1452 // or else all other parameters have default arguments (8.3.6). 1453 // C++0x [class.copy]p3: 1454 // A non-template constructor for class X is a move constructor if its 1455 // first parameter is of type X&&, const X&&, volatile X&&, or 1456 // const volatile X&&, and either there are no other parameters or else 1457 // all other parameters have default arguments. 1458 if ((getNumParams() < 1) || 1459 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 1460 (getPrimaryTemplate() != 0) || 1461 (getDescribedFunctionTemplate() != 0)) 1462 return false; 1463 1464 const ParmVarDecl *Param = getParamDecl(0); 1465 1466 // Do we have a reference type? 1467 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>(); 1468 if (!ParamRefType) 1469 return false; 1470 1471 // Is it a reference to our class type? 1472 ASTContext &Context = getASTContext(); 1473 1474 CanQualType PointeeType 1475 = Context.getCanonicalType(ParamRefType->getPointeeType()); 1476 CanQualType ClassTy 1477 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 1478 if (PointeeType.getUnqualifiedType() != ClassTy) 1479 return false; 1480 1481 // FIXME: other qualifiers? 1482 1483 // We have a copy or move constructor. 1484 TypeQuals = PointeeType.getCVRQualifiers(); 1485 return true; 1486} 1487 1488bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { 1489 // C++ [class.conv.ctor]p1: 1490 // A constructor declared without the function-specifier explicit 1491 // that can be called with a single parameter specifies a 1492 // conversion from the type of its first parameter to the type of 1493 // its class. Such a constructor is called a converting 1494 // constructor. 1495 if (isExplicit() && !AllowExplicit) 1496 return false; 1497 1498 return (getNumParams() == 0 && 1499 getType()->getAs<FunctionProtoType>()->isVariadic()) || 1500 (getNumParams() == 1) || 1501 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); 1502} 1503 1504bool CXXConstructorDecl::isSpecializationCopyingObject() const { 1505 if ((getNumParams() < 1) || 1506 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 1507 (getPrimaryTemplate() == 0) || 1508 (getDescribedFunctionTemplate() != 0)) 1509 return false; 1510 1511 const ParmVarDecl *Param = getParamDecl(0); 1512 1513 ASTContext &Context = getASTContext(); 1514 CanQualType ParamType = Context.getCanonicalType(Param->getType()); 1515 1516 // Is it the same as our our class type? 1517 CanQualType ClassTy 1518 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 1519 if (ParamType.getUnqualifiedType() != ClassTy) 1520 return false; 1521 1522 return true; 1523} 1524 1525const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const { 1526 // Hack: we store the inherited constructor in the overridden method table 1527 method_iterator It = begin_overridden_methods(); 1528 if (It == end_overridden_methods()) 1529 return 0; 1530 1531 return cast<CXXConstructorDecl>(*It); 1532} 1533 1534void 1535CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){ 1536 // Hack: we store the inherited constructor in the overridden method table 1537 assert(size_overridden_methods() == 0 && "Base ctor already set."); 1538 addOverriddenMethod(BaseCtor); 1539} 1540 1541CXXDestructorDecl * 1542CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) { 1543 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(), 1544 QualType(), 0, false, false); 1545} 1546 1547CXXDestructorDecl * 1548CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1549 SourceLocation StartLoc, 1550 const DeclarationNameInfo &NameInfo, 1551 QualType T, TypeSourceInfo *TInfo, 1552 bool isInline, bool isImplicitlyDeclared) { 1553 assert(NameInfo.getName().getNameKind() 1554 == DeclarationName::CXXDestructorName && 1555 "Name must refer to a destructor"); 1556 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline, 1557 isImplicitlyDeclared); 1558} 1559 1560CXXConversionDecl * 1561CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) { 1562 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(), 1563 QualType(), 0, false, false, false, 1564 SourceLocation()); 1565} 1566 1567CXXConversionDecl * 1568CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1569 SourceLocation StartLoc, 1570 const DeclarationNameInfo &NameInfo, 1571 QualType T, TypeSourceInfo *TInfo, 1572 bool isInline, bool isExplicit, 1573 bool isConstexpr, SourceLocation EndLocation) { 1574 assert(NameInfo.getName().getNameKind() 1575 == DeclarationName::CXXConversionFunctionName && 1576 "Name must refer to a conversion function"); 1577 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo, 1578 isInline, isExplicit, isConstexpr, 1579 EndLocation); 1580} 1581 1582LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, 1583 DeclContext *DC, 1584 SourceLocation ExternLoc, 1585 SourceLocation LangLoc, 1586 LanguageIDs Lang, 1587 SourceLocation RBraceLoc) { 1588 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc); 1589} 1590 1591UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, 1592 SourceLocation L, 1593 SourceLocation NamespaceLoc, 1594 NestedNameSpecifierLoc QualifierLoc, 1595 SourceLocation IdentLoc, 1596 NamedDecl *Used, 1597 DeclContext *CommonAncestor) { 1598 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) 1599 Used = NS->getOriginalNamespace(); 1600 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc, 1601 IdentLoc, Used, CommonAncestor); 1602} 1603 1604NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { 1605 if (NamespaceAliasDecl *NA = 1606 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) 1607 return NA->getNamespace(); 1608 return cast_or_null<NamespaceDecl>(NominatedNamespace); 1609} 1610 1611NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, 1612 SourceLocation UsingLoc, 1613 SourceLocation AliasLoc, 1614 IdentifierInfo *Alias, 1615 NestedNameSpecifierLoc QualifierLoc, 1616 SourceLocation IdentLoc, 1617 NamedDecl *Namespace) { 1618 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) 1619 Namespace = NS->getOriginalNamespace(); 1620 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, 1621 QualifierLoc, IdentLoc, Namespace); 1622} 1623 1624UsingDecl *UsingShadowDecl::getUsingDecl() const { 1625 const UsingShadowDecl *Shadow = this; 1626 while (const UsingShadowDecl *NextShadow = 1627 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow)) 1628 Shadow = NextShadow; 1629 return cast<UsingDecl>(Shadow->UsingOrNextShadow); 1630} 1631 1632void UsingDecl::addShadowDecl(UsingShadowDecl *S) { 1633 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() && 1634 "declaration already in set"); 1635 assert(S->getUsingDecl() == this); 1636 1637 if (FirstUsingShadow) 1638 S->UsingOrNextShadow = FirstUsingShadow; 1639 FirstUsingShadow = S; 1640} 1641 1642void UsingDecl::removeShadowDecl(UsingShadowDecl *S) { 1643 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() && 1644 "declaration not in set"); 1645 assert(S->getUsingDecl() == this); 1646 1647 // Remove S from the shadow decl chain. This is O(n) but hopefully rare. 1648 1649 if (FirstUsingShadow == S) { 1650 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow); 1651 S->UsingOrNextShadow = this; 1652 return; 1653 } 1654 1655 UsingShadowDecl *Prev = FirstUsingShadow; 1656 while (Prev->UsingOrNextShadow != S) 1657 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow); 1658 Prev->UsingOrNextShadow = S->UsingOrNextShadow; 1659 S->UsingOrNextShadow = this; 1660} 1661 1662UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL, 1663 NestedNameSpecifierLoc QualifierLoc, 1664 const DeclarationNameInfo &NameInfo, 1665 bool IsTypeNameArg) { 1666 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg); 1667} 1668 1669UnresolvedUsingValueDecl * 1670UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, 1671 SourceLocation UsingLoc, 1672 NestedNameSpecifierLoc QualifierLoc, 1673 const DeclarationNameInfo &NameInfo) { 1674 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, 1675 QualifierLoc, NameInfo); 1676} 1677 1678UnresolvedUsingTypenameDecl * 1679UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, 1680 SourceLocation UsingLoc, 1681 SourceLocation TypenameLoc, 1682 NestedNameSpecifierLoc QualifierLoc, 1683 SourceLocation TargetNameLoc, 1684 DeclarationName TargetName) { 1685 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, 1686 QualifierLoc, TargetNameLoc, 1687 TargetName.getAsIdentifierInfo()); 1688} 1689 1690StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, 1691 SourceLocation StaticAssertLoc, 1692 Expr *AssertExpr, 1693 StringLiteral *Message, 1694 SourceLocation RParenLoc) { 1695 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message, 1696 RParenLoc); 1697} 1698 1699static const char *getAccessName(AccessSpecifier AS) { 1700 switch (AS) { 1701 default: 1702 case AS_none: 1703 llvm_unreachable("Invalid access specifier!"); 1704 case AS_public: 1705 return "public"; 1706 case AS_private: 1707 return "private"; 1708 case AS_protected: 1709 return "protected"; 1710 } 1711} 1712 1713const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, 1714 AccessSpecifier AS) { 1715 return DB << getAccessName(AS); 1716} 1717