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