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