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