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