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