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