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