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