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" |
Zhongxing Xu | 7fe26ac | 2009-11-13 05:46:16 +0000 | [diff] [blame] | 19 | #include <cstdio> |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Anders Carlsson | 27f69d0 | 2009-11-27 22:21:51 +0000 | [diff] [blame] | 24 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 25 | class VtableBuilder { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 26 | public: |
| 27 | /// Index_t - Vtable index type. |
| 28 | typedef uint64_t Index_t; |
| 29 | private: |
| 30 | std::vector<llvm::Constant *> &methods; |
| 31 | std::vector<llvm::Constant *> submethods; |
| 32 | llvm::Type *Ptr8Ty; |
| 33 | /// Class - The most derived class that this vtable is being built for. |
| 34 | const CXXRecordDecl *Class; |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 35 | /// LayoutClass - The most derived class used for virtual base layout |
| 36 | /// information. |
| 37 | const CXXRecordDecl *LayoutClass; |
Mike Stump | 4cde626 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 38 | /// LayoutOffset - The offset for Class in LayoutClass. |
| 39 | uint64_t LayoutOffset; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 40 | /// BLayout - Layout for the most derived class that this vtable is being |
| 41 | /// built for. |
| 42 | const ASTRecordLayout &BLayout; |
| 43 | llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary; |
| 44 | llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase; |
| 45 | llvm::Constant *rtti; |
| 46 | llvm::LLVMContext &VMContext; |
| 47 | CodeGenModule &CGM; // Per-module state. |
| 48 | /// Index - Maps a method decl into a vtable index. Useful for virtual |
| 49 | /// dispatch codegen. |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 50 | llvm::DenseMap<GlobalDecl, Index_t> Index; |
| 51 | llvm::DenseMap<GlobalDecl, Index_t> VCall; |
| 52 | llvm::DenseMap<GlobalDecl, Index_t> VCallOffset; |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 53 | // This is the offset to the nearest virtual base |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 54 | llvm::DenseMap<GlobalDecl, Index_t> NonVirtualOffset; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 55 | llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex; |
Mike Stump | 94aff93 | 2009-10-27 23:46:47 +0000 | [diff] [blame] | 56 | |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 57 | /// PureVirtualFunction - Points to __cxa_pure_virtual. |
| 58 | llvm::Constant *PureVirtualFn; |
| 59 | |
Anders Carlsson | db13151 | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 60 | /// Thunk - Represents a single thunk. |
| 61 | struct Thunk { |
| 62 | Thunk() |
| 63 | : Index(0) { } |
| 64 | |
| 65 | Thunk(uint64_t Index, const ThunkAdjustment &Adjustment) |
| 66 | : Index(Index), Adjustment(Adjustment) { } |
| 67 | |
| 68 | /// Index - The index in the vtable. |
| 69 | uint64_t Index; |
| 70 | |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 71 | /// Adjustment - The thunk adjustment. |
Anders Carlsson | db13151 | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 72 | ThunkAdjustment Adjustment; |
| 73 | }; |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 74 | |
| 75 | /// Thunks - The thunks in a vtable. |
Anders Carlsson | db13151 | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 76 | typedef llvm::DenseMap<GlobalDecl, Thunk> ThunksMapTy; |
| 77 | ThunksMapTy Thunks; |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 78 | |
| 79 | /// CovariantThunk - Represents a single covariant thunk. |
| 80 | struct CovariantThunk { |
Anders Carlsson | 5f96bc1 | 2009-12-03 02:16:14 +0000 | [diff] [blame^] | 81 | CovariantThunk(GlobalDecl GD, const ThunkAdjustment &ReturnAdjustment, |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 82 | CanQualType ReturnType) |
Anders Carlsson | 5f96bc1 | 2009-12-03 02:16:14 +0000 | [diff] [blame^] | 83 | : GD(GD), ReturnAdjustment(ReturnAdjustment), |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 84 | ReturnType(ReturnType) { } |
Anders Carlsson | 5f96bc1 | 2009-12-03 02:16:14 +0000 | [diff] [blame^] | 85 | |
Anders Carlsson | bdd8e38 | 2009-12-03 02:03:29 +0000 | [diff] [blame] | 86 | GlobalDecl GD; |
| 87 | |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 88 | /// ReturnAdjustment - The covariant thunk return adjustment. |
| 89 | ThunkAdjustment ReturnAdjustment; |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 90 | |
| 91 | /// ReturnType - The return type of the function. |
| 92 | CanQualType ReturnType; |
| 93 | }; |
Anders Carlsson | db13151 | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 94 | |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 95 | /// CovariantThunks - The covariant thunks in a vtable. |
Anders Carlsson | d6f7af5 | 2009-12-03 02:12:03 +0000 | [diff] [blame] | 96 | typedef llvm::DenseMap<uint64_t, CovariantThunk> CovariantThunksMapTy; |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 97 | CovariantThunksMapTy CovariantThunks; |
| 98 | |
| 99 | /// PureVirtualMethods - Pure virtual methods. |
| 100 | typedef llvm::DenseSet<GlobalDecl> PureVirtualMethodsSetTy; |
| 101 | PureVirtualMethodsSetTy PureVirtualMethods; |
| 102 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 103 | std::vector<Index_t> VCalls; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 104 | |
| 105 | typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 106 | // subAddressPoints - Used to hold the AddressPoints (offsets) into the built |
| 107 | // vtable for use in computing the initializers for the VTT. |
| 108 | llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 109 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 110 | typedef CXXRecordDecl::method_iterator method_iter; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 111 | const bool Extern; |
| 112 | const uint32_t LLVMPointerWidth; |
| 113 | Index_t extra; |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 114 | typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 115 | static llvm::DenseMap<CtorVtable_t, int64_t>& |
| 116 | AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l, |
| 117 | const CXXRecordDecl *c) { |
| 118 | CodeGenModule::AddrMap_t *&oref = cgm.AddressPoints[l]; |
| 119 | if (oref == 0) |
| 120 | oref = new CodeGenModule::AddrMap_t; |
| 121 | |
| 122 | llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c]; |
| 123 | if (ref == 0) |
| 124 | ref = new llvm::DenseMap<CtorVtable_t, int64_t>; |
| 125 | return *ref; |
| 126 | } |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 127 | |
| 128 | /// getPureVirtualFn - Return the __cxa_pure_virtual function. |
| 129 | llvm::Constant* getPureVirtualFn() { |
| 130 | if (!PureVirtualFn) { |
| 131 | const llvm::FunctionType *Ty = |
| 132 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 133 | /*isVarArg=*/false); |
| 134 | PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual")); |
| 135 | } |
| 136 | |
| 137 | return PureVirtualFn; |
| 138 | } |
| 139 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 140 | public: |
Mike Stump | 4cde626 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 141 | VtableBuilder(std::vector<llvm::Constant *> &meth, const CXXRecordDecl *c, |
| 142 | const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm) |
| 143 | : methods(meth), Class(c), LayoutClass(l), LayoutOffset(lo), |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 144 | BLayout(cgm.getContext().getASTRecordLayout(l)), |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 145 | rtti(cgm.GenerateRTTIRef(c)), VMContext(cgm.getModule().getContext()), |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 146 | CGM(cgm), PureVirtualFn(0),subAddressPoints(AllocAddressPoint(cgm, l, c)), |
Mike Stump | 6be2b17 | 2009-11-19 00:49:05 +0000 | [diff] [blame] | 147 | Extern(!l->isInAnonymousNamespace()), |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 148 | LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 149 | Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
| 150 | } |
| 151 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 152 | llvm::DenseMap<GlobalDecl, Index_t> &getIndex() { return Index; } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 153 | llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex() |
| 154 | { return VBIndex; } |
| 155 | |
| 156 | llvm::Constant *wrap(Index_t i) { |
| 157 | llvm::Constant *m; |
| 158 | m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i); |
| 159 | return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); |
| 160 | } |
| 161 | |
| 162 | llvm::Constant *wrap(llvm::Constant *m) { |
| 163 | return llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
| 164 | } |
| 165 | |
Mike Stump | 7933628 | 2009-12-02 19:50:41 +0000 | [diff] [blame] | 166 | #define D1(x) |
| 167 | //#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0) |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 168 | |
| 169 | void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset, |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 170 | bool updateVBIndex, Index_t current_vbindex) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 171 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 172 | e = RD->bases_end(); i != e; ++i) { |
| 173 | const CXXRecordDecl *Base = |
| 174 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 175 | Index_t next_vbindex = current_vbindex; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 176 | if (i->isVirtual() && !SeenVBase.count(Base)) { |
| 177 | SeenVBase.insert(Base); |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 178 | if (updateVBIndex) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 179 | next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8) |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 180 | - 3*LLVMPointerWidth/8); |
| 181 | VBIndex[Base] = next_vbindex; |
| 182 | } |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 183 | int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8; |
| 184 | VCalls.push_back((0?700:0) + BaseOffset); |
| 185 | D1(printf(" vbase for %s at %d delta %d most derived %s\n", |
| 186 | Base->getNameAsCString(), |
| 187 | (int)-VCalls.size()-3, (int)BaseOffset, |
| 188 | Class->getNameAsCString())); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 189 | } |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 190 | // We also record offsets for non-virtual bases to closest enclosing |
| 191 | // virtual base. We do this so that we don't have to search |
| 192 | // for the nearst virtual base class when generating thunks. |
| 193 | if (updateVBIndex && VBIndex.count(Base) == 0) |
| 194 | VBIndex[Base] = next_vbindex; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 195 | GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | void StartNewTable() { |
| 200 | SeenVBase.clear(); |
| 201 | } |
| 202 | |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 203 | Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B, |
| 204 | Index_t Offset = 0) { |
| 205 | |
| 206 | if (B == D) |
| 207 | return Offset; |
| 208 | |
| 209 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D); |
| 210 | for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(), |
| 211 | e = D->bases_end(); i != e; ++i) { |
| 212 | const CXXRecordDecl *Base = |
| 213 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 214 | int64_t BaseOffset = 0; |
| 215 | if (!i->isVirtual()) |
| 216 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 217 | int64_t o = getNVOffset_1(Base, B, BaseOffset); |
| 218 | if (o >= 0) |
| 219 | return o; |
| 220 | } |
| 221 | |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | /// getNVOffset - Returns the non-virtual offset for the given (B) base of the |
| 226 | /// derived class D. |
| 227 | Index_t getNVOffset(QualType qB, QualType qD) { |
Mike Stump | 9c21289 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 228 | qD = qD->getPointeeType(); |
| 229 | qB = qB->getPointeeType(); |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 230 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 231 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
| 232 | int64_t o = getNVOffset_1(D, B); |
| 233 | if (o >= 0) |
| 234 | return o; |
| 235 | |
| 236 | assert(false && "FIXME: non-virtual base not found"); |
| 237 | return 0; |
| 238 | } |
| 239 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 240 | /// getVbaseOffset - Returns the index into the vtable for the virtual base |
| 241 | /// offset for the given (B) virtual base of the derived class D. |
| 242 | Index_t getVbaseOffset(QualType qB, QualType qD) { |
Mike Stump | 9c21289 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 243 | qD = qD->getPointeeType(); |
| 244 | qB = qB->getPointeeType(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 245 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 246 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
| 247 | if (D != Class) |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 248 | return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 249 | llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i; |
| 250 | i = VBIndex.find(B); |
| 251 | if (i != VBIndex.end()) |
| 252 | return i->second; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 253 | |
Mike Stump | ab28c13 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 254 | assert(false && "FIXME: Base not found"); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 258 | bool OverrideMethod(GlobalDecl GD, llvm::Constant *m, |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 259 | bool MorallyVirtual, Index_t OverrideOffset, |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 260 | Index_t Offset, int64_t CurrentVBaseOffset); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 261 | |
| 262 | void InstallThunks() { |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 263 | for (CovariantThunksMapTy::const_iterator i = CovariantThunks.begin(), |
| 264 | e = CovariantThunks.end(); i != e; ++i) { |
Anders Carlsson | d6f7af5 | 2009-12-03 02:12:03 +0000 | [diff] [blame] | 265 | GlobalDecl GD = i->second.GD; |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 266 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 267 | if (MD->isPure()) |
| 268 | continue; |
| 269 | |
Anders Carlsson | 5f96bc1 | 2009-12-03 02:16:14 +0000 | [diff] [blame^] | 270 | uint64_t Index = i->first; |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 271 | const CovariantThunk &Thunk = i->second; |
Anders Carlsson | 5f96bc1 | 2009-12-03 02:16:14 +0000 | [diff] [blame^] | 272 | assert(Index == VtableBuilder::Index[GD] && "Thunk index mismatch!"); |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 273 | |
| 274 | // Check if there is an adjustment for the 'this' pointer. |
| 275 | ThunkAdjustment ThisAdjustment; |
| 276 | ThunksMapTy::iterator i = Thunks.find(GD); |
| 277 | if (i != Thunks.end()) { |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 278 | ThisAdjustment = i->second.Adjustment; |
| 279 | |
| 280 | Thunks.erase(i); |
| 281 | } |
| 282 | |
| 283 | CovariantThunkAdjustment Adjustment(ThisAdjustment, |
| 284 | Thunk.ReturnAdjustment); |
Anders Carlsson | 5f96bc1 | 2009-12-03 02:16:14 +0000 | [diff] [blame^] | 285 | submethods[Index] = CGM.BuildCovariantThunk(MD, Extern, Adjustment); |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 286 | } |
| 287 | CovariantThunks.clear(); |
Anders Carlsson | bdd8e38 | 2009-12-03 02:03:29 +0000 | [diff] [blame] | 288 | |
Anders Carlsson | db13151 | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 289 | for (ThunksMapTy::const_iterator i = Thunks.begin(), e = Thunks.end(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 290 | i != e; ++i) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 291 | GlobalDecl GD = i->first; |
| 292 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Anders Carlsson | db13151 | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 293 | assert(!MD->isPure() && "Can't thunk pure virtual methods!"); |
| 294 | |
| 295 | const Thunk& Thunk = i->second; |
| 296 | assert(Thunk.Index == Index[GD] && "Thunk index mismatch!"); |
| 297 | |
| 298 | submethods[Thunk.Index] = CGM.BuildThunk(MD, Extern, Thunk.Adjustment); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 299 | } |
| 300 | Thunks.clear(); |
Anders Carlsson | db13151 | 2009-11-26 03:25:13 +0000 | [diff] [blame] | 301 | |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 302 | for (PureVirtualMethodsSetTy::iterator i = PureVirtualMethods.begin(), |
| 303 | e = PureVirtualMethods.end(); i != e; ++i) { |
| 304 | GlobalDecl GD = *i; |
Anders Carlsson | 6d4ccb7 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 305 | submethods[Index[GD]] = getPureVirtualFn(); |
Mike Stump | 94aff93 | 2009-10-27 23:46:47 +0000 | [diff] [blame] | 306 | } |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 307 | PureVirtualMethods.clear(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 310 | llvm::Constant *WrapAddrOf(GlobalDecl GD) { |
| 311 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 312 | |
Mike Stump | 1ae3178 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 313 | if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 314 | return wrap(CGM.GetAddrOfCXXDestructor(Dtor, GD.getDtorType())); |
Mike Stump | 1ae3178 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 315 | |
Anders Carlsson | ecf282b | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 316 | const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD); |
Mike Stump | 1ae3178 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 317 | |
| 318 | return wrap(CGM.GetAddrOfFunction(MD, Ty)); |
| 319 | } |
| 320 | |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 321 | void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset, |
| 322 | int64_t CurrentVBaseOffset) { |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 323 | for (Path_t::reverse_iterator i = Path->rbegin(), |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 324 | e = Path->rend(); i != e; ++i) { |
| 325 | const CXXRecordDecl *RD = i->first; |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 326 | int64_t OverrideOffset = i->second; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 327 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 328 | ++mi) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 329 | const CXXMethodDecl *MD = *mi; |
| 330 | |
| 331 | if (!MD->isVirtual()) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 332 | continue; |
| 333 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 334 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 335 | // Override both the complete and the deleting destructor. |
| 336 | GlobalDecl CompDtor(DD, Dtor_Complete); |
| 337 | OverrideMethod(CompDtor, WrapAddrOf(CompDtor), MorallyVirtual, |
| 338 | OverrideOffset, Offset, CurrentVBaseOffset); |
| 339 | |
| 340 | GlobalDecl DeletingDtor(DD, Dtor_Deleting); |
| 341 | OverrideMethod(DeletingDtor, WrapAddrOf(DeletingDtor), MorallyVirtual, |
| 342 | OverrideOffset, Offset, CurrentVBaseOffset); |
| 343 | } else { |
| 344 | OverrideMethod(MD, WrapAddrOf(MD), MorallyVirtual, OverrideOffset, |
| 345 | Offset, CurrentVBaseOffset); |
| 346 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 351 | void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 352 | int64_t CurrentVBaseOffset) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 353 | llvm::Constant *m = WrapAddrOf(GD); |
Mike Stump | 1ae3178 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 354 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 355 | // If we can find a previously allocated slot for this, reuse it. |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 356 | if (OverrideMethod(GD, m, MorallyVirtual, Offset, Offset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 357 | CurrentVBaseOffset)) |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 358 | return; |
| 359 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 360 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 361 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 362 | // else allocate a new slot. |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 363 | Index[GD] = submethods.size(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 364 | submethods.push_back(m); |
Mike Stump | e99cc45 | 2009-11-13 23:45:53 +0000 | [diff] [blame] | 365 | D1(printf(" vfn for %s at %d\n", MD->getNameAsString().c_str(), |
| 366 | (int)Index[GD])); |
Mike Stump | 7809e0d | 2009-10-28 00:35:46 +0000 | [diff] [blame] | 367 | if (MD->isPure()) |
Anders Carlsson | 5dd730a | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 368 | PureVirtualMethods.insert(GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 369 | if (MorallyVirtual) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 370 | VCallOffset[GD] = Offset/8; |
| 371 | Index_t &idx = VCall[GD]; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 372 | // Allocate the first one, after that, we reuse the previous one. |
| 373 | if (idx == 0) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 374 | NonVirtualOffset[GD] = CurrentVBaseOffset/8 - Offset/8; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 375 | idx = VCalls.size()+1; |
| 376 | VCalls.push_back(0); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 377 | D1(printf(" vcall for %s at %d with delta %d\n", |
Mike Stump | e99cc45 | 2009-11-13 23:45:53 +0000 | [diff] [blame] | 378 | MD->getNameAsString().c_str(), (int)-VCalls.size()-3, 0)); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 384 | Index_t Offset, int64_t CurrentVBaseOffset) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 385 | 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] | 386 | ++mi) { |
| 387 | const CXXMethodDecl *MD = *mi; |
| 388 | if (!MD->isVirtual()) |
| 389 | continue; |
| 390 | |
| 391 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 392 | // For destructors, add both the complete and the deleting destructor |
| 393 | // to the vtable. |
| 394 | AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 395 | CurrentVBaseOffset); |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 396 | AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset, |
| 397 | CurrentVBaseOffset); |
| 398 | } else |
| 399 | AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 400 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, |
| 404 | const CXXRecordDecl *PrimaryBase, |
| 405 | bool PrimaryBaseWasVirtual, bool MorallyVirtual, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 406 | int64_t Offset, int64_t CurrentVBaseOffset, |
| 407 | Path_t *Path) { |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 408 | Path->push_back(std::make_pair(RD, Offset)); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 409 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 410 | e = RD->bases_end(); i != e; ++i) { |
| 411 | if (i->isVirtual()) |
| 412 | continue; |
| 413 | const CXXRecordDecl *Base = |
| 414 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 415 | if (Base != PrimaryBase || PrimaryBaseWasVirtual) { |
| 416 | uint64_t o = Offset + Layout.getBaseClassOffset(Base); |
| 417 | StartNewTable(); |
Mike Stump | 4cde626 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 418 | GenerateVtableForBase(Base, o, MorallyVirtual, false, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 419 | CurrentVBaseOffset, Path); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 422 | Path->pop_back(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 425 | // #define D(X) do { X; } while (0) |
| 426 | #define D(X) |
| 427 | |
| 428 | void insertVCalls(int InsertionPoint) { |
| 429 | llvm::Constant *e = 0; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 430 | D1(printf("============= combining vbase/vcall\n")); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 431 | D(VCalls.insert(VCalls.begin(), 673)); |
| 432 | D(VCalls.push_back(672)); |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 433 | methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 434 | // The vcalls come first... |
| 435 | for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(), |
| 436 | e = VCalls.rend(); |
| 437 | i != e; ++i) |
| 438 | methods[InsertionPoint++] = wrap((0?600:0) + *i); |
| 439 | VCalls.clear(); |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 440 | VCall.clear(); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 443 | void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset, |
| 444 | Index_t AddressPoint) { |
| 445 | D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n", |
| 446 | RD->getNameAsCString(), Class->getNameAsCString(), |
| 447 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 448 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 449 | |
| 450 | // Now also add the address point for all our primary bases. |
| 451 | while (1) { |
| 452 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 453 | RD = Layout.getPrimaryBase(); |
| 454 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 455 | // FIXME: Double check this. |
| 456 | if (RD == 0) |
| 457 | break; |
| 458 | if (PrimaryBaseWasVirtual && |
| 459 | BLayout.getVBaseClassOffset(RD) != Offset) |
| 460 | break; |
| 461 | D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n", |
| 462 | RD->getNameAsCString(), Class->getNameAsCString(), |
| 463 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 464 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 469 | Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, |
| 470 | const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual, |
| 471 | bool MorallyVirtual, int64_t Offset, bool ForVirtualBase, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 472 | int64_t CurrentVBaseOffset, |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 473 | Path_t *Path) { |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 474 | bool alloc = false; |
| 475 | if (Path == 0) { |
| 476 | alloc = true; |
| 477 | Path = new Path_t; |
| 478 | } |
| 479 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 480 | StartNewTable(); |
| 481 | extra = 0; |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 482 | bool DeferVCalls = MorallyVirtual || ForVirtualBase; |
| 483 | int VCallInsertionPoint = methods.size(); |
| 484 | if (!DeferVCalls) { |
| 485 | insertVCalls(VCallInsertionPoint); |
Mike Stump | 3425b97 | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 486 | } else |
| 487 | // FIXME: just for extra, or for all uses of VCalls.size post this? |
| 488 | extra = -VCalls.size(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 489 | |
Mike Stump | 4cde626 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 490 | methods.push_back(wrap(-((Offset-LayoutOffset)/8))); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 491 | methods.push_back(rtti); |
| 492 | Index_t AddressPoint = methods.size(); |
| 493 | |
| 494 | InstallThunks(); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 495 | D1(printf("============= combining methods\n")); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 496 | methods.insert(methods.end(), submethods.begin(), submethods.end()); |
| 497 | submethods.clear(); |
| 498 | |
| 499 | // and then the non-virtual bases. |
| 500 | NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 501 | MorallyVirtual, Offset, CurrentVBaseOffset, Path); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 502 | |
| 503 | if (ForVirtualBase) { |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 504 | // FIXME: We're adding to VCalls in callers, we need to do the overrides |
| 505 | // in the inner part, so that we know the complete set of vcalls during |
| 506 | // the build and don't have to insert into methods. Saving out the |
| 507 | // AddressPoint here, would need to be fixed, if we didn't do that. Also |
| 508 | // retroactively adding vcalls for overrides later wind up in the wrong |
| 509 | // place, the vcall slot has to be alloted during the walk of the base |
| 510 | // when the function is first introduces. |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 511 | AddressPoint += VCalls.size(); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 512 | insertVCalls(VCallInsertionPoint); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Mike Stump | 65d0e28 | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 515 | AddAddressPoints(RD, Offset, AddressPoint); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 516 | |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 517 | if (alloc) { |
| 518 | delete Path; |
| 519 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 520 | return AddressPoint; |
| 521 | } |
| 522 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 523 | void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 524 | bool updateVBIndex, Index_t current_vbindex, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 525 | int64_t CurrentVBaseOffset) { |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 526 | if (!RD->isDynamicClass()) |
| 527 | return; |
| 528 | |
| 529 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 530 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 531 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 532 | |
| 533 | // vtables are composed from the chain of primaries. |
| 534 | if (PrimaryBase) { |
| 535 | D1(printf(" doing primaries for %s most derived %s\n", |
| 536 | RD->getNameAsCString(), Class->getNameAsCString())); |
| 537 | |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 538 | int BaseCurrentVBaseOffset = CurrentVBaseOffset; |
| 539 | if (PrimaryBaseWasVirtual) |
| 540 | BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase); |
| 541 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 542 | if (!PrimaryBaseWasVirtual) |
| 543 | Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 544 | updateVBIndex, current_vbindex, BaseCurrentVBaseOffset); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | D1(printf(" doing vcall entries for %s most derived %s\n", |
| 548 | RD->getNameAsCString(), Class->getNameAsCString())); |
| 549 | |
| 550 | // And add the virtuals for the class to the primary vtable. |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 551 | AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 555 | bool updateVBIndex, Index_t current_vbindex, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 556 | bool RDisVirtualBase, int64_t CurrentVBaseOffset, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 557 | bool bottom) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 558 | if (!RD->isDynamicClass()) |
| 559 | return; |
| 560 | |
| 561 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 562 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 563 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 564 | |
| 565 | // vtables are composed from the chain of primaries. |
| 566 | if (PrimaryBase) { |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 567 | int BaseCurrentVBaseOffset = CurrentVBaseOffset; |
| 568 | if (PrimaryBaseWasVirtual) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 569 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 570 | BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase); |
| 571 | } |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 572 | |
| 573 | D1(printf(" doing primaries for %s most derived %s\n", |
| 574 | RD->getNameAsCString(), Class->getNameAsCString())); |
| 575 | |
| 576 | VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 577 | updateVBIndex, current_vbindex, PrimaryBaseWasVirtual, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 578 | BaseCurrentVBaseOffset, false); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 581 | D1(printf(" doing vbase entries for %s most derived %s\n", |
| 582 | RD->getNameAsCString(), Class->getNameAsCString())); |
| 583 | GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex); |
| 584 | |
| 585 | if (RDisVirtualBase || bottom) { |
| 586 | Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex, |
Eli Friedman | 76ed1f7 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 587 | CurrentVBaseOffset); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 588 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Mike Stump | 4cde626 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 591 | int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0, |
| 592 | bool MorallyVirtual = false, |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 593 | bool ForVirtualBase = false, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 594 | int CurrentVBaseOffset = 0, |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 595 | Path_t *Path = 0) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 596 | if (!RD->isDynamicClass()) |
| 597 | return 0; |
| 598 | |
Mike Stump | 92774d1 | 2009-11-13 02:35:38 +0000 | [diff] [blame] | 599 | // Construction vtable don't need parts that have no virtual bases and |
| 600 | // aren't morally virtual. |
| 601 | if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual) |
| 602 | return 0; |
| 603 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 604 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 605 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 606 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 607 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 608 | extra = 0; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 609 | D1(printf("building entries for base %s most derived %s\n", |
| 610 | RD->getNameAsCString(), Class->getNameAsCString())); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 611 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 612 | if (ForVirtualBase) |
| 613 | extra = VCalls.size(); |
| 614 | |
| 615 | VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 616 | CurrentVBaseOffset, true); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 617 | |
| 618 | if (Path) |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 619 | OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 620 | |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 621 | return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 622 | Offset, ForVirtualBase, CurrentVBaseOffset, Path); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | void GenerateVtableForVBases(const CXXRecordDecl *RD, |
| 626 | int64_t Offset = 0, |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 627 | Path_t *Path = 0) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 628 | bool alloc = false; |
| 629 | if (Path == 0) { |
| 630 | alloc = true; |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 631 | Path = new Path_t; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 632 | } |
| 633 | // FIXME: We also need to override using all paths to a virtual base, |
| 634 | // right now, we just process the first path |
| 635 | Path->push_back(std::make_pair(RD, Offset)); |
| 636 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 637 | e = RD->bases_end(); i != e; ++i) { |
| 638 | const CXXRecordDecl *Base = |
| 639 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 640 | if (i->isVirtual() && !IndirectPrimary.count(Base)) { |
| 641 | // Mark it so we don't output it twice. |
| 642 | IndirectPrimary.insert(Base); |
| 643 | StartNewTable(); |
Mike Stump | 0ca4279 | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 644 | VCall.clear(); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 645 | int64_t BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 646 | int64_t CurrentVBaseOffset = BaseOffset; |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 647 | D1(printf("vtable %s virtual base %s\n", |
| 648 | Class->getNameAsCString(), Base->getNameAsCString())); |
Mike Stump | 4cde626 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 649 | GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset, |
Mike Stump | 9e7e3c6 | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 650 | Path); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 651 | } |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 652 | int64_t BaseOffset; |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 653 | if (i->isVirtual()) |
| 654 | BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 655 | else { |
| 656 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 657 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 658 | } |
| 659 | |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 660 | if (Base->getNumVBases()) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 661 | GenerateVtableForVBases(Base, BaseOffset, Path); |
Mike Stump | 11dea94 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 662 | } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 663 | } |
| 664 | Path->pop_back(); |
| 665 | if (alloc) |
| 666 | delete Path; |
| 667 | } |
| 668 | }; |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 669 | } // end anonymous namespace |
| 670 | |
| 671 | bool VtableBuilder::OverrideMethod(GlobalDecl GD, llvm::Constant *m, |
| 672 | bool MorallyVirtual, Index_t OverrideOffset, |
| 673 | Index_t Offset, int64_t CurrentVBaseOffset) { |
| 674 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 675 | |
| 676 | const bool isPure = MD->isPure(); |
| 677 | typedef CXXMethodDecl::method_iterator meth_iter; |
| 678 | // FIXME: Should OverrideOffset's be Offset? |
| 679 | |
| 680 | // FIXME: Don't like the nested loops. For very large inheritance |
| 681 | // heirarchies we could have a table on the side with the final overridder |
| 682 | // and just replace each instance of an overridden method once. Would be |
| 683 | // nice to measure the cost/benefit on real code. |
| 684 | |
| 685 | for (meth_iter mi = MD->begin_overridden_methods(), |
| 686 | e = MD->end_overridden_methods(); |
| 687 | mi != e; ++mi) { |
| 688 | GlobalDecl OGD; |
| 689 | |
| 690 | const CXXMethodDecl *OMD = *mi; |
| 691 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD)) |
| 692 | OGD = GlobalDecl(DD, GD.getDtorType()); |
| 693 | else |
| 694 | OGD = OMD; |
| 695 | |
| 696 | llvm::Constant *om; |
| 697 | om = WrapAddrOf(OGD); |
| 698 | om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty); |
| 699 | |
| 700 | for (Index_t i = 0, e = submethods.size(); |
| 701 | i != e; ++i) { |
| 702 | // FIXME: begin_overridden_methods might be too lax, covariance */ |
| 703 | if (submethods[i] != om) |
| 704 | continue; |
| 705 | QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType(); |
| 706 | CanQualType oret = CGM.getContext().getCanonicalType(nc_oret); |
| 707 | QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType(); |
| 708 | CanQualType ret = CGM.getContext().getCanonicalType(nc_ret); |
| 709 | ThunkAdjustment ReturnAdjustment; |
| 710 | if (oret != ret) { |
| 711 | // FIXME: calculate offsets for covariance |
Anders Carlsson | d6f7af5 | 2009-12-03 02:12:03 +0000 | [diff] [blame] | 712 | CovariantThunksMapTy::iterator it = CovariantThunks.find(i); |
| 713 | if (it != CovariantThunks.end()) { |
| 714 | oret = it->second.ReturnType; |
| 715 | CovariantThunks.erase(it); |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 716 | } |
| 717 | // FIXME: Double check oret |
| 718 | Index_t nv = getNVOffset(oret, ret)/8; |
| 719 | ReturnAdjustment = ThunkAdjustment(nv, getVbaseOffset(oret, ret)); |
| 720 | } |
| 721 | Index[GD] = i; |
| 722 | submethods[i] = m; |
| 723 | if (isPure) |
| 724 | PureVirtualMethods.insert(GD); |
| 725 | PureVirtualMethods.erase(OGD); |
| 726 | Thunks.erase(OGD); |
| 727 | if (MorallyVirtual || VCall.count(OGD)) { |
| 728 | Index_t &idx = VCall[OGD]; |
| 729 | if (idx == 0) { |
| 730 | NonVirtualOffset[GD] = -OverrideOffset/8 + CurrentVBaseOffset/8; |
| 731 | VCallOffset[GD] = OverrideOffset/8; |
| 732 | idx = VCalls.size()+1; |
| 733 | VCalls.push_back(0); |
| 734 | D1(printf(" vcall for %s at %d with delta %d most derived %s\n", |
| 735 | MD->getNameAsString().c_str(), (int)-idx-3, |
| 736 | (int)VCalls[idx-1], Class->getNameAsCString())); |
| 737 | } else { |
| 738 | NonVirtualOffset[GD] = NonVirtualOffset[OGD]; |
| 739 | VCallOffset[GD] = VCallOffset[OGD]; |
| 740 | VCalls[idx-1] = -VCallOffset[OGD] + OverrideOffset/8; |
| 741 | D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n", |
| 742 | MD->getNameAsString().c_str(), (int)-idx-3, |
| 743 | (int)VCalls[idx-1], Class->getNameAsCString())); |
| 744 | } |
| 745 | VCall[GD] = idx; |
| 746 | int64_t NonVirtualAdjustment = NonVirtualOffset[GD]; |
| 747 | int64_t VirtualAdjustment = |
| 748 | -((idx + extra + 2) * LLVMPointerWidth / 8); |
| 749 | |
| 750 | // Optimize out virtual adjustments of 0. |
| 751 | if (VCalls[idx-1] == 0) |
| 752 | VirtualAdjustment = 0; |
| 753 | |
| 754 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, |
| 755 | VirtualAdjustment); |
| 756 | |
| 757 | // FIXME: Do we always have to build a covariant thunk to save oret, |
| 758 | // which is the containing virtual base class? |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 759 | if (!ReturnAdjustment.isEmpty()) |
Anders Carlsson | d6f7af5 | 2009-12-03 02:12:03 +0000 | [diff] [blame] | 760 | CovariantThunks[i] = CovariantThunk(i, GD, ReturnAdjustment, oret); |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 761 | |
| 762 | if (!isPure && !ThisAdjustment.isEmpty()) |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 763 | Thunks[GD] = Thunk(i, ThisAdjustment); |
| 764 | return true; |
| 765 | } |
| 766 | |
| 767 | // FIXME: finish off |
| 768 | int64_t NonVirtualAdjustment = VCallOffset[OGD] - OverrideOffset/8; |
| 769 | |
| 770 | if (NonVirtualAdjustment || !ReturnAdjustment.isEmpty()) { |
| 771 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0); |
| 772 | |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 773 | if (!ReturnAdjustment.isEmpty()) |
Anders Carlsson | d6f7af5 | 2009-12-03 02:12:03 +0000 | [diff] [blame] | 774 | CovariantThunks[i] = |
Anders Carlsson | bdd8e38 | 2009-12-03 02:03:29 +0000 | [diff] [blame] | 775 | CovariantThunk(i, GD, ReturnAdjustment, oret); |
Anders Carlsson | d8ddffc | 2009-12-03 01:58:20 +0000 | [diff] [blame] | 776 | |
| 777 | if (!isPure) |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 778 | Thunks[GD] = Thunk(i, ThisAdjustment); |
| 779 | } |
| 780 | return true; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return false; |
Anders Carlsson | 27f69d0 | 2009-11-27 22:21:51 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Anders Carlsson | 27682a3 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 787 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 788 | /// TypeConversionRequiresAdjustment - Returns whether conversion from a |
| 789 | /// derived type to a base type requires adjustment. |
| 790 | static bool |
| 791 | TypeConversionRequiresAdjustment(ASTContext &Ctx, |
| 792 | const CXXRecordDecl *DerivedDecl, |
| 793 | const CXXRecordDecl *BaseDecl) { |
| 794 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 795 | /*RecordPaths=*/true, /*DetectVirtual=*/true); |
| 796 | if (!const_cast<CXXRecordDecl *>(DerivedDecl)-> |
| 797 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) { |
| 798 | assert(false && "Class must be derived from the passed in base class!"); |
| 799 | return false; |
| 800 | } |
| 801 | |
Anders Carlsson | 724e3e2 | 2009-11-28 03:03:52 +0000 | [diff] [blame] | 802 | // If we found a virtual base we always want to require adjustment. |
| 803 | if (Paths.getDetectedVirtual()) |
| 804 | return true; |
| 805 | |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 806 | const CXXBasePath &Path = Paths.front(); |
| 807 | |
Anders Carlsson | 724e3e2 | 2009-11-28 03:03:52 +0000 | [diff] [blame] | 808 | for (size_t Start = 0, End = Path.size(); Start != End; ++Start) { |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 809 | const CXXBasePathElement &Element = Path[Start]; |
| 810 | |
| 811 | // Check the base class offset. |
| 812 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class); |
| 813 | |
| 814 | const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>(); |
| 815 | const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 816 | |
| 817 | if (Layout.getBaseClassOffset(Base) != 0) { |
| 818 | // This requires an adjustment. |
| 819 | return true; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | static bool |
| 827 | TypeConversionRequiresAdjustment(ASTContext &Ctx, |
| 828 | QualType DerivedType, QualType BaseType) { |
| 829 | // Canonicalize the types. |
| 830 | QualType CanDerivedType = Ctx.getCanonicalType(DerivedType); |
| 831 | QualType CanBaseType = Ctx.getCanonicalType(BaseType); |
| 832 | |
| 833 | assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() && |
| 834 | "Types must have same type class!"); |
| 835 | |
| 836 | if (CanDerivedType == CanBaseType) { |
| 837 | // No adjustment needed. |
| 838 | return false; |
| 839 | } |
| 840 | |
| 841 | if (const ReferenceType *RT = dyn_cast<ReferenceType>(CanDerivedType)) { |
| 842 | CanDerivedType = RT->getPointeeType(); |
| 843 | CanBaseType = cast<ReferenceType>(CanBaseType)->getPointeeType(); |
| 844 | } else if (const PointerType *PT = dyn_cast<PointerType>(CanDerivedType)) { |
| 845 | CanDerivedType = PT->getPointeeType(); |
| 846 | CanBaseType = cast<PointerType>(CanBaseType)->getPointeeType(); |
| 847 | } else { |
| 848 | assert(false && "Unexpected return type!"); |
| 849 | } |
| 850 | |
| 851 | if (CanDerivedType == CanBaseType) { |
| 852 | // No adjustment needed. |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | const CXXRecordDecl *DerivedDecl = |
| 857 | cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl()); |
| 858 | |
| 859 | const CXXRecordDecl *BaseDecl = |
| 860 | cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl()); |
| 861 | |
| 862 | return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl); |
| 863 | } |
| 864 | |
| 865 | void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) { |
| 866 | |
| 867 | // Itanium C++ ABI 2.5.2: |
| 868 | // The order of the virtual function pointers in a virtual table is the |
| 869 | // order of declaration of the corresponding member functions in the class. |
| 870 | // |
| 871 | // There is an entry for any virtual function declared in a class, |
| 872 | // whether it is a new function or overrides a base class function, |
| 873 | // unless it overrides a function from the primary base, and conversion |
| 874 | // between their return types does not require an adjustment. |
| 875 | |
| 876 | int64_t CurrentIndex = 0; |
| 877 | |
| 878 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 879 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 880 | |
| 881 | if (PrimaryBase) { |
Anders Carlsson | 0121fbd | 2009-11-30 19:43:26 +0000 | [diff] [blame] | 882 | assert(PrimaryBase->isDefinition() && |
| 883 | "Should have the definition decl of the primary base!"); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 884 | |
| 885 | // Since the record decl shares its vtable pointer with the primary base |
| 886 | // we need to start counting at the end of the primary base's vtable. |
| 887 | CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase); |
| 888 | } |
| 889 | |
| 890 | const CXXDestructorDecl *ImplicitVirtualDtor = 0; |
| 891 | |
| 892 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 893 | e = RD->method_end(); i != e; ++i) { |
| 894 | const CXXMethodDecl *MD = *i; |
| 895 | |
| 896 | // We only want virtual methods. |
| 897 | if (!MD->isVirtual()) |
| 898 | continue; |
| 899 | |
| 900 | bool ShouldAddEntryForMethod = true; |
| 901 | |
| 902 | // Check if this method overrides a method in the primary base. |
| 903 | for (CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(), |
| 904 | e = MD->end_overridden_methods(); i != e; ++i) { |
| 905 | const CXXMethodDecl *OverriddenMD = *i; |
| 906 | const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent(); |
| 907 | assert(OverriddenMD->isCanonicalDecl() && |
| 908 | "Should have the canonical decl of the overridden RD!"); |
| 909 | |
| 910 | if (OverriddenRD == PrimaryBase) { |
| 911 | // Check if converting from the return type of the method to the |
| 912 | // return type of the overridden method requires conversion. |
| 913 | QualType ReturnType = |
| 914 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 915 | QualType OverriddenReturnType = |
| 916 | OverriddenMD->getType()->getAs<FunctionType>()->getResultType(); |
| 917 | |
| 918 | if (!TypeConversionRequiresAdjustment(CGM.getContext(), |
| 919 | ReturnType, OverriddenReturnType)) { |
| 920 | // This index is shared between the index in the vtable of the primary |
| 921 | // base class. |
| 922 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 923 | const CXXDestructorDecl *OverriddenDD = |
| 924 | cast<CXXDestructorDecl>(OverriddenMD); |
| 925 | |
| 926 | // Add both the complete and deleting entries. |
| 927 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = |
| 928 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete)); |
| 929 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = |
| 930 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting)); |
| 931 | } else { |
| 932 | MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD); |
| 933 | } |
| 934 | |
| 935 | // We don't need to add an entry for this method. |
| 936 | ShouldAddEntryForMethod = false; |
| 937 | break; |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | if (!ShouldAddEntryForMethod) |
| 943 | continue; |
| 944 | |
| 945 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 946 | if (MD->isImplicit()) { |
| 947 | assert(!ImplicitVirtualDtor && |
| 948 | "Did already see an implicit virtual dtor!"); |
| 949 | ImplicitVirtualDtor = DD; |
| 950 | continue; |
| 951 | } |
| 952 | |
| 953 | // Add the complete dtor. |
| 954 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++; |
| 955 | |
| 956 | // Add the deleting dtor. |
| 957 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++; |
| 958 | } else { |
| 959 | // Add the entry. |
| 960 | MethodVtableIndices[MD] = CurrentIndex++; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | if (ImplicitVirtualDtor) { |
| 965 | // Itanium C++ ABI 2.5.2: |
| 966 | // If a class has an implicitly-defined virtual destructor, |
| 967 | // its entries come after the declared virtual function pointers. |
| 968 | |
| 969 | // Add the complete dtor. |
| 970 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] = |
| 971 | CurrentIndex++; |
| 972 | |
| 973 | // Add the deleting dtor. |
| 974 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] = |
| 975 | CurrentIndex++; |
| 976 | } |
| 977 | |
| 978 | NumVirtualFunctionPointers[RD] = CurrentIndex; |
| 979 | } |
| 980 | |
| 981 | uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) { |
| 982 | llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 983 | NumVirtualFunctionPointers.find(RD); |
| 984 | if (I != NumVirtualFunctionPointers.end()) |
| 985 | return I->second; |
| 986 | |
| 987 | ComputeMethodVtableIndices(RD); |
| 988 | |
| 989 | I = NumVirtualFunctionPointers.find(RD); |
| 990 | assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!"); |
| 991 | return I->second; |
| 992 | } |
| 993 | |
| 994 | uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) { |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 995 | MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 996 | if (I != MethodVtableIndices.end()) |
| 997 | return I->second; |
| 998 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 999 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1000 | |
| 1001 | ComputeMethodVtableIndices(RD); |
| 1002 | |
Anders Carlsson | a0fdd91 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1003 | I = MethodVtableIndices.find(GD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1004 | assert(I != MethodVtableIndices.end() && "Did not find index!"); |
| 1005 | return I->second; |
| 1006 | } |
| 1007 | |
| 1008 | int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD, |
| 1009 | const CXXRecordDecl *VBase) { |
| 1010 | ClassPairTy ClassPair(RD, VBase); |
| 1011 | |
| 1012 | VirtualBaseClassIndiciesTy::iterator I = |
| 1013 | VirtualBaseClassIndicies.find(ClassPair); |
| 1014 | if (I != VirtualBaseClassIndicies.end()) |
| 1015 | return I->second; |
| 1016 | |
| 1017 | std::vector<llvm::Constant *> methods; |
| 1018 | // FIXME: This seems expensive. Can we do a partial job to get |
| 1019 | // just this data. |
Mike Stump | 4cde626 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 1020 | VtableBuilder b(methods, RD, RD, 0, CGM); |
Mike Stump | 6a9612f | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1021 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1022 | b.GenerateVtableForBase(RD); |
| 1023 | b.GenerateVtableForVBases(RD); |
| 1024 | |
| 1025 | for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 1026 | b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) { |
| 1027 | // Insert all types. |
| 1028 | ClassPairTy ClassPair(RD, I->first); |
| 1029 | |
| 1030 | VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second)); |
| 1031 | } |
| 1032 | |
| 1033 | I = VirtualBaseClassIndicies.find(ClassPair); |
| 1034 | assert(I != VirtualBaseClassIndicies.end() && "Did not find index!"); |
| 1035 | |
| 1036 | return I->second; |
| 1037 | } |
| 1038 | |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1039 | llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass, |
| 1040 | const CXXRecordDecl *RD, |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1041 | uint64_t Offset) { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1042 | llvm::SmallString<256> OutName; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1043 | if (LayoutClass != RD) |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1044 | getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset/8, RD, OutName); |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1045 | else |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1046 | getMangleContext().mangleCXXVtable(RD, OutName); |
| 1047 | llvm::StringRef Name = OutName.str(); |
Benjamin Kramer | 7a9474e | 2009-10-11 22:57:54 +0000 | [diff] [blame] | 1048 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1049 | std::vector<llvm::Constant *> methods; |
| 1050 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
| 1051 | int64_t AddressPoint; |
| 1052 | |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1053 | llvm::GlobalVariable *GV = getModule().getGlobalVariable(Name); |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1054 | if (GV && AddressPoints[LayoutClass] && !GV->isDeclaration()) { |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1055 | AddressPoint=(*(*(AddressPoints[LayoutClass]))[RD])[std::make_pair(RD, |
| 1056 | Offset)]; |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1057 | // FIXME: We can never have 0 address point. Do this for now so gepping |
| 1058 | // retains the same structure. Later, we'll just assert. |
| 1059 | if (AddressPoint == 0) |
| 1060 | AddressPoint = 1; |
| 1061 | } else { |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1062 | VtableBuilder b(methods, RD, LayoutClass, Offset, *this); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1063 | |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1064 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
| 1065 | // First comes the vtables for all the non-virtual bases... |
| 1066 | AddressPoint = b.GenerateVtableForBase(RD, Offset); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1067 | |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1068 | // then the vtables for all the virtual bases. |
| 1069 | b.GenerateVtableForVBases(RD, Offset); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1070 | |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1071 | bool CreateDefinition = true; |
| 1072 | if (LayoutClass != RD) |
| 1073 | CreateDefinition = true; |
| 1074 | else { |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1075 | const ASTRecordLayout &Layout = |
| 1076 | getContext().getASTRecordLayout(LayoutClass); |
| 1077 | |
| 1078 | if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) { |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1079 | if (!KeyFunction->getBody()) { |
| 1080 | // If there is a KeyFunction, and it isn't defined, just build a |
| 1081 | // reference to the vtable. |
| 1082 | CreateDefinition = false; |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | llvm::Constant *C = 0; |
| 1088 | llvm::Type *type = Ptr8Ty; |
| 1089 | llvm::GlobalVariable::LinkageTypes linktype |
| 1090 | = llvm::GlobalValue::ExternalLinkage; |
| 1091 | if (CreateDefinition) { |
| 1092 | llvm::ArrayType *ntype = llvm::ArrayType::get(Ptr8Ty, methods.size()); |
| 1093 | C = llvm::ConstantArray::get(ntype, methods); |
| 1094 | linktype = llvm::GlobalValue::LinkOnceODRLinkage; |
| 1095 | if (LayoutClass->isInAnonymousNamespace()) |
| 1096 | linktype = llvm::GlobalValue::InternalLinkage; |
| 1097 | type = ntype; |
| 1098 | } |
| 1099 | llvm::GlobalVariable *OGV = GV; |
| 1100 | GV = new llvm::GlobalVariable(getModule(), type, true, linktype, C, Name); |
| 1101 | if (OGV) { |
| 1102 | GV->takeName(OGV); |
| 1103 | llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV, |
| 1104 | OGV->getType()); |
| 1105 | OGV->replaceAllUsesWith(NewPtr); |
| 1106 | OGV->eraseFromParent(); |
| 1107 | } |
| 1108 | bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden; |
| 1109 | if (Hidden) |
| 1110 | GV->setVisibility(llvm::GlobalVariable::HiddenVisibility); |
| 1111 | } |
Mike Stump | e56ceca | 2009-11-18 04:00:48 +0000 | [diff] [blame] | 1112 | llvm::Constant *vtable = llvm::ConstantExpr::getBitCast(GV, Ptr8Ty); |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1113 | llvm::Constant *AddressPointC; |
| 1114 | uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0); |
| 1115 | AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
| 1116 | AddressPoint*LLVMPointerWidth/8); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1117 | vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC, |
| 1118 | 1); |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1119 | |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1120 | assert(vtable->getType() == Ptr8Ty); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1121 | return vtable; |
| 1122 | } |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1123 | |
Mike Stump | 92f2fe2 | 2009-12-02 19:07:44 +0000 | [diff] [blame] | 1124 | namespace { |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1125 | class VTTBuilder { |
| 1126 | /// Inits - The list of values built for the VTT. |
| 1127 | std::vector<llvm::Constant *> &Inits; |
| 1128 | /// Class - The most derived class that this vtable is being built for. |
| 1129 | const CXXRecordDecl *Class; |
| 1130 | CodeGenModule &CGM; // Per-module state. |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1131 | llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase; |
Mike Stump | aee8de3 | 2009-11-11 03:08:24 +0000 | [diff] [blame] | 1132 | /// BLayout - Layout for the most derived class that this vtable is being |
| 1133 | /// built for. |
| 1134 | const ASTRecordLayout &BLayout; |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1135 | CodeGenModule::AddrMap_t &AddressPoints; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1136 | // vtbl - A pointer to the vtable for Class. |
| 1137 | llvm::Constant *ClassVtbl; |
| 1138 | llvm::LLVMContext &VMContext; |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1139 | |
Mike Stump | 28f7ce1 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1140 | /// BuildVtablePtr - Build up a referene to the given secondary vtable |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1141 | llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl, |
| 1142 | const CXXRecordDecl *VtblClass, |
| 1143 | const CXXRecordDecl *RD, |
Mike Stump | 28f7ce1 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1144 | uint64_t Offset) { |
| 1145 | int64_t AddressPoint; |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1146 | AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)]; |
Mike Stump | 80ac235 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1147 | // FIXME: We can never have 0 address point. Do this for now so gepping |
Mike Stump | 23a3542 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 1148 | // retains the same structure. Later we'll just assert. |
Mike Stump | 80ac235 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1149 | if (AddressPoint == 0) |
| 1150 | AddressPoint = 1; |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1151 | D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n", |
| 1152 | RD->getNameAsCString(), VtblClass->getNameAsCString(), |
| 1153 | Class->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | 28f7ce1 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1154 | uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0); |
| 1155 | llvm::Constant *init; |
| 1156 | init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), |
| 1157 | AddressPoint*LLVMPointerWidth/8); |
| 1158 | init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1); |
| 1159 | return init; |
| 1160 | } |
| 1161 | |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1162 | /// Secondary - Add the secondary vtable pointers to Inits. Offset is the |
| 1163 | /// current offset in bits to the object we're working on. |
Mike Stump | 28f7ce1 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1164 | void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl, |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1165 | const CXXRecordDecl *VtblClass, uint64_t Offset=0, |
| 1166 | bool MorallyVirtual=false) { |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1167 | if (RD->getNumVBases() == 0 && ! MorallyVirtual) |
| 1168 | return; |
| 1169 | |
| 1170 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1171 | e = RD->bases_end(); i != e; ++i) { |
| 1172 | const CXXRecordDecl *Base = |
| 1173 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1174 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1175 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1176 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 1177 | bool NonVirtualPrimaryBase; |
| 1178 | NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase; |
| 1179 | bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual(); |
Mike Stump | aee8de3 | 2009-11-11 03:08:24 +0000 | [diff] [blame] | 1180 | uint64_t BaseOffset; |
| 1181 | if (!i->isVirtual()) { |
| 1182 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1183 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 1184 | } else |
| 1185 | BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 80ac235 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1186 | llvm::Constant *subvtbl = vtbl; |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1187 | const CXXRecordDecl *subVtblClass = VtblClass; |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1188 | if ((Base->getNumVBases() || BaseMorallyVirtual) |
| 1189 | && !NonVirtualPrimaryBase) { |
| 1190 | // FIXME: Slightly too many of these for __ZTT8test8_B2 |
Mike Stump | 28f7ce1 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1191 | llvm::Constant *init; |
Mike Stump | 80ac235 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1192 | if (BaseMorallyVirtual) |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1193 | init = BuildVtablePtr(vtbl, VtblClass, RD, Offset); |
Mike Stump | 80ac235 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1194 | else { |
Mike Stump | 28f7ce1 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1195 | init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset); |
Mike Stump | 80ac235 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1196 | subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0)); |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1197 | subVtblClass = Base; |
Mike Stump | 80ac235 | 2009-11-12 23:36:21 +0000 | [diff] [blame] | 1198 | } |
Mike Stump | 28f7ce1 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1199 | Inits.push_back(init); |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1200 | } |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1201 | Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual); |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1205 | /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the |
| 1206 | /// currnet object we're working on. |
| 1207 | void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) { |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1208 | if (RD->getNumVBases() == 0 && !MorallyVirtual) |
| 1209 | return; |
| 1210 | |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1211 | llvm::Constant *init; |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1212 | const CXXRecordDecl *VtblClass; |
| 1213 | |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1214 | // First comes the primary virtual table pointer... |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1215 | if (MorallyVirtual) { |
| 1216 | init = BuildVtablePtr(ClassVtbl, Class, RD, Offset); |
| 1217 | VtblClass = Class; |
| 1218 | } else { |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1219 | init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset); |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1220 | VtblClass = RD; |
| 1221 | } |
Mike Stump | 28f7ce1 | 2009-11-12 22:56:32 +0000 | [diff] [blame] | 1222 | llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0)); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1223 | Inits.push_back(init); |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1224 | |
| 1225 | // then the secondary VTTs.... |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1226 | SecondaryVTTs(RD, Offset, MorallyVirtual); |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1227 | |
| 1228 | // and last the secondary vtable pointers. |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1229 | Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual); |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are |
| 1233 | /// built from each direct non-virtual proper base that requires a VTT in |
| 1234 | /// declaration order. |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1235 | void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0, |
| 1236 | bool MorallyVirtual=false) { |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1237 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1238 | e = RD->bases_end(); i != e; ++i) { |
| 1239 | const CXXRecordDecl *Base = |
| 1240 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1241 | if (i->isVirtual()) |
| 1242 | continue; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1243 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1244 | uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 1245 | BuildVTT(Base, BaseOffset, MorallyVirtual); |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance |
| 1250 | /// graph preorder. |
| 1251 | void VirtualVTTs(const CXXRecordDecl *RD) { |
| 1252 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 1253 | e = RD->bases_end(); i != e; ++i) { |
| 1254 | const CXXRecordDecl *Base = |
| 1255 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1256 | if (i->isVirtual() && !SeenVBase.count(Base)) { |
| 1257 | SeenVBase.insert(Base); |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1258 | uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base); |
| 1259 | BuildVTT(Base, BaseOffset, true); |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1260 | } |
| 1261 | VirtualVTTs(Base); |
| 1262 | } |
| 1263 | } |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1264 | public: |
| 1265 | VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c, |
Mike Stump | aee8de3 | 2009-11-11 03:08:24 +0000 | [diff] [blame] | 1266 | CodeGenModule &cgm) |
| 1267 | : Inits(inits), Class(c), CGM(cgm), |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1268 | BLayout(cgm.getContext().getASTRecordLayout(c)), |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1269 | AddressPoints(*cgm.AddressPoints[c]), |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1270 | VMContext(cgm.getModule().getContext()) { |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1271 | |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1272 | // First comes the primary virtual table pointer for the complete class... |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1273 | ClassVtbl = CGM.getVtableInfo().getVtable(Class); |
| 1274 | Inits.push_back(ClassVtbl); |
| 1275 | ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0)); |
| 1276 | |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1277 | // then the secondary VTTs... |
| 1278 | SecondaryVTTs(Class); |
| 1279 | |
| 1280 | // then the secondary vtable pointers... |
Mike Stump | acfd1e5 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 1281 | Secondary(Class, ClassVtbl, Class); |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1282 | |
| 1283 | // and last, the virtual VTTs. |
| 1284 | VirtualVTTs(Class); |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1285 | } |
| 1286 | }; |
Mike Stump | 92f2fe2 | 2009-12-02 19:07:44 +0000 | [diff] [blame] | 1287 | } |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1288 | |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1289 | llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) { |
Mike Stump | f1c0333 | 2009-11-10 19:13:04 +0000 | [diff] [blame] | 1290 | // Only classes that have virtual bases need a VTT. |
| 1291 | if (RD->getNumVBases() == 0) |
| 1292 | return 0; |
| 1293 | |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1294 | llvm::SmallString<256> OutName; |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1295 | getMangleContext().mangleCXXVTT(RD, OutName); |
| 1296 | llvm::StringRef Name = OutName.str(); |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1297 | |
| 1298 | llvm::GlobalVariable::LinkageTypes linktype; |
| 1299 | linktype = llvm::GlobalValue::LinkOnceODRLinkage; |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1300 | if (RD->isInAnonymousNamespace()) |
| 1301 | linktype = llvm::GlobalValue::InternalLinkage; |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1302 | std::vector<llvm::Constant *> inits; |
| 1303 | llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); |
| 1304 | |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1305 | D1(printf("vtt %s\n", RD->getNameAsCString())); |
| 1306 | |
Mike Stump | 971977f | 2009-11-11 00:35:07 +0000 | [diff] [blame] | 1307 | VTTBuilder b(inits, RD, *this); |
| 1308 | |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1309 | llvm::Constant *C; |
| 1310 | llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size()); |
| 1311 | C = llvm::ConstantArray::get(type, inits); |
Mike Stump | e56ceca | 2009-11-18 04:00:48 +0000 | [diff] [blame] | 1312 | llvm::GlobalVariable *vtt = new llvm::GlobalVariable(getModule(), type, true, |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1313 | linktype, C, Name); |
Mike Stump | e56ceca | 2009-11-18 04:00:48 +0000 | [diff] [blame] | 1314 | bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden; |
| 1315 | if (Hidden) |
| 1316 | vtt->setVisibility(llvm::GlobalVariable::HiddenVisibility); |
| 1317 | return llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty); |
Mike Stump | fbfb52d | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1318 | } |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1319 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 1320 | void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) { |
| 1321 | Vtables[RD] = CGM.GenerateVtable(RD, RD); |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 1322 | CGM.GenerateRTTI(RD); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 1323 | CGM.GenerateVTT(RD); |
| 1324 | } |
| 1325 | |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1326 | llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) { |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1327 | llvm::Constant *&vtbl = Vtables[RD]; |
| 1328 | if (vtbl) |
| 1329 | return vtbl; |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1330 | vtbl = CGM.GenerateVtable(RD, RD); |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1331 | |
| 1332 | bool CreateDefinition = true; |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1333 | |
| 1334 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1335 | if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) { |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1336 | if (!KeyFunction->getBody()) { |
| 1337 | // If there is a KeyFunction, and it isn't defined, just build a |
| 1338 | // reference to the vtable. |
| 1339 | CreateDefinition = false; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | if (CreateDefinition) { |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 1344 | CGM.GenerateRTTI(RD); |
Mike Stump | 85615df | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1345 | CGM.GenerateVTT(RD); |
| 1346 | } |
Mike Stump | 380dd75 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1347 | return vtbl; |
| 1348 | } |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1349 | |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1350 | llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass, |
| 1351 | const CXXRecordDecl *RD, |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1352 | uint64_t Offset) { |
Mike Stump | 9840c70 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1353 | return CGM.GenerateVtable(LayoutClass, RD, Offset); |
Mike Stump | 8cfcb52 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1354 | } |
Anders Carlsson | 1a5e0d7 | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1355 | |
| 1356 | void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) { |
| 1357 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 1358 | const CXXRecordDecl *RD = MD->getParent(); |
| 1359 | |
| 1360 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1361 | |
| 1362 | // Get the key function. |
| 1363 | const CXXMethodDecl *KeyFunction = Layout.getKeyFunction(); |
| 1364 | |
| 1365 | if (!KeyFunction) { |
| 1366 | // If there's no key function, we don't want to emit the vtable here. |
| 1367 | return; |
| 1368 | } |
| 1369 | |
| 1370 | // Check if we have the key function. |
| 1371 | if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl()) |
| 1372 | return; |
| 1373 | |
| 1374 | // If the key function is a destructor, we only want to emit the vtable once, |
| 1375 | // so do it for the complete destructor. |
| 1376 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Complete) |
| 1377 | return; |
| 1378 | |
| 1379 | // Emit the data. |
| 1380 | GenerateClassData(RD); |
| 1381 | } |
| 1382 | |