VTTBuilder.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //===- VTTBuilder.cpp - C++ VTT layout builder ----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This contains code dealing with generation of the layout of virtual table
  10. // tables (VTT).
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/VTTBuilder.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/BaseSubobject.h"
  16. #include "clang/AST/CharUnits.h"
  17. #include "clang/AST/Decl.h"
  18. #include "clang/AST/DeclCXX.h"
  19. #include "clang/AST/RecordLayout.h"
  20. #include "clang/AST/Type.h"
  21. #include "clang/Basic/LLVM.h"
  22. #include "llvm/Support/Casting.h"
  23. #include <cassert>
  24. #include <cstdint>
  25. using namespace clang;
  26. #define DUMP_OVERRIDERS 0
  27. VTTBuilder::VTTBuilder(ASTContext &Ctx,
  28. const CXXRecordDecl *MostDerivedClass,
  29. bool GenerateDefinition)
  30. : Ctx(Ctx), MostDerivedClass(MostDerivedClass),
  31. MostDerivedClassLayout(Ctx.getASTRecordLayout(MostDerivedClass)),
  32. GenerateDefinition(GenerateDefinition) {
  33. // Lay out this VTT.
  34. LayoutVTT(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
  35. /*BaseIsVirtual=*/false);
  36. }
  37. void VTTBuilder::AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
  38. const CXXRecordDecl *VTableClass) {
  39. // Store the vtable pointer index if we're generating the primary VTT.
  40. if (VTableClass == MostDerivedClass) {
  41. assert(!SecondaryVirtualPointerIndices.count(Base) &&
  42. "A virtual pointer index already exists for this base subobject!");
  43. SecondaryVirtualPointerIndices[Base] = VTTComponents.size();
  44. }
  45. if (!GenerateDefinition) {
  46. VTTComponents.push_back(VTTComponent());
  47. return;
  48. }
  49. VTTComponents.push_back(VTTComponent(VTableIndex, Base));
  50. }
  51. void VTTBuilder::LayoutSecondaryVTTs(BaseSubobject Base) {
  52. const CXXRecordDecl *RD = Base.getBase();
  53. for (const auto &I : RD->bases()) {
  54. // Don't layout virtual bases.
  55. if (I.isVirtual())
  56. continue;
  57. const auto *BaseDecl =
  58. cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
  59. const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
  60. CharUnits BaseOffset = Base.getBaseOffset() +
  61. Layout.getBaseClassOffset(BaseDecl);
  62. // Layout the VTT for this base.
  63. LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/false);
  64. }
  65. }
  66. void
  67. VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
  68. bool BaseIsMorallyVirtual,
  69. uint64_t VTableIndex,
  70. const CXXRecordDecl *VTableClass,
  71. VisitedVirtualBasesSetTy &VBases) {
  72. const CXXRecordDecl *RD = Base.getBase();
  73. // We're not interested in bases that don't have virtual bases, and not
  74. // morally virtual bases.
  75. if (!RD->getNumVBases() && !BaseIsMorallyVirtual)
  76. return;
  77. for (const auto &I : RD->bases()) {
  78. const auto *BaseDecl =
  79. cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
  80. // Itanium C++ ABI 2.6.2:
  81. // Secondary virtual pointers are present for all bases with either
  82. // virtual bases or virtual function declarations overridden along a
  83. // virtual path.
  84. //
  85. // If the base class is not dynamic, we don't want to add it, nor any
  86. // of its base classes.
  87. if (!BaseDecl->isDynamicClass())
  88. continue;
  89. bool BaseDeclIsMorallyVirtual = BaseIsMorallyVirtual;
  90. bool BaseDeclIsNonVirtualPrimaryBase = false;
  91. CharUnits BaseOffset;
  92. if (I.isVirtual()) {
  93. // Ignore virtual bases that we've already visited.
  94. if (!VBases.insert(BaseDecl).second)
  95. continue;
  96. BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
  97. BaseDeclIsMorallyVirtual = true;
  98. } else {
  99. const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
  100. BaseOffset = Base.getBaseOffset() +
  101. Layout.getBaseClassOffset(BaseDecl);
  102. if (!Layout.isPrimaryBaseVirtual() &&
  103. Layout.getPrimaryBase() == BaseDecl)
  104. BaseDeclIsNonVirtualPrimaryBase = true;
  105. }
  106. // Itanium C++ ABI 2.6.2:
  107. // Secondary virtual pointers: for each base class X which (a) has virtual
  108. // bases or is reachable along a virtual path from D, and (b) is not a
  109. // non-virtual primary base, the address of the virtual table for X-in-D
  110. // or an appropriate construction virtual table.
  111. if (!BaseDeclIsNonVirtualPrimaryBase &&
  112. (BaseDecl->getNumVBases() || BaseDeclIsMorallyVirtual)) {
  113. // Add the vtable pointer.
  114. AddVTablePointer(BaseSubobject(BaseDecl, BaseOffset), VTableIndex,
  115. VTableClass);
  116. }
  117. // And lay out the secondary virtual pointers for the base class.
  118. LayoutSecondaryVirtualPointers(BaseSubobject(BaseDecl, BaseOffset),
  119. BaseDeclIsMorallyVirtual, VTableIndex,
  120. VTableClass, VBases);
  121. }
  122. }
  123. void
  124. VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
  125. uint64_t VTableIndex) {
  126. VisitedVirtualBasesSetTy VBases;
  127. LayoutSecondaryVirtualPointers(Base, /*BaseIsMorallyVirtual=*/false,
  128. VTableIndex, Base.getBase(), VBases);
  129. }
  130. void VTTBuilder::LayoutVirtualVTTs(const CXXRecordDecl *RD,
  131. VisitedVirtualBasesSetTy &VBases) {
  132. for (const auto &I : RD->bases()) {
  133. const auto *BaseDecl =
  134. cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
  135. // Check if this is a virtual base.
  136. if (I.isVirtual()) {
  137. // Check if we've seen this base before.
  138. if (!VBases.insert(BaseDecl).second)
  139. continue;
  140. CharUnits BaseOffset =
  141. MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
  142. LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/true);
  143. }
  144. // We only need to layout virtual VTTs for this base if it actually has
  145. // virtual bases.
  146. if (BaseDecl->getNumVBases())
  147. LayoutVirtualVTTs(BaseDecl, VBases);
  148. }
  149. }
  150. void VTTBuilder::LayoutVTT(BaseSubobject Base, bool BaseIsVirtual) {
  151. const CXXRecordDecl *RD = Base.getBase();
  152. // Itanium C++ ABI 2.6.2:
  153. // An array of virtual table addresses, called the VTT, is declared for
  154. // each class type that has indirect or direct virtual base classes.
  155. if (RD->getNumVBases() == 0)
  156. return;
  157. bool IsPrimaryVTT = Base.getBase() == MostDerivedClass;
  158. if (!IsPrimaryVTT) {
  159. // Remember the sub-VTT index.
  160. SubVTTIndicies[Base] = VTTComponents.size();
  161. }
  162. uint64_t VTableIndex = VTTVTables.size();
  163. VTTVTables.push_back(VTTVTable(Base, BaseIsVirtual));
  164. // Add the primary vtable pointer.
  165. AddVTablePointer(Base, VTableIndex, RD);
  166. // Add the secondary VTTs.
  167. LayoutSecondaryVTTs(Base);
  168. // Add the secondary virtual pointers.
  169. LayoutSecondaryVirtualPointers(Base, VTableIndex);
  170. // If this is the primary VTT, we want to lay out virtual VTTs as well.
  171. if (IsPrimaryVTT) {
  172. VisitedVirtualBasesSetTy VBases;
  173. LayoutVirtualVTTs(Base.getBase(), VBases);
  174. }
  175. }