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. |
| 1404 | if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) |
| 1405 | continue; |
| 1406 | |
Anders Carlsson | 617def3 | 2010-03-12 05:28:07 +0000 | [diff] [blame] | 1407 | ThisAdjustment ThisAdjustment = |
| 1408 | ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider); |
| 1409 | |
| 1410 | if (ThisAdjustment.isEmpty()) |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1411 | continue; |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1412 | |
Anders Carlsson | cd4e637 | 2010-02-27 18:09:40 +0000 | [diff] [blame] | 1413 | // Add it. |
Anders Carlsson | 3a5df3f | 2010-03-23 04:44:10 +0000 | [diff] [blame] | 1414 | VTableThunks[VtableIndex].This = ThisAdjustment; |
Anders Carlsson | 617def3 | 2010-03-12 05:28:07 +0000 | [diff] [blame] | 1415 | |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1416 | if (isa<CXXDestructorDecl>(MD)) { |
| 1417 | // Add an adjustment for the deleting destructor as well. |
Anders Carlsson | 3a5df3f | 2010-03-23 04:44:10 +0000 | [diff] [blame] | 1418 | VTableThunks[VtableIndex + 1].This = ThisAdjustment; |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | /// Clear the method info map. |
| 1423 | MethodInfoMap.clear(); |
Anders Carlsson | 2539421 | 2010-03-22 15:47:01 +0000 | [diff] [blame] | 1424 | |
| 1425 | if (isBuildingConstructorVtable()) { |
| 1426 | // We don't need to store thunk information for construction vtables. |
| 1427 | return; |
| 1428 | } |
| 1429 | |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 1430 | for (VtableThunksMapTy::const_iterator I = VTableThunks.begin(), |
Anders Carlsson | 3a5df3f | 2010-03-23 04:44:10 +0000 | [diff] [blame] | 1431 | E = VTableThunks.end(); I != E; ++I) { |
Anders Carlsson | 2539421 | 2010-03-22 15:47:01 +0000 | [diff] [blame] | 1432 | const VtableComponent &Component = Components[I->first]; |
| 1433 | const ThunkInfo &Thunk = I->second; |
Anders Carlsson | bca5d37 | 2010-03-22 20:06:40 +0000 | [diff] [blame] | 1434 | const CXXMethodDecl *MD; |
Anders Carlsson | 2539421 | 2010-03-22 15:47:01 +0000 | [diff] [blame] | 1435 | |
| 1436 | switch (Component.getKind()) { |
| 1437 | default: |
| 1438 | llvm_unreachable("Unexpected vtable component kind!"); |
| 1439 | case VtableComponent::CK_FunctionPointer: |
Anders Carlsson | bca5d37 | 2010-03-22 20:06:40 +0000 | [diff] [blame] | 1440 | MD = Component.getFunctionDecl(); |
Anders Carlsson | 2539421 | 2010-03-22 15:47:01 +0000 | [diff] [blame] | 1441 | break; |
| 1442 | case VtableComponent::CK_CompleteDtorPointer: |
Anders Carlsson | bca5d37 | 2010-03-22 20:06:40 +0000 | [diff] [blame] | 1443 | MD = Component.getDestructorDecl(); |
Anders Carlsson | 2539421 | 2010-03-22 15:47:01 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | case VtableComponent::CK_DeletingDtorPointer: |
| 1446 | // 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] | 1447 | continue; |
Anders Carlsson | 2539421 | 2010-03-22 15:47:01 +0000 | [diff] [blame] | 1448 | } |
Anders Carlsson | bca5d37 | 2010-03-22 20:06:40 +0000 | [diff] [blame] | 1449 | |
| 1450 | if (MD->getParent() == MostDerivedClass) |
| 1451 | AddThunk(MD, Thunk); |
Anders Carlsson | 2539421 | 2010-03-22 15:47:01 +0000 | [diff] [blame] | 1452 | } |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Anders Carlsson | 80d8d7d | 2010-03-23 15:13:06 +0000 | [diff] [blame] | 1455 | ReturnAdjustment VtableBuilder::ComputeReturnAdjustment(BaseOffset Offset) { |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1456 | ReturnAdjustment Adjustment; |
| 1457 | |
| 1458 | if (!Offset.isEmpty()) { |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 1459 | if (Offset.VirtualBase) { |
| 1460 | // Get the virtual base offset offset. |
Anders Carlsson | 1972ced | 2010-03-11 07:00:45 +0000 | [diff] [blame] | 1461 | if (Offset.DerivedClass == MostDerivedClass) { |
| 1462 | // We can get the offset offset directly from our map. |
| 1463 | Adjustment.VBaseOffsetOffset = |
| 1464 | VBaseOffsetOffsets.lookup(Offset.VirtualBase); |
| 1465 | } else { |
| 1466 | Adjustment.VBaseOffsetOffset = |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1467 | VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass, |
| 1468 | Offset.VirtualBase); |
Anders Carlsson | 1972ced | 2010-03-11 07:00:45 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Anders Carlsson | c89a7cf | 2010-03-11 16:06:20 +0000 | [diff] [blame] | 1471 | // FIXME: Once the assert in getVirtualBaseOffsetOffset is back again, |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 1472 | // we can get rid of this assert. |
| 1473 | assert(Adjustment.VBaseOffsetOffset != 0 && |
Anders Carlsson | c89a7cf | 2010-03-11 16:06:20 +0000 | [diff] [blame] | 1474 | "Invalid vbase offset offset!"); |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 1475 | } |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1476 | |
| 1477 | Adjustment.NonVirtual = Offset.NonVirtualOffset; |
| 1478 | } |
| 1479 | |
| 1480 | return Adjustment; |
| 1481 | } |
| 1482 | |
Anders Carlsson | 379017f | 2010-02-27 19:57:44 +0000 | [diff] [blame] | 1483 | BaseOffset |
| 1484 | VtableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base, |
| 1485 | BaseSubobject Derived) const { |
| 1486 | const CXXRecordDecl *BaseRD = Base.getBase(); |
| 1487 | const CXXRecordDecl *DerivedRD = Derived.getBase(); |
| 1488 | |
| 1489 | CXXBasePaths Paths(/*FindAmbiguities=*/true, |
| 1490 | /*RecordPaths=*/true, /*DetectVirtual=*/true); |
| 1491 | |
| 1492 | if (!const_cast<CXXRecordDecl *>(DerivedRD)-> |
| 1493 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) { |
| 1494 | assert(false && "Class must be derived from the passed in base class!"); |
| 1495 | return BaseOffset(); |
| 1496 | } |
| 1497 | |
| 1498 | // We have to go through all the paths, and see which one leads us to the |
| 1499 | // right base subobject. |
| 1500 | for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end(); |
| 1501 | I != E; ++I) { |
| 1502 | BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I); |
| 1503 | |
| 1504 | // FIXME: Should not use * 8 here. |
| 1505 | uint64_t OffsetToBaseSubobject = Offset.NonVirtualOffset * 8; |
| 1506 | |
| 1507 | if (Offset.VirtualBase) { |
| 1508 | // If we have a virtual base class, the non-virtual offset is relative |
| 1509 | // to the virtual base class offset. |
Anders Carlsson | 2bc1d3a | 2010-03-10 21:25:37 +0000 | [diff] [blame] | 1510 | const ASTRecordLayout &LayoutClassLayout = |
| 1511 | Context.getASTRecordLayout(LayoutClass); |
Anders Carlsson | 379017f | 2010-02-27 19:57:44 +0000 | [diff] [blame] | 1512 | |
| 1513 | /// Get the virtual base offset, relative to the most derived class |
| 1514 | /// layout. |
| 1515 | OffsetToBaseSubobject += |
Anders Carlsson | 2bc1d3a | 2010-03-10 21:25:37 +0000 | [diff] [blame] | 1516 | LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase); |
Anders Carlsson | 379017f | 2010-02-27 19:57:44 +0000 | [diff] [blame] | 1517 | } else { |
| 1518 | // Otherwise, the non-virtual offset is relative to the derived class |
| 1519 | // offset. |
| 1520 | OffsetToBaseSubobject += Derived.getBaseOffset(); |
| 1521 | } |
| 1522 | |
| 1523 | // Check if this path gives us the right base subobject. |
| 1524 | if (OffsetToBaseSubobject == Base.getBaseOffset()) { |
| 1525 | // Since we're going from the base class _to_ the derived class, we'll |
| 1526 | // invert the non-virtual offset here. |
| 1527 | Offset.NonVirtualOffset = -Offset.NonVirtualOffset; |
| 1528 | return Offset; |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | return BaseOffset(); |
| 1533 | } |
| 1534 | |
Anders Carlsson | 80d8d7d | 2010-03-23 15:13:06 +0000 | [diff] [blame] | 1535 | ThisAdjustment |
Anders Carlsson | 617def3 | 2010-03-12 05:28:07 +0000 | [diff] [blame] | 1536 | VtableBuilder::ComputeThisAdjustment(const CXXMethodDecl *MD, |
| 1537 | uint64_t BaseOffsetInLayoutClass, |
| 1538 | FinalOverriders::OverriderInfo Overrider) { |
Anders Carlsson | 617def3 | 2010-03-12 05:28:07 +0000 | [diff] [blame] | 1539 | // Ignore adjustments for pure virtual member functions. |
| 1540 | if (Overrider.Method->isPure()) |
| 1541 | return ThisAdjustment(); |
| 1542 | |
| 1543 | BaseSubobject OverriddenBaseSubobject(MD->getParent(), |
| 1544 | BaseOffsetInLayoutClass); |
| 1545 | |
| 1546 | BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(), |
| 1547 | Overrider.Offset); |
| 1548 | |
| 1549 | // Compute the adjustment offset. |
| 1550 | BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject, |
| 1551 | OverriderBaseSubobject); |
| 1552 | if (Offset.isEmpty()) |
| 1553 | return ThisAdjustment(); |
| 1554 | |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 1555 | ThisAdjustment Adjustment; |
| 1556 | |
Anders Carlsson | 617def3 | 2010-03-12 05:28:07 +0000 | [diff] [blame] | 1557 | if (Offset.VirtualBase) { |
| 1558 | // Get the vcall offset map for this virtual base. |
| 1559 | VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase]; |
Anders Carlsson | eb577d0 | 2010-02-18 17:26:40 +0000 | [diff] [blame] | 1560 | |
Anders Carlsson | 617def3 | 2010-03-12 05:28:07 +0000 | [diff] [blame] | 1561 | if (VCallOffsets.empty()) { |
| 1562 | // We don't have vcall offsets for this virtual base, go ahead and |
| 1563 | // build them. |
| 1564 | VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass, |
| 1565 | /*FinalOverriders=*/0, |
| 1566 | BaseSubobject(Offset.VirtualBase, 0), |
| 1567 | /*BaseIsVirtual=*/true, |
| 1568 | /*OffsetInLayoutClass=*/0); |
| 1569 | |
| 1570 | VCallOffsets = Builder.getVCallOffsets(); |
| 1571 | } |
| 1572 | |
| 1573 | Adjustment.VCallOffsetOffset = VCallOffsets.getVCallOffsetOffset(MD); |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 1574 | } |
Anders Carlsson | 617def3 | 2010-03-12 05:28:07 +0000 | [diff] [blame] | 1575 | |
| 1576 | // Set the non-virtual part of the adjustment. |
| 1577 | Adjustment.NonVirtual = Offset.NonVirtualOffset; |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 1578 | |
| 1579 | return Adjustment; |
| 1580 | } |
Anders Carlsson | 617def3 | 2010-03-12 05:28:07 +0000 | [diff] [blame] | 1581 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1582 | void |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1583 | VtableBuilder::AddMethod(const CXXMethodDecl *MD, |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1584 | ReturnAdjustment ReturnAdjustment) { |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1585 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1586 | assert(ReturnAdjustment.isEmpty() && |
| 1587 | "Destructor can't have return adjustment!"); |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 1588 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1589 | // Add both the complete destructor and the deleting destructor. |
| 1590 | Components.push_back(VtableComponent::MakeCompleteDtor(DD)); |
| 1591 | Components.push_back(VtableComponent::MakeDeletingDtor(DD)); |
| 1592 | } else { |
| 1593 | // Add the return adjustment if necessary. |
| 1594 | if (!ReturnAdjustment.isEmpty()) |
Anders Carlsson | 3a5df3f | 2010-03-23 04:44:10 +0000 | [diff] [blame] | 1595 | VTableThunks[Components.size()].Return = ReturnAdjustment; |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1596 | |
| 1597 | // Add the function. |
| 1598 | Components.push_back(VtableComponent::MakeFunction(MD)); |
| 1599 | } |
| 1600 | } |
| 1601 | |
Anders Carlsson | c784ba2 | 2010-02-27 06:38:03 +0000 | [diff] [blame] | 1602 | /// OverridesIndirectMethodInBase - Return whether the given member function |
| 1603 | /// overrides any methods in the set of given bases. |
| 1604 | /// Unlike OverridesMethodInBase, this checks "overriders of overriders". |
| 1605 | /// For example, if we have: |
| 1606 | /// |
| 1607 | /// struct A { virtual void f(); } |
| 1608 | /// struct B : A { virtual void f(); } |
| 1609 | /// struct C : B { virtual void f(); } |
| 1610 | /// |
| 1611 | /// OverridesIndirectMethodInBase will return true if given C::f as the method |
| 1612 | /// and { A } as the set of bases. |
| 1613 | static bool |
| 1614 | OverridesIndirectMethodInBases(const CXXMethodDecl *MD, |
Anders Carlsson | b9021e9 | 2010-02-27 16:18:19 +0000 | [diff] [blame] | 1615 | VtableBuilder::PrimaryBasesSetVectorTy &Bases) { |
Anders Carlsson | c784ba2 | 2010-02-27 06:38:03 +0000 | [diff] [blame] | 1616 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1617 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1618 | const CXXMethodDecl *OverriddenMD = *I; |
| 1619 | const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent(); |
| 1620 | assert(OverriddenMD->isCanonicalDecl() && |
| 1621 | "Should have the canonical decl of the overridden RD!"); |
| 1622 | |
| 1623 | if (Bases.count(OverriddenRD)) |
| 1624 | return true; |
| 1625 | |
| 1626 | // Check "indirect overriders". |
| 1627 | if (OverridesIndirectMethodInBases(OverriddenMD, Bases)) |
| 1628 | return true; |
| 1629 | } |
| 1630 | |
| 1631 | return false; |
| 1632 | } |
| 1633 | |
Anders Carlsson | 464c8f1 | 2010-02-23 04:26:39 +0000 | [diff] [blame] | 1634 | bool |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1635 | VtableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider, |
| 1636 | uint64_t BaseOffsetInLayoutClass, |
| 1637 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 1638 | uint64_t FirstBaseOffsetInLayoutClass) const { |
Anders Carlsson | 464c8f1 | 2010-02-23 04:26:39 +0000 | [diff] [blame] | 1639 | // If the base and the first base in the primary base chain have the same |
| 1640 | // offsets, then this overrider will be used. |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1641 | if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass) |
Anders Carlsson | f9fbc71 | 2010-02-23 06:34:44 +0000 | [diff] [blame] | 1642 | return true; |
| 1643 | |
| 1644 | // We know now that Base (or a direct or indirect base of it) is a primary |
| 1645 | // base in part of the class hierarchy, but not a primary base in the most |
| 1646 | // derived class. |
| 1647 | |
| 1648 | // If the overrider is the first base in the primary base chain, we know |
| 1649 | // that the overrider will be used. |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1650 | if (Overrider->getParent() == FirstBaseInPrimaryBaseChain) |
Anders Carlsson | 464c8f1 | 2010-02-23 04:26:39 +0000 | [diff] [blame] | 1651 | return true; |
| 1652 | |
Anders Carlsson | b9021e9 | 2010-02-27 16:18:19 +0000 | [diff] [blame] | 1653 | VtableBuilder::PrimaryBasesSetVectorTy PrimaryBases; |
Anders Carlsson | f9fbc71 | 2010-02-23 06:34:44 +0000 | [diff] [blame] | 1654 | |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1655 | const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain; |
Anders Carlsson | f9fbc71 | 2010-02-23 06:34:44 +0000 | [diff] [blame] | 1656 | PrimaryBases.insert(RD); |
| 1657 | |
| 1658 | // Now traverse the base chain, starting with the first base, until we find |
| 1659 | // the base that is no longer a primary base. |
| 1660 | while (true) { |
| 1661 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1662 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1663 | |
| 1664 | if (!PrimaryBase) |
| 1665 | break; |
| 1666 | |
| 1667 | if (Layout.getPrimaryBaseWasVirtual()) { |
| 1668 | assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 && |
| 1669 | "Primary base should always be at offset 0!"); |
| 1670 | |
Anders Carlsson | dad0f99 | 2010-02-28 18:37:33 +0000 | [diff] [blame] | 1671 | const ASTRecordLayout &LayoutClassLayout = |
| 1672 | Context.getASTRecordLayout(LayoutClass); |
Anders Carlsson | f9fbc71 | 2010-02-23 06:34:44 +0000 | [diff] [blame] | 1673 | |
| 1674 | // Now check if this is the primary base that is not a primary base in the |
| 1675 | // most derived class. |
Anders Carlsson | dad0f99 | 2010-02-28 18:37:33 +0000 | [diff] [blame] | 1676 | if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1677 | FirstBaseOffsetInLayoutClass) { |
Anders Carlsson | f9fbc71 | 2010-02-23 06:34:44 +0000 | [diff] [blame] | 1678 | // We found it, stop walking the chain. |
| 1679 | break; |
| 1680 | } |
| 1681 | } else { |
| 1682 | assert(Layout.getBaseClassOffset(PrimaryBase) == 0 && |
| 1683 | "Primary base should always be at offset 0!"); |
| 1684 | } |
Anders Carlsson | cd4e637 | 2010-02-27 18:09:40 +0000 | [diff] [blame] | 1685 | |
Anders Carlsson | f89bb6c | 2010-02-27 19:51:04 +0000 | [diff] [blame] | 1686 | if (!PrimaryBases.insert(PrimaryBase)) |
| 1687 | assert(false && "Found a duplicate primary base!"); |
| 1688 | |
Anders Carlsson | cd4e637 | 2010-02-27 18:09:40 +0000 | [diff] [blame] | 1689 | RD = PrimaryBase; |
Anders Carlsson | f9fbc71 | 2010-02-23 06:34:44 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | // If the final overrider is an override of one of the primary bases, |
| 1693 | // then we know that it will be used. |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1694 | return OverridesIndirectMethodInBases(Overrider, PrimaryBases); |
Anders Carlsson | 464c8f1 | 2010-02-23 04:26:39 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1697 | /// FindNearestOverriddenMethod - Given a method, returns the overridden method |
| 1698 | /// from the nearest base. Returns null if no method was found. |
| 1699 | static const CXXMethodDecl * |
| 1700 | FindNearestOverriddenMethod(const CXXMethodDecl *MD, |
| 1701 | VtableBuilder::PrimaryBasesSetVectorTy &Bases) { |
| 1702 | for (int I = Bases.size(), E = 0; I != E; --I) { |
| 1703 | const CXXRecordDecl *PrimaryBase = Bases[I - 1]; |
| 1704 | |
| 1705 | // Now check the overriden methods. |
| 1706 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1707 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1708 | const CXXMethodDecl *OverriddenMD = *I; |
| 1709 | |
| 1710 | // We found our overridden method. |
| 1711 | if (OverriddenMD->getParent() == PrimaryBase) |
| 1712 | return OverriddenMD; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1719 | void |
| 1720 | VtableBuilder::AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass, |
| 1721 | const CXXRecordDecl *FirstBaseInPrimaryBaseChain, |
| 1722 | uint64_t FirstBaseOffsetInLayoutClass, |
Anders Carlsson | b9021e9 | 2010-02-27 16:18:19 +0000 | [diff] [blame] | 1723 | PrimaryBasesSetVectorTy &PrimaryBases) { |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1724 | const CXXRecordDecl *RD = Base.getBase(); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1725 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1726 | |
| 1727 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1728 | uint64_t PrimaryBaseOffset; |
| 1729 | uint64_t PrimaryBaseOffsetInLayoutClass; |
Anders Carlsson | af280c0 | 2010-02-23 03:48:14 +0000 | [diff] [blame] | 1730 | if (Layout.getPrimaryBaseWasVirtual()) { |
| 1731 | assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 && |
| 1732 | "Primary vbase should have a zero offset!"); |
| 1733 | |
| 1734 | const ASTRecordLayout &MostDerivedClassLayout = |
| 1735 | Context.getASTRecordLayout(MostDerivedClass); |
| 1736 | |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1737 | PrimaryBaseOffset = |
| 1738 | MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase); |
| 1739 | |
| 1740 | const ASTRecordLayout &LayoutClassLayout = |
| 1741 | Context.getASTRecordLayout(LayoutClass); |
| 1742 | |
| 1743 | PrimaryBaseOffsetInLayoutClass = |
| 1744 | LayoutClassLayout.getVBaseClassOffset(PrimaryBase); |
Anders Carlsson | af280c0 | 2010-02-23 03:48:14 +0000 | [diff] [blame] | 1745 | } else { |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1746 | assert(Layout.getBaseClassOffset(PrimaryBase) == 0 && |
| 1747 | "Primary base should have a zero offset!"); |
Anders Carlsson | af280c0 | 2010-02-23 03:48:14 +0000 | [diff] [blame] | 1748 | |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1749 | PrimaryBaseOffset = Base.getBaseOffset(); |
| 1750 | PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass; |
Anders Carlsson | af280c0 | 2010-02-23 03:48:14 +0000 | [diff] [blame] | 1751 | } |
Anders Carlsson | dad0f99 | 2010-02-28 18:37:33 +0000 | [diff] [blame] | 1752 | |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1753 | AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset), |
| 1754 | PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain, |
| 1755 | FirstBaseOffsetInLayoutClass, PrimaryBases); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1756 | |
| 1757 | if (!PrimaryBases.insert(PrimaryBase)) |
| 1758 | assert(false && "Found a duplicate primary base!"); |
| 1759 | } |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 1760 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1761 | // Now go through all virtual member functions and add them. |
| 1762 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 1763 | E = RD->method_end(); I != E; ++I) { |
| 1764 | const CXXMethodDecl *MD = *I; |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1765 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1766 | if (!MD->isVirtual()) |
| 1767 | continue; |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1768 | |
| 1769 | // Get the final overrider. |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 1770 | FinalOverriders::OverriderInfo Overrider = |
| 1771 | Overriders.getOverrider(Base, MD); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Check if this virtual member function overrides a method in a primary |
| 1774 | // base. If this is the case, and the return type doesn't require adjustment |
| 1775 | // then we can just use the member function from the primary base. |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1776 | if (const CXXMethodDecl *OverriddenMD = |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1777 | FindNearestOverriddenMethod(MD, PrimaryBases)) { |
Anders Carlsson | df31af1 | 2010-02-13 23:17:31 +0000 | [diff] [blame] | 1778 | if (ComputeReturnAdjustmentBaseOffset(Context, MD, |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1779 | OverriddenMD).isEmpty()) { |
| 1780 | // Replace the method info of the overridden method with our own |
| 1781 | // method. |
| 1782 | assert(MethodInfoMap.count(OverriddenMD) && |
| 1783 | "Did not find the overridden method!"); |
| 1784 | MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD]; |
| 1785 | |
| 1786 | MethodInfo MethodInfo(Base.getBaseOffset(), |
Anders Carlsson | 2bc1d3a | 2010-03-10 21:25:37 +0000 | [diff] [blame] | 1787 | BaseOffsetInLayoutClass, |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1788 | OverriddenMethodInfo.VtableIndex); |
| 1789 | |
| 1790 | assert(!MethodInfoMap.count(MD) && |
| 1791 | "Should not have method info for this method yet!"); |
| 1792 | |
| 1793 | MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); |
| 1794 | MethodInfoMap.erase(OverriddenMD); |
Anders Carlsson | bca5d37 | 2010-03-22 20:06:40 +0000 | [diff] [blame] | 1795 | |
| 1796 | // If the overridden method exists in a virtual base class or a direct |
| 1797 | // or indirect base class of a virtual base class, we need to emit a |
| 1798 | // thunk if we ever have a class hierarchy where the base class is not |
| 1799 | // a primary base in the complete object. |
| 1800 | if (!isBuildingConstructorVtable() && OverriddenMD != MD) { |
| 1801 | // Compute the this adjustment. |
| 1802 | ThisAdjustment ThisAdjustment = |
| 1803 | ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass, |
| 1804 | Overrider); |
| 1805 | |
| 1806 | if (ThisAdjustment.VCallOffsetOffset) { |
| 1807 | // This is a virtual thunk, add it. |
| 1808 | AddThunk(Overrider.Method, |
| 1809 | ThunkInfo(ThisAdjustment, ReturnAdjustment())); |
| 1810 | } |
| 1811 | } |
| 1812 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1813 | continue; |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1814 | } |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1815 | } |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 1816 | |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1817 | // Insert the method info for this method. |
Anders Carlsson | 2bc1d3a | 2010-03-10 21:25:37 +0000 | [diff] [blame] | 1818 | MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, |
| 1819 | Components.size()); |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1820 | |
| 1821 | assert(!MethodInfoMap.count(MD) && |
| 1822 | "Should not have method info for this method yet!"); |
| 1823 | MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); |
Anders Carlsson | cd4e637 | 2010-02-27 18:09:40 +0000 | [diff] [blame] | 1824 | |
| 1825 | // Check if this overrider is going to be used. |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1826 | const CXXMethodDecl *OverriderMD = Overrider.Method; |
| 1827 | if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass, |
| 1828 | FirstBaseInPrimaryBaseChain, |
| 1829 | FirstBaseOffsetInLayoutClass)) { |
Anders Carlsson | cd4e637 | 2010-02-27 18:09:40 +0000 | [diff] [blame] | 1830 | Components.push_back(VtableComponent::MakeUnusedFunction(OverriderMD)); |
| 1831 | continue; |
| 1832 | } |
Anders Carlsson | 464c8f1 | 2010-02-23 04:26:39 +0000 | [diff] [blame] | 1833 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1834 | // Check if this overrider needs a return adjustment. |
Anders Carlsson | 2fca9be | 2010-02-23 01:34:28 +0000 | [diff] [blame] | 1835 | BaseOffset ReturnAdjustmentOffset = |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 1836 | Overriders.getReturnAdjustmentOffset(Base, MD); |
| 1837 | |
| 1838 | ReturnAdjustment ReturnAdjustment = |
| 1839 | ComputeReturnAdjustment(ReturnAdjustmentOffset); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1840 | |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1841 | AddMethod(Overrider.Method, ReturnAdjustment); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1842 | } |
| 1843 | } |
| 1844 | |
Anders Carlsson | 89ad421 | 2010-02-16 16:26:28 +0000 | [diff] [blame] | 1845 | void VtableBuilder::LayoutVtable() { |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 1846 | LayoutPrimaryAndSecondaryVtables(BaseSubobject(MostDerivedClass, 0), |
| 1847 | MostDerivedClassIsVirtual, |
| 1848 | MostDerivedClassOffset); |
Anders Carlsson | 89ad421 | 2010-02-16 16:26:28 +0000 | [diff] [blame] | 1849 | |
Anders Carlsson | 69dc04e | 2010-02-16 16:49:35 +0000 | [diff] [blame] | 1850 | VisitedVirtualBasesSetTy VBases; |
Anders Carlsson | 08c2675 | 2010-02-27 04:05:52 +0000 | [diff] [blame] | 1851 | |
| 1852 | // Determine the primary virtual bases. |
Anders Carlsson | d7fdae5 | 2010-03-10 03:02:01 +0000 | [diff] [blame] | 1853 | DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset, |
| 1854 | VBases); |
Anders Carlsson | 08c2675 | 2010-02-27 04:05:52 +0000 | [diff] [blame] | 1855 | VBases.clear(); |
| 1856 | |
Anders Carlsson | 69dc04e | 2010-02-16 16:49:35 +0000 | [diff] [blame] | 1857 | LayoutVtablesForVirtualBases(MostDerivedClass, VBases); |
Anders Carlsson | 89ad421 | 2010-02-16 16:26:28 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 1860 | void |
| 1861 | VtableBuilder::LayoutPrimaryAndSecondaryVtables(BaseSubobject Base, |
| 1862 | bool BaseIsVirtual, |
| 1863 | uint64_t OffsetInLayoutClass) { |
Chris Lattner | 04a2311 | 2010-02-17 21:42:34 +0000 | [diff] [blame] | 1864 | assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!"); |
Anders Carlsson | b828afa | 2010-02-14 17:05:59 +0000 | [diff] [blame] | 1865 | |
Anders Carlsson | 852213e | 2010-02-16 04:59:55 +0000 | [diff] [blame] | 1866 | // Add vcall and vbase offsets for this vtable. |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 1867 | VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders, |
| 1868 | Base, BaseIsVirtual, OffsetInLayoutClass); |
Anders Carlsson | d68c083 | 2010-02-25 03:45:56 +0000 | [diff] [blame] | 1869 | Components.append(Builder.components_begin(), Builder.components_end()); |
| 1870 | |
Anders Carlsson | dba6ef8 | 2010-02-25 22:23:13 +0000 | [diff] [blame] | 1871 | // Check if we need to add these vcall offsets. |
| 1872 | if (BaseIsVirtual && !Builder.getVCallOffsets().empty()) { |
| 1873 | VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()]; |
| 1874 | |
| 1875 | if (VCallOffsets.empty()) |
| 1876 | VCallOffsets = Builder.getVCallOffsets(); |
| 1877 | } |
| 1878 | |
Anders Carlsson | bb6305b | 2010-03-11 06:43:12 +0000 | [diff] [blame] | 1879 | // If we're laying out the most derived class we want to keep track of the |
| 1880 | // virtual base class offset offsets. |
| 1881 | if (Base.getBase() == MostDerivedClass) |
| 1882 | VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets(); |
| 1883 | |
Anders Carlsson | 852213e | 2010-02-16 04:59:55 +0000 | [diff] [blame] | 1884 | // Add the offset to top. |
Anders Carlsson | e67dc30 | 2010-02-14 00:16:19 +0000 | [diff] [blame] | 1885 | // FIXME: We should not use / 8 here. |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 1886 | int64_t OffsetToTop = -(int64_t)(OffsetInLayoutClass - |
| 1887 | MostDerivedClassOffset) / 8; |
Anders Carlsson | 76f1aa7 | 2010-02-13 22:05:23 +0000 | [diff] [blame] | 1888 | Components.push_back(VtableComponent::MakeOffsetToTop(OffsetToTop)); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1889 | |
| 1890 | // Next, add the RTTI. |
Anders Carlsson | 76f1aa7 | 2010-02-13 22:05:23 +0000 | [diff] [blame] | 1891 | Components.push_back(VtableComponent::MakeRTTI(MostDerivedClass)); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1892 | |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 1893 | uint64_t AddressPoint = Components.size(); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1894 | |
| 1895 | // Now go through all virtual member functions and add them. |
Anders Carlsson | b9021e9 | 2010-02-27 16:18:19 +0000 | [diff] [blame] | 1896 | PrimaryBasesSetVectorTy PrimaryBases; |
Anders Carlsson | 6039661 | 2010-03-10 06:51:42 +0000 | [diff] [blame] | 1897 | AddMethods(Base, OffsetInLayoutClass, Base.getBase(), OffsetInLayoutClass, |
| 1898 | PrimaryBases); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1899 | |
Anders Carlsson | 2ef9d6b | 2010-02-27 16:52:49 +0000 | [diff] [blame] | 1900 | // Compute 'this' pointer adjustments. |
| 1901 | ComputeThisAdjustments(); |
| 1902 | |
Anders Carlsson | 014a358 | 2010-03-25 21:45:14 +0000 | [diff] [blame] | 1903 | // Add all address points. |
| 1904 | const CXXRecordDecl *RD = Base.getBase(); |
| 1905 | while (true) { |
| 1906 | AddressPoints.insert(std::make_pair(BaseSubobject(RD, OffsetInLayoutClass), |
| 1907 | AddressPoint)); |
| 1908 | |
| 1909 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1910 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 1911 | |
Anders Carlsson | 014a358 | 2010-03-25 21:45:14 +0000 | [diff] [blame] | 1912 | if (!PrimaryBase) |
| 1913 | break; |
| 1914 | |
| 1915 | if (Layout.getPrimaryBaseWasVirtual()) { |
| 1916 | // Check if this virtual primary base is a primary base in the layout |
| 1917 | // class. If it's not, we don't want to add it. |
| 1918 | const ASTRecordLayout &LayoutClassLayout = |
| 1919 | Context.getASTRecordLayout(LayoutClass); |
| 1920 | |
| 1921 | if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != |
| 1922 | OffsetInLayoutClass) { |
| 1923 | // We don't want to add this class (or any of its primary bases). |
| 1924 | break; |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | RD = PrimaryBase; |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Anders Carlsson | a96a2e9 | 2010-03-10 19:39:11 +0000 | [diff] [blame] | 1931 | bool BaseIsMorallyVirtual = BaseIsVirtual; |
| 1932 | if (isBuildingConstructorVtable() && Base.getBase() == MostDerivedClass) |
| 1933 | BaseIsMorallyVirtual = false; |
| 1934 | |
Anders Carlsson | 5edcc37 | 2010-02-24 16:43:12 +0000 | [diff] [blame] | 1935 | // Layout secondary vtables. |
Anders Carlsson | a96a2e9 | 2010-03-10 19:39:11 +0000 | [diff] [blame] | 1936 | LayoutSecondaryVtables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass); |
Anders Carlsson | c7b6316 | 2010-02-16 16:02:57 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 1939 | void VtableBuilder::LayoutSecondaryVtables(BaseSubobject Base, |
Anders Carlsson | a96a2e9 | 2010-03-10 19:39:11 +0000 | [diff] [blame] | 1940 | bool BaseIsMorallyVirtual, |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 1941 | uint64_t OffsetInLayoutClass) { |
Anders Carlsson | c7b6316 | 2010-02-16 16:02:57 +0000 | [diff] [blame] | 1942 | // Itanium C++ ABI 2.5.2: |
| 1943 | // Following the primary virtual table of a derived class are secondary |
| 1944 | // virtual tables for each of its proper base classes, except any primary |
| 1945 | // base(s) with which it shares its primary virtual table. |
| 1946 | |
| 1947 | const CXXRecordDecl *RD = Base.getBase(); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1948 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 1949 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
Anders Carlsson | c7b6316 | 2010-02-16 16:02:57 +0000 | [diff] [blame] | 1950 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1951 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1952 | E = RD->bases_end(); I != E; ++I) { |
Anders Carlsson | c7b6316 | 2010-02-16 16:02:57 +0000 | [diff] [blame] | 1953 | // Ignore virtual bases, we'll emit them later. |
| 1954 | if (I->isVirtual()) |
| 1955 | continue; |
| 1956 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1957 | const CXXRecordDecl *BaseDecl = |
| 1958 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | b828afa | 2010-02-14 17:05:59 +0000 | [diff] [blame] | 1959 | |
Anders Carlsson | a661a54 | 2010-02-16 04:49:44 +0000 | [diff] [blame] | 1960 | // Ignore bases that don't have a vtable. |
| 1961 | if (!BaseDecl->isDynamicClass()) |
| 1962 | continue; |
Anders Carlsson | b828afa | 2010-02-14 17:05:59 +0000 | [diff] [blame] | 1963 | |
Anders Carlsson | f2c98ce | 2010-03-10 19:15:26 +0000 | [diff] [blame] | 1964 | if (isBuildingConstructorVtable()) { |
| 1965 | // Itanium C++ ABI 2.6.4: |
| 1966 | // Some of the base class subobjects may not need construction virtual |
| 1967 | // tables, which will therefore not be present in the construction |
| 1968 | // virtual table group, even though the subobject virtual tables are |
| 1969 | // present in the main virtual table group for the complete object. |
Anders Carlsson | a96a2e9 | 2010-03-10 19:39:11 +0000 | [diff] [blame] | 1970 | if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases()) |
Anders Carlsson | f2c98ce | 2010-03-10 19:15:26 +0000 | [diff] [blame] | 1971 | continue; |
Anders Carlsson | f2c98ce | 2010-03-10 19:15:26 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Anders Carlsson | 76f1aa7 | 2010-02-13 22:05:23 +0000 | [diff] [blame] | 1974 | // Get the base offset of this base. |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 1975 | uint64_t RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl); |
| 1976 | uint64_t BaseOffset = Base.getBaseOffset() + RelativeBaseOffset; |
| 1977 | |
| 1978 | uint64_t BaseOffsetInLayoutClass = OffsetInLayoutClass + RelativeBaseOffset; |
Anders Carlsson | c7b6316 | 2010-02-16 16:02:57 +0000 | [diff] [blame] | 1979 | |
| 1980 | // Don't emit a secondary vtable for a primary base. We might however want |
| 1981 | // to emit secondary vtables for other bases of this base. |
| 1982 | if (BaseDecl == PrimaryBase) { |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 1983 | LayoutSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset), |
Anders Carlsson | a96a2e9 | 2010-03-10 19:39:11 +0000 | [diff] [blame] | 1984 | BaseIsMorallyVirtual, BaseOffsetInLayoutClass); |
Anders Carlsson | c7b6316 | 2010-02-16 16:02:57 +0000 | [diff] [blame] | 1985 | continue; |
| 1986 | } |
Anders Carlsson | 76f1aa7 | 2010-02-13 22:05:23 +0000 | [diff] [blame] | 1987 | |
Anders Carlsson | 89ad421 | 2010-02-16 16:26:28 +0000 | [diff] [blame] | 1988 | // Layout the primary vtable (and any secondary vtables) for this base. |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 1989 | LayoutPrimaryAndSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset), |
| 1990 | /*BaseIsVirtual=*/false, |
| 1991 | BaseOffsetInLayoutClass); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | |
Anders Carlsson | 69dc04e | 2010-02-16 16:49:35 +0000 | [diff] [blame] | 1995 | void |
Anders Carlsson | d7fdae5 | 2010-03-10 03:02:01 +0000 | [diff] [blame] | 1996 | VtableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD, |
| 1997 | uint64_t OffsetInLayoutClass, |
Anders Carlsson | 08c2675 | 2010-02-27 04:05:52 +0000 | [diff] [blame] | 1998 | VisitedVirtualBasesSetTy &VBases) { |
| 1999 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 2000 | |
| 2001 | // Check if this base has a primary base. |
| 2002 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
Anders Carlsson | 0378bf0 | 2010-02-28 18:08:38 +0000 | [diff] [blame] | 2003 | |
| 2004 | // Check if it's virtual. |
| 2005 | if (Layout.getPrimaryBaseWasVirtual()) { |
| 2006 | bool IsPrimaryVirtualBase = true; |
| 2007 | |
| 2008 | if (isBuildingConstructorVtable()) { |
| 2009 | // Check if the base is actually a primary base in the class we use for |
| 2010 | // layout. |
Anders Carlsson | d7fdae5 | 2010-03-10 03:02:01 +0000 | [diff] [blame] | 2011 | const ASTRecordLayout &LayoutClassLayout = |
| 2012 | Context.getASTRecordLayout(LayoutClass); |
| 2013 | |
| 2014 | uint64_t PrimaryBaseOffsetInLayoutClass = |
| 2015 | LayoutClassLayout.getVBaseClassOffset(PrimaryBase); |
| 2016 | |
| 2017 | // We know that the base is not a primary base in the layout class if |
| 2018 | // the base offsets are different. |
| 2019 | if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass) |
Anders Carlsson | 0378bf0 | 2010-02-28 18:08:38 +0000 | [diff] [blame] | 2020 | IsPrimaryVirtualBase = false; |
| 2021 | } |
| 2022 | |
| 2023 | if (IsPrimaryVirtualBase) |
| 2024 | PrimaryVirtualBases.insert(PrimaryBase); |
| 2025 | } |
Anders Carlsson | 08c2675 | 2010-02-27 04:05:52 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | // Traverse bases, looking for more primary virtual bases. |
| 2029 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 2030 | E = RD->bases_end(); I != E; ++I) { |
| 2031 | const CXXRecordDecl *BaseDecl = |
| 2032 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 2033 | |
Anders Carlsson | d7fdae5 | 2010-03-10 03:02:01 +0000 | [diff] [blame] | 2034 | uint64_t BaseOffsetInLayoutClass; |
| 2035 | |
| 2036 | if (I->isVirtual()) { |
| 2037 | if (!VBases.insert(BaseDecl)) |
| 2038 | continue; |
| 2039 | |
| 2040 | const ASTRecordLayout &LayoutClassLayout = |
| 2041 | Context.getASTRecordLayout(LayoutClass); |
Anders Carlsson | 08c2675 | 2010-02-27 04:05:52 +0000 | [diff] [blame] | 2042 | |
Anders Carlsson | d7fdae5 | 2010-03-10 03:02:01 +0000 | [diff] [blame] | 2043 | BaseOffsetInLayoutClass = LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
| 2044 | } else { |
| 2045 | BaseOffsetInLayoutClass = |
| 2046 | OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl); |
| 2047 | } |
| 2048 | |
| 2049 | DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases); |
Anders Carlsson | 08c2675 | 2010-02-27 04:05:52 +0000 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | void |
Anders Carlsson | 69dc04e | 2010-02-16 16:49:35 +0000 | [diff] [blame] | 2054 | VtableBuilder::LayoutVtablesForVirtualBases(const CXXRecordDecl *RD, |
| 2055 | VisitedVirtualBasesSetTy &VBases) { |
| 2056 | // Itanium C++ ABI 2.5.2: |
| 2057 | // Then come the virtual base virtual tables, also in inheritance graph |
| 2058 | // order, and again excluding primary bases (which share virtual tables with |
| 2059 | // the classes for which they are primary). |
Anders Carlsson | 69dc04e | 2010-02-16 16:49:35 +0000 | [diff] [blame] | 2060 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 2061 | E = RD->bases_end(); I != E; ++I) { |
| 2062 | const CXXRecordDecl *BaseDecl = |
| 2063 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 2064 | |
Anders Carlsson | af280c0 | 2010-02-23 03:48:14 +0000 | [diff] [blame] | 2065 | // Check if this base needs a vtable. (If it's virtual, not a primary base |
| 2066 | // of some other class, and we haven't visited it before). |
Anders Carlsson | 69dc04e | 2010-02-16 16:49:35 +0000 | [diff] [blame] | 2067 | if (I->isVirtual() && BaseDecl->isDynamicClass() && |
Anders Carlsson | 08c2675 | 2010-02-27 04:05:52 +0000 | [diff] [blame] | 2068 | !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) { |
Anders Carlsson | 69dc04e | 2010-02-16 16:49:35 +0000 | [diff] [blame] | 2069 | const ASTRecordLayout &MostDerivedClassLayout = |
| 2070 | Context.getASTRecordLayout(MostDerivedClass); |
| 2071 | uint64_t BaseOffset = |
| 2072 | MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); |
| 2073 | |
Anders Carlsson | 530c40c | 2010-02-28 01:43:58 +0000 | [diff] [blame] | 2074 | const ASTRecordLayout &LayoutClassLayout = |
| 2075 | Context.getASTRecordLayout(LayoutClass); |
| 2076 | uint64_t BaseOffsetInLayoutClass = |
| 2077 | LayoutClassLayout.getVBaseClassOffset(BaseDecl); |
| 2078 | |
| 2079 | LayoutPrimaryAndSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset), |
| 2080 | /*BaseIsVirtual=*/true, |
| 2081 | BaseOffsetInLayoutClass); |
Anders Carlsson | 69dc04e | 2010-02-16 16:49:35 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | // We only need to check the base for virtual base vtables if it actually |
| 2085 | // has virtual bases. |
| 2086 | if (BaseDecl->getNumVBases()) |
| 2087 | LayoutVtablesForVirtualBases(BaseDecl, VBases); |
| 2088 | } |
| 2089 | } |
| 2090 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2091 | /// dumpLayout - Dump the vtable layout. |
| 2092 | void VtableBuilder::dumpLayout(llvm::raw_ostream& Out) { |
Anders Carlsson | d03a326 | 2010-02-27 20:39:05 +0000 | [diff] [blame] | 2093 | |
Anders Carlsson | bb6305b | 2010-03-11 06:43:12 +0000 | [diff] [blame] | 2094 | if (isBuildingConstructorVtable()) { |
Anders Carlsson | d03a326 | 2010-02-27 20:39:05 +0000 | [diff] [blame] | 2095 | Out << "Construction vtable for ('"; |
| 2096 | Out << MostDerivedClass->getQualifiedNameAsString() << "', "; |
| 2097 | // FIXME: Don't use / 8 . |
| 2098 | Out << MostDerivedClassOffset / 8 << ") in '"; |
| 2099 | Out << LayoutClass->getQualifiedNameAsString(); |
Anders Carlsson | bb6305b | 2010-03-11 06:43:12 +0000 | [diff] [blame] | 2100 | } else { |
| 2101 | Out << "Vtable for '"; |
| 2102 | Out << MostDerivedClass->getQualifiedNameAsString(); |
Anders Carlsson | d03a326 | 2010-02-27 20:39:05 +0000 | [diff] [blame] | 2103 | } |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2104 | Out << "' (" << Components.size() << " entries).\n"; |
| 2105 | |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 2106 | // Iterate through the address points and insert them into a new map where |
| 2107 | // they are keyed by the index and not the base object. |
| 2108 | // Since an address point can be shared by multiple subobjects, we use an |
| 2109 | // STL multimap. |
| 2110 | std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex; |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 2111 | for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(), |
| 2112 | E = AddressPoints.end(); I != E; ++I) { |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 2113 | const BaseSubobject& Base = I->first; |
| 2114 | uint64_t Index = I->second; |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2115 | |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 2116 | AddressPointsByIndex.insert(std::make_pair(Index, Base)); |
| 2117 | } |
| 2118 | |
| 2119 | for (unsigned I = 0, E = Components.size(); I != E; ++I) { |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 2120 | uint64_t Index = I; |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 2121 | |
| 2122 | Out << llvm::format("%4d | ", I); |
| 2123 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2124 | const VtableComponent &Component = Components[I]; |
| 2125 | |
| 2126 | // Dump the component. |
| 2127 | switch (Component.getKind()) { |
Anders Carlsson | d9a6704 | 2010-02-17 06:07:19 +0000 | [diff] [blame] | 2128 | |
| 2129 | case VtableComponent::CK_VCallOffset: |
| 2130 | Out << "vcall_offset (" << Component.getVCallOffset() << ")"; |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2131 | break; |
Anders Carlsson | 852213e | 2010-02-16 04:59:55 +0000 | [diff] [blame] | 2132 | |
| 2133 | case VtableComponent::CK_VBaseOffset: |
| 2134 | Out << "vbase_offset (" << Component.getVBaseOffset() << ")"; |
| 2135 | break; |
| 2136 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2137 | case VtableComponent::CK_OffsetToTop: |
| 2138 | Out << "offset_to_top (" << Component.getOffsetToTop() << ")"; |
| 2139 | break; |
| 2140 | |
| 2141 | case VtableComponent::CK_RTTI: |
| 2142 | Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI"; |
| 2143 | break; |
| 2144 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2145 | case VtableComponent::CK_FunctionPointer: { |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2146 | const CXXMethodDecl *MD = Component.getFunctionDecl(); |
| 2147 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2148 | std::string Str = |
| 2149 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 2150 | MD); |
| 2151 | Out << Str; |
Anders Carlsson | 9824142 | 2010-02-12 02:38:13 +0000 | [diff] [blame] | 2152 | if (MD->isPure()) |
| 2153 | Out << " [pure]"; |
| 2154 | |
Anders Carlsson | 3a5df3f | 2010-03-23 04:44:10 +0000 | [diff] [blame] | 2155 | ThunkInfo Thunk = VTableThunks.lookup(I); |
Anders Carlsson | dd36681 | 2010-03-12 04:54:20 +0000 | [diff] [blame] | 2156 | if (!Thunk.isEmpty()) { |
| 2157 | // If this function pointer has a return adjustment, dump it. |
| 2158 | if (!Thunk.Return.isEmpty()) { |
| 2159 | Out << "\n [return adjustment: "; |
| 2160 | Out << Thunk.Return.NonVirtual << " non-virtual"; |
| 2161 | |
| 2162 | if (Thunk.Return.VBaseOffsetOffset) { |
| 2163 | Out << ", " << Thunk.Return.VBaseOffsetOffset; |
| 2164 | Out << " vbase offset offset"; |
| 2165 | } |
Anders Carlsson | 60cc72a | 2010-02-18 17:00:09 +0000 | [diff] [blame] | 2166 | |
Anders Carlsson | dd36681 | 2010-03-12 04:54:20 +0000 | [diff] [blame] | 2167 | Out << ']'; |
| 2168 | } |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 2169 | |
Anders Carlsson | dd36681 | 2010-03-12 04:54:20 +0000 | [diff] [blame] | 2170 | // If this function pointer has a 'this' pointer adjustment, dump it. |
| 2171 | if (!Thunk.This.isEmpty()) { |
| 2172 | Out << "\n [this adjustment: "; |
| 2173 | Out << Thunk.This.NonVirtual << " non-virtual"; |
| 2174 | |
| 2175 | if (Thunk.This.VCallOffsetOffset) { |
| 2176 | Out << ", " << Thunk.This.VCallOffsetOffset; |
| 2177 | Out << " vcall offset offset"; |
| 2178 | } |
Anders Carlsson | 60cc72a | 2010-02-18 17:00:09 +0000 | [diff] [blame] | 2179 | |
Anders Carlsson | dd36681 | 2010-03-12 04:54:20 +0000 | [diff] [blame] | 2180 | Out << ']'; |
| 2181 | } |
Anders Carlsson | 49bac9a | 2010-02-13 23:40:17 +0000 | [diff] [blame] | 2182 | } |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 2183 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2184 | break; |
| 2185 | } |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2186 | |
Anders Carlsson | 6065e02 | 2010-02-19 06:03:53 +0000 | [diff] [blame] | 2187 | case VtableComponent::CK_CompleteDtorPointer: |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2188 | case VtableComponent::CK_DeletingDtorPointer: { |
Anders Carlsson | 6065e02 | 2010-02-19 06:03:53 +0000 | [diff] [blame] | 2189 | bool IsComplete = |
| 2190 | Component.getKind() == VtableComponent::CK_CompleteDtorPointer; |
| 2191 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2192 | const CXXDestructorDecl *DD = Component.getDestructorDecl(); |
| 2193 | |
Anders Carlsson | 6065e02 | 2010-02-19 06:03:53 +0000 | [diff] [blame] | 2194 | Out << DD->getQualifiedNameAsString(); |
| 2195 | if (IsComplete) |
| 2196 | Out << "() [complete]"; |
| 2197 | else |
| 2198 | Out << "() [deleting]"; |
| 2199 | |
Anders Carlsson | 9824142 | 2010-02-12 02:38:13 +0000 | [diff] [blame] | 2200 | if (DD->isPure()) |
| 2201 | Out << " [pure]"; |
| 2202 | |
Anders Carlsson | 3a5df3f | 2010-03-23 04:44:10 +0000 | [diff] [blame] | 2203 | ThunkInfo Thunk = VTableThunks.lookup(I); |
Anders Carlsson | dd36681 | 2010-03-12 04:54:20 +0000 | [diff] [blame] | 2204 | if (!Thunk.isEmpty()) { |
| 2205 | // If this destructor has a 'this' pointer adjustment, dump it. |
| 2206 | if (!Thunk.This.isEmpty()) { |
| 2207 | Out << "\n [this adjustment: "; |
| 2208 | Out << Thunk.This.NonVirtual << " non-virtual"; |
| 2209 | |
| 2210 | if (Thunk.This.VCallOffsetOffset) { |
| 2211 | Out << ", " << Thunk.This.VCallOffsetOffset; |
| 2212 | Out << " vcall offset offset"; |
| 2213 | } |
| 2214 | |
| 2215 | Out << ']'; |
| 2216 | } |
| 2217 | } |
Anders Carlsson | 6065e02 | 2010-02-19 06:03:53 +0000 | [diff] [blame] | 2218 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2219 | break; |
| 2220 | } |
| 2221 | |
Anders Carlsson | 1f46466 | 2010-02-19 20:08:13 +0000 | [diff] [blame] | 2222 | case VtableComponent::CK_UnusedFunctionPointer: { |
| 2223 | const CXXMethodDecl *MD = Component.getUnusedFunctionDecl(); |
| 2224 | |
| 2225 | std::string Str = |
| 2226 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 2227 | MD); |
| 2228 | Out << "[unused] " << Str; |
| 2229 | if (MD->isPure()) |
| 2230 | Out << " [pure]"; |
| 2231 | } |
| 2232 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2233 | } |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 2234 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2235 | Out << '\n'; |
Anders Carlsson | d2c3a7c | 2010-02-27 23:58:01 +0000 | [diff] [blame] | 2236 | |
| 2237 | // Dump the next address point. |
| 2238 | uint64_t NextIndex = Index + 1; |
| 2239 | if (AddressPointsByIndex.count(NextIndex)) { |
| 2240 | if (AddressPointsByIndex.count(NextIndex) == 1) { |
| 2241 | const BaseSubobject &Base = |
| 2242 | AddressPointsByIndex.find(NextIndex)->second; |
| 2243 | |
| 2244 | // FIXME: Instead of dividing by 8, we should be using CharUnits. |
| 2245 | Out << " -- (" << Base.getBase()->getQualifiedNameAsString(); |
| 2246 | Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n"; |
| 2247 | } else { |
| 2248 | uint64_t BaseOffset = |
| 2249 | AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset(); |
| 2250 | |
| 2251 | // We store the class names in a set to get a stable order. |
| 2252 | std::set<std::string> ClassNames; |
| 2253 | for (std::multimap<uint64_t, BaseSubobject>::const_iterator I = |
| 2254 | AddressPointsByIndex.lower_bound(NextIndex), E = |
| 2255 | AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) { |
| 2256 | assert(I->second.getBaseOffset() == BaseOffset && |
| 2257 | "Invalid base offset!"); |
| 2258 | const CXXRecordDecl *RD = I->second.getBase(); |
| 2259 | ClassNames.insert(RD->getQualifiedNameAsString()); |
| 2260 | } |
| 2261 | |
| 2262 | for (std::set<std::string>::const_iterator I = ClassNames.begin(), |
| 2263 | E = ClassNames.end(); I != E; ++I) { |
| 2264 | // FIXME: Instead of dividing by 8, we should be using CharUnits. |
| 2265 | Out << " -- (" << *I; |
| 2266 | Out << ", " << BaseOffset / 8 << ") vtable address --\n"; |
| 2267 | } |
| 2268 | } |
| 2269 | } |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2270 | } |
Anders Carlsson | d03a326 | 2010-02-27 20:39:05 +0000 | [diff] [blame] | 2271 | |
| 2272 | Out << '\n'; |
Anders Carlsson | bb6305b | 2010-03-11 06:43:12 +0000 | [diff] [blame] | 2273 | |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2274 | if (isBuildingConstructorVtable()) |
| 2275 | return; |
| 2276 | |
| 2277 | if (MostDerivedClass->getNumVBases()) { |
Anders Carlsson | bb6305b | 2010-03-11 06:43:12 +0000 | [diff] [blame] | 2278 | // We store the virtual base class names and their offsets in a map to get |
| 2279 | // a stable order. |
Anders Carlsson | bb6305b | 2010-03-11 06:43:12 +0000 | [diff] [blame] | 2280 | |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2281 | std::map<std::string, int64_t> ClassNamesAndOffsets; |
Anders Carlsson | bb6305b | 2010-03-11 06:43:12 +0000 | [diff] [blame] | 2282 | for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(), |
| 2283 | E = VBaseOffsetOffsets.end(); I != E; ++I) { |
| 2284 | std::string ClassName = I->first->getQualifiedNameAsString(); |
| 2285 | int64_t OffsetOffset = I->second; |
| 2286 | ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset)); |
| 2287 | } |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2288 | |
| 2289 | Out << "Virtual base offset offsets for '"; |
| 2290 | Out << MostDerivedClass->getQualifiedNameAsString() << "' ("; |
| 2291 | Out << ClassNamesAndOffsets.size(); |
| 2292 | Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n"; |
Anders Carlsson | bb6305b | 2010-03-11 06:43:12 +0000 | [diff] [blame] | 2293 | |
| 2294 | for (std::map<std::string, int64_t>::const_iterator I = |
| 2295 | ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end(); |
| 2296 | I != E; ++I) |
| 2297 | Out << " " << I->first << " | " << I->second << '\n'; |
| 2298 | |
| 2299 | Out << "\n"; |
| 2300 | } |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2301 | |
Anders Carlsson | 3a5df3f | 2010-03-23 04:44:10 +0000 | [diff] [blame] | 2302 | if (!Thunks.empty()) { |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2303 | // We store the method names in a map to get a stable order. |
| 2304 | std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls; |
| 2305 | |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 2306 | for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end(); |
Anders Carlsson | 3a5df3f | 2010-03-23 04:44:10 +0000 | [diff] [blame] | 2307 | I != E; ++I) { |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2308 | const CXXMethodDecl *MD = I->first; |
| 2309 | std::string MethodName = |
| 2310 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 2311 | MD); |
| 2312 | |
| 2313 | MethodNamesAndDecls.insert(std::make_pair(MethodName, MD)); |
| 2314 | } |
| 2315 | |
| 2316 | for (std::map<std::string, const CXXMethodDecl *>::const_iterator I = |
| 2317 | MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end(); |
| 2318 | I != E; ++I) { |
| 2319 | const std::string &MethodName = I->first; |
| 2320 | const CXXMethodDecl *MD = I->second; |
Anders Carlsson | 87e0ac8 | 2010-03-22 16:30:44 +0000 | [diff] [blame] | 2321 | |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 2322 | ThunkInfoVectorTy ThunksVector = Thunks[MD]; |
Anders Carlsson | 87e0ac8 | 2010-03-22 16:30:44 +0000 | [diff] [blame] | 2323 | std::sort(ThunksVector.begin(), ThunksVector.end()); |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2324 | |
| 2325 | Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size(); |
| 2326 | Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n"; |
| 2327 | |
| 2328 | for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) { |
| 2329 | const ThunkInfo &Thunk = ThunksVector[I]; |
| 2330 | |
| 2331 | Out << llvm::format("%4d | ", I); |
| 2332 | |
Anders Carlsson | 09daa50 | 2010-03-22 16:10:59 +0000 | [diff] [blame] | 2333 | // If this function pointer has a return pointer adjustment, dump it. |
| 2334 | if (!Thunk.Return.isEmpty()) { |
| 2335 | Out << "return adjustment: " << Thunk.This.NonVirtual; |
| 2336 | Out << " non-virtual"; |
| 2337 | if (Thunk.Return.VBaseOffsetOffset) { |
| 2338 | Out << ", " << Thunk.Return.VBaseOffsetOffset; |
| 2339 | Out << " vbase offset offset"; |
| 2340 | } |
| 2341 | |
| 2342 | if (!Thunk.This.isEmpty()) |
| 2343 | Out << "\n "; |
| 2344 | } |
| 2345 | |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2346 | // If this function pointer has a 'this' pointer adjustment, dump it. |
| 2347 | if (!Thunk.This.isEmpty()) { |
Anders Carlsson | 09daa50 | 2010-03-22 16:10:59 +0000 | [diff] [blame] | 2348 | Out << "this adjustment: "; |
| 2349 | Out << Thunk.This.NonVirtual << " non-virtual"; |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2350 | |
| 2351 | if (Thunk.This.VCallOffsetOffset) { |
| 2352 | Out << ", " << Thunk.This.VCallOffsetOffset; |
Anders Carlsson | 09daa50 | 2010-03-22 16:10:59 +0000 | [diff] [blame] | 2353 | Out << " vcall offset offset"; |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | Out << '\n'; |
| 2358 | } |
Anders Carlsson | bca5d37 | 2010-03-22 20:06:40 +0000 | [diff] [blame] | 2359 | |
| 2360 | Out << '\n'; |
| 2361 | |
Anders Carlsson | 9135a84 | 2010-03-18 02:44:19 +0000 | [diff] [blame] | 2362 | } |
| 2363 | } |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | } |
| 2367 | |
| 2368 | namespace { |
| 2369 | class OldVtableBuilder { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2370 | public: |
| 2371 | /// Index_t - Vtable index type. |
| 2372 | typedef uint64_t Index_t; |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2373 | typedef std::vector<std::pair<GlobalDecl, |
| 2374 | std::pair<GlobalDecl, ThunkAdjustment> > > |
| 2375 | SavedAdjustmentsVectorTy; |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 2376 | typedef llvm::DenseMap<BaseSubobject, uint64_t> AddressPointsMapTy; |
| 2377 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2378 | private: |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 2379 | |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 2380 | // VtableComponents - The components of the vtable being built. |
| 2381 | typedef llvm::SmallVector<llvm::Constant *, 64> VtableComponentsVectorTy; |
| 2382 | VtableComponentsVectorTy VtableComponents; |
| 2383 | |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 2384 | const bool BuildVtable; |
| 2385 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2386 | llvm::Type *Ptr8Ty; |
Anders Carlsson | ac3f7bd | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 2387 | |
| 2388 | /// MostDerivedClass - The most derived class that this vtable is being |
| 2389 | /// built for. |
| 2390 | const CXXRecordDecl *MostDerivedClass; |
| 2391 | |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 2392 | /// LayoutClass - The most derived class used for virtual base layout |
| 2393 | /// information. |
| 2394 | const CXXRecordDecl *LayoutClass; |
Mike Stump | 4cde626 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 2395 | /// LayoutOffset - The offset for Class in LayoutClass. |
| 2396 | uint64_t LayoutOffset; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2397 | /// BLayout - Layout for the most derived class that this vtable is being |
| 2398 | /// built for. |
| 2399 | const ASTRecordLayout &BLayout; |
| 2400 | llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary; |
| 2401 | llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase; |
| 2402 | llvm::Constant *rtti; |
| 2403 | llvm::LLVMContext &VMContext; |
| 2404 | CodeGenModule &CGM; // Per-module state. |
Anders Carlsson | 0e88116 | 2009-12-04 03:46:21 +0000 | [diff] [blame] | 2405 | |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2406 | llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall; |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2407 | llvm::DenseMap<GlobalDecl, Index_t> VCallOffset; |
Mike Stump | d99a4d2 | 2010-01-26 03:42:22 +0000 | [diff] [blame] | 2408 | llvm::DenseMap<GlobalDecl, Index_t> VCallOffsetForVCall; |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 2409 | // This is the offset to the nearest virtual base |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2410 | llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2411 | llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex; |
Mike Stump | 94aff93 | 2009-10-27 23:46:47 +0000 | [diff] [blame] | 2412 | |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 2413 | /// PureVirtualFunction - Points to __cxa_pure_virtual. |
| 2414 | llvm::Constant *PureVirtualFn; |
| 2415 | |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 2416 | /// VtableMethods - A data structure for keeping track of methods in a vtable. |
| 2417 | /// Can add methods, override methods and iterate in vtable order. |
| 2418 | class VtableMethods { |
| 2419 | // MethodToIndexMap - Maps from a global decl to the index it has in the |
| 2420 | // Methods vector. |
| 2421 | llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap; |
| 2422 | |
| 2423 | /// Methods - The methods, in vtable order. |
| 2424 | typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy; |
| 2425 | MethodsVectorTy Methods; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2426 | MethodsVectorTy OrigMethods; |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 2427 | |
| 2428 | public: |
| 2429 | /// AddMethod - Add a method to the vtable methods. |
| 2430 | void AddMethod(GlobalDecl GD) { |
| 2431 | assert(!MethodToIndexMap.count(GD) && |
| 2432 | "Method has already been added!"); |
| 2433 | |
| 2434 | MethodToIndexMap[GD] = Methods.size(); |
| 2435 | Methods.push_back(GD); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2436 | OrigMethods.push_back(GD); |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | /// OverrideMethod - Replace a method with another. |
| 2440 | void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) { |
| 2441 | llvm::DenseMap<GlobalDecl, uint64_t>::iterator i |
| 2442 | = MethodToIndexMap.find(OverriddenGD); |
| 2443 | assert(i != MethodToIndexMap.end() && "Did not find entry!"); |
| 2444 | |
| 2445 | // Get the index of the old decl. |
| 2446 | uint64_t Index = i->second; |
| 2447 | |
| 2448 | // Replace the old decl with the new decl. |
| 2449 | Methods[Index] = GD; |
| 2450 | |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 2451 | // And add the new. |
| 2452 | MethodToIndexMap[GD] = Index; |
| 2453 | } |
| 2454 | |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 2455 | /// getIndex - Gives the index of a passed in GlobalDecl. Returns false if |
| 2456 | /// the index couldn't be found. |
Benjamin Kramer | cce9fde | 2009-12-04 22:45:27 +0000 | [diff] [blame] | 2457 | bool getIndex(GlobalDecl GD, uint64_t &Index) const { |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 2458 | llvm::DenseMap<GlobalDecl, uint64_t>::const_iterator i |
| 2459 | = MethodToIndexMap.find(GD); |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 2460 | |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 2461 | if (i == MethodToIndexMap.end()) |
| 2462 | return false; |
Anders Carlsson | c0c4993 | 2009-12-04 03:41:37 +0000 | [diff] [blame] | 2463 | |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 2464 | Index = i->second; |
| 2465 | return true; |
Anders Carlsson | c0c4993 | 2009-12-04 03:41:37 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2468 | GlobalDecl getOrigMethod(uint64_t Index) const { |
| 2469 | return OrigMethods[Index]; |
| 2470 | } |
| 2471 | |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 2472 | MethodsVectorTy::size_type size() const { |
| 2473 | return Methods.size(); |
| 2474 | } |
| 2475 | |
| 2476 | void clear() { |
| 2477 | MethodToIndexMap.clear(); |
| 2478 | Methods.clear(); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2479 | OrigMethods.clear(); |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Anders Carlsson | c0c4993 | 2009-12-04 03:41:37 +0000 | [diff] [blame] | 2482 | GlobalDecl operator[](uint64_t Index) const { |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 2483 | return Methods[Index]; |
| 2484 | } |
| 2485 | }; |
| 2486 | |
| 2487 | /// Methods - The vtable methods we're currently building. |
| 2488 | VtableMethods Methods; |
| 2489 | |
Anders Carlsson | 98fdb24 | 2009-12-04 02:26:15 +0000 | [diff] [blame] | 2490 | /// ThisAdjustments - For a given index in the vtable, contains the 'this' |
| 2491 | /// pointer adjustment needed for a method. |
| 2492 | typedef llvm::DenseMap<uint64_t, ThunkAdjustment> ThisAdjustmentsMapTy; |
| 2493 | ThisAdjustmentsMapTy ThisAdjustments; |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 2494 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2495 | SavedAdjustmentsVectorTy SavedAdjustments; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2496 | |
Anders Carlsson | a875670 | 2009-12-04 02:22:02 +0000 | [diff] [blame] | 2497 | /// BaseReturnTypes - Contains the base return types of methods who have been |
| 2498 | /// overridden with methods whose return types require adjustment. Used for |
| 2499 | /// generating covariant thunk information. |
| 2500 | typedef llvm::DenseMap<uint64_t, CanQualType> BaseReturnTypesMapTy; |
| 2501 | BaseReturnTypesMapTy BaseReturnTypes; |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 2502 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2503 | std::vector<Index_t> VCalls; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 2504 | |
| 2505 | typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 2506 | // subAddressPoints - Used to hold the AddressPoints (offsets) into the built |
| 2507 | // vtable for use in computing the initializers for the VTT. |
| 2508 | llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 2509 | |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 2510 | /// AddressPoints - Address points for this vtable. |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 2511 | AddressPointsMapTy& AddressPoints; |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 2512 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2513 | typedef CXXRecordDecl::method_iterator method_iter; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2514 | const uint32_t LLVMPointerWidth; |
| 2515 | Index_t extra; |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 2516 | typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 2517 | static llvm::DenseMap<CtorVtable_t, int64_t>& |
| 2518 | AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l, |
| 2519 | const CXXRecordDecl *c) { |
Anders Carlsson | 66d567d | 2010-03-25 00:51:13 +0000 | [diff] [blame] | 2520 | CodeGenVTables::AddrMap_t *&oref = cgm.getVTables().OldAddressPoints[l]; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 2521 | if (oref == 0) |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 2522 | oref = new CodeGenVTables::AddrMap_t; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 2523 | |
| 2524 | llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c]; |
| 2525 | if (ref == 0) |
| 2526 | ref = new llvm::DenseMap<CtorVtable_t, int64_t>; |
| 2527 | return *ref; |
| 2528 | } |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 2529 | |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2530 | bool DclIsSame(const FunctionDecl *New, const FunctionDecl *Old) { |
| 2531 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 2532 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 2533 | |
| 2534 | // C++ [temp.fct]p2: |
| 2535 | // A function template can be overloaded with other function templates |
| 2536 | // and with normal (non-template) functions. |
| 2537 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 2538 | return false; |
| 2539 | |
| 2540 | // Is the function New an overload of the function Old? |
| 2541 | QualType OldQType = CGM.getContext().getCanonicalType(Old->getType()); |
| 2542 | QualType NewQType = CGM.getContext().getCanonicalType(New->getType()); |
| 2543 | |
| 2544 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 2545 | // determine whether they are overloads. If we find any mismatch |
| 2546 | // in the signature, they are overloads. |
| 2547 | |
| 2548 | // If either of these functions is a K&R-style function (no |
| 2549 | // prototype), then we consider them to have matching signatures. |
| 2550 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 2551 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 2552 | return true; |
| 2553 | |
| 2554 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 2555 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 2556 | |
| 2557 | // The signature of a function includes the types of its |
| 2558 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 2559 | // of the ellipsis; see C++ DR 357). |
| 2560 | if (OldQType != NewQType && |
| 2561 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 2562 | OldType->isVariadic() != NewType->isVariadic() || |
| 2563 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 2564 | NewType->arg_type_begin()))) |
| 2565 | return false; |
| 2566 | |
| 2567 | #if 0 |
| 2568 | // C++ [temp.over.link]p4: |
| 2569 | // The signature of a function template consists of its function |
| 2570 | // signature, its return type and its template parameter list. The names |
| 2571 | // of the template parameters are significant only for establishing the |
| 2572 | // relationship between the template parameters and the rest of the |
| 2573 | // signature. |
| 2574 | // |
| 2575 | // We check the return type and template parameter lists for function |
| 2576 | // templates first; the remaining checks follow. |
| 2577 | if (NewTemplate && |
| 2578 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 2579 | OldTemplate->getTemplateParameters(), |
| 2580 | TPL_TemplateMatch) || |
| 2581 | OldType->getResultType() != NewType->getResultType())) |
| 2582 | return false; |
| 2583 | #endif |
| 2584 | |
| 2585 | // If the function is a class member, its signature includes the |
| 2586 | // cv-qualifiers (if any) on the function itself. |
| 2587 | // |
| 2588 | // As part of this, also check whether one of the member functions |
| 2589 | // is static, in which case they are not overloads (C++ |
| 2590 | // 13.1p2). While not part of the definition of the signature, |
| 2591 | // this check is important to determine whether these functions |
| 2592 | // can be overloaded. |
| 2593 | const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 2594 | const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 2595 | if (OldMethod && NewMethod && |
| 2596 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 2597 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 2598 | return false; |
| 2599 | |
| 2600 | // The signatures match; this is not an overload. |
| 2601 | return true; |
| 2602 | } |
| 2603 | |
| 2604 | typedef llvm::DenseMap<const CXXMethodDecl *, const CXXMethodDecl*> |
| 2605 | ForwardUnique_t; |
| 2606 | ForwardUnique_t ForwardUnique; |
| 2607 | llvm::DenseMap<const CXXMethodDecl*, const CXXMethodDecl*> UniqueOverrider; |
| 2608 | |
| 2609 | void BuildUniqueOverrider(const CXXMethodDecl *U, const CXXMethodDecl *MD) { |
| 2610 | const CXXMethodDecl *PrevU = UniqueOverrider[MD]; |
| 2611 | assert(U && "no unique overrider"); |
| 2612 | if (PrevU == U) |
| 2613 | return; |
| 2614 | if (PrevU != U && PrevU != 0) { |
| 2615 | // If already set, note the two sets as the same |
| 2616 | if (0) |
| 2617 | printf("%s::%s same as %s::%s\n", |
Douglas Gregor | aef605d | 2010-02-22 16:44:27 +0000 | [diff] [blame] | 2618 | PrevU->getParent()->getNameAsString().c_str(), |
| 2619 | PrevU->getNameAsString().c_str(), |
| 2620 | U->getParent()->getNameAsString().c_str(), |
| 2621 | U->getNameAsString().c_str()); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2622 | ForwardUnique[PrevU] = U; |
| 2623 | return; |
| 2624 | } |
| 2625 | |
| 2626 | // Not set, set it now |
| 2627 | if (0) |
| 2628 | printf("marking %s::%s %p override as %s::%s\n", |
Douglas Gregor | aef605d | 2010-02-22 16:44:27 +0000 | [diff] [blame] | 2629 | MD->getParent()->getNameAsString().c_str(), |
| 2630 | MD->getNameAsString().c_str(), |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2631 | (void*)MD, |
Douglas Gregor | aef605d | 2010-02-22 16:44:27 +0000 | [diff] [blame] | 2632 | U->getParent()->getNameAsString().c_str(), |
| 2633 | U->getNameAsString().c_str()); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2634 | UniqueOverrider[MD] = U; |
| 2635 | |
| 2636 | for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(), |
| 2637 | me = MD->end_overridden_methods(); mi != me; ++mi) { |
| 2638 | BuildUniqueOverrider(U, *mi); |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | void BuildUniqueOverriders(const CXXRecordDecl *RD) { |
| 2643 | if (0) printf("walking %s\n", RD->getNameAsCString()); |
| 2644 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 2645 | e = RD->method_end(); i != e; ++i) { |
| 2646 | const CXXMethodDecl *MD = *i; |
| 2647 | if (!MD->isVirtual()) |
| 2648 | continue; |
| 2649 | |
| 2650 | if (UniqueOverrider[MD] == 0) { |
| 2651 | // Only set this, if it hasn't been set yet. |
| 2652 | BuildUniqueOverrider(MD, MD); |
| 2653 | if (0) |
| 2654 | printf("top set is %s::%s %p\n", |
Douglas Gregor | aef605d | 2010-02-22 16:44:27 +0000 | [diff] [blame] | 2655 | MD->getParent()->getNameAsString().c_str(), |
| 2656 | MD->getNameAsString().c_str(), |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2657 | (void*)MD); |
| 2658 | ForwardUnique[MD] = MD; |
| 2659 | } |
| 2660 | } |
| 2661 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 2662 | e = RD->bases_end(); i != e; ++i) { |
| 2663 | const CXXRecordDecl *Base = |
| 2664 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 2665 | BuildUniqueOverriders(Base); |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | static int DclCmp(const void *p1, const void *p2) { |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 2670 | const CXXMethodDecl *MD1 = *(const CXXMethodDecl *const *)p1; |
| 2671 | const CXXMethodDecl *MD2 = *(const CXXMethodDecl *const *)p2; |
| 2672 | |
| 2673 | return (DeclarationName::compare(MD1->getDeclName(), MD2->getDeclName())); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | void MergeForwarding() { |
| 2677 | typedef llvm::SmallVector<const CXXMethodDecl *, 100> A_t; |
| 2678 | A_t A; |
| 2679 | for (ForwardUnique_t::iterator I = ForwardUnique.begin(), |
| 2680 | E = ForwardUnique.end(); I != E; ++I) { |
| 2681 | if (I->first == I->second) |
| 2682 | // Only add the roots of all trees |
| 2683 | A.push_back(I->first); |
| 2684 | } |
| 2685 | llvm::array_pod_sort(A.begin(), A.end(), DclCmp); |
| 2686 | for (A_t::iterator I = A.begin(), |
| 2687 | E = A.end(); I != E; ++I) { |
| 2688 | A_t::iterator J = I; |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 2689 | while (++J != E && DclCmp(I, J) == 0) |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2690 | if (DclIsSame(*I, *J)) { |
Douglas Gregor | aef605d | 2010-02-22 16:44:27 +0000 | [diff] [blame] | 2691 | if (0) printf("connecting %s\n", (*I)->getNameAsString().c_str()); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2692 | ForwardUnique[*J] = *I; |
| 2693 | } |
| 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | const CXXMethodDecl *getUnique(const CXXMethodDecl *MD) { |
| 2698 | const CXXMethodDecl *U = UniqueOverrider[MD]; |
| 2699 | assert(U && "unique overrider not found"); |
| 2700 | while (ForwardUnique.count(U)) { |
| 2701 | const CXXMethodDecl *NU = ForwardUnique[U]; |
| 2702 | if (NU == U) break; |
| 2703 | U = NU; |
| 2704 | } |
| 2705 | return U; |
| 2706 | } |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 2707 | |
| 2708 | GlobalDecl getUnique(GlobalDecl GD) { |
| 2709 | const CXXMethodDecl *Unique = getUnique(cast<CXXMethodDecl>(GD.getDecl())); |
| 2710 | |
| 2711 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Unique)) |
| 2712 | return GlobalDecl(CD, GD.getCtorType()); |
| 2713 | |
| 2714 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Unique)) |
| 2715 | return GlobalDecl(DD, GD.getDtorType()); |
| 2716 | |
| 2717 | return Unique; |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 2720 | /// getPureVirtualFn - Return the __cxa_pure_virtual function. |
| 2721 | llvm::Constant* getPureVirtualFn() { |
| 2722 | if (!PureVirtualFn) { |
| 2723 | const llvm::FunctionType *Ty = |
| 2724 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 2725 | /*isVarArg=*/false); |
| 2726 | PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual")); |
| 2727 | } |
| 2728 | |
| 2729 | return PureVirtualFn; |
| 2730 | } |
| 2731 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2732 | public: |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2733 | OldVtableBuilder(const CXXRecordDecl *MostDerivedClass, |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 2734 | const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm, |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 2735 | bool build, AddressPointsMapTy& AddressPoints) |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 2736 | : BuildVtable(build), MostDerivedClass(MostDerivedClass), LayoutClass(l), |
| 2737 | LayoutOffset(lo), BLayout(cgm.getContext().getASTRecordLayout(l)), |
| 2738 | rtti(0), VMContext(cgm.getModule().getContext()),CGM(cgm), |
| 2739 | PureVirtualFn(0), |
Anders Carlsson | ac3f7bd | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 2740 | subAddressPoints(AllocAddressPoint(cgm, l, MostDerivedClass)), |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 2741 | AddressPoints(AddressPoints), |
| 2742 | LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) |
| 2743 | { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2744 | Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 2745 | if (BuildVtable) { |
| 2746 | QualType ClassType = CGM.getContext().getTagDeclType(MostDerivedClass); |
| 2747 | rtti = CGM.GetAddrOfRTTIDescriptor(ClassType); |
| 2748 | } |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2749 | BuildUniqueOverriders(MostDerivedClass); |
| 2750 | MergeForwarding(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 2753 | // getVtableComponents - Returns a reference to the vtable components. |
| 2754 | const VtableComponentsVectorTy &getVtableComponents() const { |
| 2755 | return VtableComponents; |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 2758 | llvm::DenseMap<const CXXRecordDecl *, uint64_t> &getVBIndex() |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2759 | { return VBIndex; } |
| 2760 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2761 | SavedAdjustmentsVectorTy &getSavedAdjustments() |
| 2762 | { return SavedAdjustments; } |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2763 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2764 | llvm::Constant *wrap(Index_t i) { |
| 2765 | llvm::Constant *m; |
| 2766 | m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i); |
| 2767 | return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); |
| 2768 | } |
| 2769 | |
| 2770 | llvm::Constant *wrap(llvm::Constant *m) { |
| 2771 | return llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
| 2772 | } |
| 2773 | |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 2774 | //#define D1(x) |
Douglas Gregor | 12a9109 | 2010-02-22 16:48:26 +0000 | [diff] [blame] | 2775 | #define D1(X) do { if (getenv("CLANG_VTABLE_DEBUG")) { X; } } while (0) |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 2776 | |
| 2777 | void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset, |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 2778 | bool updateVBIndex, Index_t current_vbindex) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2779 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 2780 | e = RD->bases_end(); i != e; ++i) { |
| 2781 | const CXXRecordDecl *Base = |
| 2782 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 2783 | Index_t next_vbindex = current_vbindex; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2784 | if (i->isVirtual() && !SeenVBase.count(Base)) { |
| 2785 | SeenVBase.insert(Base); |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 2786 | if (updateVBIndex) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 2787 | next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8) |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 2788 | - 3*LLVMPointerWidth/8); |
| 2789 | VBIndex[Base] = next_vbindex; |
| 2790 | } |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 2791 | int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8; |
| 2792 | VCalls.push_back((0?700:0) + BaseOffset); |
| 2793 | D1(printf(" vbase for %s at %d delta %d most derived %s\n", |
| 2794 | Base->getNameAsCString(), |
| 2795 | (int)-VCalls.size()-3, (int)BaseOffset, |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 2796 | MostDerivedClass->getNameAsCString())); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2797 | } |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 2798 | // We also record offsets for non-virtual bases to closest enclosing |
| 2799 | // virtual base. We do this so that we don't have to search |
| 2800 | // for the nearst virtual base class when generating thunks. |
| 2801 | if (updateVBIndex && VBIndex.count(Base) == 0) |
| 2802 | VBIndex[Base] = next_vbindex; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 2803 | GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2804 | } |
| 2805 | } |
| 2806 | |
| 2807 | void StartNewTable() { |
| 2808 | SeenVBase.clear(); |
| 2809 | } |
| 2810 | |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 2811 | Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B, |
| 2812 | Index_t Offset = 0) { |
| 2813 | |
| 2814 | if (B == D) |
| 2815 | return Offset; |
| 2816 | |
| 2817 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D); |
| 2818 | for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(), |
| 2819 | e = D->bases_end(); i != e; ++i) { |
| 2820 | const CXXRecordDecl *Base = |
| 2821 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 2822 | int64_t BaseOffset = 0; |
| 2823 | if (!i->isVirtual()) |
| 2824 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 2825 | int64_t o = getNVOffset_1(Base, B, BaseOffset); |
| 2826 | if (o >= 0) |
| 2827 | return o; |
| 2828 | } |
| 2829 | |
| 2830 | return -1; |
| 2831 | } |
| 2832 | |
| 2833 | /// getNVOffset - Returns the non-virtual offset for the given (B) base of the |
| 2834 | /// derived class D. |
| 2835 | Index_t getNVOffset(QualType qB, QualType qD) { |
Mike Stump | 9c21289 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 2836 | qD = qD->getPointeeType(); |
| 2837 | qB = qB->getPointeeType(); |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 2838 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 2839 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
| 2840 | int64_t o = getNVOffset_1(D, B); |
| 2841 | if (o >= 0) |
| 2842 | return o; |
| 2843 | |
| 2844 | assert(false && "FIXME: non-virtual base not found"); |
| 2845 | return 0; |
| 2846 | } |
| 2847 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2848 | /// getVbaseOffset - Returns the index into the vtable for the virtual base |
| 2849 | /// offset for the given (B) virtual base of the derived class D. |
| 2850 | Index_t getVbaseOffset(QualType qB, QualType qD) { |
Mike Stump | 9c21289 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 2851 | qD = qD->getPointeeType(); |
| 2852 | qB = qB->getPointeeType(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2853 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 2854 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
Anders Carlsson | ac3f7bd | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 2855 | if (D != MostDerivedClass) |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 2856 | return CGM.getVTables().getVirtualBaseOffsetOffset(D, B); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2857 | llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i; |
| 2858 | i = VBIndex.find(B); |
| 2859 | if (i != VBIndex.end()) |
| 2860 | return i->second; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2861 | |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 2862 | assert(false && "FIXME: Base not found"); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2863 | return 0; |
| 2864 | } |
| 2865 | |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 2866 | bool OverrideMethod(GlobalDecl GD, bool MorallyVirtual, |
| 2867 | Index_t OverrideOffset, Index_t Offset, |
| 2868 | int64_t CurrentVBaseOffset); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2869 | |
Anders Carlsson | adfa267 | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 2870 | /// AppendMethods - Append the current methods to the vtable. |
Anders Carlsson | bf54027 | 2009-12-04 02:56:03 +0000 | [diff] [blame] | 2871 | void AppendMethodsToVtable(); |
Anders Carlsson | adfa267 | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 2872 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2873 | llvm::Constant *WrapAddrOf(GlobalDecl GD) { |
| 2874 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 2875 | |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 2876 | const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD); |
Mike Stump | 1ae3178 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 2877 | |
Mike Stump | c085a98 | 2009-12-03 16:55:20 +0000 | [diff] [blame] | 2878 | return wrap(CGM.GetAddrOfFunction(GD, Ty)); |
Mike Stump | 1ae3178 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 2881 | void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset, |
| 2882 | int64_t CurrentVBaseOffset) { |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 2883 | for (Path_t::reverse_iterator i = Path->rbegin(), |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2884 | e = Path->rend(); i != e; ++i) { |
| 2885 | const CXXRecordDecl *RD = i->first; |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 2886 | int64_t OverrideOffset = i->second; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2887 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 2888 | ++mi) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2889 | const CXXMethodDecl *MD = *mi; |
| 2890 | |
| 2891 | if (!MD->isVirtual()) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2892 | continue; |
| 2893 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2894 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 2895 | // Override both the complete and the deleting destructor. |
| 2896 | GlobalDecl CompDtor(DD, Dtor_Complete); |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 2897 | OverrideMethod(CompDtor, MorallyVirtual, OverrideOffset, Offset, |
| 2898 | CurrentVBaseOffset); |
| 2899 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2900 | GlobalDecl DeletingDtor(DD, Dtor_Deleting); |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 2901 | OverrideMethod(DeletingDtor, MorallyVirtual, OverrideOffset, Offset, |
| 2902 | CurrentVBaseOffset); |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2903 | } else { |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 2904 | OverrideMethod(MD, MorallyVirtual, OverrideOffset, Offset, |
| 2905 | CurrentVBaseOffset); |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2906 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2907 | } |
| 2908 | } |
| 2909 | } |
| 2910 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2911 | void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 2912 | int64_t CurrentVBaseOffset) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2913 | // If we can find a previously allocated slot for this, reuse it. |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 2914 | if (OverrideMethod(GD, MorallyVirtual, Offset, Offset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 2915 | CurrentVBaseOffset)) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2916 | return; |
| 2917 | |
Mike Stump | 372ade2 | 2010-01-22 18:48:47 +0000 | [diff] [blame] | 2918 | D1(printf(" vfn for %s at %d\n", |
Douglas Gregor | aef605d | 2010-02-22 16:44:27 +0000 | [diff] [blame] | 2919 | dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsString().c_str(), |
Mike Stump | 372ade2 | 2010-01-22 18:48:47 +0000 | [diff] [blame] | 2920 | (int)Methods.size())); |
| 2921 | |
Anders Carlsson | a7f1911 | 2009-12-04 02:08:24 +0000 | [diff] [blame] | 2922 | // We didn't find an entry in the vtable that we could use, add a new |
| 2923 | // entry. |
| 2924 | Methods.AddMethod(GD); |
| 2925 | |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 2926 | VCallOffset[GD] = Offset/8 - CurrentVBaseOffset/8; |
| 2927 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2928 | if (MorallyVirtual) { |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 2929 | GlobalDecl UGD = getUnique(GD); |
| 2930 | const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl()); |
| 2931 | |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2932 | assert(UMD && "final overrider not found"); |
| 2933 | |
| 2934 | Index_t &idx = VCall[UMD]; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2935 | // Allocate the first one, after that, we reuse the previous one. |
| 2936 | if (idx == 0) { |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 2937 | VCallOffsetForVCall[UGD] = Offset/8; |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 2938 | NonVirtualOffset[UMD] = Offset/8 - CurrentVBaseOffset/8; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2939 | idx = VCalls.size()+1; |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2940 | VCalls.push_back(Offset/8 - CurrentVBaseOffset/8); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 2941 | D1(printf(" vcall for %s at %d with delta %d\n", |
Douglas Gregor | aef605d | 2010-02-22 16:44:27 +0000 | [diff] [blame] | 2942 | dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsString().c_str(), |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 2943 | (int)-VCalls.size()-3, (int)VCalls[idx-1])); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2944 | } |
| 2945 | } |
| 2946 | } |
| 2947 | |
| 2948 | void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 2949 | Index_t Offset, int64_t CurrentVBaseOffset) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2950 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2951 | ++mi) { |
| 2952 | const CXXMethodDecl *MD = *mi; |
| 2953 | if (!MD->isVirtual()) |
| 2954 | continue; |
| 2955 | |
| 2956 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 2957 | // For destructors, add both the complete and the deleting destructor |
| 2958 | // to the vtable. |
| 2959 | AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 2960 | CurrentVBaseOffset); |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 2961 | AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset, |
| 2962 | CurrentVBaseOffset); |
| 2963 | } else |
| 2964 | AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2965 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, |
| 2969 | const CXXRecordDecl *PrimaryBase, |
| 2970 | bool PrimaryBaseWasVirtual, bool MorallyVirtual, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 2971 | int64_t Offset, int64_t CurrentVBaseOffset, |
| 2972 | Path_t *Path) { |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 2973 | Path->push_back(std::make_pair(RD, Offset)); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2974 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 2975 | e = RD->bases_end(); i != e; ++i) { |
| 2976 | if (i->isVirtual()) |
| 2977 | continue; |
| 2978 | const CXXRecordDecl *Base = |
| 2979 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 2980 | uint64_t o = Offset + Layout.getBaseClassOffset(Base); |
| 2981 | StartNewTable(); |
| 2982 | GenerateVtableForBase(Base, o, MorallyVirtual, false, |
| 2983 | true, Base == PrimaryBase && !PrimaryBaseWasVirtual, |
| 2984 | CurrentVBaseOffset, Path); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2985 | } |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 2986 | Path->pop_back(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 2989 | // #define D(X) do { X; } while (0) |
| 2990 | #define D(X) |
| 2991 | |
| 2992 | void insertVCalls(int InsertionPoint) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 2993 | D1(printf("============= combining vbase/vcall\n")); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 2994 | D(VCalls.insert(VCalls.begin(), 673)); |
| 2995 | D(VCalls.push_back(672)); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 2996 | |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 2997 | VtableComponents.insert(VtableComponents.begin() + InsertionPoint, |
| 2998 | VCalls.size(), 0); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 2999 | if (BuildVtable) { |
| 3000 | // The vcalls come first... |
| 3001 | for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(), |
| 3002 | e = VCalls.rend(); |
| 3003 | i != e; ++i) |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 3004 | VtableComponents[InsertionPoint++] = wrap((0?600:0) + *i); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 3005 | } |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 3006 | VCalls.clear(); |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 3007 | VCall.clear(); |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 3008 | VCallOffsetForVCall.clear(); |
| 3009 | VCallOffset.clear(); |
| 3010 | NonVirtualOffset.clear(); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 3013 | void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset, |
| 3014 | Index_t AddressPoint) { |
| 3015 | D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3016 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString(), |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 3017 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 3018 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 3019 | AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint; |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 3020 | |
| 3021 | // Now also add the address point for all our primary bases. |
| 3022 | while (1) { |
| 3023 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 3024 | RD = Layout.getPrimaryBase(); |
| 3025 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 3026 | // FIXME: Double check this. |
| 3027 | if (RD == 0) |
| 3028 | break; |
| 3029 | if (PrimaryBaseWasVirtual && |
| 3030 | BLayout.getVBaseClassOffset(RD) != Offset) |
| 3031 | break; |
| 3032 | D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3033 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString(), |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 3034 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 3035 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 3036 | AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint; |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 3041 | void FinishGenerateVtable(const CXXRecordDecl *RD, |
| 3042 | const ASTRecordLayout &Layout, |
| 3043 | const CXXRecordDecl *PrimaryBase, |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3044 | bool ForNPNVBases, bool WasPrimaryBase, |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 3045 | bool PrimaryBaseWasVirtual, |
| 3046 | bool MorallyVirtual, int64_t Offset, |
| 3047 | bool ForVirtualBase, int64_t CurrentVBaseOffset, |
| 3048 | Path_t *Path) { |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 3049 | bool alloc = false; |
| 3050 | if (Path == 0) { |
| 3051 | alloc = true; |
| 3052 | Path = new Path_t; |
| 3053 | } |
| 3054 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3055 | StartNewTable(); |
| 3056 | extra = 0; |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3057 | Index_t AddressPoint = 0; |
| 3058 | int VCallInsertionPoint = 0; |
| 3059 | if (!ForNPNVBases || !WasPrimaryBase) { |
| 3060 | bool DeferVCalls = MorallyVirtual || ForVirtualBase; |
| 3061 | VCallInsertionPoint = VtableComponents.size(); |
| 3062 | if (!DeferVCalls) { |
| 3063 | insertVCalls(VCallInsertionPoint); |
| 3064 | } else |
| 3065 | // FIXME: just for extra, or for all uses of VCalls.size post this? |
| 3066 | extra = -VCalls.size(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3067 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3068 | // Add the offset to top. |
| 3069 | VtableComponents.push_back(BuildVtable ? wrap(-((Offset-LayoutOffset)/8)) : 0); |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 3070 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3071 | // Add the RTTI information. |
| 3072 | VtableComponents.push_back(rtti); |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 3073 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3074 | AddressPoint = VtableComponents.size(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3075 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3076 | AppendMethodsToVtable(); |
| 3077 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3078 | |
| 3079 | // and then the non-virtual bases. |
| 3080 | NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 3081 | MorallyVirtual, Offset, CurrentVBaseOffset, Path); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 3082 | |
| 3083 | if (ForVirtualBase) { |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 3084 | // FIXME: We're adding to VCalls in callers, we need to do the overrides |
| 3085 | // in the inner part, so that we know the complete set of vcalls during |
| 3086 | // the build and don't have to insert into methods. Saving out the |
| 3087 | // AddressPoint here, would need to be fixed, if we didn't do that. Also |
| 3088 | // retroactively adding vcalls for overrides later wind up in the wrong |
| 3089 | // place, the vcall slot has to be alloted during the walk of the base |
| 3090 | // when the function is first introduces. |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 3091 | AddressPoint += VCalls.size(); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 3092 | insertVCalls(VCallInsertionPoint); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3095 | if (!ForNPNVBases || !WasPrimaryBase) |
| 3096 | AddAddressPoints(RD, Offset, AddressPoint); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 3097 | |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 3098 | if (alloc) { |
| 3099 | delete Path; |
| 3100 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3103 | void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 3104 | bool updateVBIndex, Index_t current_vbindex, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 3105 | int64_t CurrentVBaseOffset) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3106 | if (!RD->isDynamicClass()) |
| 3107 | return; |
| 3108 | |
| 3109 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 3110 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 3111 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 3112 | |
| 3113 | // vtables are composed from the chain of primaries. |
Eli Friedman | dfe33bb | 2009-12-04 08:52:11 +0000 | [diff] [blame] | 3114 | if (PrimaryBase && !PrimaryBaseWasVirtual) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3115 | D1(printf(" doing primaries for %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3116 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Eli Friedman | dfe33bb | 2009-12-04 08:52:11 +0000 | [diff] [blame] | 3117 | Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
| 3118 | updateVBIndex, current_vbindex, CurrentVBaseOffset); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | D1(printf(" doing vcall entries for %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3122 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3123 | |
| 3124 | // And add the virtuals for the class to the primary vtable. |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 3125 | AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 3129 | bool updateVBIndex, Index_t current_vbindex, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 3130 | bool RDisVirtualBase, int64_t CurrentVBaseOffset, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 3131 | bool bottom) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3132 | if (!RD->isDynamicClass()) |
| 3133 | return; |
| 3134 | |
| 3135 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 3136 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 3137 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 3138 | |
| 3139 | // vtables are composed from the chain of primaries. |
| 3140 | if (PrimaryBase) { |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 3141 | int BaseCurrentVBaseOffset = CurrentVBaseOffset; |
| 3142 | if (PrimaryBaseWasVirtual) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3143 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 3144 | BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase); |
| 3145 | } |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3146 | |
| 3147 | D1(printf(" doing primaries for %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3148 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3149 | |
| 3150 | VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 3151 | updateVBIndex, current_vbindex, PrimaryBaseWasVirtual, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 3152 | BaseCurrentVBaseOffset, false); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3155 | D1(printf(" doing vbase entries for %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3156 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3157 | GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex); |
| 3158 | |
| 3159 | if (RDisVirtualBase || bottom) { |
| 3160 | Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 3161 | CurrentVBaseOffset); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3162 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 3165 | void GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0, |
| 3166 | bool MorallyVirtual = false, |
| 3167 | bool ForVirtualBase = false, |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3168 | bool ForNPNVBases = false, |
| 3169 | bool WasPrimaryBase = true, |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 3170 | int CurrentVBaseOffset = 0, |
| 3171 | Path_t *Path = 0) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3172 | if (!RD->isDynamicClass()) |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 3173 | return; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3174 | |
Mike Stump | 92774d1 | 2009-11-13 02:35:38 +0000 | [diff] [blame] | 3175 | // Construction vtable don't need parts that have no virtual bases and |
| 3176 | // aren't morally virtual. |
Anders Carlsson | ac3f7bd | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 3177 | if ((LayoutClass != MostDerivedClass) && |
| 3178 | RD->getNumVBases() == 0 && !MorallyVirtual) |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 3179 | return; |
Mike Stump | 92774d1 | 2009-11-13 02:35:38 +0000 | [diff] [blame] | 3180 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3181 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 3182 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 3183 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 3184 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3185 | extra = 0; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3186 | D1(printf("building entries for base %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3187 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3188 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3189 | if (ForVirtualBase) |
| 3190 | extra = VCalls.size(); |
| 3191 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3192 | if (!ForNPNVBases || !WasPrimaryBase) { |
| 3193 | VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, |
| 3194 | ForVirtualBase, CurrentVBaseOffset, true); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3195 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3196 | if (Path) |
| 3197 | OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset); |
| 3198 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3199 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3200 | FinishGenerateVtable(RD, Layout, PrimaryBase, ForNPNVBases, WasPrimaryBase, |
| 3201 | PrimaryBaseWasVirtual, MorallyVirtual, Offset, |
| 3202 | ForVirtualBase, CurrentVBaseOffset, Path); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | void GenerateVtableForVBases(const CXXRecordDecl *RD, |
| 3206 | int64_t Offset = 0, |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 3207 | Path_t *Path = 0) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3208 | bool alloc = false; |
| 3209 | if (Path == 0) { |
| 3210 | alloc = true; |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 3211 | Path = new Path_t; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3212 | } |
| 3213 | // FIXME: We also need to override using all paths to a virtual base, |
| 3214 | // right now, we just process the first path |
| 3215 | Path->push_back(std::make_pair(RD, Offset)); |
| 3216 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 3217 | e = RD->bases_end(); i != e; ++i) { |
| 3218 | const CXXRecordDecl *Base = |
| 3219 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 3220 | if (i->isVirtual() && !IndirectPrimary.count(Base)) { |
| 3221 | // Mark it so we don't output it twice. |
| 3222 | IndirectPrimary.insert(Base); |
| 3223 | StartNewTable(); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 3224 | VCall.clear(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3225 | int64_t BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 3226 | int64_t CurrentVBaseOffset = BaseOffset; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 3227 | D1(printf("vtable %s virtual base %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3228 | MostDerivedClass->getNameAsCString(), Base->getNameAsCString())); |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 3229 | GenerateVtableForBase(Base, BaseOffset, true, true, false, |
| 3230 | true, CurrentVBaseOffset, Path); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3231 | } |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 3232 | int64_t BaseOffset; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3233 | if (i->isVirtual()) |
| 3234 | BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 3235 | else { |
| 3236 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 3237 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 3238 | } |
| 3239 | |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 3240 | if (Base->getNumVBases()) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3241 | GenerateVtableForVBases(Base, BaseOffset, Path); |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 3242 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3243 | } |
| 3244 | Path->pop_back(); |
| 3245 | if (alloc) |
| 3246 | delete Path; |
| 3247 | } |
| 3248 | }; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3249 | } // end anonymous namespace |
| 3250 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 3251 | bool OldVtableBuilder::OverrideMethod(GlobalDecl GD, bool MorallyVirtual, |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 3252 | Index_t OverrideOffset, Index_t Offset, |
| 3253 | int64_t CurrentVBaseOffset) { |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3254 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 3255 | |
| 3256 | const bool isPure = MD->isPure(); |
Anders Carlsson | 7ca4643 | 2009-12-05 17:04:47 +0000 | [diff] [blame] | 3257 | |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3258 | // FIXME: Should OverrideOffset's be Offset? |
| 3259 | |
Anders Carlsson | 7ca4643 | 2009-12-05 17:04:47 +0000 | [diff] [blame] | 3260 | for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(), |
| 3261 | e = MD->end_overridden_methods(); mi != e; ++mi) { |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3262 | GlobalDecl OGD; |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 3263 | GlobalDecl OGD2; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3264 | |
Anders Carlsson | 3aaf486 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 3265 | const CXXMethodDecl *OMD = *mi; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3266 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD)) |
| 3267 | OGD = GlobalDecl(DD, GD.getDtorType()); |
| 3268 | else |
| 3269 | OGD = OMD; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3270 | |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 3271 | // Check whether this is the method being overridden in this section of |
| 3272 | // the vtable. |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 3273 | uint64_t Index; |
| 3274 | if (!Methods.getIndex(OGD, Index)) |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3275 | continue; |
| 3276 | |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 3277 | OGD2 = OGD; |
| 3278 | |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 3279 | // Get the original method, which we should be computing thunks, etc, |
| 3280 | // against. |
| 3281 | OGD = Methods.getOrigMethod(Index); |
| 3282 | OMD = cast<CXXMethodDecl>(OGD.getDecl()); |
| 3283 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3284 | QualType ReturnType = |
| 3285 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 3286 | QualType OverriddenReturnType = |
| 3287 | OMD->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3288 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3289 | // Check if we need a return type adjustment. |
Anders Carlsson | df31af1 | 2010-02-13 23:17:31 +0000 | [diff] [blame] | 3290 | if (!ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD, |
| 3291 | OMD).isEmpty()) { |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3292 | CanQualType &BaseReturnType = BaseReturnTypes[Index]; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3293 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3294 | // Insert the base return type. |
| 3295 | if (BaseReturnType.isNull()) |
| 3296 | BaseReturnType = |
| 3297 | CGM.getContext().getCanonicalType(OverriddenReturnType); |
| 3298 | } |
Anders Carlsson | dd454be | 2009-12-04 03:52:52 +0000 | [diff] [blame] | 3299 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3300 | Methods.OverrideMethod(OGD, GD); |
| 3301 | |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 3302 | GlobalDecl UGD = getUnique(GD); |
| 3303 | const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl()); |
| 3304 | assert(UGD.getDecl() && "unique overrider not found"); |
| 3305 | assert(UGD == getUnique(OGD) && "unique overrider not unique"); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 3306 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3307 | ThisAdjustments.erase(Index); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 3308 | if (MorallyVirtual || VCall.count(UMD)) { |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 3309 | |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 3310 | Index_t &idx = VCall[UMD]; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3311 | if (idx == 0) { |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 3312 | VCallOffset[GD] = VCallOffset[OGD]; |
| 3313 | // NonVirtualOffset[UMD] = CurrentVBaseOffset/8 - OverrideOffset/8; |
| 3314 | NonVirtualOffset[UMD] = VCallOffset[OGD]; |
Mike Stump | d99a4d2 | 2010-01-26 03:42:22 +0000 | [diff] [blame] | 3315 | VCallOffsetForVCall[UMD] = OverrideOffset/8; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3316 | idx = VCalls.size()+1; |
Mike Stump | d99a4d2 | 2010-01-26 03:42:22 +0000 | [diff] [blame] | 3317 | VCalls.push_back(OverrideOffset/8 - CurrentVBaseOffset/8); |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3318 | D1(printf(" vcall for %s at %d with delta %d most derived %s\n", |
| 3319 | MD->getNameAsString().c_str(), (int)-idx-3, |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3320 | (int)VCalls[idx-1], MostDerivedClass->getNameAsCString())); |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3321 | } else { |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 3322 | VCallOffset[GD] = NonVirtualOffset[UMD]; |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 3323 | VCalls[idx-1] = -VCallOffsetForVCall[UGD] + OverrideOffset/8; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3324 | D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n", |
| 3325 | MD->getNameAsString().c_str(), (int)-idx-3, |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 3326 | (int)VCalls[idx-1], MostDerivedClass->getNameAsCString())); |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3327 | } |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 3328 | int64_t NonVirtualAdjustment = -VCallOffset[OGD]; |
Mike Stump | 852f5ce | 2010-01-26 22:44:01 +0000 | [diff] [blame] | 3329 | QualType DerivedType = MD->getThisType(CGM.getContext()); |
| 3330 | QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext()); |
| 3331 | int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8); |
| 3332 | if (NonVirtualAdjustment2 != NonVirtualAdjustment) { |
| 3333 | NonVirtualAdjustment = NonVirtualAdjustment2; |
| 3334 | } |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3335 | int64_t VirtualAdjustment = |
| 3336 | -((idx + extra + 2) * LLVMPointerWidth / 8); |
Anders Carlsson | 891bb4b | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 3337 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3338 | // Optimize out virtual adjustments of 0. |
| 3339 | if (VCalls[idx-1] == 0) |
| 3340 | VirtualAdjustment = 0; |
Anders Carlsson | 891bb4b | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 3341 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3342 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, |
| 3343 | VirtualAdjustment); |
Anders Carlsson | bc0e339 | 2009-12-03 02:22:59 +0000 | [diff] [blame] | 3344 | |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 3345 | if (!isPure && !ThisAdjustment.isEmpty()) { |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3346 | ThisAdjustments[Index] = ThisAdjustment; |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 3347 | SavedAdjustments.push_back( |
| 3348 | std::make_pair(GD, std::make_pair(OGD, ThisAdjustment))); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 3349 | } |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3350 | return true; |
| 3351 | } |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3352 | |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 3353 | VCallOffset[GD] = VCallOffset[OGD2] - OverrideOffset/8; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3354 | |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 3355 | int64_t NonVirtualAdjustment = -VCallOffset[GD]; |
| 3356 | QualType DerivedType = MD->getThisType(CGM.getContext()); |
| 3357 | QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext()); |
| 3358 | int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8); |
| 3359 | if (NonVirtualAdjustment2 != NonVirtualAdjustment) { |
| 3360 | NonVirtualAdjustment = NonVirtualAdjustment2; |
| 3361 | } |
| 3362 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3363 | if (NonVirtualAdjustment) { |
| 3364 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0); |
| 3365 | |
Eli Friedman | f062d9d | 2009-12-06 22:33:51 +0000 | [diff] [blame] | 3366 | if (!isPure) { |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3367 | ThisAdjustments[Index] = ThisAdjustment; |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 3368 | SavedAdjustments.push_back( |
| 3369 | std::make_pair(GD, std::make_pair(OGD, ThisAdjustment))); |
Eli Friedman | f062d9d | 2009-12-06 22:33:51 +0000 | [diff] [blame] | 3370 | } |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 3371 | } |
| 3372 | return true; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | return false; |
Anders Carlsson | 27f69d0 | 2009-11-27 22:21:51 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 3378 | void OldVtableBuilder::AppendMethodsToVtable() { |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 3379 | if (!BuildVtable) { |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 3380 | VtableComponents.insert(VtableComponents.end(), Methods.size(), |
| 3381 | (llvm::Constant *)0); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 3382 | ThisAdjustments.clear(); |
| 3383 | BaseReturnTypes.clear(); |
| 3384 | Methods.clear(); |
| 3385 | return; |
| 3386 | } |
| 3387 | |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 3388 | // Reserve room in the vtable for our new methods. |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 3389 | VtableComponents.reserve(VtableComponents.size() + Methods.size()); |
Anders Carlsson | 29202d5 | 2009-12-04 03:07:26 +0000 | [diff] [blame] | 3390 | |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 3391 | for (unsigned i = 0, e = Methods.size(); i != e; ++i) { |
| 3392 | GlobalDecl GD = Methods[i]; |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 3393 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 3394 | |
| 3395 | // Get the 'this' pointer adjustment. |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 3396 | ThunkAdjustment ThisAdjustment = ThisAdjustments.lookup(i); |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 3397 | |
| 3398 | // Construct the return type adjustment. |
| 3399 | ThunkAdjustment ReturnAdjustment; |
| 3400 | |
| 3401 | QualType BaseReturnType = BaseReturnTypes.lookup(i); |
| 3402 | if (!BaseReturnType.isNull() && !MD->isPure()) { |
| 3403 | QualType DerivedType = |
| 3404 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 3405 | |
| 3406 | int64_t NonVirtualAdjustment = |
| 3407 | getNVOffset(BaseReturnType, DerivedType) / 8; |
| 3408 | |
| 3409 | int64_t VirtualAdjustment = |
| 3410 | getVbaseOffset(BaseReturnType, DerivedType); |
| 3411 | |
| 3412 | ReturnAdjustment = ThunkAdjustment(NonVirtualAdjustment, |
| 3413 | VirtualAdjustment); |
| 3414 | } |
| 3415 | |
Anders Carlsson | 2fce216 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 3416 | llvm::Constant *Method = 0; |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 3417 | if (!ReturnAdjustment.isEmpty()) { |
| 3418 | // Build a covariant thunk. |
| 3419 | CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 3420 | Method = wrap(CGM.GetAddrOfCovariantThunk(GD, Adjustment)); |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 3421 | } else if (!ThisAdjustment.isEmpty()) { |
| 3422 | // Build a "regular" thunk. |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 3423 | Method = wrap(CGM.GetAddrOfThunk(GD, ThisAdjustment)); |
Anders Carlsson | bf54027 | 2009-12-04 02:56:03 +0000 | [diff] [blame] | 3424 | } else if (MD->isPure()) { |
| 3425 | // We have a pure virtual method. |
Anders Carlsson | 2fce216 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 3426 | Method = getPureVirtualFn(); |
| 3427 | } else { |
| 3428 | // We have a good old regular method. |
| 3429 | Method = WrapAddrOf(GD); |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 3430 | } |
Anders Carlsson | 2fce216 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 3431 | |
| 3432 | // Add the method to the vtable. |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 3433 | VtableComponents.push_back(Method); |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
Anders Carlsson | 2fce216 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 3436 | |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 3437 | ThisAdjustments.clear(); |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 3438 | BaseReturnTypes.clear(); |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 3439 | |
Anders Carlsson | adfa267 | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 3440 | Methods.clear(); |
Anders Carlsson | adfa267 | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 3443 | void CodeGenVTables::ComputeMethodVtableIndices(const CXXRecordDecl *RD) { |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3444 | |
| 3445 | // Itanium C++ ABI 2.5.2: |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 3446 | // The order of the virtual function pointers in a virtual table is the |
| 3447 | // order of declaration of the corresponding member functions in the class. |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3448 | // |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 3449 | // There is an entry for any virtual function declared in a class, |
| 3450 | // whether it is a new function or overrides a base class function, |
| 3451 | // unless it overrides a function from the primary base, and conversion |
| 3452 | // between their return types does not require an adjustment. |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3453 | |
| 3454 | int64_t CurrentIndex = 0; |
| 3455 | |
| 3456 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 3457 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 3458 | |
| 3459 | if (PrimaryBase) { |
Anders Carlsson | 0121fbd | 2009-11-30 19:43:26 +0000 | [diff] [blame] | 3460 | assert(PrimaryBase->isDefinition() && |
| 3461 | "Should have the definition decl of the primary base!"); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3462 | |
| 3463 | // Since the record decl shares its vtable pointer with the primary base |
| 3464 | // we need to start counting at the end of the primary base's vtable. |
| 3465 | CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase); |
| 3466 | } |
Eli Friedman | ea5ae31 | 2009-12-15 03:31:17 +0000 | [diff] [blame] | 3467 | |
| 3468 | // Collect all the primary bases, so we can check whether methods override |
| 3469 | // a method from the base. |
Anders Carlsson | b9021e9 | 2010-02-27 16:18:19 +0000 | [diff] [blame] | 3470 | VtableBuilder::PrimaryBasesSetVectorTy PrimaryBases; |
Eli Friedman | ea5ae31 | 2009-12-15 03:31:17 +0000 | [diff] [blame] | 3471 | for (ASTRecordLayout::primary_base_info_iterator |
| 3472 | I = Layout.primary_base_begin(), E = Layout.primary_base_end(); |
| 3473 | I != E; ++I) |
| 3474 | PrimaryBases.insert((*I).getBase()); |
| 3475 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3476 | const CXXDestructorDecl *ImplicitVirtualDtor = 0; |
| 3477 | |
| 3478 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 3479 | e = RD->method_end(); i != e; ++i) { |
| 3480 | const CXXMethodDecl *MD = *i; |
| 3481 | |
| 3482 | // We only want virtual methods. |
| 3483 | if (!MD->isVirtual()) |
| 3484 | continue; |
| 3485 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3486 | // Check if this method overrides a method in the primary base. |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 3487 | if (const CXXMethodDecl *OverriddenMD = |
Anders Carlsson | c784ba2 | 2010-02-27 06:38:03 +0000 | [diff] [blame] | 3488 | OverridesMethodInBases(MD, PrimaryBases)) { |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 3489 | // Check if converting from the return type of the method to the |
| 3490 | // return type of the overridden method requires conversion. |
Anders Carlsson | df31af1 | 2010-02-13 23:17:31 +0000 | [diff] [blame] | 3491 | if (ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD, |
| 3492 | OverriddenMD).isEmpty()) { |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 3493 | // This index is shared between the index in the vtable of the primary |
| 3494 | // base class. |
| 3495 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 3496 | const CXXDestructorDecl *OverriddenDD = |
| 3497 | cast<CXXDestructorDecl>(OverriddenMD); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3498 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 3499 | // Add both the complete and deleting entries. |
| 3500 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = |
| 3501 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete)); |
| 3502 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = |
| 3503 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting)); |
| 3504 | } else { |
| 3505 | MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD); |
| 3506 | } |
| 3507 | |
| 3508 | // We don't need to add an entry for this method. |
Anders Carlsson | c1eec89 | 2010-02-12 18:14:46 +0000 | [diff] [blame] | 3509 | continue; |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3510 | } |
| 3511 | } |
| 3512 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3513 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 3514 | if (MD->isImplicit()) { |
| 3515 | assert(!ImplicitVirtualDtor && |
| 3516 | "Did already see an implicit virtual dtor!"); |
| 3517 | ImplicitVirtualDtor = DD; |
| 3518 | continue; |
| 3519 | } |
| 3520 | |
| 3521 | // Add the complete dtor. |
| 3522 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++; |
| 3523 | |
| 3524 | // Add the deleting dtor. |
| 3525 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++; |
| 3526 | } else { |
| 3527 | // Add the entry. |
| 3528 | MethodVtableIndices[MD] = CurrentIndex++; |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | if (ImplicitVirtualDtor) { |
| 3533 | // Itanium C++ ABI 2.5.2: |
Anders Carlsson | a661a54 | 2010-02-16 04:49:44 +0000 | [diff] [blame] | 3534 | // If a class has an implicitly-defined virtual destructor, |
| 3535 | // its entries come after the declared virtual function pointers. |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3536 | |
| 3537 | // Add the complete dtor. |
| 3538 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] = |
| 3539 | CurrentIndex++; |
| 3540 | |
| 3541 | // Add the deleting dtor. |
| 3542 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] = |
| 3543 | CurrentIndex++; |
| 3544 | } |
| 3545 | |
| 3546 | NumVirtualFunctionPointers[RD] = CurrentIndex; |
| 3547 | } |
| 3548 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 3549 | uint64_t CodeGenVTables::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) { |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3550 | llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 3551 | NumVirtualFunctionPointers.find(RD); |
| 3552 | if (I != NumVirtualFunctionPointers.end()) |
| 3553 | return I->second; |
| 3554 | |
| 3555 | ComputeMethodVtableIndices(RD); |
| 3556 | |
| 3557 | I = NumVirtualFunctionPointers.find(RD); |
| 3558 | assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!"); |
| 3559 | return I->second; |
| 3560 | } |
| 3561 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 3562 | uint64_t CodeGenVTables::getMethodVtableIndex(GlobalDecl GD) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 3563 | MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3564 | if (I != MethodVtableIndices.end()) |
| 3565 | return I->second; |
| 3566 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 3567 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 3568 | |
| 3569 | ComputeMethodVtableIndices(RD); |
| 3570 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 3571 | I = MethodVtableIndices.find(GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3572 | assert(I != MethodVtableIndices.end() && "Did not find index!"); |
| 3573 | return I->second; |
| 3574 | } |
| 3575 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 3576 | int64_t CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, |
| 3577 | const CXXRecordDecl *VBase) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3578 | ClassPairTy ClassPair(RD, VBase); |
| 3579 | |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 3580 | VirtualBaseClassOffsetOffsetsMapTy::iterator I = |
| 3581 | VirtualBaseClassOffsetOffsets.find(ClassPair); |
| 3582 | if (I != VirtualBaseClassOffsetOffsets.end()) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3583 | return I->second; |
| 3584 | |
Anders Carlsson | c89a7cf | 2010-03-11 16:06:20 +0000 | [diff] [blame] | 3585 | VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0, |
| 3586 | BaseSubobject(RD, 0), |
| 3587 | /*BaseIsVirtual=*/false, |
| 3588 | /*OffsetInLayoutClass=*/0); |
Anders Carlsson | c89a7cf | 2010-03-11 16:06:20 +0000 | [diff] [blame] | 3589 | |
| 3590 | for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I = |
| 3591 | Builder.getVBaseOffsetOffsets().begin(), |
| 3592 | E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3593 | // Insert all types. |
| 3594 | ClassPairTy ClassPair(RD, I->first); |
| 3595 | |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 3596 | VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second)); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 3599 | I = VirtualBaseClassOffsetOffsets.find(ClassPair); |
Anders Carlsson | c89a7cf | 2010-03-11 16:06:20 +0000 | [diff] [blame] | 3600 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 3601 | // FIXME: The assertion below assertion currently fails with the old vtable |
| 3602 | /// layout code if there is a non-virtual thunk adjustment in a vtable. |
| 3603 | // Once the new layout is in place, this return should be removed. |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 3604 | if (I == VirtualBaseClassOffsetOffsets.end()) |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 3605 | return 0; |
| 3606 | |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 3607 | assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!"); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3608 | |
| 3609 | return I->second; |
| 3610 | } |
| 3611 | |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 3612 | const CodeGenVTables::AddrSubMap_t & |
| 3613 | CodeGenVTables::getAddressPoints(const CXXRecordDecl *RD) { |
Anders Carlsson | 66d567d | 2010-03-25 00:51:13 +0000 | [diff] [blame] | 3614 | if (!OldAddressPoints[RD]) { |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 3615 | OldVtableBuilder::AddressPointsMapTy AddressPoints; |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 3616 | OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints); |
| 3617 | |
| 3618 | b.GenerateVtableForBase(RD, 0); |
| 3619 | b.GenerateVtableForVBases(RD, 0); |
| 3620 | } |
| 3621 | |
Anders Carlsson | 66d567d | 2010-03-25 00:51:13 +0000 | [diff] [blame] | 3622 | return *(*OldAddressPoints[RD])[RD]; |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 3625 | llvm::GlobalVariable * |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 3626 | CodeGenVTables::GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage, |
| 3627 | bool GenerateDefinition, |
| 3628 | const CXXRecordDecl *LayoutClass, |
| 3629 | const CXXRecordDecl *RD, uint64_t Offset, |
| 3630 | bool IsVirtual, |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 3631 | llvm::DenseMap<BaseSubobject, uint64_t> &AddressPoints) { |
Anders Carlsson | d03a326 | 2010-02-27 20:39:05 +0000 | [diff] [blame] | 3632 | if (GenerateDefinition) { |
| 3633 | if (LayoutClass == RD) { |
Anders Carlsson | 5d7af6b | 2010-02-28 00:36:23 +0000 | [diff] [blame] | 3634 | assert(!IsVirtual && |
Anders Carlsson | e02fc0a | 2010-03-11 05:48:21 +0000 | [diff] [blame] | 3635 | "Can only have a virtual base in construction vtables!"); |
| 3636 | assert(!Offset && |
| 3637 | "Can only have a base offset in construction vtables!"); |
Anders Carlsson | d03a326 | 2010-02-27 20:39:05 +0000 | [diff] [blame] | 3638 | } |
Anders Carlsson | e02fc0a | 2010-03-11 05:48:21 +0000 | [diff] [blame] | 3639 | |
| 3640 | VtableBuilder Builder(*this, RD, Offset, |
Anders Carlsson | 1ffd79b | 2010-03-11 06:10:52 +0000 | [diff] [blame] | 3641 | /*MostDerivedClassIsVirtual=*/IsVirtual, |
Anders Carlsson | e02fc0a | 2010-03-11 05:48:21 +0000 | [diff] [blame] | 3642 | LayoutClass); |
| 3643 | |
| 3644 | if (CGM.getLangOptions().DumpVtableLayouts) |
| 3645 | Builder.dumpLayout(llvm::errs()); |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3648 | llvm::SmallString<256> OutName; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 3649 | if (LayoutClass != RD) |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 3650 | CGM.getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset / 8, |
| 3651 | RD, OutName); |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 3652 | else |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 3653 | CGM.getMangleContext().mangleCXXVtable(RD, OutName); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 3654 | llvm::StringRef Name = OutName.str(); |
Benjamin Kramer | 7a9474e | 2009-10-11 22:57:54 +0000 | [diff] [blame] | 3655 | |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 3656 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 3657 | if (GV == 0 || GV->isDeclaration()) { |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 3658 | OldVtableBuilder b(RD, LayoutClass, Offset, CGM, GenerateDefinition, |
| 3659 | AddressPoints); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 3660 | |
| 3661 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
| 3662 | // First comes the vtables for all the non-virtual bases... |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 3663 | b.GenerateVtableForBase(RD, Offset); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 3664 | |
| 3665 | // then the vtables for all the virtual bases. |
| 3666 | b.GenerateVtableForVBases(RD, Offset); |
| 3667 | |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 3668 | llvm::Constant *Init = 0; |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 3669 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 3670 | llvm::ArrayType *ArrayType = |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 3671 | llvm::ArrayType::get(Int8PtrTy, b.getVtableComponents().size()); |
Anders Carlsson | e40477c | 2009-12-05 21:09:05 +0000 | [diff] [blame] | 3672 | |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 3673 | if (GenerateDefinition) |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 3674 | Init = llvm::ConstantArray::get(ArrayType, &b.getVtableComponents()[0], |
| 3675 | b.getVtableComponents().size()); |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 3676 | |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 3677 | llvm::GlobalVariable *OGV = GV; |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 3678 | |
| 3679 | GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType, |
| 3680 | /*isConstant=*/true, Linkage, Init, Name); |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 3681 | CGM.setGlobalVisibility(GV, RD); |
| 3682 | |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 3683 | if (OGV) { |
| 3684 | GV->takeName(OGV); |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 3685 | llvm::Constant *NewPtr = |
| 3686 | llvm::ConstantExpr::getBitCast(GV, OGV->getType()); |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 3687 | OGV->replaceAllUsesWith(NewPtr); |
| 3688 | OGV->eraseFromParent(); |
| 3689 | } |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 3690 | } |
Anders Carlsson | bb27d86 | 2009-12-05 21:28:12 +0000 | [diff] [blame] | 3691 | |
| 3692 | return GV; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 3693 | } |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 3694 | |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 3695 | llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, |
| 3696 | const ThunkInfo &Thunk) { |
| 3697 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 3698 | |
| 3699 | // Compute the mangled name. |
| 3700 | llvm::SmallString<256> Name; |
| 3701 | if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) |
| 3702 | getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), Thunk.This, |
| 3703 | Name); |
| 3704 | else |
| 3705 | getMangleContext().mangleThunk(MD, Thunk, Name); |
| 3706 | |
| 3707 | const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD); |
| 3708 | return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl()); |
| 3709 | } |
| 3710 | |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 3711 | static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF, |
| 3712 | llvm::Value *Ptr, |
| 3713 | int64_t NonVirtualAdjustment, |
| 3714 | int64_t VirtualAdjustment) { |
| 3715 | if (!NonVirtualAdjustment && !VirtualAdjustment) |
| 3716 | return Ptr; |
| 3717 | |
| 3718 | const llvm::Type *Int8PtrTy = |
| 3719 | llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 3720 | |
| 3721 | llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy); |
| 3722 | |
| 3723 | if (NonVirtualAdjustment) { |
| 3724 | // Do the non-virtual adjustment. |
| 3725 | V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); |
| 3726 | } |
| 3727 | |
| 3728 | if (VirtualAdjustment) { |
| 3729 | const llvm::Type *PtrDiffTy = |
| 3730 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 3731 | |
| 3732 | // Do the virtual adjustment. |
| 3733 | llvm::Value *VTablePtrPtr = |
| 3734 | CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo()); |
| 3735 | |
| 3736 | llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); |
| 3737 | |
| 3738 | llvm::Value *OffsetPtr = |
| 3739 | CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); |
| 3740 | |
| 3741 | OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); |
| 3742 | |
| 3743 | // Load the adjustment offset from the vtable. |
| 3744 | llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr); |
| 3745 | |
| 3746 | // Adjust our pointer. |
| 3747 | V = CGF.Builder.CreateInBoundsGEP(V, Offset); |
| 3748 | } |
| 3749 | |
| 3750 | // Cast back to the original type. |
| 3751 | return CGF.Builder.CreateBitCast(V, Ptr->getType()); |
| 3752 | } |
| 3753 | |
| 3754 | void CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD, |
| 3755 | const ThunkInfo &Thunk) { |
| 3756 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 3757 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 3758 | QualType ResultType = FPT->getResultType(); |
| 3759 | QualType ThisType = MD->getThisType(getContext()); |
| 3760 | |
| 3761 | FunctionArgList FunctionArgs; |
| 3762 | |
| 3763 | // FIXME: It would be nice if more of this code could be shared with |
| 3764 | // CodeGenFunction::GenerateCode. |
| 3765 | |
| 3766 | // Create the implicit 'this' parameter declaration. |
| 3767 | CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, |
| 3768 | MD->getLocation(), |
| 3769 | &getContext().Idents.get("this"), |
| 3770 | ThisType); |
| 3771 | |
| 3772 | // Add the 'this' parameter. |
| 3773 | FunctionArgs.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType())); |
| 3774 | |
| 3775 | // Add the rest of the parameters. |
| 3776 | for (FunctionDecl::param_const_iterator I = MD->param_begin(), |
| 3777 | E = MD->param_end(); I != E; ++I) { |
| 3778 | ParmVarDecl *Param = *I; |
| 3779 | |
| 3780 | FunctionArgs.push_back(std::make_pair(Param, Param->getType())); |
| 3781 | } |
| 3782 | |
| 3783 | StartFunction(GlobalDecl(), ResultType, Fn, FunctionArgs, SourceLocation()); |
| 3784 | |
| 3785 | // Adjust the 'this' pointer if necessary. |
| 3786 | llvm::Value *AdjustedThisPtr = |
| 3787 | PerformTypeAdjustment(*this, LoadCXXThis(), |
| 3788 | Thunk.This.NonVirtual, |
| 3789 | Thunk.This.VCallOffsetOffset); |
| 3790 | |
| 3791 | CallArgList CallArgs; |
| 3792 | |
| 3793 | // Add our adjusted 'this' pointer. |
| 3794 | CallArgs.push_back(std::make_pair(RValue::get(AdjustedThisPtr), ThisType)); |
| 3795 | |
| 3796 | // Add the rest of the parameters. |
| 3797 | for (FunctionDecl::param_const_iterator I = MD->param_begin(), |
| 3798 | E = MD->param_end(); I != E; ++I) { |
| 3799 | ParmVarDecl *Param = *I; |
| 3800 | QualType ArgType = Param->getType(); |
| 3801 | |
| 3802 | // FIXME: Declaring a DeclRefExpr on the stack is kinda icky. |
| 3803 | DeclRefExpr ArgExpr(Param, ArgType.getNonReferenceType(), SourceLocation()); |
| 3804 | CallArgs.push_back(std::make_pair(EmitCallArg(&ArgExpr, ArgType), ArgType)); |
| 3805 | } |
| 3806 | |
| 3807 | // Get our callee. |
| 3808 | const llvm::Type *Ty = |
| 3809 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 3810 | FPT->isVariadic()); |
| 3811 | llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty); |
| 3812 | |
| 3813 | const CGFunctionInfo &FnInfo = |
| 3814 | CGM.getTypes().getFunctionInfo(ResultType, CallArgs, FPT->getCallConv(), |
| 3815 | FPT->getNoReturnAttr()); |
| 3816 | |
| 3817 | // Now emit our call. |
| 3818 | RValue RV = EmitCall(FnInfo, Callee, ReturnValueSlot(), CallArgs, MD); |
| 3819 | |
| 3820 | if (!Thunk.Return.isEmpty()) { |
| 3821 | // Emit the return adjustment. |
| 3822 | bool NullCheckValue = !ResultType->isReferenceType(); |
| 3823 | |
| 3824 | llvm::BasicBlock *AdjustNull = 0; |
| 3825 | llvm::BasicBlock *AdjustNotNull = 0; |
| 3826 | llvm::BasicBlock *AdjustEnd = 0; |
| 3827 | |
| 3828 | llvm::Value *ReturnValue = RV.getScalarVal(); |
| 3829 | |
| 3830 | if (NullCheckValue) { |
| 3831 | AdjustNull = createBasicBlock("adjust.null"); |
| 3832 | AdjustNotNull = createBasicBlock("adjust.notnull"); |
| 3833 | AdjustEnd = createBasicBlock("adjust.end"); |
| 3834 | |
| 3835 | llvm::Value *IsNull = Builder.CreateIsNull(ReturnValue); |
| 3836 | Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull); |
| 3837 | EmitBlock(AdjustNotNull); |
| 3838 | } |
| 3839 | |
| 3840 | ReturnValue = PerformTypeAdjustment(*this, ReturnValue, |
| 3841 | Thunk.Return.NonVirtual, |
| 3842 | Thunk.Return.VBaseOffsetOffset); |
| 3843 | |
| 3844 | if (NullCheckValue) { |
| 3845 | Builder.CreateBr(AdjustEnd); |
| 3846 | EmitBlock(AdjustNull); |
| 3847 | Builder.CreateBr(AdjustEnd); |
| 3848 | EmitBlock(AdjustEnd); |
| 3849 | |
| 3850 | llvm::PHINode *PHI = Builder.CreatePHI(ReturnValue->getType()); |
| 3851 | PHI->reserveOperandSpace(2); |
| 3852 | PHI->addIncoming(ReturnValue, AdjustNotNull); |
| 3853 | PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()), |
| 3854 | AdjustNull); |
| 3855 | ReturnValue = PHI; |
| 3856 | } |
| 3857 | |
| 3858 | RV = RValue::get(ReturnValue); |
| 3859 | } |
| 3860 | |
| 3861 | if (!ResultType->isVoidType()) |
| 3862 | EmitReturnOfRValue(RV, ResultType); |
| 3863 | |
| 3864 | FinishFunction(); |
| 3865 | |
| 3866 | // Destroy the 'this' declaration. |
| 3867 | CXXThisDecl->Destroy(getContext()); |
| 3868 | |
| 3869 | // Set the right linkage. |
| 3870 | llvm::GlobalValue::LinkageTypes ThunkLinkage; |
| 3871 | |
| 3872 | if (CGM.getFunctionLinkage(MD) == llvm::Function::InternalLinkage) |
| 3873 | ThunkLinkage = llvm::Function::InternalLinkage; |
| 3874 | else |
| 3875 | ThunkLinkage = llvm::Function::WeakAnyLinkage; |
| 3876 | |
| 3877 | Fn->setLinkage(ThunkLinkage); |
| 3878 | |
| 3879 | // Set the right visibility. |
| 3880 | CGM.setGlobalVisibility(Fn, MD); |
| 3881 | } |
| 3882 | |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 3883 | void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk) |
| 3884 | { |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 3885 | llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 3886 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Anders Carlsson | 19879c9 | 2010-03-23 17:17:29 +0000 | [diff] [blame] | 3887 | |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 3888 | // Strip off a bitcast if we got one back. |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 3889 | if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 3890 | assert(CE->getOpcode() == llvm::Instruction::BitCast); |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 3891 | Entry = CE->getOperand(0); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 3894 | // There's already a declaration with the same name, check if it has the same |
| 3895 | // type or if we need to replace it. |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 3896 | if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != |
| 3897 | CGM.getTypes().GetFunctionTypeForVtable(MD)) { |
| 3898 | llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 3899 | |
| 3900 | // If the types mismatch then we have to rewrite the definition. |
| 3901 | assert(OldThunkFn->isDeclaration() && |
| 3902 | "Shouldn't replace non-declaration"); |
| 3903 | |
| 3904 | // Remove the name from the old thunk function and get a new thunk. |
| 3905 | OldThunkFn->setName(llvm::StringRef()); |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 3906 | Entry = CGM.GetAddrOfThunk(GD, Thunk); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 3907 | |
| 3908 | // If needed, replace the old thunk with a bitcast. |
| 3909 | if (!OldThunkFn->use_empty()) { |
| 3910 | llvm::Constant *NewPtrForOldDecl = |
Anders Carlsson | 13d6898 | 2010-03-24 00:35:44 +0000 | [diff] [blame] | 3911 | llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType()); |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 3912 | OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl); |
| 3913 | } |
| 3914 | |
| 3915 | // Remove the old thunk. |
| 3916 | OldThunkFn->eraseFromParent(); |
| 3917 | } |
Anders Carlsson | 519c328 | 2010-03-24 00:39:18 +0000 | [diff] [blame] | 3918 | |
| 3919 | // Actually generate the thunk body. |
| 3920 | llvm::Function *ThunkFn = cast<llvm::Function>(Entry); |
| 3921 | CodeGenFunction(CGM).GenerateThunk(ThunkFn, GD, Thunk); |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 3922 | } |
| 3923 | |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 3924 | void CodeGenVTables::EmitThunks(GlobalDecl GD) |
| 3925 | { |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 3926 | const CXXMethodDecl *MD = |
| 3927 | cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl(); |
| 3928 | |
| 3929 | // We don't need to generate thunks for the base destructor. |
| 3930 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 3931 | return; |
| 3932 | |
| 3933 | const CXXRecordDecl *RD = MD->getParent(); |
| 3934 | |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 3935 | // Compute VTable related info for this class. |
| 3936 | ComputeVTableRelatedInformation(RD); |
| 3937 | |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 3938 | ThunksMapTy::const_iterator I = Thunks.find(MD); |
| 3939 | if (I == Thunks.end()) { |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 3940 | // We did not find a thunk for this method. |
| 3941 | return; |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 3942 | } |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 3943 | |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 3944 | const ThunkInfoVectorTy &ThunkInfoVector = I->second; |
| 3945 | for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I) |
| 3946 | EmitThunk(GD, ThunkInfoVector[I]); |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 3949 | void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD) { |
| 3950 | uint64_t *&LayoutData = VTableLayoutMap[RD]; |
| 3951 | |
| 3952 | // Check if we've computed this information before. |
| 3953 | if (LayoutData) |
| 3954 | return; |
| 3955 | |
| 3956 | VtableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD); |
| 3957 | |
| 3958 | // Add the VTable layout. |
| 3959 | uint64_t NumVTableComponents = Builder.getNumVTableComponents(); |
| 3960 | LayoutData = new uint64_t[NumVTableComponents + 1]; |
| 3961 | |
| 3962 | // Store the number of components. |
| 3963 | LayoutData[0] = NumVTableComponents; |
| 3964 | |
| 3965 | // Store the components. |
| 3966 | std::copy(Builder.vtable_components_data_begin(), |
| 3967 | Builder.vtable_components_data_end(), |
| 3968 | &LayoutData[1]); |
| 3969 | |
| 3970 | // Add the known thunks. |
| 3971 | Thunks.insert(Builder.thunks_begin(), Builder.thunks_end()); |
| 3972 | |
Anders Carlsson | 66d567d | 2010-03-25 00:51:13 +0000 | [diff] [blame] | 3973 | // Add the address points. |
| 3974 | for (VtableBuilder::AddressPointsMapTy::const_iterator I = |
| 3975 | Builder.address_points_begin(), E = Builder.address_points_end(); |
| 3976 | I != E; ++I) { |
| 3977 | |
| 3978 | uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)]; |
| 3979 | |
| 3980 | // Check if we already have the address points for this base. |
| 3981 | assert(!AddressPoint && "Address point already exists for this base!"); |
| 3982 | |
| 3983 | AddressPoint = I->second; |
| 3984 | } |
| 3985 | |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 3986 | // If we don't have the vbase information for this class, insert it. |
| 3987 | // getVirtualBaseOffsetOffset will compute it separately without computing |
| 3988 | // the rest of the vtable related information. |
| 3989 | if (!RD->getNumVBases()) |
| 3990 | return; |
| 3991 | |
| 3992 | const RecordType *VBaseRT = |
| 3993 | RD->vbases_begin()->getType()->getAs<RecordType>(); |
| 3994 | const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl()); |
| 3995 | |
| 3996 | if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase))) |
| 3997 | return; |
| 3998 | |
| 3999 | for (VtableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I = |
| 4000 | Builder.getVBaseOffsetOffsets().begin(), |
| 4001 | E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) { |
| 4002 | // Insert all types. |
| 4003 | ClassPairTy ClassPair(RD, I->first); |
| 4004 | |
| 4005 | VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second)); |
| 4006 | } |
| 4007 | } |
| 4008 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 4009 | void |
| 4010 | CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage, |
| 4011 | const CXXRecordDecl *RD) { |
Anders Carlsson | d58987c | 2009-12-07 07:59:52 +0000 | [diff] [blame] | 4012 | llvm::GlobalVariable *&Vtable = Vtables[RD]; |
| 4013 | if (Vtable) { |
| 4014 | assert(Vtable->getInitializer() && "Vtable doesn't have a definition!"); |
| 4015 | return; |
| 4016 | } |
| 4017 | |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 4018 | llvm::DenseMap<BaseSubobject, uint64_t> AddressPoints; |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 4019 | Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0, |
Anders Carlsson | 5d7af6b | 2010-02-28 00:36:23 +0000 | [diff] [blame] | 4020 | /*IsVirtual=*/false, |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 4021 | AddressPoints); |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 4022 | GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 4025 | llvm::Constant * |
| 4026 | CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD, |
| 4027 | const uint64_t *Components, |
| 4028 | unsigned NumComponents, |
| 4029 | const VTableThunksTy &VTableThunks) { |
| 4030 | llvm::SmallVector<llvm::Constant *, 64> Inits; |
| 4031 | |
| 4032 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 4033 | |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 4034 | const llvm::Type *PtrDiffTy = |
| 4035 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 4036 | |
| 4037 | QualType ClassType = CGM.getContext().getTagDeclType(RD); |
| 4038 | llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType); |
| 4039 | |
| 4040 | unsigned NextVTableThunkIndex = 0; |
| 4041 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 4042 | for (unsigned I = 0; I != NumComponents; ++I) { |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 4043 | VtableComponent Component = |
| 4044 | VtableComponent::getFromOpaqueInteger(Components[I]); |
| 4045 | |
| 4046 | llvm::Constant *Init = 0; |
| 4047 | |
| 4048 | switch (Component.getKind()) { |
| 4049 | case VtableComponent::CK_VCallOffset: |
| 4050 | Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVCallOffset()); |
| 4051 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 4052 | break; |
| 4053 | case VtableComponent::CK_VBaseOffset: |
| 4054 | Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVBaseOffset()); |
| 4055 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 4056 | break; |
| 4057 | case VtableComponent::CK_OffsetToTop: |
| 4058 | Init = llvm::ConstantInt::get(PtrDiffTy, Component.getOffsetToTop()); |
| 4059 | Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy); |
| 4060 | break; |
| 4061 | case VtableComponent::CK_RTTI: |
| 4062 | Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy); |
| 4063 | break; |
| 4064 | case VtableComponent::CK_FunctionPointer: |
| 4065 | case VtableComponent::CK_CompleteDtorPointer: |
| 4066 | case VtableComponent::CK_DeletingDtorPointer: { |
| 4067 | GlobalDecl GD; |
| 4068 | |
| 4069 | // Get the right global decl. |
| 4070 | switch (Component.getKind()) { |
| 4071 | default: |
| 4072 | llvm_unreachable("Unexpected vtable component kind"); |
| 4073 | case VtableComponent::CK_FunctionPointer: |
| 4074 | GD = Component.getFunctionDecl(); |
| 4075 | break; |
| 4076 | case VtableComponent::CK_CompleteDtorPointer: |
| 4077 | GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete); |
| 4078 | break; |
| 4079 | case VtableComponent::CK_DeletingDtorPointer: |
| 4080 | GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting); |
| 4081 | break; |
| 4082 | } |
| 4083 | |
| 4084 | // Check if we should use a thunk. |
| 4085 | if (NextVTableThunkIndex < VTableThunks.size() && |
| 4086 | VTableThunks[NextVTableThunkIndex].first == I) { |
| 4087 | const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second; |
| 4088 | |
| 4089 | Init = CGM.GetAddrOfThunk(GD, Thunk); |
| 4090 | } else { |
| 4091 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 4092 | const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD); |
| 4093 | |
| 4094 | Init = CGM.GetAddrOfFunction(GD, Ty); |
| 4095 | } |
| 4096 | |
| 4097 | Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy); |
| 4098 | break; |
| 4099 | } |
| 4100 | |
| 4101 | case VtableComponent::CK_UnusedFunctionPointer: |
| 4102 | Init = llvm::ConstantExpr::getNullValue(Int8PtrTy); |
| 4103 | break; |
| 4104 | }; |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 4105 | |
| 4106 | Inits.push_back(Init); |
| 4107 | } |
| 4108 | |
| 4109 | llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents); |
| 4110 | return llvm::ConstantArray::get(ArrayType, Inits.data(), Inits.size()); |
| 4111 | } |
| 4112 | |
| 4113 | /// GetGlobalVariable - Will return a global variable of the given type. |
| 4114 | /// If a variable with a different type already exists then a new variable |
| 4115 | /// with the right type will be created. |
| 4116 | /// FIXME: We should move this to CodeGenModule and rename it to something |
| 4117 | /// better and then use it in CGVTT and CGRTTI. |
| 4118 | static llvm::GlobalVariable * |
| 4119 | GetGlobalVariable(llvm::Module &Module, llvm::StringRef Name, |
| 4120 | const llvm::Type *Ty, |
| 4121 | llvm::GlobalValue::LinkageTypes Linkage) { |
| 4122 | |
| 4123 | llvm::GlobalVariable *GV = Module.getNamedGlobal(Name); |
| 4124 | llvm::GlobalVariable *OldGV = 0; |
| 4125 | |
| 4126 | if (GV) { |
| 4127 | // Check if the variable has the right type. |
| 4128 | if (GV->getType()->getElementType() == Ty) { |
| 4129 | // Set the correct linkage. |
| 4130 | GV->setLinkage(Linkage); |
| 4131 | return GV; |
| 4132 | } |
| 4133 | |
| 4134 | assert(GV->isDeclaration() && "Declaration has wrong type!"); |
| 4135 | |
| 4136 | OldGV = GV; |
| 4137 | } |
| 4138 | |
| 4139 | // Create a new variable. |
| 4140 | GV = new llvm::GlobalVariable(Module, Ty, /*isConstant=*/true, |
| 4141 | Linkage, 0, Name); |
| 4142 | |
| 4143 | if (OldGV) { |
| 4144 | // Replace occurrences of the old variable if needed. |
| 4145 | GV->takeName(OldGV); |
| 4146 | |
| 4147 | if (!OldGV->use_empty()) { |
| 4148 | llvm::Constant *NewPtrForOldDecl = |
| 4149 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 4150 | OldGV->replaceAllUsesWith(NewPtrForOldDecl); |
| 4151 | } |
| 4152 | |
| 4153 | OldGV->eraseFromParent(); |
| 4154 | } |
| 4155 | |
| 4156 | return GV; |
| 4157 | } |
| 4158 | |
Anders Carlsson | 5eea876 | 2010-03-24 05:32:05 +0000 | [diff] [blame] | 4159 | llvm::Constant *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) { |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 4160 | llvm::SmallString<256> OutName; |
| 4161 | CGM.getMangleContext().mangleCXXVtable(RD, OutName); |
| 4162 | llvm::StringRef Name = OutName.str(); |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 4163 | |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 4164 | ComputeVTableRelatedInformation(RD); |
| 4165 | |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 4166 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
Anders Carlsson | ccd83d7 | 2010-03-24 16:42:11 +0000 | [diff] [blame] | 4167 | llvm::ArrayType *ArrayType = |
| 4168 | llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD)); |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 4169 | |
Anders Carlsson | 5eea876 | 2010-03-24 05:32:05 +0000 | [diff] [blame] | 4170 | llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name); |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 4171 | if (GV) { |
| 4172 | if (!GV->isDeclaration() || GV->getType()->getElementType() == ArrayType) |
| 4173 | return GV; |
| 4174 | |
| 4175 | return llvm::ConstantExpr::getBitCast(GV, ArrayType->getPointerTo()); |
| 4176 | } |
| 4177 | |
| 4178 | GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType, /*isConstant=*/true, |
| 4179 | llvm::GlobalValue::ExternalLinkage, 0, Name); |
| 4180 | |
| 4181 | return GV; |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 4182 | } |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 4183 | |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 4184 | llvm::GlobalVariable * |
| 4185 | CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD, |
| 4186 | const BaseSubobject &Base, |
| 4187 | bool BaseIsVirtual, |
| 4188 | AddressPointsMapTy& AddressPoints) { |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 4189 | if (!CGM.getLangOptions().DumpVtableLayouts) { |
| 4190 | llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPoints; |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 4191 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 4192 | llvm::GlobalVariable *VTable = |
| 4193 | GenerateVtable(llvm::GlobalValue::InternalLinkage, |
| 4194 | /*GenerateDefinition=*/true, |
| 4195 | RD, Base.getBase(), Base.getBaseOffset(), |
| 4196 | BaseIsVirtual, VTableAddressPoints); |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 4197 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 4198 | // Add the address points for this base. |
| 4199 | for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I = |
| 4200 | VTableAddressPoints.begin(), E = VTableAddressPoints.end(); |
| 4201 | I != E; ++I) { |
| 4202 | |
| 4203 | uint64_t &AddressPoint = |
| 4204 | AddressPoints[std::make_pair(Base.getBase(), I->first)]; |
| 4205 | |
| 4206 | // Check if we already have the address points for this base. |
| 4207 | assert(!AddressPoint && "Address point already exists for this base!"); |
| 4208 | |
| 4209 | AddressPoint = I->second; |
| 4210 | } |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 4211 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 4212 | return VTable; |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 4215 | VtableBuilder Builder(*this, Base.getBase(), Base.getBaseOffset(), |
| 4216 | /*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD); |
| 4217 | |
Anders Carlsson | 6a5ab5d | 2010-03-25 16:49:53 +0000 | [diff] [blame] | 4218 | // Dump the vtable layout if necessary. |
| 4219 | if (CGM.getLangOptions().DumpVtableLayouts) |
| 4220 | Builder.dumpLayout(llvm::errs()); |
| 4221 | |
| 4222 | // Add the address points. |
| 4223 | for (VtableBuilder::AddressPointsMapTy::const_iterator I = |
| 4224 | Builder.address_points_begin(), E = Builder.address_points_end(); |
| 4225 | I != E; ++I) { |
| 4226 | uint64_t &AddressPoint = |
| 4227 | AddressPoints[std::make_pair(Base.getBase(), I->first)]; |
| 4228 | |
| 4229 | #if 0 |
| 4230 | // FIXME: Figure out why this assert fires. |
| 4231 | // Check if we already have the address points for this base. |
| 4232 | assert(!AddressPoint && "Address point already exists for this base!"); |
| 4233 | #endif |
| 4234 | AddressPoint = I->second; |
| 4235 | } |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 4236 | |
| 4237 | // Get the mangled construction vtable name. |
| 4238 | llvm::SmallString<256> OutName; |
| 4239 | CGM.getMangleContext().mangleCXXCtorVtable(RD, Base.getBaseOffset() / 8, |
| 4240 | Base.getBase(), OutName); |
| 4241 | llvm::StringRef Name = OutName.str(); |
| 4242 | |
| 4243 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 4244 | llvm::ArrayType *ArrayType = |
| 4245 | llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents()); |
| 4246 | |
| 4247 | // Create the variable that will hold the construction vtable. |
| 4248 | llvm::GlobalVariable *VTable = |
| 4249 | GetGlobalVariable(CGM.getModule(), Name, ArrayType, |
| 4250 | llvm::GlobalValue::InternalLinkage); |
| 4251 | |
| 4252 | // Add the thunks. |
| 4253 | VTableThunksTy VTableThunks; |
| 4254 | VTableThunks.append(Builder.vtable_thunks_begin(), |
| 4255 | Builder.vtable_thunks_end()); |
| 4256 | |
| 4257 | // Create and set the initializer. |
| 4258 | llvm::Constant *Init = |
| 4259 | CreateVTableInitializer(Base.getBase(), |
| 4260 | Builder.vtable_components_data_begin(), |
| 4261 | Builder.getNumVTableComponents(), VTableThunks); |
| 4262 | VTable->setInitializer(Init); |
| 4263 | |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 4264 | return VTable; |
| 4265 | } |
| 4266 | |
Anders Carlsson | 13189d0 | 2010-03-23 04:15:00 +0000 | [diff] [blame] | 4267 | void CodeGenVTables::EmitVTableRelatedData(GlobalDecl GD) { |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 4268 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 4269 | const CXXRecordDecl *RD = MD->getParent(); |
| 4270 | |
Anders Carlsson | 224c312 | 2009-12-05 22:42:54 +0000 | [diff] [blame] | 4271 | // If the class doesn't have a vtable we don't need to emit one. |
| 4272 | if (!RD->isDynamicClass()) |
| 4273 | return; |
| 4274 | |
Anders Carlsson | 7270ee4 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 4275 | // Check if we need to emit thunks for this function. |
| 4276 | if (MD->isVirtual()) |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 4277 | EmitThunks(GD); |
Anders Carlsson | 7270ee4 | 2010-03-23 04:31:31 +0000 | [diff] [blame] | 4278 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 4279 | // Get the key function. |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 4280 | const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD); |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 4281 | |
Anders Carlsson | 224c312 | 2009-12-05 22:42:54 +0000 | [diff] [blame] | 4282 | if (KeyFunction) { |
| 4283 | // We don't have the right key function. |
| 4284 | if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl()) |
| 4285 | return; |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 4288 | if (Vtables.count(RD)) |
| 4289 | return; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 4290 | |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 4291 | TemplateSpecializationKind kind = RD->getTemplateSpecializationKind(); |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 4292 | if (kind == TSK_ImplicitInstantiation) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 4293 | CGM.DeferredVtables.push_back(RD); |
| 4294 | else |
| 4295 | GenerateClassData(CGM.getVtableLinkage(RD), RD); |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 4296 | } |
Anders Carlsson | ee5ab9f | 2010-03-23 04:59:02 +0000 | [diff] [blame] | 4297 | |