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