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