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