Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1 | //===--- VTableBuilder.cpp - C++ vtable layout builder --------------------===// |
| 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 generation of the layout of virtual tables. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/VTableBuilder.h" |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
David Majnemer | 70e6a00 | 2015-05-01 21:35:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTDiagnostic.h" |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
| 18 | #include "clang/AST/RecordLayout.h" |
| 19 | #include "clang/Basic/TargetInfo.h" |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SetOperations.h" |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <cstdio> |
| 26 | |
| 27 | using namespace clang; |
| 28 | |
| 29 | #define DUMP_OVERRIDERS 0 |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | /// BaseOffset - Represents an offset from a derived class to a direct or |
| 34 | /// indirect base class. |
| 35 | struct BaseOffset { |
| 36 | /// DerivedClass - The derived class. |
| 37 | const CXXRecordDecl *DerivedClass; |
| 38 | |
| 39 | /// VirtualBase - If the path from the derived class to the base class |
Timur Iskhodzhanov | bb5a17e | 2013-05-08 08:09:21 +0000 | [diff] [blame] | 40 | /// involves virtual base classes, this holds the declaration of the last |
| 41 | /// virtual base in this path (i.e. closest to the base class). |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 42 | const CXXRecordDecl *VirtualBase; |
| 43 | |
| 44 | /// NonVirtualOffset - The offset from the derived class to the base class. |
| 45 | /// (Or the offset from the virtual base class to the base class, if the |
| 46 | /// path from the derived class to the base class involves a virtual base |
| 47 | /// class. |
| 48 | CharUnits NonVirtualOffset; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 49 | |
| 50 | BaseOffset() : DerivedClass(nullptr), VirtualBase(nullptr), |
| 51 | NonVirtualOffset(CharUnits::Zero()) { } |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 52 | BaseOffset(const CXXRecordDecl *DerivedClass, |
| 53 | const CXXRecordDecl *VirtualBase, CharUnits NonVirtualOffset) |
| 54 | : DerivedClass(DerivedClass), VirtualBase(VirtualBase), |
| 55 | NonVirtualOffset(NonVirtualOffset) { } |
| 56 | |
| 57 | bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; } |
| 58 | }; |
| 59 | |
| 60 | /// FinalOverriders - Contains the final overrider member functions for all |
| 61 | /// member functions in the base subobjects of a class. |
| 62 | class FinalOverriders { |
| 63 | public: |
| 64 | /// OverriderInfo - Information about a final overrider. |
| 65 | struct OverriderInfo { |
| 66 | /// Method - The method decl of the overrider. |
| 67 | const CXXMethodDecl *Method; |
| 68 | |
Timur Iskhodzhanov | 7d19fc1 | 2014-11-17 15:11:05 +0000 | [diff] [blame] | 69 | /// VirtualBase - The virtual base class subobject of this overrider. |
Timur Iskhodzhanov | 6b12850 | 2014-04-22 17:32:02 +0000 | [diff] [blame] | 70 | /// Note that this records the closest derived virtual base class subobject. |
| 71 | const CXXRecordDecl *VirtualBase; |
| 72 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 73 | /// Offset - the base offset of the overrider's parent in the layout class. |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 74 | CharUnits Offset; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 75 | |
| 76 | OverriderInfo() : Method(nullptr), VirtualBase(nullptr), |
| 77 | Offset(CharUnits::Zero()) { } |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | private: |
| 81 | /// MostDerivedClass - The most derived class for which the final overriders |
| 82 | /// are stored. |
| 83 | const CXXRecordDecl *MostDerivedClass; |
| 84 | |
| 85 | /// MostDerivedClassOffset - If we're building final overriders for a |
| 86 | /// construction vtable, this holds the offset from the layout class to the |
| 87 | /// most derived class. |
| 88 | const CharUnits MostDerivedClassOffset; |
| 89 | |
| 90 | /// LayoutClass - The class we're using for layout information. Will be |
| 91 | /// different than the most derived class if the final overriders are for a |
| 92 | /// construction vtable. |
| 93 | const CXXRecordDecl *LayoutClass; |
| 94 | |
| 95 | ASTContext &Context; |
| 96 | |
| 97 | /// MostDerivedClassLayout - the AST record layout of the most derived class. |
| 98 | const ASTRecordLayout &MostDerivedClassLayout; |
| 99 | |
| 100 | /// MethodBaseOffsetPairTy - Uniquely identifies a member function |
| 101 | /// in a base subobject. |
| 102 | typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy; |
| 103 | |
| 104 | typedef llvm::DenseMap<MethodBaseOffsetPairTy, |
| 105 | OverriderInfo> OverridersMapTy; |
| 106 | |
| 107 | /// OverridersMap - The final overriders for all virtual member functions of |
| 108 | /// all the base subobjects of the most derived class. |
| 109 | OverridersMapTy OverridersMap; |
| 110 | |
| 111 | /// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented |
| 112 | /// as a record decl and a subobject number) and its offsets in the most |
| 113 | /// derived class as well as the layout class. |
| 114 | typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>, |
| 115 | CharUnits> SubobjectOffsetMapTy; |
| 116 | |
| 117 | typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy; |
| 118 | |
| 119 | /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the |
| 120 | /// given base. |
| 121 | void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, |
| 122 | CharUnits OffsetInLayoutClass, |
| 123 | SubobjectOffsetMapTy &SubobjectOffsets, |
| 124 | SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, |
| 125 | SubobjectCountMapTy &SubobjectCounts); |
| 126 | |
| 127 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; |
| 128 | |
| 129 | /// dump - dump the final overriders for a base subobject, and all its direct |
| 130 | /// and indirect base subobjects. |
| 131 | void dump(raw_ostream &Out, BaseSubobject Base, |
| 132 | VisitedVirtualBasesSetTy& VisitedVirtualBases); |
| 133 | |
| 134 | public: |
| 135 | FinalOverriders(const CXXRecordDecl *MostDerivedClass, |
| 136 | CharUnits MostDerivedClassOffset, |
| 137 | const CXXRecordDecl *LayoutClass); |
| 138 | |
| 139 | /// getOverrider - Get the final overrider for the given method declaration in |
| 140 | /// the subobject with the given base offset. |
| 141 | OverriderInfo getOverrider(const CXXMethodDecl *MD, |
| 142 | CharUnits BaseOffset) const { |
| 143 | assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) && |
| 144 | "Did not find overrider!"); |
| 145 | |
| 146 | return OverridersMap.lookup(std::make_pair(MD, BaseOffset)); |
| 147 | } |
| 148 | |
| 149 | /// dump - dump the final overriders. |
| 150 | void dump() { |
| 151 | VisitedVirtualBasesSetTy VisitedVirtualBases; |
| 152 | dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()), |
| 153 | VisitedVirtualBases); |
| 154 | } |
| 155 | |
| 156 | }; |
| 157 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 158 | FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass, |
| 159 | CharUnits MostDerivedClassOffset, |
| 160 | const CXXRecordDecl *LayoutClass) |
| 161 | : MostDerivedClass(MostDerivedClass), |
| 162 | MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass), |
| 163 | Context(MostDerivedClass->getASTContext()), |
| 164 | MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) { |
| 165 | |
| 166 | // Compute base offsets. |
| 167 | SubobjectOffsetMapTy SubobjectOffsets; |
| 168 | SubobjectOffsetMapTy SubobjectLayoutClassOffsets; |
| 169 | SubobjectCountMapTy SubobjectCounts; |
| 170 | ComputeBaseOffsets(BaseSubobject(MostDerivedClass, CharUnits::Zero()), |
| 171 | /*IsVirtual=*/false, |
| 172 | MostDerivedClassOffset, |
| 173 | SubobjectOffsets, SubobjectLayoutClassOffsets, |
| 174 | SubobjectCounts); |
| 175 | |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 176 | // Get the final overriders. |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 177 | CXXFinalOverriderMap FinalOverriders; |
| 178 | MostDerivedClass->getFinalOverriders(FinalOverriders); |
| 179 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 180 | for (const auto &Overrider : FinalOverriders) { |
| 181 | const CXXMethodDecl *MD = Overrider.first; |
| 182 | const OverridingMethods &Methods = Overrider.second; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 183 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 184 | for (const auto &M : Methods) { |
| 185 | unsigned SubobjectNumber = M.first; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 186 | assert(SubobjectOffsets.count(std::make_pair(MD->getParent(), |
| 187 | SubobjectNumber)) && |
| 188 | "Did not find subobject offset!"); |
| 189 | |
| 190 | CharUnits BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(), |
| 191 | SubobjectNumber)]; |
| 192 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 193 | assert(M.second.size() == 1 && "Final overrider is not unique!"); |
| 194 | const UniqueVirtualMethod &Method = M.second.front(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 195 | |
| 196 | const CXXRecordDecl *OverriderRD = Method.Method->getParent(); |
| 197 | assert(SubobjectLayoutClassOffsets.count( |
| 198 | std::make_pair(OverriderRD, Method.Subobject)) |
| 199 | && "Did not find subobject offset!"); |
| 200 | CharUnits OverriderOffset = |
| 201 | SubobjectLayoutClassOffsets[std::make_pair(OverriderRD, |
| 202 | Method.Subobject)]; |
| 203 | |
| 204 | OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)]; |
| 205 | assert(!Overrider.Method && "Overrider should not exist yet!"); |
| 206 | |
| 207 | Overrider.Offset = OverriderOffset; |
| 208 | Overrider.Method = Method.Method; |
Timur Iskhodzhanov | 6b12850 | 2014-04-22 17:32:02 +0000 | [diff] [blame] | 209 | Overrider.VirtualBase = Method.InVirtualSubobject; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | #if DUMP_OVERRIDERS |
| 214 | // And dump them (for now). |
| 215 | dump(); |
| 216 | #endif |
| 217 | } |
| 218 | |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 219 | static BaseOffset ComputeBaseOffset(const ASTContext &Context, |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 220 | const CXXRecordDecl *DerivedRD, |
| 221 | const CXXBasePath &Path) { |
| 222 | CharUnits NonVirtualOffset = CharUnits::Zero(); |
| 223 | |
| 224 | unsigned NonVirtualStart = 0; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 225 | const CXXRecordDecl *VirtualBase = nullptr; |
| 226 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 227 | // First, look for the virtual base class. |
Timur Iskhodzhanov | bb5a17e | 2013-05-08 08:09:21 +0000 | [diff] [blame] | 228 | for (int I = Path.size(), E = 0; I != E; --I) { |
| 229 | const CXXBasePathElement &Element = Path[I - 1]; |
| 230 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 231 | if (Element.Base->isVirtual()) { |
Timur Iskhodzhanov | bb5a17e | 2013-05-08 08:09:21 +0000 | [diff] [blame] | 232 | NonVirtualStart = I; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 233 | QualType VBaseType = Element.Base->getType(); |
Timur Iskhodzhanov | 7f55a45 | 2013-07-02 16:00:40 +0000 | [diff] [blame] | 234 | VirtualBase = VBaseType->getAsCXXRecordDecl(); |
Timur Iskhodzhanov | bb5a17e | 2013-05-08 08:09:21 +0000 | [diff] [blame] | 235 | break; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | // Now compute the non-virtual offset. |
| 240 | for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) { |
| 241 | const CXXBasePathElement &Element = Path[I]; |
| 242 | |
| 243 | // Check the base class offset. |
| 244 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); |
| 245 | |
Timur Iskhodzhanov | 7f55a45 | 2013-07-02 16:00:40 +0000 | [diff] [blame] | 246 | const CXXRecordDecl *Base = Element.Base->getType()->getAsCXXRecordDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 247 | |
| 248 | NonVirtualOffset += Layout.getBaseClassOffset(Base); |
| 249 | } |
| 250 | |
| 251 | // FIXME: This should probably use CharUnits or something. Maybe we should |
| 252 | // even change the base offsets in ASTRecordLayout to be specified in |
| 253 | // CharUnits. |
| 254 | return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset); |
| 255 | |
| 256 | } |
| 257 | |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 258 | static BaseOffset ComputeBaseOffset(const ASTContext &Context, |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 259 | const CXXRecordDecl *BaseRD, |
| 260 | const CXXRecordDecl *DerivedRD) { |
| 261 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 262 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
Benjamin Kramer | 325d745 | 2013-02-03 18:55:34 +0000 | [diff] [blame] | 263 | |
| 264 | if (!DerivedRD->isDerivedFrom(BaseRD, Paths)) |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 265 | llvm_unreachable("Class must be derived from the passed in base class!"); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 266 | |
| 267 | return ComputeBaseOffset(Context, DerivedRD, Paths.front()); |
| 268 | } |
| 269 | |
| 270 | static BaseOffset |
| 271 | ComputeReturnAdjustmentBaseOffset(ASTContext &Context, |
| 272 | const CXXMethodDecl *DerivedMD, |
| 273 | const CXXMethodDecl *BaseMD) { |
| 274 | const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>(); |
| 275 | const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>(); |
| 276 | |
| 277 | // Canonicalize the return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 278 | CanQualType CanDerivedReturnType = |
| 279 | Context.getCanonicalType(DerivedFT->getReturnType()); |
| 280 | CanQualType CanBaseReturnType = |
| 281 | Context.getCanonicalType(BaseFT->getReturnType()); |
| 282 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 283 | assert(CanDerivedReturnType->getTypeClass() == |
| 284 | CanBaseReturnType->getTypeClass() && |
| 285 | "Types must have same type class!"); |
| 286 | |
| 287 | if (CanDerivedReturnType == CanBaseReturnType) { |
| 288 | // No adjustment needed. |
| 289 | return BaseOffset(); |
| 290 | } |
| 291 | |
| 292 | if (isa<ReferenceType>(CanDerivedReturnType)) { |
| 293 | CanDerivedReturnType = |
| 294 | CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType(); |
| 295 | CanBaseReturnType = |
| 296 | CanBaseReturnType->getAs<ReferenceType>()->getPointeeType(); |
| 297 | } else if (isa<PointerType>(CanDerivedReturnType)) { |
| 298 | CanDerivedReturnType = |
| 299 | CanDerivedReturnType->getAs<PointerType>()->getPointeeType(); |
| 300 | CanBaseReturnType = |
| 301 | CanBaseReturnType->getAs<PointerType>()->getPointeeType(); |
| 302 | } else { |
| 303 | llvm_unreachable("Unexpected return type!"); |
| 304 | } |
| 305 | |
| 306 | // We need to compare unqualified types here; consider |
| 307 | // const T *Base::foo(); |
| 308 | // T *Derived::foo(); |
| 309 | if (CanDerivedReturnType.getUnqualifiedType() == |
| 310 | CanBaseReturnType.getUnqualifiedType()) { |
| 311 | // No adjustment needed. |
| 312 | return BaseOffset(); |
| 313 | } |
| 314 | |
| 315 | const CXXRecordDecl *DerivedRD = |
| 316 | cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl()); |
| 317 | |
| 318 | const CXXRecordDecl *BaseRD = |
| 319 | cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl()); |
| 320 | |
| 321 | return ComputeBaseOffset(Context, BaseRD, DerivedRD); |
| 322 | } |
| 323 | |
| 324 | void |
| 325 | FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, |
| 326 | CharUnits OffsetInLayoutClass, |
| 327 | SubobjectOffsetMapTy &SubobjectOffsets, |
| 328 | SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, |
| 329 | SubobjectCountMapTy &SubobjectCounts) { |
| 330 | const CXXRecordDecl *RD = Base.getBase(); |
| 331 | |
| 332 | unsigned SubobjectNumber = 0; |
| 333 | if (!IsVirtual) |
| 334 | SubobjectNumber = ++SubobjectCounts[RD]; |
| 335 | |
| 336 | // Set up the subobject to offset mapping. |
| 337 | assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber)) |
| 338 | && "Subobject offset already exists!"); |
| 339 | assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber)) |
| 340 | && "Subobject offset already exists!"); |
| 341 | |
| 342 | SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = Base.getBaseOffset(); |
| 343 | SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] = |
| 344 | OffsetInLayoutClass; |
| 345 | |
| 346 | // Traverse our bases. |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 347 | for (const auto &B : RD->bases()) { |
| 348 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 349 | |
| 350 | CharUnits BaseOffset; |
| 351 | CharUnits BaseOffsetInLayoutClass; |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 352 | if (B.isVirtual()) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 353 | // Check if we've visited this virtual base before. |
| 354 | if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0))) |
| 355 | continue; |
| 356 | |
| 357 | const ASTRecordLayout &LayoutClassLayout = |
| 358 | Context.getASTRecordLayout(LayoutClass); |
| 359 | |
| 360 | BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
| 361 | BaseOffsetInLayoutClass = |
| 362 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
| 363 | } else { |
| 364 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 365 | CharUnits Offset = Layout.getBaseClassOffset(BaseDecl); |
| 366 | |
| 367 | BaseOffset = Base.getBaseOffset() + Offset; |
| 368 | BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset; |
| 369 | } |
| 370 | |
| 371 | ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset), |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 372 | B.isVirtual(), BaseOffsetInLayoutClass, |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 373 | SubobjectOffsets, SubobjectLayoutClassOffsets, |
| 374 | SubobjectCounts); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | void FinalOverriders::dump(raw_ostream &Out, BaseSubobject Base, |
| 379 | VisitedVirtualBasesSetTy &VisitedVirtualBases) { |
| 380 | const CXXRecordDecl *RD = Base.getBase(); |
| 381 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 382 | |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 383 | for (const auto &B : RD->bases()) { |
| 384 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 385 | |
| 386 | // Ignore bases that don't have any virtual member functions. |
| 387 | if (!BaseDecl->isPolymorphic()) |
| 388 | continue; |
| 389 | |
| 390 | CharUnits BaseOffset; |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 391 | if (B.isVirtual()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 392 | if (!VisitedVirtualBases.insert(BaseDecl).second) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 393 | // We've visited this base before. |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
| 398 | } else { |
| 399 | BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset(); |
| 400 | } |
| 401 | |
| 402 | dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases); |
| 403 | } |
| 404 | |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 405 | Out << "Final overriders for ("; |
| 406 | RD->printQualifiedName(Out); |
| 407 | Out << ", "; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 408 | Out << Base.getBaseOffset().getQuantity() << ")\n"; |
| 409 | |
| 410 | // Now dump the overriders for this base subobject. |
Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 411 | for (const auto *MD : RD->methods()) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 412 | if (!MD->isVirtual()) |
| 413 | continue; |
Reid Kleckner | 1cbd9aa | 2015-02-25 19:17:48 +0000 | [diff] [blame] | 414 | MD = MD->getCanonicalDecl(); |
NAKAMURA Takumi | 073f2b4 | 2015-02-25 10:50:06 +0000 | [diff] [blame] | 415 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 416 | OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset()); |
| 417 | |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 418 | Out << " "; |
| 419 | MD->printQualifiedName(Out); |
| 420 | Out << " - ("; |
| 421 | Overrider.Method->printQualifiedName(Out); |
Timur Iskhodzhanov | be5a3d8 | 2013-06-05 06:40:07 +0000 | [diff] [blame] | 422 | Out << ", " << Overrider.Offset.getQuantity() << ')'; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 423 | |
| 424 | BaseOffset Offset; |
| 425 | if (!Overrider.Method->isPure()) |
| 426 | Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD); |
| 427 | |
| 428 | if (!Offset.isEmpty()) { |
| 429 | Out << " [ret-adj: "; |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 430 | if (Offset.VirtualBase) { |
| 431 | Offset.VirtualBase->printQualifiedName(Out); |
| 432 | Out << " vbase, "; |
| 433 | } |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 434 | |
| 435 | Out << Offset.NonVirtualOffset.getQuantity() << " nv]"; |
| 436 | } |
| 437 | |
| 438 | Out << "\n"; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /// VCallOffsetMap - Keeps track of vcall offsets when building a vtable. |
| 443 | struct VCallOffsetMap { |
| 444 | |
| 445 | typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy; |
| 446 | |
| 447 | /// Offsets - Keeps track of methods and their offsets. |
| 448 | // FIXME: This should be a real map and not a vector. |
| 449 | SmallVector<MethodAndOffsetPairTy, 16> Offsets; |
| 450 | |
| 451 | /// MethodsCanShareVCallOffset - Returns whether two virtual member functions |
| 452 | /// can share the same vcall offset. |
| 453 | static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, |
| 454 | const CXXMethodDecl *RHS); |
| 455 | |
| 456 | public: |
| 457 | /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the |
| 458 | /// add was successful, or false if there was already a member function with |
| 459 | /// the same signature in the map. |
| 460 | bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset); |
| 461 | |
| 462 | /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the |
| 463 | /// vtable address point) for the given virtual member function. |
| 464 | CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD); |
| 465 | |
| 466 | // empty - Return whether the offset map is empty or not. |
| 467 | bool empty() const { return Offsets.empty(); } |
| 468 | }; |
| 469 | |
| 470 | static bool HasSameVirtualSignature(const CXXMethodDecl *LHS, |
| 471 | const CXXMethodDecl *RHS) { |
John McCall | b6c4a7e | 2012-03-21 06:57:19 +0000 | [diff] [blame] | 472 | const FunctionProtoType *LT = |
| 473 | cast<FunctionProtoType>(LHS->getType().getCanonicalType()); |
| 474 | const FunctionProtoType *RT = |
| 475 | cast<FunctionProtoType>(RHS->getType().getCanonicalType()); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 476 | |
| 477 | // Fast-path matches in the canonical types. |
| 478 | if (LT == RT) return true; |
| 479 | |
| 480 | // Force the signatures to match. We can't rely on the overrides |
| 481 | // list here because there isn't necessarily an inheritance |
| 482 | // relationship between the two methods. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 483 | if (LT->getTypeQuals() != RT->getTypeQuals()) |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 484 | return false; |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 485 | return LT->getParamTypes() == RT->getParamTypes(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, |
| 489 | const CXXMethodDecl *RHS) { |
| 490 | assert(LHS->isVirtual() && "LHS must be virtual!"); |
| 491 | assert(RHS->isVirtual() && "LHS must be virtual!"); |
| 492 | |
| 493 | // A destructor can share a vcall offset with another destructor. |
| 494 | if (isa<CXXDestructorDecl>(LHS)) |
| 495 | return isa<CXXDestructorDecl>(RHS); |
| 496 | |
| 497 | // FIXME: We need to check more things here. |
| 498 | |
| 499 | // The methods must have the same name. |
| 500 | DeclarationName LHSName = LHS->getDeclName(); |
| 501 | DeclarationName RHSName = RHS->getDeclName(); |
| 502 | if (LHSName != RHSName) |
| 503 | return false; |
| 504 | |
| 505 | // And the same signatures. |
| 506 | return HasSameVirtualSignature(LHS, RHS); |
| 507 | } |
| 508 | |
| 509 | bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD, |
| 510 | CharUnits OffsetOffset) { |
| 511 | // Check if we can reuse an offset. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 512 | for (const auto &OffsetPair : Offsets) { |
| 513 | if (MethodsCanShareVCallOffset(OffsetPair.first, MD)) |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 514 | return false; |
| 515 | } |
| 516 | |
| 517 | // Add the offset. |
| 518 | Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset)); |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) { |
| 523 | // Look for an offset. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 524 | for (const auto &OffsetPair : Offsets) { |
| 525 | if (MethodsCanShareVCallOffset(OffsetPair.first, MD)) |
| 526 | return OffsetPair.second; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | llvm_unreachable("Should always find a vcall offset offset!"); |
| 530 | } |
| 531 | |
| 532 | /// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets. |
| 533 | class VCallAndVBaseOffsetBuilder { |
| 534 | public: |
| 535 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> |
| 536 | VBaseOffsetOffsetsMapTy; |
| 537 | |
| 538 | private: |
| 539 | /// MostDerivedClass - The most derived class for which we're building vcall |
| 540 | /// and vbase offsets. |
| 541 | const CXXRecordDecl *MostDerivedClass; |
| 542 | |
| 543 | /// LayoutClass - The class we're using for layout information. Will be |
| 544 | /// different than the most derived class if we're building a construction |
| 545 | /// vtable. |
| 546 | const CXXRecordDecl *LayoutClass; |
| 547 | |
| 548 | /// Context - The ASTContext which we will use for layout information. |
| 549 | ASTContext &Context; |
| 550 | |
| 551 | /// Components - vcall and vbase offset components |
| 552 | typedef SmallVector<VTableComponent, 64> VTableComponentVectorTy; |
| 553 | VTableComponentVectorTy Components; |
| 554 | |
| 555 | /// VisitedVirtualBases - Visited virtual bases. |
| 556 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
| 557 | |
| 558 | /// VCallOffsets - Keeps track of vcall offsets. |
| 559 | VCallOffsetMap VCallOffsets; |
| 560 | |
| 561 | |
| 562 | /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets, |
| 563 | /// relative to the address point. |
| 564 | VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; |
| 565 | |
| 566 | /// FinalOverriders - The final overriders of the most derived class. |
| 567 | /// (Can be null when we're not building a vtable of the most derived class). |
| 568 | const FinalOverriders *Overriders; |
| 569 | |
| 570 | /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the |
| 571 | /// given base subobject. |
| 572 | void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual, |
| 573 | CharUnits RealBaseOffset); |
| 574 | |
| 575 | /// AddVCallOffsets - Add vcall offsets for the given base subobject. |
| 576 | void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset); |
| 577 | |
| 578 | /// AddVBaseOffsets - Add vbase offsets for the given class. |
| 579 | void AddVBaseOffsets(const CXXRecordDecl *Base, |
| 580 | CharUnits OffsetInLayoutClass); |
| 581 | |
| 582 | /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in |
| 583 | /// chars, relative to the vtable address point. |
| 584 | CharUnits getCurrentOffsetOffset() const; |
| 585 | |
| 586 | public: |
| 587 | VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass, |
| 588 | const CXXRecordDecl *LayoutClass, |
| 589 | const FinalOverriders *Overriders, |
| 590 | BaseSubobject Base, bool BaseIsVirtual, |
| 591 | CharUnits OffsetInLayoutClass) |
| 592 | : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass), |
| 593 | Context(MostDerivedClass->getASTContext()), Overriders(Overriders) { |
| 594 | |
| 595 | // Add vcall and vbase offsets. |
| 596 | AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass); |
| 597 | } |
| 598 | |
| 599 | /// Methods for iterating over the components. |
| 600 | typedef VTableComponentVectorTy::const_reverse_iterator const_iterator; |
| 601 | const_iterator components_begin() const { return Components.rbegin(); } |
| 602 | const_iterator components_end() const { return Components.rend(); } |
| 603 | |
| 604 | const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; } |
| 605 | const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { |
| 606 | return VBaseOffsetOffsets; |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | void |
| 611 | VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base, |
| 612 | bool BaseIsVirtual, |
| 613 | CharUnits RealBaseOffset) { |
| 614 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase()); |
| 615 | |
| 616 | // Itanium C++ ABI 2.5.2: |
| 617 | // ..in classes sharing a virtual table with a primary base class, the vcall |
| 618 | // and vbase offsets added by the derived class all come before the vcall |
| 619 | // and vbase offsets required by the base class, so that the latter may be |
| 620 | // laid out as required by the base class without regard to additions from |
| 621 | // the derived class(es). |
| 622 | |
| 623 | // (Since we're emitting the vcall and vbase offsets in reverse order, we'll |
| 624 | // emit them for the primary base first). |
| 625 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
| 626 | bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
| 627 | |
| 628 | CharUnits PrimaryBaseOffset; |
| 629 | |
| 630 | // Get the base offset of the primary base. |
| 631 | if (PrimaryBaseIsVirtual) { |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 632 | assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 633 | "Primary vbase should have a zero offset!"); |
| 634 | |
| 635 | const ASTRecordLayout &MostDerivedClassLayout = |
| 636 | Context.getASTRecordLayout(MostDerivedClass); |
| 637 | |
| 638 | PrimaryBaseOffset = |
| 639 | MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase); |
| 640 | } else { |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 641 | assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 642 | "Primary base should have a zero offset!"); |
| 643 | |
| 644 | PrimaryBaseOffset = Base.getBaseOffset(); |
| 645 | } |
| 646 | |
| 647 | AddVCallAndVBaseOffsets( |
| 648 | BaseSubobject(PrimaryBase,PrimaryBaseOffset), |
| 649 | PrimaryBaseIsVirtual, RealBaseOffset); |
| 650 | } |
| 651 | |
| 652 | AddVBaseOffsets(Base.getBase(), RealBaseOffset); |
| 653 | |
| 654 | // We only want to add vcall offsets for virtual bases. |
| 655 | if (BaseIsVirtual) |
| 656 | AddVCallOffsets(Base, RealBaseOffset); |
| 657 | } |
| 658 | |
| 659 | CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const { |
| 660 | // OffsetIndex is the index of this vcall or vbase offset, relative to the |
| 661 | // vtable address point. (We subtract 3 to account for the information just |
| 662 | // above the address point, the RTTI info, the offset to top, and the |
| 663 | // vcall offset itself). |
| 664 | int64_t OffsetIndex = -(int64_t)(3 + Components.size()); |
| 665 | |
| 666 | CharUnits PointerWidth = |
| 667 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
| 668 | CharUnits OffsetOffset = PointerWidth * OffsetIndex; |
| 669 | return OffsetOffset; |
| 670 | } |
| 671 | |
| 672 | void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base, |
| 673 | CharUnits VBaseOffset) { |
| 674 | const CXXRecordDecl *RD = Base.getBase(); |
| 675 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 676 | |
| 677 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 678 | |
| 679 | // Handle the primary base first. |
| 680 | // We only want to add vcall offsets if the base is non-virtual; a virtual |
| 681 | // primary base will have its vcall and vbase offsets emitted already. |
| 682 | if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) { |
| 683 | // Get the base offset of the primary base. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 684 | assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 685 | "Primary base should have a zero offset!"); |
| 686 | |
| 687 | AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()), |
| 688 | VBaseOffset); |
| 689 | } |
| 690 | |
| 691 | // Add the vcall offsets. |
Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 692 | for (const auto *MD : RD->methods()) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 693 | if (!MD->isVirtual()) |
| 694 | continue; |
Reid Kleckner | 1cbd9aa | 2015-02-25 19:17:48 +0000 | [diff] [blame] | 695 | MD = MD->getCanonicalDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 696 | |
| 697 | CharUnits OffsetOffset = getCurrentOffsetOffset(); |
| 698 | |
| 699 | // Don't add a vcall offset if we already have one for this member function |
| 700 | // signature. |
| 701 | if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset)) |
| 702 | continue; |
| 703 | |
| 704 | CharUnits Offset = CharUnits::Zero(); |
| 705 | |
| 706 | if (Overriders) { |
| 707 | // Get the final overrider. |
| 708 | FinalOverriders::OverriderInfo Overrider = |
| 709 | Overriders->getOverrider(MD, Base.getBaseOffset()); |
| 710 | |
| 711 | /// The vcall offset is the offset from the virtual base to the object |
| 712 | /// where the function was overridden. |
| 713 | Offset = Overrider.Offset - VBaseOffset; |
| 714 | } |
| 715 | |
| 716 | Components.push_back( |
| 717 | VTableComponent::MakeVCallOffset(Offset)); |
| 718 | } |
| 719 | |
| 720 | // And iterate over all non-virtual bases (ignoring the primary base). |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 721 | for (const auto &B : RD->bases()) { |
| 722 | if (B.isVirtual()) |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 723 | continue; |
| 724 | |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 725 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 726 | if (BaseDecl == PrimaryBase) |
| 727 | continue; |
| 728 | |
| 729 | // Get the base offset of this base. |
| 730 | CharUnits BaseOffset = Base.getBaseOffset() + |
| 731 | Layout.getBaseClassOffset(BaseDecl); |
| 732 | |
| 733 | AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset), |
| 734 | VBaseOffset); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | void |
| 739 | VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD, |
| 740 | CharUnits OffsetInLayoutClass) { |
| 741 | const ASTRecordLayout &LayoutClassLayout = |
| 742 | Context.getASTRecordLayout(LayoutClass); |
| 743 | |
| 744 | // Add vbase offsets. |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 745 | for (const auto &B : RD->bases()) { |
| 746 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 747 | |
| 748 | // Check if this is a virtual base that we haven't visited before. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 749 | if (B.isVirtual() && VisitedVirtualBases.insert(BaseDecl).second) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 750 | CharUnits Offset = |
| 751 | LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass; |
| 752 | |
| 753 | // Add the vbase offset offset. |
| 754 | assert(!VBaseOffsetOffsets.count(BaseDecl) && |
| 755 | "vbase offset offset already exists!"); |
| 756 | |
| 757 | CharUnits VBaseOffsetOffset = getCurrentOffsetOffset(); |
| 758 | VBaseOffsetOffsets.insert( |
| 759 | std::make_pair(BaseDecl, VBaseOffsetOffset)); |
| 760 | |
| 761 | Components.push_back( |
| 762 | VTableComponent::MakeVBaseOffset(Offset)); |
| 763 | } |
| 764 | |
| 765 | // Check the base class looking for more vbase offsets. |
| 766 | AddVBaseOffsets(BaseDecl, OffsetInLayoutClass); |
| 767 | } |
| 768 | } |
| 769 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 770 | /// ItaniumVTableBuilder - Class for building vtable layout information. |
| 771 | class ItaniumVTableBuilder { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 772 | public: |
| 773 | /// PrimaryBasesSetVectorTy - A set vector of direct and indirect |
| 774 | /// primary bases. |
| 775 | typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> |
| 776 | PrimaryBasesSetVectorTy; |
| 777 | |
| 778 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> |
| 779 | VBaseOffsetOffsetsMapTy; |
| 780 | |
| 781 | typedef llvm::DenseMap<BaseSubobject, uint64_t> |
| 782 | AddressPointsMapTy; |
| 783 | |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 784 | typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy; |
| 785 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 786 | private: |
| 787 | /// VTables - Global vtable information. |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 788 | ItaniumVTableContext &VTables; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 789 | |
| 790 | /// MostDerivedClass - The most derived class for which we're building this |
| 791 | /// vtable. |
| 792 | const CXXRecordDecl *MostDerivedClass; |
| 793 | |
| 794 | /// MostDerivedClassOffset - If we're building a construction vtable, this |
| 795 | /// holds the offset from the layout class to the most derived class. |
| 796 | const CharUnits MostDerivedClassOffset; |
| 797 | |
| 798 | /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual |
| 799 | /// base. (This only makes sense when building a construction vtable). |
| 800 | bool MostDerivedClassIsVirtual; |
| 801 | |
| 802 | /// LayoutClass - The class we're using for layout information. Will be |
| 803 | /// different than the most derived class if we're building a construction |
| 804 | /// vtable. |
| 805 | const CXXRecordDecl *LayoutClass; |
| 806 | |
| 807 | /// Context - The ASTContext which we will use for layout information. |
| 808 | ASTContext &Context; |
| 809 | |
| 810 | /// FinalOverriders - The final overriders of the most derived class. |
| 811 | const FinalOverriders Overriders; |
| 812 | |
| 813 | /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual |
| 814 | /// bases in this vtable. |
| 815 | llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases; |
| 816 | |
| 817 | /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for |
| 818 | /// the most derived class. |
| 819 | VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; |
| 820 | |
| 821 | /// Components - The components of the vtable being built. |
| 822 | SmallVector<VTableComponent, 64> Components; |
| 823 | |
| 824 | /// AddressPoints - Address points for the vtable being built. |
| 825 | AddressPointsMapTy AddressPoints; |
| 826 | |
| 827 | /// MethodInfo - Contains information about a method in a vtable. |
| 828 | /// (Used for computing 'this' pointer adjustment thunks. |
| 829 | struct MethodInfo { |
| 830 | /// BaseOffset - The base offset of this method. |
| 831 | const CharUnits BaseOffset; |
| 832 | |
| 833 | /// BaseOffsetInLayoutClass - The base offset in the layout class of this |
| 834 | /// method. |
| 835 | const CharUnits BaseOffsetInLayoutClass; |
| 836 | |
| 837 | /// VTableIndex - The index in the vtable that this method has. |
| 838 | /// (For destructors, this is the index of the complete destructor). |
| 839 | const uint64_t VTableIndex; |
| 840 | |
| 841 | MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass, |
| 842 | uint64_t VTableIndex) |
| 843 | : BaseOffset(BaseOffset), |
| 844 | BaseOffsetInLayoutClass(BaseOffsetInLayoutClass), |
| 845 | VTableIndex(VTableIndex) { } |
| 846 | |
| 847 | MethodInfo() |
| 848 | : BaseOffset(CharUnits::Zero()), |
| 849 | BaseOffsetInLayoutClass(CharUnits::Zero()), |
| 850 | VTableIndex(0) { } |
| 851 | }; |
| 852 | |
| 853 | typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy; |
| 854 | |
| 855 | /// MethodInfoMap - The information for all methods in the vtable we're |
| 856 | /// currently building. |
| 857 | MethodInfoMapTy MethodInfoMap; |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 858 | |
| 859 | /// MethodVTableIndices - Contains the index (relative to the vtable address |
| 860 | /// point) where the function pointer for a virtual function is stored. |
| 861 | MethodVTableIndicesTy MethodVTableIndices; |
| 862 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 863 | typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy; |
| 864 | |
| 865 | /// VTableThunks - The thunks by vtable index in the vtable currently being |
| 866 | /// built. |
| 867 | VTableThunksMapTy VTableThunks; |
| 868 | |
| 869 | typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; |
| 870 | typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; |
| 871 | |
| 872 | /// Thunks - A map that contains all the thunks needed for all methods in the |
| 873 | /// most derived class for which the vtable is currently being built. |
| 874 | ThunksMapTy Thunks; |
| 875 | |
| 876 | /// AddThunk - Add a thunk for the given method. |
| 877 | void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk); |
| 878 | |
| 879 | /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the |
| 880 | /// part of the vtable we're currently building. |
| 881 | void ComputeThisAdjustments(); |
| 882 | |
| 883 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; |
| 884 | |
| 885 | /// PrimaryVirtualBases - All known virtual bases who are a primary base of |
| 886 | /// some other base. |
| 887 | VisitedVirtualBasesSetTy PrimaryVirtualBases; |
| 888 | |
| 889 | /// ComputeReturnAdjustment - Compute the return adjustment given a return |
| 890 | /// adjustment base offset. |
| 891 | ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset); |
| 892 | |
| 893 | /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting |
| 894 | /// the 'this' pointer from the base subobject to the derived subobject. |
| 895 | BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base, |
| 896 | BaseSubobject Derived) const; |
| 897 | |
| 898 | /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the |
| 899 | /// given virtual member function, its offset in the layout class and its |
| 900 | /// final overrider. |
| 901 | ThisAdjustment |
| 902 | ComputeThisAdjustment(const CXXMethodDecl *MD, |
| 903 | CharUnits BaseOffsetInLayoutClass, |
| 904 | FinalOverriders::OverriderInfo Overrider); |
| 905 | |
| 906 | /// AddMethod - Add a single virtual member function to the vtable |
| 907 | /// components vector. |
| 908 | void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment); |
| 909 | |
| 910 | /// IsOverriderUsed - Returns whether the overrider will ever be used in this |
| 911 | /// part of the vtable. |
| 912 | /// |
| 913 | /// Itanium C++ ABI 2.5.2: |
| 914 | /// |
| 915 | /// struct A { virtual void f(); }; |
| 916 | /// struct B : virtual public A { int i; }; |
| 917 | /// struct C : virtual public A { int j; }; |
| 918 | /// struct D : public B, public C {}; |
| 919 | /// |
| 920 | /// When B and C are declared, A is a primary base in each case, so although |
| 921 | /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this |
| 922 | /// adjustment is required and no thunk is generated. However, inside D |
| 923 | /// objects, A is no longer a primary base of C, so if we allowed calls to |
| 924 | /// C::f() to use the copy of A's vtable in the C subobject, we would need |
| 925 | /// to adjust this from C* to B::A*, which would require a third-party |
| 926 | /// thunk. Since we require that a call to C::f() first convert to A*, |
| 927 | /// C-in-D's copy of A's vtable is never referenced, so this is not |
| 928 | /// necessary. |
| 929 | bool IsOverriderUsed(const CXXMethodDecl *Overrider, |
| 930 | CharUnits BaseOffsetInLayoutClass, |
| 931 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 932 | CharUnits FirstBaseOffsetInLayoutClass) const; |
| 933 | |
| 934 | |
| 935 | /// AddMethods - Add the methods of this base subobject and all its |
| 936 | /// primary bases to the vtable components vector. |
| 937 | void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass, |
| 938 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 939 | CharUnits FirstBaseOffsetInLayoutClass, |
| 940 | PrimaryBasesSetVectorTy &PrimaryBases); |
| 941 | |
| 942 | // LayoutVTable - Layout the vtable for the given base class, including its |
| 943 | // secondary vtables and any vtables for virtual bases. |
| 944 | void LayoutVTable(); |
| 945 | |
| 946 | /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the |
| 947 | /// given base subobject, as well as all its secondary vtables. |
| 948 | /// |
| 949 | /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base |
| 950 | /// or a direct or indirect base of a virtual base. |
| 951 | /// |
| 952 | /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual |
| 953 | /// in the layout class. |
| 954 | void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base, |
| 955 | bool BaseIsMorallyVirtual, |
| 956 | bool BaseIsVirtualInLayoutClass, |
| 957 | CharUnits OffsetInLayoutClass); |
| 958 | |
| 959 | /// LayoutSecondaryVTables - Layout the secondary vtables for the given base |
| 960 | /// subobject. |
| 961 | /// |
| 962 | /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base |
| 963 | /// or a direct or indirect base of a virtual base. |
| 964 | void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual, |
| 965 | CharUnits OffsetInLayoutClass); |
| 966 | |
| 967 | /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this |
| 968 | /// class hierarchy. |
| 969 | void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD, |
| 970 | CharUnits OffsetInLayoutClass, |
| 971 | VisitedVirtualBasesSetTy &VBases); |
| 972 | |
| 973 | /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the |
| 974 | /// given base (excluding any primary bases). |
| 975 | void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD, |
| 976 | VisitedVirtualBasesSetTy &VBases); |
| 977 | |
| 978 | /// isBuildingConstructionVTable - Return whether this vtable builder is |
| 979 | /// building a construction vtable. |
| 980 | bool isBuildingConstructorVTable() const { |
| 981 | return MostDerivedClass != LayoutClass; |
| 982 | } |
| 983 | |
| 984 | public: |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 985 | ItaniumVTableBuilder(ItaniumVTableContext &VTables, |
| 986 | const CXXRecordDecl *MostDerivedClass, |
| 987 | CharUnits MostDerivedClassOffset, |
| 988 | bool MostDerivedClassIsVirtual, |
| 989 | const CXXRecordDecl *LayoutClass) |
| 990 | : VTables(VTables), MostDerivedClass(MostDerivedClass), |
| 991 | MostDerivedClassOffset(MostDerivedClassOffset), |
| 992 | MostDerivedClassIsVirtual(MostDerivedClassIsVirtual), |
| 993 | LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()), |
| 994 | Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) { |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 995 | assert(!Context.getTargetInfo().getCXXABI().isMicrosoft()); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 996 | |
| 997 | LayoutVTable(); |
| 998 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 999 | if (Context.getLangOpts().DumpVTableLayouts) |
Reid Kleckner | 5bc6d0f | 2013-11-08 21:28:00 +0000 | [diff] [blame] | 1000 | dumpLayout(llvm::outs()); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | uint64_t getNumThunks() const { |
| 1004 | return Thunks.size(); |
| 1005 | } |
| 1006 | |
| 1007 | ThunksMapTy::const_iterator thunks_begin() const { |
| 1008 | return Thunks.begin(); |
| 1009 | } |
| 1010 | |
| 1011 | ThunksMapTy::const_iterator thunks_end() const { |
| 1012 | return Thunks.end(); |
| 1013 | } |
| 1014 | |
| 1015 | const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { |
| 1016 | return VBaseOffsetOffsets; |
| 1017 | } |
| 1018 | |
| 1019 | const AddressPointsMapTy &getAddressPoints() const { |
| 1020 | return AddressPoints; |
| 1021 | } |
| 1022 | |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 1023 | MethodVTableIndicesTy::const_iterator vtable_indices_begin() const { |
| 1024 | return MethodVTableIndices.begin(); |
| 1025 | } |
| 1026 | |
| 1027 | MethodVTableIndicesTy::const_iterator vtable_indices_end() const { |
| 1028 | return MethodVTableIndices.end(); |
| 1029 | } |
| 1030 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1031 | /// getNumVTableComponents - Return the number of components in the vtable |
| 1032 | /// currently built. |
| 1033 | uint64_t getNumVTableComponents() const { |
| 1034 | return Components.size(); |
| 1035 | } |
| 1036 | |
| 1037 | const VTableComponent *vtable_component_begin() const { |
| 1038 | return Components.begin(); |
| 1039 | } |
| 1040 | |
| 1041 | const VTableComponent *vtable_component_end() const { |
| 1042 | return Components.end(); |
| 1043 | } |
| 1044 | |
| 1045 | AddressPointsMapTy::const_iterator address_points_begin() const { |
| 1046 | return AddressPoints.begin(); |
| 1047 | } |
| 1048 | |
| 1049 | AddressPointsMapTy::const_iterator address_points_end() const { |
| 1050 | return AddressPoints.end(); |
| 1051 | } |
| 1052 | |
| 1053 | VTableThunksMapTy::const_iterator vtable_thunks_begin() const { |
| 1054 | return VTableThunks.begin(); |
| 1055 | } |
| 1056 | |
| 1057 | VTableThunksMapTy::const_iterator vtable_thunks_end() const { |
| 1058 | return VTableThunks.end(); |
| 1059 | } |
| 1060 | |
| 1061 | /// dumpLayout - Dump the vtable layout. |
| 1062 | void dumpLayout(raw_ostream&); |
| 1063 | }; |
| 1064 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1065 | void ItaniumVTableBuilder::AddThunk(const CXXMethodDecl *MD, |
| 1066 | const ThunkInfo &Thunk) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1067 | assert(!isBuildingConstructorVTable() && |
| 1068 | "Can't add thunks for construction vtable"); |
| 1069 | |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 1070 | SmallVectorImpl<ThunkInfo> &ThunksVector = Thunks[MD]; |
| 1071 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1072 | // Check if we have this thunk already. |
| 1073 | if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) != |
| 1074 | ThunksVector.end()) |
| 1075 | return; |
| 1076 | |
| 1077 | ThunksVector.push_back(Thunk); |
| 1078 | } |
| 1079 | |
| 1080 | typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy; |
| 1081 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 1082 | /// Visit all the methods overridden by the given method recursively, |
| 1083 | /// in a depth-first pre-order. The Visitor's visitor method returns a bool |
| 1084 | /// indicating whether to continue the recursion for the given overridden |
| 1085 | /// method (i.e. returning false stops the iteration). |
| 1086 | template <class VisitorTy> |
| 1087 | static void |
| 1088 | visitAllOverriddenMethods(const CXXMethodDecl *MD, VisitorTy &Visitor) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1089 | assert(MD->isVirtual() && "Method is not virtual!"); |
| 1090 | |
| 1091 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1092 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1093 | const CXXMethodDecl *OverriddenMD = *I; |
Benjamin Kramer | 0cfa68e | 2015-07-25 16:31:30 +0000 | [diff] [blame] | 1094 | if (!Visitor(OverriddenMD)) |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 1095 | continue; |
| 1096 | visitAllOverriddenMethods(OverriddenMD, Visitor); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 1100 | /// ComputeAllOverriddenMethods - Given a method decl, will return a set of all |
| 1101 | /// the overridden methods that the function decl overrides. |
| 1102 | static void |
| 1103 | ComputeAllOverriddenMethods(const CXXMethodDecl *MD, |
| 1104 | OverriddenMethodsSetTy& OverriddenMethods) { |
Benjamin Kramer | 0cfa68e | 2015-07-25 16:31:30 +0000 | [diff] [blame] | 1105 | auto OverriddenMethodsCollector = [&](const CXXMethodDecl *MD) { |
| 1106 | // Don't recurse on this method if we've already collected it. |
| 1107 | return OverriddenMethods.insert(MD).second; |
| 1108 | }; |
| 1109 | visitAllOverriddenMethods(MD, OverriddenMethodsCollector); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1112 | void ItaniumVTableBuilder::ComputeThisAdjustments() { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1113 | // Now go through the method info map and see if any of the methods need |
| 1114 | // 'this' pointer adjustments. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 1115 | for (const auto &MI : MethodInfoMap) { |
| 1116 | const CXXMethodDecl *MD = MI.first; |
| 1117 | const MethodInfo &MethodInfo = MI.second; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1118 | |
| 1119 | // Ignore adjustments for unused function pointers. |
| 1120 | uint64_t VTableIndex = MethodInfo.VTableIndex; |
| 1121 | if (Components[VTableIndex].getKind() == |
| 1122 | VTableComponent::CK_UnusedFunctionPointer) |
| 1123 | continue; |
| 1124 | |
| 1125 | // Get the final overrider for this method. |
| 1126 | FinalOverriders::OverriderInfo Overrider = |
| 1127 | Overriders.getOverrider(MD, MethodInfo.BaseOffset); |
| 1128 | |
| 1129 | // Check if we need an adjustment at all. |
| 1130 | if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) { |
| 1131 | // When a return thunk is needed by a derived class that overrides a |
| 1132 | // virtual base, gcc uses a virtual 'this' adjustment as well. |
| 1133 | // While the thunk itself might be needed by vtables in subclasses or |
| 1134 | // in construction vtables, there doesn't seem to be a reason for using |
| 1135 | // the thunk in this vtable. Still, we do so to match gcc. |
| 1136 | if (VTableThunks.lookup(VTableIndex).Return.isEmpty()) |
| 1137 | continue; |
| 1138 | } |
| 1139 | |
| 1140 | ThisAdjustment ThisAdjustment = |
| 1141 | ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider); |
| 1142 | |
| 1143 | if (ThisAdjustment.isEmpty()) |
| 1144 | continue; |
| 1145 | |
| 1146 | // Add it. |
| 1147 | VTableThunks[VTableIndex].This = ThisAdjustment; |
| 1148 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1149 | if (isa<CXXDestructorDecl>(MD)) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1150 | // Add an adjustment for the deleting destructor as well. |
| 1151 | VTableThunks[VTableIndex + 1].This = ThisAdjustment; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | /// Clear the method info map. |
| 1156 | MethodInfoMap.clear(); |
| 1157 | |
| 1158 | if (isBuildingConstructorVTable()) { |
| 1159 | // We don't need to store thunk information for construction vtables. |
| 1160 | return; |
| 1161 | } |
| 1162 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 1163 | for (const auto &TI : VTableThunks) { |
| 1164 | const VTableComponent &Component = Components[TI.first]; |
| 1165 | const ThunkInfo &Thunk = TI.second; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1166 | const CXXMethodDecl *MD; |
| 1167 | |
| 1168 | switch (Component.getKind()) { |
| 1169 | default: |
| 1170 | llvm_unreachable("Unexpected vtable component kind!"); |
| 1171 | case VTableComponent::CK_FunctionPointer: |
| 1172 | MD = Component.getFunctionDecl(); |
| 1173 | break; |
| 1174 | case VTableComponent::CK_CompleteDtorPointer: |
| 1175 | MD = Component.getDestructorDecl(); |
| 1176 | break; |
| 1177 | case VTableComponent::CK_DeletingDtorPointer: |
| 1178 | // We've already added the thunk when we saw the complete dtor pointer. |
| 1179 | continue; |
| 1180 | } |
| 1181 | |
| 1182 | if (MD->getParent() == MostDerivedClass) |
| 1183 | AddThunk(MD, Thunk); |
| 1184 | } |
| 1185 | } |
| 1186 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1187 | ReturnAdjustment |
| 1188 | ItaniumVTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1189 | ReturnAdjustment Adjustment; |
| 1190 | |
| 1191 | if (!Offset.isEmpty()) { |
| 1192 | if (Offset.VirtualBase) { |
| 1193 | // Get the virtual base offset offset. |
| 1194 | if (Offset.DerivedClass == MostDerivedClass) { |
| 1195 | // We can get the offset offset directly from our map. |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1196 | Adjustment.Virtual.Itanium.VBaseOffsetOffset = |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1197 | VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity(); |
| 1198 | } else { |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1199 | Adjustment.Virtual.Itanium.VBaseOffsetOffset = |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1200 | VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass, |
| 1201 | Offset.VirtualBase).getQuantity(); |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity(); |
| 1206 | } |
| 1207 | |
| 1208 | return Adjustment; |
| 1209 | } |
| 1210 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1211 | BaseOffset ItaniumVTableBuilder::ComputeThisAdjustmentBaseOffset( |
| 1212 | BaseSubobject Base, BaseSubobject Derived) const { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1213 | const CXXRecordDecl *BaseRD = Base.getBase(); |
| 1214 | const CXXRecordDecl *DerivedRD = Derived.getBase(); |
| 1215 | |
| 1216 | CXXBasePaths Paths(/*FindAmbiguities=*/true, |
| 1217 | /*RecordPaths=*/true, /*DetectVirtual=*/true); |
| 1218 | |
Benjamin Kramer | 325d745 | 2013-02-03 18:55:34 +0000 | [diff] [blame] | 1219 | if (!DerivedRD->isDerivedFrom(BaseRD, Paths)) |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1220 | llvm_unreachable("Class must be derived from the passed in base class!"); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1221 | |
| 1222 | // We have to go through all the paths, and see which one leads us to the |
| 1223 | // right base subobject. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 1224 | for (const CXXBasePath &Path : Paths) { |
| 1225 | BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, Path); |
| 1226 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1227 | CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset; |
| 1228 | |
| 1229 | if (Offset.VirtualBase) { |
| 1230 | // If we have a virtual base class, the non-virtual offset is relative |
| 1231 | // to the virtual base class offset. |
| 1232 | const ASTRecordLayout &LayoutClassLayout = |
| 1233 | Context.getASTRecordLayout(LayoutClass); |
| 1234 | |
| 1235 | /// Get the virtual base offset, relative to the most derived class |
| 1236 | /// layout. |
| 1237 | OffsetToBaseSubobject += |
| 1238 | LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase); |
| 1239 | } else { |
| 1240 | // Otherwise, the non-virtual offset is relative to the derived class |
| 1241 | // offset. |
| 1242 | OffsetToBaseSubobject += Derived.getBaseOffset(); |
| 1243 | } |
| 1244 | |
| 1245 | // Check if this path gives us the right base subobject. |
| 1246 | if (OffsetToBaseSubobject == Base.getBaseOffset()) { |
| 1247 | // Since we're going from the base class _to_ the derived class, we'll |
| 1248 | // invert the non-virtual offset here. |
| 1249 | Offset.NonVirtualOffset = -Offset.NonVirtualOffset; |
| 1250 | return Offset; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | return BaseOffset(); |
| 1255 | } |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1256 | |
| 1257 | ThisAdjustment ItaniumVTableBuilder::ComputeThisAdjustment( |
| 1258 | const CXXMethodDecl *MD, CharUnits BaseOffsetInLayoutClass, |
| 1259 | FinalOverriders::OverriderInfo Overrider) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1260 | // Ignore adjustments for pure virtual member functions. |
| 1261 | if (Overrider.Method->isPure()) |
| 1262 | return ThisAdjustment(); |
| 1263 | |
| 1264 | BaseSubobject OverriddenBaseSubobject(MD->getParent(), |
| 1265 | BaseOffsetInLayoutClass); |
| 1266 | |
| 1267 | BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(), |
| 1268 | Overrider.Offset); |
| 1269 | |
| 1270 | // Compute the adjustment offset. |
| 1271 | BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject, |
| 1272 | OverriderBaseSubobject); |
| 1273 | if (Offset.isEmpty()) |
| 1274 | return ThisAdjustment(); |
| 1275 | |
| 1276 | ThisAdjustment Adjustment; |
| 1277 | |
| 1278 | if (Offset.VirtualBase) { |
| 1279 | // Get the vcall offset map for this virtual base. |
| 1280 | VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase]; |
| 1281 | |
| 1282 | if (VCallOffsets.empty()) { |
| 1283 | // We don't have vcall offsets for this virtual base, go ahead and |
| 1284 | // build them. |
| 1285 | VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1286 | /*FinalOverriders=*/nullptr, |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1287 | BaseSubobject(Offset.VirtualBase, |
| 1288 | CharUnits::Zero()), |
| 1289 | /*BaseIsVirtual=*/true, |
| 1290 | /*OffsetInLayoutClass=*/ |
| 1291 | CharUnits::Zero()); |
| 1292 | |
| 1293 | VCallOffsets = Builder.getVCallOffsets(); |
| 1294 | } |
| 1295 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 1296 | Adjustment.Virtual.Itanium.VCallOffsetOffset = |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1297 | VCallOffsets.getVCallOffsetOffset(MD).getQuantity(); |
| 1298 | } |
| 1299 | |
| 1300 | // Set the non-virtual part of the adjustment. |
| 1301 | Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity(); |
| 1302 | |
| 1303 | return Adjustment; |
| 1304 | } |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1305 | |
| 1306 | void ItaniumVTableBuilder::AddMethod(const CXXMethodDecl *MD, |
| 1307 | ReturnAdjustment ReturnAdjustment) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1308 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1309 | assert(ReturnAdjustment.isEmpty() && |
| 1310 | "Destructor can't have return adjustment!"); |
| 1311 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1312 | // Add both the complete destructor and the deleting destructor. |
| 1313 | Components.push_back(VTableComponent::MakeCompleteDtor(DD)); |
| 1314 | Components.push_back(VTableComponent::MakeDeletingDtor(DD)); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1315 | } else { |
| 1316 | // Add the return adjustment if necessary. |
| 1317 | if (!ReturnAdjustment.isEmpty()) |
| 1318 | VTableThunks[Components.size()].Return = ReturnAdjustment; |
| 1319 | |
| 1320 | // Add the function. |
| 1321 | Components.push_back(VTableComponent::MakeFunction(MD)); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | /// OverridesIndirectMethodInBase - Return whether the given member function |
| 1326 | /// overrides any methods in the set of given bases. |
| 1327 | /// Unlike OverridesMethodInBase, this checks "overriders of overriders". |
| 1328 | /// For example, if we have: |
| 1329 | /// |
| 1330 | /// struct A { virtual void f(); } |
| 1331 | /// struct B : A { virtual void f(); } |
| 1332 | /// struct C : B { virtual void f(); } |
| 1333 | /// |
| 1334 | /// OverridesIndirectMethodInBase will return true if given C::f as the method |
| 1335 | /// and { A } as the set of bases. |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1336 | static bool OverridesIndirectMethodInBases( |
| 1337 | const CXXMethodDecl *MD, |
| 1338 | ItaniumVTableBuilder::PrimaryBasesSetVectorTy &Bases) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1339 | if (Bases.count(MD->getParent())) |
| 1340 | return true; |
| 1341 | |
| 1342 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1343 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1344 | const CXXMethodDecl *OverriddenMD = *I; |
| 1345 | |
| 1346 | // Check "indirect overriders". |
| 1347 | if (OverridesIndirectMethodInBases(OverriddenMD, Bases)) |
| 1348 | return true; |
| 1349 | } |
| 1350 | |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1354 | bool ItaniumVTableBuilder::IsOverriderUsed( |
| 1355 | const CXXMethodDecl *Overrider, CharUnits BaseOffsetInLayoutClass, |
| 1356 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 1357 | CharUnits FirstBaseOffsetInLayoutClass) const { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1358 | // If the base and the first base in the primary base chain have the same |
| 1359 | // offsets, then this overrider will be used. |
| 1360 | if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass) |
| 1361 | return true; |
| 1362 | |
| 1363 | // We know now that Base (or a direct or indirect base of it) is a primary |
| 1364 | // base in part of the class hierarchy, but not a primary base in the most |
| 1365 | // derived class. |
| 1366 | |
| 1367 | // If the overrider is the first base in the primary base chain, we know |
| 1368 | // that the overrider will be used. |
| 1369 | if (Overrider->getParent() == FirstBaseInPrimaryBaseChain) |
| 1370 | return true; |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1371 | |
| 1372 | ItaniumVTableBuilder::PrimaryBasesSetVectorTy PrimaryBases; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1373 | |
| 1374 | const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain; |
| 1375 | PrimaryBases.insert(RD); |
| 1376 | |
| 1377 | // Now traverse the base chain, starting with the first base, until we find |
| 1378 | // the base that is no longer a primary base. |
| 1379 | while (true) { |
| 1380 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1381 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1382 | |
| 1383 | if (!PrimaryBase) |
| 1384 | break; |
| 1385 | |
| 1386 | if (Layout.isPrimaryBaseVirtual()) { |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1387 | assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1388 | "Primary base should always be at offset 0!"); |
| 1389 | |
| 1390 | const ASTRecordLayout &LayoutClassLayout = |
| 1391 | Context.getASTRecordLayout(LayoutClass); |
| 1392 | |
| 1393 | // Now check if this is the primary base that is not a primary base in the |
| 1394 | // most derived class. |
| 1395 | if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != |
| 1396 | FirstBaseOffsetInLayoutClass) { |
| 1397 | // We found it, stop walking the chain. |
| 1398 | break; |
| 1399 | } |
| 1400 | } else { |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1401 | assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1402 | "Primary base should always be at offset 0!"); |
| 1403 | } |
| 1404 | |
| 1405 | if (!PrimaryBases.insert(PrimaryBase)) |
| 1406 | llvm_unreachable("Found a duplicate primary base!"); |
| 1407 | |
| 1408 | RD = PrimaryBase; |
| 1409 | } |
| 1410 | |
| 1411 | // If the final overrider is an override of one of the primary bases, |
| 1412 | // then we know that it will be used. |
| 1413 | return OverridesIndirectMethodInBases(Overrider, PrimaryBases); |
| 1414 | } |
| 1415 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 1416 | typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> BasesSetVectorTy; |
| 1417 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1418 | /// FindNearestOverriddenMethod - Given a method, returns the overridden method |
| 1419 | /// from the nearest base. Returns null if no method was found. |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 1420 | /// The Bases are expected to be sorted in a base-to-derived order. |
| 1421 | static const CXXMethodDecl * |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1422 | FindNearestOverriddenMethod(const CXXMethodDecl *MD, |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 1423 | BasesSetVectorTy &Bases) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1424 | OverriddenMethodsSetTy OverriddenMethods; |
| 1425 | ComputeAllOverriddenMethods(MD, OverriddenMethods); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1426 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 1427 | for (const CXXRecordDecl *PrimaryBase : |
| 1428 | llvm::make_range(Bases.rbegin(), Bases.rend())) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 1429 | // Now check the overridden methods. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 1430 | for (const CXXMethodDecl *OverriddenMD : OverriddenMethods) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1431 | // We found our overridden method. |
| 1432 | if (OverriddenMD->getParent() == PrimaryBase) |
| 1433 | return OverriddenMD; |
| 1434 | } |
| 1435 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1436 | |
| 1437 | return nullptr; |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1438 | } |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1439 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1440 | void ItaniumVTableBuilder::AddMethods( |
| 1441 | BaseSubobject Base, CharUnits BaseOffsetInLayoutClass, |
| 1442 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 1443 | CharUnits FirstBaseOffsetInLayoutClass, |
| 1444 | PrimaryBasesSetVectorTy &PrimaryBases) { |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 1445 | // Itanium C++ ABI 2.5.2: |
| 1446 | // The order of the virtual function pointers in a virtual table is the |
| 1447 | // order of declaration of the corresponding member functions in the class. |
| 1448 | // |
| 1449 | // There is an entry for any virtual function declared in a class, |
| 1450 | // whether it is a new function or overrides a base class function, |
| 1451 | // unless it overrides a function from the primary base, and conversion |
| 1452 | // between their return types does not require an adjustment. |
| 1453 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1454 | const CXXRecordDecl *RD = Base.getBase(); |
| 1455 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1456 | |
| 1457 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
| 1458 | CharUnits PrimaryBaseOffset; |
| 1459 | CharUnits PrimaryBaseOffsetInLayoutClass; |
| 1460 | if (Layout.isPrimaryBaseVirtual()) { |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1461 | assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1462 | "Primary vbase should have a zero offset!"); |
| 1463 | |
| 1464 | const ASTRecordLayout &MostDerivedClassLayout = |
| 1465 | Context.getASTRecordLayout(MostDerivedClass); |
| 1466 | |
| 1467 | PrimaryBaseOffset = |
| 1468 | MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase); |
| 1469 | |
| 1470 | const ASTRecordLayout &LayoutClassLayout = |
| 1471 | Context.getASTRecordLayout(LayoutClass); |
| 1472 | |
| 1473 | PrimaryBaseOffsetInLayoutClass = |
| 1474 | LayoutClassLayout.getVBaseClassOffset(PrimaryBase); |
| 1475 | } else { |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1476 | assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1477 | "Primary base should have a zero offset!"); |
| 1478 | |
| 1479 | PrimaryBaseOffset = Base.getBaseOffset(); |
| 1480 | PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass; |
| 1481 | } |
| 1482 | |
| 1483 | AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset), |
| 1484 | PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain, |
| 1485 | FirstBaseOffsetInLayoutClass, PrimaryBases); |
| 1486 | |
| 1487 | if (!PrimaryBases.insert(PrimaryBase)) |
| 1488 | llvm_unreachable("Found a duplicate primary base!"); |
| 1489 | } |
| 1490 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1491 | const CXXDestructorDecl *ImplicitVirtualDtor = nullptr; |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 1492 | |
| 1493 | typedef llvm::SmallVector<const CXXMethodDecl *, 8> NewVirtualFunctionsTy; |
| 1494 | NewVirtualFunctionsTy NewVirtualFunctions; |
| 1495 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1496 | // Now go through all virtual member functions and add them. |
Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 1497 | for (const auto *MD : RD->methods()) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1498 | if (!MD->isVirtual()) |
| 1499 | continue; |
Reid Kleckner | 1cbd9aa | 2015-02-25 19:17:48 +0000 | [diff] [blame] | 1500 | MD = MD->getCanonicalDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1501 | |
| 1502 | // Get the final overrider. |
| 1503 | FinalOverriders::OverriderInfo Overrider = |
| 1504 | Overriders.getOverrider(MD, Base.getBaseOffset()); |
| 1505 | |
| 1506 | // Check if this virtual member function overrides a method in a primary |
| 1507 | // base. If this is the case, and the return type doesn't require adjustment |
| 1508 | // then we can just use the member function from the primary base. |
| 1509 | if (const CXXMethodDecl *OverriddenMD = |
| 1510 | FindNearestOverriddenMethod(MD, PrimaryBases)) { |
| 1511 | if (ComputeReturnAdjustmentBaseOffset(Context, MD, |
| 1512 | OverriddenMD).isEmpty()) { |
| 1513 | // Replace the method info of the overridden method with our own |
| 1514 | // method. |
| 1515 | assert(MethodInfoMap.count(OverriddenMD) && |
| 1516 | "Did not find the overridden method!"); |
| 1517 | MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD]; |
| 1518 | |
| 1519 | MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, |
| 1520 | OverriddenMethodInfo.VTableIndex); |
| 1521 | |
| 1522 | assert(!MethodInfoMap.count(MD) && |
| 1523 | "Should not have method info for this method yet!"); |
| 1524 | |
| 1525 | MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); |
| 1526 | MethodInfoMap.erase(OverriddenMD); |
| 1527 | |
| 1528 | // If the overridden method exists in a virtual base class or a direct |
| 1529 | // or indirect base class of a virtual base class, we need to emit a |
| 1530 | // thunk if we ever have a class hierarchy where the base class is not |
| 1531 | // a primary base in the complete object. |
| 1532 | if (!isBuildingConstructorVTable() && OverriddenMD != MD) { |
| 1533 | // Compute the this adjustment. |
| 1534 | ThisAdjustment ThisAdjustment = |
| 1535 | ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass, |
| 1536 | Overrider); |
| 1537 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 1538 | if (ThisAdjustment.Virtual.Itanium.VCallOffsetOffset && |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1539 | Overrider.Method->getParent() == MostDerivedClass) { |
| 1540 | |
| 1541 | // There's no return adjustment from OverriddenMD and MD, |
| 1542 | // but that doesn't mean there isn't one between MD and |
| 1543 | // the final overrider. |
| 1544 | BaseOffset ReturnAdjustmentOffset = |
| 1545 | ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD); |
| 1546 | ReturnAdjustment ReturnAdjustment = |
| 1547 | ComputeReturnAdjustment(ReturnAdjustmentOffset); |
| 1548 | |
| 1549 | // This is a virtual thunk for the most derived class, add it. |
| 1550 | AddThunk(Overrider.Method, |
| 1551 | ThunkInfo(ThisAdjustment, ReturnAdjustment)); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | continue; |
| 1556 | } |
| 1557 | } |
| 1558 | |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 1559 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1560 | if (MD->isImplicit()) { |
| 1561 | // Itanium C++ ABI 2.5.2: |
| 1562 | // If a class has an implicitly-defined virtual destructor, |
| 1563 | // its entries come after the declared virtual function pointers. |
| 1564 | |
| 1565 | assert(!ImplicitVirtualDtor && |
| 1566 | "Did already see an implicit virtual dtor!"); |
| 1567 | ImplicitVirtualDtor = DD; |
| 1568 | continue; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | NewVirtualFunctions.push_back(MD); |
| 1573 | } |
| 1574 | |
| 1575 | if (ImplicitVirtualDtor) |
| 1576 | NewVirtualFunctions.push_back(ImplicitVirtualDtor); |
| 1577 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 1578 | for (const CXXMethodDecl *MD : NewVirtualFunctions) { |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 1579 | // Get the final overrider. |
| 1580 | FinalOverriders::OverriderInfo Overrider = |
| 1581 | Overriders.getOverrider(MD, Base.getBaseOffset()); |
| 1582 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1583 | // Insert the method info for this method. |
| 1584 | MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, |
| 1585 | Components.size()); |
| 1586 | |
| 1587 | assert(!MethodInfoMap.count(MD) && |
| 1588 | "Should not have method info for this method yet!"); |
| 1589 | MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); |
| 1590 | |
| 1591 | // Check if this overrider is going to be used. |
| 1592 | const CXXMethodDecl *OverriderMD = Overrider.Method; |
| 1593 | if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass, |
| 1594 | FirstBaseInPrimaryBaseChain, |
| 1595 | FirstBaseOffsetInLayoutClass)) { |
| 1596 | Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD)); |
| 1597 | continue; |
| 1598 | } |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 1599 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1600 | // Check if this overrider needs a return adjustment. |
| 1601 | // We don't want to do this for pure virtual member functions. |
| 1602 | BaseOffset ReturnAdjustmentOffset; |
| 1603 | if (!OverriderMD->isPure()) { |
| 1604 | ReturnAdjustmentOffset = |
| 1605 | ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD); |
| 1606 | } |
| 1607 | |
| 1608 | ReturnAdjustment ReturnAdjustment = |
| 1609 | ComputeReturnAdjustment(ReturnAdjustmentOffset); |
| 1610 | |
| 1611 | AddMethod(Overrider.Method, ReturnAdjustment); |
| 1612 | } |
| 1613 | } |
| 1614 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1615 | void ItaniumVTableBuilder::LayoutVTable() { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1616 | LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass, |
| 1617 | CharUnits::Zero()), |
| 1618 | /*BaseIsMorallyVirtual=*/false, |
| 1619 | MostDerivedClassIsVirtual, |
| 1620 | MostDerivedClassOffset); |
| 1621 | |
| 1622 | VisitedVirtualBasesSetTy VBases; |
| 1623 | |
| 1624 | // Determine the primary virtual bases. |
| 1625 | DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset, |
| 1626 | VBases); |
| 1627 | VBases.clear(); |
| 1628 | |
| 1629 | LayoutVTablesForVirtualBases(MostDerivedClass, VBases); |
| 1630 | |
| 1631 | // -fapple-kext adds an extra entry at end of vtbl. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1632 | bool IsAppleKext = Context.getLangOpts().AppleKext; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1633 | if (IsAppleKext) |
| 1634 | Components.push_back(VTableComponent::MakeVCallOffset(CharUnits::Zero())); |
| 1635 | } |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1636 | |
| 1637 | void ItaniumVTableBuilder::LayoutPrimaryAndSecondaryVTables( |
| 1638 | BaseSubobject Base, bool BaseIsMorallyVirtual, |
| 1639 | bool BaseIsVirtualInLayoutClass, CharUnits OffsetInLayoutClass) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1640 | assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!"); |
| 1641 | |
| 1642 | // Add vcall and vbase offsets for this vtable. |
| 1643 | VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders, |
| 1644 | Base, BaseIsVirtualInLayoutClass, |
| 1645 | OffsetInLayoutClass); |
| 1646 | Components.append(Builder.components_begin(), Builder.components_end()); |
| 1647 | |
| 1648 | // Check if we need to add these vcall offsets. |
| 1649 | if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) { |
| 1650 | VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()]; |
| 1651 | |
| 1652 | if (VCallOffsets.empty()) |
| 1653 | VCallOffsets = Builder.getVCallOffsets(); |
| 1654 | } |
| 1655 | |
| 1656 | // If we're laying out the most derived class we want to keep track of the |
| 1657 | // virtual base class offset offsets. |
| 1658 | if (Base.getBase() == MostDerivedClass) |
| 1659 | VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets(); |
| 1660 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1661 | // Add the offset to top. |
| 1662 | CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass; |
| 1663 | Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop)); |
Timur Iskhodzhanov | 52b8a05 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 1664 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1665 | // Next, add the RTTI. |
| 1666 | Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass)); |
Timur Iskhodzhanov | 52b8a05 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 1667 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1668 | uint64_t AddressPoint = Components.size(); |
| 1669 | |
| 1670 | // Now go through all virtual member functions and add them. |
| 1671 | PrimaryBasesSetVectorTy PrimaryBases; |
| 1672 | AddMethods(Base, OffsetInLayoutClass, |
| 1673 | Base.getBase(), OffsetInLayoutClass, |
| 1674 | PrimaryBases); |
| 1675 | |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 1676 | const CXXRecordDecl *RD = Base.getBase(); |
| 1677 | if (RD == MostDerivedClass) { |
| 1678 | assert(MethodVTableIndices.empty()); |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 1679 | for (const auto &I : MethodInfoMap) { |
| 1680 | const CXXMethodDecl *MD = I.first; |
| 1681 | const MethodInfo &MI = I.second; |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 1682 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1683 | MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] |
| 1684 | = MI.VTableIndex - AddressPoint; |
| 1685 | MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] |
| 1686 | = MI.VTableIndex + 1 - AddressPoint; |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 1687 | } else { |
| 1688 | MethodVTableIndices[MD] = MI.VTableIndex - AddressPoint; |
| 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1693 | // Compute 'this' pointer adjustments. |
| 1694 | ComputeThisAdjustments(); |
| 1695 | |
| 1696 | // Add all address points. |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1697 | while (true) { |
| 1698 | AddressPoints.insert(std::make_pair( |
| 1699 | BaseSubobject(RD, OffsetInLayoutClass), |
| 1700 | AddressPoint)); |
| 1701 | |
| 1702 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1703 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1704 | |
| 1705 | if (!PrimaryBase) |
| 1706 | break; |
| 1707 | |
| 1708 | if (Layout.isPrimaryBaseVirtual()) { |
| 1709 | // Check if this virtual primary base is a primary base in the layout |
| 1710 | // class. If it's not, we don't want to add it. |
| 1711 | const ASTRecordLayout &LayoutClassLayout = |
| 1712 | Context.getASTRecordLayout(LayoutClass); |
| 1713 | |
| 1714 | if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != |
| 1715 | OffsetInLayoutClass) { |
| 1716 | // We don't want to add this class (or any of its primary bases). |
| 1717 | break; |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | RD = PrimaryBase; |
| 1722 | } |
| 1723 | |
| 1724 | // Layout secondary vtables. |
| 1725 | LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass); |
| 1726 | } |
| 1727 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1728 | void |
| 1729 | ItaniumVTableBuilder::LayoutSecondaryVTables(BaseSubobject Base, |
| 1730 | bool BaseIsMorallyVirtual, |
| 1731 | CharUnits OffsetInLayoutClass) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1732 | // Itanium C++ ABI 2.5.2: |
| 1733 | // Following the primary virtual table of a derived class are secondary |
| 1734 | // virtual tables for each of its proper base classes, except any primary |
| 1735 | // base(s) with which it shares its primary virtual table. |
| 1736 | |
| 1737 | const CXXRecordDecl *RD = Base.getBase(); |
| 1738 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1739 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1740 | |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 1741 | for (const auto &B : RD->bases()) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1742 | // Ignore virtual bases, we'll emit them later. |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 1743 | if (B.isVirtual()) |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1744 | continue; |
| 1745 | |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 1746 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1747 | |
| 1748 | // Ignore bases that don't have a vtable. |
| 1749 | if (!BaseDecl->isDynamicClass()) |
| 1750 | continue; |
| 1751 | |
| 1752 | if (isBuildingConstructorVTable()) { |
| 1753 | // Itanium C++ ABI 2.6.4: |
| 1754 | // Some of the base class subobjects may not need construction virtual |
| 1755 | // tables, which will therefore not be present in the construction |
| 1756 | // virtual table group, even though the subobject virtual tables are |
| 1757 | // present in the main virtual table group for the complete object. |
| 1758 | if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases()) |
| 1759 | continue; |
| 1760 | } |
| 1761 | |
| 1762 | // Get the base offset of this base. |
| 1763 | CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl); |
| 1764 | CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset; |
| 1765 | |
| 1766 | CharUnits BaseOffsetInLayoutClass = |
| 1767 | OffsetInLayoutClass + RelativeBaseOffset; |
| 1768 | |
| 1769 | // Don't emit a secondary vtable for a primary base. We might however want |
| 1770 | // to emit secondary vtables for other bases of this base. |
| 1771 | if (BaseDecl == PrimaryBase) { |
| 1772 | LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset), |
| 1773 | BaseIsMorallyVirtual, BaseOffsetInLayoutClass); |
| 1774 | continue; |
| 1775 | } |
| 1776 | |
| 1777 | // Layout the primary vtable (and any secondary vtables) for this base. |
| 1778 | LayoutPrimaryAndSecondaryVTables( |
| 1779 | BaseSubobject(BaseDecl, BaseOffset), |
| 1780 | BaseIsMorallyVirtual, |
| 1781 | /*BaseIsVirtualInLayoutClass=*/false, |
| 1782 | BaseOffsetInLayoutClass); |
| 1783 | } |
| 1784 | } |
| 1785 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1786 | void ItaniumVTableBuilder::DeterminePrimaryVirtualBases( |
| 1787 | const CXXRecordDecl *RD, CharUnits OffsetInLayoutClass, |
| 1788 | VisitedVirtualBasesSetTy &VBases) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1789 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1790 | |
| 1791 | // Check if this base has a primary base. |
| 1792 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
| 1793 | |
| 1794 | // Check if it's virtual. |
| 1795 | if (Layout.isPrimaryBaseVirtual()) { |
| 1796 | bool IsPrimaryVirtualBase = true; |
| 1797 | |
| 1798 | if (isBuildingConstructorVTable()) { |
| 1799 | // Check if the base is actually a primary base in the class we use for |
| 1800 | // layout. |
| 1801 | const ASTRecordLayout &LayoutClassLayout = |
| 1802 | Context.getASTRecordLayout(LayoutClass); |
| 1803 | |
| 1804 | CharUnits PrimaryBaseOffsetInLayoutClass = |
| 1805 | LayoutClassLayout.getVBaseClassOffset(PrimaryBase); |
| 1806 | |
| 1807 | // We know that the base is not a primary base in the layout class if |
| 1808 | // the base offsets are different. |
| 1809 | if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass) |
| 1810 | IsPrimaryVirtualBase = false; |
| 1811 | } |
| 1812 | |
| 1813 | if (IsPrimaryVirtualBase) |
| 1814 | PrimaryVirtualBases.insert(PrimaryBase); |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | // Traverse bases, looking for more primary virtual bases. |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 1819 | for (const auto &B : RD->bases()) { |
| 1820 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1821 | |
| 1822 | CharUnits BaseOffsetInLayoutClass; |
| 1823 | |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 1824 | if (B.isVirtual()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1825 | if (!VBases.insert(BaseDecl).second) |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1826 | continue; |
| 1827 | |
| 1828 | const ASTRecordLayout &LayoutClassLayout = |
| 1829 | Context.getASTRecordLayout(LayoutClass); |
| 1830 | |
| 1831 | BaseOffsetInLayoutClass = |
| 1832 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
| 1833 | } else { |
| 1834 | BaseOffsetInLayoutClass = |
| 1835 | OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl); |
| 1836 | } |
| 1837 | |
| 1838 | DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases); |
| 1839 | } |
| 1840 | } |
| 1841 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1842 | void ItaniumVTableBuilder::LayoutVTablesForVirtualBases( |
| 1843 | const CXXRecordDecl *RD, VisitedVirtualBasesSetTy &VBases) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1844 | // Itanium C++ ABI 2.5.2: |
| 1845 | // Then come the virtual base virtual tables, also in inheritance graph |
| 1846 | // order, and again excluding primary bases (which share virtual tables with |
| 1847 | // the classes for which they are primary). |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 1848 | for (const auto &B : RD->bases()) { |
| 1849 | const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1850 | |
| 1851 | // Check if this base needs a vtable. (If it's virtual, not a primary base |
| 1852 | // of some other class, and we haven't visited it before). |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1853 | if (B.isVirtual() && BaseDecl->isDynamicClass() && |
| 1854 | !PrimaryVirtualBases.count(BaseDecl) && |
| 1855 | VBases.insert(BaseDecl).second) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1856 | const ASTRecordLayout &MostDerivedClassLayout = |
| 1857 | Context.getASTRecordLayout(MostDerivedClass); |
| 1858 | CharUnits BaseOffset = |
| 1859 | MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
| 1860 | |
| 1861 | const ASTRecordLayout &LayoutClassLayout = |
| 1862 | Context.getASTRecordLayout(LayoutClass); |
| 1863 | CharUnits BaseOffsetInLayoutClass = |
| 1864 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
| 1865 | |
| 1866 | LayoutPrimaryAndSecondaryVTables( |
| 1867 | BaseSubobject(BaseDecl, BaseOffset), |
| 1868 | /*BaseIsMorallyVirtual=*/true, |
| 1869 | /*BaseIsVirtualInLayoutClass=*/true, |
| 1870 | BaseOffsetInLayoutClass); |
| 1871 | } |
| 1872 | |
| 1873 | // We only need to check the base for virtual base vtables if it actually |
| 1874 | // has virtual bases. |
| 1875 | if (BaseDecl->getNumVBases()) |
| 1876 | LayoutVTablesForVirtualBases(BaseDecl, VBases); |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | /// dumpLayout - Dump the vtable layout. |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1881 | void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) { |
Timur Iskhodzhanov | 1151031 | 2013-06-28 15:42:28 +0000 | [diff] [blame] | 1882 | // FIXME: write more tests that actually use the dumpLayout output to prevent |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 1883 | // ItaniumVTableBuilder regressions. |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1884 | |
| 1885 | if (isBuildingConstructorVTable()) { |
| 1886 | Out << "Construction vtable for ('"; |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 1887 | MostDerivedClass->printQualifiedName(Out); |
| 1888 | Out << "', "; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1889 | Out << MostDerivedClassOffset.getQuantity() << ") in '"; |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 1890 | LayoutClass->printQualifiedName(Out); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1891 | } else { |
| 1892 | Out << "Vtable for '"; |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 1893 | MostDerivedClass->printQualifiedName(Out); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1894 | } |
| 1895 | Out << "' (" << Components.size() << " entries).\n"; |
| 1896 | |
| 1897 | // Iterate through the address points and insert them into a new map where |
| 1898 | // they are keyed by the index and not the base object. |
| 1899 | // Since an address point can be shared by multiple subobjects, we use an |
| 1900 | // STL multimap. |
| 1901 | std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex; |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 1902 | for (const auto &AP : AddressPoints) { |
| 1903 | const BaseSubobject &Base = AP.first; |
| 1904 | uint64_t Index = AP.second; |
| 1905 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1906 | AddressPointsByIndex.insert(std::make_pair(Index, Base)); |
| 1907 | } |
| 1908 | |
| 1909 | for (unsigned I = 0, E = Components.size(); I != E; ++I) { |
| 1910 | uint64_t Index = I; |
| 1911 | |
| 1912 | Out << llvm::format("%4d | ", I); |
| 1913 | |
| 1914 | const VTableComponent &Component = Components[I]; |
| 1915 | |
| 1916 | // Dump the component. |
| 1917 | switch (Component.getKind()) { |
| 1918 | |
| 1919 | case VTableComponent::CK_VCallOffset: |
| 1920 | Out << "vcall_offset (" |
| 1921 | << Component.getVCallOffset().getQuantity() |
| 1922 | << ")"; |
| 1923 | break; |
| 1924 | |
| 1925 | case VTableComponent::CK_VBaseOffset: |
| 1926 | Out << "vbase_offset (" |
| 1927 | << Component.getVBaseOffset().getQuantity() |
| 1928 | << ")"; |
| 1929 | break; |
| 1930 | |
| 1931 | case VTableComponent::CK_OffsetToTop: |
| 1932 | Out << "offset_to_top (" |
| 1933 | << Component.getOffsetToTop().getQuantity() |
| 1934 | << ")"; |
| 1935 | break; |
| 1936 | |
| 1937 | case VTableComponent::CK_RTTI: |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 1938 | Component.getRTTIDecl()->printQualifiedName(Out); |
| 1939 | Out << " RTTI"; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1940 | break; |
| 1941 | |
| 1942 | case VTableComponent::CK_FunctionPointer: { |
| 1943 | const CXXMethodDecl *MD = Component.getFunctionDecl(); |
| 1944 | |
| 1945 | std::string Str = |
| 1946 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 1947 | MD); |
| 1948 | Out << Str; |
| 1949 | if (MD->isPure()) |
| 1950 | Out << " [pure]"; |
| 1951 | |
David Blaikie | 596d2ca | 2012-10-16 20:25:33 +0000 | [diff] [blame] | 1952 | if (MD->isDeleted()) |
| 1953 | Out << " [deleted]"; |
| 1954 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1955 | ThunkInfo Thunk = VTableThunks.lookup(I); |
| 1956 | if (!Thunk.isEmpty()) { |
| 1957 | // If this function pointer has a return adjustment, dump it. |
| 1958 | if (!Thunk.Return.isEmpty()) { |
| 1959 | Out << "\n [return adjustment: "; |
| 1960 | Out << Thunk.Return.NonVirtual << " non-virtual"; |
| 1961 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1962 | if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) { |
| 1963 | Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1964 | Out << " vbase offset offset"; |
| 1965 | } |
| 1966 | |
| 1967 | Out << ']'; |
| 1968 | } |
| 1969 | |
| 1970 | // If this function pointer has a 'this' pointer adjustment, dump it. |
| 1971 | if (!Thunk.This.isEmpty()) { |
| 1972 | Out << "\n [this adjustment: "; |
| 1973 | Out << Thunk.This.NonVirtual << " non-virtual"; |
| 1974 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 1975 | if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) { |
| 1976 | Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1977 | Out << " vcall offset offset"; |
| 1978 | } |
| 1979 | |
| 1980 | Out << ']'; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | break; |
| 1985 | } |
| 1986 | |
| 1987 | case VTableComponent::CK_CompleteDtorPointer: |
| 1988 | case VTableComponent::CK_DeletingDtorPointer: { |
| 1989 | bool IsComplete = |
| 1990 | Component.getKind() == VTableComponent::CK_CompleteDtorPointer; |
| 1991 | |
| 1992 | const CXXDestructorDecl *DD = Component.getDestructorDecl(); |
| 1993 | |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 1994 | DD->printQualifiedName(Out); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1995 | if (IsComplete) |
| 1996 | Out << "() [complete]"; |
| 1997 | else |
| 1998 | Out << "() [deleting]"; |
| 1999 | |
| 2000 | if (DD->isPure()) |
| 2001 | Out << " [pure]"; |
| 2002 | |
| 2003 | ThunkInfo Thunk = VTableThunks.lookup(I); |
| 2004 | if (!Thunk.isEmpty()) { |
| 2005 | // If this destructor has a 'this' pointer adjustment, dump it. |
| 2006 | if (!Thunk.This.isEmpty()) { |
| 2007 | Out << "\n [this adjustment: "; |
| 2008 | Out << Thunk.This.NonVirtual << " non-virtual"; |
| 2009 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2010 | if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) { |
| 2011 | Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2012 | Out << " vcall offset offset"; |
| 2013 | } |
| 2014 | |
| 2015 | Out << ']'; |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | break; |
| 2020 | } |
| 2021 | |
| 2022 | case VTableComponent::CK_UnusedFunctionPointer: { |
| 2023 | const CXXMethodDecl *MD = Component.getUnusedFunctionDecl(); |
| 2024 | |
| 2025 | std::string Str = |
| 2026 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 2027 | MD); |
| 2028 | Out << "[unused] " << Str; |
| 2029 | if (MD->isPure()) |
| 2030 | Out << " [pure]"; |
| 2031 | } |
| 2032 | |
| 2033 | } |
| 2034 | |
| 2035 | Out << '\n'; |
| 2036 | |
| 2037 | // Dump the next address point. |
| 2038 | uint64_t NextIndex = Index + 1; |
| 2039 | if (AddressPointsByIndex.count(NextIndex)) { |
| 2040 | if (AddressPointsByIndex.count(NextIndex) == 1) { |
| 2041 | const BaseSubobject &Base = |
| 2042 | AddressPointsByIndex.find(NextIndex)->second; |
| 2043 | |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 2044 | Out << " -- ("; |
| 2045 | Base.getBase()->printQualifiedName(Out); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2046 | Out << ", " << Base.getBaseOffset().getQuantity(); |
| 2047 | Out << ") vtable address --\n"; |
| 2048 | } else { |
| 2049 | CharUnits BaseOffset = |
| 2050 | AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset(); |
| 2051 | |
| 2052 | // We store the class names in a set to get a stable order. |
| 2053 | std::set<std::string> ClassNames; |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2054 | for (const auto &I : |
| 2055 | llvm::make_range(AddressPointsByIndex.equal_range(NextIndex))) { |
| 2056 | assert(I.second.getBaseOffset() == BaseOffset && |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2057 | "Invalid base offset!"); |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2058 | const CXXRecordDecl *RD = I.second.getBase(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2059 | ClassNames.insert(RD->getQualifiedNameAsString()); |
| 2060 | } |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2061 | |
| 2062 | for (const std::string &Name : ClassNames) { |
| 2063 | Out << " -- (" << Name; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2064 | Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n"; |
| 2065 | } |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | Out << '\n'; |
| 2071 | |
| 2072 | if (isBuildingConstructorVTable()) |
| 2073 | return; |
| 2074 | |
| 2075 | if (MostDerivedClass->getNumVBases()) { |
| 2076 | // We store the virtual base class names and their offsets in a map to get |
| 2077 | // a stable order. |
| 2078 | |
| 2079 | std::map<std::string, CharUnits> ClassNamesAndOffsets; |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2080 | for (const auto &I : VBaseOffsetOffsets) { |
| 2081 | std::string ClassName = I.first->getQualifiedNameAsString(); |
| 2082 | CharUnits OffsetOffset = I.second; |
| 2083 | ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset)); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | Out << "Virtual base offset offsets for '"; |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 2087 | MostDerivedClass->printQualifiedName(Out); |
| 2088 | Out << "' ("; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2089 | Out << ClassNamesAndOffsets.size(); |
| 2090 | Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n"; |
| 2091 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2092 | for (const auto &I : ClassNamesAndOffsets) |
| 2093 | Out << " " << I.first << " | " << I.second.getQuantity() << '\n'; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2094 | |
| 2095 | Out << "\n"; |
| 2096 | } |
| 2097 | |
| 2098 | if (!Thunks.empty()) { |
| 2099 | // We store the method names in a map to get a stable order. |
| 2100 | std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls; |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2101 | |
| 2102 | for (const auto &I : Thunks) { |
| 2103 | const CXXMethodDecl *MD = I.first; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2104 | std::string MethodName = |
| 2105 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 2106 | MD); |
| 2107 | |
| 2108 | MethodNamesAndDecls.insert(std::make_pair(MethodName, MD)); |
| 2109 | } |
| 2110 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2111 | for (const auto &I : MethodNamesAndDecls) { |
| 2112 | const std::string &MethodName = I.first; |
| 2113 | const CXXMethodDecl *MD = I.second; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2114 | |
| 2115 | ThunkInfoVectorTy ThunksVector = Thunks[MD]; |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2116 | std::sort(ThunksVector.begin(), ThunksVector.end(), |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 2117 | [](const ThunkInfo &LHS, const ThunkInfo &RHS) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2118 | assert(LHS.Method == nullptr && RHS.Method == nullptr); |
Benjamin Kramer | a741b8c | 2014-03-03 20:26:46 +0000 | [diff] [blame] | 2119 | return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return); |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 2120 | }); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2121 | |
| 2122 | Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size(); |
| 2123 | Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n"; |
| 2124 | |
| 2125 | for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) { |
| 2126 | const ThunkInfo &Thunk = ThunksVector[I]; |
| 2127 | |
| 2128 | Out << llvm::format("%4d | ", I); |
| 2129 | |
| 2130 | // If this function pointer has a return pointer adjustment, dump it. |
| 2131 | if (!Thunk.Return.isEmpty()) { |
Timur Iskhodzhanov | 1151031 | 2013-06-28 15:42:28 +0000 | [diff] [blame] | 2132 | Out << "return adjustment: " << Thunk.Return.NonVirtual; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2133 | Out << " non-virtual"; |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 2134 | if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) { |
| 2135 | Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2136 | Out << " vbase offset offset"; |
| 2137 | } |
| 2138 | |
| 2139 | if (!Thunk.This.isEmpty()) |
| 2140 | Out << "\n "; |
| 2141 | } |
| 2142 | |
| 2143 | // If this function pointer has a 'this' pointer adjustment, dump it. |
| 2144 | if (!Thunk.This.isEmpty()) { |
| 2145 | Out << "this adjustment: "; |
| 2146 | Out << Thunk.This.NonVirtual << " non-virtual"; |
| 2147 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2148 | if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) { |
| 2149 | Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2150 | Out << " vcall offset offset"; |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | Out << '\n'; |
| 2155 | } |
| 2156 | |
| 2157 | Out << '\n'; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | // Compute the vtable indices for all the member functions. |
| 2162 | // Store them in a map keyed by the index so we'll get a sorted table. |
| 2163 | std::map<uint64_t, std::string> IndicesMap; |
| 2164 | |
Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 2165 | for (const auto *MD : MostDerivedClass->methods()) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2166 | // We only want virtual member functions. |
| 2167 | if (!MD->isVirtual()) |
| 2168 | continue; |
Reid Kleckner | 1cbd9aa | 2015-02-25 19:17:48 +0000 | [diff] [blame] | 2169 | MD = MD->getCanonicalDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2170 | |
| 2171 | std::string MethodName = |
| 2172 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 2173 | MD); |
| 2174 | |
| 2175 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 2176 | GlobalDecl GD(DD, Dtor_Complete); |
| 2177 | assert(MethodVTableIndices.count(GD)); |
| 2178 | uint64_t VTableIndex = MethodVTableIndices[GD]; |
| 2179 | IndicesMap[VTableIndex] = MethodName + " [complete]"; |
| 2180 | IndicesMap[VTableIndex + 1] = MethodName + " [deleting]"; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2181 | } else { |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 2182 | assert(MethodVTableIndices.count(MD)); |
| 2183 | IndicesMap[MethodVTableIndices[MD]] = MethodName; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | // Print the vtable indices for all the member functions. |
| 2188 | if (!IndicesMap.empty()) { |
| 2189 | Out << "VTable indices for '"; |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 2190 | MostDerivedClass->printQualifiedName(Out); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2191 | Out << "' (" << IndicesMap.size() << " entries).\n"; |
| 2192 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2193 | for (const auto &I : IndicesMap) { |
| 2194 | uint64_t VTableIndex = I.first; |
| 2195 | const std::string &MethodName = I.second; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2196 | |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 2197 | Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName |
Benjamin Kramer | 5291e68 | 2012-03-10 02:06:27 +0000 | [diff] [blame] | 2198 | << '\n'; |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | Out << '\n'; |
| 2203 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2204 | } |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2205 | |
| 2206 | VTableLayout::VTableLayout(uint64_t NumVTableComponents, |
| 2207 | const VTableComponent *VTableComponents, |
| 2208 | uint64_t NumVTableThunks, |
| 2209 | const VTableThunkTy *VTableThunks, |
Timur Iskhodzhanov | 52b8a05 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2210 | const AddressPointsMapTy &AddressPoints, |
| 2211 | bool IsMicrosoftABI) |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2212 | : NumVTableComponents(NumVTableComponents), |
| 2213 | VTableComponents(new VTableComponent[NumVTableComponents]), |
| 2214 | NumVTableThunks(NumVTableThunks), |
| 2215 | VTableThunks(new VTableThunkTy[NumVTableThunks]), |
Timur Iskhodzhanov | 52b8a05 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2216 | AddressPoints(AddressPoints), |
| 2217 | IsMicrosoftABI(IsMicrosoftABI) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2218 | std::copy(VTableComponents, VTableComponents+NumVTableComponents, |
Benjamin Kramer | e298063 | 2012-04-14 14:13:43 +0000 | [diff] [blame] | 2219 | this->VTableComponents.get()); |
| 2220 | std::copy(VTableThunks, VTableThunks+NumVTableThunks, |
| 2221 | this->VTableThunks.get()); |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2222 | std::sort(this->VTableThunks.get(), |
| 2223 | this->VTableThunks.get() + NumVTableThunks, |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 2224 | [](const VTableLayout::VTableThunkTy &LHS, |
| 2225 | const VTableLayout::VTableThunkTy &RHS) { |
| 2226 | assert((LHS.first != RHS.first || LHS.second == RHS.second) && |
| 2227 | "Different thunks should have unique indices!"); |
| 2228 | return LHS.first < RHS.first; |
| 2229 | }); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 2232 | VTableLayout::~VTableLayout() { } |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2233 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 2234 | ItaniumVTableContext::ItaniumVTableContext(ASTContext &Context) |
Reid Kleckner | b60a3d5 | 2013-12-20 23:58:52 +0000 | [diff] [blame] | 2235 | : VTableContextBase(/*MS=*/false) {} |
Timur Iskhodzhanov | 52b8a05 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2236 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 2237 | ItaniumVTableContext::~ItaniumVTableContext() { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2238 | llvm::DeleteContainerSeconds(VTableLayouts); |
| 2239 | } |
| 2240 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 2241 | uint64_t ItaniumVTableContext::getMethodVTableIndex(GlobalDecl GD) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2242 | MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD); |
| 2243 | if (I != MethodVTableIndices.end()) |
| 2244 | return I->second; |
| 2245 | |
| 2246 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
| 2247 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2248 | computeVTableRelatedInformation(RD); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2249 | |
| 2250 | I = MethodVTableIndices.find(GD); |
| 2251 | assert(I != MethodVTableIndices.end() && "Did not find index!"); |
| 2252 | return I->second; |
| 2253 | } |
| 2254 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 2255 | CharUnits |
| 2256 | ItaniumVTableContext::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, |
| 2257 | const CXXRecordDecl *VBase) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2258 | ClassPairTy ClassPair(RD, VBase); |
| 2259 | |
| 2260 | VirtualBaseClassOffsetOffsetsMapTy::iterator I = |
| 2261 | VirtualBaseClassOffsetOffsets.find(ClassPair); |
| 2262 | if (I != VirtualBaseClassOffsetOffsets.end()) |
| 2263 | return I->second; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2264 | |
| 2265 | VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/nullptr, |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2266 | BaseSubobject(RD, CharUnits::Zero()), |
| 2267 | /*BaseIsVirtual=*/false, |
| 2268 | /*OffsetInLayoutClass=*/CharUnits::Zero()); |
| 2269 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2270 | for (const auto &I : Builder.getVBaseOffsetOffsets()) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2271 | // Insert all types. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2272 | ClassPairTy ClassPair(RD, I.first); |
| 2273 | |
| 2274 | VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I.second)); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | I = VirtualBaseClassOffsetOffsets.find(ClassPair); |
| 2278 | assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!"); |
| 2279 | |
| 2280 | return I->second; |
| 2281 | } |
| 2282 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 2283 | static VTableLayout *CreateVTableLayout(const ItaniumVTableBuilder &Builder) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2284 | SmallVector<VTableLayout::VTableThunkTy, 1> |
| 2285 | VTableThunks(Builder.vtable_thunks_begin(), Builder.vtable_thunks_end()); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2286 | |
| 2287 | return new VTableLayout(Builder.getNumVTableComponents(), |
| 2288 | Builder.vtable_component_begin(), |
| 2289 | VTableThunks.size(), |
| 2290 | VTableThunks.data(), |
Timur Iskhodzhanov | 52b8a05 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2291 | Builder.getAddressPoints(), |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 2292 | /*IsMicrosoftABI=*/false); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 2295 | void |
| 2296 | ItaniumVTableContext::computeVTableRelatedInformation(const CXXRecordDecl *RD) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2297 | const VTableLayout *&Entry = VTableLayouts[RD]; |
| 2298 | |
| 2299 | // Check if we've computed this information before. |
| 2300 | if (Entry) |
| 2301 | return; |
| 2302 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 2303 | ItaniumVTableBuilder Builder(*this, RD, CharUnits::Zero(), |
| 2304 | /*MostDerivedClassIsVirtual=*/0, RD); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2305 | Entry = CreateVTableLayout(Builder); |
| 2306 | |
Timur Iskhodzhanov | 05e3670 | 2013-06-05 14:05:50 +0000 | [diff] [blame] | 2307 | MethodVTableIndices.insert(Builder.vtable_indices_begin(), |
| 2308 | Builder.vtable_indices_end()); |
| 2309 | |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2310 | // Add the known thunks. |
| 2311 | Thunks.insert(Builder.thunks_begin(), Builder.thunks_end()); |
| 2312 | |
| 2313 | // If we don't have the vbase information for this class, insert it. |
| 2314 | // getVirtualBaseOffsetOffset will compute it separately without computing |
| 2315 | // the rest of the vtable related information. |
| 2316 | if (!RD->getNumVBases()) |
| 2317 | return; |
| 2318 | |
Timur Iskhodzhanov | 7f55a45 | 2013-07-02 16:00:40 +0000 | [diff] [blame] | 2319 | const CXXRecordDecl *VBase = |
| 2320 | RD->vbases_begin()->getType()->getAsCXXRecordDecl(); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2321 | |
| 2322 | if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase))) |
| 2323 | return; |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 2324 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2325 | for (const auto &I : Builder.getVBaseOffsetOffsets()) { |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2326 | // Insert all types. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2327 | ClassPairTy ClassPair(RD, I.first); |
| 2328 | |
| 2329 | VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I.second)); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2330 | } |
| 2331 | } |
| 2332 | |
Timur Iskhodzhanov | e1ebc5f | 2013-10-09 11:33:51 +0000 | [diff] [blame] | 2333 | VTableLayout *ItaniumVTableContext::createConstructionVTableLayout( |
| 2334 | const CXXRecordDecl *MostDerivedClass, CharUnits MostDerivedClassOffset, |
| 2335 | bool MostDerivedClassIsVirtual, const CXXRecordDecl *LayoutClass) { |
| 2336 | ItaniumVTableBuilder Builder(*this, MostDerivedClass, MostDerivedClassOffset, |
| 2337 | MostDerivedClassIsVirtual, LayoutClass); |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2338 | return CreateVTableLayout(Builder); |
| 2339 | } |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2340 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2341 | namespace { |
| 2342 | |
| 2343 | // Vtables in the Microsoft ABI are different from the Itanium ABI. |
| 2344 | // |
| 2345 | // The main differences are: |
| 2346 | // 1. Separate vftable and vbtable. |
| 2347 | // |
| 2348 | // 2. Each subobject with a vfptr gets its own vftable rather than an address |
| 2349 | // point in a single vtable shared between all the subobjects. |
| 2350 | // Each vftable is represented by a separate section and virtual calls |
| 2351 | // must be done using the vftable which has a slot for the function to be |
| 2352 | // called. |
| 2353 | // |
| 2354 | // 3. Virtual method definitions expect their 'this' parameter to point to the |
| 2355 | // first vfptr whose table provides a compatible overridden method. In many |
| 2356 | // cases, this permits the original vf-table entry to directly call |
| 2357 | // the method instead of passing through a thunk. |
Timur Iskhodzhanov | 46a18ef | 2014-11-14 14:10:15 +0000 | [diff] [blame] | 2358 | // See example before VFTableBuilder::ComputeThisOffset below. |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2359 | // |
| 2360 | // A compatible overridden method is one which does not have a non-trivial |
| 2361 | // covariant-return adjustment. |
| 2362 | // |
| 2363 | // The first vfptr is the one with the lowest offset in the complete-object |
| 2364 | // layout of the defining class, and the method definition will subtract |
| 2365 | // that constant offset from the parameter value to get the real 'this' |
| 2366 | // value. Therefore, if the offset isn't really constant (e.g. if a virtual |
| 2367 | // function defined in a virtual base is overridden in a more derived |
| 2368 | // virtual base and these bases have a reverse order in the complete |
| 2369 | // object), the vf-table may require a this-adjustment thunk. |
| 2370 | // |
| 2371 | // 4. vftables do not contain new entries for overrides that merely require |
| 2372 | // this-adjustment. Together with #3, this keeps vf-tables smaller and |
| 2373 | // eliminates the need for this-adjustment thunks in many cases, at the cost |
| 2374 | // of often requiring redundant work to adjust the "this" pointer. |
| 2375 | // |
| 2376 | // 5. Instead of VTT and constructor vtables, vbtables and vtordisps are used. |
| 2377 | // Vtordisps are emitted into the class layout if a class has |
| 2378 | // a) a user-defined ctor/dtor |
| 2379 | // and |
| 2380 | // b) a method overriding a method in a virtual base. |
Timur Iskhodzhanov | 46a18ef | 2014-11-14 14:10:15 +0000 | [diff] [blame] | 2381 | // |
| 2382 | // To get a better understanding of this code, |
| 2383 | // you might want to see examples in test/CodeGenCXX/microsoft-abi-vtables-*.cpp |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2384 | |
| 2385 | class VFTableBuilder { |
| 2386 | public: |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 2387 | typedef MicrosoftVTableContext::MethodVFTableLocation MethodVFTableLocation; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2388 | |
| 2389 | typedef llvm::DenseMap<GlobalDecl, MethodVFTableLocation> |
| 2390 | MethodVFTableLocationsTy; |
| 2391 | |
Timur Iskhodzhanov | ba55702 | 2014-03-20 20:38:34 +0000 | [diff] [blame] | 2392 | typedef llvm::iterator_range<MethodVFTableLocationsTy::const_iterator> |
| 2393 | method_locations_range; |
| 2394 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2395 | private: |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 2396 | /// VTables - Global vtable information. |
| 2397 | MicrosoftVTableContext &VTables; |
| 2398 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2399 | /// Context - The ASTContext which we will use for layout information. |
| 2400 | ASTContext &Context; |
| 2401 | |
| 2402 | /// MostDerivedClass - The most derived class for which we're building this |
| 2403 | /// vtable. |
| 2404 | const CXXRecordDecl *MostDerivedClass; |
| 2405 | |
| 2406 | const ASTRecordLayout &MostDerivedClassLayout; |
| 2407 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 2408 | const VPtrInfo &WhichVFPtr; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2409 | |
| 2410 | /// FinalOverriders - The final overriders of the most derived class. |
| 2411 | const FinalOverriders Overriders; |
| 2412 | |
| 2413 | /// Components - The components of the vftable being built. |
| 2414 | SmallVector<VTableComponent, 64> Components; |
| 2415 | |
| 2416 | MethodVFTableLocationsTy MethodVFTableLocations; |
| 2417 | |
David Majnemer | 3c7228e | 2014-07-01 21:10:07 +0000 | [diff] [blame] | 2418 | /// \brief Does this class have an RTTI component? |
David Majnemer | 2d8b200 | 2016-02-11 17:49:28 +0000 | [diff] [blame] | 2419 | bool HasRTTIComponent = false; |
David Majnemer | 3c7228e | 2014-07-01 21:10:07 +0000 | [diff] [blame] | 2420 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2421 | /// MethodInfo - Contains information about a method in a vtable. |
| 2422 | /// (Used for computing 'this' pointer adjustment thunks. |
| 2423 | struct MethodInfo { |
| 2424 | /// VBTableIndex - The nonzero index in the vbtable that |
| 2425 | /// this method's base has, or zero. |
| 2426 | const uint64_t VBTableIndex; |
| 2427 | |
| 2428 | /// VFTableIndex - The index in the vftable that this method has. |
| 2429 | const uint64_t VFTableIndex; |
| 2430 | |
| 2431 | /// Shadowed - Indicates if this vftable slot is shadowed by |
| 2432 | /// a slot for a covariant-return override. If so, it shouldn't be printed |
| 2433 | /// or used for vcalls in the most derived class. |
| 2434 | bool Shadowed; |
| 2435 | |
Timur Iskhodzhanov | 16055e7 | 2014-08-10 11:40:51 +0000 | [diff] [blame] | 2436 | /// UsesExtraSlot - Indicates if this vftable slot was created because |
| 2437 | /// any of the overridden slots required a return adjusting thunk. |
| 2438 | bool UsesExtraSlot; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2439 | |
Timur Iskhodzhanov | 16055e7 | 2014-08-10 11:40:51 +0000 | [diff] [blame] | 2440 | MethodInfo(uint64_t VBTableIndex, uint64_t VFTableIndex, |
| 2441 | bool UsesExtraSlot = false) |
| 2442 | : VBTableIndex(VBTableIndex), VFTableIndex(VFTableIndex), |
| 2443 | Shadowed(false), UsesExtraSlot(UsesExtraSlot) {} |
| 2444 | |
| 2445 | MethodInfo() |
| 2446 | : VBTableIndex(0), VFTableIndex(0), Shadowed(false), |
| 2447 | UsesExtraSlot(false) {} |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2448 | }; |
| 2449 | |
| 2450 | typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy; |
| 2451 | |
| 2452 | /// MethodInfoMap - The information for all methods in the vftable we're |
| 2453 | /// currently building. |
| 2454 | MethodInfoMapTy MethodInfoMap; |
| 2455 | |
| 2456 | typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy; |
| 2457 | |
| 2458 | /// VTableThunks - The thunks by vftable index in the vftable currently being |
| 2459 | /// built. |
| 2460 | VTableThunksMapTy VTableThunks; |
| 2461 | |
| 2462 | typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; |
| 2463 | typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; |
| 2464 | |
| 2465 | /// Thunks - A map that contains all the thunks needed for all methods in the |
| 2466 | /// most derived class for which the vftable is currently being built. |
| 2467 | ThunksMapTy Thunks; |
| 2468 | |
| 2469 | /// AddThunk - Add a thunk for the given method. |
| 2470 | void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) { |
| 2471 | SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD]; |
| 2472 | |
| 2473 | // Check if we have this thunk already. |
| 2474 | if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) != |
| 2475 | ThunksVector.end()) |
| 2476 | return; |
| 2477 | |
| 2478 | ThunksVector.push_back(Thunk); |
| 2479 | } |
| 2480 | |
| 2481 | /// ComputeThisOffset - Returns the 'this' argument offset for the given |
Timur Iskhodzhanov | 3a9ac93 | 2014-03-04 18:17:38 +0000 | [diff] [blame] | 2482 | /// method, relative to the beginning of the MostDerivedClass. |
| 2483 | CharUnits ComputeThisOffset(FinalOverriders::OverriderInfo Overrider); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2484 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2485 | void CalculateVtordispAdjustment(FinalOverriders::OverriderInfo Overrider, |
| 2486 | CharUnits ThisOffset, ThisAdjustment &TA); |
| 2487 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2488 | /// AddMethod - Add a single virtual member function to the vftable |
| 2489 | /// components vector. |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2490 | void AddMethod(const CXXMethodDecl *MD, ThunkInfo TI) { |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 2491 | if (!TI.isEmpty()) { |
| 2492 | VTableThunks[Components.size()] = TI; |
| 2493 | AddThunk(MD, TI); |
| 2494 | } |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2495 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 2496 | assert(TI.Return.isEmpty() && |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2497 | "Destructor can't have return adjustment!"); |
| 2498 | Components.push_back(VTableComponent::MakeDeletingDtor(DD)); |
| 2499 | } else { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2500 | Components.push_back(VTableComponent::MakeFunction(MD)); |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | /// AddMethods - Add the methods of this base subobject and the relevant |
| 2505 | /// subbases to the vftable we're currently laying out. |
| 2506 | void AddMethods(BaseSubobject Base, unsigned BaseDepth, |
| 2507 | const CXXRecordDecl *LastVBase, |
| 2508 | BasesSetVectorTy &VisitedBases); |
| 2509 | |
| 2510 | void LayoutVFTable() { |
David Majnemer | 6e9ae78 | 2014-09-22 20:39:37 +0000 | [diff] [blame] | 2511 | // RTTI data goes before all other entries. |
| 2512 | if (HasRTTIComponent) |
| 2513 | Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass)); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2514 | |
| 2515 | BasesSetVectorTy VisitedBases; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2516 | AddMethods(BaseSubobject(MostDerivedClass, CharUnits::Zero()), 0, nullptr, |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2517 | VisitedBases); |
David Majnemer | a9b7e66 | 2014-09-26 08:07:55 +0000 | [diff] [blame] | 2518 | assert((HasRTTIComponent ? Components.size() - 1 : Components.size()) && |
| 2519 | "vftable can't be empty"); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2520 | |
| 2521 | assert(MethodVFTableLocations.empty()); |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2522 | for (const auto &I : MethodInfoMap) { |
| 2523 | const CXXMethodDecl *MD = I.first; |
| 2524 | const MethodInfo &MI = I.second; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2525 | // Skip the methods that the MostDerivedClass didn't override |
| 2526 | // and the entries shadowed by return adjusting thunks. |
| 2527 | if (MD->getParent() != MostDerivedClass || MI.Shadowed) |
| 2528 | continue; |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 2529 | MethodVFTableLocation Loc(MI.VBTableIndex, WhichVFPtr.getVBaseWithVPtr(), |
| 2530 | WhichVFPtr.NonVirtualOffset, MI.VFTableIndex); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2531 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 2532 | MethodVFTableLocations[GlobalDecl(DD, Dtor_Deleting)] = Loc; |
| 2533 | } else { |
| 2534 | MethodVFTableLocations[MD] = Loc; |
| 2535 | } |
| 2536 | } |
| 2537 | } |
| 2538 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2539 | public: |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 2540 | VFTableBuilder(MicrosoftVTableContext &VTables, |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 2541 | const CXXRecordDecl *MostDerivedClass, const VPtrInfo *Which) |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 2542 | : VTables(VTables), |
| 2543 | Context(MostDerivedClass->getASTContext()), |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2544 | MostDerivedClass(MostDerivedClass), |
| 2545 | MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)), |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 2546 | WhichVFPtr(*Which), |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2547 | Overriders(MostDerivedClass, CharUnits(), MostDerivedClass) { |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 2548 | // Only include the RTTI component if we know that we will provide a |
David Majnemer | 2d8b200 | 2016-02-11 17:49:28 +0000 | [diff] [blame] | 2549 | // definition of the vftable. We always provide the definition of |
| 2550 | // dllimported classes. |
| 2551 | if (Context.getLangOpts().RTTIData) |
| 2552 | if (MostDerivedClass->hasAttr<DLLImportAttr>() || |
| 2553 | MostDerivedClass->getTemplateSpecializationKind() != |
| 2554 | TSK_ExplicitInstantiationDeclaration) |
| 2555 | HasRTTIComponent = true; |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 2556 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2557 | LayoutVFTable(); |
| 2558 | |
| 2559 | if (Context.getLangOpts().DumpVTableLayouts) |
Reid Kleckner | 5bc6d0f | 2013-11-08 21:28:00 +0000 | [diff] [blame] | 2560 | dumpLayout(llvm::outs()); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | uint64_t getNumThunks() const { return Thunks.size(); } |
| 2564 | |
| 2565 | ThunksMapTy::const_iterator thunks_begin() const { return Thunks.begin(); } |
| 2566 | |
| 2567 | ThunksMapTy::const_iterator thunks_end() const { return Thunks.end(); } |
| 2568 | |
Timur Iskhodzhanov | ba55702 | 2014-03-20 20:38:34 +0000 | [diff] [blame] | 2569 | method_locations_range vtable_locations() const { |
| 2570 | return method_locations_range(MethodVFTableLocations.begin(), |
| 2571 | MethodVFTableLocations.end()); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | uint64_t getNumVTableComponents() const { return Components.size(); } |
| 2575 | |
| 2576 | const VTableComponent *vtable_component_begin() const { |
| 2577 | return Components.begin(); |
| 2578 | } |
| 2579 | |
| 2580 | const VTableComponent *vtable_component_end() const { |
| 2581 | return Components.end(); |
| 2582 | } |
| 2583 | |
| 2584 | VTableThunksMapTy::const_iterator vtable_thunks_begin() const { |
| 2585 | return VTableThunks.begin(); |
| 2586 | } |
| 2587 | |
| 2588 | VTableThunksMapTy::const_iterator vtable_thunks_end() const { |
| 2589 | return VTableThunks.end(); |
| 2590 | } |
| 2591 | |
| 2592 | void dumpLayout(raw_ostream &); |
| 2593 | }; |
| 2594 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 2595 | } // end namespace |
| 2596 | |
Timur Iskhodzhanov | 46a18ef | 2014-11-14 14:10:15 +0000 | [diff] [blame] | 2597 | // Let's study one class hierarchy as an example: |
| 2598 | // struct A { |
| 2599 | // virtual void f(); |
| 2600 | // int x; |
| 2601 | // }; |
| 2602 | // |
| 2603 | // struct B : virtual A { |
| 2604 | // virtual void f(); |
| 2605 | // }; |
| 2606 | // |
| 2607 | // Record layouts: |
| 2608 | // struct A: |
Timur Iskhodzhanov | 4d28002 | 2014-11-14 14:16:34 +0000 | [diff] [blame] | 2609 | // 0 | (A vftable pointer) |
Timur Iskhodzhanov | 46a18ef | 2014-11-14 14:10:15 +0000 | [diff] [blame] | 2610 | // 4 | int x |
| 2611 | // |
| 2612 | // struct B: |
Timur Iskhodzhanov | 4d28002 | 2014-11-14 14:16:34 +0000 | [diff] [blame] | 2613 | // 0 | (B vbtable pointer) |
| 2614 | // 4 | struct A (virtual base) |
| 2615 | // 4 | (A vftable pointer) |
Timur Iskhodzhanov | 46a18ef | 2014-11-14 14:10:15 +0000 | [diff] [blame] | 2616 | // 8 | int x |
| 2617 | // |
| 2618 | // Let's assume we have a pointer to the A part of an object of dynamic type B: |
| 2619 | // B b; |
| 2620 | // A *a = (A*)&b; |
| 2621 | // a->f(); |
| 2622 | // |
| 2623 | // In this hierarchy, f() belongs to the vftable of A, so B::f() expects |
| 2624 | // "this" parameter to point at the A subobject, which is B+4. |
| 2625 | // In the B::f() prologue, it adjusts "this" back to B by subtracting 4, |
Nico Weber | cda5c7c | 2014-11-29 23:57:35 +0000 | [diff] [blame] | 2626 | // performed as a *static* adjustment. |
Timur Iskhodzhanov | 46a18ef | 2014-11-14 14:10:15 +0000 | [diff] [blame] | 2627 | // |
| 2628 | // Interesting thing happens when we alter the relative placement of A and B |
| 2629 | // subobjects in a class: |
| 2630 | // struct C : virtual B { }; |
| 2631 | // |
| 2632 | // C c; |
| 2633 | // A *a = (A*)&c; |
| 2634 | // a->f(); |
| 2635 | // |
| 2636 | // Respective record layout is: |
| 2637 | // 0 | (C vbtable pointer) |
| 2638 | // 4 | struct A (virtual base) |
| 2639 | // 4 | (A vftable pointer) |
| 2640 | // 8 | int x |
| 2641 | // 12 | struct B (virtual base) |
| 2642 | // 12 | (B vbtable pointer) |
| 2643 | // |
| 2644 | // The final overrider of f() in class C is still B::f(), so B+4 should be |
| 2645 | // passed as "this" to that code. However, "a" points at B-8, so the respective |
| 2646 | // vftable entry should hold a thunk that adds 12 to the "this" argument before |
| 2647 | // performing a tail call to B::f(). |
| 2648 | // |
| 2649 | // With this example in mind, we can now calculate the 'this' argument offset |
| 2650 | // for the given method, relative to the beginning of the MostDerivedClass. |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2651 | CharUnits |
Timur Iskhodzhanov | 3a9ac93 | 2014-03-04 18:17:38 +0000 | [diff] [blame] | 2652 | VFTableBuilder::ComputeThisOffset(FinalOverriders::OverriderInfo Overrider) { |
Benjamin Kramer | 0cfa68e | 2015-07-25 16:31:30 +0000 | [diff] [blame] | 2653 | BasesSetVectorTy Bases; |
| 2654 | |
| 2655 | { |
| 2656 | // Find the set of least derived bases that define the given method. |
| 2657 | OverriddenMethodsSetTy VisitedOverriddenMethods; |
| 2658 | auto InitialOverriddenDefinitionCollector = [&]( |
| 2659 | const CXXMethodDecl *OverriddenMD) { |
| 2660 | if (OverriddenMD->size_overridden_methods() == 0) |
| 2661 | Bases.insert(OverriddenMD->getParent()); |
| 2662 | // Don't recurse on this method if we've already collected it. |
| 2663 | return VisitedOverriddenMethods.insert(OverriddenMD).second; |
| 2664 | }; |
| 2665 | visitAllOverriddenMethods(Overrider.Method, |
| 2666 | InitialOverriddenDefinitionCollector); |
| 2667 | } |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2668 | |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 2669 | // If there are no overrides then 'this' is located |
| 2670 | // in the base that defines the method. |
Benjamin Kramer | 0cfa68e | 2015-07-25 16:31:30 +0000 | [diff] [blame] | 2671 | if (Bases.size() == 0) |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 2672 | return Overrider.Offset; |
| 2673 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2674 | CXXBasePaths Paths; |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 2675 | Overrider.Method->getParent()->lookupInBases( |
Benjamin Kramer | 0cfa68e | 2015-07-25 16:31:30 +0000 | [diff] [blame] | 2676 | [&Bases](const CXXBaseSpecifier *Specifier, CXXBasePath &) { |
| 2677 | return Bases.count(Specifier->getType()->getAsCXXRecordDecl()); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 2678 | }, |
| 2679 | Paths); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2680 | |
| 2681 | // This will hold the smallest this offset among overridees of MD. |
| 2682 | // This implies that an offset of a non-virtual base will dominate an offset |
| 2683 | // of a virtual base to potentially reduce the number of thunks required |
| 2684 | // in the derived classes that inherit this method. |
| 2685 | CharUnits Ret; |
| 2686 | bool First = true; |
| 2687 | |
Timur Iskhodzhanov | ed11ae3 | 2014-04-17 22:01:48 +0000 | [diff] [blame] | 2688 | const ASTRecordLayout &OverriderRDLayout = |
| 2689 | Context.getASTRecordLayout(Overrider.Method->getParent()); |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2690 | for (const CXXBasePath &Path : Paths) { |
Timur Iskhodzhanov | 3a9ac93 | 2014-03-04 18:17:38 +0000 | [diff] [blame] | 2691 | CharUnits ThisOffset = Overrider.Offset; |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2692 | CharUnits LastVBaseOffset; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2693 | |
Nico Weber | d73258a | 2015-05-16 23:49:53 +0000 | [diff] [blame] | 2694 | // For each path from the overrider to the parents of the overridden |
| 2695 | // methods, traverse the path, calculating the this offset in the most |
| 2696 | // derived class. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2697 | for (const CXXBasePathElement &Element : Path) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2698 | QualType CurTy = Element.Base->getType(); |
| 2699 | const CXXRecordDecl *PrevRD = Element.Class, |
| 2700 | *CurRD = CurTy->getAsCXXRecordDecl(); |
| 2701 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(PrevRD); |
| 2702 | |
| 2703 | if (Element.Base->isVirtual()) { |
Timur Iskhodzhanov | ed11ae3 | 2014-04-17 22:01:48 +0000 | [diff] [blame] | 2704 | // The interesting things begin when you have virtual inheritance. |
| 2705 | // The final overrider will use a static adjustment equal to the offset |
| 2706 | // of the vbase in the final overrider class. |
| 2707 | // For example, if the final overrider is in a vbase B of the most |
| 2708 | // derived class and it overrides a method of the B's own vbase A, |
| 2709 | // it uses A* as "this". In its prologue, it can cast A* to B* with |
| 2710 | // a static offset. This offset is used regardless of the actual |
| 2711 | // offset of A from B in the most derived class, requiring an |
| 2712 | // this-adjusting thunk in the vftable if A and B are laid out |
| 2713 | // differently in the most derived class. |
| 2714 | LastVBaseOffset = ThisOffset = |
| 2715 | Overrider.Offset + OverriderRDLayout.getVBaseClassOffset(CurRD); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2716 | } else { |
| 2717 | ThisOffset += Layout.getBaseClassOffset(CurRD); |
| 2718 | } |
| 2719 | } |
| 2720 | |
Timur Iskhodzhanov | 3a9ac93 | 2014-03-04 18:17:38 +0000 | [diff] [blame] | 2721 | if (isa<CXXDestructorDecl>(Overrider.Method)) { |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2722 | if (LastVBaseOffset.isZero()) { |
| 2723 | // If a "Base" class has at least one non-virtual base with a virtual |
| 2724 | // destructor, the "Base" virtual destructor will take the address |
| 2725 | // of the "Base" subobject as the "this" argument. |
Timur Iskhodzhanov | 3a9ac93 | 2014-03-04 18:17:38 +0000 | [diff] [blame] | 2726 | ThisOffset = Overrider.Offset; |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2727 | } else { |
| 2728 | // A virtual destructor of a virtual base takes the address of the |
| 2729 | // virtual base subobject as the "this" argument. |
Timur Iskhodzhanov | 3a9ac93 | 2014-03-04 18:17:38 +0000 | [diff] [blame] | 2730 | ThisOffset = LastVBaseOffset; |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2731 | } |
| 2732 | } |
Timur Iskhodzhanov | 62082b7 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 2733 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2734 | if (Ret > ThisOffset || First) { |
| 2735 | First = false; |
| 2736 | Ret = ThisOffset; |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | assert(!First && "Method not found in the given subobject?"); |
| 2741 | return Ret; |
| 2742 | } |
| 2743 | |
Timur Iskhodzhanov | 7d19fc1 | 2014-11-17 15:11:05 +0000 | [diff] [blame] | 2744 | // Things are getting even more complex when the "this" adjustment has to |
| 2745 | // use a dynamic offset instead of a static one, or even two dynamic offsets. |
| 2746 | // This is sometimes required when a virtual call happens in the middle of |
| 2747 | // a non-most-derived class construction or destruction. |
| 2748 | // |
| 2749 | // Let's take a look at the following example: |
| 2750 | // struct A { |
| 2751 | // virtual void f(); |
| 2752 | // }; |
| 2753 | // |
| 2754 | // void foo(A *a) { a->f(); } // Knows nothing about siblings of A. |
| 2755 | // |
| 2756 | // struct B : virtual A { |
| 2757 | // virtual void f(); |
| 2758 | // B() { |
| 2759 | // foo(this); |
| 2760 | // } |
| 2761 | // }; |
| 2762 | // |
| 2763 | // struct C : virtual B { |
| 2764 | // virtual void f(); |
| 2765 | // }; |
| 2766 | // |
| 2767 | // Record layouts for these classes are: |
| 2768 | // struct A |
| 2769 | // 0 | (A vftable pointer) |
| 2770 | // |
| 2771 | // struct B |
| 2772 | // 0 | (B vbtable pointer) |
| 2773 | // 4 | (vtordisp for vbase A) |
| 2774 | // 8 | struct A (virtual base) |
| 2775 | // 8 | (A vftable pointer) |
| 2776 | // |
| 2777 | // struct C |
| 2778 | // 0 | (C vbtable pointer) |
| 2779 | // 4 | (vtordisp for vbase A) |
| 2780 | // 8 | struct A (virtual base) // A precedes B! |
| 2781 | // 8 | (A vftable pointer) |
| 2782 | // 12 | struct B (virtual base) |
| 2783 | // 12 | (B vbtable pointer) |
| 2784 | // |
| 2785 | // When one creates an object of type C, the C constructor: |
| 2786 | // - initializes all the vbptrs, then |
| 2787 | // - calls the A subobject constructor |
| 2788 | // (initializes A's vfptr with an address of A vftable), then |
| 2789 | // - calls the B subobject constructor |
| 2790 | // (initializes A's vfptr with an address of B vftable and vtordisp for A), |
| 2791 | // that in turn calls foo(), then |
| 2792 | // - initializes A's vfptr with an address of C vftable and zeroes out the |
| 2793 | // vtordisp |
| 2794 | // FIXME: if a structor knows it belongs to MDC, why doesn't it use a vftable |
| 2795 | // without vtordisp thunks? |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 2796 | // FIXME: how are vtordisp handled in the presence of nooverride/final? |
Timur Iskhodzhanov | 7d19fc1 | 2014-11-17 15:11:05 +0000 | [diff] [blame] | 2797 | // |
| 2798 | // When foo() is called, an object with a layout of class C has a vftable |
| 2799 | // referencing B::f() that assumes a B layout, so the "this" adjustments are |
| 2800 | // incorrect, unless an extra adjustment is done. This adjustment is called |
| 2801 | // "vtordisp adjustment". Vtordisp basically holds the difference between the |
| 2802 | // actual location of a vbase in the layout class and the location assumed by |
| 2803 | // the vftable of the class being constructed/destructed. Vtordisp is only |
| 2804 | // needed if "this" escapes a |
| 2805 | // structor (or we can't prove otherwise). |
| 2806 | // [i.e. vtordisp is a dynamic adjustment for a static adjustment, which is an |
| 2807 | // estimation of a dynamic adjustment] |
| 2808 | // |
| 2809 | // foo() gets a pointer to the A vbase and doesn't know anything about B or C, |
| 2810 | // so it just passes that pointer as "this" in a virtual call. |
| 2811 | // If there was no vtordisp, that would just dispatch to B::f(). |
| 2812 | // However, B::f() assumes B+8 is passed as "this", |
| 2813 | // yet the pointer foo() passes along is B-4 (i.e. C+8). |
| 2814 | // An extra adjustment is needed, so we emit a thunk into the B vftable. |
| 2815 | // This vtordisp thunk subtracts the value of vtordisp |
| 2816 | // from the "this" argument (-12) before making a tailcall to B::f(). |
| 2817 | // |
| 2818 | // Let's consider an even more complex example: |
| 2819 | // struct D : virtual B, virtual C { |
| 2820 | // D() { |
| 2821 | // foo(this); |
| 2822 | // } |
| 2823 | // }; |
| 2824 | // |
| 2825 | // struct D |
| 2826 | // 0 | (D vbtable pointer) |
| 2827 | // 4 | (vtordisp for vbase A) |
| 2828 | // 8 | struct A (virtual base) // A precedes both B and C! |
| 2829 | // 8 | (A vftable pointer) |
| 2830 | // 12 | struct B (virtual base) // B precedes C! |
| 2831 | // 12 | (B vbtable pointer) |
| 2832 | // 16 | struct C (virtual base) |
| 2833 | // 16 | (C vbtable pointer) |
| 2834 | // |
| 2835 | // When D::D() calls foo(), we find ourselves in a thunk that should tailcall |
| 2836 | // to C::f(), which assumes C+8 as its "this" parameter. This time, foo() |
| 2837 | // passes along A, which is C-8. The A vtordisp holds |
| 2838 | // "D.vbptr[index_of_A] - offset_of_A_in_D" |
| 2839 | // and we statically know offset_of_A_in_D, so can get a pointer to D. |
| 2840 | // When we know it, we can make an extra vbtable lookup to locate the C vbase |
| 2841 | // and one extra static adjustment to calculate the expected value of C+8. |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2842 | void VFTableBuilder::CalculateVtordispAdjustment( |
| 2843 | FinalOverriders::OverriderInfo Overrider, CharUnits ThisOffset, |
| 2844 | ThisAdjustment &TA) { |
| 2845 | const ASTRecordLayout::VBaseOffsetsMapTy &VBaseMap = |
| 2846 | MostDerivedClassLayout.getVBaseOffsetsMap(); |
| 2847 | const ASTRecordLayout::VBaseOffsetsMapTy::const_iterator &VBaseMapEntry = |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 2848 | VBaseMap.find(WhichVFPtr.getVBaseWithVPtr()); |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2849 | assert(VBaseMapEntry != VBaseMap.end()); |
| 2850 | |
Timur Iskhodzhanov | 6b12850 | 2014-04-22 17:32:02 +0000 | [diff] [blame] | 2851 | // If there's no vtordisp or the final overrider is defined in the same vbase |
| 2852 | // as the initial declaration, we don't need any vtordisp adjustment. |
| 2853 | if (!VBaseMapEntry->second.hasVtorDisp() || |
| 2854 | Overrider.VirtualBase == WhichVFPtr.getVBaseWithVPtr()) |
Timur Iskhodzhanov | 057fa3a | 2014-04-17 11:01:41 +0000 | [diff] [blame] | 2855 | return; |
| 2856 | |
| 2857 | // OK, now we know we need to use a vtordisp thunk. |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2858 | // The implicit vtordisp field is located right before the vbase. |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 2859 | CharUnits OffsetOfVBaseWithVFPtr = VBaseMapEntry->second.VBaseOffset; |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2860 | TA.Virtual.Microsoft.VtordispOffset = |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 2861 | (OffsetOfVBaseWithVFPtr - WhichVFPtr.FullOffsetInMDC).getQuantity() - 4; |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2862 | |
Timur Iskhodzhanov | 057fa3a | 2014-04-17 11:01:41 +0000 | [diff] [blame] | 2863 | // A simple vtordisp thunk will suffice if the final overrider is defined |
| 2864 | // in either the most derived class or its non-virtual base. |
Timur Iskhodzhanov | 6b12850 | 2014-04-22 17:32:02 +0000 | [diff] [blame] | 2865 | if (Overrider.Method->getParent() == MostDerivedClass || |
| 2866 | !Overrider.VirtualBase) |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2867 | return; |
| 2868 | |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2869 | // Otherwise, we need to do use the dynamic offset of the final overrider |
| 2870 | // in order to get "this" adjustment right. |
| 2871 | TA.Virtual.Microsoft.VBPtrOffset = |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 2872 | (OffsetOfVBaseWithVFPtr + WhichVFPtr.NonVirtualOffset - |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2873 | MostDerivedClassLayout.getVBPtrOffset()).getQuantity(); |
| 2874 | TA.Virtual.Microsoft.VBOffsetOffset = |
| 2875 | Context.getTypeSizeInChars(Context.IntTy).getQuantity() * |
Timur Iskhodzhanov | 6b12850 | 2014-04-22 17:32:02 +0000 | [diff] [blame] | 2876 | VTables.getVBTableIndex(MostDerivedClass, Overrider.VirtualBase); |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 2877 | |
| 2878 | TA.NonVirtual = (ThisOffset - Overrider.Offset).getQuantity(); |
| 2879 | } |
| 2880 | |
Timur Iskhodzhanov | 20df98c | 2013-10-06 15:31:37 +0000 | [diff] [blame] | 2881 | static void GroupNewVirtualOverloads( |
| 2882 | const CXXRecordDecl *RD, |
| 2883 | SmallVector<const CXXMethodDecl *, 10> &VirtualMethods) { |
| 2884 | // Put the virtual methods into VirtualMethods in the proper order: |
| 2885 | // 1) Group overloads by declaration name. New groups are added to the |
| 2886 | // vftable in the order of their first declarations in this class |
David Majnemer | 70effde | 2015-11-19 00:03:54 +0000 | [diff] [blame] | 2887 | // (including overrides, non-virtual methods and any other named decl that |
| 2888 | // might be nested within the class). |
Timur Iskhodzhanov | 20df98c | 2013-10-06 15:31:37 +0000 | [diff] [blame] | 2889 | // 2) In each group, new overloads appear in the reverse order of declaration. |
| 2890 | typedef SmallVector<const CXXMethodDecl *, 1> MethodGroup; |
| 2891 | SmallVector<MethodGroup, 10> Groups; |
| 2892 | typedef llvm::DenseMap<DeclarationName, unsigned> VisitedGroupIndicesTy; |
| 2893 | VisitedGroupIndicesTy VisitedGroupIndices; |
David Majnemer | 70effde | 2015-11-19 00:03:54 +0000 | [diff] [blame] | 2894 | for (const auto *D : RD->decls()) { |
| 2895 | const auto *ND = dyn_cast<NamedDecl>(D); |
| 2896 | if (!ND) |
| 2897 | continue; |
Timur Iskhodzhanov | 20df98c | 2013-10-06 15:31:37 +0000 | [diff] [blame] | 2898 | VisitedGroupIndicesTy::iterator J; |
| 2899 | bool Inserted; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 2900 | std::tie(J, Inserted) = VisitedGroupIndices.insert( |
David Majnemer | 70effde | 2015-11-19 00:03:54 +0000 | [diff] [blame] | 2901 | std::make_pair(ND->getDeclName(), Groups.size())); |
Timur Iskhodzhanov | 20df98c | 2013-10-06 15:31:37 +0000 | [diff] [blame] | 2902 | if (Inserted) |
Reid Kleckner | 6701de2 | 2014-02-19 22:06:10 +0000 | [diff] [blame] | 2903 | Groups.push_back(MethodGroup()); |
David Majnemer | 70effde | 2015-11-19 00:03:54 +0000 | [diff] [blame] | 2904 | if (const auto *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 2905 | if (MD->isVirtual()) |
| 2906 | Groups[J->second].push_back(MD->getCanonicalDecl()); |
Timur Iskhodzhanov | 20df98c | 2013-10-06 15:31:37 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2909 | for (const MethodGroup &Group : Groups) |
| 2910 | VirtualMethods.append(Group.rbegin(), Group.rend()); |
Timur Iskhodzhanov | 20df98c | 2013-10-06 15:31:37 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Timur Iskhodzhanov | dd0a2766 | 2014-03-26 08:12:53 +0000 | [diff] [blame] | 2913 | static bool isDirectVBase(const CXXRecordDecl *Base, const CXXRecordDecl *RD) { |
| 2914 | for (const auto &B : RD->bases()) { |
| 2915 | if (B.isVirtual() && B.getType()->getAsCXXRecordDecl() == Base) |
| 2916 | return true; |
| 2917 | } |
| 2918 | return false; |
| 2919 | } |
| 2920 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2921 | void VFTableBuilder::AddMethods(BaseSubobject Base, unsigned BaseDepth, |
| 2922 | const CXXRecordDecl *LastVBase, |
| 2923 | BasesSetVectorTy &VisitedBases) { |
| 2924 | const CXXRecordDecl *RD = Base.getBase(); |
| 2925 | if (!RD->isPolymorphic()) |
| 2926 | return; |
| 2927 | |
| 2928 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 2929 | |
| 2930 | // See if this class expands a vftable of the base we look at, which is either |
Nico Weber | d73258a | 2015-05-16 23:49:53 +0000 | [diff] [blame] | 2931 | // the one defined by the vfptr base path or the primary base of the current |
| 2932 | // class. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2933 | const CXXRecordDecl *NextBase = nullptr, *NextLastVBase = LastVBase; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2934 | CharUnits NextBaseOffset; |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 2935 | if (BaseDepth < WhichVFPtr.PathToBaseWithVPtr.size()) { |
| 2936 | NextBase = WhichVFPtr.PathToBaseWithVPtr[BaseDepth]; |
Timur Iskhodzhanov | dd0a2766 | 2014-03-26 08:12:53 +0000 | [diff] [blame] | 2937 | if (isDirectVBase(NextBase, RD)) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2938 | NextLastVBase = NextBase; |
| 2939 | NextBaseOffset = MostDerivedClassLayout.getVBaseClassOffset(NextBase); |
| 2940 | } else { |
| 2941 | NextBaseOffset = |
| 2942 | Base.getBaseOffset() + Layout.getBaseClassOffset(NextBase); |
| 2943 | } |
| 2944 | } else if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
| 2945 | assert(!Layout.isPrimaryBaseVirtual() && |
| 2946 | "No primary virtual bases in this ABI"); |
| 2947 | NextBase = PrimaryBase; |
| 2948 | NextBaseOffset = Base.getBaseOffset(); |
| 2949 | } |
| 2950 | |
| 2951 | if (NextBase) { |
| 2952 | AddMethods(BaseSubobject(NextBase, NextBaseOffset), BaseDepth + 1, |
| 2953 | NextLastVBase, VisitedBases); |
| 2954 | if (!VisitedBases.insert(NextBase)) |
| 2955 | llvm_unreachable("Found a duplicate primary base!"); |
| 2956 | } |
| 2957 | |
Timur Iskhodzhanov | 20df98c | 2013-10-06 15:31:37 +0000 | [diff] [blame] | 2958 | SmallVector<const CXXMethodDecl*, 10> VirtualMethods; |
| 2959 | // Put virtual methods in the proper order. |
| 2960 | GroupNewVirtualOverloads(RD, VirtualMethods); |
| 2961 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2962 | // Now go through all virtual member functions and add them to the current |
| 2963 | // vftable. This is done by |
| 2964 | // - replacing overridden methods in their existing slots, as long as they |
| 2965 | // don't require return adjustment; calculating This adjustment if needed. |
| 2966 | // - adding new slots for methods of the current base not present in any |
| 2967 | // sub-bases; |
| 2968 | // - adding new slots for methods that require Return adjustment. |
| 2969 | // We keep track of the methods visited in the sub-bases in MethodInfoMap. |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 2970 | for (const CXXMethodDecl *MD : VirtualMethods) { |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 2971 | FinalOverriders::OverriderInfo FinalOverrider = |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2972 | Overriders.getOverrider(MD, Base.getBaseOffset()); |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 2973 | const CXXMethodDecl *FinalOverriderMD = FinalOverrider.Method; |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 2974 | const CXXMethodDecl *OverriddenMD = |
| 2975 | FindNearestOverriddenMethod(MD, VisitedBases); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2976 | |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 2977 | ThisAdjustment ThisAdjustmentOffset; |
Timur Iskhodzhanov | 16055e7 | 2014-08-10 11:40:51 +0000 | [diff] [blame] | 2978 | bool ReturnAdjustingThunk = false, ForceReturnAdjustmentMangling = false; |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 2979 | CharUnits ThisOffset = ComputeThisOffset(FinalOverrider); |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 2980 | ThisAdjustmentOffset.NonVirtual = |
| 2981 | (ThisOffset - WhichVFPtr.FullOffsetInMDC).getQuantity(); |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 2982 | if ((OverriddenMD || FinalOverriderMD != MD) && |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 2983 | WhichVFPtr.getVBaseWithVPtr()) |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 2984 | CalculateVtordispAdjustment(FinalOverrider, ThisOffset, |
| 2985 | ThisAdjustmentOffset); |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 2986 | |
| 2987 | if (OverriddenMD) { |
Nico Weber | d73258a | 2015-05-16 23:49:53 +0000 | [diff] [blame] | 2988 | // If MD overrides anything in this vftable, we need to update the |
| 2989 | // entries. |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 2990 | MethodInfoMapTy::iterator OverriddenMDIterator = |
| 2991 | MethodInfoMap.find(OverriddenMD); |
| 2992 | |
| 2993 | // If the overridden method went to a different vftable, skip it. |
| 2994 | if (OverriddenMDIterator == MethodInfoMap.end()) |
| 2995 | continue; |
| 2996 | |
| 2997 | MethodInfo &OverriddenMethodInfo = OverriddenMDIterator->second; |
| 2998 | |
Timur Iskhodzhanov | 16055e7 | 2014-08-10 11:40:51 +0000 | [diff] [blame] | 2999 | // Let's check if the overrider requires any return adjustments. |
| 3000 | // We must create a new slot if the MD's return type is not trivially |
| 3001 | // convertible to the OverriddenMD's one. |
| 3002 | // Once a chain of method overrides adds a return adjusting vftable slot, |
| 3003 | // all subsequent overrides will also use an extra method slot. |
| 3004 | ReturnAdjustingThunk = !ComputeReturnAdjustmentBaseOffset( |
| 3005 | Context, MD, OverriddenMD).isEmpty() || |
| 3006 | OverriddenMethodInfo.UsesExtraSlot; |
| 3007 | |
| 3008 | if (!ReturnAdjustingThunk) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3009 | // No return adjustment needed - just replace the overridden method info |
| 3010 | // with the current info. |
| 3011 | MethodInfo MI(OverriddenMethodInfo.VBTableIndex, |
| 3012 | OverriddenMethodInfo.VFTableIndex); |
| 3013 | MethodInfoMap.erase(OverriddenMDIterator); |
| 3014 | |
| 3015 | assert(!MethodInfoMap.count(MD) && |
| 3016 | "Should not have method info for this method yet!"); |
| 3017 | MethodInfoMap.insert(std::make_pair(MD, MI)); |
| 3018 | continue; |
Reid Kleckner | 31a9f74 | 2013-12-27 20:29:16 +0000 | [diff] [blame] | 3019 | } |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3020 | |
Reid Kleckner | 31a9f74 | 2013-12-27 20:29:16 +0000 | [diff] [blame] | 3021 | // In case we need a return adjustment, we'll add a new slot for |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 3022 | // the overrider. Mark the overriden method as shadowed by the new slot. |
Reid Kleckner | 31a9f74 | 2013-12-27 20:29:16 +0000 | [diff] [blame] | 3023 | OverriddenMethodInfo.Shadowed = true; |
Reid Kleckner | 31a9f74 | 2013-12-27 20:29:16 +0000 | [diff] [blame] | 3024 | |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 3025 | // Force a special name mangling for a return-adjusting thunk |
| 3026 | // unless the method is the final overrider without this adjustment. |
Timur Iskhodzhanov | 16055e7 | 2014-08-10 11:40:51 +0000 | [diff] [blame] | 3027 | ForceReturnAdjustmentMangling = |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 3028 | !(MD == FinalOverriderMD && ThisAdjustmentOffset.isEmpty()); |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3029 | } else if (Base.getBaseOffset() != WhichVFPtr.FullOffsetInMDC || |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3030 | MD->size_overridden_methods()) { |
| 3031 | // Skip methods that don't belong to the vftable of the current class, |
| 3032 | // e.g. each method that wasn't seen in any of the visited sub-bases |
| 3033 | // but overrides multiple methods of other sub-bases. |
| 3034 | continue; |
| 3035 | } |
| 3036 | |
| 3037 | // If we got here, MD is a method not seen in any of the sub-bases or |
| 3038 | // it requires return adjustment. Insert the method info for this method. |
| 3039 | unsigned VBIndex = |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3040 | LastVBase ? VTables.getVBTableIndex(MostDerivedClass, LastVBase) : 0; |
David Majnemer | 3c7228e | 2014-07-01 21:10:07 +0000 | [diff] [blame] | 3041 | MethodInfo MI(VBIndex, |
Timur Iskhodzhanov | 16055e7 | 2014-08-10 11:40:51 +0000 | [diff] [blame] | 3042 | HasRTTIComponent ? Components.size() - 1 : Components.size(), |
| 3043 | ReturnAdjustingThunk); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3044 | |
| 3045 | assert(!MethodInfoMap.count(MD) && |
| 3046 | "Should not have method info for this method yet!"); |
| 3047 | MethodInfoMap.insert(std::make_pair(MD, MI)); |
| 3048 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3049 | // Check if this overrider needs a return adjustment. |
| 3050 | // We don't want to do this for pure virtual member functions. |
| 3051 | BaseOffset ReturnAdjustmentOffset; |
| 3052 | ReturnAdjustment ReturnAdjustment; |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 3053 | if (!FinalOverriderMD->isPure()) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3054 | ReturnAdjustmentOffset = |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 3055 | ComputeReturnAdjustmentBaseOffset(Context, FinalOverriderMD, MD); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3056 | } |
| 3057 | if (!ReturnAdjustmentOffset.isEmpty()) { |
Timur Iskhodzhanov | 16055e7 | 2014-08-10 11:40:51 +0000 | [diff] [blame] | 3058 | ForceReturnAdjustmentMangling = true; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3059 | ReturnAdjustment.NonVirtual = |
| 3060 | ReturnAdjustmentOffset.NonVirtualOffset.getQuantity(); |
| 3061 | if (ReturnAdjustmentOffset.VirtualBase) { |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 3062 | const ASTRecordLayout &DerivedLayout = |
| 3063 | Context.getASTRecordLayout(ReturnAdjustmentOffset.DerivedClass); |
| 3064 | ReturnAdjustment.Virtual.Microsoft.VBPtrOffset = |
| 3065 | DerivedLayout.getVBPtrOffset().getQuantity(); |
| 3066 | ReturnAdjustment.Virtual.Microsoft.VBIndex = |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3067 | VTables.getVBTableIndex(ReturnAdjustmentOffset.DerivedClass, |
| 3068 | ReturnAdjustmentOffset.VirtualBase); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3069 | } |
| 3070 | } |
| 3071 | |
Timur Iskhodzhanov | 7081460 | 2014-11-17 15:53:50 +0000 | [diff] [blame] | 3072 | AddMethod(FinalOverriderMD, |
Timur Iskhodzhanov | 16055e7 | 2014-08-10 11:40:51 +0000 | [diff] [blame] | 3073 | ThunkInfo(ThisAdjustmentOffset, ReturnAdjustment, |
| 3074 | ForceReturnAdjustmentMangling ? MD : nullptr)); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3075 | } |
| 3076 | } |
| 3077 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3078 | static void PrintBasePath(const VPtrInfo::BasePath &Path, raw_ostream &Out) { |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3079 | for (const CXXRecordDecl *Elem : |
| 3080 | llvm::make_range(Path.rbegin(), Path.rend())) { |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 3081 | Out << "'"; |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3082 | Elem->printQualifiedName(Out); |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 3083 | Out << "' in "; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3084 | } |
| 3085 | } |
| 3086 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 3087 | static void dumpMicrosoftThunkAdjustment(const ThunkInfo &TI, raw_ostream &Out, |
| 3088 | bool ContinueFirstLine) { |
| 3089 | const ReturnAdjustment &R = TI.Return; |
| 3090 | bool Multiline = false; |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 3091 | const char *LinePrefix = "\n "; |
| 3092 | if (!R.isEmpty() || TI.Method) { |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 3093 | if (!ContinueFirstLine) |
| 3094 | Out << LinePrefix; |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 3095 | Out << "[return adjustment (to type '" |
| 3096 | << TI.Method->getReturnType().getCanonicalType().getAsString() |
| 3097 | << "'): "; |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 3098 | if (R.Virtual.Microsoft.VBPtrOffset) |
| 3099 | Out << "vbptr at offset " << R.Virtual.Microsoft.VBPtrOffset << ", "; |
| 3100 | if (R.Virtual.Microsoft.VBIndex) |
| 3101 | Out << "vbase #" << R.Virtual.Microsoft.VBIndex << ", "; |
| 3102 | Out << R.NonVirtual << " non-virtual]"; |
| 3103 | Multiline = true; |
| 3104 | } |
| 3105 | |
| 3106 | const ThisAdjustment &T = TI.This; |
| 3107 | if (!T.isEmpty()) { |
| 3108 | if (Multiline || !ContinueFirstLine) |
| 3109 | Out << LinePrefix; |
| 3110 | Out << "[this adjustment: "; |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 3111 | if (!TI.This.Virtual.isEmpty()) { |
| 3112 | assert(T.Virtual.Microsoft.VtordispOffset < 0); |
| 3113 | Out << "vtordisp at " << T.Virtual.Microsoft.VtordispOffset << ", "; |
| 3114 | if (T.Virtual.Microsoft.VBPtrOffset) { |
| 3115 | Out << "vbptr at " << T.Virtual.Microsoft.VBPtrOffset |
Timur Iskhodzhanov | a895758 | 2014-03-07 09:34:59 +0000 | [diff] [blame] | 3116 | << " to the left,"; |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 3117 | assert(T.Virtual.Microsoft.VBOffsetOffset > 0); |
| 3118 | Out << LinePrefix << " vboffset at " |
| 3119 | << T.Virtual.Microsoft.VBOffsetOffset << " in the vbtable, "; |
| 3120 | } |
| 3121 | } |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 3122 | Out << T.NonVirtual << " non-virtual]"; |
| 3123 | } |
| 3124 | } |
| 3125 | |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3126 | void VFTableBuilder::dumpLayout(raw_ostream &Out) { |
| 3127 | Out << "VFTable for "; |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3128 | PrintBasePath(WhichVFPtr.PathToBaseWithVPtr, Out); |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 3129 | Out << "'"; |
| 3130 | MostDerivedClass->printQualifiedName(Out); |
Timur Iskhodzhanov | 77764b6 | 2014-03-05 13:54:07 +0000 | [diff] [blame] | 3131 | Out << "' (" << Components.size() |
| 3132 | << (Components.size() == 1 ? " entry" : " entries") << ").\n"; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3133 | |
| 3134 | for (unsigned I = 0, E = Components.size(); I != E; ++I) { |
| 3135 | Out << llvm::format("%4d | ", I); |
| 3136 | |
| 3137 | const VTableComponent &Component = Components[I]; |
| 3138 | |
| 3139 | // Dump the component. |
| 3140 | switch (Component.getKind()) { |
| 3141 | case VTableComponent::CK_RTTI: |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 3142 | Component.getRTTIDecl()->printQualifiedName(Out); |
| 3143 | Out << " RTTI"; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3144 | break; |
| 3145 | |
| 3146 | case VTableComponent::CK_FunctionPointer: { |
| 3147 | const CXXMethodDecl *MD = Component.getFunctionDecl(); |
| 3148 | |
Reid Kleckner | 604c8b4 | 2013-12-27 19:43:59 +0000 | [diff] [blame] | 3149 | // FIXME: Figure out how to print the real thunk type, since they can |
| 3150 | // differ in the return type. |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3151 | std::string Str = PredefinedExpr::ComputeName( |
| 3152 | PredefinedExpr::PrettyFunctionNoVirtual, MD); |
| 3153 | Out << Str; |
| 3154 | if (MD->isPure()) |
| 3155 | Out << " [pure]"; |
| 3156 | |
David Majnemer | d59becb | 2014-09-12 04:38:08 +0000 | [diff] [blame] | 3157 | if (MD->isDeleted()) |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3158 | Out << " [deleted]"; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3159 | |
| 3160 | ThunkInfo Thunk = VTableThunks.lookup(I); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 3161 | if (!Thunk.isEmpty()) |
| 3162 | dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3163 | |
| 3164 | break; |
| 3165 | } |
| 3166 | |
| 3167 | case VTableComponent::CK_DeletingDtorPointer: { |
| 3168 | const CXXDestructorDecl *DD = Component.getDestructorDecl(); |
| 3169 | |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 3170 | DD->printQualifiedName(Out); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3171 | Out << "() [scalar deleting]"; |
| 3172 | |
| 3173 | if (DD->isPure()) |
| 3174 | Out << " [pure]"; |
| 3175 | |
| 3176 | ThunkInfo Thunk = VTableThunks.lookup(I); |
| 3177 | if (!Thunk.isEmpty()) { |
| 3178 | assert(Thunk.Return.isEmpty() && |
| 3179 | "No return adjustment needed for destructors!"); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 3180 | dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | break; |
| 3184 | } |
| 3185 | |
| 3186 | default: |
| 3187 | DiagnosticsEngine &Diags = Context.getDiagnostics(); |
| 3188 | unsigned DiagID = Diags.getCustomDiagID( |
| 3189 | DiagnosticsEngine::Error, |
| 3190 | "Unexpected vftable component type %0 for component number %1"); |
| 3191 | Diags.Report(MostDerivedClass->getLocation(), DiagID) |
| 3192 | << I << Component.getKind(); |
| 3193 | } |
| 3194 | |
| 3195 | Out << '\n'; |
| 3196 | } |
| 3197 | |
| 3198 | Out << '\n'; |
| 3199 | |
| 3200 | if (!Thunks.empty()) { |
| 3201 | // We store the method names in a map to get a stable order. |
| 3202 | std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls; |
| 3203 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3204 | for (const auto &I : Thunks) { |
| 3205 | const CXXMethodDecl *MD = I.first; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3206 | std::string MethodName = PredefinedExpr::ComputeName( |
| 3207 | PredefinedExpr::PrettyFunctionNoVirtual, MD); |
| 3208 | |
| 3209 | MethodNamesAndDecls.insert(std::make_pair(MethodName, MD)); |
| 3210 | } |
| 3211 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3212 | for (const auto &MethodNameAndDecl : MethodNamesAndDecls) { |
| 3213 | const std::string &MethodName = MethodNameAndDecl.first; |
| 3214 | const CXXMethodDecl *MD = MethodNameAndDecl.second; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3215 | |
| 3216 | ThunkInfoVectorTy ThunksVector = Thunks[MD]; |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 3217 | std::stable_sort(ThunksVector.begin(), ThunksVector.end(), |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 3218 | [](const ThunkInfo &LHS, const ThunkInfo &RHS) { |
| 3219 | // Keep different thunks with the same adjustments in the order they |
| 3220 | // were put into the vector. |
Benjamin Kramer | a741b8c | 2014-03-03 20:26:46 +0000 | [diff] [blame] | 3221 | return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return); |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 3222 | }); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3223 | |
| 3224 | Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size(); |
| 3225 | Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n"; |
| 3226 | |
| 3227 | for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) { |
| 3228 | const ThunkInfo &Thunk = ThunksVector[I]; |
| 3229 | |
| 3230 | Out << llvm::format("%4d | ", I); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 3231 | dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/true); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3232 | Out << '\n'; |
| 3233 | } |
| 3234 | |
| 3235 | Out << '\n'; |
| 3236 | } |
| 3237 | } |
Timur Iskhodzhanov | 4fea4f9 | 2014-03-20 13:42:14 +0000 | [diff] [blame] | 3238 | |
| 3239 | Out.flush(); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3240 | } |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3241 | |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3242 | static bool setsIntersect(const llvm::SmallPtrSet<const CXXRecordDecl *, 4> &A, |
Craig Topper | 3cb91b2 | 2014-08-27 06:28:16 +0000 | [diff] [blame] | 3243 | ArrayRef<const CXXRecordDecl *> B) { |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3244 | for (const CXXRecordDecl *Decl : B) { |
| 3245 | if (A.count(Decl)) |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3246 | return true; |
| 3247 | } |
| 3248 | return false; |
| 3249 | } |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3250 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3251 | static bool rebucketPaths(VPtrInfoVector &Paths); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3252 | |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3253 | /// Produces MSVC-compatible vbtable data. The symbols produced by this |
| 3254 | /// algorithm match those produced by MSVC 2012 and newer, which is different |
| 3255 | /// from MSVC 2010. |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3256 | /// |
| 3257 | /// MSVC 2012 appears to minimize the vbtable names using the following |
| 3258 | /// algorithm. First, walk the class hierarchy in the usual order, depth first, |
| 3259 | /// left to right, to find all of the subobjects which contain a vbptr field. |
| 3260 | /// Visiting each class node yields a list of inheritance paths to vbptrs. Each |
| 3261 | /// record with a vbptr creates an initially empty path. |
| 3262 | /// |
| 3263 | /// To combine paths from child nodes, the paths are compared to check for |
| 3264 | /// ambiguity. Paths are "ambiguous" if multiple paths have the same set of |
| 3265 | /// components in the same order. Each group of ambiguous paths is extended by |
| 3266 | /// appending the class of the base from which it came. If the current class |
| 3267 | /// node produced an ambiguous path, its path is extended with the current class. |
| 3268 | /// After extending paths, MSVC again checks for ambiguity, and extends any |
| 3269 | /// ambiguous path which wasn't already extended. Because each node yields an |
| 3270 | /// unambiguous set of paths, MSVC doesn't need to extend any path more than once |
| 3271 | /// to produce an unambiguous set of paths. |
| 3272 | /// |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3273 | /// TODO: Presumably vftables use the same algorithm. |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3274 | void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables, |
| 3275 | const CXXRecordDecl *RD, |
| 3276 | VPtrInfoVector &Paths) { |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3277 | assert(Paths.empty()); |
| 3278 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3279 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3280 | // Base case: this subobject has its own vptr. |
| 3281 | if (ForVBTables ? Layout.hasOwnVBPtr() : Layout.hasOwnVFPtr()) |
| 3282 | Paths.push_back(new VPtrInfo(RD)); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3283 | |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3284 | // Recursive case: get all the vbtables from our bases and remove anything |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3285 | // that shares a virtual base. |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3286 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> VBasesSeen; |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3287 | for (const auto &B : RD->bases()) { |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 3288 | const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl(); |
| 3289 | if (B.isVirtual() && VBasesSeen.count(Base)) |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3290 | continue; |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3291 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3292 | if (!Base->isDynamicClass()) |
| 3293 | continue; |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3294 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3295 | const VPtrInfoVector &BasePaths = |
| 3296 | ForVBTables ? enumerateVBTables(Base) : getVFPtrOffsets(Base); |
| 3297 | |
Reid Kleckner | fd38540 | 2014-04-17 22:47:52 +0000 | [diff] [blame] | 3298 | for (VPtrInfo *BaseInfo : BasePaths) { |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3299 | // Don't include the path if it goes through a virtual base that we've |
| 3300 | // already included. |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3301 | if (setsIntersect(VBasesSeen, BaseInfo->ContainingVBases)) |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3302 | continue; |
| 3303 | |
| 3304 | // Copy the path and adjust it as necessary. |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3305 | VPtrInfo *P = new VPtrInfo(*BaseInfo); |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3306 | |
| 3307 | // We mangle Base into the path if the path would've been ambiguous and it |
| 3308 | // wasn't already extended with Base. |
| 3309 | if (P->MangledPath.empty() || P->MangledPath.back() != Base) |
| 3310 | P->NextBaseToMangle = Base; |
| 3311 | |
Reid Kleckner | fd38540 | 2014-04-17 22:47:52 +0000 | [diff] [blame] | 3312 | // Keep track of which vtable the derived class is going to extend with |
| 3313 | // new methods or bases. We append to either the vftable of our primary |
| 3314 | // base, or the first non-virtual base that has a vbtable. |
| 3315 | if (P->ReusingBase == Base && |
| 3316 | Base == (ForVBTables ? Layout.getBaseSharingVBPtr() |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3317 | : Layout.getPrimaryBase())) |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3318 | P->ReusingBase = RD; |
Reid Kleckner | fd38540 | 2014-04-17 22:47:52 +0000 | [diff] [blame] | 3319 | |
| 3320 | // Keep track of the full adjustment from the MDC to this vtable. The |
| 3321 | // adjustment is captured by an optional vbase and a non-virtual offset. |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 3322 | if (B.isVirtual()) |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3323 | P->ContainingVBases.push_back(Base); |
| 3324 | else if (P->ContainingVBases.empty()) |
| 3325 | P->NonVirtualOffset += Layout.getBaseClassOffset(Base); |
| 3326 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3327 | // Update the full offset in the MDC. |
| 3328 | P->FullOffsetInMDC = P->NonVirtualOffset; |
| 3329 | if (const CXXRecordDecl *VB = P->getVBaseWithVPtr()) |
| 3330 | P->FullOffsetInMDC += Layout.getVBaseClassOffset(VB); |
| 3331 | |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3332 | Paths.push_back(P); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
Timur Iskhodzhanov | 9ae7d3b | 2014-03-31 11:01:51 +0000 | [diff] [blame] | 3335 | if (B.isVirtual()) |
| 3336 | VBasesSeen.insert(Base); |
| 3337 | |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3338 | // After visiting any direct base, we've transitively visited all of its |
| 3339 | // morally virtual bases. |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 3340 | for (const auto &VB : Base->vbases()) |
| 3341 | VBasesSeen.insert(VB.getType()->getAsCXXRecordDecl()); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3344 | // Sort the paths into buckets, and if any of them are ambiguous, extend all |
| 3345 | // paths in ambiguous buckets. |
| 3346 | bool Changed = true; |
| 3347 | while (Changed) |
| 3348 | Changed = rebucketPaths(Paths); |
| 3349 | } |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3350 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3351 | static bool extendPath(VPtrInfo *P) { |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3352 | if (P->NextBaseToMangle) { |
| 3353 | P->MangledPath.push_back(P->NextBaseToMangle); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3354 | P->NextBaseToMangle = nullptr;// Prevent the path from being extended twice. |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3355 | return true; |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3356 | } |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3357 | return false; |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3360 | static bool rebucketPaths(VPtrInfoVector &Paths) { |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3361 | // What we're essentially doing here is bucketing together ambiguous paths. |
| 3362 | // Any bucket with more than one path in it gets extended by NextBase, which |
| 3363 | // is usually the direct base of the inherited the vbptr. This code uses a |
| 3364 | // sorted vector to implement a multiset to form the buckets. Note that the |
| 3365 | // ordering is based on pointers, but it doesn't change our output order. The |
| 3366 | // current algorithm is designed to match MSVC 2012's names. |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3367 | VPtrInfoVector PathsSorted(Paths); |
Benjamin Kramer | 15ae783 | 2014-03-07 21:35:40 +0000 | [diff] [blame] | 3368 | std::sort(PathsSorted.begin(), PathsSorted.end(), |
| 3369 | [](const VPtrInfo *LHS, const VPtrInfo *RHS) { |
| 3370 | return LHS->MangledPath < RHS->MangledPath; |
| 3371 | }); |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3372 | bool Changed = false; |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3373 | for (size_t I = 0, E = PathsSorted.size(); I != E;) { |
| 3374 | // Scan forward to find the end of the bucket. |
| 3375 | size_t BucketStart = I; |
| 3376 | do { |
| 3377 | ++I; |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3378 | } while (I != E && PathsSorted[BucketStart]->MangledPath == |
| 3379 | PathsSorted[I]->MangledPath); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3380 | |
| 3381 | // If this bucket has multiple paths, extend them all. |
| 3382 | if (I - BucketStart > 1) { |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3383 | for (size_t II = BucketStart; II != I; ++II) |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3384 | Changed |= extendPath(PathsSorted[II]); |
| 3385 | assert(Changed && "no paths were extended to fix ambiguity"); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3386 | } |
| 3387 | } |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3388 | return Changed; |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | MicrosoftVTableContext::~MicrosoftVTableContext() { |
Nico Weber | d19e6a7 | 2014-04-24 19:52:12 +0000 | [diff] [blame] | 3392 | for (auto &P : VFPtrLocations) |
| 3393 | llvm::DeleteContainerPointers(*P.second); |
Reid Kleckner | 3331128 | 2014-02-28 23:26:22 +0000 | [diff] [blame] | 3394 | llvm::DeleteContainerSeconds(VFPtrLocations); |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3395 | llvm::DeleteContainerSeconds(VFTableLayouts); |
| 3396 | llvm::DeleteContainerSeconds(VBaseInfo); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3399 | namespace { |
| 3400 | typedef llvm::SetVector<BaseSubobject, std::vector<BaseSubobject>, |
| 3401 | llvm::DenseSet<BaseSubobject>> FullPathTy; |
| 3402 | } |
| 3403 | |
| 3404 | // This recursive function finds all paths from a subobject centered at |
| 3405 | // (RD, Offset) to the subobject located at BaseWithVPtr. |
| 3406 | static void findPathsToSubobject(ASTContext &Context, |
| 3407 | const ASTRecordLayout &MostDerivedLayout, |
| 3408 | const CXXRecordDecl *RD, CharUnits Offset, |
| 3409 | BaseSubobject BaseWithVPtr, |
| 3410 | FullPathTy &FullPath, |
| 3411 | std::list<FullPathTy> &Paths) { |
| 3412 | if (BaseSubobject(RD, Offset) == BaseWithVPtr) { |
| 3413 | Paths.push_back(FullPath); |
| 3414 | return; |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 3418 | |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3419 | for (const CXXBaseSpecifier &BS : RD->bases()) { |
David Majnemer | ead9757 | 2015-05-01 21:35:41 +0000 | [diff] [blame] | 3420 | const CXXRecordDecl *Base = BS.getType()->getAsCXXRecordDecl(); |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3421 | CharUnits NewOffset = BS.isVirtual() |
| 3422 | ? MostDerivedLayout.getVBaseClassOffset(Base) |
| 3423 | : Offset + Layout.getBaseClassOffset(Base); |
| 3424 | FullPath.insert(BaseSubobject(Base, NewOffset)); |
| 3425 | findPathsToSubobject(Context, MostDerivedLayout, Base, NewOffset, |
| 3426 | BaseWithVPtr, FullPath, Paths); |
| 3427 | FullPath.pop_back(); |
| 3428 | } |
| 3429 | } |
David Majnemer | d950f15 | 2015-04-30 17:15:48 +0000 | [diff] [blame] | 3430 | |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3431 | // Return the paths which are not subsets of other paths. |
| 3432 | static void removeRedundantPaths(std::list<FullPathTy> &FullPaths) { |
| 3433 | FullPaths.remove_if([&](const FullPathTy &SpecificPath) { |
| 3434 | for (const FullPathTy &OtherPath : FullPaths) { |
| 3435 | if (&SpecificPath == &OtherPath) |
David Majnemer | 70e6a00 | 2015-05-01 21:35:45 +0000 | [diff] [blame] | 3436 | continue; |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3437 | if (std::all_of(SpecificPath.begin(), SpecificPath.end(), |
| 3438 | [&](const BaseSubobject &BSO) { |
| 3439 | return OtherPath.count(BSO) != 0; |
| 3440 | })) { |
| 3441 | return true; |
David Majnemer | 70e6a00 | 2015-05-01 21:35:45 +0000 | [diff] [blame] | 3442 | } |
David Majnemer | d950f15 | 2015-04-30 17:15:48 +0000 | [diff] [blame] | 3443 | } |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3444 | return false; |
| 3445 | }); |
| 3446 | } |
| 3447 | |
| 3448 | static CharUnits getOffsetOfFullPath(ASTContext &Context, |
| 3449 | const CXXRecordDecl *RD, |
| 3450 | const FullPathTy &FullPath) { |
| 3451 | const ASTRecordLayout &MostDerivedLayout = |
| 3452 | Context.getASTRecordLayout(RD); |
| 3453 | CharUnits Offset = CharUnits::fromQuantity(-1); |
| 3454 | for (const BaseSubobject &BSO : FullPath) { |
| 3455 | const CXXRecordDecl *Base = BSO.getBase(); |
| 3456 | // The first entry in the path is always the most derived record, skip it. |
| 3457 | if (Base == RD) { |
| 3458 | assert(Offset.getQuantity() == -1); |
| 3459 | Offset = CharUnits::Zero(); |
| 3460 | continue; |
| 3461 | } |
| 3462 | assert(Offset.getQuantity() != -1); |
| 3463 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 3464 | // While we know which base has to be traversed, we don't know if that base |
| 3465 | // was a virtual base. |
| 3466 | const CXXBaseSpecifier *BaseBS = std::find_if( |
| 3467 | RD->bases_begin(), RD->bases_end(), [&](const CXXBaseSpecifier &BS) { |
| 3468 | return BS.getType()->getAsCXXRecordDecl() == Base; |
| 3469 | }); |
| 3470 | Offset = BaseBS->isVirtual() ? MostDerivedLayout.getVBaseClassOffset(Base) |
| 3471 | : Offset + Layout.getBaseClassOffset(Base); |
| 3472 | RD = Base; |
David Majnemer | d950f15 | 2015-04-30 17:15:48 +0000 | [diff] [blame] | 3473 | } |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3474 | return Offset; |
| 3475 | } |
David Majnemer | ead9757 | 2015-05-01 21:35:41 +0000 | [diff] [blame] | 3476 | |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3477 | // We want to select the path which introduces the most covariant overrides. If |
| 3478 | // two paths introduce overrides which the other path doesn't contain, issue a |
| 3479 | // diagnostic. |
| 3480 | static const FullPathTy *selectBestPath(ASTContext &Context, |
| 3481 | const CXXRecordDecl *RD, VPtrInfo *Info, |
| 3482 | std::list<FullPathTy> &FullPaths) { |
David Majnemer | e48630f | 2015-05-05 01:39:20 +0000 | [diff] [blame] | 3483 | // Handle some easy cases first. |
| 3484 | if (FullPaths.empty()) |
| 3485 | return nullptr; |
| 3486 | if (FullPaths.size() == 1) |
| 3487 | return &FullPaths.front(); |
| 3488 | |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3489 | const FullPathTy *BestPath = nullptr; |
| 3490 | typedef std::set<const CXXMethodDecl *> OverriderSetTy; |
| 3491 | OverriderSetTy LastOverrides; |
| 3492 | for (const FullPathTy &SpecificPath : FullPaths) { |
David Majnemer | e48630f | 2015-05-05 01:39:20 +0000 | [diff] [blame] | 3493 | assert(!SpecificPath.empty()); |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3494 | OverriderSetTy CurrentOverrides; |
| 3495 | const CXXRecordDecl *TopLevelRD = SpecificPath.begin()->getBase(); |
| 3496 | // Find the distance from the start of the path to the subobject with the |
| 3497 | // VPtr. |
| 3498 | CharUnits BaseOffset = |
| 3499 | getOffsetOfFullPath(Context, TopLevelRD, SpecificPath); |
| 3500 | FinalOverriders Overriders(TopLevelRD, CharUnits::Zero(), TopLevelRD); |
| 3501 | for (const CXXMethodDecl *MD : Info->BaseWithVPtr->methods()) { |
| 3502 | if (!MD->isVirtual()) |
| 3503 | continue; |
| 3504 | FinalOverriders::OverriderInfo OI = |
| 3505 | Overriders.getOverrider(MD->getCanonicalDecl(), BaseOffset); |
David Majnemer | e48630f | 2015-05-05 01:39:20 +0000 | [diff] [blame] | 3506 | const CXXMethodDecl *OverridingMethod = OI.Method; |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3507 | // Only overriders which have a return adjustment introduce problematic |
| 3508 | // thunks. |
David Majnemer | e48630f | 2015-05-05 01:39:20 +0000 | [diff] [blame] | 3509 | if (ComputeReturnAdjustmentBaseOffset(Context, OverridingMethod, MD) |
| 3510 | .isEmpty()) |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3511 | continue; |
| 3512 | // It's possible that the overrider isn't in this path. If so, skip it |
| 3513 | // because this path didn't introduce it. |
David Majnemer | e48630f | 2015-05-05 01:39:20 +0000 | [diff] [blame] | 3514 | const CXXRecordDecl *OverridingParent = OverridingMethod->getParent(); |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3515 | if (std::none_of(SpecificPath.begin(), SpecificPath.end(), |
| 3516 | [&](const BaseSubobject &BSO) { |
| 3517 | return BSO.getBase() == OverridingParent; |
| 3518 | })) |
| 3519 | continue; |
David Majnemer | e48630f | 2015-05-05 01:39:20 +0000 | [diff] [blame] | 3520 | CurrentOverrides.insert(OverridingMethod); |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3521 | } |
| 3522 | OverriderSetTy NewOverrides = |
| 3523 | llvm::set_difference(CurrentOverrides, LastOverrides); |
| 3524 | if (NewOverrides.empty()) |
| 3525 | continue; |
| 3526 | OverriderSetTy MissingOverrides = |
| 3527 | llvm::set_difference(LastOverrides, CurrentOverrides); |
| 3528 | if (MissingOverrides.empty()) { |
| 3529 | // This path is a strict improvement over the last path, let's use it. |
| 3530 | BestPath = &SpecificPath; |
| 3531 | std::swap(CurrentOverrides, LastOverrides); |
| 3532 | } else { |
| 3533 | // This path introduces an overrider with a conflicting covariant thunk. |
| 3534 | DiagnosticsEngine &Diags = Context.getDiagnostics(); |
| 3535 | const CXXMethodDecl *CovariantMD = *NewOverrides.begin(); |
| 3536 | const CXXMethodDecl *ConflictMD = *MissingOverrides.begin(); |
| 3537 | Diags.Report(RD->getLocation(), diag::err_vftable_ambiguous_component) |
| 3538 | << RD; |
| 3539 | Diags.Report(CovariantMD->getLocation(), diag::note_covariant_thunk) |
| 3540 | << CovariantMD; |
| 3541 | Diags.Report(ConflictMD->getLocation(), diag::note_covariant_thunk) |
| 3542 | << ConflictMD; |
| 3543 | } |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3544 | } |
David Majnemer | e48630f | 2015-05-05 01:39:20 +0000 | [diff] [blame] | 3545 | // Go with the path that introduced the most covariant overrides. If there is |
| 3546 | // no such path, pick the first path. |
| 3547 | return BestPath ? BestPath : &FullPaths.front(); |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | static void computeFullPathsForVFTables(ASTContext &Context, |
| 3551 | const CXXRecordDecl *RD, |
| 3552 | VPtrInfoVector &Paths) { |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3553 | const ASTRecordLayout &MostDerivedLayout = Context.getASTRecordLayout(RD); |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3554 | FullPathTy FullPath; |
| 3555 | std::list<FullPathTy> FullPaths; |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3556 | for (VPtrInfo *Info : Paths) { |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3557 | findPathsToSubobject( |
| 3558 | Context, MostDerivedLayout, RD, CharUnits::Zero(), |
| 3559 | BaseSubobject(Info->BaseWithVPtr, Info->FullOffsetInMDC), FullPath, |
| 3560 | FullPaths); |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3561 | FullPath.clear(); |
David Majnemer | ab13092 | 2015-05-04 18:47:54 +0000 | [diff] [blame] | 3562 | removeRedundantPaths(FullPaths); |
| 3563 | Info->PathToBaseWithVPtr.clear(); |
| 3564 | if (const FullPathTy *BestPath = |
| 3565 | selectBestPath(Context, RD, Info, FullPaths)) |
| 3566 | for (const BaseSubobject &BSO : *BestPath) |
| 3567 | Info->PathToBaseWithVPtr.push_back(BSO.getBase()); |
| 3568 | FullPaths.clear(); |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3569 | } |
| 3570 | } |
| 3571 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3572 | void MicrosoftVTableContext::computeVTableRelatedInformation( |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3573 | const CXXRecordDecl *RD) { |
| 3574 | assert(RD->isDynamicClass()); |
| 3575 | |
| 3576 | // Check if we've computed this information before. |
| 3577 | if (VFPtrLocations.count(RD)) |
| 3578 | return; |
| 3579 | |
| 3580 | const VTableLayout::AddressPointsMapTy EmptyAddressPointsMap; |
| 3581 | |
Reid Kleckner | d6f9b83 | 2014-02-27 22:51:43 +0000 | [diff] [blame] | 3582 | VPtrInfoVector *VFPtrs = new VPtrInfoVector(); |
| 3583 | computeVTablePaths(/*ForVBTables=*/false, RD, *VFPtrs); |
Reid Kleckner | 15fdcf1 | 2014-09-22 23:14:46 +0000 | [diff] [blame] | 3584 | computeFullPathsForVFTables(Context, RD, *VFPtrs); |
Reid Kleckner | d6f9b83 | 2014-02-27 22:51:43 +0000 | [diff] [blame] | 3585 | VFPtrLocations[RD] = VFPtrs; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3586 | |
| 3587 | MethodVFTableLocationsTy NewMethodLocations; |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3588 | for (const VPtrInfo *VFPtr : *VFPtrs) { |
| 3589 | VFTableBuilder Builder(*this, RD, VFPtr); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3590 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3591 | VFTableIdTy id(RD, VFPtr->FullOffsetInMDC); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3592 | assert(VFTableLayouts.count(id) == 0); |
| 3593 | SmallVector<VTableLayout::VTableThunkTy, 1> VTableThunks( |
| 3594 | Builder.vtable_thunks_begin(), Builder.vtable_thunks_end()); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3595 | VFTableLayouts[id] = new VTableLayout( |
| 3596 | Builder.getNumVTableComponents(), Builder.vtable_component_begin(), |
| 3597 | VTableThunks.size(), VTableThunks.data(), EmptyAddressPointsMap, true); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3598 | Thunks.insert(Builder.thunks_begin(), Builder.thunks_end()); |
Timur Iskhodzhanov | ba55702 | 2014-03-20 20:38:34 +0000 | [diff] [blame] | 3599 | |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 3600 | for (const auto &Loc : Builder.vtable_locations()) { |
| 3601 | GlobalDecl GD = Loc.first; |
| 3602 | MethodVFTableLocation NewLoc = Loc.second; |
Timur Iskhodzhanov | ba55702 | 2014-03-20 20:38:34 +0000 | [diff] [blame] | 3603 | auto M = NewMethodLocations.find(GD); |
| 3604 | if (M == NewMethodLocations.end() || NewLoc < M->second) |
| 3605 | NewMethodLocations[GD] = NewLoc; |
| 3606 | } |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | MethodVFTableLocations.insert(NewMethodLocations.begin(), |
| 3610 | NewMethodLocations.end()); |
| 3611 | if (Context.getLangOpts().DumpVTableLayouts) |
Reid Kleckner | 5bc6d0f | 2013-11-08 21:28:00 +0000 | [diff] [blame] | 3612 | dumpMethodLocations(RD, NewMethodLocations, llvm::outs()); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3615 | void MicrosoftVTableContext::dumpMethodLocations( |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3616 | const CXXRecordDecl *RD, const MethodVFTableLocationsTy &NewMethods, |
| 3617 | raw_ostream &Out) { |
| 3618 | // Compute the vtable indices for all the member functions. |
| 3619 | // Store them in a map keyed by the location so we'll get a sorted table. |
| 3620 | std::map<MethodVFTableLocation, std::string> IndicesMap; |
| 3621 | bool HasNonzeroOffset = false; |
| 3622 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3623 | for (const auto &I : NewMethods) { |
| 3624 | const CXXMethodDecl *MD = cast<const CXXMethodDecl>(I.first.getDecl()); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3625 | assert(MD->isVirtual()); |
| 3626 | |
| 3627 | std::string MethodName = PredefinedExpr::ComputeName( |
| 3628 | PredefinedExpr::PrettyFunctionNoVirtual, MD); |
| 3629 | |
| 3630 | if (isa<CXXDestructorDecl>(MD)) { |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3631 | IndicesMap[I.second] = MethodName + " [scalar deleting]"; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3632 | } else { |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3633 | IndicesMap[I.second] = MethodName; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3636 | if (!I.second.VFPtrOffset.isZero() || I.second.VBTableIndex != 0) |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3637 | HasNonzeroOffset = true; |
| 3638 | } |
| 3639 | |
| 3640 | // Print the vtable indices for all the member functions. |
| 3641 | if (!IndicesMap.empty()) { |
| 3642 | Out << "VFTable indices for "; |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 3643 | Out << "'"; |
| 3644 | RD->printQualifiedName(Out); |
Timur Iskhodzhanov | 77764b6 | 2014-03-05 13:54:07 +0000 | [diff] [blame] | 3645 | Out << "' (" << IndicesMap.size() |
| 3646 | << (IndicesMap.size() == 1 ? " entry" : " entries") << ").\n"; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3647 | |
| 3648 | CharUnits LastVFPtrOffset = CharUnits::fromQuantity(-1); |
| 3649 | uint64_t LastVBIndex = 0; |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3650 | for (const auto &I : IndicesMap) { |
| 3651 | CharUnits VFPtrOffset = I.first.VFPtrOffset; |
| 3652 | uint64_t VBIndex = I.first.VBTableIndex; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3653 | if (HasNonzeroOffset && |
| 3654 | (VFPtrOffset != LastVFPtrOffset || VBIndex != LastVBIndex)) { |
| 3655 | assert(VBIndex > LastVBIndex || VFPtrOffset > LastVFPtrOffset); |
| 3656 | Out << " -- accessible via "; |
| 3657 | if (VBIndex) |
| 3658 | Out << "vbtable index " << VBIndex << ", "; |
| 3659 | Out << "vfptr at offset " << VFPtrOffset.getQuantity() << " --\n"; |
| 3660 | LastVFPtrOffset = VFPtrOffset; |
| 3661 | LastVBIndex = VBIndex; |
| 3662 | } |
| 3663 | |
Benjamin Kramer | a37e765 | 2015-07-25 17:10:49 +0000 | [diff] [blame] | 3664 | uint64_t VTableIndex = I.first.Index; |
| 3665 | const std::string &MethodName = I.second; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3666 | Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName << '\n'; |
| 3667 | } |
| 3668 | Out << '\n'; |
| 3669 | } |
Timur Iskhodzhanov | 4fea4f9 | 2014-03-20 13:42:14 +0000 | [diff] [blame] | 3670 | |
| 3671 | Out.flush(); |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3674 | const VirtualBaseInfo *MicrosoftVTableContext::computeVBTableRelatedInformation( |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3675 | const CXXRecordDecl *RD) { |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3676 | VirtualBaseInfo *VBI; |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3677 | |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3678 | { |
| 3679 | // Get or create a VBI for RD. Don't hold a reference to the DenseMap cell, |
| 3680 | // as it may be modified and rehashed under us. |
| 3681 | VirtualBaseInfo *&Entry = VBaseInfo[RD]; |
| 3682 | if (Entry) |
| 3683 | return Entry; |
| 3684 | Entry = VBI = new VirtualBaseInfo(); |
| 3685 | } |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3686 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3687 | computeVTablePaths(/*ForVBTables=*/true, RD, VBI->VBPtrPaths); |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3688 | |
Timur Iskhodzhanov | 2c9341f | 2013-11-08 11:45:35 +0000 | [diff] [blame] | 3689 | // First, see if the Derived class shared the vbptr with a non-virtual base. |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3690 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Timur Iskhodzhanov | 2c9341f | 2013-11-08 11:45:35 +0000 | [diff] [blame] | 3691 | if (const CXXRecordDecl *VBPtrBase = Layout.getBaseSharingVBPtr()) { |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3692 | // If the Derived class shares the vbptr with a non-virtual base, the shared |
| 3693 | // virtual bases come first so that the layout is the same. |
| 3694 | const VirtualBaseInfo *BaseInfo = |
| 3695 | computeVBTableRelatedInformation(VBPtrBase); |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3696 | VBI->VBTableIndices.insert(BaseInfo->VBTableIndices.begin(), |
| 3697 | BaseInfo->VBTableIndices.end()); |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | // New vbases are added to the end of the vbtable. |
| 3701 | // Skip the self entry and vbases visited in the non-virtual base, if any. |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3702 | unsigned VBTableIndex = 1 + VBI->VBTableIndices.size(); |
Timur Iskhodzhanov | 1523c61 | 2014-03-26 08:22:48 +0000 | [diff] [blame] | 3703 | for (const auto &VB : RD->vbases()) { |
| 3704 | const CXXRecordDecl *CurVBase = VB.getType()->getAsCXXRecordDecl(); |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3705 | if (!VBI->VBTableIndices.count(CurVBase)) |
| 3706 | VBI->VBTableIndices[CurVBase] = VBTableIndex++; |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3707 | } |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3708 | |
Reid Kleckner | 5f08094 | 2014-01-03 23:42:00 +0000 | [diff] [blame] | 3709 | return VBI; |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | unsigned MicrosoftVTableContext::getVBTableIndex(const CXXRecordDecl *Derived, |
| 3713 | const CXXRecordDecl *VBase) { |
| 3714 | const VirtualBaseInfo *VBInfo = computeVBTableRelatedInformation(Derived); |
| 3715 | assert(VBInfo->VBTableIndices.count(VBase)); |
| 3716 | return VBInfo->VBTableIndices.find(VBase)->second; |
| 3717 | } |
| 3718 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3719 | const VPtrInfoVector & |
Reid Kleckner | b40a27d | 2014-01-03 00:14:35 +0000 | [diff] [blame] | 3720 | MicrosoftVTableContext::enumerateVBTables(const CXXRecordDecl *RD) { |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3721 | return computeVBTableRelatedInformation(RD)->VBPtrPaths; |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Reid Kleckner | 9c6e9e3 | 2014-02-27 19:40:09 +0000 | [diff] [blame] | 3724 | const VPtrInfoVector & |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3725 | MicrosoftVTableContext::getVFPtrOffsets(const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3726 | computeVTableRelatedInformation(RD); |
| 3727 | |
| 3728 | assert(VFPtrLocations.count(RD) && "Couldn't find vfptr locations"); |
Reid Kleckner | d6f9b83 | 2014-02-27 22:51:43 +0000 | [diff] [blame] | 3729 | return *VFPtrLocations[RD]; |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | const VTableLayout & |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3733 | MicrosoftVTableContext::getVFTableLayout(const CXXRecordDecl *RD, |
| 3734 | CharUnits VFPtrOffset) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3735 | computeVTableRelatedInformation(RD); |
| 3736 | |
| 3737 | VFTableIdTy id(RD, VFPtrOffset); |
| 3738 | assert(VFTableLayouts.count(id) && "Couldn't find a VFTable at this offset"); |
| 3739 | return *VFTableLayouts[id]; |
| 3740 | } |
| 3741 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 3742 | const MicrosoftVTableContext::MethodVFTableLocation & |
| 3743 | MicrosoftVTableContext::getMethodVFTableLocation(GlobalDecl GD) { |
Timur Iskhodzhanov | df7e7fb | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 3744 | assert(cast<CXXMethodDecl>(GD.getDecl())->isVirtual() && |
| 3745 | "Only use this method for virtual methods or dtors"); |
| 3746 | if (isa<CXXDestructorDecl>(GD.getDecl())) |
| 3747 | assert(GD.getDtorType() == Dtor_Deleting); |
| 3748 | |
| 3749 | MethodVFTableLocationsTy::iterator I = MethodVFTableLocations.find(GD); |
| 3750 | if (I != MethodVFTableLocations.end()) |
| 3751 | return I->second; |
| 3752 | |
| 3753 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
| 3754 | |
| 3755 | computeVTableRelatedInformation(RD); |
| 3756 | |
| 3757 | I = MethodVFTableLocations.find(GD); |
| 3758 | assert(I != MethodVFTableLocations.end() && "Did not find index!"); |
| 3759 | return I->second; |
| 3760 | } |