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