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