DeclCXX.cpp revision 5e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766
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" 20using namespace clang; 21 22//===----------------------------------------------------------------------===// 23// Decl Allocation/Deallocation Method Implementations 24//===----------------------------------------------------------------------===// 25 26CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, 27 SourceLocation L, IdentifierInfo *Id, 28 SourceLocation TKL) 29 : RecordDecl(K, TK, DC, L, Id, TKL), 30 UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), 31 UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false), 32 Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false), 33 HasTrivialConstructor(true), HasTrivialCopyConstructor(true), 34 HasTrivialCopyAssignment(true), HasTrivialDestructor(true), 35 Bases(0), NumBases(0), VBases(0), NumVBases(0), 36 Conversions(DC, DeclarationName()), 37 TemplateOrInstantiation() { } 38 39CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, 40 SourceLocation L, IdentifierInfo *Id, 41 SourceLocation TKL, 42 CXXRecordDecl* PrevDecl, 43 bool DelayTypeCreation) { 44 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id, TKL); 45 if (!DelayTypeCreation) 46 C.getTypeDeclType(R, PrevDecl); 47 return R; 48} 49 50CXXRecordDecl::~CXXRecordDecl() { 51} 52 53void CXXRecordDecl::Destroy(ASTContext &C) { 54 C.Deallocate(Bases); 55 C.Deallocate(VBases); 56 this->RecordDecl::Destroy(C); 57} 58 59void 60CXXRecordDecl::setBases(ASTContext &C, 61 CXXBaseSpecifier const * const *Bases, 62 unsigned NumBases) { 63 // C++ [dcl.init.aggr]p1: 64 // An aggregate is an array or a class (clause 9) with [...] 65 // no base classes [...]. 66 Aggregate = false; 67 68 if (this->Bases) 69 C.Deallocate(this->Bases); 70 71 int vbaseCount = 0; 72 llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases; 73 bool hasDirectVirtualBase = false; 74 75 this->Bases = new(C) CXXBaseSpecifier [NumBases]; 76 this->NumBases = NumBases; 77 for (unsigned i = 0; i < NumBases; ++i) { 78 this->Bases[i] = *Bases[i]; 79 // Keep track of inherited vbases for this base class. 80 const CXXBaseSpecifier *Base = Bases[i]; 81 QualType BaseType = Base->getType(); 82 // Skip template types. 83 // FIXME. This means that this list must be rebuilt during template 84 // instantiation. 85 if (BaseType->isDependentType()) 86 continue; 87 CXXRecordDecl *BaseClassDecl 88 = cast<CXXRecordDecl>(BaseType->getAsRecordType()->getDecl()); 89 if (Base->isVirtual()) 90 hasDirectVirtualBase = true; 91 for (CXXRecordDecl::base_class_iterator VBase = 92 BaseClassDecl->vbases_begin(), 93 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { 94 // Add this vbase to the array of vbases for current class if it is 95 // not already in the list. 96 // FIXME. Note that we do a linear search as number of such classes are 97 // very few. 98 int i; 99 for (i = 0; i < vbaseCount; ++i) 100 if (UniqueVbases[i]->getType() == VBase->getType()) 101 break; 102 if (i == vbaseCount) { 103 UniqueVbases.push_back(VBase); 104 ++vbaseCount; 105 } 106 } 107 } 108 if (hasDirectVirtualBase) { 109 // Iterate one more time through the direct bases and add the virtual 110 // base to the list of vritual bases for current class. 111 for (unsigned i = 0; i < NumBases; ++i) { 112 const CXXBaseSpecifier *VBase = Bases[i]; 113 if (!VBase->isVirtual()) 114 continue; 115 int j; 116 for (j = 0; j < vbaseCount; ++j) 117 if (UniqueVbases[j]->getType() == VBase->getType()) 118 break; 119 if (j == vbaseCount) { 120 UniqueVbases.push_back(VBase); 121 ++vbaseCount; 122 } 123 } 124 } 125 if (vbaseCount > 0) { 126 // build AST for inhireted, direct or indirect, virtual bases. 127 this->VBases = new (C) CXXBaseSpecifier [vbaseCount]; 128 this->NumVBases = vbaseCount; 129 for (int i = 0; i < vbaseCount; i++) { 130 QualType QT = UniqueVbases[i]->getType(); 131 CXXRecordDecl *VBaseClassDecl 132 = cast<CXXRecordDecl>(QT->getAsRecordType()->getDecl()); 133 this->VBases[i] = 134 CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true, 135 VBaseClassDecl->getTagKind() == RecordDecl::TK_class, 136 UniqueVbases[i]->getAccessSpecifier(), QT); 137 } 138 } 139} 140 141bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const { 142 return getCopyConstructor(Context, QualType::Const) != 0; 143} 144 145CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context, 146 unsigned TypeQuals) const{ 147 QualType ClassType 148 = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); 149 DeclarationName ConstructorName 150 = Context.DeclarationNames.getCXXConstructorName( 151 Context.getCanonicalType(ClassType)); 152 unsigned FoundTQs; 153 DeclContext::lookup_const_iterator Con, ConEnd; 154 for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName); 155 Con != ConEnd; ++Con) { 156 if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, 157 FoundTQs)) { 158 if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) || 159 (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const))) 160 return cast<CXXConstructorDecl>(*Con); 161 162 } 163 } 164 return 0; 165} 166 167bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const { 168 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( 169 const_cast<CXXRecordDecl*>(this))); 170 DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal); 171 172 DeclContext::lookup_const_iterator Op, OpEnd; 173 for (llvm::tie(Op, OpEnd) = this->lookup(OpName); 174 Op != OpEnd; ++Op) { 175 // C++ [class.copy]p9: 176 // A user-declared copy assignment operator is a non-static non-template 177 // member function of class X with exactly one parameter of type X, X&, 178 // const X&, volatile X& or const volatile X&. 179 const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op); 180 if (Method->isStatic()) 181 continue; 182 // TODO: Skip templates? Or is this implicitly done due to parameter types? 183 const FunctionProtoType *FnType = 184 Method->getType()->getAsFunctionProtoType(); 185 assert(FnType && "Overloaded operator has no prototype."); 186 // Don't assert on this; an invalid decl might have been left in the AST. 187 if (FnType->getNumArgs() != 1 || FnType->isVariadic()) 188 continue; 189 bool AcceptsConst = true; 190 QualType ArgType = FnType->getArgType(0); 191 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) { 192 ArgType = Ref->getPointeeType(); 193 // Is it a non-const lvalue reference? 194 if (!ArgType.isConstQualified()) 195 AcceptsConst = false; 196 } 197 if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType) 198 continue; 199 200 // We have a single argument of type cv X or cv X&, i.e. we've found the 201 // copy assignment operator. Return whether it accepts const arguments. 202 return AcceptsConst; 203 } 204 assert(isInvalidDecl() && 205 "No copy assignment operator declared in valid code."); 206 return false; 207} 208 209void 210CXXRecordDecl::addedConstructor(ASTContext &Context, 211 CXXConstructorDecl *ConDecl) { 212 assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl"); 213 // Note that we have a user-declared constructor. 214 UserDeclaredConstructor = true; 215 216 // C++ [dcl.init.aggr]p1: 217 // An aggregate is an array or a class (clause 9) with no 218 // user-declared constructors (12.1) [...]. 219 Aggregate = false; 220 221 // C++ [class]p4: 222 // A POD-struct is an aggregate class [...] 223 PlainOldData = false; 224 225 // C++ [class.ctor]p5: 226 // A constructor is trivial if it is an implicitly-declared default 227 // constructor. 228 // FIXME: C++0x: don't do this for "= default" default constructors. 229 HasTrivialConstructor = false; 230 231 // Note when we have a user-declared copy constructor, which will 232 // suppress the implicit declaration of a copy constructor. 233 if (ConDecl->isCopyConstructor(Context)) { 234 UserDeclaredCopyConstructor = true; 235 236 // C++ [class.copy]p6: 237 // A copy constructor is trivial if it is implicitly declared. 238 // FIXME: C++0x: don't do this for "= default" copy constructors. 239 HasTrivialCopyConstructor = false; 240 } 241} 242 243void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context, 244 CXXMethodDecl *OpDecl) { 245 // We're interested specifically in copy assignment operators. 246 const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType(); 247 assert(FnType && "Overloaded operator has no proto function type."); 248 assert(FnType->getNumArgs() == 1 && !FnType->isVariadic()); 249 QualType ArgType = FnType->getArgType(0); 250 if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) 251 ArgType = Ref->getPointeeType(); 252 253 ArgType = ArgType.getUnqualifiedType(); 254 QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( 255 const_cast<CXXRecordDecl*>(this))); 256 257 if (ClassType != Context.getCanonicalType(ArgType)) 258 return; 259 260 // This is a copy assignment operator. 261 // Suppress the implicit declaration of a copy constructor. 262 UserDeclaredCopyAssignment = true; 263 264 // C++ [class.copy]p11: 265 // A copy assignment operator is trivial if it is implicitly declared. 266 // FIXME: C++0x: don't do this for "= default" copy operators. 267 HasTrivialCopyAssignment = false; 268 269 // C++ [class]p4: 270 // A POD-struct is an aggregate class that [...] has no user-defined copy 271 // assignment operator [...]. 272 PlainOldData = false; 273} 274 275void CXXRecordDecl::addConversionFunction(ASTContext &Context, 276 CXXConversionDecl *ConvDecl) { 277 Conversions.addOverload(ConvDecl); 278} 279 280 281CXXConstructorDecl * 282CXXRecordDecl::getDefaultConstructor(ASTContext &Context) { 283 QualType ClassType = Context.getTypeDeclType(this); 284 DeclarationName ConstructorName 285 = Context.DeclarationNames.getCXXConstructorName( 286 Context.getCanonicalType(ClassType.getUnqualifiedType())); 287 288 DeclContext::lookup_const_iterator Con, ConEnd; 289 for (llvm::tie(Con, ConEnd) = lookup(ConstructorName); 290 Con != ConEnd; ++Con) { 291 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); 292 if (Constructor->isDefaultConstructor()) 293 return Constructor; 294 } 295 return 0; 296} 297 298const CXXDestructorDecl * 299CXXRecordDecl::getDestructor(ASTContext &Context) { 300 QualType ClassType = Context.getTypeDeclType(this); 301 302 DeclarationName Name 303 = Context.DeclarationNames.getCXXDestructorName(ClassType); 304 305 DeclContext::lookup_iterator I, E; 306 llvm::tie(I, E) = lookup(Name); 307 assert(I != E && "Did not find a destructor!"); 308 309 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); 310 assert(++I == E && "Found more than one destructor!"); 311 312 return Dtor; 313} 314 315CXXMethodDecl * 316CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, 317 SourceLocation L, DeclarationName N, 318 QualType T, bool isStatic, bool isInline) { 319 return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline); 320} 321 322 323typedef llvm::DenseMap<const CXXMethodDecl*, 324 std::vector<const CXXMethodDecl *> *> 325 OverriddenMethodsMapTy; 326 327static OverriddenMethodsMapTy *OverriddenMethods = 0; 328 329void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { 330 // FIXME: The CXXMethodDecl dtor needs to remove and free the entry. 331 332 if (!OverriddenMethods) 333 OverriddenMethods = new OverriddenMethodsMapTy(); 334 335 std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this]; 336 if (!Methods) 337 Methods = new std::vector<const CXXMethodDecl *>; 338 339 Methods->push_back(MD); 340} 341 342CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { 343 if (!OverriddenMethods) 344 return 0; 345 346 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); 347 if (it == OverriddenMethods->end()) 348 return 0; 349 return &(*it->second)[0]; 350} 351 352CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { 353 if (!OverriddenMethods) 354 return 0; 355 356 OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); 357 if (it == OverriddenMethods->end()) 358 return 0; 359 360 return &(*it->second)[it->second->size()]; 361} 362 363QualType CXXMethodDecl::getThisType(ASTContext &C) const { 364 // C++ 9.3.2p1: The type of this in a member function of a class X is X*. 365 // If the member function is declared const, the type of this is const X*, 366 // if the member function is declared volatile, the type of this is 367 // volatile X*, and if the member function is declared const volatile, 368 // the type of this is const volatile X*. 369 370 assert(isInstance() && "No 'this' for static methods!"); 371 372 QualType ClassTy; 373 if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate()) 374 ClassTy = TD->getInjectedClassNameType(C); 375 else 376 ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); 377 ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers()); 378 return C.getPointerType(ClassTy); 379} 380 381CXXBaseOrMemberInitializer:: 382CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs, 383 CXXConstructorDecl *C, 384 SourceLocation L) 385 : Args(0), NumArgs(0), IdLoc(L) { 386 BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr()); 387 assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer"); 388 BaseOrMember |= 0x01; 389 390 if (NumArgs > 0) { 391 this->NumArgs = NumArgs; 392 this->Args = new Expr*[NumArgs]; 393 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 394 this->Args[Idx] = Args[Idx]; 395 } 396 CtorToCall = C; 397} 398 399CXXBaseOrMemberInitializer:: 400CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs, 401 CXXConstructorDecl *C, 402 SourceLocation L) 403 : Args(0), NumArgs(0), IdLoc(L) { 404 BaseOrMember = reinterpret_cast<uintptr_t>(Member); 405 assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer"); 406 407 if (NumArgs > 0) { 408 this->NumArgs = NumArgs; 409 this->Args = new Expr*[NumArgs]; 410 for (unsigned Idx = 0; Idx < NumArgs; ++Idx) 411 this->Args[Idx] = Args[Idx]; 412 } 413 CtorToCall = C; 414} 415 416CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() { 417 delete [] Args; 418} 419 420CXXConstructorDecl * 421CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 422 SourceLocation L, DeclarationName N, 423 QualType T, bool isExplicit, 424 bool isInline, bool isImplicitlyDeclared) { 425 assert(N.getNameKind() == DeclarationName::CXXConstructorName && 426 "Name must refer to a constructor"); 427 return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline, 428 isImplicitlyDeclared); 429} 430 431bool CXXConstructorDecl::isDefaultConstructor() const { 432 // C++ [class.ctor]p5: 433 // A default constructor for a class X is a constructor of class 434 // X that can be called without an argument. 435 return (getNumParams() == 0) || 436 (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0); 437} 438 439bool 440CXXConstructorDecl::isCopyConstructor(ASTContext &Context, 441 unsigned &TypeQuals) const { 442 // C++ [class.copy]p2: 443 // A non-template constructor for class X is a copy constructor 444 // if its first parameter is of type X&, const X&, volatile X& or 445 // const volatile X&, and either there are no other parameters 446 // or else all other parameters have default arguments (8.3.6). 447 if ((getNumParams() < 1) || 448 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg())) 449 return false; 450 451 const ParmVarDecl *Param = getParamDecl(0); 452 453 // Do we have a reference type? Rvalue references don't count. 454 const LValueReferenceType *ParamRefType = 455 Param->getType()->getAsLValueReferenceType(); 456 if (!ParamRefType) 457 return false; 458 459 // Is it a reference to our class type? 460 QualType PointeeType 461 = Context.getCanonicalType(ParamRefType->getPointeeType()); 462 QualType ClassTy 463 = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); 464 if (PointeeType.getUnqualifiedType() != ClassTy) 465 return false; 466 467 // We have a copy constructor. 468 TypeQuals = PointeeType.getCVRQualifiers(); 469 return true; 470} 471 472bool CXXConstructorDecl::isConvertingConstructor() const { 473 // C++ [class.conv.ctor]p1: 474 // A constructor declared without the function-specifier explicit 475 // that can be called with a single parameter specifies a 476 // conversion from the type of its first parameter to the type of 477 // its class. Such a constructor is called a converting 478 // constructor. 479 if (isExplicit()) 480 return false; 481 482 return (getNumParams() == 0 && 483 getType()->getAsFunctionProtoType()->isVariadic()) || 484 (getNumParams() == 1) || 485 (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); 486} 487 488CXXDestructorDecl * 489CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, 490 SourceLocation L, DeclarationName N, 491 QualType T, bool isInline, 492 bool isImplicitlyDeclared) { 493 assert(N.getNameKind() == DeclarationName::CXXDestructorName && 494 "Name must refer to a destructor"); 495 return new (C) CXXDestructorDecl(RD, L, N, T, isInline, 496 isImplicitlyDeclared); 497} 498 499void 500CXXDestructorDecl::Destroy(ASTContext& C) { 501 C.Deallocate(BaseOrMemberDestructions); 502 CXXMethodDecl::Destroy(C); 503} 504 505void 506CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) { 507 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext()); 508 llvm::SmallVector<uintptr_t, 32> AllToDestruct; 509 510 for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), 511 E = ClassDecl->vbases_end(); VBase != E; ++VBase) { 512 // Skip over virtual bases which have trivial destructors. 513 CXXRecordDecl *BaseClassDecl 514 = cast<CXXRecordDecl>(VBase->getType()->getAsRecordType()->getDecl()); 515 if (BaseClassDecl->hasTrivialDestructor()) 516 continue; 517 uintptr_t Member = 518 reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | VBASE; 519 AllToDestruct.push_back(Member); 520 } 521 for (CXXRecordDecl::base_class_iterator Base = 522 ClassDecl->bases_begin(), 523 E = ClassDecl->bases_end(); Base != E; ++Base) { 524 if (Base->isVirtual()) 525 continue; 526 // Skip over virtual bases which have trivial destructors. 527 CXXRecordDecl *BaseClassDecl 528 = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl()); 529 if (BaseClassDecl->hasTrivialDestructor()) 530 continue; 531 532 uintptr_t Member = 533 reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | DRCTNONVBASE; 534 AllToDestruct.push_back(Member); 535 } 536 537 // non-static data members. 538 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 539 E = ClassDecl->field_end(); Field != E; ++Field) { 540 QualType FieldType = C.getBaseElementType((*Field)->getType()); 541 542 if (const RecordType* RT = FieldType->getAsRecordType()) { 543 // Skip over virtual bases which have trivial destructors. 544 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 545 if (BaseClassDecl->hasTrivialDestructor()) 546 continue; 547 uintptr_t Member = reinterpret_cast<uintptr_t>(*Field); 548 AllToDestruct.push_back(Member); 549 } 550 } 551 552 unsigned NumDestructions = AllToDestruct.size(); 553 if (NumDestructions > 0) { 554 NumBaseOrMemberDestructions = NumDestructions; 555 BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions]; 556 // Insert in reverse order. 557 for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx) 558 BaseOrMemberDestructions[i++] = AllToDestruct[Idx]; 559 } 560} 561 562void 563CXXConstructorDecl::setBaseOrMemberInitializers( 564 ASTContext &C, 565 CXXBaseOrMemberInitializer **Initializers, 566 unsigned NumInitializers, 567 llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases, 568 llvm::SmallVectorImpl<FieldDecl *>&Fields) { 569 // We need to build the initializer AST according to order of construction 570 // and not what user specified in the Initializers list. 571 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext()); 572 llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; 573 llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; 574 575 for (unsigned i = 0; i < NumInitializers; i++) { 576 CXXBaseOrMemberInitializer *Member = Initializers[i]; 577 const void * Key = Member->isBaseInitializer() ? 578 reinterpret_cast<const void *>( 579 Member->getBaseClass()->getAsRecordType()) : 580 reinterpret_cast<const void *>(Member->getMember()); 581 AllBaseFields[Key] = Member; 582 } 583 584 // Push virtual bases before others. 585 for (CXXRecordDecl::base_class_iterator VBase = 586 ClassDecl->vbases_begin(), 587 E = ClassDecl->vbases_end(); VBase != E; ++VBase) { 588 const void *Key = reinterpret_cast<const void *>( 589 VBase->getType()->getAsRecordType()); 590 if (AllBaseFields[Key]) 591 AllToInit.push_back(AllBaseFields[Key]); 592 else { 593 CXXRecordDecl *VBaseDecl = 594 cast<CXXRecordDecl>(VBase->getType()->getAsRecordType()->getDecl()); 595 assert(VBaseDecl && "setBaseOrMemberInitializers - VBaseDecl null"); 596 if (!VBaseDecl->getDefaultConstructor(C) && 597 !VBase->getType()->isDependentType()) 598 Bases.push_back(VBase); 599 CXXBaseOrMemberInitializer *Member = 600 new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0, 601 VBaseDecl->getDefaultConstructor(C), 602 SourceLocation()); 603 AllToInit.push_back(Member); 604 } 605 } 606 607 for (CXXRecordDecl::base_class_iterator Base = 608 ClassDecl->bases_begin(), 609 E = ClassDecl->bases_end(); Base != E; ++Base) { 610 // Virtuals are in the virtual base list and already constructed. 611 if (Base->isVirtual()) 612 continue; 613 const void *Key = reinterpret_cast<const void *>( 614 Base->getType()->getAsRecordType()); 615 if (AllBaseFields[Key]) 616 AllToInit.push_back(AllBaseFields[Key]); 617 else { 618 CXXRecordDecl *BaseDecl = 619 cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl()); 620 assert(BaseDecl && "setBaseOrMemberInitializers - BaseDecl null"); 621 if (!BaseDecl->getDefaultConstructor(C) && 622 !Base->getType()->isDependentType()) 623 Bases.push_back(Base); 624 CXXBaseOrMemberInitializer *Member = 625 new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0, 626 BaseDecl->getDefaultConstructor(C), 627 SourceLocation()); 628 AllToInit.push_back(Member); 629 } 630 } 631 632 // non-static data members. 633 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 634 E = ClassDecl->field_end(); Field != E; ++Field) { 635 const void * Key = reinterpret_cast<const void *>(*Field); 636 if (AllBaseFields[Key]) { 637 AllToInit.push_back(AllBaseFields[Key]); 638 continue; 639 } 640 641 QualType FT = C.getBaseElementType((*Field)->getType()); 642 if (const RecordType* RT = FT->getAsRecordType()) { 643 CXXConstructorDecl *Ctor = 644 cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(C); 645 if (!Ctor && !FT->isDependentType()) 646 Fields.push_back(*Field); 647 CXXBaseOrMemberInitializer *Member = 648 new (C) CXXBaseOrMemberInitializer((*Field), 0, 0, 649 Ctor, 650 SourceLocation()); 651 AllToInit.push_back(Member); 652 } 653 } 654 655 NumInitializers = AllToInit.size(); 656 if (NumInitializers > 0) { 657 NumBaseOrMemberInitializers = NumInitializers; 658 BaseOrMemberInitializers = 659 new (C) CXXBaseOrMemberInitializer*[NumInitializers]; 660 for (unsigned Idx = 0; Idx < NumInitializers; ++Idx) 661 BaseOrMemberInitializers[Idx] = AllToInit[Idx]; 662 } 663} 664 665void 666CXXConstructorDecl::Destroy(ASTContext& C) { 667 C.Deallocate(BaseOrMemberInitializers); 668 CXXMethodDecl::Destroy(C); 669} 670 671CXXConversionDecl * 672CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, 673 SourceLocation L, DeclarationName N, 674 QualType T, bool isInline, bool isExplicit) { 675 assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName && 676 "Name must refer to a conversion function"); 677 return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit); 678} 679 680OverloadedFunctionDecl * 681OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC, 682 DeclarationName N) { 683 return new (C) OverloadedFunctionDecl(DC, N); 684} 685 686void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) { 687 Functions.push_back(F); 688 this->setLocation(F.get()->getLocation()); 689} 690 691OverloadIterator::reference OverloadIterator::operator*() const { 692 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 693 return FD; 694 695 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) 696 return FTD; 697 698 assert(isa<OverloadedFunctionDecl>(D)); 699 return *Iter; 700} 701 702OverloadIterator &OverloadIterator::operator++() { 703 if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { 704 D = 0; 705 return *this; 706 } 707 708 if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end()) 709 D = 0; 710 711 return *this; 712} 713 714bool OverloadIterator::Equals(const OverloadIterator &Other) const { 715 if (!D || !Other.D) 716 return D == Other.D; 717 718 if (D != Other.D) 719 return false; 720 721 return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter; 722} 723 724LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, 725 DeclContext *DC, 726 SourceLocation L, 727 LanguageIDs Lang, bool Braces) { 728 return new (C) LinkageSpecDecl(DC, L, Lang, Braces); 729} 730 731UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, 732 SourceLocation L, 733 SourceLocation NamespaceLoc, 734 SourceRange QualifierRange, 735 NestedNameSpecifier *Qualifier, 736 SourceLocation IdentLoc, 737 NamespaceDecl *Used, 738 DeclContext *CommonAncestor) { 739 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange, 740 Qualifier, IdentLoc, Used, CommonAncestor); 741} 742 743NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, 744 SourceLocation L, 745 SourceLocation AliasLoc, 746 IdentifierInfo *Alias, 747 SourceRange QualifierRange, 748 NestedNameSpecifier *Qualifier, 749 SourceLocation IdentLoc, 750 NamedDecl *Namespace) { 751 return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange, 752 Qualifier, IdentLoc, Namespace); 753} 754 755UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, 756 SourceLocation L, SourceRange NNR, SourceLocation TargetNL, 757 SourceLocation UL, NamedDecl* Target, 758 NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) { 759 return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target, 760 TargetNNS, IsTypeNameArg); 761} 762 763StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, 764 SourceLocation L, Expr *AssertExpr, 765 StringLiteral *Message) { 766 return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); 767} 768 769void StaticAssertDecl::Destroy(ASTContext& C) { 770 AssertExpr->Destroy(C); 771 Message->Destroy(C); 772 this->~StaticAssertDecl(); 773 C.Deallocate((void *)this); 774} 775 776StaticAssertDecl::~StaticAssertDecl() { 777} 778 779static const char *getAccessName(AccessSpecifier AS) { 780 switch (AS) { 781 default: 782 case AS_none: 783 assert("Invalid access specifier!"); 784 return 0; 785 case AS_public: 786 return "public"; 787 case AS_private: 788 return "private"; 789 case AS_protected: 790 return "protected"; 791 } 792} 793 794const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, 795 AccessSpecifier AS) { 796 return DB << getAccessName(AS); 797} 798 799 800