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