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