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