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 // 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. 254 static bool SawBase(const CXXRecordDecl *, void *) { 255 return true; 256 } 257 258 bool CXXRecordDecl::hasAnyDependentBases() const { 259 if (!isDependentContext()) 260 return false; 261 262 return !forallBases(SawBase, 0); 263 } 264 265 bool CXXRecordDecl::hasConstCopyConstructor() const { 266 return getCopyConstructor(Qualifiers::Const) != 0; 267 } 268 269 bool 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). 289 static CXXMethodDecl * 290 GetBestOverloadCandidateSimple( 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 309 CXXConstructorDecl *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 340 CXXConstructorDecl *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 348 CXXMethodDecl *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 398 CXXMethodDecl *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 406 void 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 412 void 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 497 NotASpecialMember:; 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++ [dcl.init.aggr]p1: 657 // An aggregate is an array or a class (clause 9) with [...] no 658 // private or protected non-static data members (clause 11). 659 // 660 // A POD must be an aggregate. 661 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) { 662 data().Aggregate = false; 663 data().PlainOldData = false; 664 } 665 666 // C++0x [class]p7: 667 // A standard-layout class is a class that: 668 // [...] 669 // -- has the same access control for all non-static data members, 670 switch (D->getAccess()) { 671 case AS_private: data().HasPrivateFields = true; break; 672 case AS_protected: data().HasProtectedFields = true; break; 673 case AS_public: data().HasPublicFields = true; break; 674 case AS_none: llvm_unreachable("Invalid access specifier"); 675 }; 676 if ((data().HasPrivateFields + data().HasProtectedFields + 677 data().HasPublicFields) > 1) 678 data().IsStandardLayout = false; 679 680 // Keep track of the presence of mutable fields. 681 if (Field->isMutable()) 682 data().HasMutableFields = true; 683 684 // C++0x [class]p9: 685 // A POD struct is a class that is both a trivial class and a 686 // standard-layout class, and has no non-static data members of type 687 // non-POD struct, non-POD union (or array of such types). 688 // 689 // Automatic Reference Counting: the presence of a member of Objective-C pointer type 690 // that does not explicitly have no lifetime makes the class a non-POD. 691 // However, we delay setting PlainOldData to false in this case so that 692 // Sema has a chance to diagnostic causes where the same class will be 693 // non-POD with Automatic Reference Counting but a POD without Instant Objects. 694 // In this case, the class will become a non-POD class when we complete 695 // the definition. 696 ASTContext &Context = getASTContext(); 697 QualType T = Context.getBaseElementType(Field->getType()); 698 if (T->isObjCRetainableType() || T.isObjCGCStrong()) { 699 if (!Context.getLangOptions().ObjCAutoRefCount || 700 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) 701 setHasObjectMember(true); 702 } else if (!T.isPODType(Context)) 703 data().PlainOldData = false; 704 705 if (T->isReferenceType()) { 706 data().HasTrivialDefaultConstructor = false; 707 708 // C++0x [class]p7: 709 // A standard-layout class is a class that: 710 // -- has no non-static data members of type [...] reference, 711 data().IsStandardLayout = false; 712 } 713 714 // Record if this field is the first non-literal field or base. 715 if (!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) 716 data().HasNonLiteralTypeFieldsOrBases = true; 717 718 if (Field->hasInClassInitializer()) { 719 // C++0x [class]p5: 720 // A default constructor is trivial if [...] no non-static data member 721 // of its class has a brace-or-equal-initializer. 722 data().HasTrivialDefaultConstructor = false; 723 724 // C++0x [dcl.init.aggr]p1: 725 // An aggregate is a [...] class with [...] no 726 // brace-or-equal-initializers for non-static data members. 727 data().Aggregate = false; 728 729 // C++0x [class]p10: 730 // A POD struct is [...] a trivial class. 731 data().PlainOldData = false; 732 } 733 734 if (const RecordType *RecordTy = T->getAs<RecordType>()) { 735 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl()); 736 if (FieldRec->getDefinition()) { 737 // C++0x [class.ctor]p5: 738 // A defulat constructor is trivial [...] if: 739 // -- for all the non-static data members of its class that are of 740 // class type (or array thereof), each such class has a trivial 741 // default constructor. 742 if (!FieldRec->hasTrivialDefaultConstructor()) 743 data().HasTrivialDefaultConstructor = false; 744 745 // C++0x [class.copy]p13: 746 // A copy/move constructor for class X is trivial if [...] 747 // [...] 748 // -- for each non-static data member of X that is of class type (or 749 // an array thereof), the constructor selected to copy/move that 750 // member is trivial; 751 // FIXME: C++0x: We don't correctly model 'selected' constructors. 752 if (!FieldRec->hasTrivialCopyConstructor()) 753 data().HasTrivialCopyConstructor = false; 754 if (!FieldRec->hasTrivialMoveConstructor()) 755 data().HasTrivialMoveConstructor = false; 756 757 // C++0x [class.copy]p27: 758 // A copy/move assignment operator for class X is trivial if [...] 759 // [...] 760 // -- for each non-static data member of X that is of class type (or 761 // an array thereof), the assignment operator selected to 762 // copy/move that member is trivial; 763 // FIXME: C++0x: We don't correctly model 'selected' operators. 764 if (!FieldRec->hasTrivialCopyAssignment()) 765 data().HasTrivialCopyAssignment = false; 766 if (!FieldRec->hasTrivialMoveAssignment()) 767 data().HasTrivialMoveAssignment = false; 768 769 if (!FieldRec->hasTrivialDestructor()) 770 data().HasTrivialDestructor = false; 771 if (FieldRec->hasObjectMember()) 772 setHasObjectMember(true); 773 774 // C++0x [class]p7: 775 // A standard-layout class is a class that: 776 // -- has no non-static data members of type non-standard-layout 777 // class (or array of such types) [...] 778 if (!FieldRec->isStandardLayout()) 779 data().IsStandardLayout = false; 780 781 // C++0x [class]p7: 782 // A standard-layout class is a class that: 783 // [...] 784 // -- has no base classes of the same type as the first non-static 785 // data member. 786 // We don't want to expend bits in the state of the record decl 787 // tracking whether this is the first non-static data member so we 788 // cheat a bit and use some of the existing state: the empty bit. 789 // Virtual bases and virtual methods make a class non-empty, but they 790 // also make it non-standard-layout so we needn't check here. 791 // A non-empty base class may leave the class standard-layout, but not 792 // if we have arrived here, and have at least on non-static data 793 // member. If IsStandardLayout remains true, then the first non-static 794 // data member must come through here with Empty still true, and Empty 795 // will subsequently be set to false below. 796 if (data().IsStandardLayout && data().Empty) { 797 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(), 798 BE = bases_end(); 799 BI != BE; ++BI) { 800 if (Context.hasSameUnqualifiedType(BI->getType(), T)) { 801 data().IsStandardLayout = false; 802 break; 803 } 804 } 805 } 806 807 // Keep track of the presence of mutable fields. 808 if (FieldRec->hasMutableFields()) 809 data().HasMutableFields = true; 810 } 811 } 812 813 // C++0x [class]p7: 814 // A standard-layout class is a class that: 815 // [...] 816 // -- either has no non-static data members in the most derived 817 // class and at most one base class with non-static data members, 818 // or has no base classes with non-static data members, and 819 // At this point we know that we have a non-static data member, so the last 820 // clause holds. 821 if (!data().HasNoNonEmptyBases) 822 data().IsStandardLayout = false; 823 824 // If this is not a zero-length bit-field, then the class is not empty. 825 if (data().Empty) { 826 if (!Field->getBitWidth()) 827 data().Empty = false; 828 else if (!Field->getBitWidth()->isTypeDependent() && 829 !Field->getBitWidth()->isValueDependent()) { 830 llvm::APSInt Bits; 831 if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context)) 832 if (!!Bits) 833 data().Empty = false; 834 } 835 } 836 } 837 838 // Handle using declarations of conversion functions. 839 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) 840 if (Shadow->getDeclName().getNameKind() 841 == DeclarationName::CXXConversionFunctionName) 842 data().Conversions.addDecl(Shadow, Shadow->getAccess()); 843 } 844 845 static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { 846 QualType T; 847 if (isa<UsingShadowDecl>(Conv)) 848 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl(); 849 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv)) 850 T = ConvTemp->getTemplatedDecl()->getResultType(); 851 else 852 T = cast<CXXConversionDecl>(Conv)->getConversionType(); 853 return Context.getCanonicalType(T); 854 } 855 856 /// Collect the visible conversions of a base class. 857 /// 858 /// \param Base a base class of the class we're considering 859 /// \param InVirtual whether this base class is a virtual base (or a base 860 /// of a virtual base) 861 /// \param Access the access along the inheritance path to this base 862 /// \param ParentHiddenTypes the conversions provided by the inheritors 863 /// of this base 864 /// \param Output the set to which to add conversions from non-virtual bases 865 /// \param VOutput the set to which to add conversions from virtual bases 866 /// \param HiddenVBaseCs the set of conversions which were hidden in a 867 /// virtual base along some inheritance path 868 static void CollectVisibleConversions(ASTContext &Context, 869 CXXRecordDecl *Record, 870 bool InVirtual, 871 AccessSpecifier Access, 872 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, 873 UnresolvedSetImpl &Output, 874 UnresolvedSetImpl &VOutput, 875 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { 876 // The set of types which have conversions in this class or its 877 // subclasses. As an optimization, we don't copy the derived set 878 // unless it might change. 879 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; 880 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; 881 882 // Collect the direct conversions and figure out which conversions 883 // will be hidden in the subclasses. 884 UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); 885 if (!Cs.empty()) { 886 HiddenTypesBuffer = ParentHiddenTypes; 887 HiddenTypes = &HiddenTypesBuffer; 888 889 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) { 890 bool Hidden = 891 !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl())); 892 893 // If this conversion is hidden and we're in a virtual base, 894 // remember that it's hidden along some inheritance path. 895 if (Hidden && InVirtual) 896 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); 897 898 // If this conversion isn't hidden, add it to the appropriate output. 899 else if (!Hidden) { 900 AccessSpecifier IAccess 901 = CXXRecordDecl::MergeAccess(Access, I.getAccess()); 902 903 if (InVirtual) 904 VOutput.addDecl(I.getDecl(), IAccess); 905 else 906 Output.addDecl(I.getDecl(), IAccess); 907 } 908 } 909 } 910 911 // Collect information recursively from any base classes. 912 for (CXXRecordDecl::base_class_iterator 913 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { 914 const RecordType *RT = I->getType()->getAs<RecordType>(); 915 if (!RT) continue; 916 917 AccessSpecifier BaseAccess 918 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier()); 919 bool BaseInVirtual = InVirtual || I->isVirtual(); 920 921 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); 922 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, 923 *HiddenTypes, Output, VOutput, HiddenVBaseCs); 924 } 925 } 926 927 /// Collect the visible conversions of a class. 928 /// 929 /// This would be extremely straightforward if it weren't for virtual 930 /// bases. It might be worth special-casing that, really. 931 static void CollectVisibleConversions(ASTContext &Context, 932 CXXRecordDecl *Record, 933 UnresolvedSetImpl &Output) { 934 // The collection of all conversions in virtual bases that we've 935 // found. These will be added to the output as long as they don't 936 // appear in the hidden-conversions set. 937 UnresolvedSet<8> VBaseCs; 938 939 // The set of conversions in virtual bases that we've determined to 940 // be hidden. 941 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; 942 943 // The set of types hidden by classes derived from this one. 944 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; 945 946 // Go ahead and collect the direct conversions and add them to the 947 // hidden-types set. 948 UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); 949 Output.append(Cs.begin(), Cs.end()); 950 for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) 951 HiddenTypes.insert(GetConversionType(Context, I.getDecl())); 952 953 // Recursively collect conversions from base classes. 954 for (CXXRecordDecl::base_class_iterator 955 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { 956 const RecordType *RT = I->getType()->getAs<RecordType>(); 957 if (!RT) continue; 958 959 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), 960 I->isVirtual(), I->getAccessSpecifier(), 961 HiddenTypes, Output, VBaseCs, HiddenVBaseCs); 962 } 963 964 // Add any unhidden conversions provided by virtual bases. 965 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); 966 I != E; ++I) { 967 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) 968 Output.addDecl(I.getDecl(), I.getAccess()); 969 } 970 } 971 972 /// getVisibleConversionFunctions - get all conversion functions visible 973 /// in current class; including conversion function templates. 974 const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() { 975 // If root class, all conversions are visible. 976 if (bases_begin() == bases_end()) 977 return &data().Conversions; 978 // If visible conversion list is already evaluated, return it. 979 if (data().ComputedVisibleConversions) 980 return &data().VisibleConversions; 981 CollectVisibleConversions(getASTContext(), this, data().VisibleConversions); 982 data().ComputedVisibleConversions = true; 983 return &data().VisibleConversions; 984 } 985 986 void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) { 987 // This operation is O(N) but extremely rare. Sema only uses it to 988 // remove UsingShadowDecls in a class that were followed by a direct 989 // declaration, e.g.: 990 // class A : B { 991 // using B::operator int; 992 // operator int(); 993 // }; 994 // This is uncommon by itself and even more uncommon in conjunction 995 // with sufficiently large numbers of directly-declared conversions 996 // that asymptotic behavior matters. 997 998 UnresolvedSetImpl &Convs = *getConversionFunctions(); 999 for (unsigned I = 0, E = Convs.size(); I != E; ++I) { 1000 if (Convs[I].getDecl() == ConvDecl) { 1001 Convs.erase(I); 1002 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end() 1003 && "conversion was found multiple times in unresolved set"); 1004 return; 1005 } 1006 } 1007 1008 llvm_unreachable("conversion not found in set!"); 1009 } 1010 1011 CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { 1012 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 1013 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); 1014 1015 return 0; 1016 } 1017 1018 MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { 1019 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); 1020 } 1021 1022 void 1023 CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, 1024 TemplateSpecializationKind TSK) { 1025 assert(TemplateOrInstantiation.isNull() && 1026 "Previous template or instantiation?"); 1027 assert(!isa<ClassTemplateSpecializationDecl>(this)); 1028 TemplateOrInstantiation 1029 = new (getASTContext()) MemberSpecializationInfo(RD, TSK); 1030 } 1031 1032 TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ 1033 if (const ClassTemplateSpecializationDecl *Spec 1034 = dyn_cast<ClassTemplateSpecializationDecl>(this)) 1035 return Spec->getSpecializationKind(); 1036 1037 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 1038 return MSInfo->getTemplateSpecializationKind(); 1039 1040 return TSK_Undeclared; 1041 } 1042 1043 void 1044 CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { 1045 if (ClassTemplateSpecializationDecl *Spec 1046 = dyn_cast<ClassTemplateSpecializationDecl>(this)) { 1047 Spec->setSpecializationKind(TSK); 1048 return; 1049 } 1050 1051 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { 1052 MSInfo->setTemplateSpecializationKind(TSK); 1053 return; 1054 } 1055 1056 llvm_unreachable("Not a class template or member class specialization"); 1057 } 1058 1059 CXXDestructorDecl *CXXRecordDecl::getDestructor() const { 1060 ASTContext &Context = getASTContext(); 1061 QualType ClassType = Context.getTypeDeclType(this); 1062 1063 DeclarationName Name 1064 = Context.DeclarationNames.getCXXDestructorName( 1065 Context.getCanonicalType(ClassType)); 1066 1067 DeclContext::lookup_const_iterator I, E; 1068 llvm::tie(I, E) = lookup(Name); 1069 if (I == E) 1070 return 0; 1071 1072 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); 1073 return Dtor; 1074 } 1075 1076 void CXXRecordDecl::completeDefinition() { 1077 completeDefinition(0); 1078 } 1079 1080 void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) { 1081 RecordDecl::completeDefinition(); 1082 1083 if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) { 1084 // Objective-C Automatic Reference Counting: 1085 // If a class has a non-static data member of Objective-C pointer 1086 // type (or array thereof), it is a non-POD type and its 1087 // default constructor (if any), copy constructor, copy assignment 1088 // operator, and destructor are non-trivial. 1089 struct DefinitionData &Data = data(); 1090 Data.PlainOldData = false; 1091 Data.HasTrivialDefaultConstructor = false; 1092 Data.HasTrivialCopyConstructor = false; 1093 Data.HasTrivialCopyAssignment = false; 1094 Data.HasTrivialDestructor = false; 1095 } 1096 1097 // If the class may be abstract (but hasn't been marked as such), check for 1098 // any pure final overriders. 1099 if (mayBeAbstract()) { 1100 CXXFinalOverriderMap MyFinalOverriders; 1101 if (!FinalOverriders) { 1102 getFinalOverriders(MyFinalOverriders); 1103 FinalOverriders = &MyFinalOverriders; 1104 } 1105 1106 bool Done = false; 1107 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(), 1108 MEnd = FinalOverriders->end(); 1109 M != MEnd && !Done; ++M) { 1110 for (OverridingMethods::iterator SO = M->second.begin(), 1111 SOEnd = M->second.end(); 1112 SO != SOEnd && !Done; ++SO) { 1113 assert(SO->second.size() > 0 && 1114 "All virtual functions have overridding virtual functions"); 1115 1116 // C++ [class.abstract]p4: 1117 // A class is abstract if it contains or inherits at least one 1118 // pure virtual function for which the final overrider is pure 1119 // virtual. 1120 if (SO->second.front().Method->isPure()) { 1121 data().Abstract = true; 1122 Done = true; 1123 break; 1124 } 1125 } 1126 } 1127 } 1128 1129 // Set access bits correctly on the directly-declared conversions. 1130 for (UnresolvedSetIterator I = data().Conversions.begin(), 1131 E = data().Conversions.end(); 1132 I != E; ++I) 1133 data().Conversions.setAccess(I, (*I)->getAccess()); 1134 } 1135 1136 bool CXXRecordDecl::mayBeAbstract() const { 1137 if (data().Abstract || isInvalidDecl() || !data().Polymorphic || 1138 isDependentContext()) 1139 return false; 1140 1141 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(), 1142 BEnd = bases_end(); 1143 B != BEnd; ++B) { 1144 CXXRecordDecl *BaseDecl 1145 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); 1146 if (BaseDecl->isAbstract()) 1147 return true; 1148 } 1149 1150 return false; 1151 } 1152 1153 CXXMethodDecl * 1154 CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1155 SourceLocation StartLoc, 1156 const DeclarationNameInfo &NameInfo, 1157 QualType T, TypeSourceInfo *TInfo, 1158 bool isStatic, StorageClass SCAsWritten, bool isInline, 1159 bool isConstexpr, SourceLocation EndLocation) { 1160 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo, 1161 isStatic, SCAsWritten, isInline, isConstexpr, 1162 EndLocation); 1163 } 1164 1165 bool CXXMethodDecl::isUsualDeallocationFunction() const { 1166 if (getOverloadedOperator() != OO_Delete && 1167 getOverloadedOperator() != OO_Array_Delete) 1168 return false; 1169 1170 // C++ [basic.stc.dynamic.deallocation]p2: 1171 // A template instance is never a usual deallocation function, 1172 // regardless of its signature. 1173 if (getPrimaryTemplate()) 1174 return false; 1175 1176 // C++ [basic.stc.dynamic.deallocation]p2: 1177 // If a class T has a member deallocation function named operator delete 1178 // with exactly one parameter, then that function is a usual (non-placement) 1179 // deallocation function. [...] 1180 if (getNumParams() == 1) 1181 return true; 1182 1183 // C++ [basic.stc.dynamic.deallocation]p2: 1184 // [...] If class T does not declare such an operator delete but does 1185 // declare a member deallocation function named operator delete with 1186 // exactly two parameters, the second of which has type std::size_t (18.1), 1187 // then this function is a usual deallocation function. 1188 ASTContext &Context = getASTContext(); 1189 if (getNumParams() != 2 || 1190 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(), 1191 Context.getSizeType())) 1192 return false; 1193 1194 // This function is a usual deallocation function if there are no 1195 // single-parameter deallocation functions of the same kind. 1196 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); 1197 R.first != R.second; ++R.first) { 1198 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) 1199 if (FD->getNumParams() == 1) 1200 return false; 1201 } 1202 1203 return true; 1204 } 1205 1206 bool CXXMethodDecl::isCopyAssignmentOperator() const { 1207 // C++0x [class.copy]p17: 1208 // A user-declared copy assignment operator X::operator= is a non-static 1209 // non-template member function of class X with exactly one parameter of 1210 // type X, X&, const X&, volatile X& or const volatile X&. 1211 if (/*operator=*/getOverloadedOperator() != OO_Equal || 1212 /*non-static*/ isStatic() || 1213 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate()) 1214 return false; 1215 1216 QualType ParamType = getParamDecl(0)->getType(); 1217 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>()) 1218 ParamType = Ref->getPointeeType(); 1219 1220 ASTContext &Context = getASTContext(); 1221 QualType ClassType 1222 = Context.getCanonicalType(Context.getTypeDeclType(getParent())); 1223 return Context.hasSameUnqualifiedType(ClassType, ParamType); 1224 } 1225 1226 bool CXXMethodDecl::isMoveAssignmentOperator() const { 1227 // C++0x [class.copy]p19: 1228 // A user-declared move assignment operator X::operator= is a non-static 1229 // non-template member function of class X with exactly one parameter of type 1230 // X&&, const X&&, volatile X&&, or const volatile X&&. 1231 if (getOverloadedOperator() != OO_Equal || isStatic() || 1232 getPrimaryTemplate() || getDescribedFunctionTemplate()) 1233 return false; 1234 1235 QualType ParamType = getParamDecl(0)->getType(); 1236 if (!isa<RValueReferenceType>(ParamType)) 1237 return false; 1238 ParamType = ParamType->getPointeeType(); 1239 1240 ASTContext &Context = getASTContext(); 1241 QualType ClassType 1242 = Context.getCanonicalType(Context.getTypeDeclType(getParent())); 1243 return Context.hasSameUnqualifiedType(ClassType, ParamType); 1244 } 1245 1246 void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { 1247 assert(MD->isCanonicalDecl() && "Method is not canonical!"); 1248 assert(!MD->getParent()->isDependentContext() && 1249 "Can't add an overridden method to a class template!"); 1250 1251 getASTContext().addOverriddenMethod(this, MD); 1252 } 1253 1254 CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { 1255 return getASTContext().overridden_methods_begin(this); 1256 } 1257 1258 CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { 1259 return getASTContext().overridden_methods_end(this); 1260 } 1261 1262 unsigned CXXMethodDecl::size_overridden_methods() const { 1263 return getASTContext().overridden_methods_size(this); 1264 } 1265 1266 QualType CXXMethodDecl::getThisType(ASTContext &C) const { 1267 // C++ 9.3.2p1: The type of this in a member function of a class X is X*. 1268 // If the member function is declared const, the type of this is const X*, 1269 // if the member function is declared volatile, the type of this is 1270 // volatile X*, and if the member function is declared const volatile, 1271 // the type of this is const volatile X*. 1272 1273 assert(isInstance() && "No 'this' for static methods!"); 1274 1275 QualType ClassTy = C.getTypeDeclType(getParent()); 1276 ClassTy = C.getQualifiedType(ClassTy, 1277 Qualifiers::fromCVRMask(getTypeQualifiers())); 1278 return C.getPointerType(ClassTy); 1279 } 1280 1281 bool CXXMethodDecl::hasInlineBody() const { 1282 // If this function is a template instantiation, look at the template from 1283 // which it was instantiated. 1284 const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); 1285 if (!CheckFn) 1286 CheckFn = this; 1287 1288 const FunctionDecl *fn; 1289 return CheckFn->hasBody(fn) && !fn->isOutOfLine(); 1290 } 1291 1292 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1293 TypeSourceInfo *TInfo, bool IsVirtual, 1294 SourceLocation L, Expr *Init, 1295 SourceLocation R, 1296 SourceLocation EllipsisLoc) 1297 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init), 1298 LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false), 1299 SourceOrderOrNumArrayIndices(0) 1300 { 1301 } 1302 1303 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1304 FieldDecl *Member, 1305 SourceLocation MemberLoc, 1306 SourceLocation L, Expr *Init, 1307 SourceLocation R) 1308 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 1309 LParenLoc(L), RParenLoc(R), IsVirtual(false), 1310 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1311 { 1312 } 1313 1314 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1315 IndirectFieldDecl *Member, 1316 SourceLocation MemberLoc, 1317 SourceLocation L, Expr *Init, 1318 SourceLocation R) 1319 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 1320 LParenLoc(L), RParenLoc(R), IsVirtual(false), 1321 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1322 { 1323 } 1324 1325 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1326 SourceLocation D, SourceLocation L, 1327 CXXConstructorDecl *Target, Expr *Init, 1328 SourceLocation R) 1329 : Initializee(Target), MemberOrEllipsisLocation(D), Init(Init), 1330 LParenLoc(L), RParenLoc(R), IsVirtual(false), 1331 IsWritten(false), SourceOrderOrNumArrayIndices(0) 1332 { 1333 } 1334 1335 CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, 1336 FieldDecl *Member, 1337 SourceLocation MemberLoc, 1338 SourceLocation L, Expr *Init, 1339 SourceLocation R, 1340 VarDecl **Indices, 1341 unsigned NumIndices) 1342 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), 1343 LParenLoc(L), RParenLoc(R), IsVirtual(false), 1344 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices) 1345 { 1346 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1); 1347 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *)); 1348 } 1349 1350 CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context, 1351 FieldDecl *Member, 1352 SourceLocation MemberLoc, 1353 SourceLocation L, Expr *Init, 1354 SourceLocation R, 1355 VarDecl **Indices, 1356 unsigned NumIndices) { 1357 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) + 1358 sizeof(VarDecl *) * NumIndices, 1359 llvm::alignOf<CXXCtorInitializer>()); 1360 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R, 1361 Indices, NumIndices); 1362 } 1363 1364 TypeLoc CXXCtorInitializer::getBaseClassLoc() const { 1365 if (isBaseInitializer()) 1366 return Initializee.get<TypeSourceInfo*>()->getTypeLoc(); 1367 else 1368 return TypeLoc(); 1369 } 1370 1371 const Type *CXXCtorInitializer::getBaseClass() const { 1372 if (isBaseInitializer()) 1373 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr(); 1374 else 1375 return 0; 1376 } 1377 1378 SourceLocation CXXCtorInitializer::getSourceLocation() const { 1379 if (isAnyMemberInitializer() || isDelegatingInitializer()) 1380 return getMemberLocation(); 1381 1382 if (isInClassMemberInitializer()) 1383 return getAnyMember()->getLocation(); 1384 1385 return getBaseClassLoc().getLocalSourceRange().getBegin(); 1386 } 1387 1388 SourceRange CXXCtorInitializer::getSourceRange() const { 1389 if (isInClassMemberInitializer()) { 1390 FieldDecl *D = getAnyMember(); 1391 if (Expr *I = D->getInClassInitializer()) 1392 return I->getSourceRange(); 1393 return SourceRange(); 1394 } 1395 1396 return SourceRange(getSourceLocation(), getRParenLoc()); 1397 } 1398 1399 CXXConstructorDecl * 1400 CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) { 1401 return new (C) CXXConstructorDecl(0, SourceLocation(), DeclarationNameInfo(), 1402 QualType(), 0, false, false, false, false); 1403 } 1404 1405 CXXConstructorDecl * 1406 CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1407 SourceLocation StartLoc, 1408 const DeclarationNameInfo &NameInfo, 1409 QualType T, TypeSourceInfo *TInfo, 1410 bool isExplicit, bool isInline, 1411 bool isImplicitlyDeclared, bool isConstexpr) { 1412 assert(NameInfo.getName().getNameKind() 1413 == DeclarationName::CXXConstructorName && 1414 "Name must refer to a constructor"); 1415 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo, 1416 isExplicit, isInline, isImplicitlyDeclared, 1417 isConstexpr); 1418 } 1419 1420 bool CXXConstructorDecl::isDefaultConstructor() const { 1421 // C++ [class.ctor]p5: 1422 // A default constructor for a class X is a constructor of class 1423 // X that can be called without an argument. 1424 return (getNumParams() == 0) || 1425 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); 1426 } 1427 1428 bool 1429 CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { 1430 return isCopyOrMoveConstructor(TypeQuals) && 1431 getParamDecl(0)->getType()->isLValueReferenceType(); 1432 } 1433 1434 bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const { 1435 return isCopyOrMoveConstructor(TypeQuals) && 1436 getParamDecl(0)->getType()->isRValueReferenceType(); 1437 } 1438 1439 /// \brief Determine whether this is a copy or move constructor. 1440 bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const { 1441 // C++ [class.copy]p2: 1442 // A non-template constructor for class X is a copy constructor 1443 // if its first parameter is of type X&, const X&, volatile X& or 1444 // const volatile X&, and either there are no other parameters 1445 // or else all other parameters have default arguments (8.3.6). 1446 // C++0x [class.copy]p3: 1447 // A non-template constructor for class X is a move constructor if its 1448 // first parameter is of type X&&, const X&&, volatile X&&, or 1449 // const volatile X&&, and either there are no other parameters or else 1450 // all other parameters have default arguments. 1451 if ((getNumParams() < 1) || 1452 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 1453 (getPrimaryTemplate() != 0) || 1454 (getDescribedFunctionTemplate() != 0)) 1455 return false; 1456 1457 const ParmVarDecl *Param = getParamDecl(0); 1458 1459 // Do we have a reference type? 1460 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>(); 1461 if (!ParamRefType) 1462 return false; 1463 1464 // Is it a reference to our class type? 1465 ASTContext &Context = getASTContext(); 1466 1467 CanQualType PointeeType 1468 = Context.getCanonicalType(ParamRefType->getPointeeType()); 1469 CanQualType ClassTy 1470 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 1471 if (PointeeType.getUnqualifiedType() != ClassTy) 1472 return false; 1473 1474 // FIXME: other qualifiers? 1475 1476 // We have a copy or move constructor. 1477 TypeQuals = PointeeType.getCVRQualifiers(); 1478 return true; 1479 } 1480 1481 bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { 1482 // C++ [class.conv.ctor]p1: 1483 // A constructor declared without the function-specifier explicit 1484 // that can be called with a single parameter specifies a 1485 // conversion from the type of its first parameter to the type of 1486 // its class. Such a constructor is called a converting 1487 // constructor. 1488 if (isExplicit() && !AllowExplicit) 1489 return false; 1490 1491 return (getNumParams() == 0 && 1492 getType()->getAs<FunctionProtoType>()->isVariadic()) || 1493 (getNumParams() == 1) || 1494 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); 1495 } 1496 1497 bool CXXConstructorDecl::isSpecializationCopyingObject() const { 1498 if ((getNumParams() < 1) || 1499 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 1500 (getPrimaryTemplate() == 0) || 1501 (getDescribedFunctionTemplate() != 0)) 1502 return false; 1503 1504 const ParmVarDecl *Param = getParamDecl(0); 1505 1506 ASTContext &Context = getASTContext(); 1507 CanQualType ParamType = Context.getCanonicalType(Param->getType()); 1508 1509 // Is it the same as our our class type? 1510 CanQualType ClassTy 1511 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 1512 if (ParamType.getUnqualifiedType() != ClassTy) 1513 return false; 1514 1515 return true; 1516 } 1517 1518 const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const { 1519 // Hack: we store the inherited constructor in the overridden method table 1520 method_iterator It = begin_overridden_methods(); 1521 if (It == end_overridden_methods()) 1522 return 0; 1523 1524 return cast<CXXConstructorDecl>(*It); 1525 } 1526 1527 void 1528 CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){ 1529 // Hack: we store the inherited constructor in the overridden method table 1530 assert(size_overridden_methods() == 0 && "Base ctor already set."); 1531 addOverriddenMethod(BaseCtor); 1532 } 1533 1534 CXXDestructorDecl * 1535 CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) { 1536 return new (C) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(), 1537 QualType(), 0, false, false); 1538 } 1539 1540 CXXDestructorDecl * 1541 CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1542 SourceLocation StartLoc, 1543 const DeclarationNameInfo &NameInfo, 1544 QualType T, TypeSourceInfo *TInfo, 1545 bool isInline, bool isImplicitlyDeclared) { 1546 assert(NameInfo.getName().getNameKind() 1547 == DeclarationName::CXXDestructorName && 1548 "Name must refer to a destructor"); 1549 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline, 1550 isImplicitlyDeclared); 1551 } 1552 1553 CXXConversionDecl * 1554 CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) { 1555 return new (C) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(), 1556 QualType(), 0, false, false, false, 1557 SourceLocation()); 1558 } 1559 1560 CXXConversionDecl * 1561 CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, 1562 SourceLocation StartLoc, 1563 const DeclarationNameInfo &NameInfo, 1564 QualType T, TypeSourceInfo *TInfo, 1565 bool isInline, bool isExplicit, 1566 bool isConstexpr, SourceLocation EndLocation) { 1567 assert(NameInfo.getName().getNameKind() 1568 == DeclarationName::CXXConversionFunctionName && 1569 "Name must refer to a conversion function"); 1570 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo, 1571 isInline, isExplicit, isConstexpr, 1572 EndLocation); 1573 } 1574 1575 LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, 1576 DeclContext *DC, 1577 SourceLocation ExternLoc, 1578 SourceLocation LangLoc, 1579 LanguageIDs Lang, 1580 SourceLocation RBraceLoc) { 1581 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc); 1582 } 1583 1584 UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, 1585 SourceLocation L, 1586 SourceLocation NamespaceLoc, 1587 NestedNameSpecifierLoc QualifierLoc, 1588 SourceLocation IdentLoc, 1589 NamedDecl *Used, 1590 DeclContext *CommonAncestor) { 1591 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) 1592 Used = NS->getOriginalNamespace(); 1593 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc, 1594 IdentLoc, Used, CommonAncestor); 1595 } 1596 1597 NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { 1598 if (NamespaceAliasDecl *NA = 1599 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) 1600 return NA->getNamespace(); 1601 return cast_or_null<NamespaceDecl>(NominatedNamespace); 1602 } 1603 1604 NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, 1605 SourceLocation UsingLoc, 1606 SourceLocation AliasLoc, 1607 IdentifierInfo *Alias, 1608 NestedNameSpecifierLoc QualifierLoc, 1609 SourceLocation IdentLoc, 1610 NamedDecl *Namespace) { 1611 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) 1612 Namespace = NS->getOriginalNamespace(); 1613 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, 1614 QualifierLoc, IdentLoc, Namespace); 1615 } 1616 1617 UsingDecl *UsingShadowDecl::getUsingDecl() const { 1618 const UsingShadowDecl *Shadow = this; 1619 while (const UsingShadowDecl *NextShadow = 1620 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow)) 1621 Shadow = NextShadow; 1622 return cast<UsingDecl>(Shadow->UsingOrNextShadow); 1623 } 1624 1625 void UsingDecl::addShadowDecl(UsingShadowDecl *S) { 1626 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() && 1627 "declaration already in set"); 1628 assert(S->getUsingDecl() == this); 1629 1630 if (FirstUsingShadow) 1631 S->UsingOrNextShadow = FirstUsingShadow; 1632 FirstUsingShadow = S; 1633 } 1634 1635 void UsingDecl::removeShadowDecl(UsingShadowDecl *S) { 1636 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() && 1637 "declaration not in set"); 1638 assert(S->getUsingDecl() == this); 1639 1640 // Remove S from the shadow decl chain. This is O(n) but hopefully rare. 1641 1642 if (FirstUsingShadow == S) { 1643 FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow); 1644 S->UsingOrNextShadow = this; 1645 return; 1646 } 1647 1648 UsingShadowDecl *Prev = FirstUsingShadow; 1649 while (Prev->UsingOrNextShadow != S) 1650 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow); 1651 Prev->UsingOrNextShadow = S->UsingOrNextShadow; 1652 S->UsingOrNextShadow = this; 1653 } 1654 1655 UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL, 1656 NestedNameSpecifierLoc QualifierLoc, 1657 const DeclarationNameInfo &NameInfo, 1658 bool IsTypeNameArg) { 1659 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg); 1660 } 1661 1662 UnresolvedUsingValueDecl * 1663 UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, 1664 SourceLocation UsingLoc, 1665 NestedNameSpecifierLoc QualifierLoc, 1666 const DeclarationNameInfo &NameInfo) { 1667 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, 1668 QualifierLoc, NameInfo); 1669 } 1670 1671 UnresolvedUsingTypenameDecl * 1672 UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, 1673 SourceLocation UsingLoc, 1674 SourceLocation TypenameLoc, 1675 NestedNameSpecifierLoc QualifierLoc, 1676 SourceLocation TargetNameLoc, 1677 DeclarationName TargetName) { 1678 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, 1679 QualifierLoc, TargetNameLoc, 1680 TargetName.getAsIdentifierInfo()); 1681 } 1682 1683 StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, 1684 SourceLocation StaticAssertLoc, 1685 Expr *AssertExpr, 1686 StringLiteral *Message, 1687 SourceLocation RParenLoc) { 1688 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message, 1689 RParenLoc); 1690 } 1691 1692 static const char *getAccessName(AccessSpecifier AS) { 1693 switch (AS) { 1694 default: 1695 case AS_none: 1696 llvm_unreachable("Invalid access specifier!"); 1697 case AS_public: 1698 return "public"; 1699 case AS_private: 1700 return "private"; 1701 case AS_protected: 1702 return "protected"; 1703 } 1704 } 1705 1706 const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, 1707 AccessSpecifier AS) { 1708 return DB << getAccessName(AS); 1709 } 1710