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