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" |
Chandler Carruth | e087f07 | 2010-02-13 10:38:52 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Format.h" |
Zhongxing Xu | 7fe26ac | 2009-11-13 05:46:16 +0000 | [diff] [blame] | 21 | #include <cstdio> |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Anders Carlsson | 9a14060 | 2010-02-11 19:39:49 +0000 | [diff] [blame] | 26 | /// TypeConversionRequiresAdjustment - Returns whether conversion from a |
| 27 | /// derived type to a base type requires adjustment. |
| 28 | static bool |
| 29 | TypeConversionRequiresAdjustment(ASTContext &Ctx, |
| 30 | const CXXRecordDecl *DerivedDecl, |
| 31 | const CXXRecordDecl *BaseDecl) { |
| 32 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 33 | /*RecordPaths=*/true, /*DetectVirtual=*/true); |
| 34 | if (!const_cast<CXXRecordDecl *>(DerivedDecl)-> |
| 35 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) { |
| 36 | assert(false && "Class must be derived from the passed in base class!"); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | // If we found a virtual base we always want to require adjustment. |
| 41 | if (Paths.getDetectedVirtual()) |
| 42 | return true; |
| 43 | |
| 44 | const CXXBasePath &Path = Paths.front(); |
| 45 | |
| 46 | for (size_t Start = 0, End = Path.size(); Start != End; ++Start) { |
| 47 | const CXXBasePathElement &Element = Path[Start]; |
| 48 | |
| 49 | // Check the base class offset. |
| 50 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class); |
| 51 | |
| 52 | const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>(); |
| 53 | const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 54 | |
| 55 | if (Layout.getBaseClassOffset(Base) != 0) { |
| 56 | // This requires an adjustment. |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | static bool |
| 65 | TypeConversionRequiresAdjustment(ASTContext &Ctx, |
| 66 | QualType DerivedType, QualType BaseType) { |
| 67 | // Canonicalize the types. |
John McCall | 9605895 | 2010-02-12 06:15:07 +0000 | [diff] [blame] | 68 | CanQualType CanDerivedType = Ctx.getCanonicalType(DerivedType); |
| 69 | CanQualType CanBaseType = Ctx.getCanonicalType(BaseType); |
Anders Carlsson | 9a14060 | 2010-02-11 19:39:49 +0000 | [diff] [blame] | 70 | |
| 71 | assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() && |
| 72 | "Types must have same type class!"); |
| 73 | |
| 74 | if (CanDerivedType == CanBaseType) { |
| 75 | // No adjustment needed. |
| 76 | return false; |
| 77 | } |
| 78 | |
John McCall | 9605895 | 2010-02-12 06:15:07 +0000 | [diff] [blame] | 79 | if (isa<ReferenceType>(CanDerivedType)) { |
| 80 | CanDerivedType = CanDerivedType->getAs<ReferenceType>()->getPointeeType(); |
Anders Carlsson | 9abb273 | 2010-02-11 19:45:15 +0000 | [diff] [blame] | 81 | CanBaseType = CanBaseType->getAs<ReferenceType>()->getPointeeType(); |
John McCall | 9605895 | 2010-02-12 06:15:07 +0000 | [diff] [blame] | 82 | } else if (isa<PointerType>(CanDerivedType)) { |
| 83 | CanDerivedType = CanDerivedType->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9abb273 | 2010-02-11 19:45:15 +0000 | [diff] [blame] | 84 | CanBaseType = CanBaseType->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9a14060 | 2010-02-11 19:39:49 +0000 | [diff] [blame] | 85 | } else { |
| 86 | assert(false && "Unexpected return type!"); |
| 87 | } |
| 88 | |
John McCall | 9605895 | 2010-02-12 06:15:07 +0000 | [diff] [blame] | 89 | // We need to compare unqualified types here; consider |
| 90 | // const T *Base::foo(); |
| 91 | // T *Derived::foo(); |
| 92 | if (CanDerivedType.getUnqualifiedType() == CanBaseType.getUnqualifiedType()) { |
Anders Carlsson | 9a14060 | 2010-02-11 19:39:49 +0000 | [diff] [blame] | 93 | // No adjustment needed. |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | const CXXRecordDecl *DerivedDecl = |
John McCall | 9605895 | 2010-02-12 06:15:07 +0000 | [diff] [blame] | 98 | cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl()); |
Anders Carlsson | 9a14060 | 2010-02-11 19:39:49 +0000 | [diff] [blame] | 99 | |
| 100 | const CXXRecordDecl *BaseDecl = |
John McCall | 9605895 | 2010-02-12 06:15:07 +0000 | [diff] [blame] | 101 | cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl()); |
Anders Carlsson | 9a14060 | 2010-02-11 19:39:49 +0000 | [diff] [blame] | 102 | |
| 103 | return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl); |
| 104 | } |
| 105 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 106 | static bool |
Anders Carlsson | 104f45c | 2010-02-12 17:37:14 +0000 | [diff] [blame] | 107 | ReturnTypeConversionRequiresAdjustment(const CXXMethodDecl *DerivedMD, |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 108 | const CXXMethodDecl *BaseMD) { |
Anders Carlsson | 104f45c | 2010-02-12 17:37:14 +0000 | [diff] [blame] | 109 | ASTContext &Context = DerivedMD->getASTContext(); |
| 110 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 111 | const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>(); |
| 112 | const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>(); |
| 113 | |
| 114 | return TypeConversionRequiresAdjustment(Context, DerivedFT->getResultType(), |
| 115 | BaseFT->getResultType()); |
| 116 | } |
| 117 | |
Anders Carlsson | 27f69d0 | 2009-11-27 22:21:51 +0000 | [diff] [blame] | 118 | namespace { |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 119 | |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 120 | /// FinalOverriders - Contains the final overrider member functions for all |
| 121 | /// member functions in the base subobjects of a class. |
| 122 | class FinalOverriders { |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 123 | public: |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 124 | /// BaseOffset - Represents an offset from a derived class to a direct or |
| 125 | /// indirect base class. |
| 126 | struct BaseOffset { |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 127 | /// DerivedClass - The derived class. |
| 128 | const CXXRecordDecl *DerivedClass; |
| 129 | |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 130 | /// VirtualBase - If the path from the derived class to the base class |
| 131 | /// involves a virtual base class, this holds its declaration. |
| 132 | const CXXRecordDecl *VirtualBase; |
| 133 | |
| 134 | /// NonVirtualOffset - The offset from the derived class to the base class. |
| 135 | /// Or the offset from the virtual base class to the base class, if the path |
| 136 | /// from the derived class to the base class involves a virtual base class. |
| 137 | uint64_t NonVirtualOffset; |
| 138 | |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 139 | BaseOffset() : DerivedClass(0), VirtualBase(0), NonVirtualOffset(0) { } |
| 140 | BaseOffset(const CXXRecordDecl *DerivedClass, |
| 141 | const CXXRecordDecl *VirtualBase, uint64_t NonVirtualOffset) |
| 142 | : DerivedClass(DerivedClass), VirtualBase(VirtualBase), |
| 143 | NonVirtualOffset(NonVirtualOffset) { } |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 144 | |
Benjamin Kramer | 03d15f0 | 2010-02-13 09:15:07 +0000 | [diff] [blame] | 145 | bool isEmpty() const { return !NonVirtualOffset && !VirtualBase; } |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 148 | /// OverriderInfo - Information about a final overrider. |
| 149 | struct OverriderInfo { |
| 150 | /// Method - The method decl of the overrider. |
| 151 | const CXXMethodDecl *Method; |
| 152 | |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 153 | OverriderInfo() : Method(0) { } |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | private: |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 157 | /// MostDerivedClass - The most derived class for which the final overriders |
| 158 | /// are stored. |
| 159 | const CXXRecordDecl *MostDerivedClass; |
| 160 | |
| 161 | ASTContext &Context; |
| 162 | |
| 163 | /// MostDerivedClassLayout - the AST record layout of the most derived class. |
| 164 | const ASTRecordLayout &MostDerivedClassLayout; |
| 165 | |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 166 | /// BaseSubobjectMethodPairTy - Uniquely identifies a member function |
| 167 | /// in a base subobject. |
| 168 | typedef std::pair<BaseSubobject, const CXXMethodDecl *> |
| 169 | BaseSubobjectMethodPairTy; |
| 170 | |
| 171 | typedef llvm::DenseMap<BaseSubobjectMethodPairTy, |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 172 | OverriderInfo> OverridersMapTy; |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 173 | |
| 174 | /// OverridersMap - The final overriders for all virtual member functions of |
| 175 | /// all the base subobjects of the most derived class. |
| 176 | OverridersMapTy OverridersMap; |
| 177 | |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 178 | typedef llvm::DenseMap<BaseSubobjectMethodPairTy, BaseOffset> |
| 179 | AdjustmentOffsetsMapTy; |
| 180 | |
| 181 | /// ReturnAdjustments - Holds return adjustments for all the overriders that |
| 182 | /// need to perform return value adjustments. |
| 183 | AdjustmentOffsetsMapTy ReturnAdjustments; |
| 184 | |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 185 | typedef llvm::SmallVector<uint64_t, 1> OffsetVectorTy; |
| 186 | |
| 187 | /// SubobjectOffsetsMapTy - This map is used for keeping track of all the |
| 188 | /// base subobject offsets that a single class declaration might refer to. |
| 189 | /// |
| 190 | /// For example, in: |
| 191 | /// |
| 192 | /// struct A { virtual void f(); }; |
| 193 | /// struct B1 : A { }; |
| 194 | /// struct B2 : A { }; |
| 195 | /// struct C : B1, B2 { virtual void f(); }; |
| 196 | /// |
| 197 | /// when we determine that C::f() overrides A::f(), we need to update the |
| 198 | /// overriders map for both A-in-B1 and A-in-B2 and the subobject offsets map |
| 199 | /// will have the subobject offsets for both A copies. |
| 200 | typedef llvm::DenseMap<const CXXRecordDecl *, OffsetVectorTy> |
| 201 | SubobjectOffsetsMapTy; |
| 202 | |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 203 | /// ComputeFinalOverriders - Compute the final overriders for a given base |
| 204 | /// subobject (and all its direct and indirect bases). |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 205 | void ComputeFinalOverriders(BaseSubobject Base, |
| 206 | SubobjectOffsetsMapTy &Offsets); |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 207 | |
| 208 | /// AddOverriders - Add the final overriders for this base subobject to the |
| 209 | /// map of final overriders. |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 210 | void AddOverriders(BaseSubobject Base, SubobjectOffsetsMapTy &Offsets); |
| 211 | |
| 212 | /// PropagateOverrider - Propagate the NewMD overrider to all the functions |
| 213 | /// that OldMD overrides. For example, if we have: |
| 214 | /// |
| 215 | /// struct A { virtual void f(); }; |
| 216 | /// struct B : A { virtual void f(); }; |
| 217 | /// struct C : B { virtual void f(); }; |
| 218 | /// |
| 219 | /// and we want to override B::f with C::f, we also need to override A::f with |
| 220 | /// C::f. |
| 221 | void PropagateOverrider(const CXXMethodDecl *OldMD, |
| 222 | const CXXMethodDecl *NewMD, |
| 223 | SubobjectOffsetsMapTy &Offsets); |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 224 | |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 225 | static void MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets, |
| 226 | SubobjectOffsetsMapTy &Offsets); |
| 227 | |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 228 | public: |
| 229 | explicit FinalOverriders(const CXXRecordDecl *MostDerivedClass); |
| 230 | |
| 231 | /// getOverrider - Get the final overrider for the given method declaration in |
| 232 | /// the given base subobject. |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 233 | OverriderInfo getOverrider(BaseSubobject Base, |
| 234 | const CXXMethodDecl *MD) const { |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 235 | assert(OverridersMap.count(std::make_pair(Base, MD)) && |
| 236 | "Did not find overrider!"); |
| 237 | |
| 238 | return OverridersMap.lookup(std::make_pair(Base, MD)); |
| 239 | } |
| 240 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 241 | BaseOffset getReturnAdjustmentOffset(BaseSubobject Base, |
| 242 | const CXXMethodDecl *MD) const { |
| 243 | return ReturnAdjustments.lookup(std::make_pair(Base, MD)); |
| 244 | } |
| 245 | |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 246 | /// dump - dump the final overriders. |
| 247 | void dump() const { |
| 248 | dump(llvm::errs(), BaseSubobject(MostDerivedClass, 0)); |
| 249 | } |
| 250 | |
| 251 | /// dump - dump the final overriders for a base subobject, and all its direct |
| 252 | /// and indirect base subobjects. |
| 253 | void dump(llvm::raw_ostream &Out, BaseSubobject Base) const; |
| 254 | }; |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 255 | |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 256 | FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass) |
| 257 | : MostDerivedClass(MostDerivedClass), |
| 258 | Context(MostDerivedClass->getASTContext()), |
| 259 | MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) { |
| 260 | |
| 261 | // Compute the final overriders. |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 262 | SubobjectOffsetsMapTy Offsets; |
| 263 | ComputeFinalOverriders(BaseSubobject(MostDerivedClass, 0), Offsets); |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 264 | |
| 265 | // And dump them (for now). |
| 266 | dump(); |
| 267 | } |
| 268 | |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 269 | void FinalOverriders::AddOverriders(BaseSubobject Base, |
| 270 | SubobjectOffsetsMapTy &Offsets) { |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 271 | const CXXRecordDecl *RD = Base.getBase(); |
| 272 | |
| 273 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 274 | E = RD->method_end(); I != E; ++I) { |
| 275 | const CXXMethodDecl *MD = *I; |
| 276 | |
| 277 | if (!MD->isVirtual()) |
| 278 | continue; |
| 279 | |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 280 | // First, propagate the overrider. |
| 281 | PropagateOverrider(MD, MD, Offsets); |
| 282 | |
| 283 | // Add the overrider as the final overrider of itself. |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 284 | OverriderInfo& Overrider = OverridersMap[std::make_pair(Base, MD)]; |
| 285 | assert(!Overrider.Method && "Overrider should not exist yet!"); |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 286 | |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 287 | Overrider.Method = MD; |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 291 | static FinalOverriders::BaseOffset |
| 292 | ComputeBaseOffset(ASTContext &Context, |
| 293 | const CXXRecordDecl *DerivedRD, |
| 294 | const CXXRecordDecl *BaseRD) { |
| 295 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
Anders Carlsson | a4a5417 | 2010-02-13 20:41:15 +0000 | [diff] [blame] | 296 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 297 | |
| 298 | if (!const_cast<CXXRecordDecl *>(DerivedRD)-> |
| 299 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) { |
| 300 | assert(false && "Class must be derived from the passed in base class!"); |
| 301 | return FinalOverriders::BaseOffset(); |
| 302 | } |
| 303 | |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 304 | uint64_t NonVirtualOffset = 0; |
| 305 | |
| 306 | const CXXBasePath &Path = Paths.front(); |
| 307 | |
Anders Carlsson | a4a5417 | 2010-02-13 20:41:15 +0000 | [diff] [blame] | 308 | unsigned NonVirtualStart = 0; |
| 309 | const CXXRecordDecl *VirtualBase = 0; |
| 310 | |
| 311 | // First, look for the virtual base class. |
| 312 | for (unsigned I = 0, E = Path.size(); I != E; ++I) { |
| 313 | const CXXBasePathElement &Element = Path[I]; |
| 314 | |
| 315 | if (Element.Base->isVirtual()) { |
| 316 | // FIXME: Can we break when we find the first virtual base? |
| 317 | // (If we can't, can't we just iterate over the path in reverse order?) |
| 318 | NonVirtualStart = I + 1; |
| 319 | QualType VBaseType = Element.Base->getType(); |
| 320 | VirtualBase = |
| 321 | cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // Now compute the non-virtual offset. |
| 326 | for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) { |
| 327 | const CXXBasePathElement &Element = Path[I]; |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 328 | |
| 329 | // Check the base class offset. |
| 330 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); |
| 331 | |
| 332 | const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>(); |
| 333 | const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 334 | |
| 335 | NonVirtualOffset += Layout.getBaseClassOffset(Base); |
| 336 | } |
| 337 | |
| 338 | // FIXME: This should probably use CharUnits or something. Maybe we should |
| 339 | // even change the base offsets in ASTRecordLayout to be specified in |
| 340 | // CharUnits. |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 341 | return FinalOverriders::BaseOffset(DerivedRD, VirtualBase, |
| 342 | NonVirtualOffset / 8); |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static FinalOverriders::BaseOffset |
| 346 | ComputeReturnTypeBaseOffset(ASTContext &Context, |
| 347 | const CXXMethodDecl *DerivedMD, |
| 348 | const CXXMethodDecl *BaseMD) { |
| 349 | const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>(); |
| 350 | const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>(); |
| 351 | |
| 352 | // Canonicalize the return types. |
| 353 | CanQualType CanDerivedReturnType = |
| 354 | Context.getCanonicalType(DerivedFT->getResultType()); |
| 355 | CanQualType CanBaseReturnType = |
| 356 | Context.getCanonicalType(BaseFT->getResultType()); |
| 357 | |
| 358 | assert(CanDerivedReturnType->getTypeClass() == |
| 359 | CanBaseReturnType->getTypeClass() && |
| 360 | "Types must have same type class!"); |
| 361 | |
| 362 | if (CanDerivedReturnType == CanBaseReturnType) { |
| 363 | // No adjustment needed. |
| 364 | return FinalOverriders::BaseOffset(); |
| 365 | } |
| 366 | |
| 367 | if (isa<ReferenceType>(CanDerivedReturnType)) { |
| 368 | CanDerivedReturnType = |
| 369 | CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType(); |
| 370 | CanBaseReturnType = |
| 371 | CanBaseReturnType->getAs<ReferenceType>()->getPointeeType(); |
| 372 | } else if (isa<PointerType>(CanDerivedReturnType)) { |
| 373 | CanDerivedReturnType = |
| 374 | CanDerivedReturnType->getAs<PointerType>()->getPointeeType(); |
| 375 | CanBaseReturnType = |
| 376 | CanBaseReturnType->getAs<PointerType>()->getPointeeType(); |
| 377 | } else { |
| 378 | assert(false && "Unexpected return type!"); |
| 379 | } |
| 380 | |
| 381 | // We need to compare unqualified types here; consider |
| 382 | // const T *Base::foo(); |
| 383 | // T *Derived::foo(); |
| 384 | if (CanDerivedReturnType.getUnqualifiedType() == |
| 385 | CanBaseReturnType.getUnqualifiedType()) { |
| 386 | // No adjustment needed. |
| 387 | return FinalOverriders::BaseOffset(); |
| 388 | } |
| 389 | |
| 390 | const CXXRecordDecl *DerivedRD = |
| 391 | cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl()); |
| 392 | |
| 393 | const CXXRecordDecl *BaseRD = |
| 394 | cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl()); |
| 395 | |
| 396 | return ComputeBaseOffset(Context, DerivedRD, BaseRD); |
| 397 | } |
| 398 | |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 399 | void FinalOverriders::PropagateOverrider(const CXXMethodDecl *OldMD, |
| 400 | const CXXMethodDecl *NewMD, |
| 401 | SubobjectOffsetsMapTy &Offsets) { |
| 402 | for (CXXMethodDecl::method_iterator I = OldMD->begin_overridden_methods(), |
| 403 | E = OldMD->end_overridden_methods(); I != E; ++I) { |
| 404 | const CXXMethodDecl *OverriddenMD = *I; |
| 405 | const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent(); |
| 406 | |
| 407 | // We want to override OverriddenMD in all subobjects, for example: |
| 408 | // |
| 409 | /// struct A { virtual void f(); }; |
| 410 | /// struct B1 : A { }; |
| 411 | /// struct B2 : A { }; |
| 412 | /// struct C : B1, B2 { virtual void f(); }; |
| 413 | /// |
| 414 | /// When overriding A::f with C::f we need to do so in both A subobjects. |
| 415 | const OffsetVectorTy &OffsetVector = Offsets[OverriddenRD]; |
| 416 | |
| 417 | // Go through all the subobjects. |
| 418 | for (unsigned I = 0, E = OffsetVector.size(); I != E; ++I) { |
| 419 | uint64_t Offset = OffsetVector[I]; |
| 420 | |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 421 | BaseSubobjectMethodPairTy SubobjectAndMethod = |
| 422 | std::make_pair(BaseSubobject(OverriddenRD, Offset), OverriddenMD); |
| 423 | |
| 424 | OverriderInfo &Overrider = OverridersMap[SubobjectAndMethod]; |
| 425 | |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 426 | assert(Overrider.Method && "Did not find existing overrider!"); |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 427 | |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 428 | // Get the return adjustment base offset. |
Anders Carlsson | 1d05be5 | 2010-02-13 21:16:54 +0000 | [diff] [blame^] | 429 | // (We don't want to do this for pure virtual member functions). |
| 430 | if (!NewMD->isPure()) { |
| 431 | BaseOffset ReturnBaseOffset = |
| 432 | ComputeReturnTypeBaseOffset(Context, NewMD, OverriddenMD); |
| 433 | if (!ReturnBaseOffset.isEmpty()) { |
| 434 | // Store the return adjustment base offset. |
| 435 | ReturnAdjustments[SubobjectAndMethod] = ReturnBaseOffset; |
| 436 | } |
Anders Carlsson | 16b7312 | 2010-02-12 17:13:23 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 439 | // Set the new overrider. |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 440 | Overrider.Method = NewMD; |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 441 | |
| 442 | // And propagate it further. |
| 443 | PropagateOverrider(OverriddenMD, NewMD, Offsets); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | FinalOverriders::MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets, |
| 450 | SubobjectOffsetsMapTy &Offsets) { |
| 451 | // Iterate over the new offsets. |
| 452 | for (SubobjectOffsetsMapTy::const_iterator I = NewOffsets.begin(), |
| 453 | E = NewOffsets.end(); I != E; ++I) { |
| 454 | const CXXRecordDecl *NewRD = I->first; |
| 455 | const OffsetVectorTy& NewOffsetsVector = I->second; |
| 456 | |
| 457 | OffsetVectorTy &OffsetsVector = Offsets[NewRD]; |
| 458 | if (OffsetsVector.empty()) { |
| 459 | // There were no previous offsets in this vector, just insert all entries |
| 460 | // from the new offsets vector. |
| 461 | OffsetsVector.append(NewOffsetsVector.begin(), NewOffsetsVector.end()); |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | assert(false && "FIXME: Handle merging the subobject offsets!"); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | void FinalOverriders::ComputeFinalOverriders(BaseSubobject Base, |
| 470 | SubobjectOffsetsMapTy &Offsets) { |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 471 | const CXXRecordDecl *RD = Base.getBase(); |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 472 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 473 | |
Anders Carlsson | b8358d8 | 2010-02-12 01:40:03 +0000 | [diff] [blame] | 474 | SubobjectOffsetsMapTy NewOffsets; |
| 475 | |
| 476 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 477 | E = RD->bases_end(); I != E; ++I) { |
| 478 | const CXXRecordDecl *BaseDecl = |
| 479 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 480 | |
| 481 | assert(!I->isVirtual() && "FIXME: Handle virtual bases!"); |
| 482 | |
| 483 | uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl) + |
| 484 | Base.getBaseOffset(); |
| 485 | |
| 486 | // Compute the final overriders for this base. |
| 487 | ComputeFinalOverriders(BaseSubobject(BaseDecl, BaseOffset), NewOffsets); |
| 488 | } |
| 489 | |
| 490 | /// Now add the overriders for this particular subobject. |
| 491 | AddOverriders(Base, NewOffsets); |
| 492 | |
| 493 | // And merge the newly discovered subobject offsets. |
| 494 | MergeSubobjectOffsets(NewOffsets, Offsets); |
| 495 | |
| 496 | /// Finally, add the offset for our own subobject. |
| 497 | Offsets[RD].push_back(Base.getBaseOffset()); |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void FinalOverriders::dump(llvm::raw_ostream &Out, BaseSubobject Base) const { |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 501 | const CXXRecordDecl *RD = Base.getBase(); |
| 502 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 503 | |
| 504 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 505 | E = RD->bases_end(); I != E; ++I) { |
| 506 | assert(!I->isVirtual() && "FIXME: Handle virtual bases!"); |
| 507 | |
| 508 | const CXXRecordDecl *BaseDecl = |
| 509 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 510 | |
| 511 | uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl) + |
| 512 | Base.getBaseOffset(); |
| 513 | |
| 514 | dump(Out, BaseSubobject(BaseDecl, BaseOffset)); |
| 515 | } |
| 516 | |
| 517 | Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", "; |
| 518 | Out << Base.getBaseOffset() << ")\n"; |
| 519 | |
| 520 | // Now dump the overriders for this base subobject. |
| 521 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 522 | E = RD->method_end(); I != E; ++I) { |
| 523 | const CXXMethodDecl *MD = *I; |
| 524 | |
| 525 | if (!MD->isVirtual()) |
| 526 | continue; |
| 527 | |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 528 | OverriderInfo Overrider = getOverrider(Base, MD); |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 529 | |
| 530 | Out << " " << MD->getQualifiedNameAsString() << " - "; |
Anders Carlsson | 16b7312 | 2010-02-12 17:13:23 +0000 | [diff] [blame] | 531 | Out << Overrider.Method->getQualifiedNameAsString(); |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 532 | |
Benjamin Kramer | e3d0b1c | 2010-02-13 09:11:28 +0000 | [diff] [blame] | 533 | AdjustmentOffsetsMapTy::const_iterator AI = |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 534 | ReturnAdjustments.find(std::make_pair(Base, MD)); |
Benjamin Kramer | e3d0b1c | 2010-02-13 09:11:28 +0000 | [diff] [blame] | 535 | if (AI != ReturnAdjustments.end()) { |
| 536 | const BaseOffset &Offset = AI->second; |
Anders Carlsson | a4a5417 | 2010-02-13 20:41:15 +0000 | [diff] [blame] | 537 | |
| 538 | Out << " [ret-adj: "; |
| 539 | if (Offset.VirtualBase) |
| 540 | Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, "; |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 541 | |
Anders Carlsson | a4a5417 | 2010-02-13 20:41:15 +0000 | [diff] [blame] | 542 | Out << Offset.NonVirtualOffset << " nv]"; |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 543 | } |
Anders Carlsson | 16b7312 | 2010-02-12 17:13:23 +0000 | [diff] [blame] | 544 | Out << "\n"; |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 548 | /// VtableComponent - Represents a single component in a vtable. |
| 549 | class VtableComponent { |
| 550 | public: |
| 551 | enum Kind { |
| 552 | CK_VCallOffset, |
| 553 | CK_VBaseOffset, |
| 554 | CK_OffsetToTop, |
| 555 | CK_RTTI, |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 556 | CK_FunctionPointer, |
| 557 | |
| 558 | /// CK_CompleteDtorPointer - A pointer to the complete destructor. |
| 559 | CK_CompleteDtorPointer, |
| 560 | |
| 561 | /// CK_DeletingDtorPointer - A pointer to the deleting destructor. |
| 562 | CK_DeletingDtorPointer |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 563 | }; |
| 564 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 565 | static VtableComponent MakeOffsetToTop(int64_t Offset) { |
| 566 | return VtableComponent(CK_OffsetToTop, Offset); |
| 567 | } |
| 568 | |
| 569 | static VtableComponent MakeRTTI(const CXXRecordDecl *RD) { |
| 570 | return VtableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD)); |
| 571 | } |
| 572 | |
| 573 | static VtableComponent MakeFunction(const CXXMethodDecl *MD) { |
| 574 | assert(!isa<CXXDestructorDecl>(MD) && |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 575 | "Don't use MakeFunction with destructors!"); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 576 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 577 | return VtableComponent(CK_FunctionPointer, |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 578 | reinterpret_cast<uintptr_t>(MD)); |
| 579 | } |
| 580 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 581 | static VtableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) { |
| 582 | return VtableComponent(CK_CompleteDtorPointer, |
| 583 | reinterpret_cast<uintptr_t>(DD)); |
| 584 | } |
| 585 | |
| 586 | static VtableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) { |
| 587 | return VtableComponent(CK_DeletingDtorPointer, |
| 588 | reinterpret_cast<uintptr_t>(DD)); |
| 589 | } |
| 590 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 591 | /// getKind - Get the kind of this vtable component. |
| 592 | Kind getKind() const { |
| 593 | return (Kind)(Value & 0x7); |
| 594 | } |
| 595 | |
| 596 | int64_t getOffsetToTop() const { |
| 597 | assert(getKind() == CK_OffsetToTop && "Invalid component kind!"); |
| 598 | |
| 599 | return getOffset(); |
| 600 | } |
| 601 | |
| 602 | const CXXRecordDecl *getRTTIDecl() const { |
| 603 | assert(getKind() == CK_RTTI && "Invalid component kind!"); |
| 604 | |
| 605 | return reinterpret_cast<CXXRecordDecl *>(getPointer()); |
| 606 | } |
| 607 | |
| 608 | const CXXMethodDecl *getFunctionDecl() const { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 609 | assert(getKind() == CK_FunctionPointer); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 610 | |
| 611 | return reinterpret_cast<CXXMethodDecl *>(getPointer()); |
| 612 | } |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 613 | |
| 614 | const CXXDestructorDecl *getDestructorDecl() const { |
| 615 | assert((getKind() == CK_CompleteDtorPointer || |
| 616 | getKind() == CK_DeletingDtorPointer) && "Invalid component kind!"); |
| 617 | |
| 618 | return reinterpret_cast<CXXDestructorDecl *>(getPointer()); |
| 619 | } |
| 620 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 621 | private: |
| 622 | VtableComponent(Kind ComponentKind, int64_t Offset) { |
| 623 | assert((ComponentKind == CK_VCallOffset || |
| 624 | ComponentKind == CK_VBaseOffset || |
| 625 | ComponentKind == CK_OffsetToTop) && "Invalid component kind!"); |
| 626 | assert(Offset <= ((1LL << 56) - 1) && "Offset is too big!"); |
| 627 | |
| 628 | Value = ((Offset << 3) | ComponentKind); |
| 629 | } |
| 630 | |
| 631 | VtableComponent(Kind ComponentKind, uintptr_t Ptr) { |
| 632 | assert((ComponentKind == CK_RTTI || |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 633 | ComponentKind == CK_FunctionPointer || |
| 634 | ComponentKind == CK_CompleteDtorPointer || |
| 635 | ComponentKind == CK_DeletingDtorPointer) && |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 636 | "Invalid component kind!"); |
| 637 | |
| 638 | assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!"); |
| 639 | |
| 640 | Value = Ptr | ComponentKind; |
| 641 | } |
| 642 | |
| 643 | int64_t getOffset() const { |
| 644 | assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset || |
| 645 | getKind() == CK_OffsetToTop) && "Invalid component kind!"); |
| 646 | |
| 647 | return Value >> 3; |
| 648 | } |
| 649 | |
| 650 | uintptr_t getPointer() const { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 651 | assert((getKind() == CK_RTTI || |
| 652 | getKind() == CK_FunctionPointer || |
| 653 | getKind() == CK_CompleteDtorPointer || |
| 654 | getKind() == CK_DeletingDtorPointer) && |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 655 | "Invalid component kind!"); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 656 | |
| 657 | return static_cast<uintptr_t>(Value & ~7ULL); |
| 658 | } |
| 659 | |
| 660 | /// The kind is stored in the lower 3 bits of the value. For offsets, we |
| 661 | /// make use of the facts that classes can't be larger than 2^55 bytes, |
| 662 | /// so we store the offset in the lower part of the 61 bytes that remain. |
| 663 | /// (The reason that we're not simply using a PointerIntPair here is that we |
| 664 | /// need the offsets to be 64-bit, even when on a 32-bit machine). |
| 665 | int64_t Value; |
| 666 | }; |
| 667 | |
| 668 | /// VtableBuilder - Class for building vtable layout information. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 669 | class VtableBuilder { |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 670 | public: |
| 671 | /// PrimaryBasesSetTy - A set of direct and indirect primary bases. |
| 672 | typedef llvm::SmallPtrSet<const CXXRecordDecl *, 8> PrimaryBasesSetTy; |
| 673 | |
| 674 | private: |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 675 | /// VtableInfo - Global vtable information. |
| 676 | CGVtableInfo &VtableInfo; |
| 677 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 678 | /// MostDerivedClass - The most derived class for which we're building this |
| 679 | /// vtable. |
| 680 | const CXXRecordDecl *MostDerivedClass; |
| 681 | |
| 682 | /// Context - The ASTContext which we will use for layout information. |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 683 | ASTContext &Context; |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 684 | |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 685 | /// FinalOverriders - The final overriders of the most derived class. |
| 686 | FinalOverriders Overriders; |
| 687 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 688 | /// Components - The components of the vtable being built. |
| 689 | llvm::SmallVector<VtableComponent, 64> Components; |
| 690 | |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 691 | /// AddressPoints - Address points for the vtable being built. |
| 692 | CGVtableInfo::AddressPointsMapTy AddressPoints; |
| 693 | |
Anders Carlsson | 104f45c | 2010-02-12 17:37:14 +0000 | [diff] [blame] | 694 | /// ReturnAdjustment - A return adjustment thunk. |
| 695 | struct ReturnAdjustment { |
| 696 | /// NonVirtual - The non-virtual adjustment from the derived object to its |
| 697 | /// nearest virtual base. |
| 698 | int64_t NonVirtual; |
| 699 | |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 700 | /// VBaseOffsetOffset - The offset, in bytes, relative to the address point |
| 701 | /// of the virtual base class offset. |
| 702 | int64_t VBaseOffsetOffset; |
Anders Carlsson | 104f45c | 2010-02-12 17:37:14 +0000 | [diff] [blame] | 703 | |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 704 | ReturnAdjustment() : NonVirtual(0), VBaseOffsetOffset(0) { } |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 705 | |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 706 | bool isEmpty() const { return !NonVirtual && !VBaseOffsetOffset; } |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 707 | }; |
| 708 | |
| 709 | /// ReturnAdjustments - The return adjustments needed in this vtable. |
| 710 | llvm::SmallVector<std::pair<uint64_t, ReturnAdjustment>, 16> |
| 711 | ReturnAdjustments; |
| 712 | |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 713 | /// ComputeReturnAdjustment - Compute the return adjustment given a return |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 714 | /// adjustment base offset. |
| 715 | ReturnAdjustment ComputeReturnAdjustment(FinalOverriders::BaseOffset Offset); |
| 716 | |
| 717 | /// AddMethod - Add a single virtual member function to the vtable |
| 718 | /// components vector. |
| 719 | void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment); |
| 720 | |
| 721 | /// AddMethods - Add the methods of this base subobject and all its |
| 722 | /// primary bases to the vtable components vector. |
| 723 | void AddMethods(BaseSubobject Base, PrimaryBasesSetTy &PrimaryBases); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 724 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 725 | /// layoutSimpleVtable - A test function that will layout very simple vtables |
| 726 | /// without any bases. Just used for testing for now. |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 727 | void layoutSimpleVtable(BaseSubobject Base); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 728 | |
| 729 | public: |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 730 | VtableBuilder(CGVtableInfo &VtableInfo, const CXXRecordDecl *MostDerivedClass) |
| 731 | : VtableInfo(VtableInfo), MostDerivedClass(MostDerivedClass), |
Anders Carlsson | bdf73d8 | 2010-02-11 21:24:32 +0000 | [diff] [blame] | 732 | Context(MostDerivedClass->getASTContext()), Overriders(MostDerivedClass) { |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 733 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 734 | layoutSimpleVtable(BaseSubobject(MostDerivedClass, 0)); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /// dumpLayout - Dump the vtable layout. |
| 738 | void dumpLayout(llvm::raw_ostream&); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 739 | }; |
| 740 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 741 | /// OverridesMethodInPrimaryBase - Checks whether whether this virtual member |
| 742 | /// function overrides a member function in a direct or indirect primary base. |
| 743 | /// Returns the overridden member function, or null if none was found. |
| 744 | static const CXXMethodDecl * |
| 745 | OverridesMethodInPrimaryBase(const CXXMethodDecl *MD, |
| 746 | VtableBuilder::PrimaryBasesSetTy &PrimaryBases) { |
| 747 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 748 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 749 | const CXXMethodDecl *OverriddenMD = *I; |
| 750 | const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent(); |
| 751 | assert(OverriddenMD->isCanonicalDecl() && |
| 752 | "Should have the canonical decl of the overridden RD!"); |
| 753 | |
| 754 | if (PrimaryBases.count(OverriddenRD)) |
| 755 | return OverriddenMD; |
| 756 | } |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 761 | VtableBuilder::ReturnAdjustment |
| 762 | VtableBuilder::ComputeReturnAdjustment(FinalOverriders::BaseOffset Offset) { |
| 763 | ReturnAdjustment Adjustment; |
| 764 | |
| 765 | if (!Offset.isEmpty()) { |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 766 | if (Offset.VirtualBase) { |
| 767 | // Get the virtual base offset offset. |
| 768 | Adjustment.VBaseOffsetOffset = |
| 769 | VtableInfo.getVirtualBaseOffsetIndex(Offset.DerivedClass, |
| 770 | Offset.VirtualBase); |
| 771 | // FIXME: Once the assert in getVirtualBaseOffsetIndex is back again, |
| 772 | // we can get rid of this assert. |
| 773 | assert(Adjustment.VBaseOffsetOffset != 0 && |
| 774 | "Invalid base offset offset!"); |
| 775 | } |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 776 | |
| 777 | Adjustment.NonVirtual = Offset.NonVirtualOffset; |
| 778 | } |
| 779 | |
| 780 | return Adjustment; |
| 781 | } |
| 782 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 783 | void |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 784 | VtableBuilder::AddMethod(const CXXMethodDecl *MD, |
| 785 | ReturnAdjustment ReturnAdjustment) { |
| 786 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 787 | assert(ReturnAdjustment.isEmpty() && |
| 788 | "Destructor can't have return adjustment!"); |
| 789 | // Add both the complete destructor and the deleting destructor. |
| 790 | Components.push_back(VtableComponent::MakeCompleteDtor(DD)); |
| 791 | Components.push_back(VtableComponent::MakeDeletingDtor(DD)); |
| 792 | } else { |
| 793 | // Add the return adjustment if necessary. |
| 794 | if (!ReturnAdjustment.isEmpty()) |
| 795 | ReturnAdjustments.push_back(std::make_pair(Components.size(), |
| 796 | ReturnAdjustment)); |
| 797 | |
| 798 | // Add the function. |
| 799 | Components.push_back(VtableComponent::MakeFunction(MD)); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | void |
| 804 | VtableBuilder::AddMethods(BaseSubobject Base, PrimaryBasesSetTy &PrimaryBases) { |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 805 | const CXXRecordDecl *RD = Base.getBase(); |
| 806 | |
| 807 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 808 | |
| 809 | if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { |
| 810 | if (Layout.getPrimaryBaseWasVirtual()) |
| 811 | assert(false && "FIXME: Handle vbases here."); |
| 812 | else |
| 813 | assert(Layout.getBaseClassOffset(PrimaryBase) == 0 && |
| 814 | "Primary base should have a zero offset!"); |
| 815 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 816 | AddMethods(BaseSubobject(PrimaryBase, 0), PrimaryBases); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 817 | |
| 818 | if (!PrimaryBases.insert(PrimaryBase)) |
| 819 | assert(false && "Found a duplicate primary base!"); |
| 820 | } |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 821 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 822 | // Now go through all virtual member functions and add them. |
| 823 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 824 | E = RD->method_end(); I != E; ++I) { |
| 825 | const CXXMethodDecl *MD = *I; |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 826 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 827 | if (!MD->isVirtual()) |
| 828 | continue; |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 829 | |
| 830 | // Get the final overrider. |
Anders Carlsson | a757582 | 2010-02-12 16:55:34 +0000 | [diff] [blame] | 831 | FinalOverriders::OverriderInfo Overrider = |
| 832 | Overriders.getOverrider(Base, MD); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 833 | |
| 834 | // Check if this virtual member function overrides a method in a primary |
| 835 | // base. If this is the case, and the return type doesn't require adjustment |
| 836 | // then we can just use the member function from the primary base. |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 837 | if (const CXXMethodDecl *OverriddenMD = |
| 838 | OverridesMethodInPrimaryBase(MD, PrimaryBases)) { |
| 839 | if (!ReturnTypeConversionRequiresAdjustment(MD, OverriddenMD)) |
| 840 | continue; |
| 841 | } |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 842 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 843 | // Check if this overrider needs a return adjustment. |
| 844 | FinalOverriders::BaseOffset ReturnAdjustmentOffset = |
| 845 | Overriders.getReturnAdjustmentOffset(Base, MD); |
| 846 | |
| 847 | ReturnAdjustment ReturnAdjustment = |
| 848 | ComputeReturnAdjustment(ReturnAdjustmentOffset); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 849 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 850 | AddMethod(Overrider.Method, ReturnAdjustment); |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 854 | void VtableBuilder::layoutSimpleVtable(BaseSubobject Base) { |
| 855 | const CXXRecordDecl *RD = Base.getBase(); |
| 856 | |
| 857 | // First, add the offset to top. |
| 858 | Components.push_back(VtableComponent::MakeOffsetToTop(0)); |
| 859 | |
| 860 | // Next, add the RTTI. |
| 861 | Components.push_back(VtableComponent::MakeRTTI(RD)); |
| 862 | |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 863 | uint64_t AddressPoint = Components.size(); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 864 | |
| 865 | // Now go through all virtual member functions and add them. |
| 866 | PrimaryBasesSetTy PrimaryBases; |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 867 | AddMethods(Base, PrimaryBases); |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 868 | |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 869 | // Record the address point. |
| 870 | AddressPoints.insert(std::make_pair(Base, AddressPoint)); |
| 871 | |
| 872 | // Record the address points for all primary bases. |
| 873 | for (PrimaryBasesSetTy::const_iterator I = PrimaryBases.begin(), |
| 874 | E = PrimaryBases.end(); I != E; ++I) { |
| 875 | const CXXRecordDecl *BaseDecl = *I; |
| 876 | |
| 877 | // We know that all the primary bases have the same offset as the base |
| 878 | // subobject. |
| 879 | BaseSubobject PrimaryBase(BaseDecl, Base.getBaseOffset()); |
| 880 | AddressPoints.insert(std::make_pair(PrimaryBase, AddressPoint)); |
| 881 | } |
| 882 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 883 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 884 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 885 | |
| 886 | // Traverse bases. |
| 887 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 888 | E = RD->bases_end(); I != E; ++I) { |
| 889 | const CXXRecordDecl *BaseDecl = |
| 890 | cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 891 | |
| 892 | // Ignore the primary base. |
| 893 | if (BaseDecl == PrimaryBase) |
| 894 | continue; |
| 895 | |
| 896 | assert(!I->isVirtual() && "FIXME: Handle virtual bases"); |
| 897 | |
| 898 | assert(false && "FIXME: Handle secondary virtual tables!"); |
| 899 | } |
| 900 | } |
| 901 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 902 | /// dumpLayout - Dump the vtable layout. |
| 903 | void VtableBuilder::dumpLayout(llvm::raw_ostream& Out) { |
| 904 | |
| 905 | Out << "Vtable for '" << MostDerivedClass->getQualifiedNameAsString(); |
| 906 | Out << "' (" << Components.size() << " entries).\n"; |
| 907 | |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 908 | // Iterate through the address points and insert them into a new map where |
| 909 | // they are keyed by the index and not the base object. |
| 910 | // Since an address point can be shared by multiple subobjects, we use an |
| 911 | // STL multimap. |
| 912 | std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex; |
| 913 | for (CGVtableInfo::AddressPointsMapTy::const_iterator I = |
| 914 | AddressPoints.begin(), E = AddressPoints.end(); I != E; ++I) { |
| 915 | const BaseSubobject& Base = I->first; |
| 916 | uint64_t Index = I->second; |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 917 | |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 918 | AddressPointsByIndex.insert(std::make_pair(Index, Base)); |
| 919 | } |
| 920 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 921 | unsigned NextReturnAdjustmentIndex = 0; |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 922 | for (unsigned I = 0, E = Components.size(); I != E; ++I) { |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 923 | uint64_t Index = I; |
| 924 | |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 925 | if (AddressPointsByIndex.count(I)) { |
Anders Carlsson | 28cbc8b | 2010-02-12 07:43:48 +0000 | [diff] [blame] | 926 | if (AddressPointsByIndex.count(Index) == 1) { |
| 927 | const BaseSubobject &Base = AddressPointsByIndex.find(Index)->second; |
| 928 | |
| 929 | // FIXME: Instead of dividing by 8, we should be using CharUnits. |
| 930 | Out << " -- (" << Base.getBase()->getQualifiedNameAsString(); |
| 931 | Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n"; |
| 932 | } else { |
| 933 | uint64_t BaseOffset = |
| 934 | AddressPointsByIndex.lower_bound(Index)->second.getBaseOffset(); |
| 935 | |
| 936 | // We store the class names in a set to get a stable order. |
| 937 | std::set<std::string> ClassNames; |
| 938 | for (std::multimap<uint64_t, BaseSubobject>::const_iterator I = |
| 939 | AddressPointsByIndex.lower_bound(Index), E = |
| 940 | AddressPointsByIndex.upper_bound(Index); I != E; ++I) { |
| 941 | assert(I->second.getBaseOffset() == BaseOffset && |
| 942 | "Invalid base offset!"); |
| 943 | const CXXRecordDecl *RD = I->second.getBase(); |
| 944 | ClassNames.insert(RD->getQualifiedNameAsString()); |
| 945 | } |
| 946 | |
| 947 | for (std::set<std::string>::const_iterator I = ClassNames.begin(), |
| 948 | E = ClassNames.end(); I != E; ++I) { |
| 949 | // FIXME: Instead of dividing by 8, we should be using CharUnits. |
| 950 | Out << " -- (" << *I; |
| 951 | Out << ", " << BaseOffset / 8 << ") vtable address --\n"; |
| 952 | } |
| 953 | } |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | Out << llvm::format("%4d | ", I); |
| 957 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 958 | const VtableComponent &Component = Components[I]; |
| 959 | |
| 960 | // Dump the component. |
| 961 | switch (Component.getKind()) { |
| 962 | // FIXME: Remove this default case. |
| 963 | default: |
| 964 | assert(false && "Unhandled component kind!"); |
| 965 | break; |
| 966 | |
| 967 | case VtableComponent::CK_OffsetToTop: |
| 968 | Out << "offset_to_top (" << Component.getOffsetToTop() << ")"; |
| 969 | break; |
| 970 | |
| 971 | case VtableComponent::CK_RTTI: |
| 972 | Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI"; |
| 973 | break; |
| 974 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 975 | case VtableComponent::CK_FunctionPointer: { |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 976 | const CXXMethodDecl *MD = Component.getFunctionDecl(); |
| 977 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 978 | std::string Str = |
| 979 | PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, |
| 980 | MD); |
| 981 | Out << Str; |
Anders Carlsson | 9824142 | 2010-02-12 02:38:13 +0000 | [diff] [blame] | 982 | if (MD->isPure()) |
| 983 | Out << " [pure]"; |
| 984 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 985 | // If this function pointer has a return adjustment thunk, dump it. |
| 986 | if (NextReturnAdjustmentIndex < ReturnAdjustments.size() && |
| 987 | ReturnAdjustments[NextReturnAdjustmentIndex].first == I) { |
| 988 | const ReturnAdjustment Adjustment = |
| 989 | ReturnAdjustments[NextReturnAdjustmentIndex].second; |
| 990 | |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 991 | Out << "\n [return adjustment: "; |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 992 | Out << Adjustment.NonVirtual << " non-virtual"; |
| 993 | |
| 994 | if (Adjustment.VBaseOffsetOffset) |
| 995 | Out << ", " << Adjustment.VBaseOffsetOffset << " vbase offset offset"; |
| 996 | Out << ']'; |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 997 | |
| 998 | NextReturnAdjustmentIndex++; |
| 999 | } |
| 1000 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 1001 | break; |
| 1002 | } |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1003 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 1004 | case VtableComponent::CK_CompleteDtorPointer: { |
| 1005 | const CXXDestructorDecl *DD = Component.getDestructorDecl(); |
| 1006 | |
| 1007 | Out << DD->getQualifiedNameAsString() << "() [complete]"; |
Anders Carlsson | 9824142 | 2010-02-12 02:38:13 +0000 | [diff] [blame] | 1008 | if (DD->isPure()) |
| 1009 | Out << " [pure]"; |
| 1010 | |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 1011 | break; |
| 1012 | } |
| 1013 | |
| 1014 | case VtableComponent::CK_DeletingDtorPointer: { |
| 1015 | const CXXDestructorDecl *DD = Component.getDestructorDecl(); |
| 1016 | |
| 1017 | Out << DD->getQualifiedNameAsString() << "() [deleting]"; |
Anders Carlsson | 9824142 | 2010-02-12 02:38:13 +0000 | [diff] [blame] | 1018 | if (DD->isPure()) |
| 1019 | Out << " [pure]"; |
| 1020 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | } |
Anders Carlsson | 822307b | 2010-02-11 17:18:51 +0000 | [diff] [blame] | 1025 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1026 | Out << '\n'; |
| 1027 | } |
| 1028 | |
| 1029 | } |
| 1030 | |
| 1031 | } |
| 1032 | |
| 1033 | namespace { |
| 1034 | class OldVtableBuilder { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1035 | public: |
| 1036 | /// Index_t - Vtable index type. |
| 1037 | typedef uint64_t Index_t; |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1038 | typedef std::vector<std::pair<GlobalDecl, |
| 1039 | std::pair<GlobalDecl, ThunkAdjustment> > > |
| 1040 | SavedAdjustmentsVectorTy; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1041 | private: |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 1042 | |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1043 | // VtableComponents - The components of the vtable being built. |
| 1044 | typedef llvm::SmallVector<llvm::Constant *, 64> VtableComponentsVectorTy; |
| 1045 | VtableComponentsVectorTy VtableComponents; |
| 1046 | |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1047 | const bool BuildVtable; |
| 1048 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1049 | llvm::Type *Ptr8Ty; |
Anders Carlsson | ac3f7bd | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 1050 | |
| 1051 | /// MostDerivedClass - The most derived class that this vtable is being |
| 1052 | /// built for. |
| 1053 | const CXXRecordDecl *MostDerivedClass; |
| 1054 | |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1055 | /// LayoutClass - The most derived class used for virtual base layout |
| 1056 | /// information. |
| 1057 | const CXXRecordDecl *LayoutClass; |
Mike Stump | 4cde626 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 1058 | /// LayoutOffset - The offset for Class in LayoutClass. |
| 1059 | uint64_t LayoutOffset; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1060 | /// BLayout - Layout for the most derived class that this vtable is being |
| 1061 | /// built for. |
| 1062 | const ASTRecordLayout &BLayout; |
| 1063 | llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary; |
| 1064 | llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase; |
| 1065 | llvm::Constant *rtti; |
| 1066 | llvm::LLVMContext &VMContext; |
| 1067 | CodeGenModule &CGM; // Per-module state. |
Anders Carlsson | 0e88116 | 2009-12-04 03:46:21 +0000 | [diff] [blame] | 1068 | |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1069 | llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall; |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1070 | llvm::DenseMap<GlobalDecl, Index_t> VCallOffset; |
Mike Stump | d99a4d2 | 2010-01-26 03:42:22 +0000 | [diff] [blame] | 1071 | llvm::DenseMap<GlobalDecl, Index_t> VCallOffsetForVCall; |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1072 | // This is the offset to the nearest virtual base |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1073 | llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1074 | llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex; |
Mike Stump | 94aff93 | 2009-10-27 23:46:47 +0000 | [diff] [blame] | 1075 | |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 1076 | /// PureVirtualFunction - Points to __cxa_pure_virtual. |
| 1077 | llvm::Constant *PureVirtualFn; |
| 1078 | |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 1079 | /// VtableMethods - A data structure for keeping track of methods in a vtable. |
| 1080 | /// Can add methods, override methods and iterate in vtable order. |
| 1081 | class VtableMethods { |
| 1082 | // MethodToIndexMap - Maps from a global decl to the index it has in the |
| 1083 | // Methods vector. |
| 1084 | llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap; |
| 1085 | |
| 1086 | /// Methods - The methods, in vtable order. |
| 1087 | typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy; |
| 1088 | MethodsVectorTy Methods; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1089 | MethodsVectorTy OrigMethods; |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 1090 | |
| 1091 | public: |
| 1092 | /// AddMethod - Add a method to the vtable methods. |
| 1093 | void AddMethod(GlobalDecl GD) { |
| 1094 | assert(!MethodToIndexMap.count(GD) && |
| 1095 | "Method has already been added!"); |
| 1096 | |
| 1097 | MethodToIndexMap[GD] = Methods.size(); |
| 1098 | Methods.push_back(GD); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1099 | OrigMethods.push_back(GD); |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /// OverrideMethod - Replace a method with another. |
| 1103 | void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) { |
| 1104 | llvm::DenseMap<GlobalDecl, uint64_t>::iterator i |
| 1105 | = MethodToIndexMap.find(OverriddenGD); |
| 1106 | assert(i != MethodToIndexMap.end() && "Did not find entry!"); |
| 1107 | |
| 1108 | // Get the index of the old decl. |
| 1109 | uint64_t Index = i->second; |
| 1110 | |
| 1111 | // Replace the old decl with the new decl. |
| 1112 | Methods[Index] = GD; |
| 1113 | |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 1114 | // And add the new. |
| 1115 | MethodToIndexMap[GD] = Index; |
| 1116 | } |
| 1117 | |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 1118 | /// getIndex - Gives the index of a passed in GlobalDecl. Returns false if |
| 1119 | /// the index couldn't be found. |
Benjamin Kramer | cce9fde | 2009-12-04 22:45:27 +0000 | [diff] [blame] | 1120 | bool getIndex(GlobalDecl GD, uint64_t &Index) const { |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 1121 | llvm::DenseMap<GlobalDecl, uint64_t>::const_iterator i |
| 1122 | = MethodToIndexMap.find(GD); |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1123 | |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 1124 | if (i == MethodToIndexMap.end()) |
| 1125 | return false; |
Anders Carlsson | c0c4993 | 2009-12-04 03:41:37 +0000 | [diff] [blame] | 1126 | |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 1127 | Index = i->second; |
| 1128 | return true; |
Anders Carlsson | c0c4993 | 2009-12-04 03:41:37 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1131 | GlobalDecl getOrigMethod(uint64_t Index) const { |
| 1132 | return OrigMethods[Index]; |
| 1133 | } |
| 1134 | |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 1135 | MethodsVectorTy::size_type size() const { |
| 1136 | return Methods.size(); |
| 1137 | } |
| 1138 | |
| 1139 | void clear() { |
| 1140 | MethodToIndexMap.clear(); |
| 1141 | Methods.clear(); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1142 | OrigMethods.clear(); |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Anders Carlsson | c0c4993 | 2009-12-04 03:41:37 +0000 | [diff] [blame] | 1145 | GlobalDecl operator[](uint64_t Index) const { |
Anders Carlsson | c7ab1a8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 1146 | return Methods[Index]; |
| 1147 | } |
| 1148 | }; |
| 1149 | |
| 1150 | /// Methods - The vtable methods we're currently building. |
| 1151 | VtableMethods Methods; |
| 1152 | |
Anders Carlsson | 98fdb24 | 2009-12-04 02:26:15 +0000 | [diff] [blame] | 1153 | /// ThisAdjustments - For a given index in the vtable, contains the 'this' |
| 1154 | /// pointer adjustment needed for a method. |
| 1155 | typedef llvm::DenseMap<uint64_t, ThunkAdjustment> ThisAdjustmentsMapTy; |
| 1156 | ThisAdjustmentsMapTy ThisAdjustments; |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 1157 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1158 | SavedAdjustmentsVectorTy SavedAdjustments; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1159 | |
Anders Carlsson | a875670 | 2009-12-04 02:22:02 +0000 | [diff] [blame] | 1160 | /// BaseReturnTypes - Contains the base return types of methods who have been |
| 1161 | /// overridden with methods whose return types require adjustment. Used for |
| 1162 | /// generating covariant thunk information. |
| 1163 | typedef llvm::DenseMap<uint64_t, CanQualType> BaseReturnTypesMapTy; |
| 1164 | BaseReturnTypesMapTy BaseReturnTypes; |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 1165 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1166 | std::vector<Index_t> VCalls; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1167 | |
| 1168 | typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1169 | // subAddressPoints - Used to hold the AddressPoints (offsets) into the built |
| 1170 | // vtable for use in computing the initializers for the VTT. |
| 1171 | llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1172 | |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1173 | /// AddressPoints - Address points for this vtable. |
| 1174 | CGVtableInfo::AddressPointsMapTy& AddressPoints; |
| 1175 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1176 | typedef CXXRecordDecl::method_iterator method_iter; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1177 | const uint32_t LLVMPointerWidth; |
| 1178 | Index_t extra; |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1179 | typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1180 | static llvm::DenseMap<CtorVtable_t, int64_t>& |
| 1181 | AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l, |
| 1182 | const CXXRecordDecl *c) { |
Anders Carlsson | 21431c5 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 1183 | CGVtableInfo::AddrMap_t *&oref = cgm.getVtableInfo().AddressPoints[l]; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1184 | if (oref == 0) |
Anders Carlsson | 21431c5 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 1185 | oref = new CGVtableInfo::AddrMap_t; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1186 | |
| 1187 | llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c]; |
| 1188 | if (ref == 0) |
| 1189 | ref = new llvm::DenseMap<CtorVtable_t, int64_t>; |
| 1190 | return *ref; |
| 1191 | } |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 1192 | |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1193 | bool DclIsSame(const FunctionDecl *New, const FunctionDecl *Old) { |
| 1194 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 1195 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 1196 | |
| 1197 | // C++ [temp.fct]p2: |
| 1198 | // A function template can be overloaded with other function templates |
| 1199 | // and with normal (non-template) functions. |
| 1200 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 1201 | return false; |
| 1202 | |
| 1203 | // Is the function New an overload of the function Old? |
| 1204 | QualType OldQType = CGM.getContext().getCanonicalType(Old->getType()); |
| 1205 | QualType NewQType = CGM.getContext().getCanonicalType(New->getType()); |
| 1206 | |
| 1207 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 1208 | // determine whether they are overloads. If we find any mismatch |
| 1209 | // in the signature, they are overloads. |
| 1210 | |
| 1211 | // If either of these functions is a K&R-style function (no |
| 1212 | // prototype), then we consider them to have matching signatures. |
| 1213 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 1214 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 1215 | return true; |
| 1216 | |
| 1217 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 1218 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 1219 | |
| 1220 | // The signature of a function includes the types of its |
| 1221 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 1222 | // of the ellipsis; see C++ DR 357). |
| 1223 | if (OldQType != NewQType && |
| 1224 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 1225 | OldType->isVariadic() != NewType->isVariadic() || |
| 1226 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 1227 | NewType->arg_type_begin()))) |
| 1228 | return false; |
| 1229 | |
| 1230 | #if 0 |
| 1231 | // C++ [temp.over.link]p4: |
| 1232 | // The signature of a function template consists of its function |
| 1233 | // signature, its return type and its template parameter list. The names |
| 1234 | // of the template parameters are significant only for establishing the |
| 1235 | // relationship between the template parameters and the rest of the |
| 1236 | // signature. |
| 1237 | // |
| 1238 | // We check the return type and template parameter lists for function |
| 1239 | // templates first; the remaining checks follow. |
| 1240 | if (NewTemplate && |
| 1241 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 1242 | OldTemplate->getTemplateParameters(), |
| 1243 | TPL_TemplateMatch) || |
| 1244 | OldType->getResultType() != NewType->getResultType())) |
| 1245 | return false; |
| 1246 | #endif |
| 1247 | |
| 1248 | // If the function is a class member, its signature includes the |
| 1249 | // cv-qualifiers (if any) on the function itself. |
| 1250 | // |
| 1251 | // As part of this, also check whether one of the member functions |
| 1252 | // is static, in which case they are not overloads (C++ |
| 1253 | // 13.1p2). While not part of the definition of the signature, |
| 1254 | // this check is important to determine whether these functions |
| 1255 | // can be overloaded. |
| 1256 | const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 1257 | const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 1258 | if (OldMethod && NewMethod && |
| 1259 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 1260 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 1261 | return false; |
| 1262 | |
| 1263 | // The signatures match; this is not an overload. |
| 1264 | return true; |
| 1265 | } |
| 1266 | |
| 1267 | typedef llvm::DenseMap<const CXXMethodDecl *, const CXXMethodDecl*> |
| 1268 | ForwardUnique_t; |
| 1269 | ForwardUnique_t ForwardUnique; |
| 1270 | llvm::DenseMap<const CXXMethodDecl*, const CXXMethodDecl*> UniqueOverrider; |
| 1271 | |
| 1272 | void BuildUniqueOverrider(const CXXMethodDecl *U, const CXXMethodDecl *MD) { |
| 1273 | const CXXMethodDecl *PrevU = UniqueOverrider[MD]; |
| 1274 | assert(U && "no unique overrider"); |
| 1275 | if (PrevU == U) |
| 1276 | return; |
| 1277 | if (PrevU != U && PrevU != 0) { |
| 1278 | // If already set, note the two sets as the same |
| 1279 | if (0) |
| 1280 | printf("%s::%s same as %s::%s\n", |
| 1281 | PrevU->getParent()->getNameAsCString(), |
| 1282 | PrevU->getNameAsCString(), |
| 1283 | U->getParent()->getNameAsCString(), |
| 1284 | U->getNameAsCString()); |
| 1285 | ForwardUnique[PrevU] = U; |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | // Not set, set it now |
| 1290 | if (0) |
| 1291 | printf("marking %s::%s %p override as %s::%s\n", |
| 1292 | MD->getParent()->getNameAsCString(), |
| 1293 | MD->getNameAsCString(), |
| 1294 | (void*)MD, |
| 1295 | U->getParent()->getNameAsCString(), |
| 1296 | U->getNameAsCString()); |
| 1297 | UniqueOverrider[MD] = U; |
| 1298 | |
| 1299 | for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(), |
| 1300 | me = MD->end_overridden_methods(); mi != me; ++mi) { |
| 1301 | BuildUniqueOverrider(U, *mi); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | void BuildUniqueOverriders(const CXXRecordDecl *RD) { |
| 1306 | if (0) printf("walking %s\n", RD->getNameAsCString()); |
| 1307 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 1308 | e = RD->method_end(); i != e; ++i) { |
| 1309 | const CXXMethodDecl *MD = *i; |
| 1310 | if (!MD->isVirtual()) |
| 1311 | continue; |
| 1312 | |
| 1313 | if (UniqueOverrider[MD] == 0) { |
| 1314 | // Only set this, if it hasn't been set yet. |
| 1315 | BuildUniqueOverrider(MD, MD); |
| 1316 | if (0) |
| 1317 | printf("top set is %s::%s %p\n", |
| 1318 | MD->getParent()->getNameAsCString(), |
| 1319 | MD->getNameAsCString(), |
| 1320 | (void*)MD); |
| 1321 | ForwardUnique[MD] = MD; |
| 1322 | } |
| 1323 | } |
| 1324 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1325 | e = RD->bases_end(); i != e; ++i) { |
| 1326 | const CXXRecordDecl *Base = |
| 1327 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1328 | BuildUniqueOverriders(Base); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | static int DclCmp(const void *p1, const void *p2) { |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 1333 | const CXXMethodDecl *MD1 = *(const CXXMethodDecl *const *)p1; |
| 1334 | const CXXMethodDecl *MD2 = *(const CXXMethodDecl *const *)p2; |
| 1335 | |
| 1336 | return (DeclarationName::compare(MD1->getDeclName(), MD2->getDeclName())); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | void MergeForwarding() { |
| 1340 | typedef llvm::SmallVector<const CXXMethodDecl *, 100> A_t; |
| 1341 | A_t A; |
| 1342 | for (ForwardUnique_t::iterator I = ForwardUnique.begin(), |
| 1343 | E = ForwardUnique.end(); I != E; ++I) { |
| 1344 | if (I->first == I->second) |
| 1345 | // Only add the roots of all trees |
| 1346 | A.push_back(I->first); |
| 1347 | } |
| 1348 | llvm::array_pod_sort(A.begin(), A.end(), DclCmp); |
| 1349 | for (A_t::iterator I = A.begin(), |
| 1350 | E = A.end(); I != E; ++I) { |
| 1351 | A_t::iterator J = I; |
John McCall | 7fe0b9e | 2010-02-13 01:04:05 +0000 | [diff] [blame] | 1352 | while (++J != E && DclCmp(I, J) == 0) |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1353 | if (DclIsSame(*I, *J)) { |
Eli Friedman | 4967762 | 2010-02-13 00:03:21 +0000 | [diff] [blame] | 1354 | if (0) printf("connecting %s\n", (*I)->getNameAsCString()); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1355 | ForwardUnique[*J] = *I; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | const CXXMethodDecl *getUnique(const CXXMethodDecl *MD) { |
| 1361 | const CXXMethodDecl *U = UniqueOverrider[MD]; |
| 1362 | assert(U && "unique overrider not found"); |
| 1363 | while (ForwardUnique.count(U)) { |
| 1364 | const CXXMethodDecl *NU = ForwardUnique[U]; |
| 1365 | if (NU == U) break; |
| 1366 | U = NU; |
| 1367 | } |
| 1368 | return U; |
| 1369 | } |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 1370 | |
| 1371 | GlobalDecl getUnique(GlobalDecl GD) { |
| 1372 | const CXXMethodDecl *Unique = getUnique(cast<CXXMethodDecl>(GD.getDecl())); |
| 1373 | |
| 1374 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Unique)) |
| 1375 | return GlobalDecl(CD, GD.getCtorType()); |
| 1376 | |
| 1377 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Unique)) |
| 1378 | return GlobalDecl(DD, GD.getDtorType()); |
| 1379 | |
| 1380 | return Unique; |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 1383 | /// getPureVirtualFn - Return the __cxa_pure_virtual function. |
| 1384 | llvm::Constant* getPureVirtualFn() { |
| 1385 | if (!PureVirtualFn) { |
| 1386 | const llvm::FunctionType *Ty = |
| 1387 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 1388 | /*isVarArg=*/false); |
| 1389 | PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual")); |
| 1390 | } |
| 1391 | |
| 1392 | return PureVirtualFn; |
| 1393 | } |
| 1394 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1395 | public: |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1396 | OldVtableBuilder(const CXXRecordDecl *MostDerivedClass, |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1397 | const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm, |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1398 | bool build, CGVtableInfo::AddressPointsMapTy& AddressPoints) |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1399 | : BuildVtable(build), MostDerivedClass(MostDerivedClass), LayoutClass(l), |
| 1400 | LayoutOffset(lo), BLayout(cgm.getContext().getASTRecordLayout(l)), |
| 1401 | rtti(0), VMContext(cgm.getModule().getContext()),CGM(cgm), |
| 1402 | PureVirtualFn(0), |
Anders Carlsson | ac3f7bd | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 1403 | subAddressPoints(AllocAddressPoint(cgm, l, MostDerivedClass)), |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1404 | AddressPoints(AddressPoints), |
| 1405 | LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) |
| 1406 | { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1407 | Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 1408 | if (BuildVtable) { |
| 1409 | QualType ClassType = CGM.getContext().getTagDeclType(MostDerivedClass); |
| 1410 | rtti = CGM.GetAddrOfRTTIDescriptor(ClassType); |
| 1411 | } |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1412 | BuildUniqueOverriders(MostDerivedClass); |
| 1413 | MergeForwarding(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1416 | // getVtableComponents - Returns a reference to the vtable components. |
| 1417 | const VtableComponentsVectorTy &getVtableComponents() const { |
| 1418 | return VtableComponents; |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1421 | llvm::DenseMap<const CXXRecordDecl *, uint64_t> &getVBIndex() |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1422 | { return VBIndex; } |
| 1423 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1424 | SavedAdjustmentsVectorTy &getSavedAdjustments() |
| 1425 | { return SavedAdjustments; } |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1426 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1427 | llvm::Constant *wrap(Index_t i) { |
| 1428 | llvm::Constant *m; |
| 1429 | m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i); |
| 1430 | return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); |
| 1431 | } |
| 1432 | |
| 1433 | llvm::Constant *wrap(llvm::Constant *m) { |
| 1434 | return llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
| 1435 | } |
| 1436 | |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1437 | //#define D1(x) |
| 1438 | #define D1(X) do { if (getenv("DEBUG")) { X; } } while (0) |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1439 | |
| 1440 | void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset, |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 1441 | bool updateVBIndex, Index_t current_vbindex) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1442 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1443 | e = RD->bases_end(); i != e; ++i) { |
| 1444 | const CXXRecordDecl *Base = |
| 1445 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 1446 | Index_t next_vbindex = current_vbindex; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1447 | if (i->isVirtual() && !SeenVBase.count(Base)) { |
| 1448 | SeenVBase.insert(Base); |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 1449 | if (updateVBIndex) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1450 | next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8) |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 1451 | - 3*LLVMPointerWidth/8); |
| 1452 | VBIndex[Base] = next_vbindex; |
| 1453 | } |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1454 | int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8; |
| 1455 | VCalls.push_back((0?700:0) + BaseOffset); |
| 1456 | D1(printf(" vbase for %s at %d delta %d most derived %s\n", |
| 1457 | Base->getNameAsCString(), |
| 1458 | (int)-VCalls.size()-3, (int)BaseOffset, |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1459 | MostDerivedClass->getNameAsCString())); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1460 | } |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 1461 | // We also record offsets for non-virtual bases to closest enclosing |
| 1462 | // virtual base. We do this so that we don't have to search |
| 1463 | // for the nearst virtual base class when generating thunks. |
| 1464 | if (updateVBIndex && VBIndex.count(Base) == 0) |
| 1465 | VBIndex[Base] = next_vbindex; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1466 | GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | void StartNewTable() { |
| 1471 | SeenVBase.clear(); |
| 1472 | } |
| 1473 | |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 1474 | Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B, |
| 1475 | Index_t Offset = 0) { |
| 1476 | |
| 1477 | if (B == D) |
| 1478 | return Offset; |
| 1479 | |
| 1480 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D); |
| 1481 | for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(), |
| 1482 | e = D->bases_end(); i != e; ++i) { |
| 1483 | const CXXRecordDecl *Base = |
| 1484 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1485 | int64_t BaseOffset = 0; |
| 1486 | if (!i->isVirtual()) |
| 1487 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 1488 | int64_t o = getNVOffset_1(Base, B, BaseOffset); |
| 1489 | if (o >= 0) |
| 1490 | return o; |
| 1491 | } |
| 1492 | |
| 1493 | return -1; |
| 1494 | } |
| 1495 | |
| 1496 | /// getNVOffset - Returns the non-virtual offset for the given (B) base of the |
| 1497 | /// derived class D. |
| 1498 | Index_t getNVOffset(QualType qB, QualType qD) { |
Mike Stump | 9c21289 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 1499 | qD = qD->getPointeeType(); |
| 1500 | qB = qB->getPointeeType(); |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 1501 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 1502 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
| 1503 | int64_t o = getNVOffset_1(D, B); |
| 1504 | if (o >= 0) |
| 1505 | return o; |
| 1506 | |
| 1507 | assert(false && "FIXME: non-virtual base not found"); |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1511 | /// getVbaseOffset - Returns the index into the vtable for the virtual base |
| 1512 | /// offset for the given (B) virtual base of the derived class D. |
| 1513 | Index_t getVbaseOffset(QualType qB, QualType qD) { |
Mike Stump | 9c21289 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 1514 | qD = qD->getPointeeType(); |
| 1515 | qB = qB->getPointeeType(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1516 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 1517 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
Anders Carlsson | ac3f7bd | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 1518 | if (D != MostDerivedClass) |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 1519 | return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1520 | llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i; |
| 1521 | i = VBIndex.find(B); |
| 1522 | if (i != VBIndex.end()) |
| 1523 | return i->second; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1524 | |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 1525 | assert(false && "FIXME: Base not found"); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1526 | return 0; |
| 1527 | } |
| 1528 | |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 1529 | bool OverrideMethod(GlobalDecl GD, bool MorallyVirtual, |
| 1530 | Index_t OverrideOffset, Index_t Offset, |
| 1531 | int64_t CurrentVBaseOffset); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1532 | |
Anders Carlsson | adfa267 | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 1533 | /// AppendMethods - Append the current methods to the vtable. |
Anders Carlsson | bf54027 | 2009-12-04 02:56:03 +0000 | [diff] [blame] | 1534 | void AppendMethodsToVtable(); |
Anders Carlsson | adfa267 | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 1535 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1536 | llvm::Constant *WrapAddrOf(GlobalDecl GD) { |
| 1537 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 1538 | |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 1539 | const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD); |
Mike Stump | 1ae3178 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 1540 | |
Mike Stump | c085a98 | 2009-12-03 16:55:20 +0000 | [diff] [blame] | 1541 | return wrap(CGM.GetAddrOfFunction(GD, Ty)); |
Mike Stump | 1ae3178 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1544 | void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset, |
| 1545 | int64_t CurrentVBaseOffset) { |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1546 | for (Path_t::reverse_iterator i = Path->rbegin(), |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1547 | e = Path->rend(); i != e; ++i) { |
| 1548 | const CXXRecordDecl *RD = i->first; |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 1549 | int64_t OverrideOffset = i->second; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1550 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 1551 | ++mi) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1552 | const CXXMethodDecl *MD = *mi; |
| 1553 | |
| 1554 | if (!MD->isVirtual()) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1555 | continue; |
| 1556 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1557 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1558 | // Override both the complete and the deleting destructor. |
| 1559 | GlobalDecl CompDtor(DD, Dtor_Complete); |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 1560 | OverrideMethod(CompDtor, MorallyVirtual, OverrideOffset, Offset, |
| 1561 | CurrentVBaseOffset); |
| 1562 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1563 | GlobalDecl DeletingDtor(DD, Dtor_Deleting); |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 1564 | OverrideMethod(DeletingDtor, MorallyVirtual, OverrideOffset, Offset, |
| 1565 | CurrentVBaseOffset); |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1566 | } else { |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 1567 | OverrideMethod(MD, MorallyVirtual, OverrideOffset, Offset, |
| 1568 | CurrentVBaseOffset); |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1569 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | } |
| 1573 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1574 | void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 1575 | int64_t CurrentVBaseOffset) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1576 | // If we can find a previously allocated slot for this, reuse it. |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 1577 | if (OverrideMethod(GD, MorallyVirtual, Offset, Offset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1578 | CurrentVBaseOffset)) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1579 | return; |
| 1580 | |
Mike Stump | 372ade2 | 2010-01-22 18:48:47 +0000 | [diff] [blame] | 1581 | D1(printf(" vfn for %s at %d\n", |
| 1582 | dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(), |
| 1583 | (int)Methods.size())); |
| 1584 | |
Anders Carlsson | a7f1911 | 2009-12-04 02:08:24 +0000 | [diff] [blame] | 1585 | // We didn't find an entry in the vtable that we could use, add a new |
| 1586 | // entry. |
| 1587 | Methods.AddMethod(GD); |
| 1588 | |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1589 | VCallOffset[GD] = Offset/8 - CurrentVBaseOffset/8; |
| 1590 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1591 | if (MorallyVirtual) { |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 1592 | GlobalDecl UGD = getUnique(GD); |
| 1593 | const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl()); |
| 1594 | |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1595 | assert(UMD && "final overrider not found"); |
| 1596 | |
| 1597 | Index_t &idx = VCall[UMD]; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1598 | // Allocate the first one, after that, we reuse the previous one. |
| 1599 | if (idx == 0) { |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 1600 | VCallOffsetForVCall[UGD] = Offset/8; |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1601 | NonVirtualOffset[UMD] = Offset/8 - CurrentVBaseOffset/8; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1602 | idx = VCalls.size()+1; |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1603 | VCalls.push_back(Offset/8 - CurrentVBaseOffset/8); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1604 | D1(printf(" vcall for %s at %d with delta %d\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1605 | dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(), |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1606 | (int)-VCalls.size()-3, (int)VCalls[idx-1])); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 1612 | Index_t Offset, int64_t CurrentVBaseOffset) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1613 | 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] | 1614 | ++mi) { |
| 1615 | const CXXMethodDecl *MD = *mi; |
| 1616 | if (!MD->isVirtual()) |
| 1617 | continue; |
| 1618 | |
| 1619 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1620 | // For destructors, add both the complete and the deleting destructor |
| 1621 | // to the vtable. |
| 1622 | AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1623 | CurrentVBaseOffset); |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 1624 | AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset, |
| 1625 | CurrentVBaseOffset); |
| 1626 | } else |
| 1627 | AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1628 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, |
| 1632 | const CXXRecordDecl *PrimaryBase, |
| 1633 | bool PrimaryBaseWasVirtual, bool MorallyVirtual, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1634 | int64_t Offset, int64_t CurrentVBaseOffset, |
| 1635 | Path_t *Path) { |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1636 | Path->push_back(std::make_pair(RD, Offset)); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1637 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1638 | e = RD->bases_end(); i != e; ++i) { |
| 1639 | if (i->isVirtual()) |
| 1640 | continue; |
| 1641 | const CXXRecordDecl *Base = |
| 1642 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1643 | uint64_t o = Offset + Layout.getBaseClassOffset(Base); |
| 1644 | StartNewTable(); |
| 1645 | GenerateVtableForBase(Base, o, MorallyVirtual, false, |
| 1646 | true, Base == PrimaryBase && !PrimaryBaseWasVirtual, |
| 1647 | CurrentVBaseOffset, Path); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1648 | } |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1649 | Path->pop_back(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 1652 | // #define D(X) do { X; } while (0) |
| 1653 | #define D(X) |
| 1654 | |
| 1655 | void insertVCalls(int InsertionPoint) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1656 | D1(printf("============= combining vbase/vcall\n")); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 1657 | D(VCalls.insert(VCalls.begin(), 673)); |
| 1658 | D(VCalls.push_back(672)); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1659 | |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1660 | VtableComponents.insert(VtableComponents.begin() + InsertionPoint, |
| 1661 | VCalls.size(), 0); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1662 | if (BuildVtable) { |
| 1663 | // The vcalls come first... |
| 1664 | for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(), |
| 1665 | e = VCalls.rend(); |
| 1666 | i != e; ++i) |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1667 | VtableComponents[InsertionPoint++] = wrap((0?600:0) + *i); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1668 | } |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 1669 | VCalls.clear(); |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1670 | VCall.clear(); |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1671 | VCallOffsetForVCall.clear(); |
| 1672 | VCallOffset.clear(); |
| 1673 | NonVirtualOffset.clear(); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 1676 | void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset, |
| 1677 | Index_t AddressPoint) { |
| 1678 | 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] | 1679 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString(), |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 1680 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1681 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1682 | AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint; |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 1683 | |
| 1684 | // Now also add the address point for all our primary bases. |
| 1685 | while (1) { |
| 1686 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1687 | RD = Layout.getPrimaryBase(); |
| 1688 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 1689 | // FIXME: Double check this. |
| 1690 | if (RD == 0) |
| 1691 | break; |
| 1692 | if (PrimaryBaseWasVirtual && |
| 1693 | BLayout.getVBaseClassOffset(RD) != Offset) |
| 1694 | break; |
| 1695 | 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] | 1696 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString(), |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 1697 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1698 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1699 | AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint; |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 1704 | void FinishGenerateVtable(const CXXRecordDecl *RD, |
| 1705 | const ASTRecordLayout &Layout, |
| 1706 | const CXXRecordDecl *PrimaryBase, |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1707 | bool ForNPNVBases, bool WasPrimaryBase, |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 1708 | bool PrimaryBaseWasVirtual, |
| 1709 | bool MorallyVirtual, int64_t Offset, |
| 1710 | bool ForVirtualBase, int64_t CurrentVBaseOffset, |
| 1711 | Path_t *Path) { |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1712 | bool alloc = false; |
| 1713 | if (Path == 0) { |
| 1714 | alloc = true; |
| 1715 | Path = new Path_t; |
| 1716 | } |
| 1717 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1718 | StartNewTable(); |
| 1719 | extra = 0; |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1720 | Index_t AddressPoint = 0; |
| 1721 | int VCallInsertionPoint = 0; |
| 1722 | if (!ForNPNVBases || !WasPrimaryBase) { |
| 1723 | bool DeferVCalls = MorallyVirtual || ForVirtualBase; |
| 1724 | VCallInsertionPoint = VtableComponents.size(); |
| 1725 | if (!DeferVCalls) { |
| 1726 | insertVCalls(VCallInsertionPoint); |
| 1727 | } else |
| 1728 | // FIXME: just for extra, or for all uses of VCalls.size post this? |
| 1729 | extra = -VCalls.size(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1730 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1731 | // Add the offset to top. |
| 1732 | VtableComponents.push_back(BuildVtable ? wrap(-((Offset-LayoutOffset)/8)) : 0); |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 1733 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1734 | // Add the RTTI information. |
| 1735 | VtableComponents.push_back(rtti); |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 1736 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1737 | AddressPoint = VtableComponents.size(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1738 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1739 | AppendMethodsToVtable(); |
| 1740 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1741 | |
| 1742 | // and then the non-virtual bases. |
| 1743 | NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1744 | MorallyVirtual, Offset, CurrentVBaseOffset, Path); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 1745 | |
| 1746 | if (ForVirtualBase) { |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1747 | // FIXME: We're adding to VCalls in callers, we need to do the overrides |
| 1748 | // in the inner part, so that we know the complete set of vcalls during |
| 1749 | // the build and don't have to insert into methods. Saving out the |
| 1750 | // AddressPoint here, would need to be fixed, if we didn't do that. Also |
| 1751 | // retroactively adding vcalls for overrides later wind up in the wrong |
| 1752 | // place, the vcall slot has to be alloted during the walk of the base |
| 1753 | // when the function is first introduces. |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 1754 | AddressPoint += VCalls.size(); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1755 | insertVCalls(VCallInsertionPoint); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1758 | if (!ForNPNVBases || !WasPrimaryBase) |
| 1759 | AddAddressPoints(RD, Offset, AddressPoint); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1760 | |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1761 | if (alloc) { |
| 1762 | delete Path; |
| 1763 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1766 | void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 1767 | bool updateVBIndex, Index_t current_vbindex, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 1768 | int64_t CurrentVBaseOffset) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1769 | if (!RD->isDynamicClass()) |
| 1770 | return; |
| 1771 | |
| 1772 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1773 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1774 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 1775 | |
| 1776 | // vtables are composed from the chain of primaries. |
Eli Friedman | dfe33bb | 2009-12-04 08:52:11 +0000 | [diff] [blame] | 1777 | if (PrimaryBase && !PrimaryBaseWasVirtual) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1778 | D1(printf(" doing primaries for %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1779 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Eli Friedman | dfe33bb | 2009-12-04 08:52:11 +0000 | [diff] [blame] | 1780 | Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
| 1781 | updateVBIndex, current_vbindex, CurrentVBaseOffset); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | D1(printf(" doing vcall entries for %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1785 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1786 | |
| 1787 | // And add the virtuals for the class to the primary vtable. |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 1788 | AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 1792 | bool updateVBIndex, Index_t current_vbindex, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1793 | bool RDisVirtualBase, int64_t CurrentVBaseOffset, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 1794 | bool bottom) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1795 | if (!RD->isDynamicClass()) |
| 1796 | return; |
| 1797 | |
| 1798 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1799 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1800 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 1801 | |
| 1802 | // vtables are composed from the chain of primaries. |
| 1803 | if (PrimaryBase) { |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1804 | int BaseCurrentVBaseOffset = CurrentVBaseOffset; |
| 1805 | if (PrimaryBaseWasVirtual) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1806 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1807 | BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase); |
| 1808 | } |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1809 | |
| 1810 | D1(printf(" doing primaries for %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1811 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1812 | |
| 1813 | VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1814 | updateVBIndex, current_vbindex, PrimaryBaseWasVirtual, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 1815 | BaseCurrentVBaseOffset, false); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1818 | D1(printf(" doing vbase entries for %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1819 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1820 | GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex); |
| 1821 | |
| 1822 | if (RDisVirtualBase || bottom) { |
| 1823 | Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 1824 | CurrentVBaseOffset); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1825 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 1828 | void GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0, |
| 1829 | bool MorallyVirtual = false, |
| 1830 | bool ForVirtualBase = false, |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1831 | bool ForNPNVBases = false, |
| 1832 | bool WasPrimaryBase = true, |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 1833 | int CurrentVBaseOffset = 0, |
| 1834 | Path_t *Path = 0) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1835 | if (!RD->isDynamicClass()) |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 1836 | return; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1837 | |
Mike Stump | 92774d1 | 2009-11-13 02:35:38 +0000 | [diff] [blame] | 1838 | // Construction vtable don't need parts that have no virtual bases and |
| 1839 | // aren't morally virtual. |
Anders Carlsson | ac3f7bd | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 1840 | if ((LayoutClass != MostDerivedClass) && |
| 1841 | RD->getNumVBases() == 0 && !MorallyVirtual) |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 1842 | return; |
Mike Stump | 92774d1 | 2009-11-13 02:35:38 +0000 | [diff] [blame] | 1843 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1844 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1845 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1846 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 1847 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1848 | extra = 0; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1849 | D1(printf("building entries for base %s most derived %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1850 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1851 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1852 | if (ForVirtualBase) |
| 1853 | extra = VCalls.size(); |
| 1854 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1855 | if (!ForNPNVBases || !WasPrimaryBase) { |
| 1856 | VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, |
| 1857 | ForVirtualBase, CurrentVBaseOffset, true); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1858 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1859 | if (Path) |
| 1860 | OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset); |
| 1861 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1862 | |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1863 | FinishGenerateVtable(RD, Layout, PrimaryBase, ForNPNVBases, WasPrimaryBase, |
| 1864 | PrimaryBaseWasVirtual, MorallyVirtual, Offset, |
| 1865 | ForVirtualBase, CurrentVBaseOffset, Path); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | void GenerateVtableForVBases(const CXXRecordDecl *RD, |
| 1869 | int64_t Offset = 0, |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1870 | Path_t *Path = 0) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1871 | bool alloc = false; |
| 1872 | if (Path == 0) { |
| 1873 | alloc = true; |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1874 | Path = new Path_t; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1875 | } |
| 1876 | // FIXME: We also need to override using all paths to a virtual base, |
| 1877 | // right now, we just process the first path |
| 1878 | Path->push_back(std::make_pair(RD, Offset)); |
| 1879 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1880 | e = RD->bases_end(); i != e; ++i) { |
| 1881 | const CXXRecordDecl *Base = |
| 1882 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1883 | if (i->isVirtual() && !IndirectPrimary.count(Base)) { |
| 1884 | // Mark it so we don't output it twice. |
| 1885 | IndirectPrimary.insert(Base); |
| 1886 | StartNewTable(); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 1887 | VCall.clear(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1888 | int64_t BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 1889 | int64_t CurrentVBaseOffset = BaseOffset; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1890 | D1(printf("vtable %s virtual base %s\n", |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1891 | MostDerivedClass->getNameAsCString(), Base->getNameAsCString())); |
Mike Stump | fc9f16c | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 1892 | GenerateVtableForBase(Base, BaseOffset, true, true, false, |
| 1893 | true, CurrentVBaseOffset, Path); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1894 | } |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1895 | int64_t BaseOffset; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1896 | if (i->isVirtual()) |
| 1897 | BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1898 | else { |
| 1899 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1900 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 1901 | } |
| 1902 | |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1903 | if (Base->getNumVBases()) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1904 | GenerateVtableForVBases(Base, BaseOffset, Path); |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 1905 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1906 | } |
| 1907 | Path->pop_back(); |
| 1908 | if (alloc) |
| 1909 | delete Path; |
| 1910 | } |
| 1911 | }; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1912 | } // end anonymous namespace |
| 1913 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 1914 | bool OldVtableBuilder::OverrideMethod(GlobalDecl GD, bool MorallyVirtual, |
Eli Friedman | 367d122 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 1915 | Index_t OverrideOffset, Index_t Offset, |
| 1916 | int64_t CurrentVBaseOffset) { |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1917 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 1918 | |
| 1919 | const bool isPure = MD->isPure(); |
Anders Carlsson | 7ca4643 | 2009-12-05 17:04:47 +0000 | [diff] [blame] | 1920 | |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1921 | // FIXME: Should OverrideOffset's be Offset? |
| 1922 | |
Anders Carlsson | 7ca4643 | 2009-12-05 17:04:47 +0000 | [diff] [blame] | 1923 | for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(), |
| 1924 | e = MD->end_overridden_methods(); mi != e; ++mi) { |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1925 | GlobalDecl OGD; |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1926 | GlobalDecl OGD2; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1927 | |
Anders Carlsson | 3aaf486 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 1928 | const CXXMethodDecl *OMD = *mi; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1929 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD)) |
| 1930 | OGD = GlobalDecl(DD, GD.getDtorType()); |
| 1931 | else |
| 1932 | OGD = OMD; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1933 | |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1934 | // Check whether this is the method being overridden in this section of |
| 1935 | // the vtable. |
Anders Carlsson | 77c23e5 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 1936 | uint64_t Index; |
| 1937 | if (!Methods.getIndex(OGD, Index)) |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1938 | continue; |
| 1939 | |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1940 | OGD2 = OGD; |
| 1941 | |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1942 | // Get the original method, which we should be computing thunks, etc, |
| 1943 | // against. |
| 1944 | OGD = Methods.getOrigMethod(Index); |
| 1945 | OMD = cast<CXXMethodDecl>(OGD.getDecl()); |
| 1946 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1947 | QualType ReturnType = |
| 1948 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 1949 | QualType OverriddenReturnType = |
| 1950 | OMD->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1951 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1952 | // Check if we need a return type adjustment. |
| 1953 | if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType, |
| 1954 | OverriddenReturnType)) { |
| 1955 | CanQualType &BaseReturnType = BaseReturnTypes[Index]; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1956 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1957 | // Insert the base return type. |
| 1958 | if (BaseReturnType.isNull()) |
| 1959 | BaseReturnType = |
| 1960 | CGM.getContext().getCanonicalType(OverriddenReturnType); |
| 1961 | } |
Anders Carlsson | dd454be | 2009-12-04 03:52:52 +0000 | [diff] [blame] | 1962 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1963 | Methods.OverrideMethod(OGD, GD); |
| 1964 | |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 1965 | GlobalDecl UGD = getUnique(GD); |
| 1966 | const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl()); |
| 1967 | assert(UGD.getDecl() && "unique overrider not found"); |
| 1968 | assert(UGD == getUnique(OGD) && "unique overrider not unique"); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1969 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1970 | ThisAdjustments.erase(Index); |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1971 | if (MorallyVirtual || VCall.count(UMD)) { |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1972 | |
Mike Stump | cf0b9cc | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1973 | Index_t &idx = VCall[UMD]; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1974 | if (idx == 0) { |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1975 | VCallOffset[GD] = VCallOffset[OGD]; |
| 1976 | // NonVirtualOffset[UMD] = CurrentVBaseOffset/8 - OverrideOffset/8; |
| 1977 | NonVirtualOffset[UMD] = VCallOffset[OGD]; |
Mike Stump | d99a4d2 | 2010-01-26 03:42:22 +0000 | [diff] [blame] | 1978 | VCallOffsetForVCall[UMD] = OverrideOffset/8; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1979 | idx = VCalls.size()+1; |
Mike Stump | d99a4d2 | 2010-01-26 03:42:22 +0000 | [diff] [blame] | 1980 | VCalls.push_back(OverrideOffset/8 - CurrentVBaseOffset/8); |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1981 | D1(printf(" vcall for %s at %d with delta %d most derived %s\n", |
| 1982 | MD->getNameAsString().c_str(), (int)-idx-3, |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1983 | (int)VCalls[idx-1], MostDerivedClass->getNameAsCString())); |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1984 | } else { |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1985 | VCallOffset[GD] = NonVirtualOffset[UMD]; |
Anders Carlsson | 0f0bbbc | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 1986 | VCalls[idx-1] = -VCallOffsetForVCall[UGD] + OverrideOffset/8; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1987 | D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n", |
| 1988 | MD->getNameAsString().c_str(), (int)-idx-3, |
Mike Stump | d658490 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1989 | (int)VCalls[idx-1], MostDerivedClass->getNameAsCString())); |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1990 | } |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1991 | int64_t NonVirtualAdjustment = -VCallOffset[OGD]; |
Mike Stump | 852f5ce | 2010-01-26 22:44:01 +0000 | [diff] [blame] | 1992 | QualType DerivedType = MD->getThisType(CGM.getContext()); |
| 1993 | QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext()); |
| 1994 | int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8); |
| 1995 | if (NonVirtualAdjustment2 != NonVirtualAdjustment) { |
| 1996 | NonVirtualAdjustment = NonVirtualAdjustment2; |
| 1997 | } |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1998 | int64_t VirtualAdjustment = |
| 1999 | -((idx + extra + 2) * LLVMPointerWidth / 8); |
Anders Carlsson | 891bb4b | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 2000 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 2001 | // Optimize out virtual adjustments of 0. |
| 2002 | if (VCalls[idx-1] == 0) |
| 2003 | VirtualAdjustment = 0; |
Anders Carlsson | 891bb4b | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 2004 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 2005 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, |
| 2006 | VirtualAdjustment); |
Anders Carlsson | bc0e339 | 2009-12-03 02:22:59 +0000 | [diff] [blame] | 2007 | |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2008 | if (!isPure && !ThisAdjustment.isEmpty()) { |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 2009 | ThisAdjustments[Index] = ThisAdjustment; |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2010 | SavedAdjustments.push_back( |
| 2011 | std::make_pair(GD, std::make_pair(OGD, ThisAdjustment))); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2012 | } |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 2013 | return true; |
| 2014 | } |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 2015 | |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 2016 | VCallOffset[GD] = VCallOffset[OGD2] - OverrideOffset/8; |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 2017 | |
Mike Stump | 15189fb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 2018 | int64_t NonVirtualAdjustment = -VCallOffset[GD]; |
| 2019 | QualType DerivedType = MD->getThisType(CGM.getContext()); |
| 2020 | QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext()); |
| 2021 | int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8); |
| 2022 | if (NonVirtualAdjustment2 != NonVirtualAdjustment) { |
| 2023 | NonVirtualAdjustment = NonVirtualAdjustment2; |
| 2024 | } |
| 2025 | |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 2026 | if (NonVirtualAdjustment) { |
| 2027 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0); |
| 2028 | |
Eli Friedman | f062d9d | 2009-12-06 22:33:51 +0000 | [diff] [blame] | 2029 | if (!isPure) { |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 2030 | ThisAdjustments[Index] = ThisAdjustment; |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2031 | SavedAdjustments.push_back( |
| 2032 | std::make_pair(GD, std::make_pair(OGD, ThisAdjustment))); |
Eli Friedman | f062d9d | 2009-12-06 22:33:51 +0000 | [diff] [blame] | 2033 | } |
Eli Friedman | 4714583 | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 2034 | } |
| 2035 | return true; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | return false; |
Anders Carlsson | 27f69d0 | 2009-11-27 22:21:51 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2041 | void OldVtableBuilder::AppendMethodsToVtable() { |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 2042 | if (!BuildVtable) { |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 2043 | VtableComponents.insert(VtableComponents.end(), Methods.size(), |
| 2044 | (llvm::Constant *)0); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 2045 | ThisAdjustments.clear(); |
| 2046 | BaseReturnTypes.clear(); |
| 2047 | Methods.clear(); |
| 2048 | return; |
| 2049 | } |
| 2050 | |
Anders Carlsson | 15318f4 | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 2051 | // Reserve room in the vtable for our new methods. |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 2052 | VtableComponents.reserve(VtableComponents.size() + Methods.size()); |
Anders Carlsson | 29202d5 | 2009-12-04 03:07:26 +0000 | [diff] [blame] | 2053 | |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 2054 | for (unsigned i = 0, e = Methods.size(); i != e; ++i) { |
| 2055 | GlobalDecl GD = Methods[i]; |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 2056 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 2057 | |
| 2058 | // Get the 'this' pointer adjustment. |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 2059 | ThunkAdjustment ThisAdjustment = ThisAdjustments.lookup(i); |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 2060 | |
| 2061 | // Construct the return type adjustment. |
| 2062 | ThunkAdjustment ReturnAdjustment; |
| 2063 | |
| 2064 | QualType BaseReturnType = BaseReturnTypes.lookup(i); |
| 2065 | if (!BaseReturnType.isNull() && !MD->isPure()) { |
| 2066 | QualType DerivedType = |
| 2067 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 2068 | |
| 2069 | int64_t NonVirtualAdjustment = |
| 2070 | getNVOffset(BaseReturnType, DerivedType) / 8; |
| 2071 | |
| 2072 | int64_t VirtualAdjustment = |
| 2073 | getVbaseOffset(BaseReturnType, DerivedType); |
| 2074 | |
| 2075 | ReturnAdjustment = ThunkAdjustment(NonVirtualAdjustment, |
| 2076 | VirtualAdjustment); |
| 2077 | } |
| 2078 | |
Anders Carlsson | 2fce216 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 2079 | llvm::Constant *Method = 0; |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 2080 | if (!ReturnAdjustment.isEmpty()) { |
| 2081 | // Build a covariant thunk. |
| 2082 | CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2083 | Method = wrap(CGM.GetAddrOfCovariantThunk(GD, Adjustment)); |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 2084 | } else if (!ThisAdjustment.isEmpty()) { |
| 2085 | // Build a "regular" thunk. |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2086 | Method = wrap(CGM.GetAddrOfThunk(GD, ThisAdjustment)); |
Anders Carlsson | bf54027 | 2009-12-04 02:56:03 +0000 | [diff] [blame] | 2087 | } else if (MD->isPure()) { |
| 2088 | // We have a pure virtual method. |
Anders Carlsson | 2fce216 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 2089 | Method = getPureVirtualFn(); |
| 2090 | } else { |
| 2091 | // We have a good old regular method. |
| 2092 | Method = WrapAddrOf(GD); |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 2093 | } |
Anders Carlsson | 2fce216 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 2094 | |
| 2095 | // Add the method to the vtable. |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 2096 | VtableComponents.push_back(Method); |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Anders Carlsson | 2fce216 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 2099 | |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 2100 | ThisAdjustments.clear(); |
Anders Carlsson | ea35722 | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 2101 | BaseReturnTypes.clear(); |
Anders Carlsson | b73ba39 | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 2102 | |
Anders Carlsson | adfa267 | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 2103 | Methods.clear(); |
Anders Carlsson | adfa267 | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2106 | void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) { |
| 2107 | |
| 2108 | // Itanium C++ ABI 2.5.2: |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 2109 | // The order of the virtual function pointers in a virtual table is the |
| 2110 | // order of declaration of the corresponding member functions in the class. |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2111 | // |
Anders Carlsson | 45147d0 | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 2112 | // There is an entry for any virtual function declared in a class, |
| 2113 | // whether it is a new function or overrides a base class function, |
| 2114 | // unless it overrides a function from the primary base, and conversion |
| 2115 | // between their return types does not require an adjustment. |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2116 | |
| 2117 | int64_t CurrentIndex = 0; |
| 2118 | |
| 2119 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 2120 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 2121 | |
| 2122 | if (PrimaryBase) { |
Anders Carlsson | 0121fbd | 2009-11-30 19:43:26 +0000 | [diff] [blame] | 2123 | assert(PrimaryBase->isDefinition() && |
| 2124 | "Should have the definition decl of the primary base!"); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2125 | |
| 2126 | // Since the record decl shares its vtable pointer with the primary base |
| 2127 | // we need to start counting at the end of the primary base's vtable. |
| 2128 | CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase); |
| 2129 | } |
Eli Friedman | ea5ae31 | 2009-12-15 03:31:17 +0000 | [diff] [blame] | 2130 | |
| 2131 | // Collect all the primary bases, so we can check whether methods override |
| 2132 | // a method from the base. |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 2133 | VtableBuilder::PrimaryBasesSetTy PrimaryBases; |
Eli Friedman | ea5ae31 | 2009-12-15 03:31:17 +0000 | [diff] [blame] | 2134 | for (ASTRecordLayout::primary_base_info_iterator |
| 2135 | I = Layout.primary_base_begin(), E = Layout.primary_base_end(); |
| 2136 | I != E; ++I) |
| 2137 | PrimaryBases.insert((*I).getBase()); |
| 2138 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2139 | const CXXDestructorDecl *ImplicitVirtualDtor = 0; |
| 2140 | |
| 2141 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 2142 | e = RD->method_end(); i != e; ++i) { |
| 2143 | const CXXMethodDecl *MD = *i; |
| 2144 | |
| 2145 | // We only want virtual methods. |
| 2146 | if (!MD->isVirtual()) |
| 2147 | continue; |
| 2148 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2149 | // Check if this method overrides a method in the primary base. |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 2150 | if (const CXXMethodDecl *OverriddenMD = |
| 2151 | OverridesMethodInPrimaryBase(MD, PrimaryBases)) { |
| 2152 | // Check if converting from the return type of the method to the |
| 2153 | // return type of the overridden method requires conversion. |
Anders Carlsson | 104f45c | 2010-02-12 17:37:14 +0000 | [diff] [blame] | 2154 | if (!ReturnTypeConversionRequiresAdjustment(MD, OverriddenMD)) { |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 2155 | // This index is shared between the index in the vtable of the primary |
| 2156 | // base class. |
| 2157 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 2158 | const CXXDestructorDecl *OverriddenDD = |
| 2159 | cast<CXXDestructorDecl>(OverriddenMD); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2160 | |
Anders Carlsson | 57071e2 | 2010-02-12 05:25:12 +0000 | [diff] [blame] | 2161 | // Add both the complete and deleting entries. |
| 2162 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = |
| 2163 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete)); |
| 2164 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = |
| 2165 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting)); |
| 2166 | } else { |
| 2167 | MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD); |
| 2168 | } |
| 2169 | |
| 2170 | // We don't need to add an entry for this method. |
Anders Carlsson | c1eec89 | 2010-02-12 18:14:46 +0000 | [diff] [blame] | 2171 | continue; |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2172 | } |
| 2173 | } |
| 2174 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2175 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 2176 | if (MD->isImplicit()) { |
| 2177 | assert(!ImplicitVirtualDtor && |
| 2178 | "Did already see an implicit virtual dtor!"); |
| 2179 | ImplicitVirtualDtor = DD; |
| 2180 | continue; |
| 2181 | } |
| 2182 | |
| 2183 | // Add the complete dtor. |
| 2184 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++; |
| 2185 | |
| 2186 | // Add the deleting dtor. |
| 2187 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++; |
| 2188 | } else { |
| 2189 | // Add the entry. |
| 2190 | MethodVtableIndices[MD] = CurrentIndex++; |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | if (ImplicitVirtualDtor) { |
| 2195 | // Itanium C++ ABI 2.5.2: |
| 2196 | // If a class has an implicitly-defined virtual destructor, |
| 2197 | // its entries come after the declared virtual function pointers. |
| 2198 | |
| 2199 | // Add the complete dtor. |
| 2200 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] = |
| 2201 | CurrentIndex++; |
| 2202 | |
| 2203 | // Add the deleting dtor. |
| 2204 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] = |
| 2205 | CurrentIndex++; |
| 2206 | } |
| 2207 | |
| 2208 | NumVirtualFunctionPointers[RD] = CurrentIndex; |
| 2209 | } |
| 2210 | |
| 2211 | uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) { |
| 2212 | llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 2213 | NumVirtualFunctionPointers.find(RD); |
| 2214 | if (I != NumVirtualFunctionPointers.end()) |
| 2215 | return I->second; |
| 2216 | |
| 2217 | ComputeMethodVtableIndices(RD); |
| 2218 | |
| 2219 | I = NumVirtualFunctionPointers.find(RD); |
| 2220 | assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!"); |
| 2221 | return I->second; |
| 2222 | } |
| 2223 | |
| 2224 | uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2225 | MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2226 | if (I != MethodVtableIndices.end()) |
| 2227 | return I->second; |
| 2228 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2229 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 2230 | |
| 2231 | ComputeMethodVtableIndices(RD); |
| 2232 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 2233 | I = MethodVtableIndices.find(GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2234 | assert(I != MethodVtableIndices.end() && "Did not find index!"); |
| 2235 | return I->second; |
| 2236 | } |
| 2237 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2238 | CGVtableInfo::AdjustmentVectorTy* |
| 2239 | CGVtableInfo::getAdjustments(GlobalDecl GD) { |
| 2240 | SavedAdjustmentsTy::iterator I = SavedAdjustments.find(GD); |
| 2241 | if (I != SavedAdjustments.end()) |
| 2242 | return &I->second; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2243 | |
| 2244 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(GD.getDecl()->getDeclContext()); |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2245 | if (!SavedAdjustmentRecords.insert(RD).second) |
| 2246 | return 0; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2247 | |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 2248 | AddressPointsMapTy AddressPoints; |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2249 | OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2250 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
| 2251 | b.GenerateVtableForBase(RD); |
| 2252 | b.GenerateVtableForVBases(RD); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2253 | |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2254 | for (OldVtableBuilder::SavedAdjustmentsVectorTy::iterator |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2255 | i = b.getSavedAdjustments().begin(), |
| 2256 | e = b.getSavedAdjustments().end(); i != e; i++) |
| 2257 | SavedAdjustments[i->first].push_back(i->second); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2258 | |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2259 | I = SavedAdjustments.find(GD); |
| 2260 | if (I != SavedAdjustments.end()) |
| 2261 | return &I->second; |
| 2262 | |
| 2263 | return 0; |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2266 | int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD, |
| 2267 | const CXXRecordDecl *VBase) { |
| 2268 | ClassPairTy ClassPair(RD, VBase); |
| 2269 | |
| 2270 | VirtualBaseClassIndiciesTy::iterator I = |
| 2271 | VirtualBaseClassIndicies.find(ClassPair); |
| 2272 | if (I != VirtualBaseClassIndicies.end()) |
| 2273 | return I->second; |
| 2274 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2275 | // FIXME: This seems expensive. Can we do a partial job to get |
| 2276 | // just this data. |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 2277 | AddressPointsMapTy AddressPoints; |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2278 | OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 2279 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2280 | b.GenerateVtableForBase(RD); |
| 2281 | b.GenerateVtableForVBases(RD); |
| 2282 | |
| 2283 | for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 2284 | b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) { |
| 2285 | // Insert all types. |
| 2286 | ClassPairTy ClassPair(RD, I->first); |
| 2287 | |
| 2288 | VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second)); |
| 2289 | } |
| 2290 | |
| 2291 | I = VirtualBaseClassIndicies.find(ClassPair); |
Anders Carlsson | 7dbf47a | 2010-02-13 20:11:51 +0000 | [diff] [blame] | 2292 | // FIXME: The assertion below assertion currently fails with the old vtable |
| 2293 | /// layout code if there is a non-virtual thunk adjustment in a vtable. |
| 2294 | // Once the new layout is in place, this return should be removed. |
| 2295 | if (I == VirtualBaseClassIndicies.end()) |
| 2296 | return 0; |
| 2297 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2298 | assert(I != VirtualBaseClassIndicies.end() && "Did not find index!"); |
| 2299 | |
| 2300 | return I->second; |
| 2301 | } |
| 2302 | |
Anders Carlsson | 9ac95b9 | 2009-12-05 21:03:56 +0000 | [diff] [blame] | 2303 | uint64_t CGVtableInfo::getVtableAddressPoint(const CXXRecordDecl *RD) { |
| 2304 | uint64_t AddressPoint = |
Anders Carlsson | 21431c5 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 2305 | (*(*(CGM.getVtableInfo().AddressPoints[RD]))[RD])[std::make_pair(RD, 0)]; |
Anders Carlsson | 9ac95b9 | 2009-12-05 21:03:56 +0000 | [diff] [blame] | 2306 | |
| 2307 | return AddressPoint; |
| 2308 | } |
| 2309 | |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 2310 | llvm::GlobalVariable * |
Anders Carlsson | 3527225 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 2311 | CGVtableInfo::GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 2312 | bool GenerateDefinition, |
Anders Carlsson | 3527225 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 2313 | const CXXRecordDecl *LayoutClass, |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 2314 | const CXXRecordDecl *RD, uint64_t Offset, |
| 2315 | AddressPointsMapTy& AddressPoints) { |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 2316 | if (GenerateDefinition && CGM.getLangOptions().DumpVtableLayouts) { |
Anders Carlsson | 60db0ee | 2010-02-13 21:07:32 +0000 | [diff] [blame] | 2317 | VtableBuilder Builder(*this, RD); |
Anders Carlsson | 0c0eeb2 | 2010-02-13 02:02:03 +0000 | [diff] [blame] | 2318 | |
| 2319 | Builder.dumpLayout(llvm::errs()); |
| 2320 | } |
| 2321 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2322 | llvm::SmallString<256> OutName; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 2323 | if (LayoutClass != RD) |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 2324 | CGM.getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset / 8, |
| 2325 | RD, OutName); |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 2326 | else |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 2327 | CGM.getMangleContext().mangleCXXVtable(RD, OutName); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 2328 | llvm::StringRef Name = OutName.str(); |
Benjamin Kramer | 7a9474e | 2009-10-11 22:57:54 +0000 | [diff] [blame] | 2329 | |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 2330 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
Anders Carlsson | 21431c5 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 2331 | if (GV == 0 || CGM.getVtableInfo().AddressPoints[LayoutClass] == 0 || |
| 2332 | GV->isDeclaration()) { |
Anders Carlsson | 824d7ea | 2010-02-11 08:02:13 +0000 | [diff] [blame] | 2333 | OldVtableBuilder b(RD, LayoutClass, Offset, CGM, GenerateDefinition, |
| 2334 | AddressPoints); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 2335 | |
| 2336 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
| 2337 | // First comes the vtables for all the non-virtual bases... |
Mike Stump | 8cc4f10 | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 2338 | b.GenerateVtableForBase(RD, Offset); |
Eli Friedman | 152b5b1 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 2339 | |
| 2340 | // then the vtables for all the virtual bases. |
| 2341 | b.GenerateVtableForVBases(RD, Offset); |
| 2342 | |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 2343 | llvm::Constant *Init = 0; |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 2344 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 2345 | llvm::ArrayType *ArrayType = |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 2346 | llvm::ArrayType::get(Int8PtrTy, b.getVtableComponents().size()); |
Anders Carlsson | e40477c | 2009-12-05 21:09:05 +0000 | [diff] [blame] | 2347 | |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 2348 | if (GenerateDefinition) |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 2349 | Init = llvm::ConstantArray::get(ArrayType, &b.getVtableComponents()[0], |
| 2350 | b.getVtableComponents().size()); |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 2351 | |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 2352 | llvm::GlobalVariable *OGV = GV; |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 2353 | |
| 2354 | GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType, |
| 2355 | /*isConstant=*/true, Linkage, Init, Name); |
Anders Carlsson | c3a46ef | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 2356 | CGM.setGlobalVisibility(GV, RD); |
| 2357 | |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 2358 | if (OGV) { |
| 2359 | GV->takeName(OGV); |
Anders Carlsson | 152d4dc | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 2360 | llvm::Constant *NewPtr = |
| 2361 | llvm::ConstantExpr::getBitCast(GV, OGV->getType()); |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 2362 | OGV->replaceAllUsesWith(NewPtr); |
| 2363 | OGV->eraseFromParent(); |
| 2364 | } |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 2365 | } |
Anders Carlsson | bb27d86 | 2009-12-05 21:28:12 +0000 | [diff] [blame] | 2366 | |
| 2367 | return GV; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 2368 | } |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 2369 | |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 2370 | void CGVtableInfo::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage, |
| 2371 | const CXXRecordDecl *RD) { |
Anders Carlsson | d58987c | 2009-12-07 07:59:52 +0000 | [diff] [blame] | 2372 | llvm::GlobalVariable *&Vtable = Vtables[RD]; |
| 2373 | if (Vtable) { |
| 2374 | assert(Vtable->getInitializer() && "Vtable doesn't have a definition!"); |
| 2375 | return; |
| 2376 | } |
| 2377 | |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 2378 | AddressPointsMapTy AddressPoints; |
| 2379 | Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0, |
| 2380 | AddressPoints); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 2381 | GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 2384 | llvm::GlobalVariable *CGVtableInfo::getVtable(const CXXRecordDecl *RD) { |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 2385 | llvm::GlobalVariable *Vtable = Vtables.lookup(RD); |
Anders Carlsson | 8c2d36f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 2386 | |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 2387 | if (!Vtable) { |
| 2388 | AddressPointsMapTy AddressPoints; |
Anders Carlsson | 5794c97 | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 2389 | Vtable = GenerateVtable(llvm::GlobalValue::ExternalLinkage, |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 2390 | /*GenerateDefinition=*/false, RD, RD, 0, |
| 2391 | AddressPoints); |
| 2392 | } |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 2393 | |
Anders Carlsson | 224c312 | 2009-12-05 22:42:54 +0000 | [diff] [blame] | 2394 | return Vtable; |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 2395 | } |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 2396 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2397 | void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) { |
| 2398 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 2399 | const CXXRecordDecl *RD = MD->getParent(); |
| 2400 | |
Anders Carlsson | 224c312 | 2009-12-05 22:42:54 +0000 | [diff] [blame] | 2401 | // If the class doesn't have a vtable we don't need to emit one. |
| 2402 | if (!RD->isDynamicClass()) |
| 2403 | return; |
| 2404 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2405 | // Get the key function. |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 2406 | const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD); |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2407 | |
Anders Carlsson | 224c312 | 2009-12-05 22:42:54 +0000 | [diff] [blame] | 2408 | if (KeyFunction) { |
| 2409 | // We don't have the right key function. |
| 2410 | if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl()) |
| 2411 | return; |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2414 | // Emit the data. |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 2415 | GenerateClassData(CGM.getVtableLinkage(RD), RD); |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2416 | |
| 2417 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 2418 | e = RD->method_end(); i != e; ++i) { |
Eli Friedman | b455f0e | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 2419 | if ((*i)->isVirtual() && ((*i)->hasInlineBody() || (*i)->isImplicit())) { |
Eli Friedman | 72649ed | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 2420 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(*i)) { |
| 2421 | CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Complete)); |
| 2422 | CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Deleting)); |
| 2423 | } else { |
| 2424 | CGM.BuildThunksForVirtual(GlobalDecl(*i)); |
| 2425 | } |
| 2426 | } |
| 2427 | } |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |