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