CGVTables.cpp revision a14f5979572aa25c03d24750ee4724d2031d4ede
1//===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===// 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 contains code dealing with C++ code generation of virtual tables. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CodeGenModule.h" 15#include "CodeGenFunction.h" 16#include "CGCXXABI.h" 17#include "clang/AST/CXXInheritance.h" 18#include "clang/AST/RecordLayout.h" 19#include "clang/Frontend/CodeGenOptions.h" 20#include "llvm/ADT/DenseSet.h" 21#include "llvm/ADT/SetVector.h" 22#include "llvm/Support/Compiler.h" 23#include "llvm/Support/Format.h" 24#include <algorithm> 25#include <cstdio> 26 27using namespace clang; 28using namespace CodeGen; 29 30namespace { 31 32/// BaseOffset - Represents an offset from a derived class to a direct or 33/// indirect base class. 34struct BaseOffset { 35 /// DerivedClass - The derived class. 36 const CXXRecordDecl *DerivedClass; 37 38 /// VirtualBase - If the path from the derived class to the base class 39 /// involves a virtual base class, this holds its declaration. 40 const CXXRecordDecl *VirtualBase; 41 42 /// NonVirtualOffset - The offset from the derived class to the base class. 43 /// (Or the offset from the virtual base class to the base class, if the 44 /// path from the derived class to the base class involves a virtual base 45 /// class. 46 int64_t NonVirtualOffset; 47 48 BaseOffset() : DerivedClass(0), VirtualBase(0), NonVirtualOffset(0) { } 49 BaseOffset(const CXXRecordDecl *DerivedClass, 50 const CXXRecordDecl *VirtualBase, int64_t NonVirtualOffset) 51 : DerivedClass(DerivedClass), VirtualBase(VirtualBase), 52 NonVirtualOffset(NonVirtualOffset) { } 53 54 bool isEmpty() const { return !NonVirtualOffset && !VirtualBase; } 55}; 56 57/// FinalOverriders - Contains the final overrider member functions for all 58/// member functions in the base subobjects of a class. 59class FinalOverriders { 60public: 61 /// OverriderInfo - Information about a final overrider. 62 struct OverriderInfo { 63 /// Method - The method decl of the overrider. 64 const CXXMethodDecl *Method; 65 66 /// Offset - the base offset of the overrider in the layout class. 67 uint64_t Offset; 68 69 OverriderInfo() : Method(0), Offset(0) { } 70 }; 71 72private: 73 /// MostDerivedClass - The most derived class for which the final overriders 74 /// are stored. 75 const CXXRecordDecl *MostDerivedClass; 76 77 /// MostDerivedClassOffset - If we're building final overriders for a 78 /// construction vtable, this holds the offset from the layout class to the 79 /// most derived class. 80 const uint64_t MostDerivedClassOffset; 81 82 /// LayoutClass - The class we're using for layout information. Will be 83 /// different than the most derived class if the final overriders are for a 84 /// construction vtable. 85 const CXXRecordDecl *LayoutClass; 86 87 ASTContext &Context; 88 89 /// MostDerivedClassLayout - the AST record layout of the most derived class. 90 const ASTRecordLayout &MostDerivedClassLayout; 91 92 /// MethodBaseOffsetPairTy - Uniquely identifies a member function 93 /// in a base subobject. 94 typedef std::pair<const CXXMethodDecl *, uint64_t> MethodBaseOffsetPairTy; 95 96 typedef llvm::DenseMap<MethodBaseOffsetPairTy, 97 OverriderInfo> OverridersMapTy; 98 99 /// OverridersMap - The final overriders for all virtual member functions of 100 /// all the base subobjects of the most derived class. 101 OverridersMapTy OverridersMap; 102 103 /// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented 104 /// as a record decl and a subobject number) and its offsets in the most 105 /// derived class as well as the layout class. 106 typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>, 107 uint64_t> SubobjectOffsetMapTy; 108 109 typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy; 110 111 /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the 112 /// given base. 113 void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, 114 uint64_t OffsetInLayoutClass, 115 SubobjectOffsetMapTy &SubobjectOffsets, 116 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, 117 SubobjectCountMapTy &SubobjectCounts); 118 119 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; 120 121 /// dump - dump the final overriders for a base subobject, and all its direct 122 /// and indirect base subobjects. 123 void dump(llvm::raw_ostream &Out, BaseSubobject Base, 124 VisitedVirtualBasesSetTy& VisitedVirtualBases); 125 126public: 127 FinalOverriders(const CXXRecordDecl *MostDerivedClass, 128 uint64_t MostDerivedClassOffset, 129 const CXXRecordDecl *LayoutClass); 130 131 /// getOverrider - Get the final overrider for the given method declaration in 132 /// the subobject with the given base offset. 133 OverriderInfo getOverrider(const CXXMethodDecl *MD, 134 uint64_t BaseOffset) const { 135 assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) && 136 "Did not find overrider!"); 137 138 return OverridersMap.lookup(std::make_pair(MD, BaseOffset)); 139 } 140 141 /// dump - dump the final overriders. 142 void dump() { 143 VisitedVirtualBasesSetTy VisitedVirtualBases; 144 dump(llvm::errs(), BaseSubobject(MostDerivedClass, 0), VisitedVirtualBases); 145 } 146 147}; 148 149#define DUMP_OVERRIDERS 0 150 151FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass, 152 uint64_t MostDerivedClassOffset, 153 const CXXRecordDecl *LayoutClass) 154 : MostDerivedClass(MostDerivedClass), 155 MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass), 156 Context(MostDerivedClass->getASTContext()), 157 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) { 158 159 // Compute base offsets. 160 SubobjectOffsetMapTy SubobjectOffsets; 161 SubobjectOffsetMapTy SubobjectLayoutClassOffsets; 162 SubobjectCountMapTy SubobjectCounts; 163 ComputeBaseOffsets(BaseSubobject(MostDerivedClass, 0), /*IsVirtual=*/false, 164 MostDerivedClassOffset, SubobjectOffsets, 165 SubobjectLayoutClassOffsets, SubobjectCounts); 166 167 // Get the the final overriders. 168 CXXFinalOverriderMap FinalOverriders; 169 MostDerivedClass->getFinalOverriders(FinalOverriders); 170 171 for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(), 172 E = FinalOverriders.end(); I != E; ++I) { 173 const CXXMethodDecl *MD = I->first; 174 const OverridingMethods& Methods = I->second; 175 176 for (OverridingMethods::const_iterator I = Methods.begin(), 177 E = Methods.end(); I != E; ++I) { 178 unsigned SubobjectNumber = I->first; 179 assert(SubobjectOffsets.count(std::make_pair(MD->getParent(), 180 SubobjectNumber)) && 181 "Did not find subobject offset!"); 182 183 uint64_t BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(), 184 SubobjectNumber)]; 185 186 assert(I->second.size() == 1 && "Final overrider is not unique!"); 187 const UniqueVirtualMethod &Method = I->second.front(); 188 189 const CXXRecordDecl *OverriderRD = Method.Method->getParent(); 190 assert(SubobjectLayoutClassOffsets.count( 191 std::make_pair(OverriderRD, Method.Subobject)) 192 && "Did not find subobject offset!"); 193 uint64_t OverriderOffset = 194 SubobjectLayoutClassOffsets[std::make_pair(OverriderRD, 195 Method.Subobject)]; 196 197 OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)]; 198 assert(!Overrider.Method && "Overrider should not exist yet!"); 199 200 Overrider.Offset = OverriderOffset; 201 Overrider.Method = Method.Method; 202 } 203 } 204 205#if DUMP_OVERRIDERS 206 // And dump them (for now). 207 dump(); 208#endif 209} 210 211static BaseOffset ComputeBaseOffset(ASTContext &Context, 212 const CXXRecordDecl *DerivedRD, 213 const CXXBasePath &Path) { 214 int64_t NonVirtualOffset = 0; 215 216 unsigned NonVirtualStart = 0; 217 const CXXRecordDecl *VirtualBase = 0; 218 219 // First, look for the virtual base class. 220 for (unsigned I = 0, E = Path.size(); I != E; ++I) { 221 const CXXBasePathElement &Element = Path[I]; 222 223 if (Element.Base->isVirtual()) { 224 // FIXME: Can we break when we find the first virtual base? 225 // (If we can't, can't we just iterate over the path in reverse order?) 226 NonVirtualStart = I + 1; 227 QualType VBaseType = Element.Base->getType(); 228 VirtualBase = 229 cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); 230 } 231 } 232 233 // Now compute the non-virtual offset. 234 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) { 235 const CXXBasePathElement &Element = Path[I]; 236 237 // Check the base class offset. 238 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); 239 240 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>(); 241 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl()); 242 243 NonVirtualOffset += Layout.getBaseClassOffsetInBits(Base); 244 } 245 246 // FIXME: This should probably use CharUnits or something. Maybe we should 247 // even change the base offsets in ASTRecordLayout to be specified in 248 // CharUnits. 249 return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset / 8); 250 251} 252 253static BaseOffset ComputeBaseOffset(ASTContext &Context, 254 const CXXRecordDecl *BaseRD, 255 const CXXRecordDecl *DerivedRD) { 256 CXXBasePaths Paths(/*FindAmbiguities=*/false, 257 /*RecordPaths=*/true, /*DetectVirtual=*/false); 258 259 if (!const_cast<CXXRecordDecl *>(DerivedRD)-> 260 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) { 261 assert(false && "Class must be derived from the passed in base class!"); 262 return BaseOffset(); 263 } 264 265 return ComputeBaseOffset(Context, DerivedRD, Paths.front()); 266} 267 268static BaseOffset 269ComputeReturnAdjustmentBaseOffset(ASTContext &Context, 270 const CXXMethodDecl *DerivedMD, 271 const CXXMethodDecl *BaseMD) { 272 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>(); 273 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>(); 274 275 // Canonicalize the return types. 276 CanQualType CanDerivedReturnType = 277 Context.getCanonicalType(DerivedFT->getResultType()); 278 CanQualType CanBaseReturnType = 279 Context.getCanonicalType(BaseFT->getResultType()); 280 281 assert(CanDerivedReturnType->getTypeClass() == 282 CanBaseReturnType->getTypeClass() && 283 "Types must have same type class!"); 284 285 if (CanDerivedReturnType == CanBaseReturnType) { 286 // No adjustment needed. 287 return BaseOffset(); 288 } 289 290 if (isa<ReferenceType>(CanDerivedReturnType)) { 291 CanDerivedReturnType = 292 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType(); 293 CanBaseReturnType = 294 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType(); 295 } else if (isa<PointerType>(CanDerivedReturnType)) { 296 CanDerivedReturnType = 297 CanDerivedReturnType->getAs<PointerType>()->getPointeeType(); 298 CanBaseReturnType = 299 CanBaseReturnType->getAs<PointerType>()->getPointeeType(); 300 } else { 301 assert(false && "Unexpected return type!"); 302 } 303 304 // We need to compare unqualified types here; consider 305 // const T *Base::foo(); 306 // T *Derived::foo(); 307 if (CanDerivedReturnType.getUnqualifiedType() == 308 CanBaseReturnType.getUnqualifiedType()) { 309 // No adjustment needed. 310 return BaseOffset(); 311 } 312 313 const CXXRecordDecl *DerivedRD = 314 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl()); 315 316 const CXXRecordDecl *BaseRD = 317 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl()); 318 319 return ComputeBaseOffset(Context, BaseRD, DerivedRD); 320} 321 322void 323FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, 324 uint64_t OffsetInLayoutClass, 325 SubobjectOffsetMapTy &SubobjectOffsets, 326 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, 327 SubobjectCountMapTy &SubobjectCounts) { 328 const CXXRecordDecl *RD = Base.getBase(); 329 330 unsigned SubobjectNumber = 0; 331 if (!IsVirtual) 332 SubobjectNumber = ++SubobjectCounts[RD]; 333 334 // Set up the subobject to offset mapping. 335 assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber)) 336 && "Subobject offset already exists!"); 337 assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber)) 338 && "Subobject offset already exists!"); 339 340 SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = 341 Base.getBaseOffset(); 342 SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] = 343 OffsetInLayoutClass; 344 345 // Traverse our bases. 346 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 347 E = RD->bases_end(); I != E; ++I) { 348 const CXXRecordDecl *BaseDecl = 349 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 350 351 uint64_t BaseOffset; 352 uint64_t BaseOffsetInLayoutClass; 353 if (I->isVirtual()) { 354 // Check if we've visited this virtual base before. 355 if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0))) 356 continue; 357 358 const ASTRecordLayout &LayoutClassLayout = 359 Context.getASTRecordLayout(LayoutClass); 360 361 BaseOffset = MostDerivedClassLayout.getVBaseClassOffsetInBits(BaseDecl); 362 BaseOffsetInLayoutClass = 363 LayoutClassLayout.getVBaseClassOffsetInBits(BaseDecl); 364 } else { 365 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 366 uint64_t Offset = Layout.getBaseClassOffsetInBits(BaseDecl); 367 368 BaseOffset = Base.getBaseOffset() + Offset; 369 BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset; 370 } 371 372 ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset), I->isVirtual(), 373 BaseOffsetInLayoutClass, SubobjectOffsets, 374 SubobjectLayoutClassOffsets, SubobjectCounts); 375 } 376} 377 378void FinalOverriders::dump(llvm::raw_ostream &Out, BaseSubobject Base, 379 VisitedVirtualBasesSetTy &VisitedVirtualBases) { 380 const CXXRecordDecl *RD = Base.getBase(); 381 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 382 383 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 384 E = RD->bases_end(); I != E; ++I) { 385 const CXXRecordDecl *BaseDecl = 386 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 387 388 // Ignore bases that don't have any virtual member functions. 389 if (!BaseDecl->isPolymorphic()) 390 continue; 391 392 uint64_t BaseOffset; 393 if (I->isVirtual()) { 394 if (!VisitedVirtualBases.insert(BaseDecl)) { 395 // We've visited this base before. 396 continue; 397 } 398 399 BaseOffset = MostDerivedClassLayout.getVBaseClassOffsetInBits(BaseDecl); 400 } else { 401 BaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl) + 402 Base.getBaseOffset(); 403 } 404 405 dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases); 406 } 407 408 Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", "; 409 Out << Base.getBaseOffset() / 8 << ")\n"; 410 411 // Now dump the overriders for this base subobject. 412 for (CXXRecordDecl::method_iterator I = RD->method_begin(), 413 E = RD->method_end(); I != E; ++I) { 414 const CXXMethodDecl *MD = *I; 415 416 if (!MD->isVirtual()) 417 continue; 418 419 OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset()); 420 421 Out << " " << MD->getQualifiedNameAsString() << " - ("; 422 Out << Overrider.Method->getQualifiedNameAsString(); 423 Out << ", " << ", " << Overrider.Offset / 8 << ')'; 424 425 BaseOffset Offset; 426 if (!Overrider.Method->isPure()) 427 Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD); 428 429 if (!Offset.isEmpty()) { 430 Out << " [ret-adj: "; 431 if (Offset.VirtualBase) 432 Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, "; 433 434 Out << Offset.NonVirtualOffset << " nv]"; 435 } 436 437 Out << "\n"; 438 } 439} 440 441/// VTableComponent - Represents a single component in a vtable. 442class VTableComponent { 443public: 444 enum Kind { 445 CK_VCallOffset, 446 CK_VBaseOffset, 447 CK_OffsetToTop, 448 CK_RTTI, 449 CK_FunctionPointer, 450 451 /// CK_CompleteDtorPointer - A pointer to the complete destructor. 452 CK_CompleteDtorPointer, 453 454 /// CK_DeletingDtorPointer - A pointer to the deleting destructor. 455 CK_DeletingDtorPointer, 456 457 /// CK_UnusedFunctionPointer - In some cases, a vtable function pointer 458 /// will end up never being called. Such vtable function pointers are 459 /// represented as a CK_UnusedFunctionPointer. 460 CK_UnusedFunctionPointer 461 }; 462 463 static VTableComponent MakeVCallOffset(int64_t Offset) { 464 return VTableComponent(CK_VCallOffset, Offset); 465 } 466 467 static VTableComponent MakeVBaseOffset(int64_t Offset) { 468 return VTableComponent(CK_VBaseOffset, Offset); 469 } 470 471 static VTableComponent MakeOffsetToTop(int64_t Offset) { 472 return VTableComponent(CK_OffsetToTop, Offset); 473 } 474 475 static VTableComponent MakeRTTI(const CXXRecordDecl *RD) { 476 return VTableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD)); 477 } 478 479 static VTableComponent MakeFunction(const CXXMethodDecl *MD) { 480 assert(!isa<CXXDestructorDecl>(MD) && 481 "Don't use MakeFunction with destructors!"); 482 483 return VTableComponent(CK_FunctionPointer, 484 reinterpret_cast<uintptr_t>(MD)); 485 } 486 487 static VTableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) { 488 return VTableComponent(CK_CompleteDtorPointer, 489 reinterpret_cast<uintptr_t>(DD)); 490 } 491 492 static VTableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) { 493 return VTableComponent(CK_DeletingDtorPointer, 494 reinterpret_cast<uintptr_t>(DD)); 495 } 496 497 static VTableComponent MakeUnusedFunction(const CXXMethodDecl *MD) { 498 assert(!isa<CXXDestructorDecl>(MD) && 499 "Don't use MakeUnusedFunction with destructors!"); 500 return VTableComponent(CK_UnusedFunctionPointer, 501 reinterpret_cast<uintptr_t>(MD)); 502 } 503 504 static VTableComponent getFromOpaqueInteger(uint64_t I) { 505 return VTableComponent(I); 506 } 507 508 /// getKind - Get the kind of this vtable component. 509 Kind getKind() const { 510 return (Kind)(Value & 0x7); 511 } 512 513 int64_t getVCallOffset() const { 514 assert(getKind() == CK_VCallOffset && "Invalid component kind!"); 515 516 return getOffset(); 517 } 518 519 int64_t getVBaseOffset() const { 520 assert(getKind() == CK_VBaseOffset && "Invalid component kind!"); 521 522 return getOffset(); 523 } 524 525 int64_t getOffsetToTop() const { 526 assert(getKind() == CK_OffsetToTop && "Invalid component kind!"); 527 528 return getOffset(); 529 } 530 531 const CXXRecordDecl *getRTTIDecl() const { 532 assert(getKind() == CK_RTTI && "Invalid component kind!"); 533 534 return reinterpret_cast<CXXRecordDecl *>(getPointer()); 535 } 536 537 const CXXMethodDecl *getFunctionDecl() const { 538 assert(getKind() == CK_FunctionPointer); 539 540 return reinterpret_cast<CXXMethodDecl *>(getPointer()); 541 } 542 543 const CXXDestructorDecl *getDestructorDecl() const { 544 assert((getKind() == CK_CompleteDtorPointer || 545 getKind() == CK_DeletingDtorPointer) && "Invalid component kind!"); 546 547 return reinterpret_cast<CXXDestructorDecl *>(getPointer()); 548 } 549 550 const CXXMethodDecl *getUnusedFunctionDecl() const { 551 assert(getKind() == CK_UnusedFunctionPointer); 552 553 return reinterpret_cast<CXXMethodDecl *>(getPointer()); 554 } 555 556private: 557 VTableComponent(Kind ComponentKind, int64_t Offset) { 558 assert((ComponentKind == CK_VCallOffset || 559 ComponentKind == CK_VBaseOffset || 560 ComponentKind == CK_OffsetToTop) && "Invalid component kind!"); 561 assert(Offset <= ((1LL << 56) - 1) && "Offset is too big!"); 562 563 Value = ((Offset << 3) | ComponentKind); 564 } 565 566 VTableComponent(Kind ComponentKind, uintptr_t Ptr) { 567 assert((ComponentKind == CK_RTTI || 568 ComponentKind == CK_FunctionPointer || 569 ComponentKind == CK_CompleteDtorPointer || 570 ComponentKind == CK_DeletingDtorPointer || 571 ComponentKind == CK_UnusedFunctionPointer) && 572 "Invalid component kind!"); 573 574 assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!"); 575 576 Value = Ptr | ComponentKind; 577 } 578 579 int64_t getOffset() const { 580 assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset || 581 getKind() == CK_OffsetToTop) && "Invalid component kind!"); 582 583 return Value >> 3; 584 } 585 586 uintptr_t getPointer() const { 587 assert((getKind() == CK_RTTI || 588 getKind() == CK_FunctionPointer || 589 getKind() == CK_CompleteDtorPointer || 590 getKind() == CK_DeletingDtorPointer || 591 getKind() == CK_UnusedFunctionPointer) && 592 "Invalid component kind!"); 593 594 return static_cast<uintptr_t>(Value & ~7ULL); 595 } 596 597 explicit VTableComponent(uint64_t Value) 598 : Value(Value) { } 599 600 /// The kind is stored in the lower 3 bits of the value. For offsets, we 601 /// make use of the facts that classes can't be larger than 2^55 bytes, 602 /// so we store the offset in the lower part of the 61 bytes that remain. 603 /// (The reason that we're not simply using a PointerIntPair here is that we 604 /// need the offsets to be 64-bit, even when on a 32-bit machine). 605 int64_t Value; 606}; 607 608/// VCallOffsetMap - Keeps track of vcall offsets when building a vtable. 609struct VCallOffsetMap { 610 611 typedef std::pair<const CXXMethodDecl *, int64_t> MethodAndOffsetPairTy; 612 613 /// Offsets - Keeps track of methods and their offsets. 614 // FIXME: This should be a real map and not a vector. 615 llvm::SmallVector<MethodAndOffsetPairTy, 16> Offsets; 616 617 /// MethodsCanShareVCallOffset - Returns whether two virtual member functions 618 /// can share the same vcall offset. 619 static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, 620 const CXXMethodDecl *RHS); 621 622public: 623 /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the 624 /// add was successful, or false if there was already a member function with 625 /// the same signature in the map. 626 bool AddVCallOffset(const CXXMethodDecl *MD, int64_t OffsetOffset); 627 628 /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the 629 /// vtable address point) for the given virtual member function. 630 int64_t getVCallOffsetOffset(const CXXMethodDecl *MD); 631 632 // empty - Return whether the offset map is empty or not. 633 bool empty() const { return Offsets.empty(); } 634}; 635 636static bool HasSameVirtualSignature(const CXXMethodDecl *LHS, 637 const CXXMethodDecl *RHS) { 638 ASTContext &C = LHS->getASTContext(); // TODO: thread this down 639 CanQual<FunctionProtoType> 640 LT = C.getCanonicalType(LHS->getType()).getAs<FunctionProtoType>(), 641 RT = C.getCanonicalType(RHS->getType()).getAs<FunctionProtoType>(); 642 643 // Fast-path matches in the canonical types. 644 if (LT == RT) return true; 645 646 // Force the signatures to match. We can't rely on the overrides 647 // list here because there isn't necessarily an inheritance 648 // relationship between the two methods. 649 if (LT.getQualifiers() != RT.getQualifiers() || 650 LT->getNumArgs() != RT->getNumArgs()) 651 return false; 652 for (unsigned I = 0, E = LT->getNumArgs(); I != E; ++I) 653 if (LT->getArgType(I) != RT->getArgType(I)) 654 return false; 655 return true; 656} 657 658bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, 659 const CXXMethodDecl *RHS) { 660 assert(LHS->isVirtual() && "LHS must be virtual!"); 661 assert(RHS->isVirtual() && "LHS must be virtual!"); 662 663 // A destructor can share a vcall offset with another destructor. 664 if (isa<CXXDestructorDecl>(LHS)) 665 return isa<CXXDestructorDecl>(RHS); 666 667 // FIXME: We need to check more things here. 668 669 // The methods must have the same name. 670 DeclarationName LHSName = LHS->getDeclName(); 671 DeclarationName RHSName = RHS->getDeclName(); 672 if (LHSName != RHSName) 673 return false; 674 675 // And the same signatures. 676 return HasSameVirtualSignature(LHS, RHS); 677} 678 679bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD, 680 int64_t OffsetOffset) { 681 // Check if we can reuse an offset. 682 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) { 683 if (MethodsCanShareVCallOffset(Offsets[I].first, MD)) 684 return false; 685 } 686 687 // Add the offset. 688 Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset)); 689 return true; 690} 691 692int64_t VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) { 693 // Look for an offset. 694 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) { 695 if (MethodsCanShareVCallOffset(Offsets[I].first, MD)) 696 return Offsets[I].second; 697 } 698 699 assert(false && "Should always find a vcall offset offset!"); 700 return 0; 701} 702 703/// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets. 704class VCallAndVBaseOffsetBuilder { 705public: 706 typedef llvm::DenseMap<const CXXRecordDecl *, int64_t> 707 VBaseOffsetOffsetsMapTy; 708 709private: 710 /// MostDerivedClass - The most derived class for which we're building vcall 711 /// and vbase offsets. 712 const CXXRecordDecl *MostDerivedClass; 713 714 /// LayoutClass - The class we're using for layout information. Will be 715 /// different than the most derived class if we're building a construction 716 /// vtable. 717 const CXXRecordDecl *LayoutClass; 718 719 /// Context - The ASTContext which we will use for layout information. 720 ASTContext &Context; 721 722 /// Components - vcall and vbase offset components 723 typedef llvm::SmallVector<VTableComponent, 64> VTableComponentVectorTy; 724 VTableComponentVectorTy Components; 725 726 /// VisitedVirtualBases - Visited virtual bases. 727 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; 728 729 /// VCallOffsets - Keeps track of vcall offsets. 730 VCallOffsetMap VCallOffsets; 731 732 733 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets, 734 /// relative to the address point. 735 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; 736 737 /// FinalOverriders - The final overriders of the most derived class. 738 /// (Can be null when we're not building a vtable of the most derived class). 739 const FinalOverriders *Overriders; 740 741 /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the 742 /// given base subobject. 743 void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual, 744 uint64_t RealBaseOffset); 745 746 /// AddVCallOffsets - Add vcall offsets for the given base subobject. 747 void AddVCallOffsets(BaseSubobject Base, uint64_t VBaseOffset); 748 749 /// AddVBaseOffsets - Add vbase offsets for the given class. 750 void AddVBaseOffsets(const CXXRecordDecl *Base, uint64_t OffsetInLayoutClass); 751 752 /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in 753 /// bytes, relative to the vtable address point. 754 int64_t getCurrentOffsetOffset() const; 755 756public: 757 VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass, 758 const CXXRecordDecl *LayoutClass, 759 const FinalOverriders *Overriders, 760 BaseSubobject Base, bool BaseIsVirtual, 761 uint64_t OffsetInLayoutClass) 762 : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass), 763 Context(MostDerivedClass->getASTContext()), Overriders(Overriders) { 764 765 // Add vcall and vbase offsets. 766 AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass); 767 } 768 769 /// Methods for iterating over the components. 770 typedef VTableComponentVectorTy::const_reverse_iterator const_iterator; 771 const_iterator components_begin() const { return Components.rbegin(); } 772 const_iterator components_end() const { return Components.rend(); } 773 774 const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; } 775 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { 776 return VBaseOffsetOffsets; 777 } 778}; 779 780void 781VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base, 782 bool BaseIsVirtual, 783 uint64_t RealBaseOffset) { 784 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase()); 785 786 // Itanium C++ ABI 2.5.2: 787 // ..in classes sharing a virtual table with a primary base class, the vcall 788 // and vbase offsets added by the derived class all come before the vcall 789 // and vbase offsets required by the base class, so that the latter may be 790 // laid out as required by the base class without regard to additions from 791 // the derived class(es). 792 793 // (Since we're emitting the vcall and vbase offsets in reverse order, we'll 794 // emit them for the primary base first). 795 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { 796 bool PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual(); 797 798 uint64_t PrimaryBaseOffset; 799 800 // Get the base offset of the primary base. 801 if (PrimaryBaseIsVirtual) { 802 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 && 803 "Primary vbase should have a zero offset!"); 804 805 const ASTRecordLayout &MostDerivedClassLayout = 806 Context.getASTRecordLayout(MostDerivedClass); 807 808 PrimaryBaseOffset = 809 MostDerivedClassLayout.getVBaseClassOffsetInBits(PrimaryBase); 810 } else { 811 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 && 812 "Primary base should have a zero offset!"); 813 814 PrimaryBaseOffset = Base.getBaseOffset(); 815 } 816 817 AddVCallAndVBaseOffsets(BaseSubobject(PrimaryBase, PrimaryBaseOffset), 818 PrimaryBaseIsVirtual, RealBaseOffset); 819 } 820 821 AddVBaseOffsets(Base.getBase(), RealBaseOffset); 822 823 // We only want to add vcall offsets for virtual bases. 824 if (BaseIsVirtual) 825 AddVCallOffsets(Base, RealBaseOffset); 826} 827 828int64_t VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const { 829 // OffsetIndex is the index of this vcall or vbase offset, relative to the 830 // vtable address point. (We subtract 3 to account for the information just 831 // above the address point, the RTTI info, the offset to top, and the 832 // vcall offset itself). 833 int64_t OffsetIndex = -(int64_t)(3 + Components.size()); 834 835 // FIXME: We shouldn't use / 8 here. 836 int64_t OffsetOffset = OffsetIndex * 837 (int64_t)Context.Target.getPointerWidth(0) / 8; 838 839 return OffsetOffset; 840} 841 842void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base, 843 uint64_t VBaseOffset) { 844 const CXXRecordDecl *RD = Base.getBase(); 845 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 846 847 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 848 849 // Handle the primary base first. 850 // We only want to add vcall offsets if the base is non-virtual; a virtual 851 // primary base will have its vcall and vbase offsets emitted already. 852 if (PrimaryBase && !Layout.getPrimaryBaseWasVirtual()) { 853 // Get the base offset of the primary base. 854 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 && 855 "Primary base should have a zero offset!"); 856 857 AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()), 858 VBaseOffset); 859 } 860 861 // Add the vcall offsets. 862 for (CXXRecordDecl::method_iterator I = RD->method_begin(), 863 E = RD->method_end(); I != E; ++I) { 864 const CXXMethodDecl *MD = *I; 865 866 if (!MD->isVirtual()) 867 continue; 868 869 int64_t OffsetOffset = getCurrentOffsetOffset(); 870 871 // Don't add a vcall offset if we already have one for this member function 872 // signature. 873 if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset)) 874 continue; 875 876 int64_t Offset = 0; 877 878 if (Overriders) { 879 // Get the final overrider. 880 FinalOverriders::OverriderInfo Overrider = 881 Overriders->getOverrider(MD, Base.getBaseOffset()); 882 883 /// The vcall offset is the offset from the virtual base to the object 884 /// where the function was overridden. 885 // FIXME: We should not use / 8 here. 886 Offset = (int64_t)(Overrider.Offset - VBaseOffset) / 8; 887 } 888 889 Components.push_back(VTableComponent::MakeVCallOffset(Offset)); 890 } 891 892 // And iterate over all non-virtual bases (ignoring the primary base). 893 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 894 E = RD->bases_end(); I != E; ++I) { 895 896 if (I->isVirtual()) 897 continue; 898 899 const CXXRecordDecl *BaseDecl = 900 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 901 if (BaseDecl == PrimaryBase) 902 continue; 903 904 // Get the base offset of this base. 905 uint64_t BaseOffset = Base.getBaseOffset() + 906 Layout.getBaseClassOffsetInBits(BaseDecl); 907 908 AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset), VBaseOffset); 909 } 910} 911 912void VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD, 913 uint64_t OffsetInLayoutClass) { 914 const ASTRecordLayout &LayoutClassLayout = 915 Context.getASTRecordLayout(LayoutClass); 916 917 // Add vbase offsets. 918 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 919 E = RD->bases_end(); I != E; ++I) { 920 const CXXRecordDecl *BaseDecl = 921 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 922 923 // Check if this is a virtual base that we haven't visited before. 924 if (I->isVirtual() && VisitedVirtualBases.insert(BaseDecl)) { 925 // FIXME: We shouldn't use / 8 here. 926 int64_t Offset = 927 (int64_t)(LayoutClassLayout.getVBaseClassOffsetInBits(BaseDecl) - 928 OffsetInLayoutClass) / 8; 929 930 // Add the vbase offset offset. 931 assert(!VBaseOffsetOffsets.count(BaseDecl) && 932 "vbase offset offset already exists!"); 933 934 int64_t VBaseOffsetOffset = getCurrentOffsetOffset(); 935 VBaseOffsetOffsets.insert(std::make_pair(BaseDecl, VBaseOffsetOffset)); 936 937 Components.push_back(VTableComponent::MakeVBaseOffset(Offset)); 938 } 939 940 // Check the base class looking for more vbase offsets. 941 AddVBaseOffsets(BaseDecl, OffsetInLayoutClass); 942 } 943} 944 945/// VTableBuilder - Class for building vtable layout information. 946class VTableBuilder { 947public: 948 /// PrimaryBasesSetVectorTy - A set vector of direct and indirect 949 /// primary bases. 950 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> 951 PrimaryBasesSetVectorTy; 952 953 typedef llvm::DenseMap<const CXXRecordDecl *, int64_t> 954 VBaseOffsetOffsetsMapTy; 955 956 typedef llvm::DenseMap<BaseSubobject, uint64_t> 957 AddressPointsMapTy; 958 959private: 960 /// VTables - Global vtable information. 961 CodeGenVTables &VTables; 962 963 /// MostDerivedClass - The most derived class for which we're building this 964 /// vtable. 965 const CXXRecordDecl *MostDerivedClass; 966 967 /// MostDerivedClassOffset - If we're building a construction vtable, this 968 /// holds the offset from the layout class to the most derived class. 969 const uint64_t MostDerivedClassOffset; 970 971 /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual 972 /// base. (This only makes sense when building a construction vtable). 973 bool MostDerivedClassIsVirtual; 974 975 /// LayoutClass - The class we're using for layout information. Will be 976 /// different than the most derived class if we're building a construction 977 /// vtable. 978 const CXXRecordDecl *LayoutClass; 979 980 /// Context - The ASTContext which we will use for layout information. 981 ASTContext &Context; 982 983 /// FinalOverriders - The final overriders of the most derived class. 984 const FinalOverriders Overriders; 985 986 /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual 987 /// bases in this vtable. 988 llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases; 989 990 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for 991 /// the most derived class. 992 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; 993 994 /// Components - The components of the vtable being built. 995 llvm::SmallVector<VTableComponent, 64> Components; 996 997 /// AddressPoints - Address points for the vtable being built. 998 AddressPointsMapTy AddressPoints; 999 1000 /// MethodInfo - Contains information about a method in a vtable. 1001 /// (Used for computing 'this' pointer adjustment thunks. 1002 struct MethodInfo { 1003 /// BaseOffset - The base offset of this method. 1004 const uint64_t BaseOffset; 1005 1006 /// BaseOffsetInLayoutClass - The base offset in the layout class of this 1007 /// method. 1008 const uint64_t BaseOffsetInLayoutClass; 1009 1010 /// VTableIndex - The index in the vtable that this method has. 1011 /// (For destructors, this is the index of the complete destructor). 1012 const uint64_t VTableIndex; 1013 1014 MethodInfo(uint64_t BaseOffset, uint64_t BaseOffsetInLayoutClass, 1015 uint64_t VTableIndex) 1016 : BaseOffset(BaseOffset), 1017 BaseOffsetInLayoutClass(BaseOffsetInLayoutClass), 1018 VTableIndex(VTableIndex) { } 1019 1020 MethodInfo() : BaseOffset(0), BaseOffsetInLayoutClass(0), VTableIndex(0) { } 1021 }; 1022 1023 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy; 1024 1025 /// MethodInfoMap - The information for all methods in the vtable we're 1026 /// currently building. 1027 MethodInfoMapTy MethodInfoMap; 1028 1029 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy; 1030 1031 /// VTableThunks - The thunks by vtable index in the vtable currently being 1032 /// built. 1033 VTableThunksMapTy VTableThunks; 1034 1035 typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; 1036 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; 1037 1038 /// Thunks - A map that contains all the thunks needed for all methods in the 1039 /// most derived class for which the vtable is currently being built. 1040 ThunksMapTy Thunks; 1041 1042 /// AddThunk - Add a thunk for the given method. 1043 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk); 1044 1045 /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the 1046 /// part of the vtable we're currently building. 1047 void ComputeThisAdjustments(); 1048 1049 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; 1050 1051 /// PrimaryVirtualBases - All known virtual bases who are a primary base of 1052 /// some other base. 1053 VisitedVirtualBasesSetTy PrimaryVirtualBases; 1054 1055 /// ComputeReturnAdjustment - Compute the return adjustment given a return 1056 /// adjustment base offset. 1057 ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset); 1058 1059 /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting 1060 /// the 'this' pointer from the base subobject to the derived subobject. 1061 BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base, 1062 BaseSubobject Derived) const; 1063 1064 /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the 1065 /// given virtual member function, its offset in the layout class and its 1066 /// final overrider. 1067 ThisAdjustment 1068 ComputeThisAdjustment(const CXXMethodDecl *MD, 1069 uint64_t BaseOffsetInLayoutClass, 1070 FinalOverriders::OverriderInfo Overrider); 1071 1072 /// AddMethod - Add a single virtual member function to the vtable 1073 /// components vector. 1074 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment); 1075 1076 /// IsOverriderUsed - Returns whether the overrider will ever be used in this 1077 /// part of the vtable. 1078 /// 1079 /// Itanium C++ ABI 2.5.2: 1080 /// 1081 /// struct A { virtual void f(); }; 1082 /// struct B : virtual public A { int i; }; 1083 /// struct C : virtual public A { int j; }; 1084 /// struct D : public B, public C {}; 1085 /// 1086 /// When B and C are declared, A is a primary base in each case, so although 1087 /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this 1088 /// adjustment is required and no thunk is generated. However, inside D 1089 /// objects, A is no longer a primary base of C, so if we allowed calls to 1090 /// C::f() to use the copy of A's vtable in the C subobject, we would need 1091 /// to adjust this from C* to B::A*, which would require a third-party 1092 /// thunk. Since we require that a call to C::f() first convert to A*, 1093 /// C-in-D's copy of A's vtable is never referenced, so this is not 1094 /// necessary. 1095 bool IsOverriderUsed(const CXXMethodDecl *Overrider, 1096 uint64_t BaseOffsetInLayoutClass, 1097 const CXXRecordDecl *FirstBaseInPrimaryBaseChain, 1098 uint64_t FirstBaseOffsetInLayoutClass) const; 1099 1100 1101 /// AddMethods - Add the methods of this base subobject and all its 1102 /// primary bases to the vtable components vector. 1103 void AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass, 1104 const CXXRecordDecl *FirstBaseInPrimaryBaseChain, 1105 uint64_t FirstBaseOffsetInLayoutClass, 1106 PrimaryBasesSetVectorTy &PrimaryBases); 1107 1108 // LayoutVTable - Layout the vtable for the given base class, including its 1109 // secondary vtables and any vtables for virtual bases. 1110 void LayoutVTable(); 1111 1112 /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the 1113 /// given base subobject, as well as all its secondary vtables. 1114 /// 1115 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base 1116 /// or a direct or indirect base of a virtual base. 1117 /// 1118 /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual 1119 /// in the layout class. 1120 void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base, 1121 bool BaseIsMorallyVirtual, 1122 bool BaseIsVirtualInLayoutClass, 1123 uint64_t OffsetInLayoutClass); 1124 1125 /// LayoutSecondaryVTables - Layout the secondary vtables for the given base 1126 /// subobject. 1127 /// 1128 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base 1129 /// or a direct or indirect base of a virtual base. 1130 void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual, 1131 uint64_t OffsetInLayoutClass); 1132 1133 /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this 1134 /// class hierarchy. 1135 void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD, 1136 uint64_t OffsetInLayoutClass, 1137 VisitedVirtualBasesSetTy &VBases); 1138 1139 /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the 1140 /// given base (excluding any primary bases). 1141 void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD, 1142 VisitedVirtualBasesSetTy &VBases); 1143 1144 /// isBuildingConstructionVTable - Return whether this vtable builder is 1145 /// building a construction vtable. 1146 bool isBuildingConstructorVTable() const { 1147 return MostDerivedClass != LayoutClass; 1148 } 1149 1150public: 1151 VTableBuilder(CodeGenVTables &VTables, const CXXRecordDecl *MostDerivedClass, 1152 uint64_t MostDerivedClassOffset, bool MostDerivedClassIsVirtual, 1153 const CXXRecordDecl *LayoutClass) 1154 : VTables(VTables), MostDerivedClass(MostDerivedClass), 1155 MostDerivedClassOffset(MostDerivedClassOffset), 1156 MostDerivedClassIsVirtual(MostDerivedClassIsVirtual), 1157 LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()), 1158 Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) { 1159 1160 LayoutVTable(); 1161 } 1162 1163 ThunksMapTy::const_iterator thunks_begin() const { 1164 return Thunks.begin(); 1165 } 1166 1167 ThunksMapTy::const_iterator thunks_end() const { 1168 return Thunks.end(); 1169 } 1170 1171 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { 1172 return VBaseOffsetOffsets; 1173 } 1174 1175 /// getNumVTableComponents - Return the number of components in the vtable 1176 /// currently built. 1177 uint64_t getNumVTableComponents() const { 1178 return Components.size(); 1179 } 1180 1181 const uint64_t *vtable_components_data_begin() const { 1182 return reinterpret_cast<const uint64_t *>(Components.begin()); 1183 } 1184 1185 const uint64_t *vtable_components_data_end() const { 1186 return reinterpret_cast<const uint64_t *>(Components.end()); 1187 } 1188 1189 AddressPointsMapTy::const_iterator address_points_begin() const { 1190 return AddressPoints.begin(); 1191 } 1192 1193 AddressPointsMapTy::const_iterator address_points_end() const { 1194 return AddressPoints.end(); 1195 } 1196 1197 VTableThunksMapTy::const_iterator vtable_thunks_begin() const { 1198 return VTableThunks.begin(); 1199 } 1200 1201 VTableThunksMapTy::const_iterator vtable_thunks_end() const { 1202 return VTableThunks.end(); 1203 } 1204 1205 /// dumpLayout - Dump the vtable layout. 1206 void dumpLayout(llvm::raw_ostream&); 1207}; 1208 1209void VTableBuilder::AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) { 1210 assert(!isBuildingConstructorVTable() && 1211 "Can't add thunks for construction vtable"); 1212 1213 llvm::SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD]; 1214 1215 // Check if we have this thunk already. 1216 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) != 1217 ThunksVector.end()) 1218 return; 1219 1220 ThunksVector.push_back(Thunk); 1221} 1222 1223typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy; 1224 1225/// ComputeAllOverriddenMethods - Given a method decl, will return a set of all 1226/// the overridden methods that the function decl overrides. 1227static void 1228ComputeAllOverriddenMethods(const CXXMethodDecl *MD, 1229 OverriddenMethodsSetTy& OverriddenMethods) { 1230 assert(MD->isVirtual() && "Method is not virtual!"); 1231 1232 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), 1233 E = MD->end_overridden_methods(); I != E; ++I) { 1234 const CXXMethodDecl *OverriddenMD = *I; 1235 1236 OverriddenMethods.insert(OverriddenMD); 1237 1238 ComputeAllOverriddenMethods(OverriddenMD, OverriddenMethods); 1239 } 1240} 1241 1242void VTableBuilder::ComputeThisAdjustments() { 1243 // Now go through the method info map and see if any of the methods need 1244 // 'this' pointer adjustments. 1245 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(), 1246 E = MethodInfoMap.end(); I != E; ++I) { 1247 const CXXMethodDecl *MD = I->first; 1248 const MethodInfo &MethodInfo = I->second; 1249 1250 // Ignore adjustments for unused function pointers. 1251 uint64_t VTableIndex = MethodInfo.VTableIndex; 1252 if (Components[VTableIndex].getKind() == 1253 VTableComponent::CK_UnusedFunctionPointer) 1254 continue; 1255 1256 // Get the final overrider for this method. 1257 FinalOverriders::OverriderInfo Overrider = 1258 Overriders.getOverrider(MD, MethodInfo.BaseOffset); 1259 1260 // Check if we need an adjustment at all. 1261 if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) { 1262 // When a return thunk is needed by a derived class that overrides a 1263 // virtual base, gcc uses a virtual 'this' adjustment as well. 1264 // While the thunk itself might be needed by vtables in subclasses or 1265 // in construction vtables, there doesn't seem to be a reason for using 1266 // the thunk in this vtable. Still, we do so to match gcc. 1267 if (VTableThunks.lookup(VTableIndex).Return.isEmpty()) 1268 continue; 1269 } 1270 1271 ThisAdjustment ThisAdjustment = 1272 ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider); 1273 1274 if (ThisAdjustment.isEmpty()) 1275 continue; 1276 1277 // Add it. 1278 VTableThunks[VTableIndex].This = ThisAdjustment; 1279 1280 if (isa<CXXDestructorDecl>(MD)) { 1281 // Add an adjustment for the deleting destructor as well. 1282 VTableThunks[VTableIndex + 1].This = ThisAdjustment; 1283 } 1284 } 1285 1286 /// Clear the method info map. 1287 MethodInfoMap.clear(); 1288 1289 if (isBuildingConstructorVTable()) { 1290 // We don't need to store thunk information for construction vtables. 1291 return; 1292 } 1293 1294 for (VTableThunksMapTy::const_iterator I = VTableThunks.begin(), 1295 E = VTableThunks.end(); I != E; ++I) { 1296 const VTableComponent &Component = Components[I->first]; 1297 const ThunkInfo &Thunk = I->second; 1298 const CXXMethodDecl *MD; 1299 1300 switch (Component.getKind()) { 1301 default: 1302 llvm_unreachable("Unexpected vtable component kind!"); 1303 case VTableComponent::CK_FunctionPointer: 1304 MD = Component.getFunctionDecl(); 1305 break; 1306 case VTableComponent::CK_CompleteDtorPointer: 1307 MD = Component.getDestructorDecl(); 1308 break; 1309 case VTableComponent::CK_DeletingDtorPointer: 1310 // We've already added the thunk when we saw the complete dtor pointer. 1311 continue; 1312 } 1313 1314 if (MD->getParent() == MostDerivedClass) 1315 AddThunk(MD, Thunk); 1316 } 1317} 1318 1319ReturnAdjustment VTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) { 1320 ReturnAdjustment Adjustment; 1321 1322 if (!Offset.isEmpty()) { 1323 if (Offset.VirtualBase) { 1324 // Get the virtual base offset offset. 1325 if (Offset.DerivedClass == MostDerivedClass) { 1326 // We can get the offset offset directly from our map. 1327 Adjustment.VBaseOffsetOffset = 1328 VBaseOffsetOffsets.lookup(Offset.VirtualBase); 1329 } else { 1330 Adjustment.VBaseOffsetOffset = 1331 VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass, 1332 Offset.VirtualBase); 1333 } 1334 } 1335 1336 Adjustment.NonVirtual = Offset.NonVirtualOffset; 1337 } 1338 1339 return Adjustment; 1340} 1341 1342BaseOffset 1343VTableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base, 1344 BaseSubobject Derived) const { 1345 const CXXRecordDecl *BaseRD = Base.getBase(); 1346 const CXXRecordDecl *DerivedRD = Derived.getBase(); 1347 1348 CXXBasePaths Paths(/*FindAmbiguities=*/true, 1349 /*RecordPaths=*/true, /*DetectVirtual=*/true); 1350 1351 if (!const_cast<CXXRecordDecl *>(DerivedRD)-> 1352 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) { 1353 assert(false && "Class must be derived from the passed in base class!"); 1354 return BaseOffset(); 1355 } 1356 1357 // We have to go through all the paths, and see which one leads us to the 1358 // right base subobject. 1359 for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end(); 1360 I != E; ++I) { 1361 BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I); 1362 1363 // FIXME: Should not use * 8 here. 1364 uint64_t OffsetToBaseSubobject = Offset.NonVirtualOffset * 8; 1365 1366 if (Offset.VirtualBase) { 1367 // If we have a virtual base class, the non-virtual offset is relative 1368 // to the virtual base class offset. 1369 const ASTRecordLayout &LayoutClassLayout = 1370 Context.getASTRecordLayout(LayoutClass); 1371 1372 /// Get the virtual base offset, relative to the most derived class 1373 /// layout. 1374 OffsetToBaseSubobject += 1375 LayoutClassLayout.getVBaseClassOffsetInBits(Offset.VirtualBase); 1376 } else { 1377 // Otherwise, the non-virtual offset is relative to the derived class 1378 // offset. 1379 OffsetToBaseSubobject += Derived.getBaseOffset(); 1380 } 1381 1382 // Check if this path gives us the right base subobject. 1383 if (OffsetToBaseSubobject == Base.getBaseOffset()) { 1384 // Since we're going from the base class _to_ the derived class, we'll 1385 // invert the non-virtual offset here. 1386 Offset.NonVirtualOffset = -Offset.NonVirtualOffset; 1387 return Offset; 1388 } 1389 } 1390 1391 return BaseOffset(); 1392} 1393 1394ThisAdjustment 1395VTableBuilder::ComputeThisAdjustment(const CXXMethodDecl *MD, 1396 uint64_t BaseOffsetInLayoutClass, 1397 FinalOverriders::OverriderInfo Overrider) { 1398 // Ignore adjustments for pure virtual member functions. 1399 if (Overrider.Method->isPure()) 1400 return ThisAdjustment(); 1401 1402 BaseSubobject OverriddenBaseSubobject(MD->getParent(), 1403 BaseOffsetInLayoutClass); 1404 1405 BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(), 1406 Overrider.Offset); 1407 1408 // Compute the adjustment offset. 1409 BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject, 1410 OverriderBaseSubobject); 1411 if (Offset.isEmpty()) 1412 return ThisAdjustment(); 1413 1414 ThisAdjustment Adjustment; 1415 1416 if (Offset.VirtualBase) { 1417 // Get the vcall offset map for this virtual base. 1418 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase]; 1419 1420 if (VCallOffsets.empty()) { 1421 // We don't have vcall offsets for this virtual base, go ahead and 1422 // build them. 1423 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass, 1424 /*FinalOverriders=*/0, 1425 BaseSubobject(Offset.VirtualBase, 0), 1426 /*BaseIsVirtual=*/true, 1427 /*OffsetInLayoutClass=*/0); 1428 1429 VCallOffsets = Builder.getVCallOffsets(); 1430 } 1431 1432 Adjustment.VCallOffsetOffset = VCallOffsets.getVCallOffsetOffset(MD); 1433 } 1434 1435 // Set the non-virtual part of the adjustment. 1436 Adjustment.NonVirtual = Offset.NonVirtualOffset; 1437 1438 return Adjustment; 1439} 1440 1441void 1442VTableBuilder::AddMethod(const CXXMethodDecl *MD, 1443 ReturnAdjustment ReturnAdjustment) { 1444 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 1445 assert(ReturnAdjustment.isEmpty() && 1446 "Destructor can't have return adjustment!"); 1447 1448 // Add both the complete destructor and the deleting destructor. 1449 Components.push_back(VTableComponent::MakeCompleteDtor(DD)); 1450 Components.push_back(VTableComponent::MakeDeletingDtor(DD)); 1451 } else { 1452 // Add the return adjustment if necessary. 1453 if (!ReturnAdjustment.isEmpty()) 1454 VTableThunks[Components.size()].Return = ReturnAdjustment; 1455 1456 // Add the function. 1457 Components.push_back(VTableComponent::MakeFunction(MD)); 1458 } 1459} 1460 1461/// OverridesIndirectMethodInBase - Return whether the given member function 1462/// overrides any methods in the set of given bases. 1463/// Unlike OverridesMethodInBase, this checks "overriders of overriders". 1464/// For example, if we have: 1465/// 1466/// struct A { virtual void f(); } 1467/// struct B : A { virtual void f(); } 1468/// struct C : B { virtual void f(); } 1469/// 1470/// OverridesIndirectMethodInBase will return true if given C::f as the method 1471/// and { A } as the set of bases. 1472static bool 1473OverridesIndirectMethodInBases(const CXXMethodDecl *MD, 1474 VTableBuilder::PrimaryBasesSetVectorTy &Bases) { 1475 if (Bases.count(MD->getParent())) 1476 return true; 1477 1478 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), 1479 E = MD->end_overridden_methods(); I != E; ++I) { 1480 const CXXMethodDecl *OverriddenMD = *I; 1481 1482 // Check "indirect overriders". 1483 if (OverridesIndirectMethodInBases(OverriddenMD, Bases)) 1484 return true; 1485 } 1486 1487 return false; 1488} 1489 1490bool 1491VTableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider, 1492 uint64_t BaseOffsetInLayoutClass, 1493 const CXXRecordDecl *FirstBaseInPrimaryBaseChain, 1494 uint64_t FirstBaseOffsetInLayoutClass) const { 1495 // If the base and the first base in the primary base chain have the same 1496 // offsets, then this overrider will be used. 1497 if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass) 1498 return true; 1499 1500 // We know now that Base (or a direct or indirect base of it) is a primary 1501 // base in part of the class hierarchy, but not a primary base in the most 1502 // derived class. 1503 1504 // If the overrider is the first base in the primary base chain, we know 1505 // that the overrider will be used. 1506 if (Overrider->getParent() == FirstBaseInPrimaryBaseChain) 1507 return true; 1508 1509 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases; 1510 1511 const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain; 1512 PrimaryBases.insert(RD); 1513 1514 // Now traverse the base chain, starting with the first base, until we find 1515 // the base that is no longer a primary base. 1516 while (true) { 1517 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 1518 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 1519 1520 if (!PrimaryBase) 1521 break; 1522 1523 if (Layout.getPrimaryBaseWasVirtual()) { 1524 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 && 1525 "Primary base should always be at offset 0!"); 1526 1527 const ASTRecordLayout &LayoutClassLayout = 1528 Context.getASTRecordLayout(LayoutClass); 1529 1530 // Now check if this is the primary base that is not a primary base in the 1531 // most derived class. 1532 if (LayoutClassLayout.getVBaseClassOffsetInBits(PrimaryBase) != 1533 FirstBaseOffsetInLayoutClass) { 1534 // We found it, stop walking the chain. 1535 break; 1536 } 1537 } else { 1538 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 && 1539 "Primary base should always be at offset 0!"); 1540 } 1541 1542 if (!PrimaryBases.insert(PrimaryBase)) 1543 assert(false && "Found a duplicate primary base!"); 1544 1545 RD = PrimaryBase; 1546 } 1547 1548 // If the final overrider is an override of one of the primary bases, 1549 // then we know that it will be used. 1550 return OverridesIndirectMethodInBases(Overrider, PrimaryBases); 1551} 1552 1553/// FindNearestOverriddenMethod - Given a method, returns the overridden method 1554/// from the nearest base. Returns null if no method was found. 1555static const CXXMethodDecl * 1556FindNearestOverriddenMethod(const CXXMethodDecl *MD, 1557 VTableBuilder::PrimaryBasesSetVectorTy &Bases) { 1558 OverriddenMethodsSetTy OverriddenMethods; 1559 ComputeAllOverriddenMethods(MD, OverriddenMethods); 1560 1561 for (int I = Bases.size(), E = 0; I != E; --I) { 1562 const CXXRecordDecl *PrimaryBase = Bases[I - 1]; 1563 1564 // Now check the overriden methods. 1565 for (OverriddenMethodsSetTy::const_iterator I = OverriddenMethods.begin(), 1566 E = OverriddenMethods.end(); I != E; ++I) { 1567 const CXXMethodDecl *OverriddenMD = *I; 1568 1569 // We found our overridden method. 1570 if (OverriddenMD->getParent() == PrimaryBase) 1571 return OverriddenMD; 1572 } 1573 } 1574 1575 return 0; 1576} 1577 1578void 1579VTableBuilder::AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass, 1580 const CXXRecordDecl *FirstBaseInPrimaryBaseChain, 1581 uint64_t FirstBaseOffsetInLayoutClass, 1582 PrimaryBasesSetVectorTy &PrimaryBases) { 1583 const CXXRecordDecl *RD = Base.getBase(); 1584 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 1585 1586 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { 1587 uint64_t PrimaryBaseOffset; 1588 uint64_t PrimaryBaseOffsetInLayoutClass; 1589 if (Layout.getPrimaryBaseWasVirtual()) { 1590 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 && 1591 "Primary vbase should have a zero offset!"); 1592 1593 const ASTRecordLayout &MostDerivedClassLayout = 1594 Context.getASTRecordLayout(MostDerivedClass); 1595 1596 PrimaryBaseOffset = 1597 MostDerivedClassLayout.getVBaseClassOffsetInBits(PrimaryBase); 1598 1599 const ASTRecordLayout &LayoutClassLayout = 1600 Context.getASTRecordLayout(LayoutClass); 1601 1602 PrimaryBaseOffsetInLayoutClass = 1603 LayoutClassLayout.getVBaseClassOffsetInBits(PrimaryBase); 1604 } else { 1605 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 && 1606 "Primary base should have a zero offset!"); 1607 1608 PrimaryBaseOffset = Base.getBaseOffset(); 1609 PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass; 1610 } 1611 1612 AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset), 1613 PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain, 1614 FirstBaseOffsetInLayoutClass, PrimaryBases); 1615 1616 if (!PrimaryBases.insert(PrimaryBase)) 1617 assert(false && "Found a duplicate primary base!"); 1618 } 1619 1620 // Now go through all virtual member functions and add them. 1621 for (CXXRecordDecl::method_iterator I = RD->method_begin(), 1622 E = RD->method_end(); I != E; ++I) { 1623 const CXXMethodDecl *MD = *I; 1624 1625 if (!MD->isVirtual()) 1626 continue; 1627 1628 // Get the final overrider. 1629 FinalOverriders::OverriderInfo Overrider = 1630 Overriders.getOverrider(MD, Base.getBaseOffset()); 1631 1632 // Check if this virtual member function overrides a method in a primary 1633 // base. If this is the case, and the return type doesn't require adjustment 1634 // then we can just use the member function from the primary base. 1635 if (const CXXMethodDecl *OverriddenMD = 1636 FindNearestOverriddenMethod(MD, PrimaryBases)) { 1637 if (ComputeReturnAdjustmentBaseOffset(Context, MD, 1638 OverriddenMD).isEmpty()) { 1639 // Replace the method info of the overridden method with our own 1640 // method. 1641 assert(MethodInfoMap.count(OverriddenMD) && 1642 "Did not find the overridden method!"); 1643 MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD]; 1644 1645 MethodInfo MethodInfo(Base.getBaseOffset(), 1646 BaseOffsetInLayoutClass, 1647 OverriddenMethodInfo.VTableIndex); 1648 1649 assert(!MethodInfoMap.count(MD) && 1650 "Should not have method info for this method yet!"); 1651 1652 MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); 1653 MethodInfoMap.erase(OverriddenMD); 1654 1655 // If the overridden method exists in a virtual base class or a direct 1656 // or indirect base class of a virtual base class, we need to emit a 1657 // thunk if we ever have a class hierarchy where the base class is not 1658 // a primary base in the complete object. 1659 if (!isBuildingConstructorVTable() && OverriddenMD != MD) { 1660 // Compute the this adjustment. 1661 ThisAdjustment ThisAdjustment = 1662 ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass, 1663 Overrider); 1664 1665 if (ThisAdjustment.VCallOffsetOffset && 1666 Overrider.Method->getParent() == MostDerivedClass) { 1667 // This is a virtual thunk for the most derived class, add it. 1668 AddThunk(Overrider.Method, 1669 ThunkInfo(ThisAdjustment, ReturnAdjustment())); 1670 } 1671 } 1672 1673 continue; 1674 } 1675 } 1676 1677 // Insert the method info for this method. 1678 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, 1679 Components.size()); 1680 1681 assert(!MethodInfoMap.count(MD) && 1682 "Should not have method info for this method yet!"); 1683 MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); 1684 1685 // Check if this overrider is going to be used. 1686 const CXXMethodDecl *OverriderMD = Overrider.Method; 1687 if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass, 1688 FirstBaseInPrimaryBaseChain, 1689 FirstBaseOffsetInLayoutClass)) { 1690 Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD)); 1691 continue; 1692 } 1693 1694 // Check if this overrider needs a return adjustment. 1695 // We don't want to do this for pure virtual member functions. 1696 BaseOffset ReturnAdjustmentOffset; 1697 if (!OverriderMD->isPure()) { 1698 ReturnAdjustmentOffset = 1699 ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD); 1700 } 1701 1702 ReturnAdjustment ReturnAdjustment = 1703 ComputeReturnAdjustment(ReturnAdjustmentOffset); 1704 1705 AddMethod(Overrider.Method, ReturnAdjustment); 1706 } 1707} 1708 1709void VTableBuilder::LayoutVTable() { 1710 LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass, 0), 1711 /*BaseIsMorallyVirtual=*/false, 1712 MostDerivedClassIsVirtual, 1713 MostDerivedClassOffset); 1714 1715 VisitedVirtualBasesSetTy VBases; 1716 1717 // Determine the primary virtual bases. 1718 DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset, 1719 VBases); 1720 VBases.clear(); 1721 1722 LayoutVTablesForVirtualBases(MostDerivedClass, VBases); 1723} 1724 1725void 1726VTableBuilder::LayoutPrimaryAndSecondaryVTables(BaseSubobject Base, 1727 bool BaseIsMorallyVirtual, 1728 bool BaseIsVirtualInLayoutClass, 1729 uint64_t OffsetInLayoutClass) { 1730 assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!"); 1731 1732 // Add vcall and vbase offsets for this vtable. 1733 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders, 1734 Base, BaseIsVirtualInLayoutClass, 1735 OffsetInLayoutClass); 1736 Components.append(Builder.components_begin(), Builder.components_end()); 1737 1738 // Check if we need to add these vcall offsets. 1739 if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) { 1740 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()]; 1741 1742 if (VCallOffsets.empty()) 1743 VCallOffsets = Builder.getVCallOffsets(); 1744 } 1745 1746 // If we're laying out the most derived class we want to keep track of the 1747 // virtual base class offset offsets. 1748 if (Base.getBase() == MostDerivedClass) 1749 VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets(); 1750 1751 // Add the offset to top. 1752 // FIXME: We should not use / 8 here. 1753 int64_t OffsetToTop = -(int64_t)(OffsetInLayoutClass - 1754 MostDerivedClassOffset) / 8; 1755 Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop)); 1756 1757 // Next, add the RTTI. 1758 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass)); 1759 1760 uint64_t AddressPoint = Components.size(); 1761 1762 // Now go through all virtual member functions and add them. 1763 PrimaryBasesSetVectorTy PrimaryBases; 1764 AddMethods(Base, OffsetInLayoutClass, Base.getBase(), OffsetInLayoutClass, 1765 PrimaryBases); 1766 1767 // Compute 'this' pointer adjustments. 1768 ComputeThisAdjustments(); 1769 1770 // Add all address points. 1771 const CXXRecordDecl *RD = Base.getBase(); 1772 while (true) { 1773 AddressPoints.insert(std::make_pair(BaseSubobject(RD, OffsetInLayoutClass), 1774 AddressPoint)); 1775 1776 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 1777 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 1778 1779 if (!PrimaryBase) 1780 break; 1781 1782 if (Layout.getPrimaryBaseWasVirtual()) { 1783 // Check if this virtual primary base is a primary base in the layout 1784 // class. If it's not, we don't want to add it. 1785 const ASTRecordLayout &LayoutClassLayout = 1786 Context.getASTRecordLayout(LayoutClass); 1787 1788 if (LayoutClassLayout.getVBaseClassOffsetInBits(PrimaryBase) != 1789 OffsetInLayoutClass) { 1790 // We don't want to add this class (or any of its primary bases). 1791 break; 1792 } 1793 } 1794 1795 RD = PrimaryBase; 1796 } 1797 1798 // Layout secondary vtables. 1799 LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass); 1800} 1801 1802void VTableBuilder::LayoutSecondaryVTables(BaseSubobject Base, 1803 bool BaseIsMorallyVirtual, 1804 uint64_t OffsetInLayoutClass) { 1805 // Itanium C++ ABI 2.5.2: 1806 // Following the primary virtual table of a derived class are secondary 1807 // virtual tables for each of its proper base classes, except any primary 1808 // base(s) with which it shares its primary virtual table. 1809 1810 const CXXRecordDecl *RD = Base.getBase(); 1811 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 1812 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 1813 1814 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 1815 E = RD->bases_end(); I != E; ++I) { 1816 // Ignore virtual bases, we'll emit them later. 1817 if (I->isVirtual()) 1818 continue; 1819 1820 const CXXRecordDecl *BaseDecl = 1821 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 1822 1823 // Ignore bases that don't have a vtable. 1824 if (!BaseDecl->isDynamicClass()) 1825 continue; 1826 1827 if (isBuildingConstructorVTable()) { 1828 // Itanium C++ ABI 2.6.4: 1829 // Some of the base class subobjects may not need construction virtual 1830 // tables, which will therefore not be present in the construction 1831 // virtual table group, even though the subobject virtual tables are 1832 // present in the main virtual table group for the complete object. 1833 if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases()) 1834 continue; 1835 } 1836 1837 // Get the base offset of this base. 1838 uint64_t RelativeBaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl); 1839 uint64_t BaseOffset = Base.getBaseOffset() + RelativeBaseOffset; 1840 1841 uint64_t BaseOffsetInLayoutClass = OffsetInLayoutClass + RelativeBaseOffset; 1842 1843 // Don't emit a secondary vtable for a primary base. We might however want 1844 // to emit secondary vtables for other bases of this base. 1845 if (BaseDecl == PrimaryBase) { 1846 LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset), 1847 BaseIsMorallyVirtual, BaseOffsetInLayoutClass); 1848 continue; 1849 } 1850 1851 // Layout the primary vtable (and any secondary vtables) for this base. 1852 LayoutPrimaryAndSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset), 1853 BaseIsMorallyVirtual, 1854 /*BaseIsVirtualInLayoutClass=*/false, 1855 BaseOffsetInLayoutClass); 1856 } 1857} 1858 1859void 1860VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD, 1861 uint64_t OffsetInLayoutClass, 1862 VisitedVirtualBasesSetTy &VBases) { 1863 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 1864 1865 // Check if this base has a primary base. 1866 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { 1867 1868 // Check if it's virtual. 1869 if (Layout.getPrimaryBaseWasVirtual()) { 1870 bool IsPrimaryVirtualBase = true; 1871 1872 if (isBuildingConstructorVTable()) { 1873 // Check if the base is actually a primary base in the class we use for 1874 // layout. 1875 const ASTRecordLayout &LayoutClassLayout = 1876 Context.getASTRecordLayout(LayoutClass); 1877 1878 uint64_t PrimaryBaseOffsetInLayoutClass = 1879 LayoutClassLayout.getVBaseClassOffsetInBits(PrimaryBase); 1880 1881 // We know that the base is not a primary base in the layout class if 1882 // the base offsets are different. 1883 if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass) 1884 IsPrimaryVirtualBase = false; 1885 } 1886 1887 if (IsPrimaryVirtualBase) 1888 PrimaryVirtualBases.insert(PrimaryBase); 1889 } 1890 } 1891 1892 // Traverse bases, looking for more primary virtual bases. 1893 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 1894 E = RD->bases_end(); I != E; ++I) { 1895 const CXXRecordDecl *BaseDecl = 1896 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 1897 1898 uint64_t BaseOffsetInLayoutClass; 1899 1900 if (I->isVirtual()) { 1901 if (!VBases.insert(BaseDecl)) 1902 continue; 1903 1904 const ASTRecordLayout &LayoutClassLayout = 1905 Context.getASTRecordLayout(LayoutClass); 1906 1907 BaseOffsetInLayoutClass = 1908 LayoutClassLayout.getVBaseClassOffsetInBits(BaseDecl); 1909 } else { 1910 BaseOffsetInLayoutClass = 1911 OffsetInLayoutClass + Layout.getBaseClassOffsetInBits(BaseDecl); 1912 } 1913 1914 DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases); 1915 } 1916} 1917 1918void 1919VTableBuilder::LayoutVTablesForVirtualBases(const CXXRecordDecl *RD, 1920 VisitedVirtualBasesSetTy &VBases) { 1921 // Itanium C++ ABI 2.5.2: 1922 // Then come the virtual base virtual tables, also in inheritance graph 1923 // order, and again excluding primary bases (which share virtual tables with 1924 // the classes for which they are primary). 1925 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 1926 E = RD->bases_end(); I != E; ++I) { 1927 const CXXRecordDecl *BaseDecl = 1928 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 1929 1930 // Check if this base needs a vtable. (If it's virtual, not a primary base 1931 // of some other class, and we haven't visited it before). 1932 if (I->isVirtual() && BaseDecl->isDynamicClass() && 1933 !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) { 1934 const ASTRecordLayout &MostDerivedClassLayout = 1935 Context.getASTRecordLayout(MostDerivedClass); 1936 uint64_t BaseOffset = 1937 MostDerivedClassLayout.getVBaseClassOffsetInBits(BaseDecl); 1938 1939 const ASTRecordLayout &LayoutClassLayout = 1940 Context.getASTRecordLayout(LayoutClass); 1941 uint64_t BaseOffsetInLayoutClass = 1942 LayoutClassLayout.getVBaseClassOffsetInBits(BaseDecl); 1943 1944 LayoutPrimaryAndSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset), 1945 /*BaseIsMorallyVirtual=*/true, 1946 /*BaseIsVirtualInLayoutClass=*/true, 1947 BaseOffsetInLayoutClass); 1948 } 1949 1950 // We only need to check the base for virtual base vtables if it actually 1951 // has virtual bases. 1952 if (BaseDecl->getNumVBases()) 1953 LayoutVTablesForVirtualBases(BaseDecl, VBases); 1954 } 1955} 1956 1957/// dumpLayout - Dump the vtable layout. 1958void VTableBuilder::dumpLayout(llvm::raw_ostream& Out) { 1959 1960 if (isBuildingConstructorVTable()) { 1961 Out << "Construction vtable for ('"; 1962 Out << MostDerivedClass->getQualifiedNameAsString() << "', "; 1963 // FIXME: Don't use / 8 . 1964 Out << MostDerivedClassOffset / 8 << ") in '"; 1965 Out << LayoutClass->getQualifiedNameAsString(); 1966 } else { 1967 Out << "Vtable for '"; 1968 Out << MostDerivedClass->getQualifiedNameAsString(); 1969 } 1970 Out << "' (" << Components.size() << " entries).\n"; 1971 1972 // Iterate through the address points and insert them into a new map where 1973 // they are keyed by the index and not the base object. 1974 // Since an address point can be shared by multiple subobjects, we use an 1975 // STL multimap. 1976 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex; 1977 for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(), 1978 E = AddressPoints.end(); I != E; ++I) { 1979 const BaseSubobject& Base = I->first; 1980 uint64_t Index = I->second; 1981 1982 AddressPointsByIndex.insert(std::make_pair(Index, Base)); 1983 } 1984 1985 for (unsigned I = 0, E = Components.size(); I != E; ++I) { 1986 uint64_t Index = I; 1987 1988 Out << llvm::format("%4d | ", I); 1989 1990 const VTableComponent &Component = Components[I]; 1991 1992 // Dump the component. 1993 switch (Component.getKind()) { 1994 1995 case VTableComponent::CK_VCallOffset: 1996 Out << "vcall_offset (" << Component.getVCallOffset() << ")"; 1997 break; 1998 1999 case VTableComponent::CK_VBaseOffset: 2000 Out << "vbase_offset (" << Component.getVBaseOffset() << ")"; 2001 break; 2002 2003 case VTableComponent::CK_OffsetToTop: 2004 Out << "offset_to_top (" << Component.getOffsetToTop() << ")"; 2005 break; 2006 2007 case VTableComponent::CK_RTTI: 2008 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI"; 2009 break; 2010 2011 case VTableComponent::CK_FunctionPointer: { 2012 const CXXMethodDecl *MD = Component.getFunctionDecl(); 2013 2014 std::string Str = 2015 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, 2016 MD); 2017 Out << Str; 2018 if (MD->isPure()) 2019 Out << " [pure]"; 2020 2021 ThunkInfo Thunk = VTableThunks.lookup(I); 2022 if (!Thunk.isEmpty()) { 2023 // If this function pointer has a return adjustment, dump it. 2024 if (!Thunk.Return.isEmpty()) { 2025 Out << "\n [return adjustment: "; 2026 Out << Thunk.Return.NonVirtual << " non-virtual"; 2027 2028 if (Thunk.Return.VBaseOffsetOffset) { 2029 Out << ", " << Thunk.Return.VBaseOffsetOffset; 2030 Out << " vbase offset offset"; 2031 } 2032 2033 Out << ']'; 2034 } 2035 2036 // If this function pointer has a 'this' pointer adjustment, dump it. 2037 if (!Thunk.This.isEmpty()) { 2038 Out << "\n [this adjustment: "; 2039 Out << Thunk.This.NonVirtual << " non-virtual"; 2040 2041 if (Thunk.This.VCallOffsetOffset) { 2042 Out << ", " << Thunk.This.VCallOffsetOffset; 2043 Out << " vcall offset offset"; 2044 } 2045 2046 Out << ']'; 2047 } 2048 } 2049 2050 break; 2051 } 2052 2053 case VTableComponent::CK_CompleteDtorPointer: 2054 case VTableComponent::CK_DeletingDtorPointer: { 2055 bool IsComplete = 2056 Component.getKind() == VTableComponent::CK_CompleteDtorPointer; 2057 2058 const CXXDestructorDecl *DD = Component.getDestructorDecl(); 2059 2060 Out << DD->getQualifiedNameAsString(); 2061 if (IsComplete) 2062 Out << "() [complete]"; 2063 else 2064 Out << "() [deleting]"; 2065 2066 if (DD->isPure()) 2067 Out << " [pure]"; 2068 2069 ThunkInfo Thunk = VTableThunks.lookup(I); 2070 if (!Thunk.isEmpty()) { 2071 // If this destructor has a 'this' pointer adjustment, dump it. 2072 if (!Thunk.This.isEmpty()) { 2073 Out << "\n [this adjustment: "; 2074 Out << Thunk.This.NonVirtual << " non-virtual"; 2075 2076 if (Thunk.This.VCallOffsetOffset) { 2077 Out << ", " << Thunk.This.VCallOffsetOffset; 2078 Out << " vcall offset offset"; 2079 } 2080 2081 Out << ']'; 2082 } 2083 } 2084 2085 break; 2086 } 2087 2088 case VTableComponent::CK_UnusedFunctionPointer: { 2089 const CXXMethodDecl *MD = Component.getUnusedFunctionDecl(); 2090 2091 std::string Str = 2092 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, 2093 MD); 2094 Out << "[unused] " << Str; 2095 if (MD->isPure()) 2096 Out << " [pure]"; 2097 } 2098 2099 } 2100 2101 Out << '\n'; 2102 2103 // Dump the next address point. 2104 uint64_t NextIndex = Index + 1; 2105 if (AddressPointsByIndex.count(NextIndex)) { 2106 if (AddressPointsByIndex.count(NextIndex) == 1) { 2107 const BaseSubobject &Base = 2108 AddressPointsByIndex.find(NextIndex)->second; 2109 2110 // FIXME: Instead of dividing by 8, we should be using CharUnits. 2111 Out << " -- (" << Base.getBase()->getQualifiedNameAsString(); 2112 Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n"; 2113 } else { 2114 uint64_t BaseOffset = 2115 AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset(); 2116 2117 // We store the class names in a set to get a stable order. 2118 std::set<std::string> ClassNames; 2119 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I = 2120 AddressPointsByIndex.lower_bound(NextIndex), E = 2121 AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) { 2122 assert(I->second.getBaseOffset() == BaseOffset && 2123 "Invalid base offset!"); 2124 const CXXRecordDecl *RD = I->second.getBase(); 2125 ClassNames.insert(RD->getQualifiedNameAsString()); 2126 } 2127 2128 for (std::set<std::string>::const_iterator I = ClassNames.begin(), 2129 E = ClassNames.end(); I != E; ++I) { 2130 // FIXME: Instead of dividing by 8, we should be using CharUnits. 2131 Out << " -- (" << *I; 2132 Out << ", " << BaseOffset / 8 << ") vtable address --\n"; 2133 } 2134 } 2135 } 2136 } 2137 2138 Out << '\n'; 2139 2140 if (isBuildingConstructorVTable()) 2141 return; 2142 2143 if (MostDerivedClass->getNumVBases()) { 2144 // We store the virtual base class names and their offsets in a map to get 2145 // a stable order. 2146 2147 std::map<std::string, int64_t> ClassNamesAndOffsets; 2148 for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(), 2149 E = VBaseOffsetOffsets.end(); I != E; ++I) { 2150 std::string ClassName = I->first->getQualifiedNameAsString(); 2151 int64_t OffsetOffset = I->second; 2152 ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset)); 2153 } 2154 2155 Out << "Virtual base offset offsets for '"; 2156 Out << MostDerivedClass->getQualifiedNameAsString() << "' ("; 2157 Out << ClassNamesAndOffsets.size(); 2158 Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n"; 2159 2160 for (std::map<std::string, int64_t>::const_iterator I = 2161 ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end(); 2162 I != E; ++I) 2163 Out << " " << I->first << " | " << I->second << '\n'; 2164 2165 Out << "\n"; 2166 } 2167 2168 if (!Thunks.empty()) { 2169 // We store the method names in a map to get a stable order. 2170 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls; 2171 2172 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end(); 2173 I != E; ++I) { 2174 const CXXMethodDecl *MD = I->first; 2175 std::string MethodName = 2176 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, 2177 MD); 2178 2179 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD)); 2180 } 2181 2182 for (std::map<std::string, const CXXMethodDecl *>::const_iterator I = 2183 MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end(); 2184 I != E; ++I) { 2185 const std::string &MethodName = I->first; 2186 const CXXMethodDecl *MD = I->second; 2187 2188 ThunkInfoVectorTy ThunksVector = Thunks[MD]; 2189 std::sort(ThunksVector.begin(), ThunksVector.end()); 2190 2191 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size(); 2192 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n"; 2193 2194 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) { 2195 const ThunkInfo &Thunk = ThunksVector[I]; 2196 2197 Out << llvm::format("%4d | ", I); 2198 2199 // If this function pointer has a return pointer adjustment, dump it. 2200 if (!Thunk.Return.isEmpty()) { 2201 Out << "return adjustment: " << Thunk.This.NonVirtual; 2202 Out << " non-virtual"; 2203 if (Thunk.Return.VBaseOffsetOffset) { 2204 Out << ", " << Thunk.Return.VBaseOffsetOffset; 2205 Out << " vbase offset offset"; 2206 } 2207 2208 if (!Thunk.This.isEmpty()) 2209 Out << "\n "; 2210 } 2211 2212 // If this function pointer has a 'this' pointer adjustment, dump it. 2213 if (!Thunk.This.isEmpty()) { 2214 Out << "this adjustment: "; 2215 Out << Thunk.This.NonVirtual << " non-virtual"; 2216 2217 if (Thunk.This.VCallOffsetOffset) { 2218 Out << ", " << Thunk.This.VCallOffsetOffset; 2219 Out << " vcall offset offset"; 2220 } 2221 } 2222 2223 Out << '\n'; 2224 } 2225 2226 Out << '\n'; 2227 2228 } 2229 } 2230} 2231 2232} 2233 2234void CodeGenVTables::ComputeMethodVTableIndices(const CXXRecordDecl *RD) { 2235 2236 // Itanium C++ ABI 2.5.2: 2237 // The order of the virtual function pointers in a virtual table is the 2238 // order of declaration of the corresponding member functions in the class. 2239 // 2240 // There is an entry for any virtual function declared in a class, 2241 // whether it is a new function or overrides a base class function, 2242 // unless it overrides a function from the primary base, and conversion 2243 // between their return types does not require an adjustment. 2244 2245 int64_t CurrentIndex = 0; 2246 2247 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 2248 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 2249 2250 if (PrimaryBase) { 2251 assert(PrimaryBase->isDefinition() && 2252 "Should have the definition decl of the primary base!"); 2253 2254 // Since the record decl shares its vtable pointer with the primary base 2255 // we need to start counting at the end of the primary base's vtable. 2256 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase); 2257 } 2258 2259 // Collect all the primary bases, so we can check whether methods override 2260 // a method from the base. 2261 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases; 2262 for (ASTRecordLayout::primary_base_info_iterator 2263 I = Layout.primary_base_begin(), E = Layout.primary_base_end(); 2264 I != E; ++I) 2265 PrimaryBases.insert((*I).getBase()); 2266 2267 const CXXDestructorDecl *ImplicitVirtualDtor = 0; 2268 2269 for (CXXRecordDecl::method_iterator i = RD->method_begin(), 2270 e = RD->method_end(); i != e; ++i) { 2271 const CXXMethodDecl *MD = *i; 2272 2273 // We only want virtual methods. 2274 if (!MD->isVirtual()) 2275 continue; 2276 2277 // Check if this method overrides a method in the primary base. 2278 if (const CXXMethodDecl *OverriddenMD = 2279 FindNearestOverriddenMethod(MD, PrimaryBases)) { 2280 // Check if converting from the return type of the method to the 2281 // return type of the overridden method requires conversion. 2282 if (ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD, 2283 OverriddenMD).isEmpty()) { 2284 // This index is shared between the index in the vtable of the primary 2285 // base class. 2286 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 2287 const CXXDestructorDecl *OverriddenDD = 2288 cast<CXXDestructorDecl>(OverriddenMD); 2289 2290 // Add both the complete and deleting entries. 2291 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = 2292 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Complete)); 2293 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = 2294 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting)); 2295 } else { 2296 MethodVTableIndices[MD] = getMethodVTableIndex(OverriddenMD); 2297 } 2298 2299 // We don't need to add an entry for this method. 2300 continue; 2301 } 2302 } 2303 2304 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 2305 if (MD->isImplicit()) { 2306 assert(!ImplicitVirtualDtor && 2307 "Did already see an implicit virtual dtor!"); 2308 ImplicitVirtualDtor = DD; 2309 continue; 2310 } 2311 2312 // Add the complete dtor. 2313 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++; 2314 2315 // Add the deleting dtor. 2316 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++; 2317 } else { 2318 // Add the entry. 2319 MethodVTableIndices[MD] = CurrentIndex++; 2320 } 2321 } 2322 2323 if (ImplicitVirtualDtor) { 2324 // Itanium C++ ABI 2.5.2: 2325 // If a class has an implicitly-defined virtual destructor, 2326 // its entries come after the declared virtual function pointers. 2327 2328 // Add the complete dtor. 2329 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] = 2330 CurrentIndex++; 2331 2332 // Add the deleting dtor. 2333 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] = 2334 CurrentIndex++; 2335 } 2336 2337 NumVirtualFunctionPointers[RD] = CurrentIndex; 2338} 2339 2340bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) { 2341 assert(RD->isDynamicClass() && "Non dynamic classes have no VTable."); 2342 2343 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind(); 2344 if (TSK == TSK_ExplicitInstantiationDeclaration) 2345 return false; 2346 2347 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD); 2348 if (!KeyFunction) 2349 return true; 2350 2351 // Itanium C++ ABI, 5.2.6 Instantiated Templates: 2352 // An instantiation of a class template requires: 2353 // - In the object where instantiated, the virtual table... 2354 if (TSK == TSK_ImplicitInstantiation || 2355 TSK == TSK_ExplicitInstantiationDefinition) 2356 return true; 2357 2358 return KeyFunction->hasBody(); 2359} 2360 2361uint64_t CodeGenVTables::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) { 2362 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = 2363 NumVirtualFunctionPointers.find(RD); 2364 if (I != NumVirtualFunctionPointers.end()) 2365 return I->second; 2366 2367 ComputeMethodVTableIndices(RD); 2368 2369 I = NumVirtualFunctionPointers.find(RD); 2370 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!"); 2371 return I->second; 2372} 2373 2374uint64_t CodeGenVTables::getMethodVTableIndex(GlobalDecl GD) { 2375 MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD); 2376 if (I != MethodVTableIndices.end()) 2377 return I->second; 2378 2379 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); 2380 2381 ComputeMethodVTableIndices(RD); 2382 2383 I = MethodVTableIndices.find(GD); 2384 assert(I != MethodVTableIndices.end() && "Did not find index!"); 2385 return I->second; 2386} 2387 2388int64_t CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, 2389 const CXXRecordDecl *VBase) { 2390 ClassPairTy ClassPair(RD, VBase); 2391 2392 VirtualBaseClassOffsetOffsetsMapTy::iterator I = 2393 VirtualBaseClassOffsetOffsets.find(ClassPair); 2394 if (I != VirtualBaseClassOffsetOffsets.end()) 2395 return I->second; 2396 2397 VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0, 2398 BaseSubobject(RD, 0), 2399 /*BaseIsVirtual=*/false, 2400 /*OffsetInLayoutClass=*/0); 2401 2402 for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I = 2403 Builder.getVBaseOffsetOffsets().begin(), 2404 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) { 2405 // Insert all types. 2406 ClassPairTy ClassPair(RD, I->first); 2407 2408 VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second)); 2409 } 2410 2411 I = VirtualBaseClassOffsetOffsets.find(ClassPair); 2412 assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!"); 2413 2414 return I->second; 2415} 2416 2417uint64_t 2418CodeGenVTables::getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD) { 2419 assert(AddressPoints.count(std::make_pair(RD, Base)) && 2420 "Did not find address point!"); 2421 2422 uint64_t AddressPoint = AddressPoints.lookup(std::make_pair(RD, Base)); 2423 assert(AddressPoint && "Address point must not be zero!"); 2424 2425 return AddressPoint; 2426} 2427 2428llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, 2429 const ThunkInfo &Thunk) { 2430 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 2431 2432 // Compute the mangled name. 2433 llvm::SmallString<256> Name; 2434 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) 2435 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), 2436 Thunk.This, Name); 2437 else 2438 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Name); 2439 2440 const llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD); 2441 return GetOrCreateLLVMFunction(Name, Ty, GD); 2442} 2443 2444static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF, 2445 llvm::Value *Ptr, 2446 int64_t NonVirtualAdjustment, 2447 int64_t VirtualAdjustment) { 2448 if (!NonVirtualAdjustment && !VirtualAdjustment) 2449 return Ptr; 2450 2451 const llvm::Type *Int8PtrTy = 2452 llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 2453 2454 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy); 2455 2456 if (NonVirtualAdjustment) { 2457 // Do the non-virtual adjustment. 2458 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); 2459 } 2460 2461 if (VirtualAdjustment) { 2462 const llvm::Type *PtrDiffTy = 2463 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 2464 2465 // Do the virtual adjustment. 2466 llvm::Value *VTablePtrPtr = 2467 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo()); 2468 2469 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); 2470 2471 llvm::Value *OffsetPtr = 2472 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); 2473 2474 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); 2475 2476 // Load the adjustment offset from the vtable. 2477 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr); 2478 2479 // Adjust our pointer. 2480 V = CGF.Builder.CreateInBoundsGEP(V, Offset); 2481 } 2482 2483 // Cast back to the original type. 2484 return CGF.Builder.CreateBitCast(V, Ptr->getType()); 2485} 2486 2487static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD, 2488 const ThunkInfo &Thunk, llvm::Function *Fn) { 2489 CGM.setGlobalVisibility(Fn, MD, /*ForDef*/ true); 2490 2491 if (!CGM.getCodeGenOpts().HiddenWeakVTables) 2492 return; 2493 2494 // If the thunk has weak/linkonce linkage, but the function must be 2495 // emitted in every translation unit that references it, then we can 2496 // emit its thunks with hidden visibility, since its thunks must be 2497 // emitted when the function is. 2498 2499 // This follows CodeGenModule::setTypeVisibility; see the comments 2500 // there for explanation. 2501 2502 if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage && 2503 Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) || 2504 Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility) 2505 return; 2506 2507 if (MD->hasAttr<VisibilityAttr>()) 2508 return; 2509 2510 switch (MD->getTemplateSpecializationKind()) { 2511 case TSK_ExplicitInstantiationDefinition: 2512 case TSK_ExplicitInstantiationDeclaration: 2513 return; 2514 2515 case TSK_Undeclared: 2516 break; 2517 2518 case TSK_ExplicitSpecialization: 2519 case TSK_ImplicitInstantiation: 2520 if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables) 2521 return; 2522 break; 2523 } 2524 2525 // If there's an explicit definition, and that definition is 2526 // out-of-line, then we can't assume that all users will have a 2527 // definition to emit. 2528 const FunctionDecl *Def = 0; 2529 if (MD->hasBody(Def) && Def->isOutOfLine()) 2530 return; 2531 2532 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility); 2533} 2534 2535void CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD, 2536 const ThunkInfo &Thunk) { 2537 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 2538 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 2539 QualType ResultType = FPT->getResultType(); 2540 QualType ThisType = MD->getThisType(getContext()); 2541 2542 FunctionArgList FunctionArgs; 2543 2544 // FIXME: It would be nice if more of this code could be shared with 2545 // CodeGenFunction::GenerateCode. 2546 2547 // Create the implicit 'this' parameter declaration. 2548 CurGD = GD; 2549 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs); 2550 2551 // Add the rest of the parameters. 2552 for (FunctionDecl::param_const_iterator I = MD->param_begin(), 2553 E = MD->param_end(); I != E; ++I) { 2554 ParmVarDecl *Param = *I; 2555 2556 FunctionArgs.push_back(std::make_pair(Param, Param->getType())); 2557 } 2558 2559 StartFunction(GlobalDecl(), ResultType, Fn, FunctionArgs, SourceLocation()); 2560 2561 CGM.getCXXABI().EmitInstanceFunctionProlog(*this); 2562 2563 // Adjust the 'this' pointer if necessary. 2564 llvm::Value *AdjustedThisPtr = 2565 PerformTypeAdjustment(*this, LoadCXXThis(), 2566 Thunk.This.NonVirtual, 2567 Thunk.This.VCallOffsetOffset); 2568 2569 CallArgList CallArgs; 2570 2571 // Add our adjusted 'this' pointer. 2572 CallArgs.push_back(std::make_pair(RValue::get(AdjustedThisPtr), ThisType)); 2573 2574 // Add the rest of the parameters. 2575 for (FunctionDecl::param_const_iterator I = MD->param_begin(), 2576 E = MD->param_end(); I != E; ++I) { 2577 ParmVarDecl *Param = *I; 2578 QualType ArgType = Param->getType(); 2579 RValue Arg = EmitDelegateCallArg(Param); 2580 2581 CallArgs.push_back(std::make_pair(Arg, ArgType)); 2582 } 2583 2584 // Get our callee. 2585 const llvm::Type *Ty = 2586 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(GD), 2587 FPT->isVariadic()); 2588 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty); 2589 2590 const CGFunctionInfo &FnInfo = 2591 CGM.getTypes().getFunctionInfo(ResultType, CallArgs, 2592 FPT->getExtInfo()); 2593 2594 // Determine whether we have a return value slot to use. 2595 ReturnValueSlot Slot; 2596 if (!ResultType->isVoidType() && 2597 FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect && 2598 hasAggregateLLVMType(CurFnInfo->getReturnType())) 2599 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified()); 2600 2601 // Now emit our call. 2602 RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD); 2603 2604 if (!Thunk.Return.isEmpty()) { 2605 // Emit the return adjustment. 2606 bool NullCheckValue = !ResultType->isReferenceType(); 2607 2608 llvm::BasicBlock *AdjustNull = 0; 2609 llvm::BasicBlock *AdjustNotNull = 0; 2610 llvm::BasicBlock *AdjustEnd = 0; 2611 2612 llvm::Value *ReturnValue = RV.getScalarVal(); 2613 2614 if (NullCheckValue) { 2615 AdjustNull = createBasicBlock("adjust.null"); 2616 AdjustNotNull = createBasicBlock("adjust.notnull"); 2617 AdjustEnd = createBasicBlock("adjust.end"); 2618 2619 llvm::Value *IsNull = Builder.CreateIsNull(ReturnValue); 2620 Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull); 2621 EmitBlock(AdjustNotNull); 2622 } 2623 2624 ReturnValue = PerformTypeAdjustment(*this, ReturnValue, 2625 Thunk.Return.NonVirtual, 2626 Thunk.Return.VBaseOffsetOffset); 2627 2628 if (NullCheckValue) { 2629 Builder.CreateBr(AdjustEnd); 2630 EmitBlock(AdjustNull); 2631 Builder.CreateBr(AdjustEnd); 2632 EmitBlock(AdjustEnd); 2633 2634 llvm::PHINode *PHI = Builder.CreatePHI(ReturnValue->getType()); 2635 PHI->reserveOperandSpace(2); 2636 PHI->addIncoming(ReturnValue, AdjustNotNull); 2637 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()), 2638 AdjustNull); 2639 ReturnValue = PHI; 2640 } 2641 2642 RV = RValue::get(ReturnValue); 2643 } 2644 2645 if (!ResultType->isVoidType() && Slot.isNull()) 2646 CGM.getCXXABI().EmitReturnFromThunk(CGF, RV, ResultType); 2647 2648 FinishFunction(); 2649 2650 // Set the right linkage. 2651 CGM.setFunctionLinkage(MD, Fn); 2652 2653 // Set the right visibility. 2654 setThunkVisibility(CGM, MD, Thunk, Fn); 2655} 2656 2657void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk) 2658{ 2659 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk); 2660 2661 // Strip off a bitcast if we got one back. 2662 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 2663 assert(CE->getOpcode() == llvm::Instruction::BitCast); 2664 Entry = CE->getOperand(0); 2665 } 2666 2667 // There's already a declaration with the same name, check if it has the same 2668 // type or if we need to replace it. 2669 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != 2670 CGM.getTypes().GetFunctionTypeForVTable(GD)) { 2671 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry); 2672 2673 // If the types mismatch then we have to rewrite the definition. 2674 assert(OldThunkFn->isDeclaration() && 2675 "Shouldn't replace non-declaration"); 2676 2677 // Remove the name from the old thunk function and get a new thunk. 2678 OldThunkFn->setName(llvm::StringRef()); 2679 Entry = CGM.GetAddrOfThunk(GD, Thunk); 2680 2681 // If needed, replace the old thunk with a bitcast. 2682 if (!OldThunkFn->use_empty()) { 2683 llvm::Constant *NewPtrForOldDecl = 2684 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType()); 2685 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl); 2686 } 2687 2688 // Remove the old thunk. 2689 OldThunkFn->eraseFromParent(); 2690 } 2691 2692 // Actually generate the thunk body. 2693 llvm::Function *ThunkFn = cast<llvm::Function>(Entry); 2694 CodeGenFunction(CGM).GenerateThunk(ThunkFn, GD, Thunk); 2695} 2696 2697void CodeGenVTables::EmitThunks(GlobalDecl GD) 2698{ 2699 const CXXMethodDecl *MD = 2700 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl(); 2701 2702 // We don't need to generate thunks for the base destructor. 2703 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 2704 return; 2705 2706 const CXXRecordDecl *RD = MD->getParent(); 2707 2708 // Compute VTable related info for this class. 2709 ComputeVTableRelatedInformation(RD, false); 2710 2711 ThunksMapTy::const_iterator I = Thunks.find(MD); 2712 if (I == Thunks.end()) { 2713 // We did not find a thunk for this method. 2714 return; 2715 } 2716 2717 const ThunkInfoVectorTy &ThunkInfoVector = I->second; 2718 for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I) 2719 EmitThunk(GD, ThunkInfoVector[I]); 2720} 2721 2722void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD, 2723 bool RequireVTable) { 2724 VTableLayoutData &Entry = VTableLayoutMap[RD]; 2725 2726 // We may need to generate a definition for this vtable. 2727 if (RequireVTable && !Entry.getInt()) { 2728 if (ShouldEmitVTableInThisTU(RD)) 2729 CGM.DeferredVTables.push_back(RD); 2730 2731 Entry.setInt(true); 2732 } 2733 2734 // Check if we've computed this information before. 2735 if (Entry.getPointer()) 2736 return; 2737 2738 VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD); 2739 2740 // Add the VTable layout. 2741 uint64_t NumVTableComponents = Builder.getNumVTableComponents(); 2742 uint64_t *LayoutData = new uint64_t[NumVTableComponents + 1]; 2743 Entry.setPointer(LayoutData); 2744 2745 // Store the number of components. 2746 LayoutData[0] = NumVTableComponents; 2747 2748 // Store the components. 2749 std::copy(Builder.vtable_components_data_begin(), 2750 Builder.vtable_components_data_end(), 2751 &LayoutData[1]); 2752 2753 // Add the known thunks. 2754 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end()); 2755 2756 // Add the thunks needed in this vtable. 2757 assert(!VTableThunksMap.count(RD) && 2758 "Thunks already exists for this vtable!"); 2759 2760 VTableThunksTy &VTableThunks = VTableThunksMap[RD]; 2761 VTableThunks.append(Builder.vtable_thunks_begin(), 2762 Builder.vtable_thunks_end()); 2763 2764 // Sort them. 2765 std::sort(VTableThunks.begin(), VTableThunks.end()); 2766 2767 // Add the address points. 2768 for (VTableBuilder::AddressPointsMapTy::const_iterator I = 2769 Builder.address_points_begin(), E = Builder.address_points_end(); 2770 I != E; ++I) { 2771 2772 uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)]; 2773 2774 // Check if we already have the address points for this base. 2775 assert(!AddressPoint && "Address point already exists for this base!"); 2776 2777 AddressPoint = I->second; 2778 } 2779 2780 // If we don't have the vbase information for this class, insert it. 2781 // getVirtualBaseOffsetOffset will compute it separately without computing 2782 // the rest of the vtable related information. 2783 if (!RD->getNumVBases()) 2784 return; 2785 2786 const RecordType *VBaseRT = 2787 RD->vbases_begin()->getType()->getAs<RecordType>(); 2788 const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl()); 2789 2790 if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase))) 2791 return; 2792 2793 for (VTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I = 2794 Builder.getVBaseOffsetOffsets().begin(), 2795 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) { 2796 // Insert all types. 2797 ClassPairTy ClassPair(RD, I->first); 2798 2799 VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second)); 2800 } 2801} 2802 2803llvm::Constant * 2804CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD, 2805 const uint64_t *Components, 2806 unsigned NumComponents, 2807 const VTableThunksTy &VTableThunks) { 2808 llvm::SmallVector<llvm::Constant *, 64> Inits; 2809 2810 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); 2811 2812 const llvm::Type *PtrDiffTy = 2813 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); 2814 2815 QualType ClassType = CGM.getContext().getTagDeclType(RD); 2816 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType); 2817 2818 unsigned NextVTableThunkIndex = 0; 2819 2820 llvm::Constant* PureVirtualFn = 0; 2821 2822 for (unsigned I = 0; I != NumComponents; ++I) { 2823 VTableComponent Component = 2824 VTableComponent::getFromOpaqueInteger(Components[I]); 2825 2826 llvm::Constant *Init = 0; 2827 2828 switch (Component.getKind()) { 2829 case VTableComponent::CK_VCallOffset: 2830 Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVCallOffset()); 2831 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); 2832 break; 2833 case VTableComponent::CK_VBaseOffset: 2834 Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVBaseOffset()); 2835 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); 2836 break; 2837 case VTableComponent::CK_OffsetToTop: 2838 Init = llvm::ConstantInt::get(PtrDiffTy, Component.getOffsetToTop()); 2839 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); 2840 break; 2841 case VTableComponent::CK_RTTI: 2842 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy); 2843 break; 2844 case VTableComponent::CK_FunctionPointer: 2845 case VTableComponent::CK_CompleteDtorPointer: 2846 case VTableComponent::CK_DeletingDtorPointer: { 2847 GlobalDecl GD; 2848 2849 // Get the right global decl. 2850 switch (Component.getKind()) { 2851 default: 2852 llvm_unreachable("Unexpected vtable component kind"); 2853 case VTableComponent::CK_FunctionPointer: 2854 GD = Component.getFunctionDecl(); 2855 break; 2856 case VTableComponent::CK_CompleteDtorPointer: 2857 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete); 2858 break; 2859 case VTableComponent::CK_DeletingDtorPointer: 2860 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting); 2861 break; 2862 } 2863 2864 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) { 2865 // We have a pure virtual member function. 2866 if (!PureVirtualFn) { 2867 const llvm::FunctionType *Ty = 2868 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()), 2869 /*isVarArg=*/false); 2870 PureVirtualFn = 2871 CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"); 2872 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn, 2873 Int8PtrTy); 2874 } 2875 2876 Init = PureVirtualFn; 2877 } else { 2878 // Check if we should use a thunk. 2879 if (NextVTableThunkIndex < VTableThunks.size() && 2880 VTableThunks[NextVTableThunkIndex].first == I) { 2881 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second; 2882 2883 Init = CGM.GetAddrOfThunk(GD, Thunk); 2884 2885 NextVTableThunkIndex++; 2886 } else { 2887 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD); 2888 2889 Init = CGM.GetAddrOfFunction(GD, Ty); 2890 } 2891 2892 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy); 2893 } 2894 break; 2895 } 2896 2897 case VTableComponent::CK_UnusedFunctionPointer: 2898 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy); 2899 break; 2900 }; 2901 2902 Inits.push_back(Init); 2903 } 2904 2905 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents); 2906 return llvm::ConstantArray::get(ArrayType, Inits.data(), Inits.size()); 2907} 2908 2909/// GetGlobalVariable - Will return a global variable of the given type. 2910/// If a variable with a different type already exists then a new variable 2911/// with the right type will be created. 2912/// FIXME: We should move this to CodeGenModule and rename it to something 2913/// better and then use it in CGVTT and CGRTTI. 2914static llvm::GlobalVariable * 2915GetGlobalVariable(llvm::Module &Module, llvm::StringRef Name, 2916 const llvm::Type *Ty, 2917 llvm::GlobalValue::LinkageTypes Linkage) { 2918 2919 llvm::GlobalVariable *GV = Module.getNamedGlobal(Name); 2920 llvm::GlobalVariable *OldGV = 0; 2921 2922 if (GV) { 2923 // Check if the variable has the right type. 2924 if (GV->getType()->getElementType() == Ty) 2925 return GV; 2926 2927 assert(GV->isDeclaration() && "Declaration has wrong type!"); 2928 2929 OldGV = GV; 2930 } 2931 2932 // Create a new variable. 2933 GV = new llvm::GlobalVariable(Module, Ty, /*isConstant=*/true, 2934 Linkage, 0, Name); 2935 2936 if (OldGV) { 2937 // Replace occurrences of the old variable if needed. 2938 GV->takeName(OldGV); 2939 2940 if (!OldGV->use_empty()) { 2941 llvm::Constant *NewPtrForOldDecl = 2942 llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 2943 OldGV->replaceAllUsesWith(NewPtrForOldDecl); 2944 } 2945 2946 OldGV->eraseFromParent(); 2947 } 2948 2949 return GV; 2950} 2951 2952llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) { 2953 llvm::SmallString<256> OutName; 2954 CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, OutName); 2955 llvm::StringRef Name = OutName.str(); 2956 2957 ComputeVTableRelatedInformation(RD, true); 2958 2959 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); 2960 llvm::ArrayType *ArrayType = 2961 llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD)); 2962 2963 return GetGlobalVariable(CGM.getModule(), Name, ArrayType, 2964 llvm::GlobalValue::ExternalLinkage); 2965} 2966 2967void 2968CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable, 2969 llvm::GlobalVariable::LinkageTypes Linkage, 2970 const CXXRecordDecl *RD) { 2971 // Dump the vtable layout if necessary. 2972 if (CGM.getLangOptions().DumpVTableLayouts) { 2973 VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD); 2974 2975 Builder.dumpLayout(llvm::errs()); 2976 } 2977 2978 assert(VTableThunksMap.count(RD) && 2979 "No thunk status for this record decl!"); 2980 2981 const VTableThunksTy& Thunks = VTableThunksMap[RD]; 2982 2983 // Create and set the initializer. 2984 llvm::Constant *Init = 2985 CreateVTableInitializer(RD, getVTableComponentsData(RD), 2986 getNumVTableComponents(RD), Thunks); 2987 VTable->setInitializer(Init); 2988 2989 // Set the correct linkage. 2990 VTable->setLinkage(Linkage); 2991 2992 // Set the right visibility. 2993 CGM.setTypeVisibility(VTable, RD, /*ForRTTI*/ false, /*ForDef*/ true); 2994} 2995 2996llvm::GlobalVariable * 2997CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD, 2998 const BaseSubobject &Base, 2999 bool BaseIsVirtual, 3000 VTableAddressPointsMapTy& AddressPoints) { 3001 VTableBuilder Builder(*this, Base.getBase(), Base.getBaseOffset(), 3002 /*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD); 3003 3004 // Dump the vtable layout if necessary. 3005 if (CGM.getLangOptions().DumpVTableLayouts) 3006 Builder.dumpLayout(llvm::errs()); 3007 3008 // Add the address points. 3009 AddressPoints.insert(Builder.address_points_begin(), 3010 Builder.address_points_end()); 3011 3012 // Get the mangled construction vtable name. 3013 llvm::SmallString<256> OutName; 3014 CGM.getCXXABI().getMangleContext(). 3015 mangleCXXCtorVTable(RD, Base.getBaseOffset() / 8, Base.getBase(), OutName); 3016 llvm::StringRef Name = OutName.str(); 3017 3018 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); 3019 llvm::ArrayType *ArrayType = 3020 llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents()); 3021 3022 // Create the variable that will hold the construction vtable. 3023 llvm::GlobalVariable *VTable = 3024 GetGlobalVariable(CGM.getModule(), Name, ArrayType, 3025 llvm::GlobalValue::InternalLinkage); 3026 3027 // Add the thunks. 3028 VTableThunksTy VTableThunks; 3029 VTableThunks.append(Builder.vtable_thunks_begin(), 3030 Builder.vtable_thunks_end()); 3031 3032 // Sort them. 3033 std::sort(VTableThunks.begin(), VTableThunks.end()); 3034 3035 // Create and set the initializer. 3036 llvm::Constant *Init = 3037 CreateVTableInitializer(Base.getBase(), 3038 Builder.vtable_components_data_begin(), 3039 Builder.getNumVTableComponents(), VTableThunks); 3040 VTable->setInitializer(Init); 3041 3042 return VTable; 3043} 3044 3045void 3046CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage, 3047 const CXXRecordDecl *RD) { 3048 llvm::GlobalVariable *&VTable = VTables[RD]; 3049 if (VTable) { 3050 assert(VTable->getInitializer() && "VTable doesn't have a definition!"); 3051 return; 3052 } 3053 3054 VTable = GetAddrOfVTable(RD); 3055 EmitVTableDefinition(VTable, Linkage, RD); 3056 3057 GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD); 3058 3059 // If this is the magic class __cxxabiv1::__fundamental_type_info, 3060 // we will emit the typeinfo for the fundamental types. This is the 3061 // same behaviour as GCC. 3062 const DeclContext *DC = RD->getDeclContext(); 3063 if (RD->getIdentifier() && 3064 RD->getIdentifier()->isStr("__fundamental_type_info") && 3065 isa<NamespaceDecl>(DC) && 3066 cast<NamespaceDecl>(DC)->getIdentifier() && 3067 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") && 3068 DC->getParent()->isTranslationUnit()) 3069 CGM.EmitFundamentalRTTIDescriptors(); 3070} 3071