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