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