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