Peter Collingbourne | 2401846 | 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 | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Peter Collingbourne | 2401846 | 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" |
| 19 | #include "llvm/Support/Format.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <cstdio> |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | #define DUMP_OVERRIDERS 0 |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | /// BaseOffset - Represents an offset from a derived class to a direct or |
| 31 | /// indirect base class. |
| 32 | struct BaseOffset { |
| 33 | /// DerivedClass - The derived class. |
| 34 | const CXXRecordDecl *DerivedClass; |
| 35 | |
| 36 | /// VirtualBase - If the path from the derived class to the base class |
| 37 | /// involves a virtual base class, this holds its declaration. |
| 38 | const CXXRecordDecl *VirtualBase; |
| 39 | |
| 40 | /// NonVirtualOffset - The offset from the derived class to the base class. |
| 41 | /// (Or the offset from the virtual base class to the base class, if the |
| 42 | /// path from the derived class to the base class involves a virtual base |
| 43 | /// class. |
| 44 | CharUnits NonVirtualOffset; |
| 45 | |
| 46 | BaseOffset() : DerivedClass(0), VirtualBase(0), |
| 47 | NonVirtualOffset(CharUnits::Zero()) { } |
| 48 | BaseOffset(const CXXRecordDecl *DerivedClass, |
| 49 | const CXXRecordDecl *VirtualBase, CharUnits NonVirtualOffset) |
| 50 | : DerivedClass(DerivedClass), VirtualBase(VirtualBase), |
| 51 | NonVirtualOffset(NonVirtualOffset) { } |
| 52 | |
| 53 | bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; } |
| 54 | }; |
| 55 | |
| 56 | /// FinalOverriders - Contains the final overrider member functions for all |
| 57 | /// member functions in the base subobjects of a class. |
| 58 | class FinalOverriders { |
| 59 | public: |
| 60 | /// OverriderInfo - Information about a final overrider. |
| 61 | struct OverriderInfo { |
| 62 | /// Method - The method decl of the overrider. |
| 63 | const CXXMethodDecl *Method; |
| 64 | |
| 65 | /// Offset - the base offset of the overrider in the layout class. |
| 66 | CharUnits Offset; |
| 67 | |
| 68 | OverriderInfo() : Method(0), Offset(CharUnits::Zero()) { } |
| 69 | }; |
| 70 | |
| 71 | private: |
| 72 | /// MostDerivedClass - The most derived class for which the final overriders |
| 73 | /// are stored. |
| 74 | const CXXRecordDecl *MostDerivedClass; |
| 75 | |
| 76 | /// MostDerivedClassOffset - If we're building final overriders for a |
| 77 | /// construction vtable, this holds the offset from the layout class to the |
| 78 | /// most derived class. |
| 79 | const CharUnits MostDerivedClassOffset; |
| 80 | |
| 81 | /// LayoutClass - The class we're using for layout information. Will be |
| 82 | /// different than the most derived class if the final overriders are for a |
| 83 | /// construction vtable. |
| 84 | const CXXRecordDecl *LayoutClass; |
| 85 | |
| 86 | ASTContext &Context; |
| 87 | |
| 88 | /// MostDerivedClassLayout - the AST record layout of the most derived class. |
| 89 | const ASTRecordLayout &MostDerivedClassLayout; |
| 90 | |
| 91 | /// MethodBaseOffsetPairTy - Uniquely identifies a member function |
| 92 | /// in a base subobject. |
| 93 | typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy; |
| 94 | |
| 95 | typedef llvm::DenseMap<MethodBaseOffsetPairTy, |
| 96 | OverriderInfo> OverridersMapTy; |
| 97 | |
| 98 | /// OverridersMap - The final overriders for all virtual member functions of |
| 99 | /// all the base subobjects of the most derived class. |
| 100 | OverridersMapTy OverridersMap; |
| 101 | |
| 102 | /// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented |
| 103 | /// as a record decl and a subobject number) and its offsets in the most |
| 104 | /// derived class as well as the layout class. |
| 105 | typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>, |
| 106 | CharUnits> SubobjectOffsetMapTy; |
| 107 | |
| 108 | typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy; |
| 109 | |
| 110 | /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the |
| 111 | /// given base. |
| 112 | void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, |
| 113 | CharUnits OffsetInLayoutClass, |
| 114 | SubobjectOffsetMapTy &SubobjectOffsets, |
| 115 | SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, |
| 116 | SubobjectCountMapTy &SubobjectCounts); |
| 117 | |
| 118 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; |
| 119 | |
| 120 | /// dump - dump the final overriders for a base subobject, and all its direct |
| 121 | /// and indirect base subobjects. |
| 122 | void dump(raw_ostream &Out, BaseSubobject Base, |
| 123 | VisitedVirtualBasesSetTy& VisitedVirtualBases); |
| 124 | |
| 125 | public: |
| 126 | FinalOverriders(const CXXRecordDecl *MostDerivedClass, |
| 127 | CharUnits MostDerivedClassOffset, |
| 128 | const CXXRecordDecl *LayoutClass); |
| 129 | |
| 130 | /// getOverrider - Get the final overrider for the given method declaration in |
| 131 | /// the subobject with the given base offset. |
| 132 | OverriderInfo getOverrider(const CXXMethodDecl *MD, |
| 133 | CharUnits BaseOffset) const { |
| 134 | assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) && |
| 135 | "Did not find overrider!"); |
| 136 | |
| 137 | return OverridersMap.lookup(std::make_pair(MD, BaseOffset)); |
| 138 | } |
| 139 | |
| 140 | /// dump - dump the final overriders. |
| 141 | void dump() { |
| 142 | VisitedVirtualBasesSetTy VisitedVirtualBases; |
| 143 | dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()), |
| 144 | VisitedVirtualBases); |
| 145 | } |
| 146 | |
| 147 | }; |
| 148 | |
| 149 | #define DUMP_OVERRIDERS 0 |
| 150 | |
| 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 | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 169 | // Get the final overriders. |
Peter Collingbourne | 2401846 | 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. |
| 222 | for (unsigned I = 0, E = Path.size(); I != E; ++I) { |
| 223 | const CXXBasePathElement &Element = Path[I]; |
| 224 | |
| 225 | if (Element.Base->isVirtual()) { |
| 226 | // FIXME: Can we break when we find the first virtual base? |
| 227 | // (If we can't, can't we just iterate over the path in reverse order?) |
| 228 | NonVirtualStart = I + 1; |
| 229 | QualType VBaseType = Element.Base->getType(); |
| 230 | VirtualBase = |
| 231 | cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Now compute the non-virtual offset. |
| 236 | for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) { |
| 237 | const CXXBasePathElement &Element = Path[I]; |
| 238 | |
| 239 | // Check the base class offset. |
| 240 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); |
| 241 | |
| 242 | const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>(); |
| 243 | const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 244 | |
| 245 | NonVirtualOffset += Layout.getBaseClassOffset(Base); |
| 246 | } |
| 247 | |
| 248 | // FIXME: This should probably use CharUnits or something. Maybe we should |
| 249 | // even change the base offsets in ASTRecordLayout to be specified in |
| 250 | // CharUnits. |
| 251 | return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset); |
| 252 | |
| 253 | } |
| 254 | |
| 255 | static BaseOffset ComputeBaseOffset(ASTContext &Context, |
| 256 | const CXXRecordDecl *BaseRD, |
| 257 | const CXXRecordDecl *DerivedRD) { |
| 258 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 259 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
| 260 | |
| 261 | if (!const_cast<CXXRecordDecl *>(DerivedRD)-> |
| 262 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) { |
| 263 | llvm_unreachable("Class must be derived from the passed in base class!"); |
| 264 | } |
| 265 | |
| 266 | return ComputeBaseOffset(Context, DerivedRD, Paths.front()); |
| 267 | } |
| 268 | |
| 269 | static BaseOffset |
| 270 | ComputeReturnAdjustmentBaseOffset(ASTContext &Context, |
| 271 | const CXXMethodDecl *DerivedMD, |
| 272 | const CXXMethodDecl *BaseMD) { |
| 273 | const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>(); |
| 274 | const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>(); |
| 275 | |
| 276 | // Canonicalize the return types. |
| 277 | CanQualType CanDerivedReturnType = |
| 278 | Context.getCanonicalType(DerivedFT->getResultType()); |
| 279 | CanQualType CanBaseReturnType = |
| 280 | Context.getCanonicalType(BaseFT->getResultType()); |
| 281 | |
| 282 | assert(CanDerivedReturnType->getTypeClass() == |
| 283 | CanBaseReturnType->getTypeClass() && |
| 284 | "Types must have same type class!"); |
| 285 | |
| 286 | if (CanDerivedReturnType == CanBaseReturnType) { |
| 287 | // No adjustment needed. |
| 288 | return BaseOffset(); |
| 289 | } |
| 290 | |
| 291 | if (isa<ReferenceType>(CanDerivedReturnType)) { |
| 292 | CanDerivedReturnType = |
| 293 | CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType(); |
| 294 | CanBaseReturnType = |
| 295 | CanBaseReturnType->getAs<ReferenceType>()->getPointeeType(); |
| 296 | } else if (isa<PointerType>(CanDerivedReturnType)) { |
| 297 | CanDerivedReturnType = |
| 298 | CanDerivedReturnType->getAs<PointerType>()->getPointeeType(); |
| 299 | CanBaseReturnType = |
| 300 | CanBaseReturnType->getAs<PointerType>()->getPointeeType(); |
| 301 | } else { |
| 302 | llvm_unreachable("Unexpected return type!"); |
| 303 | } |
| 304 | |
| 305 | // We need to compare unqualified types here; consider |
| 306 | // const T *Base::foo(); |
| 307 | // T *Derived::foo(); |
| 308 | if (CanDerivedReturnType.getUnqualifiedType() == |
| 309 | CanBaseReturnType.getUnqualifiedType()) { |
| 310 | // No adjustment needed. |
| 311 | return BaseOffset(); |
| 312 | } |
| 313 | |
| 314 | const CXXRecordDecl *DerivedRD = |
| 315 | cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl()); |
| 316 | |
| 317 | const CXXRecordDecl *BaseRD = |
| 318 | cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl()); |
| 319 | |
| 320 | return ComputeBaseOffset(Context, BaseRD, DerivedRD); |
| 321 | } |
| 322 | |
| 323 | void |
| 324 | FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, |
| 325 | CharUnits OffsetInLayoutClass, |
| 326 | SubobjectOffsetMapTy &SubobjectOffsets, |
| 327 | SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, |
| 328 | SubobjectCountMapTy &SubobjectCounts) { |
| 329 | const CXXRecordDecl *RD = Base.getBase(); |
| 330 | |
| 331 | unsigned SubobjectNumber = 0; |
| 332 | if (!IsVirtual) |
| 333 | SubobjectNumber = ++SubobjectCounts[RD]; |
| 334 | |
| 335 | // Set up the subobject to offset mapping. |
| 336 | assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber)) |
| 337 | && "Subobject offset already exists!"); |
| 338 | assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber)) |
| 339 | && "Subobject offset already exists!"); |
| 340 | |
| 341 | SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = Base.getBaseOffset(); |
| 342 | SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] = |
| 343 | OffsetInLayoutClass; |
| 344 | |
| 345 | // Traverse our bases. |
| 346 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 347 | E = RD->bases_end(); I != E; ++I) { |
| 348 | const CXXRecordDecl *BaseDecl = |
| 349 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 350 | |
| 351 | CharUnits BaseOffset; |
| 352 | CharUnits BaseOffsetInLayoutClass; |
| 353 | if (I->isVirtual()) { |
| 354 | // Check if we've visited this virtual base before. |
| 355 | if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0))) |
| 356 | continue; |
| 357 | |
| 358 | const ASTRecordLayout &LayoutClassLayout = |
| 359 | Context.getASTRecordLayout(LayoutClass); |
| 360 | |
| 361 | BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
| 362 | BaseOffsetInLayoutClass = |
| 363 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
| 364 | } else { |
| 365 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 366 | CharUnits Offset = Layout.getBaseClassOffset(BaseDecl); |
| 367 | |
| 368 | BaseOffset = Base.getBaseOffset() + Offset; |
| 369 | BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset; |
| 370 | } |
| 371 | |
| 372 | ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset), |
| 373 | I->isVirtual(), BaseOffsetInLayoutClass, |
| 374 | SubobjectOffsets, SubobjectLayoutClassOffsets, |
| 375 | SubobjectCounts); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void FinalOverriders::dump(raw_ostream &Out, BaseSubobject Base, |
| 380 | VisitedVirtualBasesSetTy &VisitedVirtualBases) { |
| 381 | const CXXRecordDecl *RD = Base.getBase(); |
| 382 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 383 | |
| 384 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 385 | E = RD->bases_end(); I != E; ++I) { |
| 386 | const CXXRecordDecl *BaseDecl = |
| 387 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 388 | |
| 389 | // Ignore bases that don't have any virtual member functions. |
| 390 | if (!BaseDecl->isPolymorphic()) |
| 391 | continue; |
| 392 | |
| 393 | CharUnits BaseOffset; |
| 394 | if (I->isVirtual()) { |
| 395 | if (!VisitedVirtualBases.insert(BaseDecl)) { |
| 396 | // We've visited this base before. |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
| 401 | } else { |
| 402 | BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset(); |
| 403 | } |
| 404 | |
| 405 | dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases); |
| 406 | } |
| 407 | |
| 408 | Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", "; |
| 409 | Out << Base.getBaseOffset().getQuantity() << ")\n"; |
| 410 | |
| 411 | // Now dump the overriders for this base subobject. |
| 412 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 413 | E = RD->method_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 414 | const CXXMethodDecl *MD = *I; |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 415 | |
| 416 | if (!MD->isVirtual()) |
| 417 | continue; |
| 418 | |
| 419 | OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset()); |
| 420 | |
| 421 | Out << " " << MD->getQualifiedNameAsString() << " - ("; |
| 422 | Out << Overrider.Method->getQualifiedNameAsString(); |
| 423 | Out << ", " << ", " << Overrider.Offset.getQuantity() << ')'; |
| 424 | |
| 425 | BaseOffset Offset; |
| 426 | if (!Overrider.Method->isPure()) |
| 427 | Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD); |
| 428 | |
| 429 | if (!Offset.isEmpty()) { |
| 430 | Out << " [ret-adj: "; |
| 431 | if (Offset.VirtualBase) |
| 432 | Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, "; |
| 433 | |
| 434 | Out << Offset.NonVirtualOffset.getQuantity() << " nv]"; |
| 435 | } |
| 436 | |
| 437 | Out << "\n"; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /// VCallOffsetMap - Keeps track of vcall offsets when building a vtable. |
| 442 | struct VCallOffsetMap { |
| 443 | |
| 444 | typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy; |
| 445 | |
| 446 | /// Offsets - Keeps track of methods and their offsets. |
| 447 | // FIXME: This should be a real map and not a vector. |
| 448 | SmallVector<MethodAndOffsetPairTy, 16> Offsets; |
| 449 | |
| 450 | /// MethodsCanShareVCallOffset - Returns whether two virtual member functions |
| 451 | /// can share the same vcall offset. |
| 452 | static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, |
| 453 | const CXXMethodDecl *RHS); |
| 454 | |
| 455 | public: |
| 456 | /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the |
| 457 | /// add was successful, or false if there was already a member function with |
| 458 | /// the same signature in the map. |
| 459 | bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset); |
| 460 | |
| 461 | /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the |
| 462 | /// vtable address point) for the given virtual member function. |
| 463 | CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD); |
| 464 | |
| 465 | // empty - Return whether the offset map is empty or not. |
| 466 | bool empty() const { return Offsets.empty(); } |
| 467 | }; |
| 468 | |
| 469 | static bool HasSameVirtualSignature(const CXXMethodDecl *LHS, |
| 470 | const CXXMethodDecl *RHS) { |
John McCall | 260a3e4 | 2012-03-21 06:57:19 +0000 | [diff] [blame] | 471 | const FunctionProtoType *LT = |
| 472 | cast<FunctionProtoType>(LHS->getType().getCanonicalType()); |
| 473 | const FunctionProtoType *RT = |
| 474 | cast<FunctionProtoType>(RHS->getType().getCanonicalType()); |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 475 | |
| 476 | // Fast-path matches in the canonical types. |
| 477 | if (LT == RT) return true; |
| 478 | |
| 479 | // Force the signatures to match. We can't rely on the overrides |
| 480 | // list here because there isn't necessarily an inheritance |
| 481 | // relationship between the two methods. |
John McCall | 260a3e4 | 2012-03-21 06:57:19 +0000 | [diff] [blame] | 482 | if (LT->getTypeQuals() != RT->getTypeQuals() || |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 483 | LT->getNumArgs() != RT->getNumArgs()) |
| 484 | return false; |
| 485 | for (unsigned I = 0, E = LT->getNumArgs(); I != E; ++I) |
| 486 | if (LT->getArgType(I) != RT->getArgType(I)) |
| 487 | return false; |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, |
| 492 | const CXXMethodDecl *RHS) { |
| 493 | assert(LHS->isVirtual() && "LHS must be virtual!"); |
| 494 | assert(RHS->isVirtual() && "LHS must be virtual!"); |
| 495 | |
| 496 | // A destructor can share a vcall offset with another destructor. |
| 497 | if (isa<CXXDestructorDecl>(LHS)) |
| 498 | return isa<CXXDestructorDecl>(RHS); |
| 499 | |
| 500 | // FIXME: We need to check more things here. |
| 501 | |
| 502 | // The methods must have the same name. |
| 503 | DeclarationName LHSName = LHS->getDeclName(); |
| 504 | DeclarationName RHSName = RHS->getDeclName(); |
| 505 | if (LHSName != RHSName) |
| 506 | return false; |
| 507 | |
| 508 | // And the same signatures. |
| 509 | return HasSameVirtualSignature(LHS, RHS); |
| 510 | } |
| 511 | |
| 512 | bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD, |
| 513 | CharUnits OffsetOffset) { |
| 514 | // Check if we can reuse an offset. |
| 515 | for (unsigned I = 0, E = Offsets.size(); I != E; ++I) { |
| 516 | if (MethodsCanShareVCallOffset(Offsets[I].first, MD)) |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | // Add the offset. |
| 521 | Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset)); |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) { |
| 526 | // Look for an offset. |
| 527 | for (unsigned I = 0, E = Offsets.size(); I != E; ++I) { |
| 528 | if (MethodsCanShareVCallOffset(Offsets[I].first, MD)) |
| 529 | return Offsets[I].second; |
| 530 | } |
| 531 | |
| 532 | llvm_unreachable("Should always find a vcall offset offset!"); |
| 533 | } |
| 534 | |
| 535 | /// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets. |
| 536 | class VCallAndVBaseOffsetBuilder { |
| 537 | public: |
| 538 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> |
| 539 | VBaseOffsetOffsetsMapTy; |
| 540 | |
| 541 | private: |
| 542 | /// MostDerivedClass - The most derived class for which we're building vcall |
| 543 | /// and vbase offsets. |
| 544 | const CXXRecordDecl *MostDerivedClass; |
| 545 | |
| 546 | /// LayoutClass - The class we're using for layout information. Will be |
| 547 | /// different than the most derived class if we're building a construction |
| 548 | /// vtable. |
| 549 | const CXXRecordDecl *LayoutClass; |
| 550 | |
| 551 | /// Context - The ASTContext which we will use for layout information. |
| 552 | ASTContext &Context; |
| 553 | |
| 554 | /// Components - vcall and vbase offset components |
| 555 | typedef SmallVector<VTableComponent, 64> VTableComponentVectorTy; |
| 556 | VTableComponentVectorTy Components; |
| 557 | |
| 558 | /// VisitedVirtualBases - Visited virtual bases. |
| 559 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; |
| 560 | |
| 561 | /// VCallOffsets - Keeps track of vcall offsets. |
| 562 | VCallOffsetMap VCallOffsets; |
| 563 | |
| 564 | |
| 565 | /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets, |
| 566 | /// relative to the address point. |
| 567 | VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; |
| 568 | |
| 569 | /// FinalOverriders - The final overriders of the most derived class. |
| 570 | /// (Can be null when we're not building a vtable of the most derived class). |
| 571 | const FinalOverriders *Overriders; |
| 572 | |
| 573 | /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the |
| 574 | /// given base subobject. |
| 575 | void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual, |
| 576 | CharUnits RealBaseOffset); |
| 577 | |
| 578 | /// AddVCallOffsets - Add vcall offsets for the given base subobject. |
| 579 | void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset); |
| 580 | |
| 581 | /// AddVBaseOffsets - Add vbase offsets for the given class. |
| 582 | void AddVBaseOffsets(const CXXRecordDecl *Base, |
| 583 | CharUnits OffsetInLayoutClass); |
| 584 | |
| 585 | /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in |
| 586 | /// chars, relative to the vtable address point. |
| 587 | CharUnits getCurrentOffsetOffset() const; |
| 588 | |
| 589 | public: |
| 590 | VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass, |
| 591 | const CXXRecordDecl *LayoutClass, |
| 592 | const FinalOverriders *Overriders, |
| 593 | BaseSubobject Base, bool BaseIsVirtual, |
| 594 | CharUnits OffsetInLayoutClass) |
| 595 | : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass), |
| 596 | Context(MostDerivedClass->getASTContext()), Overriders(Overriders) { |
| 597 | |
| 598 | // Add vcall and vbase offsets. |
| 599 | AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass); |
| 600 | } |
| 601 | |
| 602 | /// Methods for iterating over the components. |
| 603 | typedef VTableComponentVectorTy::const_reverse_iterator const_iterator; |
| 604 | const_iterator components_begin() const { return Components.rbegin(); } |
| 605 | const_iterator components_end() const { return Components.rend(); } |
| 606 | |
| 607 | const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; } |
| 608 | const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { |
| 609 | return VBaseOffsetOffsets; |
| 610 | } |
| 611 | }; |
| 612 | |
| 613 | void |
| 614 | VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base, |
| 615 | bool BaseIsVirtual, |
| 616 | CharUnits RealBaseOffset) { |
| 617 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase()); |
| 618 | |
| 619 | // Itanium C++ ABI 2.5.2: |
| 620 | // ..in classes sharing a virtual table with a primary base class, the vcall |
| 621 | // and vbase offsets added by the derived class all come before the vcall |
| 622 | // and vbase offsets required by the base class, so that the latter may be |
| 623 | // laid out as required by the base class without regard to additions from |
| 624 | // the derived class(es). |
| 625 | |
| 626 | // (Since we're emitting the vcall and vbase offsets in reverse order, we'll |
| 627 | // emit them for the primary base first). |
| 628 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
| 629 | bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); |
| 630 | |
| 631 | CharUnits PrimaryBaseOffset; |
| 632 | |
| 633 | // Get the base offset of the primary base. |
| 634 | if (PrimaryBaseIsVirtual) { |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 635 | assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 636 | "Primary vbase should have a zero offset!"); |
| 637 | |
| 638 | const ASTRecordLayout &MostDerivedClassLayout = |
| 639 | Context.getASTRecordLayout(MostDerivedClass); |
| 640 | |
| 641 | PrimaryBaseOffset = |
| 642 | MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase); |
| 643 | } else { |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 644 | assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 645 | "Primary base should have a zero offset!"); |
| 646 | |
| 647 | PrimaryBaseOffset = Base.getBaseOffset(); |
| 648 | } |
| 649 | |
| 650 | AddVCallAndVBaseOffsets( |
| 651 | BaseSubobject(PrimaryBase,PrimaryBaseOffset), |
| 652 | PrimaryBaseIsVirtual, RealBaseOffset); |
| 653 | } |
| 654 | |
| 655 | AddVBaseOffsets(Base.getBase(), RealBaseOffset); |
| 656 | |
| 657 | // We only want to add vcall offsets for virtual bases. |
| 658 | if (BaseIsVirtual) |
| 659 | AddVCallOffsets(Base, RealBaseOffset); |
| 660 | } |
| 661 | |
| 662 | CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const { |
| 663 | // OffsetIndex is the index of this vcall or vbase offset, relative to the |
| 664 | // vtable address point. (We subtract 3 to account for the information just |
| 665 | // above the address point, the RTTI info, the offset to top, and the |
| 666 | // vcall offset itself). |
| 667 | int64_t OffsetIndex = -(int64_t)(3 + Components.size()); |
| 668 | |
| 669 | CharUnits PointerWidth = |
| 670 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
| 671 | CharUnits OffsetOffset = PointerWidth * OffsetIndex; |
| 672 | return OffsetOffset; |
| 673 | } |
| 674 | |
| 675 | void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base, |
| 676 | CharUnits VBaseOffset) { |
| 677 | const CXXRecordDecl *RD = Base.getBase(); |
| 678 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 679 | |
| 680 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 681 | |
| 682 | // Handle the primary base first. |
| 683 | // We only want to add vcall offsets if the base is non-virtual; a virtual |
| 684 | // primary base will have its vcall and vbase offsets emitted already. |
| 685 | if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) { |
| 686 | // Get the base offset of the primary base. |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 687 | assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 688 | "Primary base should have a zero offset!"); |
| 689 | |
| 690 | AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()), |
| 691 | VBaseOffset); |
| 692 | } |
| 693 | |
| 694 | // Add the vcall offsets. |
| 695 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 696 | E = RD->method_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 697 | const CXXMethodDecl *MD = *I; |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 698 | |
| 699 | if (!MD->isVirtual()) |
| 700 | continue; |
| 701 | |
| 702 | CharUnits OffsetOffset = getCurrentOffsetOffset(); |
| 703 | |
| 704 | // Don't add a vcall offset if we already have one for this member function |
| 705 | // signature. |
| 706 | if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset)) |
| 707 | continue; |
| 708 | |
| 709 | CharUnits Offset = CharUnits::Zero(); |
| 710 | |
| 711 | if (Overriders) { |
| 712 | // Get the final overrider. |
| 713 | FinalOverriders::OverriderInfo Overrider = |
| 714 | Overriders->getOverrider(MD, Base.getBaseOffset()); |
| 715 | |
| 716 | /// The vcall offset is the offset from the virtual base to the object |
| 717 | /// where the function was overridden. |
| 718 | Offset = Overrider.Offset - VBaseOffset; |
| 719 | } |
| 720 | |
| 721 | Components.push_back( |
| 722 | VTableComponent::MakeVCallOffset(Offset)); |
| 723 | } |
| 724 | |
| 725 | // And iterate over all non-virtual bases (ignoring the primary base). |
| 726 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 727 | E = RD->bases_end(); I != E; ++I) { |
| 728 | |
| 729 | if (I->isVirtual()) |
| 730 | continue; |
| 731 | |
| 732 | const CXXRecordDecl *BaseDecl = |
| 733 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 734 | if (BaseDecl == PrimaryBase) |
| 735 | continue; |
| 736 | |
| 737 | // Get the base offset of this base. |
| 738 | CharUnits BaseOffset = Base.getBaseOffset() + |
| 739 | Layout.getBaseClassOffset(BaseDecl); |
| 740 | |
| 741 | AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset), |
| 742 | VBaseOffset); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | void |
| 747 | VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD, |
| 748 | CharUnits OffsetInLayoutClass) { |
| 749 | const ASTRecordLayout &LayoutClassLayout = |
| 750 | Context.getASTRecordLayout(LayoutClass); |
| 751 | |
| 752 | // Add vbase offsets. |
| 753 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 754 | E = RD->bases_end(); I != E; ++I) { |
| 755 | const CXXRecordDecl *BaseDecl = |
| 756 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 757 | |
| 758 | // Check if this is a virtual base that we haven't visited before. |
| 759 | if (I->isVirtual() && VisitedVirtualBases.insert(BaseDecl)) { |
| 760 | CharUnits Offset = |
| 761 | LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass; |
| 762 | |
| 763 | // Add the vbase offset offset. |
| 764 | assert(!VBaseOffsetOffsets.count(BaseDecl) && |
| 765 | "vbase offset offset already exists!"); |
| 766 | |
| 767 | CharUnits VBaseOffsetOffset = getCurrentOffsetOffset(); |
| 768 | VBaseOffsetOffsets.insert( |
| 769 | std::make_pair(BaseDecl, VBaseOffsetOffset)); |
| 770 | |
| 771 | Components.push_back( |
| 772 | VTableComponent::MakeVBaseOffset(Offset)); |
| 773 | } |
| 774 | |
| 775 | // Check the base class looking for more vbase offsets. |
| 776 | AddVBaseOffsets(BaseDecl, OffsetInLayoutClass); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /// VTableBuilder - Class for building vtable layout information. |
| 781 | class VTableBuilder { |
| 782 | public: |
| 783 | /// PrimaryBasesSetVectorTy - A set vector of direct and indirect |
| 784 | /// primary bases. |
| 785 | typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> |
| 786 | PrimaryBasesSetVectorTy; |
| 787 | |
| 788 | typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> |
| 789 | VBaseOffsetOffsetsMapTy; |
| 790 | |
| 791 | typedef llvm::DenseMap<BaseSubobject, uint64_t> |
| 792 | AddressPointsMapTy; |
| 793 | |
| 794 | private: |
| 795 | /// VTables - Global vtable information. |
| 796 | VTableContext &VTables; |
| 797 | |
| 798 | /// MostDerivedClass - The most derived class for which we're building this |
| 799 | /// vtable. |
| 800 | const CXXRecordDecl *MostDerivedClass; |
| 801 | |
| 802 | /// MostDerivedClassOffset - If we're building a construction vtable, this |
| 803 | /// holds the offset from the layout class to the most derived class. |
| 804 | const CharUnits MostDerivedClassOffset; |
| 805 | |
| 806 | /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual |
| 807 | /// base. (This only makes sense when building a construction vtable). |
| 808 | bool MostDerivedClassIsVirtual; |
| 809 | |
| 810 | /// LayoutClass - The class we're using for layout information. Will be |
| 811 | /// different than the most derived class if we're building a construction |
| 812 | /// vtable. |
| 813 | const CXXRecordDecl *LayoutClass; |
| 814 | |
| 815 | /// Context - The ASTContext which we will use for layout information. |
| 816 | ASTContext &Context; |
| 817 | |
| 818 | /// FinalOverriders - The final overriders of the most derived class. |
| 819 | const FinalOverriders Overriders; |
| 820 | |
| 821 | /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual |
| 822 | /// bases in this vtable. |
| 823 | llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases; |
| 824 | |
| 825 | /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for |
| 826 | /// the most derived class. |
| 827 | VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; |
| 828 | |
| 829 | /// Components - The components of the vtable being built. |
| 830 | SmallVector<VTableComponent, 64> Components; |
| 831 | |
| 832 | /// AddressPoints - Address points for the vtable being built. |
| 833 | AddressPointsMapTy AddressPoints; |
| 834 | |
| 835 | /// MethodInfo - Contains information about a method in a vtable. |
| 836 | /// (Used for computing 'this' pointer adjustment thunks. |
| 837 | struct MethodInfo { |
| 838 | /// BaseOffset - The base offset of this method. |
| 839 | const CharUnits BaseOffset; |
| 840 | |
| 841 | /// BaseOffsetInLayoutClass - The base offset in the layout class of this |
| 842 | /// method. |
| 843 | const CharUnits BaseOffsetInLayoutClass; |
| 844 | |
| 845 | /// VTableIndex - The index in the vtable that this method has. |
| 846 | /// (For destructors, this is the index of the complete destructor). |
| 847 | const uint64_t VTableIndex; |
| 848 | |
| 849 | MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass, |
| 850 | uint64_t VTableIndex) |
| 851 | : BaseOffset(BaseOffset), |
| 852 | BaseOffsetInLayoutClass(BaseOffsetInLayoutClass), |
| 853 | VTableIndex(VTableIndex) { } |
| 854 | |
| 855 | MethodInfo() |
| 856 | : BaseOffset(CharUnits::Zero()), |
| 857 | BaseOffsetInLayoutClass(CharUnits::Zero()), |
| 858 | VTableIndex(0) { } |
| 859 | }; |
| 860 | |
| 861 | typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy; |
| 862 | |
| 863 | /// MethodInfoMap - The information for all methods in the vtable we're |
| 864 | /// currently building. |
| 865 | MethodInfoMapTy MethodInfoMap; |
| 866 | |
| 867 | typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy; |
| 868 | |
| 869 | /// VTableThunks - The thunks by vtable index in the vtable currently being |
| 870 | /// built. |
| 871 | VTableThunksMapTy VTableThunks; |
| 872 | |
| 873 | typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; |
| 874 | typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; |
| 875 | |
| 876 | /// Thunks - A map that contains all the thunks needed for all methods in the |
| 877 | /// most derived class for which the vtable is currently being built. |
| 878 | ThunksMapTy Thunks; |
| 879 | |
| 880 | /// AddThunk - Add a thunk for the given method. |
| 881 | void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk); |
| 882 | |
| 883 | /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the |
| 884 | /// part of the vtable we're currently building. |
| 885 | void ComputeThisAdjustments(); |
| 886 | |
| 887 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; |
| 888 | |
| 889 | /// PrimaryVirtualBases - All known virtual bases who are a primary base of |
| 890 | /// some other base. |
| 891 | VisitedVirtualBasesSetTy PrimaryVirtualBases; |
| 892 | |
| 893 | /// ComputeReturnAdjustment - Compute the return adjustment given a return |
| 894 | /// adjustment base offset. |
| 895 | ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset); |
| 896 | |
| 897 | /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting |
| 898 | /// the 'this' pointer from the base subobject to the derived subobject. |
| 899 | BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base, |
| 900 | BaseSubobject Derived) const; |
| 901 | |
| 902 | /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the |
| 903 | /// given virtual member function, its offset in the layout class and its |
| 904 | /// final overrider. |
| 905 | ThisAdjustment |
| 906 | ComputeThisAdjustment(const CXXMethodDecl *MD, |
| 907 | CharUnits BaseOffsetInLayoutClass, |
| 908 | FinalOverriders::OverriderInfo Overrider); |
| 909 | |
| 910 | /// AddMethod - Add a single virtual member function to the vtable |
| 911 | /// components vector. |
| 912 | void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment); |
| 913 | |
| 914 | /// IsOverriderUsed - Returns whether the overrider will ever be used in this |
| 915 | /// part of the vtable. |
| 916 | /// |
| 917 | /// Itanium C++ ABI 2.5.2: |
| 918 | /// |
| 919 | /// struct A { virtual void f(); }; |
| 920 | /// struct B : virtual public A { int i; }; |
| 921 | /// struct C : virtual public A { int j; }; |
| 922 | /// struct D : public B, public C {}; |
| 923 | /// |
| 924 | /// When B and C are declared, A is a primary base in each case, so although |
| 925 | /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this |
| 926 | /// adjustment is required and no thunk is generated. However, inside D |
| 927 | /// objects, A is no longer a primary base of C, so if we allowed calls to |
| 928 | /// C::f() to use the copy of A's vtable in the C subobject, we would need |
| 929 | /// to adjust this from C* to B::A*, which would require a third-party |
| 930 | /// thunk. Since we require that a call to C::f() first convert to A*, |
| 931 | /// C-in-D's copy of A's vtable is never referenced, so this is not |
| 932 | /// necessary. |
| 933 | bool IsOverriderUsed(const CXXMethodDecl *Overrider, |
| 934 | CharUnits BaseOffsetInLayoutClass, |
| 935 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 936 | CharUnits FirstBaseOffsetInLayoutClass) const; |
| 937 | |
| 938 | |
| 939 | /// AddMethods - Add the methods of this base subobject and all its |
| 940 | /// primary bases to the vtable components vector. |
| 941 | void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass, |
| 942 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 943 | CharUnits FirstBaseOffsetInLayoutClass, |
| 944 | PrimaryBasesSetVectorTy &PrimaryBases); |
| 945 | |
| 946 | // LayoutVTable - Layout the vtable for the given base class, including its |
| 947 | // secondary vtables and any vtables for virtual bases. |
| 948 | void LayoutVTable(); |
| 949 | |
| 950 | /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the |
| 951 | /// given base subobject, as well as all its secondary vtables. |
| 952 | /// |
| 953 | /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base |
| 954 | /// or a direct or indirect base of a virtual base. |
| 955 | /// |
| 956 | /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual |
| 957 | /// in the layout class. |
| 958 | void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base, |
| 959 | bool BaseIsMorallyVirtual, |
| 960 | bool BaseIsVirtualInLayoutClass, |
| 961 | CharUnits OffsetInLayoutClass); |
| 962 | |
| 963 | /// LayoutSecondaryVTables - Layout the secondary vtables for the given base |
| 964 | /// subobject. |
| 965 | /// |
| 966 | /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base |
| 967 | /// or a direct or indirect base of a virtual base. |
| 968 | void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual, |
| 969 | CharUnits OffsetInLayoutClass); |
| 970 | |
| 971 | /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this |
| 972 | /// class hierarchy. |
| 973 | void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD, |
| 974 | CharUnits OffsetInLayoutClass, |
| 975 | VisitedVirtualBasesSetTy &VBases); |
| 976 | |
| 977 | /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the |
| 978 | /// given base (excluding any primary bases). |
| 979 | void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD, |
| 980 | VisitedVirtualBasesSetTy &VBases); |
| 981 | |
| 982 | /// isBuildingConstructionVTable - Return whether this vtable builder is |
| 983 | /// building a construction vtable. |
| 984 | bool isBuildingConstructorVTable() const { |
| 985 | return MostDerivedClass != LayoutClass; |
| 986 | } |
| 987 | |
| 988 | public: |
| 989 | VTableBuilder(VTableContext &VTables, const CXXRecordDecl *MostDerivedClass, |
| 990 | CharUnits MostDerivedClassOffset, |
| 991 | bool MostDerivedClassIsVirtual, const |
| 992 | CXXRecordDecl *LayoutClass) |
| 993 | : VTables(VTables), MostDerivedClass(MostDerivedClass), |
| 994 | MostDerivedClassOffset(MostDerivedClassOffset), |
| 995 | MostDerivedClassIsVirtual(MostDerivedClassIsVirtual), |
| 996 | LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()), |
| 997 | Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) { |
| 998 | |
| 999 | LayoutVTable(); |
| 1000 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1001 | if (Context.getLangOpts().DumpVTableLayouts) |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1002 | dumpLayout(llvm::errs()); |
| 1003 | } |
| 1004 | |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 1005 | bool isMicrosoftABI() const { |
| 1006 | return VTables.isMicrosoftABI(); |
| 1007 | } |
| 1008 | |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1009 | uint64_t getNumThunks() const { |
| 1010 | return Thunks.size(); |
| 1011 | } |
| 1012 | |
| 1013 | ThunksMapTy::const_iterator thunks_begin() const { |
| 1014 | return Thunks.begin(); |
| 1015 | } |
| 1016 | |
| 1017 | ThunksMapTy::const_iterator thunks_end() const { |
| 1018 | return Thunks.end(); |
| 1019 | } |
| 1020 | |
| 1021 | const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { |
| 1022 | return VBaseOffsetOffsets; |
| 1023 | } |
| 1024 | |
| 1025 | const AddressPointsMapTy &getAddressPoints() const { |
| 1026 | return AddressPoints; |
| 1027 | } |
| 1028 | |
| 1029 | /// getNumVTableComponents - Return the number of components in the vtable |
| 1030 | /// currently built. |
| 1031 | uint64_t getNumVTableComponents() const { |
| 1032 | return Components.size(); |
| 1033 | } |
| 1034 | |
| 1035 | const VTableComponent *vtable_component_begin() const { |
| 1036 | return Components.begin(); |
| 1037 | } |
| 1038 | |
| 1039 | const VTableComponent *vtable_component_end() const { |
| 1040 | return Components.end(); |
| 1041 | } |
| 1042 | |
| 1043 | AddressPointsMapTy::const_iterator address_points_begin() const { |
| 1044 | return AddressPoints.begin(); |
| 1045 | } |
| 1046 | |
| 1047 | AddressPointsMapTy::const_iterator address_points_end() const { |
| 1048 | return AddressPoints.end(); |
| 1049 | } |
| 1050 | |
| 1051 | VTableThunksMapTy::const_iterator vtable_thunks_begin() const { |
| 1052 | return VTableThunks.begin(); |
| 1053 | } |
| 1054 | |
| 1055 | VTableThunksMapTy::const_iterator vtable_thunks_end() const { |
| 1056 | return VTableThunks.end(); |
| 1057 | } |
| 1058 | |
| 1059 | /// dumpLayout - Dump the vtable layout. |
| 1060 | void dumpLayout(raw_ostream&); |
| 1061 | }; |
| 1062 | |
| 1063 | void VTableBuilder::AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) { |
| 1064 | assert(!isBuildingConstructorVTable() && |
| 1065 | "Can't add thunks for construction vtable"); |
| 1066 | |
| 1067 | SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD]; |
| 1068 | |
| 1069 | // Check if we have this thunk already. |
| 1070 | if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) != |
| 1071 | ThunksVector.end()) |
| 1072 | return; |
| 1073 | |
| 1074 | ThunksVector.push_back(Thunk); |
| 1075 | } |
| 1076 | |
| 1077 | typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy; |
| 1078 | |
| 1079 | /// ComputeAllOverriddenMethods - Given a method decl, will return a set of all |
| 1080 | /// the overridden methods that the function decl overrides. |
| 1081 | static void |
| 1082 | ComputeAllOverriddenMethods(const CXXMethodDecl *MD, |
| 1083 | OverriddenMethodsSetTy& OverriddenMethods) { |
| 1084 | assert(MD->isVirtual() && "Method is not virtual!"); |
| 1085 | |
| 1086 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1087 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1088 | const CXXMethodDecl *OverriddenMD = *I; |
| 1089 | |
| 1090 | OverriddenMethods.insert(OverriddenMD); |
| 1091 | |
| 1092 | ComputeAllOverriddenMethods(OverriddenMD, OverriddenMethods); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | void VTableBuilder::ComputeThisAdjustments() { |
| 1097 | // Now go through the method info map and see if any of the methods need |
| 1098 | // 'this' pointer adjustments. |
| 1099 | for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(), |
| 1100 | E = MethodInfoMap.end(); I != E; ++I) { |
| 1101 | const CXXMethodDecl *MD = I->first; |
| 1102 | const MethodInfo &MethodInfo = I->second; |
| 1103 | |
| 1104 | // Ignore adjustments for unused function pointers. |
| 1105 | uint64_t VTableIndex = MethodInfo.VTableIndex; |
| 1106 | if (Components[VTableIndex].getKind() == |
| 1107 | VTableComponent::CK_UnusedFunctionPointer) |
| 1108 | continue; |
| 1109 | |
| 1110 | // Get the final overrider for this method. |
| 1111 | FinalOverriders::OverriderInfo Overrider = |
| 1112 | Overriders.getOverrider(MD, MethodInfo.BaseOffset); |
| 1113 | |
| 1114 | // Check if we need an adjustment at all. |
| 1115 | if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) { |
| 1116 | // When a return thunk is needed by a derived class that overrides a |
| 1117 | // virtual base, gcc uses a virtual 'this' adjustment as well. |
| 1118 | // While the thunk itself might be needed by vtables in subclasses or |
| 1119 | // in construction vtables, there doesn't seem to be a reason for using |
| 1120 | // the thunk in this vtable. Still, we do so to match gcc. |
| 1121 | if (VTableThunks.lookup(VTableIndex).Return.isEmpty()) |
| 1122 | continue; |
| 1123 | } |
| 1124 | |
| 1125 | ThisAdjustment ThisAdjustment = |
| 1126 | ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider); |
| 1127 | |
| 1128 | if (ThisAdjustment.isEmpty()) |
| 1129 | continue; |
| 1130 | |
| 1131 | // Add it. |
| 1132 | VTableThunks[VTableIndex].This = ThisAdjustment; |
| 1133 | |
| 1134 | if (isa<CXXDestructorDecl>(MD)) { |
| 1135 | // Add an adjustment for the deleting destructor as well. |
| 1136 | VTableThunks[VTableIndex + 1].This = ThisAdjustment; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | /// Clear the method info map. |
| 1141 | MethodInfoMap.clear(); |
| 1142 | |
| 1143 | if (isBuildingConstructorVTable()) { |
| 1144 | // We don't need to store thunk information for construction vtables. |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | for (VTableThunksMapTy::const_iterator I = VTableThunks.begin(), |
| 1149 | E = VTableThunks.end(); I != E; ++I) { |
| 1150 | const VTableComponent &Component = Components[I->first]; |
| 1151 | const ThunkInfo &Thunk = I->second; |
| 1152 | const CXXMethodDecl *MD; |
| 1153 | |
| 1154 | switch (Component.getKind()) { |
| 1155 | default: |
| 1156 | llvm_unreachable("Unexpected vtable component kind!"); |
| 1157 | case VTableComponent::CK_FunctionPointer: |
| 1158 | MD = Component.getFunctionDecl(); |
| 1159 | break; |
| 1160 | case VTableComponent::CK_CompleteDtorPointer: |
| 1161 | MD = Component.getDestructorDecl(); |
| 1162 | break; |
| 1163 | case VTableComponent::CK_DeletingDtorPointer: |
| 1164 | // We've already added the thunk when we saw the complete dtor pointer. |
| 1165 | continue; |
| 1166 | } |
| 1167 | |
| 1168 | if (MD->getParent() == MostDerivedClass) |
| 1169 | AddThunk(MD, Thunk); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | ReturnAdjustment VTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) { |
| 1174 | ReturnAdjustment Adjustment; |
| 1175 | |
| 1176 | if (!Offset.isEmpty()) { |
| 1177 | if (Offset.VirtualBase) { |
| 1178 | // Get the virtual base offset offset. |
| 1179 | if (Offset.DerivedClass == MostDerivedClass) { |
| 1180 | // We can get the offset offset directly from our map. |
| 1181 | Adjustment.VBaseOffsetOffset = |
| 1182 | VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity(); |
| 1183 | } else { |
| 1184 | Adjustment.VBaseOffsetOffset = |
| 1185 | VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass, |
| 1186 | Offset.VirtualBase).getQuantity(); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity(); |
| 1191 | } |
| 1192 | |
| 1193 | return Adjustment; |
| 1194 | } |
| 1195 | |
| 1196 | BaseOffset |
| 1197 | VTableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base, |
| 1198 | BaseSubobject Derived) const { |
| 1199 | const CXXRecordDecl *BaseRD = Base.getBase(); |
| 1200 | const CXXRecordDecl *DerivedRD = Derived.getBase(); |
| 1201 | |
| 1202 | CXXBasePaths Paths(/*FindAmbiguities=*/true, |
| 1203 | /*RecordPaths=*/true, /*DetectVirtual=*/true); |
| 1204 | |
| 1205 | if (!const_cast<CXXRecordDecl *>(DerivedRD)-> |
| 1206 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) { |
| 1207 | llvm_unreachable("Class must be derived from the passed in base class!"); |
| 1208 | } |
| 1209 | |
| 1210 | // We have to go through all the paths, and see which one leads us to the |
| 1211 | // right base subobject. |
| 1212 | for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end(); |
| 1213 | I != E; ++I) { |
| 1214 | BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I); |
| 1215 | |
| 1216 | CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset; |
| 1217 | |
| 1218 | if (Offset.VirtualBase) { |
| 1219 | // If we have a virtual base class, the non-virtual offset is relative |
| 1220 | // to the virtual base class offset. |
| 1221 | const ASTRecordLayout &LayoutClassLayout = |
| 1222 | Context.getASTRecordLayout(LayoutClass); |
| 1223 | |
| 1224 | /// Get the virtual base offset, relative to the most derived class |
| 1225 | /// layout. |
| 1226 | OffsetToBaseSubobject += |
| 1227 | LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase); |
| 1228 | } else { |
| 1229 | // Otherwise, the non-virtual offset is relative to the derived class |
| 1230 | // offset. |
| 1231 | OffsetToBaseSubobject += Derived.getBaseOffset(); |
| 1232 | } |
| 1233 | |
| 1234 | // Check if this path gives us the right base subobject. |
| 1235 | if (OffsetToBaseSubobject == Base.getBaseOffset()) { |
| 1236 | // Since we're going from the base class _to_ the derived class, we'll |
| 1237 | // invert the non-virtual offset here. |
| 1238 | Offset.NonVirtualOffset = -Offset.NonVirtualOffset; |
| 1239 | return Offset; |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | return BaseOffset(); |
| 1244 | } |
| 1245 | |
| 1246 | ThisAdjustment |
| 1247 | VTableBuilder::ComputeThisAdjustment(const CXXMethodDecl *MD, |
| 1248 | CharUnits BaseOffsetInLayoutClass, |
| 1249 | FinalOverriders::OverriderInfo Overrider) { |
| 1250 | // Ignore adjustments for pure virtual member functions. |
| 1251 | if (Overrider.Method->isPure()) |
| 1252 | return ThisAdjustment(); |
| 1253 | |
| 1254 | BaseSubobject OverriddenBaseSubobject(MD->getParent(), |
| 1255 | BaseOffsetInLayoutClass); |
| 1256 | |
| 1257 | BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(), |
| 1258 | Overrider.Offset); |
| 1259 | |
| 1260 | // Compute the adjustment offset. |
| 1261 | BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject, |
| 1262 | OverriderBaseSubobject); |
| 1263 | if (Offset.isEmpty()) |
| 1264 | return ThisAdjustment(); |
| 1265 | |
| 1266 | ThisAdjustment Adjustment; |
| 1267 | |
| 1268 | if (Offset.VirtualBase) { |
| 1269 | // Get the vcall offset map for this virtual base. |
| 1270 | VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase]; |
| 1271 | |
| 1272 | if (VCallOffsets.empty()) { |
| 1273 | // We don't have vcall offsets for this virtual base, go ahead and |
| 1274 | // build them. |
| 1275 | VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass, |
| 1276 | /*FinalOverriders=*/0, |
| 1277 | BaseSubobject(Offset.VirtualBase, |
| 1278 | CharUnits::Zero()), |
| 1279 | /*BaseIsVirtual=*/true, |
| 1280 | /*OffsetInLayoutClass=*/ |
| 1281 | CharUnits::Zero()); |
| 1282 | |
| 1283 | VCallOffsets = Builder.getVCallOffsets(); |
| 1284 | } |
| 1285 | |
| 1286 | Adjustment.VCallOffsetOffset = |
| 1287 | VCallOffsets.getVCallOffsetOffset(MD).getQuantity(); |
| 1288 | } |
| 1289 | |
| 1290 | // Set the non-virtual part of the adjustment. |
| 1291 | Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity(); |
| 1292 | |
| 1293 | return Adjustment; |
| 1294 | } |
| 1295 | |
| 1296 | void |
| 1297 | VTableBuilder::AddMethod(const CXXMethodDecl *MD, |
| 1298 | ReturnAdjustment ReturnAdjustment) { |
| 1299 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1300 | assert(ReturnAdjustment.isEmpty() && |
| 1301 | "Destructor can't have return adjustment!"); |
| 1302 | |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 1303 | // FIXME: Should probably add a layer of abstraction for vtable generation. |
| 1304 | if (!isMicrosoftABI()) { |
| 1305 | // Add both the complete destructor and the deleting destructor. |
| 1306 | Components.push_back(VTableComponent::MakeCompleteDtor(DD)); |
| 1307 | Components.push_back(VTableComponent::MakeDeletingDtor(DD)); |
| 1308 | } else { |
| 1309 | // Add only one destructor in MS mode. |
| 1310 | // FIXME: The virtual destructors are handled differently in MS ABI, |
| 1311 | // we should add such a support later. For now, put the complete |
| 1312 | // destructor into the vftable just to make its layout right. |
| 1313 | Components.push_back(VTableComponent::MakeCompleteDtor(DD)); |
| 1314 | } |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1315 | } else { |
| 1316 | // Add the return adjustment if necessary. |
| 1317 | if (!ReturnAdjustment.isEmpty()) |
| 1318 | VTableThunks[Components.size()].Return = ReturnAdjustment; |
| 1319 | |
| 1320 | // Add the function. |
| 1321 | Components.push_back(VTableComponent::MakeFunction(MD)); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | /// OverridesIndirectMethodInBase - Return whether the given member function |
| 1326 | /// overrides any methods in the set of given bases. |
| 1327 | /// Unlike OverridesMethodInBase, this checks "overriders of overriders". |
| 1328 | /// For example, if we have: |
| 1329 | /// |
| 1330 | /// struct A { virtual void f(); } |
| 1331 | /// struct B : A { virtual void f(); } |
| 1332 | /// struct C : B { virtual void f(); } |
| 1333 | /// |
| 1334 | /// OverridesIndirectMethodInBase will return true if given C::f as the method |
| 1335 | /// and { A } as the set of bases. |
| 1336 | static bool |
| 1337 | OverridesIndirectMethodInBases(const CXXMethodDecl *MD, |
| 1338 | VTableBuilder::PrimaryBasesSetVectorTy &Bases) { |
| 1339 | if (Bases.count(MD->getParent())) |
| 1340 | return true; |
| 1341 | |
| 1342 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1343 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1344 | const CXXMethodDecl *OverriddenMD = *I; |
| 1345 | |
| 1346 | // Check "indirect overriders". |
| 1347 | if (OverridesIndirectMethodInBases(OverriddenMD, Bases)) |
| 1348 | return true; |
| 1349 | } |
| 1350 | |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
| 1354 | bool |
| 1355 | VTableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider, |
| 1356 | CharUnits BaseOffsetInLayoutClass, |
| 1357 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 1358 | CharUnits FirstBaseOffsetInLayoutClass) const { |
| 1359 | // If the base and the first base in the primary base chain have the same |
| 1360 | // offsets, then this overrider will be used. |
| 1361 | if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass) |
| 1362 | return true; |
| 1363 | |
| 1364 | // We know now that Base (or a direct or indirect base of it) is a primary |
| 1365 | // base in part of the class hierarchy, but not a primary base in the most |
| 1366 | // derived class. |
| 1367 | |
| 1368 | // If the overrider is the first base in the primary base chain, we know |
| 1369 | // that the overrider will be used. |
| 1370 | if (Overrider->getParent() == FirstBaseInPrimaryBaseChain) |
| 1371 | return true; |
| 1372 | |
| 1373 | VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases; |
| 1374 | |
| 1375 | const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain; |
| 1376 | PrimaryBases.insert(RD); |
| 1377 | |
| 1378 | // Now traverse the base chain, starting with the first base, until we find |
| 1379 | // the base that is no longer a primary base. |
| 1380 | while (true) { |
| 1381 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1382 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1383 | |
| 1384 | if (!PrimaryBase) |
| 1385 | break; |
| 1386 | |
| 1387 | if (Layout.isPrimaryBaseVirtual()) { |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1388 | assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1389 | "Primary base should always be at offset 0!"); |
| 1390 | |
| 1391 | const ASTRecordLayout &LayoutClassLayout = |
| 1392 | Context.getASTRecordLayout(LayoutClass); |
| 1393 | |
| 1394 | // Now check if this is the primary base that is not a primary base in the |
| 1395 | // most derived class. |
| 1396 | if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != |
| 1397 | FirstBaseOffsetInLayoutClass) { |
| 1398 | // We found it, stop walking the chain. |
| 1399 | break; |
| 1400 | } |
| 1401 | } else { |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1402 | assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1403 | "Primary base should always be at offset 0!"); |
| 1404 | } |
| 1405 | |
| 1406 | if (!PrimaryBases.insert(PrimaryBase)) |
| 1407 | llvm_unreachable("Found a duplicate primary base!"); |
| 1408 | |
| 1409 | RD = PrimaryBase; |
| 1410 | } |
| 1411 | |
| 1412 | // If the final overrider is an override of one of the primary bases, |
| 1413 | // then we know that it will be used. |
| 1414 | return OverridesIndirectMethodInBases(Overrider, PrimaryBases); |
| 1415 | } |
| 1416 | |
| 1417 | /// FindNearestOverriddenMethod - Given a method, returns the overridden method |
| 1418 | /// from the nearest base. Returns null if no method was found. |
| 1419 | static const CXXMethodDecl * |
| 1420 | FindNearestOverriddenMethod(const CXXMethodDecl *MD, |
| 1421 | VTableBuilder::PrimaryBasesSetVectorTy &Bases) { |
| 1422 | OverriddenMethodsSetTy OverriddenMethods; |
| 1423 | ComputeAllOverriddenMethods(MD, OverriddenMethods); |
| 1424 | |
| 1425 | for (int I = Bases.size(), E = 0; I != E; --I) { |
| 1426 | const CXXRecordDecl *PrimaryBase = Bases[I - 1]; |
| 1427 | |
| 1428 | // Now check the overriden methods. |
| 1429 | for (OverriddenMethodsSetTy::const_iterator I = OverriddenMethods.begin(), |
| 1430 | E = OverriddenMethods.end(); I != E; ++I) { |
| 1431 | const CXXMethodDecl *OverriddenMD = *I; |
| 1432 | |
| 1433 | // We found our overridden method. |
| 1434 | if (OverriddenMD->getParent() == PrimaryBase) |
| 1435 | return OverriddenMD; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | void |
| 1443 | VTableBuilder::AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass, |
| 1444 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 1445 | CharUnits FirstBaseOffsetInLayoutClass, |
| 1446 | PrimaryBasesSetVectorTy &PrimaryBases) { |
| 1447 | const CXXRecordDecl *RD = Base.getBase(); |
| 1448 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1449 | |
| 1450 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
| 1451 | CharUnits PrimaryBaseOffset; |
| 1452 | CharUnits PrimaryBaseOffsetInLayoutClass; |
| 1453 | if (Layout.isPrimaryBaseVirtual()) { |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1454 | assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1455 | "Primary vbase should have a zero offset!"); |
| 1456 | |
| 1457 | const ASTRecordLayout &MostDerivedClassLayout = |
| 1458 | Context.getASTRecordLayout(MostDerivedClass); |
| 1459 | |
| 1460 | PrimaryBaseOffset = |
| 1461 | MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase); |
| 1462 | |
| 1463 | const ASTRecordLayout &LayoutClassLayout = |
| 1464 | Context.getASTRecordLayout(LayoutClass); |
| 1465 | |
| 1466 | PrimaryBaseOffsetInLayoutClass = |
| 1467 | LayoutClassLayout.getVBaseClassOffset(PrimaryBase); |
| 1468 | } else { |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1469 | assert(Layout.getBaseClassOffset(PrimaryBase).isZero() && |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1470 | "Primary base should have a zero offset!"); |
| 1471 | |
| 1472 | PrimaryBaseOffset = Base.getBaseOffset(); |
| 1473 | PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass; |
| 1474 | } |
| 1475 | |
| 1476 | AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset), |
| 1477 | PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain, |
| 1478 | FirstBaseOffsetInLayoutClass, PrimaryBases); |
| 1479 | |
| 1480 | if (!PrimaryBases.insert(PrimaryBase)) |
| 1481 | llvm_unreachable("Found a duplicate primary base!"); |
| 1482 | } |
| 1483 | |
| 1484 | // Now go through all virtual member functions and add them. |
| 1485 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 1486 | E = RD->method_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1487 | const CXXMethodDecl *MD = *I; |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1488 | |
| 1489 | if (!MD->isVirtual()) |
| 1490 | continue; |
| 1491 | |
| 1492 | // Get the final overrider. |
| 1493 | FinalOverriders::OverriderInfo Overrider = |
| 1494 | Overriders.getOverrider(MD, Base.getBaseOffset()); |
| 1495 | |
| 1496 | // Check if this virtual member function overrides a method in a primary |
| 1497 | // base. If this is the case, and the return type doesn't require adjustment |
| 1498 | // then we can just use the member function from the primary base. |
| 1499 | if (const CXXMethodDecl *OverriddenMD = |
| 1500 | FindNearestOverriddenMethod(MD, PrimaryBases)) { |
| 1501 | if (ComputeReturnAdjustmentBaseOffset(Context, MD, |
| 1502 | OverriddenMD).isEmpty()) { |
| 1503 | // Replace the method info of the overridden method with our own |
| 1504 | // method. |
| 1505 | assert(MethodInfoMap.count(OverriddenMD) && |
| 1506 | "Did not find the overridden method!"); |
| 1507 | MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD]; |
| 1508 | |
| 1509 | MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, |
| 1510 | OverriddenMethodInfo.VTableIndex); |
| 1511 | |
| 1512 | assert(!MethodInfoMap.count(MD) && |
| 1513 | "Should not have method info for this method yet!"); |
| 1514 | |
| 1515 | MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); |
| 1516 | MethodInfoMap.erase(OverriddenMD); |
| 1517 | |
| 1518 | // If the overridden method exists in a virtual base class or a direct |
| 1519 | // or indirect base class of a virtual base class, we need to emit a |
| 1520 | // thunk if we ever have a class hierarchy where the base class is not |
| 1521 | // a primary base in the complete object. |
| 1522 | if (!isBuildingConstructorVTable() && OverriddenMD != MD) { |
| 1523 | // Compute the this adjustment. |
| 1524 | ThisAdjustment ThisAdjustment = |
| 1525 | ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass, |
| 1526 | Overrider); |
| 1527 | |
| 1528 | if (ThisAdjustment.VCallOffsetOffset && |
| 1529 | Overrider.Method->getParent() == MostDerivedClass) { |
| 1530 | |
| 1531 | // There's no return adjustment from OverriddenMD and MD, |
| 1532 | // but that doesn't mean there isn't one between MD and |
| 1533 | // the final overrider. |
| 1534 | BaseOffset ReturnAdjustmentOffset = |
| 1535 | ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD); |
| 1536 | ReturnAdjustment ReturnAdjustment = |
| 1537 | ComputeReturnAdjustment(ReturnAdjustmentOffset); |
| 1538 | |
| 1539 | // This is a virtual thunk for the most derived class, add it. |
| 1540 | AddThunk(Overrider.Method, |
| 1541 | ThunkInfo(ThisAdjustment, ReturnAdjustment)); |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | continue; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | // Insert the method info for this method. |
| 1550 | MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, |
| 1551 | Components.size()); |
| 1552 | |
| 1553 | assert(!MethodInfoMap.count(MD) && |
| 1554 | "Should not have method info for this method yet!"); |
| 1555 | MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); |
| 1556 | |
| 1557 | // Check if this overrider is going to be used. |
| 1558 | const CXXMethodDecl *OverriderMD = Overrider.Method; |
| 1559 | if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass, |
| 1560 | FirstBaseInPrimaryBaseChain, |
| 1561 | FirstBaseOffsetInLayoutClass)) { |
| 1562 | Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD)); |
| 1563 | continue; |
| 1564 | } |
| 1565 | |
| 1566 | // Check if this overrider needs a return adjustment. |
| 1567 | // We don't want to do this for pure virtual member functions. |
| 1568 | BaseOffset ReturnAdjustmentOffset; |
| 1569 | if (!OverriderMD->isPure()) { |
| 1570 | ReturnAdjustmentOffset = |
| 1571 | ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD); |
| 1572 | } |
| 1573 | |
| 1574 | ReturnAdjustment ReturnAdjustment = |
| 1575 | ComputeReturnAdjustment(ReturnAdjustmentOffset); |
| 1576 | |
| 1577 | AddMethod(Overrider.Method, ReturnAdjustment); |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | void VTableBuilder::LayoutVTable() { |
| 1582 | LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass, |
| 1583 | CharUnits::Zero()), |
| 1584 | /*BaseIsMorallyVirtual=*/false, |
| 1585 | MostDerivedClassIsVirtual, |
| 1586 | MostDerivedClassOffset); |
| 1587 | |
| 1588 | VisitedVirtualBasesSetTy VBases; |
| 1589 | |
| 1590 | // Determine the primary virtual bases. |
| 1591 | DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset, |
| 1592 | VBases); |
| 1593 | VBases.clear(); |
| 1594 | |
| 1595 | LayoutVTablesForVirtualBases(MostDerivedClass, VBases); |
| 1596 | |
| 1597 | // -fapple-kext adds an extra entry at end of vtbl. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1598 | bool IsAppleKext = Context.getLangOpts().AppleKext; |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1599 | if (IsAppleKext) |
| 1600 | Components.push_back(VTableComponent::MakeVCallOffset(CharUnits::Zero())); |
| 1601 | } |
| 1602 | |
| 1603 | void |
| 1604 | VTableBuilder::LayoutPrimaryAndSecondaryVTables(BaseSubobject Base, |
| 1605 | bool BaseIsMorallyVirtual, |
| 1606 | bool BaseIsVirtualInLayoutClass, |
| 1607 | CharUnits OffsetInLayoutClass) { |
| 1608 | assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!"); |
| 1609 | |
| 1610 | // Add vcall and vbase offsets for this vtable. |
| 1611 | VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders, |
| 1612 | Base, BaseIsVirtualInLayoutClass, |
| 1613 | OffsetInLayoutClass); |
| 1614 | Components.append(Builder.components_begin(), Builder.components_end()); |
| 1615 | |
| 1616 | // Check if we need to add these vcall offsets. |
| 1617 | if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) { |
| 1618 | VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()]; |
| 1619 | |
| 1620 | if (VCallOffsets.empty()) |
| 1621 | VCallOffsets = Builder.getVCallOffsets(); |
| 1622 | } |
| 1623 | |
| 1624 | // If we're laying out the most derived class we want to keep track of the |
| 1625 | // virtual base class offset offsets. |
| 1626 | if (Base.getBase() == MostDerivedClass) |
| 1627 | VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets(); |
| 1628 | |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 1629 | // FIXME: Should probably add a layer of abstraction for vtable generation. |
| 1630 | if (!isMicrosoftABI()) { |
| 1631 | // Add the offset to top. |
| 1632 | CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass; |
| 1633 | Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop)); |
| 1634 | |
| 1635 | // Next, add the RTTI. |
| 1636 | Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass)); |
| 1637 | } else { |
| 1638 | // FIXME: unclear what to do with RTTI in MS ABI as emitting it anywhere |
| 1639 | // breaks the vftable layout. Just skip RTTI for now, can't mangle anyway. |
| 1640 | } |
| 1641 | |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1642 | uint64_t AddressPoint = Components.size(); |
| 1643 | |
| 1644 | // Now go through all virtual member functions and add them. |
| 1645 | PrimaryBasesSetVectorTy PrimaryBases; |
| 1646 | AddMethods(Base, OffsetInLayoutClass, |
| 1647 | Base.getBase(), OffsetInLayoutClass, |
| 1648 | PrimaryBases); |
| 1649 | |
| 1650 | // Compute 'this' pointer adjustments. |
| 1651 | ComputeThisAdjustments(); |
| 1652 | |
| 1653 | // Add all address points. |
| 1654 | const CXXRecordDecl *RD = Base.getBase(); |
| 1655 | while (true) { |
| 1656 | AddressPoints.insert(std::make_pair( |
| 1657 | BaseSubobject(RD, OffsetInLayoutClass), |
| 1658 | AddressPoint)); |
| 1659 | |
| 1660 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1661 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1662 | |
| 1663 | if (!PrimaryBase) |
| 1664 | break; |
| 1665 | |
| 1666 | if (Layout.isPrimaryBaseVirtual()) { |
| 1667 | // Check if this virtual primary base is a primary base in the layout |
| 1668 | // class. If it's not, we don't want to add it. |
| 1669 | const ASTRecordLayout &LayoutClassLayout = |
| 1670 | Context.getASTRecordLayout(LayoutClass); |
| 1671 | |
| 1672 | if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != |
| 1673 | OffsetInLayoutClass) { |
| 1674 | // We don't want to add this class (or any of its primary bases). |
| 1675 | break; |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | RD = PrimaryBase; |
| 1680 | } |
| 1681 | |
| 1682 | // Layout secondary vtables. |
| 1683 | LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass); |
| 1684 | } |
| 1685 | |
| 1686 | void VTableBuilder::LayoutSecondaryVTables(BaseSubobject Base, |
| 1687 | bool BaseIsMorallyVirtual, |
| 1688 | CharUnits OffsetInLayoutClass) { |
| 1689 | // Itanium C++ ABI 2.5.2: |
| 1690 | // Following the primary virtual table of a derived class are secondary |
| 1691 | // virtual tables for each of its proper base classes, except any primary |
| 1692 | // base(s) with which it shares its primary virtual table. |
| 1693 | |
| 1694 | const CXXRecordDecl *RD = Base.getBase(); |
| 1695 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1696 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1697 | |
| 1698 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1699 | E = RD->bases_end(); I != E; ++I) { |
| 1700 | // Ignore virtual bases, we'll emit them later. |
| 1701 | if (I->isVirtual()) |
| 1702 | continue; |
| 1703 | |
| 1704 | const CXXRecordDecl *BaseDecl = |
| 1705 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1706 | |
| 1707 | // Ignore bases that don't have a vtable. |
| 1708 | if (!BaseDecl->isDynamicClass()) |
| 1709 | continue; |
| 1710 | |
| 1711 | if (isBuildingConstructorVTable()) { |
| 1712 | // Itanium C++ ABI 2.6.4: |
| 1713 | // Some of the base class subobjects may not need construction virtual |
| 1714 | // tables, which will therefore not be present in the construction |
| 1715 | // virtual table group, even though the subobject virtual tables are |
| 1716 | // present in the main virtual table group for the complete object. |
| 1717 | if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases()) |
| 1718 | continue; |
| 1719 | } |
| 1720 | |
| 1721 | // Get the base offset of this base. |
| 1722 | CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl); |
| 1723 | CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset; |
| 1724 | |
| 1725 | CharUnits BaseOffsetInLayoutClass = |
| 1726 | OffsetInLayoutClass + RelativeBaseOffset; |
| 1727 | |
| 1728 | // Don't emit a secondary vtable for a primary base. We might however want |
| 1729 | // to emit secondary vtables for other bases of this base. |
| 1730 | if (BaseDecl == PrimaryBase) { |
| 1731 | LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset), |
| 1732 | BaseIsMorallyVirtual, BaseOffsetInLayoutClass); |
| 1733 | continue; |
| 1734 | } |
| 1735 | |
| 1736 | // Layout the primary vtable (and any secondary vtables) for this base. |
| 1737 | LayoutPrimaryAndSecondaryVTables( |
| 1738 | BaseSubobject(BaseDecl, BaseOffset), |
| 1739 | BaseIsMorallyVirtual, |
| 1740 | /*BaseIsVirtualInLayoutClass=*/false, |
| 1741 | BaseOffsetInLayoutClass); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | void |
| 1746 | VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD, |
| 1747 | CharUnits OffsetInLayoutClass, |
| 1748 | VisitedVirtualBasesSetTy &VBases) { |
| 1749 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1750 | |
| 1751 | // Check if this base has a primary base. |
| 1752 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
| 1753 | |
| 1754 | // Check if it's virtual. |
| 1755 | if (Layout.isPrimaryBaseVirtual()) { |
| 1756 | bool IsPrimaryVirtualBase = true; |
| 1757 | |
| 1758 | if (isBuildingConstructorVTable()) { |
| 1759 | // Check if the base is actually a primary base in the class we use for |
| 1760 | // layout. |
| 1761 | const ASTRecordLayout &LayoutClassLayout = |
| 1762 | Context.getASTRecordLayout(LayoutClass); |
| 1763 | |
| 1764 | CharUnits PrimaryBaseOffsetInLayoutClass = |
| 1765 | LayoutClassLayout.getVBaseClassOffset(PrimaryBase); |
| 1766 | |
| 1767 | // We know that the base is not a primary base in the layout class if |
| 1768 | // the base offsets are different. |
| 1769 | if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass) |
| 1770 | IsPrimaryVirtualBase = false; |
| 1771 | } |
| 1772 | |
| 1773 | if (IsPrimaryVirtualBase) |
| 1774 | PrimaryVirtualBases.insert(PrimaryBase); |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | // Traverse bases, looking for more primary virtual bases. |
| 1779 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1780 | E = RD->bases_end(); I != E; ++I) { |
| 1781 | const CXXRecordDecl *BaseDecl = |
| 1782 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1783 | |
| 1784 | CharUnits BaseOffsetInLayoutClass; |
| 1785 | |
| 1786 | if (I->isVirtual()) { |
| 1787 | if (!VBases.insert(BaseDecl)) |
| 1788 | continue; |
| 1789 | |
| 1790 | const ASTRecordLayout &LayoutClassLayout = |
| 1791 | Context.getASTRecordLayout(LayoutClass); |
| 1792 | |
| 1793 | BaseOffsetInLayoutClass = |
| 1794 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
| 1795 | } else { |
| 1796 | BaseOffsetInLayoutClass = |
| 1797 | OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl); |
| 1798 | } |
| 1799 | |
| 1800 | DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases); |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | void |
| 1805 | VTableBuilder::LayoutVTablesForVirtualBases(const CXXRecordDecl *RD, |
| 1806 | VisitedVirtualBasesSetTy &VBases) { |
| 1807 | // Itanium C++ ABI 2.5.2: |
| 1808 | // Then come the virtual base virtual tables, also in inheritance graph |
| 1809 | // order, and again excluding primary bases (which share virtual tables with |
| 1810 | // the classes for which they are primary). |
| 1811 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1812 | E = RD->bases_end(); I != E; ++I) { |
| 1813 | const CXXRecordDecl *BaseDecl = |
| 1814 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1815 | |
| 1816 | // Check if this base needs a vtable. (If it's virtual, not a primary base |
| 1817 | // of some other class, and we haven't visited it before). |
| 1818 | if (I->isVirtual() && BaseDecl->isDynamicClass() && |
| 1819 | !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) { |
| 1820 | const ASTRecordLayout &MostDerivedClassLayout = |
| 1821 | Context.getASTRecordLayout(MostDerivedClass); |
| 1822 | CharUnits BaseOffset = |
| 1823 | MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
| 1824 | |
| 1825 | const ASTRecordLayout &LayoutClassLayout = |
| 1826 | Context.getASTRecordLayout(LayoutClass); |
| 1827 | CharUnits BaseOffsetInLayoutClass = |
| 1828 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
| 1829 | |
| 1830 | LayoutPrimaryAndSecondaryVTables( |
| 1831 | BaseSubobject(BaseDecl, BaseOffset), |
| 1832 | /*BaseIsMorallyVirtual=*/true, |
| 1833 | /*BaseIsVirtualInLayoutClass=*/true, |
| 1834 | BaseOffsetInLayoutClass); |
| 1835 | } |
| 1836 | |
| 1837 | // We only need to check the base for virtual base vtables if it actually |
| 1838 | // has virtual bases. |
| 1839 | if (BaseDecl->getNumVBases()) |
| 1840 | LayoutVTablesForVirtualBases(BaseDecl, VBases); |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | /// dumpLayout - Dump the vtable layout. |
| 1845 | void VTableBuilder::dumpLayout(raw_ostream& Out) { |
| 1846 | |
| 1847 | if (isBuildingConstructorVTable()) { |
| 1848 | Out << "Construction vtable for ('"; |
| 1849 | Out << MostDerivedClass->getQualifiedNameAsString() << "', "; |
| 1850 | Out << MostDerivedClassOffset.getQuantity() << ") in '"; |
| 1851 | Out << LayoutClass->getQualifiedNameAsString(); |
| 1852 | } else { |
| 1853 | Out << "Vtable for '"; |
| 1854 | Out << MostDerivedClass->getQualifiedNameAsString(); |
| 1855 | } |
| 1856 | Out << "' (" << Components.size() << " entries).\n"; |
| 1857 | |
| 1858 | // Iterate through the address points and insert them into a new map where |
| 1859 | // they are keyed by the index and not the base object. |
| 1860 | // Since an address point can be shared by multiple subobjects, we use an |
| 1861 | // STL multimap. |
| 1862 | std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex; |
| 1863 | for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(), |
| 1864 | E = AddressPoints.end(); I != E; ++I) { |
| 1865 | const BaseSubobject& Base = I->first; |
| 1866 | uint64_t Index = I->second; |
| 1867 | |
| 1868 | AddressPointsByIndex.insert(std::make_pair(Index, Base)); |
| 1869 | } |
| 1870 | |
| 1871 | for (unsigned I = 0, E = Components.size(); I != E; ++I) { |
| 1872 | uint64_t Index = I; |
| 1873 | |
| 1874 | Out << llvm::format("%4d | ", I); |
| 1875 | |
| 1876 | const VTableComponent &Component = Components[I]; |
| 1877 | |
| 1878 | // Dump the component. |
| 1879 | switch (Component.getKind()) { |
| 1880 | |
| 1881 | case VTableComponent::CK_VCallOffset: |
| 1882 | Out << "vcall_offset (" |
| 1883 | << Component.getVCallOffset().getQuantity() |
| 1884 | << ")"; |
| 1885 | break; |
| 1886 | |
| 1887 | case VTableComponent::CK_VBaseOffset: |
| 1888 | Out << "vbase_offset (" |
| 1889 | << Component.getVBaseOffset().getQuantity() |
| 1890 | << ")"; |
| 1891 | break; |
| 1892 | |
| 1893 | case VTableComponent::CK_OffsetToTop: |
| 1894 | Out << "offset_to_top (" |
| 1895 | << Component.getOffsetToTop().getQuantity() |
| 1896 | << ")"; |
| 1897 | break; |
| 1898 | |
| 1899 | case VTableComponent::CK_RTTI: |
| 1900 | Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI"; |
| 1901 | break; |
| 1902 | |
| 1903 | case VTableComponent::CK_FunctionPointer: { |
| 1904 | const CXXMethodDecl *MD = Component.getFunctionDecl(); |
| 1905 | |
| 1906 | std::string Str = |
| 1907 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 1908 | MD); |
| 1909 | Out << Str; |
| 1910 | if (MD->isPure()) |
| 1911 | Out << " [pure]"; |
| 1912 | |
David Blaikie | d954ab4 | 2012-10-16 20:25:33 +0000 | [diff] [blame] | 1913 | if (MD->isDeleted()) |
| 1914 | Out << " [deleted]"; |
| 1915 | |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 1916 | ThunkInfo Thunk = VTableThunks.lookup(I); |
| 1917 | if (!Thunk.isEmpty()) { |
| 1918 | // If this function pointer has a return adjustment, dump it. |
| 1919 | if (!Thunk.Return.isEmpty()) { |
| 1920 | Out << "\n [return adjustment: "; |
| 1921 | Out << Thunk.Return.NonVirtual << " non-virtual"; |
| 1922 | |
| 1923 | if (Thunk.Return.VBaseOffsetOffset) { |
| 1924 | Out << ", " << Thunk.Return.VBaseOffsetOffset; |
| 1925 | Out << " vbase offset offset"; |
| 1926 | } |
| 1927 | |
| 1928 | Out << ']'; |
| 1929 | } |
| 1930 | |
| 1931 | // If this function pointer has a 'this' pointer adjustment, dump it. |
| 1932 | if (!Thunk.This.isEmpty()) { |
| 1933 | Out << "\n [this adjustment: "; |
| 1934 | Out << Thunk.This.NonVirtual << " non-virtual"; |
| 1935 | |
| 1936 | if (Thunk.This.VCallOffsetOffset) { |
| 1937 | Out << ", " << Thunk.This.VCallOffsetOffset; |
| 1938 | Out << " vcall offset offset"; |
| 1939 | } |
| 1940 | |
| 1941 | Out << ']'; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | break; |
| 1946 | } |
| 1947 | |
| 1948 | case VTableComponent::CK_CompleteDtorPointer: |
| 1949 | case VTableComponent::CK_DeletingDtorPointer: { |
| 1950 | bool IsComplete = |
| 1951 | Component.getKind() == VTableComponent::CK_CompleteDtorPointer; |
| 1952 | |
| 1953 | const CXXDestructorDecl *DD = Component.getDestructorDecl(); |
| 1954 | |
| 1955 | Out << DD->getQualifiedNameAsString(); |
| 1956 | if (IsComplete) |
| 1957 | Out << "() [complete]"; |
| 1958 | else |
| 1959 | Out << "() [deleting]"; |
| 1960 | |
| 1961 | if (DD->isPure()) |
| 1962 | Out << " [pure]"; |
| 1963 | |
| 1964 | ThunkInfo Thunk = VTableThunks.lookup(I); |
| 1965 | if (!Thunk.isEmpty()) { |
| 1966 | // If this destructor has a 'this' pointer adjustment, dump it. |
| 1967 | if (!Thunk.This.isEmpty()) { |
| 1968 | Out << "\n [this adjustment: "; |
| 1969 | Out << Thunk.This.NonVirtual << " non-virtual"; |
| 1970 | |
| 1971 | if (Thunk.This.VCallOffsetOffset) { |
| 1972 | Out << ", " << Thunk.This.VCallOffsetOffset; |
| 1973 | Out << " vcall offset offset"; |
| 1974 | } |
| 1975 | |
| 1976 | Out << ']'; |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | break; |
| 1981 | } |
| 1982 | |
| 1983 | case VTableComponent::CK_UnusedFunctionPointer: { |
| 1984 | const CXXMethodDecl *MD = Component.getUnusedFunctionDecl(); |
| 1985 | |
| 1986 | std::string Str = |
| 1987 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 1988 | MD); |
| 1989 | Out << "[unused] " << Str; |
| 1990 | if (MD->isPure()) |
| 1991 | Out << " [pure]"; |
| 1992 | } |
| 1993 | |
| 1994 | } |
| 1995 | |
| 1996 | Out << '\n'; |
| 1997 | |
| 1998 | // Dump the next address point. |
| 1999 | uint64_t NextIndex = Index + 1; |
| 2000 | if (AddressPointsByIndex.count(NextIndex)) { |
| 2001 | if (AddressPointsByIndex.count(NextIndex) == 1) { |
| 2002 | const BaseSubobject &Base = |
| 2003 | AddressPointsByIndex.find(NextIndex)->second; |
| 2004 | |
| 2005 | Out << " -- (" << Base.getBase()->getQualifiedNameAsString(); |
| 2006 | Out << ", " << Base.getBaseOffset().getQuantity(); |
| 2007 | Out << ") vtable address --\n"; |
| 2008 | } else { |
| 2009 | CharUnits BaseOffset = |
| 2010 | AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset(); |
| 2011 | |
| 2012 | // We store the class names in a set to get a stable order. |
| 2013 | std::set<std::string> ClassNames; |
| 2014 | for (std::multimap<uint64_t, BaseSubobject>::const_iterator I = |
| 2015 | AddressPointsByIndex.lower_bound(NextIndex), E = |
| 2016 | AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) { |
| 2017 | assert(I->second.getBaseOffset() == BaseOffset && |
| 2018 | "Invalid base offset!"); |
| 2019 | const CXXRecordDecl *RD = I->second.getBase(); |
| 2020 | ClassNames.insert(RD->getQualifiedNameAsString()); |
| 2021 | } |
| 2022 | |
| 2023 | for (std::set<std::string>::const_iterator I = ClassNames.begin(), |
| 2024 | E = ClassNames.end(); I != E; ++I) { |
| 2025 | Out << " -- (" << *I; |
| 2026 | Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n"; |
| 2027 | } |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | Out << '\n'; |
| 2033 | |
| 2034 | if (isBuildingConstructorVTable()) |
| 2035 | return; |
| 2036 | |
| 2037 | if (MostDerivedClass->getNumVBases()) { |
| 2038 | // We store the virtual base class names and their offsets in a map to get |
| 2039 | // a stable order. |
| 2040 | |
| 2041 | std::map<std::string, CharUnits> ClassNamesAndOffsets; |
| 2042 | for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(), |
| 2043 | E = VBaseOffsetOffsets.end(); I != E; ++I) { |
| 2044 | std::string ClassName = I->first->getQualifiedNameAsString(); |
| 2045 | CharUnits OffsetOffset = I->second; |
| 2046 | ClassNamesAndOffsets.insert( |
| 2047 | std::make_pair(ClassName, OffsetOffset)); |
| 2048 | } |
| 2049 | |
| 2050 | Out << "Virtual base offset offsets for '"; |
| 2051 | Out << MostDerivedClass->getQualifiedNameAsString() << "' ("; |
| 2052 | Out << ClassNamesAndOffsets.size(); |
| 2053 | Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n"; |
| 2054 | |
| 2055 | for (std::map<std::string, CharUnits>::const_iterator I = |
| 2056 | ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end(); |
| 2057 | I != E; ++I) |
| 2058 | Out << " " << I->first << " | " << I->second.getQuantity() << '\n'; |
| 2059 | |
| 2060 | Out << "\n"; |
| 2061 | } |
| 2062 | |
| 2063 | if (!Thunks.empty()) { |
| 2064 | // We store the method names in a map to get a stable order. |
| 2065 | std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls; |
| 2066 | |
| 2067 | for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end(); |
| 2068 | I != E; ++I) { |
| 2069 | const CXXMethodDecl *MD = I->first; |
| 2070 | std::string MethodName = |
| 2071 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 2072 | MD); |
| 2073 | |
| 2074 | MethodNamesAndDecls.insert(std::make_pair(MethodName, MD)); |
| 2075 | } |
| 2076 | |
| 2077 | for (std::map<std::string, const CXXMethodDecl *>::const_iterator I = |
| 2078 | MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end(); |
| 2079 | I != E; ++I) { |
| 2080 | const std::string &MethodName = I->first; |
| 2081 | const CXXMethodDecl *MD = I->second; |
| 2082 | |
| 2083 | ThunkInfoVectorTy ThunksVector = Thunks[MD]; |
| 2084 | std::sort(ThunksVector.begin(), ThunksVector.end()); |
| 2085 | |
| 2086 | Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size(); |
| 2087 | Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n"; |
| 2088 | |
| 2089 | for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) { |
| 2090 | const ThunkInfo &Thunk = ThunksVector[I]; |
| 2091 | |
| 2092 | Out << llvm::format("%4d | ", I); |
| 2093 | |
| 2094 | // If this function pointer has a return pointer adjustment, dump it. |
| 2095 | if (!Thunk.Return.isEmpty()) { |
| 2096 | Out << "return adjustment: " << Thunk.This.NonVirtual; |
| 2097 | Out << " non-virtual"; |
| 2098 | if (Thunk.Return.VBaseOffsetOffset) { |
| 2099 | Out << ", " << Thunk.Return.VBaseOffsetOffset; |
| 2100 | Out << " vbase offset offset"; |
| 2101 | } |
| 2102 | |
| 2103 | if (!Thunk.This.isEmpty()) |
| 2104 | Out << "\n "; |
| 2105 | } |
| 2106 | |
| 2107 | // If this function pointer has a 'this' pointer adjustment, dump it. |
| 2108 | if (!Thunk.This.isEmpty()) { |
| 2109 | Out << "this adjustment: "; |
| 2110 | Out << Thunk.This.NonVirtual << " non-virtual"; |
| 2111 | |
| 2112 | if (Thunk.This.VCallOffsetOffset) { |
| 2113 | Out << ", " << Thunk.This.VCallOffsetOffset; |
| 2114 | Out << " vcall offset offset"; |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | Out << '\n'; |
| 2119 | } |
| 2120 | |
| 2121 | Out << '\n'; |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | // Compute the vtable indices for all the member functions. |
| 2126 | // Store them in a map keyed by the index so we'll get a sorted table. |
| 2127 | std::map<uint64_t, std::string> IndicesMap; |
| 2128 | |
| 2129 | for (CXXRecordDecl::method_iterator i = MostDerivedClass->method_begin(), |
| 2130 | e = MostDerivedClass->method_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2131 | const CXXMethodDecl *MD = *i; |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2132 | |
| 2133 | // We only want virtual member functions. |
| 2134 | if (!MD->isVirtual()) |
| 2135 | continue; |
| 2136 | |
| 2137 | std::string MethodName = |
| 2138 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 2139 | MD); |
| 2140 | |
| 2141 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2142 | // FIXME: Should add a layer of abstraction for vtable generation. |
| 2143 | if (!isMicrosoftABI()) { |
| 2144 | IndicesMap[VTables.getMethodVTableIndex(GlobalDecl(DD, Dtor_Complete))] |
| 2145 | = MethodName + " [complete]"; |
| 2146 | IndicesMap[VTables.getMethodVTableIndex(GlobalDecl(DD, Dtor_Deleting))] |
| 2147 | = MethodName + " [deleting]"; |
| 2148 | } else { |
| 2149 | IndicesMap[VTables.getMethodVTableIndex(GlobalDecl(DD, Dtor_Complete))] |
| 2150 | = MethodName; |
| 2151 | } |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2152 | } else { |
| 2153 | IndicesMap[VTables.getMethodVTableIndex(MD)] = MethodName; |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | // Print the vtable indices for all the member functions. |
| 2158 | if (!IndicesMap.empty()) { |
| 2159 | Out << "VTable indices for '"; |
| 2160 | Out << MostDerivedClass->getQualifiedNameAsString(); |
| 2161 | Out << "' (" << IndicesMap.size() << " entries).\n"; |
| 2162 | |
| 2163 | for (std::map<uint64_t, std::string>::const_iterator I = IndicesMap.begin(), |
| 2164 | E = IndicesMap.end(); I != E; ++I) { |
| 2165 | uint64_t VTableIndex = I->first; |
| 2166 | const std::string &MethodName = I->second; |
| 2167 | |
Benjamin Kramer | 79a5501 | 2012-03-10 02:06:27 +0000 | [diff] [blame] | 2168 | Out << llvm::format(" %4" PRIu64 " | ", VTableIndex) << MethodName |
| 2169 | << '\n'; |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | Out << '\n'; |
| 2174 | } |
| 2175 | |
| 2176 | } |
| 2177 | |
| 2178 | VTableLayout::VTableLayout(uint64_t NumVTableComponents, |
| 2179 | const VTableComponent *VTableComponents, |
| 2180 | uint64_t NumVTableThunks, |
| 2181 | const VTableThunkTy *VTableThunks, |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2182 | const AddressPointsMapTy &AddressPoints, |
| 2183 | bool IsMicrosoftABI) |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2184 | : NumVTableComponents(NumVTableComponents), |
| 2185 | VTableComponents(new VTableComponent[NumVTableComponents]), |
| 2186 | NumVTableThunks(NumVTableThunks), |
| 2187 | VTableThunks(new VTableThunkTy[NumVTableThunks]), |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2188 | AddressPoints(AddressPoints), |
| 2189 | IsMicrosoftABI(IsMicrosoftABI) { |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2190 | std::copy(VTableComponents, VTableComponents+NumVTableComponents, |
Benjamin Kramer | 8fb9fb6 | 2012-04-14 14:13:43 +0000 | [diff] [blame] | 2191 | this->VTableComponents.get()); |
| 2192 | std::copy(VTableThunks, VTableThunks+NumVTableThunks, |
| 2193 | this->VTableThunks.get()); |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Benjamin Kramer | 8fb9fb6 | 2012-04-14 14:13:43 +0000 | [diff] [blame] | 2196 | VTableLayout::~VTableLayout() { } |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2197 | |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2198 | VTableContext::VTableContext(ASTContext &Context) |
| 2199 | : Context(Context), |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame^] | 2200 | IsMicrosoftABI(Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 2201 | } |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2202 | |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2203 | VTableContext::~VTableContext() { |
| 2204 | llvm::DeleteContainerSeconds(VTableLayouts); |
| 2205 | } |
| 2206 | |
| 2207 | static void |
| 2208 | CollectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context, |
| 2209 | VTableBuilder::PrimaryBasesSetVectorTy &PrimaryBases) { |
| 2210 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 2211 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 2212 | |
| 2213 | if (!PrimaryBase) |
| 2214 | return; |
| 2215 | |
| 2216 | CollectPrimaryBases(PrimaryBase, Context, PrimaryBases); |
| 2217 | |
| 2218 | if (!PrimaryBases.insert(PrimaryBase)) |
| 2219 | llvm_unreachable("Found a duplicate primary base!"); |
| 2220 | } |
| 2221 | |
| 2222 | void VTableContext::ComputeMethodVTableIndices(const CXXRecordDecl *RD) { |
| 2223 | |
| 2224 | // Itanium C++ ABI 2.5.2: |
| 2225 | // The order of the virtual function pointers in a virtual table is the |
| 2226 | // order of declaration of the corresponding member functions in the class. |
| 2227 | // |
| 2228 | // There is an entry for any virtual function declared in a class, |
| 2229 | // whether it is a new function or overrides a base class function, |
| 2230 | // unless it overrides a function from the primary base, and conversion |
| 2231 | // between their return types does not require an adjustment. |
| 2232 | |
| 2233 | int64_t CurrentIndex = 0; |
| 2234 | |
| 2235 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 2236 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 2237 | |
| 2238 | if (PrimaryBase) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2239 | assert(PrimaryBase->isCompleteDefinition() && |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2240 | "Should have the definition decl of the primary base!"); |
| 2241 | |
| 2242 | // Since the record decl shares its vtable pointer with the primary base |
| 2243 | // we need to start counting at the end of the primary base's vtable. |
| 2244 | CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase); |
| 2245 | } |
| 2246 | |
| 2247 | // Collect all the primary bases, so we can check whether methods override |
| 2248 | // a method from the base. |
| 2249 | VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases; |
| 2250 | CollectPrimaryBases(RD, Context, PrimaryBases); |
| 2251 | |
| 2252 | const CXXDestructorDecl *ImplicitVirtualDtor = 0; |
| 2253 | |
| 2254 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 2255 | e = RD->method_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2256 | const CXXMethodDecl *MD = *i; |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2257 | |
| 2258 | // We only want virtual methods. |
| 2259 | if (!MD->isVirtual()) |
| 2260 | continue; |
| 2261 | |
| 2262 | // Check if this method overrides a method in the primary base. |
| 2263 | if (const CXXMethodDecl *OverriddenMD = |
| 2264 | FindNearestOverriddenMethod(MD, PrimaryBases)) { |
| 2265 | // Check if converting from the return type of the method to the |
| 2266 | // return type of the overridden method requires conversion. |
| 2267 | if (ComputeReturnAdjustmentBaseOffset(Context, MD, |
| 2268 | OverriddenMD).isEmpty()) { |
| 2269 | // This index is shared between the index in the vtable of the primary |
| 2270 | // base class. |
| 2271 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 2272 | const CXXDestructorDecl *OverriddenDD = |
| 2273 | cast<CXXDestructorDecl>(OverriddenMD); |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2274 | |
| 2275 | if (!isMicrosoftABI()) { |
| 2276 | // Add both the complete and deleting entries. |
| 2277 | MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = |
| 2278 | getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Complete)); |
| 2279 | MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = |
| 2280 | getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting)); |
| 2281 | } else { |
| 2282 | MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = |
| 2283 | getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Complete)); |
| 2284 | } |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2285 | } else { |
| 2286 | MethodVTableIndices[MD] = getMethodVTableIndex(OverriddenMD); |
| 2287 | } |
| 2288 | |
| 2289 | // We don't need to add an entry for this method. |
| 2290 | continue; |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 2295 | if (MD->isImplicit()) { |
| 2296 | assert(!ImplicitVirtualDtor && |
| 2297 | "Did already see an implicit virtual dtor!"); |
| 2298 | ImplicitVirtualDtor = DD; |
| 2299 | continue; |
| 2300 | } |
| 2301 | |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2302 | if (!isMicrosoftABI()) { |
| 2303 | // Add the complete dtor. |
| 2304 | MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++; |
| 2305 | |
| 2306 | // Add the deleting dtor. |
| 2307 | MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++; |
| 2308 | } else { |
| 2309 | // Add only the deleting dtor. |
| 2310 | // FIXME: The virtual destructors are handled differently in MS ABI, |
| 2311 | // we should add such a support later. For now, put the complete |
| 2312 | // destructor into the vftable indices. |
| 2313 | MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++; |
| 2314 | } |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2315 | } else { |
| 2316 | // Add the entry. |
| 2317 | MethodVTableIndices[MD] = CurrentIndex++; |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | if (ImplicitVirtualDtor) { |
| 2322 | // Itanium C++ ABI 2.5.2: |
| 2323 | // If a class has an implicitly-defined virtual destructor, |
| 2324 | // its entries come after the declared virtual function pointers. |
| 2325 | |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2326 | if (isMicrosoftABI()) { |
| 2327 | ErrorUnsupported("implicit virtual destructor in the Microsoft ABI", |
| 2328 | ImplicitVirtualDtor->getLocation()); |
| 2329 | } |
| 2330 | |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2331 | // Add the complete dtor. |
| 2332 | MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] = |
| 2333 | CurrentIndex++; |
| 2334 | |
| 2335 | // Add the deleting dtor. |
| 2336 | MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] = |
| 2337 | CurrentIndex++; |
| 2338 | } |
| 2339 | |
| 2340 | NumVirtualFunctionPointers[RD] = CurrentIndex; |
| 2341 | } |
| 2342 | |
| 2343 | uint64_t VTableContext::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) { |
| 2344 | llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 2345 | NumVirtualFunctionPointers.find(RD); |
| 2346 | if (I != NumVirtualFunctionPointers.end()) |
| 2347 | return I->second; |
| 2348 | |
| 2349 | ComputeMethodVTableIndices(RD); |
| 2350 | |
| 2351 | I = NumVirtualFunctionPointers.find(RD); |
| 2352 | assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!"); |
| 2353 | return I->second; |
| 2354 | } |
| 2355 | |
| 2356 | uint64_t VTableContext::getMethodVTableIndex(GlobalDecl GD) { |
| 2357 | MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD); |
| 2358 | if (I != MethodVTableIndices.end()) |
| 2359 | return I->second; |
| 2360 | |
| 2361 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
| 2362 | |
| 2363 | ComputeMethodVTableIndices(RD); |
| 2364 | |
| 2365 | I = MethodVTableIndices.find(GD); |
| 2366 | assert(I != MethodVTableIndices.end() && "Did not find index!"); |
| 2367 | return I->second; |
| 2368 | } |
| 2369 | |
| 2370 | CharUnits |
| 2371 | VTableContext::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, |
| 2372 | const CXXRecordDecl *VBase) { |
| 2373 | ClassPairTy ClassPair(RD, VBase); |
| 2374 | |
| 2375 | VirtualBaseClassOffsetOffsetsMapTy::iterator I = |
| 2376 | VirtualBaseClassOffsetOffsets.find(ClassPair); |
| 2377 | if (I != VirtualBaseClassOffsetOffsets.end()) |
| 2378 | return I->second; |
| 2379 | |
| 2380 | VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0, |
| 2381 | BaseSubobject(RD, CharUnits::Zero()), |
| 2382 | /*BaseIsVirtual=*/false, |
| 2383 | /*OffsetInLayoutClass=*/CharUnits::Zero()); |
| 2384 | |
| 2385 | for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I = |
| 2386 | Builder.getVBaseOffsetOffsets().begin(), |
| 2387 | E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) { |
| 2388 | // Insert all types. |
| 2389 | ClassPairTy ClassPair(RD, I->first); |
| 2390 | |
| 2391 | VirtualBaseClassOffsetOffsets.insert( |
| 2392 | std::make_pair(ClassPair, I->second)); |
| 2393 | } |
| 2394 | |
| 2395 | I = VirtualBaseClassOffsetOffsets.find(ClassPair); |
| 2396 | assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!"); |
| 2397 | |
| 2398 | return I->second; |
| 2399 | } |
| 2400 | |
| 2401 | static VTableLayout *CreateVTableLayout(const VTableBuilder &Builder) { |
| 2402 | SmallVector<VTableLayout::VTableThunkTy, 1> |
| 2403 | VTableThunks(Builder.vtable_thunks_begin(), Builder.vtable_thunks_end()); |
| 2404 | std::sort(VTableThunks.begin(), VTableThunks.end()); |
| 2405 | |
| 2406 | return new VTableLayout(Builder.getNumVTableComponents(), |
| 2407 | Builder.vtable_component_begin(), |
| 2408 | VTableThunks.size(), |
| 2409 | VTableThunks.data(), |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2410 | Builder.getAddressPoints(), |
| 2411 | Builder.isMicrosoftABI()); |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | void VTableContext::ComputeVTableRelatedInformation(const CXXRecordDecl *RD) { |
| 2415 | const VTableLayout *&Entry = VTableLayouts[RD]; |
| 2416 | |
| 2417 | // Check if we've computed this information before. |
| 2418 | if (Entry) |
| 2419 | return; |
| 2420 | |
| 2421 | VTableBuilder Builder(*this, RD, CharUnits::Zero(), |
| 2422 | /*MostDerivedClassIsVirtual=*/0, RD); |
| 2423 | Entry = CreateVTableLayout(Builder); |
| 2424 | |
| 2425 | // Add the known thunks. |
| 2426 | Thunks.insert(Builder.thunks_begin(), Builder.thunks_end()); |
| 2427 | |
| 2428 | // If we don't have the vbase information for this class, insert it. |
| 2429 | // getVirtualBaseOffsetOffset will compute it separately without computing |
| 2430 | // the rest of the vtable related information. |
| 2431 | if (!RD->getNumVBases()) |
| 2432 | return; |
| 2433 | |
| 2434 | const RecordType *VBaseRT = |
| 2435 | RD->vbases_begin()->getType()->getAs<RecordType>(); |
| 2436 | const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl()); |
| 2437 | |
| 2438 | if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase))) |
| 2439 | return; |
| 2440 | |
| 2441 | for (VTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I = |
| 2442 | Builder.getVBaseOffsetOffsets().begin(), |
| 2443 | E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) { |
| 2444 | // Insert all types. |
| 2445 | ClassPairTy ClassPair(RD, I->first); |
| 2446 | |
| 2447 | VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second)); |
| 2448 | } |
| 2449 | } |
| 2450 | |
Timur Iskhodzhanov | 649c731 | 2013-01-21 13:02:41 +0000 | [diff] [blame] | 2451 | void VTableContext::ErrorUnsupported(StringRef Feature, |
| 2452 | SourceLocation Location) { |
| 2453 | clang::DiagnosticsEngine &Diags = Context.getDiagnostics(); |
| 2454 | unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 2455 | "v-table layout for %0 is not supported yet"); |
| 2456 | Diags.Report(Context.getFullLoc(Location), DiagID) << Feature; |
| 2457 | } |
| 2458 | |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 2459 | VTableLayout *VTableContext::createConstructionVTableLayout( |
| 2460 | const CXXRecordDecl *MostDerivedClass, |
| 2461 | CharUnits MostDerivedClassOffset, |
| 2462 | bool MostDerivedClassIsVirtual, |
| 2463 | const CXXRecordDecl *LayoutClass) { |
| 2464 | VTableBuilder Builder(*this, MostDerivedClass, MostDerivedClassOffset, |
| 2465 | MostDerivedClassIsVirtual, LayoutClass); |
| 2466 | return CreateVTableLayout(Builder); |
| 2467 | } |