DeclCXX.cpp revision 202879
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/Expr.h" 18#include "clang/AST/TypeLoc.h" 19#include "clang/Basic/IdentifierTable.h" 20#include "llvm/ADT/STLExtras.h" 21#include "llvm/ADT/SmallPtrSet.h" 22using namespace clang; 23 24//===----------------------------------------------------------------------===// 25// Decl Allocation/Deallocation Method Implementations 26//===----------------------------------------------------------------------===// 27 28CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 29 SourceLocation L, IdentifierInfo *Id, 30 CXXRecordDecl *PrevDecl, 31 SourceLocation TKL) 32 : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL), 33 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), 34 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false), 35 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false), 36 Abstract(false), HasTrivialConstructor(true), 37 HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true), 38 HasTrivialDestructor(true), ComputedVisibleConversions(false), 39 Bases(0), NumBases(0), VBases(0), NumVBases(0), 40 TemplateOrInstantiation() { } 41 42CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, 43 SourceLocation L, IdentifierInfo *Id, 44 SourceLocation TKL, 45 CXXRecordDecl* PrevDecl, 46 bool DelayTypeCreation) { 47 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id, 48 PrevDecl, TKL); 49 50 // FIXME: DelayTypeCreation seems like such a hack 51 if (!DelayTypeCreation) 52 C.getTypeDeclType(R, PrevDecl); 53 return R; 54} 55 56CXXRecordDecl::~CXXRecordDecl() { 57} 58 59void CXXRecordDecl::Destroy(ASTContext &C) { 60 C.Deallocate(Bases); 61 C.Deallocate(VBases); 62 this->RecordDecl::Destroy(C); 63} 64 65void 66CXXRecordDecl::setBases(ASTContext &C, 67 CXXBaseSpecifier const * const *Bases, 68 unsigned NumBases) { 69 // C++ [dcl.init.aggr]p1: 70 // An aggregate is an array or a class (clause 9) with [...] 71 // no base classes [...]. 72 Aggregate = false; 73 74 if (this->Bases) 75 C.Deallocate(this->Bases); 76 77 int vbaseCount = 0; 78 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases; 79 bool hasDirectVirtualBase = false; 80 81 this->Bases = new(C) CXXBaseSpecifier [NumBases]; 82 this->NumBases = NumBases; 83 for (unsigned i = 0; i < NumBases; ++i) { 84 this->Bases[i] = *Bases[i]; 85 // Keep track of inherited vbases for this base class. 86 const CXXBaseSpecifier *Base = Bases[i]; 87 QualType BaseType = Base->getType(); 88 // Skip template types. 89 // FIXME. This means that this list must be rebuilt during template 90 // instantiation. 91 if (BaseType->isDependentType()) 92 continue; 93 CXXRecordDecl *BaseClassDecl 94 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); 95 if (Base->isVirtual()) 96 hasDirectVirtualBase = true; 97 for (CXXRecordDecl::base_class_iterator VBase = 98 BaseClassDecl->vbases_begin(), 99 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { 100 // Add this vbase to the array of vbases for current class if it is 101 // not already in the list. 102 // FIXME. Note that we do a linear search as number of such classes are 103 // very few. 104 int i; 105 for (i = 0; i < vbaseCount; ++i) 106 if (UniqueVbases[i]->getType() == VBase->getType()) 107 break; 108 if (i == vbaseCount) { 109 UniqueVbases.push_back(VBase); 110 ++vbaseCount; 111 } 112 } 113 } 114 if (hasDirectVirtualBase) { 115 // Iterate one more time through the direct bases and add the virtual 116 // base to the list of vritual bases for current class. 117 for (unsigned i = 0; i < NumBases; ++i) { 118 const CXXBaseSpecifier *VBase = Bases[i]; 119 if (!VBase->isVirtual()) 120 continue; 121 int j; 122 for (j = 0; j < vbaseCount; ++j) 123 if (UniqueVbases[j]->getType() == VBase->getType()) 124 break; 125 if (j == vbaseCount) { 126 UniqueVbases.push_back(VBase); 127 ++vbaseCount; 128 } 129 } 130 } 131 if (vbaseCount > 0) { 132 // build AST for inhireted, direct or indirect, virtual bases. 133 this->VBases = new (C) CXXBaseSpecifier [vbaseCount]; 134 this->NumVBases = vbaseCount; 135 for (int i = 0; i < vbaseCount; i++) { 136 QualType QT = UniqueVbases[i]->getType(); 137 CXXRecordDecl *VBaseClassDecl 138 = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl()); 139 this->VBases[i] = 140 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true, 141 VBaseClassDecl->getTagKind() == RecordDecl::TK_class, 142 UniqueVbases[i]->getAccessSpecifier(), QT); 143 } 144 } 145} 146 147/// Callback function for CXXRecordDecl::forallBases that acknowledges 148/// that it saw a base class. 149static bool SawBase(const CXXRecordDecl *, void *) { 150 return true; 151} 152 153bool CXXRecordDecl::hasAnyDependentBases() const { 154 if (!isDependentContext()) 155 return false; 156 157 return !forallBases(SawBase, 0); 158} 159 160bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const { 161 return getCopyConstructor(Context, Qualifiers::Const) != 0; 162} 163 164CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context, 165 unsigned TypeQuals) const{ 166 QualType ClassType 167 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); 168 DeclarationName ConstructorName 169 = Context.DeclarationNames.getCXXConstructorName( 170 Context.getCanonicalType(ClassType)); 171 unsigned FoundTQs; 172 DeclContext::lookup_const_iterator Con, ConEnd; 173 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName); 174 Con != ConEnd; ++Con) { 175 // C++ [class.copy]p2: 176 // A non-template constructor for class X is a copy constructor if [...] 177 if (isa<FunctionTemplateDecl>(*Con)) 178 continue; 179 180 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(FoundTQs)) { 181 if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) || 182 (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const))) 183 return cast<CXXConstructorDecl>(*Con); 184 185 } 186 } 187 return 0; 188} 189 190bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context, 191 const CXXMethodDecl *& MD) const { 192 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( 193 const_cast<CXXRecordDecl*>(this))); 194 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal); 195 196 DeclContext::lookup_const_iterator Op, OpEnd; 197 for (llvm::tie(Op, OpEnd) = this->lookup(OpName); 198 Op != OpEnd; ++Op) { 199 // C++ [class.copy]p9: 200 // A user-declared copy assignment operator is a non-static non-template 201 // member function of class X with exactly one parameter of type X, X&, 202 // const X&, volatile X& or const volatile X&. 203 const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); 204 if (!Method) 205 continue; 206 207 if (Method->isStatic()) 208 continue; 209 if (Method->getPrimaryTemplate()) 210 continue; 211 const FunctionProtoType *FnType = 212 Method->getType()->getAs<FunctionProtoType>(); 213 assert(FnType && "Overloaded operator has no prototype."); 214 // Don't assert on this; an invalid decl might have been left in the AST. 215 if (FnType->getNumArgs() != 1 || FnType->isVariadic()) 216 continue; 217 bool AcceptsConst = true; 218 QualType ArgType = FnType->getArgType(0); 219 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) { 220 ArgType = Ref->getPointeeType(); 221 // Is it a non-const lvalue reference? 222 if (!ArgType.isConstQualified()) 223 AcceptsConst = false; 224 } 225 if (!Context.hasSameUnqualifiedType(ArgType, ClassType)) 226 continue; 227 MD = Method; 228 // We have a single argument of type cv X or cv X&, i.e. we've found the 229 // copy assignment operator. Return whether it accepts const arguments. 230 return AcceptsConst; 231 } 232 assert(isInvalidDecl() && 233 "No copy assignment operator declared in valid code."); 234 return false; 235} 236 237void 238CXXRecordDecl::addedConstructor(ASTContext &Context, 239 CXXConstructorDecl *ConDecl) { 240 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl"); 241 // Note that we have a user-declared constructor. 242 UserDeclaredConstructor = true; 243 244 // C++ [dcl.init.aggr]p1: 245 // An aggregate is an array or a class (clause 9) with no 246 // user-declared constructors (12.1) [...]. 247 Aggregate = false; 248 249 // C++ [class]p4: 250 // A POD-struct is an aggregate class [...] 251 PlainOldData = false; 252 253 // C++ [class.ctor]p5: 254 // A constructor is trivial if it is an implicitly-declared default 255 // constructor. 256 // FIXME: C++0x: don't do this for "= default" default constructors. 257 HasTrivialConstructor = false; 258 259 // Note when we have a user-declared copy constructor, which will 260 // suppress the implicit declaration of a copy constructor. 261 if (ConDecl->isCopyConstructor()) { 262 UserDeclaredCopyConstructor = true; 263 264 // C++ [class.copy]p6: 265 // A copy constructor is trivial if it is implicitly declared. 266 // FIXME: C++0x: don't do this for "= default" copy constructors. 267 HasTrivialCopyConstructor = false; 268 } 269} 270 271void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context, 272 CXXMethodDecl *OpDecl) { 273 // We're interested specifically in copy assignment operators. 274 const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>(); 275 assert(FnType && "Overloaded operator has no proto function type."); 276 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic()); 277 278 // Copy assignment operators must be non-templates. 279 if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate()) 280 return; 281 282 QualType ArgType = FnType->getArgType(0); 283 if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) 284 ArgType = Ref->getPointeeType(); 285 286 ArgType = ArgType.getUnqualifiedType(); 287 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( 288 const_cast<CXXRecordDecl*>(this))); 289 290 if (!Context.hasSameUnqualifiedType(ClassType, ArgType)) 291 return; 292 293 // This is a copy assignment operator. 294 // Note on the decl that it is a copy assignment operator. 295 OpDecl->setCopyAssignment(true); 296 297 // Suppress the implicit declaration of a copy constructor. 298 UserDeclaredCopyAssignment = true; 299 300 // C++ [class.copy]p11: 301 // A copy assignment operator is trivial if it is implicitly declared. 302 // FIXME: C++0x: don't do this for "= default" copy operators. 303 HasTrivialCopyAssignment = false; 304 305 // C++ [class]p4: 306 // A POD-struct is an aggregate class that [...] has no user-defined copy 307 // assignment operator [...]. 308 PlainOldData = false; 309} 310 311void 312CXXRecordDecl::collectConversionFunctions( 313 llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const 314{ 315 const UnresolvedSetImpl *Cs = getConversionFunctions(); 316 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end(); 317 I != E; ++I) { 318 NamedDecl *TopConv = *I; 319 CanQualType TConvType; 320 if (FunctionTemplateDecl *TConversionTemplate = 321 dyn_cast<FunctionTemplateDecl>(TopConv)) 322 TConvType = 323 getASTContext().getCanonicalType( 324 TConversionTemplate->getTemplatedDecl()->getResultType()); 325 else 326 TConvType = 327 getASTContext().getCanonicalType( 328 cast<CXXConversionDecl>(TopConv)->getConversionType()); 329 ConversionsTypeSet.insert(TConvType); 330 } 331} 332 333/// getNestedVisibleConversionFunctions - imports unique conversion 334/// functions from base classes into the visible conversion function 335/// list of the class 'RD'. This is a private helper method. 336/// TopConversionsTypeSet is the set of conversion functions of the class 337/// we are interested in. HiddenConversionTypes is set of conversion functions 338/// of the immediate derived class which hides the conversion functions found 339/// in current class. 340void 341CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD, 342 const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet, 343 const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes) 344{ 345 bool inTopClass = (RD == this); 346 QualType ClassType = getASTContext().getTypeDeclType(this); 347 if (const RecordType *Record = ClassType->getAs<RecordType>()) { 348 const UnresolvedSetImpl *Cs 349 = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); 350 351 for (UnresolvedSetImpl::iterator I = Cs->begin(), E = Cs->end(); 352 I != E; ++I) { 353 NamedDecl *Conv = *I; 354 // Only those conversions not exact match of conversions in current 355 // class are candidateconversion routines. 356 CanQualType ConvType; 357 if (FunctionTemplateDecl *ConversionTemplate = 358 dyn_cast<FunctionTemplateDecl>(Conv)) 359 ConvType = 360 getASTContext().getCanonicalType( 361 ConversionTemplate->getTemplatedDecl()->getResultType()); 362 else 363 ConvType = 364 getASTContext().getCanonicalType( 365 cast<CXXConversionDecl>(Conv)->getConversionType()); 366 // We only add conversion functions found in the base class if they 367 // are not hidden by those found in HiddenConversionTypes which are 368 // the conversion functions in its derived class. 369 if (inTopClass || 370 (!TopConversionsTypeSet.count(ConvType) && 371 !HiddenConversionTypes.count(ConvType)) ) { 372 if (FunctionTemplateDecl *ConversionTemplate = 373 dyn_cast<FunctionTemplateDecl>(Conv)) 374 RD->addVisibleConversionFunction(ConversionTemplate); 375 else 376 RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv)); 377 } 378 } 379 } 380 381 if (getNumBases() == 0 && getNumVBases() == 0) 382 return; 383 384 llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions; 385 if (!inTopClass) 386 collectConversionFunctions(ConversionFunctions); 387 388 for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(), 389 E = vbases_end(); VBase != E; ++VBase) { 390 if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) { 391 CXXRecordDecl *VBaseClassDecl 392 = cast<CXXRecordDecl>(RT->getDecl()); 393 VBaseClassDecl->getNestedVisibleConversionFunctions(RD, 394 TopConversionsTypeSet, 395 (inTopClass ? TopConversionsTypeSet : ConversionFunctions)); 396 } 397 } 398 for (CXXRecordDecl::base_class_iterator Base = bases_begin(), 399 E = bases_end(); Base != E; ++Base) { 400 if (Base->isVirtual()) 401 continue; 402 if (const RecordType *RT = Base->getType()->getAs<RecordType>()) { 403 CXXRecordDecl *BaseClassDecl 404 = cast<CXXRecordDecl>(RT->getDecl()); 405 406 BaseClassDecl->getNestedVisibleConversionFunctions(RD, 407 TopConversionsTypeSet, 408 (inTopClass ? TopConversionsTypeSet : ConversionFunctions)); 409 } 410 } 411} 412 413/// getVisibleConversionFunctions - get all conversion functions visible 414/// in current class; including conversion function templates. 415const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() { 416 // If root class, all conversions are visible. 417 if (bases_begin() == bases_end()) 418 return &Conversions; 419 // If visible conversion list is already evaluated, return it. 420 if (ComputedVisibleConversions) 421 return &VisibleConversions; 422 llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet; 423 collectConversionFunctions(TopConversionsTypeSet); 424 getNestedVisibleConversionFunctions(this, TopConversionsTypeSet, 425 TopConversionsTypeSet); 426 ComputedVisibleConversions = true; 427 return &VisibleConversions; 428} 429 430void CXXRecordDecl::addVisibleConversionFunction( 431 CXXConversionDecl *ConvDecl) { 432 assert(!ConvDecl->getDescribedFunctionTemplate() && 433 "Conversion function templates should cast to FunctionTemplateDecl."); 434 VisibleConversions.addDecl(ConvDecl); 435} 436 437void CXXRecordDecl::addVisibleConversionFunction( 438 FunctionTemplateDecl *ConvDecl) { 439 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) && 440 "Function template is not a conversion function template"); 441 VisibleConversions.addDecl(ConvDecl); 442} 443 444void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) { 445 assert(!ConvDecl->getDescribedFunctionTemplate() && 446 "Conversion function templates should cast to FunctionTemplateDecl."); 447 Conversions.addDecl(ConvDecl); 448} 449 450void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) { 451 assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) && 452 "Function template is not a conversion function template"); 453 Conversions.addDecl(ConvDecl); 454} 455 456 457void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) { 458 Method->setVirtualAsWritten(true); 459 setAggregate(false); 460 setPOD(false); 461 setEmpty(false); 462 setPolymorphic(true); 463 setHasTrivialConstructor(false); 464 setHasTrivialCopyConstructor(false); 465 setHasTrivialCopyAssignment(false); 466} 467 468CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { 469 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 470 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); 471 472 return 0; 473} 474 475MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { 476 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); 477} 478 479void 480CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, 481 TemplateSpecializationKind TSK) { 482 assert(TemplateOrInstantiation.isNull() && 483 "Previous template or instantiation?"); 484 assert(!isa<ClassTemplateSpecializationDecl>(this)); 485 TemplateOrInstantiation 486 = new (getASTContext()) MemberSpecializationInfo(RD, TSK); 487} 488 489TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ 490 if (const ClassTemplateSpecializationDecl *Spec 491 = dyn_cast<ClassTemplateSpecializationDecl>(this)) 492 return Spec->getSpecializationKind(); 493 494 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) 495 return MSInfo->getTemplateSpecializationKind(); 496 497 return TSK_Undeclared; 498} 499 500void 501CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { 502 if (ClassTemplateSpecializationDecl *Spec 503 = dyn_cast<ClassTemplateSpecializationDecl>(this)) { 504 Spec->setSpecializationKind(TSK); 505 return; 506 } 507 508 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { 509 MSInfo->setTemplateSpecializationKind(TSK); 510 return; 511 } 512 513 assert(false && "Not a class template or member class specialization"); 514} 515 516CXXConstructorDecl * 517CXXRecordDecl::getDefaultConstructor(ASTContext &Context) { 518 QualType ClassType = Context.getTypeDeclType(this); 519 DeclarationName ConstructorName 520 = Context.DeclarationNames.getCXXConstructorName( 521 Context.getCanonicalType(ClassType.getUnqualifiedType())); 522 523 DeclContext::lookup_const_iterator Con, ConEnd; 524 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName); 525 Con != ConEnd; ++Con) { 526 // FIXME: In C++0x, a constructor template can be a default constructor. 527 if (isa<FunctionTemplateDecl>(*Con)) 528 continue; 529 530 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); 531 if (Constructor->isDefaultConstructor()) 532 return Constructor; 533 } 534 return 0; 535} 536 537CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) { 538 QualType ClassType = Context.getTypeDeclType(this); 539 540 DeclarationName Name 541 = Context.DeclarationNames.getCXXDestructorName( 542 Context.getCanonicalType(ClassType)); 543 544 DeclContext::lookup_iterator I, E; 545 llvm::tie(I, E) = lookup(Name); 546 assert(I != E && "Did not find a destructor!"); 547 548 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); 549 assert(++I == E && "Found more than one destructor!"); 550 551 return Dtor; 552} 553 554CXXMethodDecl * 555CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, 556 SourceLocation L, DeclarationName N, 557 QualType T, TypeSourceInfo *TInfo, 558 bool isStatic, bool isInline) { 559 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo, 560 isStatic, isInline); 561} 562 563bool CXXMethodDecl::isUsualDeallocationFunction() const { 564 if (getOverloadedOperator() != OO_Delete && 565 getOverloadedOperator() != OO_Array_Delete) 566 return false; 567 568 // C++ [basic.stc.dynamic.deallocation]p2: 569 // If a class T has a member deallocation function named operator delete 570 // with exactly one parameter, then that function is a usual (non-placement) 571 // deallocation function. [...] 572 if (getNumParams() == 1) 573 return true; 574 575 // C++ [basic.stc.dynamic.deallocation]p2: 576 // [...] If class T does not declare such an operator delete but does 577 // declare a member deallocation function named operator delete with 578 // exactly two parameters, the second of which has type std::size_t (18.1), 579 // then this function is a usual deallocation function. 580 ASTContext &Context = getASTContext(); 581 if (getNumParams() != 2 || 582 !Context.hasSameType(getParamDecl(1)->getType(), Context.getSizeType())) 583 return false; 584 585 // This function is a usual deallocation function if there are no 586 // single-parameter deallocation functions of the same kind. 587 for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); 588 R.first != R.second; ++R.first) { 589 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) 590 if (FD->getNumParams() == 1) 591 return false; 592 } 593 594 return true; 595} 596 597typedef llvm::DenseMap<const CXXMethodDecl*, 598 std::vector<const CXXMethodDecl *> *> 599 OverriddenMethodsMapTy; 600 601// FIXME: We hate static data. This doesn't survive PCH saving/loading, and 602// the vtable building code uses it at CG time. 603static OverriddenMethodsMapTy *OverriddenMethods = 0; 604 605void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { 606 assert(MD->isCanonicalDecl() && "Method is not canonical!"); 607 608 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry. 609 610 if (!OverriddenMethods) 611 OverriddenMethods = new OverriddenMethodsMapTy(); 612 613 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this]; 614 if (!Methods) 615 Methods = new std::vector<const CXXMethodDecl *>; 616 617 Methods->push_back(MD); 618} 619 620CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { 621 if (!OverriddenMethods) 622 return 0; 623 624 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); 625 if (it == OverriddenMethods->end() || it->second->empty()) 626 return 0; 627 628 return &(*it->second)[0]; 629} 630 631CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { 632 if (!OverriddenMethods) 633 return 0; 634 635 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); 636 if (it == OverriddenMethods->end() || it->second->empty()) 637 return 0; 638 639 return &(*it->second)[0] + it->second->size(); 640} 641 642QualType CXXMethodDecl::getThisType(ASTContext &C) const { 643 // C++ 9.3.2p1: The type of this in a member function of a class X is X*. 644 // If the member function is declared const, the type of this is const X*, 645 // if the member function is declared volatile, the type of this is 646 // volatile X*, and if the member function is declared const volatile, 647 // the type of this is const volatile X*. 648 649 assert(isInstance() && "No 'this' for static methods!"); 650 651 QualType ClassTy; 652 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate()) 653 ClassTy = TD->getInjectedClassNameType(C); 654 else 655 ClassTy = C.getTagDeclType(getParent()); 656 ClassTy = C.getQualifiedType(ClassTy, 657 Qualifiers::fromCVRMask(getTypeQualifiers())); 658 return C.getPointerType(ClassTy); 659} 660 661bool CXXMethodDecl::hasInlineBody() const { 662 // If this function is a template instantiation, look at the template from 663 // which it was instantiated. 664 const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); 665 if (!CheckFn) 666 CheckFn = this; 667 668 const FunctionDecl *fn; 669 return CheckFn->getBody(fn) && !fn->isOutOfLine(); 670} 671 672CXXBaseOrMemberInitializer:: 673CXXBaseOrMemberInitializer(ASTContext &Context, 674 TypeSourceInfo *TInfo, CXXConstructorDecl *C, 675 SourceLocation L, 676 Expr **Args, unsigned NumArgs, 677 SourceLocation R) 678 : BaseOrMember(TInfo), Args(0), NumArgs(0), CtorOrAnonUnion(C), 679 LParenLoc(L), RParenLoc(R) 680{ 681 if (NumArgs > 0) { 682 this->NumArgs = NumArgs; 683 this->Args = new (Context) Stmt*[NumArgs]; 684 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 685 this->Args[Idx] = Args[Idx]; 686 } 687} 688 689CXXBaseOrMemberInitializer:: 690CXXBaseOrMemberInitializer(ASTContext &Context, 691 FieldDecl *Member, SourceLocation MemberLoc, 692 CXXConstructorDecl *C, SourceLocation L, 693 Expr **Args, unsigned NumArgs, 694 SourceLocation R) 695 : BaseOrMember(Member), MemberLocation(MemberLoc), Args(0), NumArgs(0), 696 CtorOrAnonUnion(C), LParenLoc(L), RParenLoc(R) 697{ 698 if (NumArgs > 0) { 699 this->NumArgs = NumArgs; 700 this->Args = new (Context) Stmt*[NumArgs]; 701 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 702 this->Args[Idx] = Args[Idx]; 703 } 704} 705 706void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) { 707 for (unsigned I = 0; I != NumArgs; ++I) 708 Args[I]->Destroy(Context); 709 Context.Deallocate(Args); 710 this->~CXXBaseOrMemberInitializer(); 711} 712 713TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const { 714 if (isBaseInitializer()) 715 return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc(); 716 else 717 return TypeLoc(); 718} 719 720Type *CXXBaseOrMemberInitializer::getBaseClass() { 721 if (isBaseInitializer()) 722 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); 723 else 724 return 0; 725} 726 727const Type *CXXBaseOrMemberInitializer::getBaseClass() const { 728 if (isBaseInitializer()) 729 return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); 730 else 731 return 0; 732} 733 734SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const { 735 if (isMemberInitializer()) 736 return getMemberLocation(); 737 738 return getBaseClassLoc().getSourceRange().getBegin(); 739} 740 741SourceRange CXXBaseOrMemberInitializer::getSourceRange() const { 742 return SourceRange(getSourceLocation(), getRParenLoc()); 743} 744 745CXXConstructorDecl * 746CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 747 SourceLocation L, DeclarationName N, 748 QualType T, TypeSourceInfo *TInfo, 749 bool isExplicit, 750 bool isInline, bool isImplicitlyDeclared) { 751 assert(N.getNameKind() == DeclarationName::CXXConstructorName && 752 "Name must refer to a constructor"); 753 return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline, 754 isImplicitlyDeclared); 755} 756 757bool CXXConstructorDecl::isDefaultConstructor() const { 758 // C++ [class.ctor]p5: 759 // A default constructor for a class X is a constructor of class 760 // X that can be called without an argument. 761 return (getNumParams() == 0) || 762 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); 763} 764 765bool 766CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { 767 // C++ [class.copy]p2: 768 // A non-template constructor for class X is a copy constructor 769 // if its first parameter is of type X&, const X&, volatile X& or 770 // const volatile X&, and either there are no other parameters 771 // or else all other parameters have default arguments (8.3.6). 772 if ((getNumParams() < 1) || 773 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 774 (getPrimaryTemplate() != 0) || 775 (getDescribedFunctionTemplate() != 0)) 776 return false; 777 778 const ParmVarDecl *Param = getParamDecl(0); 779 780 // Do we have a reference type? Rvalue references don't count. 781 const LValueReferenceType *ParamRefType = 782 Param->getType()->getAs<LValueReferenceType>(); 783 if (!ParamRefType) 784 return false; 785 786 // Is it a reference to our class type? 787 ASTContext &Context = getASTContext(); 788 789 CanQualType PointeeType 790 = Context.getCanonicalType(ParamRefType->getPointeeType()); 791 CanQualType ClassTy 792 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 793 if (PointeeType.getUnqualifiedType() != ClassTy) 794 return false; 795 796 // FIXME: other qualifiers? 797 798 // We have a copy constructor. 799 TypeQuals = PointeeType.getCVRQualifiers(); 800 return true; 801} 802 803bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { 804 // C++ [class.conv.ctor]p1: 805 // A constructor declared without the function-specifier explicit 806 // that can be called with a single parameter specifies a 807 // conversion from the type of its first parameter to the type of 808 // its class. Such a constructor is called a converting 809 // constructor. 810 if (isExplicit() && !AllowExplicit) 811 return false; 812 813 return (getNumParams() == 0 && 814 getType()->getAs<FunctionProtoType>()->isVariadic()) || 815 (getNumParams() == 1) || 816 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); 817} 818 819bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const { 820 if ((getNumParams() < 1) || 821 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || 822 (getPrimaryTemplate() == 0) || 823 (getDescribedFunctionTemplate() != 0)) 824 return false; 825 826 const ParmVarDecl *Param = getParamDecl(0); 827 828 ASTContext &Context = getASTContext(); 829 CanQualType ParamType = Context.getCanonicalType(Param->getType()); 830 831 // Strip off the lvalue reference, if any. 832 if (CanQual<LValueReferenceType> ParamRefType 833 = ParamType->getAs<LValueReferenceType>()) 834 ParamType = ParamRefType->getPointeeType(); 835 836 837 // Is it the same as our our class type? 838 CanQualType ClassTy 839 = Context.getCanonicalType(Context.getTagDeclType(getParent())); 840 if (ParamType.getUnqualifiedType() != ClassTy) 841 return false; 842 843 return true; 844} 845 846CXXDestructorDecl * 847CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 848 SourceLocation L, DeclarationName N, 849 QualType T, bool isInline, 850 bool isImplicitlyDeclared) { 851 assert(N.getNameKind() == DeclarationName::CXXDestructorName && 852 "Name must refer to a destructor"); 853 return new (C) CXXDestructorDecl(RD, L, N, T, isInline, 854 isImplicitlyDeclared); 855} 856 857void 858CXXConstructorDecl::Destroy(ASTContext& C) { 859 C.Deallocate(BaseOrMemberInitializers); 860 CXXMethodDecl::Destroy(C); 861} 862 863CXXConversionDecl * 864CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, 865 SourceLocation L, DeclarationName N, 866 QualType T, TypeSourceInfo *TInfo, 867 bool isInline, bool isExplicit) { 868 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName && 869 "Name must refer to a conversion function"); 870 return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit); 871} 872 873FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC, 874 SourceLocation L, 875 FriendUnion Friend, 876 SourceLocation FriendL) { 877#ifndef NDEBUG 878 if (Friend.is<NamedDecl*>()) { 879 NamedDecl *D = Friend.get<NamedDecl*>(); 880 assert(isa<FunctionDecl>(D) || 881 isa<CXXRecordDecl>(D) || 882 isa<FunctionTemplateDecl>(D) || 883 isa<ClassTemplateDecl>(D)); 884 885 // As a temporary hack, we permit template instantiation to point 886 // to the original declaration when instantiating members. 887 assert(D->getFriendObjectKind() || 888 (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind())); 889 } 890#endif 891 892 return new (C) FriendDecl(DC, L, Friend, FriendL); 893} 894 895LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, 896 DeclContext *DC, 897 SourceLocation L, 898 LanguageIDs Lang, bool Braces) { 899 return new (C) LinkageSpecDecl(DC, L, Lang, Braces); 900} 901 902UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, 903 SourceLocation L, 904 SourceLocation NamespaceLoc, 905 SourceRange QualifierRange, 906 NestedNameSpecifier *Qualifier, 907 SourceLocation IdentLoc, 908 NamedDecl *Used, 909 DeclContext *CommonAncestor) { 910 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) 911 Used = NS->getOriginalNamespace(); 912 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange, 913 Qualifier, IdentLoc, Used, CommonAncestor); 914} 915 916NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { 917 if (NamespaceAliasDecl *NA = 918 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) 919 return NA->getNamespace(); 920 return cast_or_null<NamespaceDecl>(NominatedNamespace); 921} 922 923NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, 924 SourceLocation L, 925 SourceLocation AliasLoc, 926 IdentifierInfo *Alias, 927 SourceRange QualifierRange, 928 NestedNameSpecifier *Qualifier, 929 SourceLocation IdentLoc, 930 NamedDecl *Namespace) { 931 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) 932 Namespace = NS->getOriginalNamespace(); 933 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange, 934 Qualifier, IdentLoc, Namespace); 935} 936 937UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, 938 SourceLocation L, SourceRange NNR, SourceLocation UL, 939 NestedNameSpecifier* TargetNNS, DeclarationName Name, 940 bool IsTypeNameArg) { 941 return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg); 942} 943 944UnresolvedUsingValueDecl * 945UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, 946 SourceLocation UsingLoc, 947 SourceRange TargetNNR, 948 NestedNameSpecifier *TargetNNS, 949 SourceLocation TargetNameLoc, 950 DeclarationName TargetName) { 951 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, 952 TargetNNR, TargetNNS, 953 TargetNameLoc, TargetName); 954} 955 956UnresolvedUsingTypenameDecl * 957UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, 958 SourceLocation UsingLoc, 959 SourceLocation TypenameLoc, 960 SourceRange TargetNNR, 961 NestedNameSpecifier *TargetNNS, 962 SourceLocation TargetNameLoc, 963 DeclarationName TargetName) { 964 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, 965 TargetNNR, TargetNNS, 966 TargetNameLoc, 967 TargetName.getAsIdentifierInfo()); 968} 969 970StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, 971 SourceLocation L, Expr *AssertExpr, 972 StringLiteral *Message) { 973 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); 974} 975 976void StaticAssertDecl::Destroy(ASTContext& C) { 977 AssertExpr->Destroy(C); 978 Message->Destroy(C); 979 this->~StaticAssertDecl(); 980 C.Deallocate((void *)this); 981} 982 983StaticAssertDecl::~StaticAssertDecl() { 984} 985 986static const char *getAccessName(AccessSpecifier AS) { 987 switch (AS) { 988 default: 989 case AS_none: 990 assert("Invalid access specifier!"); 991 return 0; 992 case AS_public: 993 return "public"; 994 case AS_private: 995 return "private"; 996 case AS_protected: 997 return "protected"; 998 } 999} 1000 1001const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, 1002 AccessSpecifier AS) { 1003 return DB << getAccessName(AS); 1004} 1005 1006 1007