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