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