Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 1 | //===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===// |
| 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 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ code generation targeting the Microsoft Visual C++ ABI. |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 11 | // The class in this file generates structures that follow the Microsoft |
| 12 | // Visual C++ ABI, which is actually not very well documented at all outside |
| 13 | // of Microsoft. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "CGCXXABI.h" |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 18 | #include "CGVTables.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 19 | #include "CodeGenModule.h" |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
| 21 | #include "clang/AST/DeclCXX.h" |
Timur Iskhodzhanov | 635de28 | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 22 | #include "clang/AST/VTableBuilder.h" |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringSet.h" |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 25 | #include "llvm/IR/CallSite.h" |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
| 30 | namespace { |
| 31 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 32 | /// Holds all the vbtable globals for a given class. |
| 33 | struct VBTableGlobals { |
| 34 | const VPtrInfoVector *VBTables; |
| 35 | SmallVector<llvm::GlobalVariable *, 2> Globals; |
| 36 | }; |
| 37 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 38 | class MicrosoftCXXABI : public CGCXXABI { |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 39 | public: |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 40 | MicrosoftCXXABI(CodeGenModule &CGM) |
| 41 | : CGCXXABI(CGM), BaseClassDescriptorType(nullptr), |
| 42 | ClassHierarchyDescriptorType(nullptr), |
| 43 | CompleteObjectLocatorType(nullptr) {} |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 44 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 45 | bool HasThisReturn(GlobalDecl GD) const override; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 46 | bool hasMostDerivedReturn(GlobalDecl GD) const override; |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 47 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 48 | bool classifyReturnType(CGFunctionInfo &FI) const override; |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 49 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 50 | RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override; |
| 51 | |
| 52 | bool isSRetParameterAfterThis() const override { return true; } |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 53 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 54 | size_t getSrcArgforCopyCtor(const CXXConstructorDecl *CD, |
| 55 | FunctionArgList &Args) const override { |
| 56 | assert(Args.size() >= 2 && |
| 57 | "expected the arglist to have at least two args!"); |
| 58 | // The 'most_derived' parameter goes second if the ctor is variadic and |
| 59 | // has v-bases. |
| 60 | if (CD->getParent()->getNumVBases() > 0 && |
| 61 | CD->getType()->castAs<FunctionProtoType>()->isVariadic()) |
| 62 | return 2; |
| 63 | return 1; |
| 64 | } |
| 65 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 66 | StringRef GetPureVirtualCallName() override { return "_purecall"; } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 67 | StringRef GetDeletedVirtualCallName() override { return "_purecall"; } |
Joao Matos | 285baac | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 68 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 69 | void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, |
| 70 | llvm::Value *Ptr, QualType ElementType, |
| 71 | const CXXDestructorDecl *Dtor) override; |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 72 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 73 | void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override; |
| 74 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 75 | llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD, |
| 76 | const VPtrInfo *Info); |
| 77 | |
| 78 | llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override; |
| 79 | |
| 80 | bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override; |
| 81 | void EmitBadTypeidCall(CodeGenFunction &CGF) override; |
| 82 | llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, |
| 83 | llvm::Value *ThisPtr, |
| 84 | llvm::Type *StdTypeInfoPtrTy) override; |
| 85 | |
| 86 | bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, |
| 87 | QualType SrcRecordTy) override; |
| 88 | |
| 89 | llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, |
| 90 | QualType SrcRecordTy, QualType DestTy, |
| 91 | QualType DestRecordTy, |
| 92 | llvm::BasicBlock *CastEnd) override; |
| 93 | |
| 94 | llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value, |
| 95 | QualType SrcRecordTy, |
| 96 | QualType DestTy) override; |
| 97 | |
| 98 | bool EmitBadCastCall(CodeGenFunction &CGF) override; |
| 99 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 100 | llvm::Value * |
| 101 | GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This, |
| 102 | const CXXRecordDecl *ClassDecl, |
| 103 | const CXXRecordDecl *BaseClassDecl) override; |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 104 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 105 | llvm::BasicBlock * |
| 106 | EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 107 | const CXXRecordDecl *RD) override; |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 108 | |
Timur Iskhodzhanov | 5bd0d44 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 109 | void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 110 | const CXXRecordDecl *RD) override; |
Timur Iskhodzhanov | 5bd0d44 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 111 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 112 | void EmitCXXConstructors(const CXXConstructorDecl *D) override; |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 113 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 114 | // Background on MSVC destructors |
| 115 | // ============================== |
| 116 | // |
| 117 | // Both Itanium and MSVC ABIs have destructor variants. The variant names |
| 118 | // roughly correspond in the following way: |
| 119 | // Itanium Microsoft |
| 120 | // Base -> no name, just ~Class |
| 121 | // Complete -> vbase destructor |
| 122 | // Deleting -> scalar deleting destructor |
| 123 | // vector deleting destructor |
| 124 | // |
| 125 | // The base and complete destructors are the same as in Itanium, although the |
| 126 | // complete destructor does not accept a VTT parameter when there are virtual |
| 127 | // bases. A separate mechanism involving vtordisps is used to ensure that |
| 128 | // virtual methods of destroyed subobjects are not called. |
| 129 | // |
| 130 | // The deleting destructors accept an i32 bitfield as a second parameter. Bit |
| 131 | // 1 indicates if the memory should be deleted. Bit 2 indicates if the this |
| 132 | // pointer points to an array. The scalar deleting destructor assumes that |
| 133 | // bit 2 is zero, and therefore does not contain a loop. |
| 134 | // |
| 135 | // For virtual destructors, only one entry is reserved in the vftable, and it |
| 136 | // always points to the vector deleting destructor. The vector deleting |
| 137 | // destructor is the most general, so it can be used to destroy objects in |
| 138 | // place, delete single heap objects, or delete arrays. |
| 139 | // |
| 140 | // A TU defining a non-inline destructor is only guaranteed to emit a base |
| 141 | // destructor, and all of the other variants are emitted on an as-needed basis |
| 142 | // in COMDATs. Because a non-base destructor can be emitted in a TU that |
| 143 | // lacks a definition for the destructor, non-base destructors must always |
| 144 | // delegate to or alias the base destructor. |
| 145 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 146 | void buildStructorSignature(const CXXMethodDecl *MD, StructorType T, |
| 147 | SmallVectorImpl<CanQualType> &ArgTys) override; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 148 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 149 | /// Non-base dtors should be emitted as delegating thunks in this ABI. |
| 150 | bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 151 | CXXDtorType DT) const override { |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 152 | return DT != Dtor_Base; |
| 153 | } |
| 154 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 155 | void EmitCXXDestructors(const CXXDestructorDecl *D) override; |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 156 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 157 | const CXXRecordDecl * |
| 158 | getThisArgumentTypeForMethod(const CXXMethodDecl *MD) override { |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 159 | MD = MD->getCanonicalDecl(); |
| 160 | if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) { |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 161 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 162 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 163 | // The vbases might be ordered differently in the final overrider object |
| 164 | // and the complete object, so the "this" argument may sometimes point to |
| 165 | // memory that has no particular type (e.g. past the complete object). |
| 166 | // In this case, we just use a generic pointer type. |
| 167 | // FIXME: might want to have a more precise type in the non-virtual |
| 168 | // multiple inheritance case. |
Timur Iskhodzhanov | 40aa366 | 2013-11-07 13:34:02 +0000 | [diff] [blame] | 169 | if (ML.VBase || !ML.VFPtrOffset.isZero()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 170 | return nullptr; |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 171 | } |
| 172 | return MD->getParent(); |
| 173 | } |
| 174 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 175 | llvm::Value * |
| 176 | adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD, |
| 177 | llvm::Value *This, |
| 178 | bool VirtualCall) override; |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 179 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 180 | void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, |
| 181 | FunctionArgList &Params) override; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 182 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 183 | llvm::Value *adjustThisParameterInVirtualFunctionPrologue( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 184 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) override; |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 185 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 186 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override; |
John McCall | fd70826 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 187 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 188 | unsigned addImplicitConstructorArgs(CodeGenFunction &CGF, |
| 189 | const CXXConstructorDecl *D, |
| 190 | CXXCtorType Type, bool ForVirtualBase, |
| 191 | bool Delegating, |
| 192 | CallArgList &Args) override; |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 193 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 194 | void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, |
| 195 | CXXDtorType Type, bool ForVirtualBase, |
| 196 | bool Delegating, llvm::Value *This) override; |
| 197 | |
| 198 | void emitVTableDefinitions(CodeGenVTables &CGVT, |
| 199 | const CXXRecordDecl *RD) override; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 200 | |
| 201 | llvm::Value *getVTableAddressPointInStructor( |
| 202 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, |
| 203 | BaseSubobject Base, const CXXRecordDecl *NearestVBase, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 204 | bool &NeedsVirtualOffset) override; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 205 | |
| 206 | llvm::Constant * |
| 207 | getVTableAddressPointForConstExpr(BaseSubobject Base, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 208 | const CXXRecordDecl *VTableClass) override; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 209 | |
| 210 | llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 211 | CharUnits VPtrOffset) override; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 212 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 213 | llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 214 | llvm::Value *This, |
| 215 | llvm::Type *Ty) override; |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 216 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 217 | llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 218 | const CXXDestructorDecl *Dtor, |
| 219 | CXXDtorType DtorType, |
| 220 | llvm::Value *This, |
| 221 | const CXXMemberCallExpr *CE) override; |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 222 | |
Timur Iskhodzhanov | 2cb17a0 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 223 | void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 224 | CallArgList &CallArgs) override { |
Timur Iskhodzhanov | 2cb17a0 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 225 | assert(GD.getDtorType() == Dtor_Deleting && |
| 226 | "Only deleting destructor thunks are available in this ABI"); |
| 227 | CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)), |
| 228 | CGM.getContext().IntTy); |
| 229 | } |
| 230 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 231 | void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override; |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 232 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 233 | llvm::GlobalVariable * |
| 234 | getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, |
| 235 | llvm::GlobalVariable::LinkageTypes Linkage); |
| 236 | |
| 237 | void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD, |
| 238 | llvm::GlobalVariable *GV) const; |
| 239 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 240 | void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, |
| 241 | GlobalDecl GD, bool ReturnAdjustment) override { |
| 242 | // Never dllimport/dllexport thunks. |
| 243 | Thunk->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); |
| 244 | |
| 245 | GVALinkage Linkage = |
| 246 | getContext().GetGVALinkageForFunction(cast<FunctionDecl>(GD.getDecl())); |
| 247 | |
| 248 | if (Linkage == GVA_Internal) |
| 249 | Thunk->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 250 | else if (ReturnAdjustment) |
| 251 | Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage); |
| 252 | else |
| 253 | Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); |
Timur Iskhodzhanov | 2cb17a0 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 256 | llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 257 | const ThisAdjustment &TA) override; |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 258 | |
| 259 | llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 260 | const ReturnAdjustment &RA) override; |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 261 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 262 | void EmitThreadLocalInitFuncs( |
| 263 | CodeGenModule &CGM, |
| 264 | ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> |
| 265 | CXXThreadLocals, |
| 266 | ArrayRef<llvm::Function *> CXXThreadLocalInits, |
| 267 | ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override; |
| 268 | |
| 269 | bool usesThreadWrapperFunction() const override { return false; } |
| 270 | LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, |
| 271 | QualType LValType) override; |
| 272 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 273 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| 274 | llvm::GlobalVariable *DeclPtr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 275 | bool PerformInit) override; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 276 | void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, |
| 277 | llvm::Constant *Dtor, llvm::Constant *Addr) override; |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 278 | |
John McCall | fd70826 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 279 | // ==== Notes on array cookies ========= |
| 280 | // |
| 281 | // MSVC seems to only use cookies when the class has a destructor; a |
| 282 | // two-argument usual array deallocation function isn't sufficient. |
| 283 | // |
| 284 | // For example, this code prints "100" and "1": |
| 285 | // struct A { |
| 286 | // char x; |
| 287 | // void *operator new[](size_t sz) { |
| 288 | // printf("%u\n", sz); |
| 289 | // return malloc(sz); |
| 290 | // } |
| 291 | // void operator delete[](void *p, size_t sz) { |
| 292 | // printf("%u\n", sz); |
| 293 | // free(p); |
| 294 | // } |
| 295 | // }; |
| 296 | // int main() { |
| 297 | // A *p = new A[100]; |
| 298 | // delete[] p; |
| 299 | // } |
| 300 | // Whereas it prints "104" and "104" if you give A a destructor. |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 301 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 302 | bool requiresArrayCookie(const CXXDeleteExpr *expr, |
| 303 | QualType elementType) override; |
| 304 | bool requiresArrayCookie(const CXXNewExpr *expr) override; |
| 305 | CharUnits getArrayCookieSizeImpl(QualType type) override; |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 306 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 307 | llvm::Value *NewPtr, |
| 308 | llvm::Value *NumElements, |
| 309 | const CXXNewExpr *expr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 310 | QualType ElementType) override; |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 311 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, |
| 312 | llvm::Value *allocPtr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 313 | CharUnits cookieSize) override; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 314 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 315 | friend struct MSRTTIBuilder; |
| 316 | |
| 317 | bool isImageRelative() const { |
| 318 | return CGM.getTarget().getPointerWidth(/*AddressSpace=*/0) == 64; |
| 319 | } |
| 320 | |
| 321 | // 5 routines for constructing the llvm types for MS RTTI structs. |
| 322 | llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) { |
| 323 | llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor"); |
| 324 | TDTypeName += llvm::utostr(TypeInfoString.size()); |
| 325 | llvm::StructType *&TypeDescriptorType = |
| 326 | TypeDescriptorTypeMap[TypeInfoString.size()]; |
| 327 | if (TypeDescriptorType) |
| 328 | return TypeDescriptorType; |
| 329 | llvm::Type *FieldTypes[] = { |
| 330 | CGM.Int8PtrPtrTy, |
| 331 | CGM.Int8PtrTy, |
| 332 | llvm::ArrayType::get(CGM.Int8Ty, TypeInfoString.size() + 1)}; |
| 333 | TypeDescriptorType = |
| 334 | llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, TDTypeName); |
| 335 | return TypeDescriptorType; |
| 336 | } |
| 337 | |
| 338 | llvm::Type *getImageRelativeType(llvm::Type *PtrType) { |
| 339 | if (!isImageRelative()) |
| 340 | return PtrType; |
| 341 | return CGM.IntTy; |
| 342 | } |
| 343 | |
| 344 | llvm::StructType *getBaseClassDescriptorType() { |
| 345 | if (BaseClassDescriptorType) |
| 346 | return BaseClassDescriptorType; |
| 347 | llvm::Type *FieldTypes[] = { |
| 348 | getImageRelativeType(CGM.Int8PtrTy), |
| 349 | CGM.IntTy, |
| 350 | CGM.IntTy, |
| 351 | CGM.IntTy, |
| 352 | CGM.IntTy, |
| 353 | CGM.IntTy, |
| 354 | getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), |
| 355 | }; |
| 356 | BaseClassDescriptorType = llvm::StructType::create( |
| 357 | CGM.getLLVMContext(), FieldTypes, "rtti.BaseClassDescriptor"); |
| 358 | return BaseClassDescriptorType; |
| 359 | } |
| 360 | |
| 361 | llvm::StructType *getClassHierarchyDescriptorType() { |
| 362 | if (ClassHierarchyDescriptorType) |
| 363 | return ClassHierarchyDescriptorType; |
| 364 | // Forward-declare RTTIClassHierarchyDescriptor to break a cycle. |
| 365 | ClassHierarchyDescriptorType = llvm::StructType::create( |
| 366 | CGM.getLLVMContext(), "rtti.ClassHierarchyDescriptor"); |
| 367 | llvm::Type *FieldTypes[] = { |
| 368 | CGM.IntTy, |
| 369 | CGM.IntTy, |
| 370 | CGM.IntTy, |
| 371 | getImageRelativeType( |
| 372 | getBaseClassDescriptorType()->getPointerTo()->getPointerTo()), |
| 373 | }; |
| 374 | ClassHierarchyDescriptorType->setBody(FieldTypes); |
| 375 | return ClassHierarchyDescriptorType; |
| 376 | } |
| 377 | |
| 378 | llvm::StructType *getCompleteObjectLocatorType() { |
| 379 | if (CompleteObjectLocatorType) |
| 380 | return CompleteObjectLocatorType; |
| 381 | CompleteObjectLocatorType = llvm::StructType::create( |
| 382 | CGM.getLLVMContext(), "rtti.CompleteObjectLocator"); |
| 383 | llvm::Type *FieldTypes[] = { |
| 384 | CGM.IntTy, |
| 385 | CGM.IntTy, |
| 386 | CGM.IntTy, |
| 387 | getImageRelativeType(CGM.Int8PtrTy), |
| 388 | getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), |
| 389 | getImageRelativeType(CompleteObjectLocatorType), |
| 390 | }; |
| 391 | llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes); |
| 392 | if (!isImageRelative()) |
| 393 | FieldTypesRef = FieldTypesRef.drop_back(); |
| 394 | CompleteObjectLocatorType->setBody(FieldTypesRef); |
| 395 | return CompleteObjectLocatorType; |
| 396 | } |
| 397 | |
| 398 | llvm::GlobalVariable *getImageBase() { |
| 399 | StringRef Name = "__ImageBase"; |
| 400 | if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name)) |
| 401 | return GV; |
| 402 | |
| 403 | return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, |
| 404 | /*isConstant=*/true, |
| 405 | llvm::GlobalValue::ExternalLinkage, |
| 406 | /*Initializer=*/nullptr, Name); |
| 407 | } |
| 408 | |
| 409 | llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) { |
| 410 | if (!isImageRelative()) |
| 411 | return PtrVal; |
| 412 | |
| 413 | llvm::Constant *ImageBaseAsInt = |
| 414 | llvm::ConstantExpr::getPtrToInt(getImageBase(), CGM.IntPtrTy); |
| 415 | llvm::Constant *PtrValAsInt = |
| 416 | llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy); |
| 417 | llvm::Constant *Diff = |
| 418 | llvm::ConstantExpr::getSub(PtrValAsInt, ImageBaseAsInt, |
| 419 | /*HasNUW=*/true, /*HasNSW=*/true); |
| 420 | return llvm::ConstantExpr::getTrunc(Diff, CGM.IntTy); |
| 421 | } |
| 422 | |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 423 | private: |
Timur Iskhodzhanov | 11f22a3 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 424 | MicrosoftMangleContext &getMangleContext() { |
| 425 | return cast<MicrosoftMangleContext>(CodeGen::CGCXXABI::getMangleContext()); |
| 426 | } |
| 427 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 428 | llvm::Constant *getZeroInt() { |
| 429 | return llvm::ConstantInt::get(CGM.IntTy, 0); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 432 | llvm::Constant *getAllOnesInt() { |
| 433 | return llvm::Constant::getAllOnesValue(CGM.IntTy); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 436 | llvm::Constant *getConstantOrZeroInt(llvm::Constant *C) { |
| 437 | return C ? C : getZeroInt(); |
| 438 | } |
| 439 | |
| 440 | llvm::Value *getValueOrZeroInt(llvm::Value *C) { |
| 441 | return C ? C : getZeroInt(); |
| 442 | } |
| 443 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 444 | CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD); |
| 445 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 446 | void |
| 447 | GetNullMemberPointerFields(const MemberPointerType *MPT, |
| 448 | llvm::SmallVectorImpl<llvm::Constant *> &fields); |
| 449 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 450 | /// \brief Shared code for virtual base adjustment. Returns the offset from |
| 451 | /// the vbptr to the virtual base. Optionally returns the address of the |
| 452 | /// vbptr itself. |
| 453 | llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 454 | llvm::Value *Base, |
| 455 | llvm::Value *VBPtrOffset, |
| 456 | llvm::Value *VBTableOffset, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 457 | llvm::Value **VBPtr = nullptr); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 458 | |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 459 | llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 460 | llvm::Value *Base, |
| 461 | int32_t VBPtrOffset, |
| 462 | int32_t VBTableOffset, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 463 | llvm::Value **VBPtr = nullptr) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 464 | assert(VBTableOffset % 4 == 0 && "should be byte offset into table of i32s"); |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 465 | llvm::Value *VBPOffset = llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), |
| 466 | *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset); |
| 467 | return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr); |
| 468 | } |
| 469 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 470 | /// \brief Performs a full virtual base adjustment. Used to dereference |
| 471 | /// pointers to members of virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 472 | llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E, |
| 473 | const CXXRecordDecl *RD, llvm::Value *Base, |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 474 | llvm::Value *VirtualBaseAdjustmentOffset, |
| 475 | llvm::Value *VBPtrOffset /* optional */); |
| 476 | |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 477 | /// \brief Emits a full member pointer with the fields common to data and |
| 478 | /// function member pointers. |
| 479 | llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField, |
| 480 | bool IsMemberFunction, |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 481 | const CXXRecordDecl *RD, |
| 482 | CharUnits NonVirtualBaseAdjustment); |
| 483 | |
| 484 | llvm::Constant *BuildMemberPointer(const CXXRecordDecl *RD, |
| 485 | const CXXMethodDecl *MD, |
| 486 | CharUnits NonVirtualBaseAdjustment); |
| 487 | |
| 488 | bool MemberPointerConstantIsNull(const MemberPointerType *MPT, |
| 489 | llvm::Constant *MP); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 490 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 491 | /// \brief - Initialize all vbptrs of 'this' with RD as the complete type. |
| 492 | void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD); |
| 493 | |
| 494 | /// \brief Caching wrapper around VBTableBuilder::enumerateVBTables(). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 495 | const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 496 | |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 497 | /// \brief Generate a thunk for calling a virtual member function MD. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 498 | llvm::Function *EmitVirtualMemPtrThunk( |
| 499 | const CXXMethodDecl *MD, |
| 500 | const MicrosoftVTableContext::MethodVFTableLocation &ML); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 501 | |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 502 | public: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 503 | llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 504 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 505 | bool isZeroInitializable(const MemberPointerType *MPT) override; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 506 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 507 | bool isMemberPointerConvertible(const MemberPointerType *MPT) const override { |
| 508 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 509 | return RD->hasAttr<MSInheritanceAttr>(); |
| 510 | } |
| 511 | |
| 512 | bool isTypeInfoCalculable(QualType Ty) const override { |
| 513 | if (!CGCXXABI::isTypeInfoCalculable(Ty)) |
| 514 | return false; |
| 515 | if (const auto *MPT = Ty->getAs<MemberPointerType>()) { |
| 516 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 517 | if (!RD->hasAttr<MSInheritanceAttr>()) |
| 518 | return false; |
| 519 | } |
| 520 | return true; |
| 521 | } |
| 522 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 523 | llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 524 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 525 | llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
| 526 | CharUnits offset) override; |
| 527 | llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD) override; |
| 528 | llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 529 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 530 | llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 531 | llvm::Value *L, |
| 532 | llvm::Value *R, |
| 533 | const MemberPointerType *MPT, |
| 534 | bool Inequality) override; |
Reid Kleckner | 3d2f000 | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 535 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 536 | llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 537 | llvm::Value *MemPtr, |
| 538 | const MemberPointerType *MPT) override; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 539 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 540 | llvm::Value * |
| 541 | EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, |
| 542 | llvm::Value *Base, llvm::Value *MemPtr, |
| 543 | const MemberPointerType *MPT) override; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 544 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 545 | llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 546 | const CastExpr *E, |
| 547 | llvm::Value *Src) override; |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 548 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 549 | llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
| 550 | llvm::Constant *Src) override; |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 551 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 552 | llvm::Value * |
| 553 | EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E, |
| 554 | llvm::Value *&This, llvm::Value *MemPtr, |
| 555 | const MemberPointerType *MPT) override; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 556 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 557 | void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override; |
| 558 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 559 | private: |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 560 | typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 561 | typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy; |
| 562 | typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 563 | /// \brief All the vftables that have been referenced. |
| 564 | VFTablesMapTy VFTablesMap; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 565 | VTablesMapTy VTablesMap; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 566 | |
| 567 | /// \brief This set holds the record decls we've deferred vtable emission for. |
| 568 | llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables; |
| 569 | |
| 570 | |
| 571 | /// \brief All the vbtables which have been referenced. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 572 | llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap; |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 573 | |
| 574 | /// Info on the global variable used to guard initialization of static locals. |
| 575 | /// The BitIndex field is only used for externally invisible declarations. |
| 576 | struct GuardInfo { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 577 | GuardInfo() : Guard(nullptr), BitIndex(0) {} |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 578 | llvm::GlobalVariable *Guard; |
| 579 | unsigned BitIndex; |
| 580 | }; |
| 581 | |
| 582 | /// Map from DeclContext to the current guard variable. We assume that the |
| 583 | /// AST is visited in source code order. |
| 584 | llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 585 | |
| 586 | llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap; |
| 587 | llvm::StructType *BaseClassDescriptorType; |
| 588 | llvm::StructType *ClassHierarchyDescriptorType; |
| 589 | llvm::StructType *CompleteObjectLocatorType; |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | } |
| 593 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 594 | CGCXXABI::RecordArgABI |
| 595 | MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const { |
| 596 | switch (CGM.getTarget().getTriple().getArch()) { |
| 597 | default: |
| 598 | // FIXME: Implement for other architectures. |
| 599 | return RAA_Default; |
| 600 | |
| 601 | case llvm::Triple::x86: |
| 602 | // All record arguments are passed in memory on x86. Decide whether to |
| 603 | // construct the object directly in argument memory, or to construct the |
| 604 | // argument elsewhere and copy the bytes during the call. |
| 605 | |
| 606 | // If C++ prohibits us from making a copy, construct the arguments directly |
| 607 | // into argument memory. |
| 608 | if (!canCopyArgument(RD)) |
| 609 | return RAA_DirectInMemory; |
| 610 | |
| 611 | // Otherwise, construct the argument into a temporary and copy the bytes |
| 612 | // into the outgoing argument memory. |
| 613 | return RAA_Default; |
| 614 | |
| 615 | case llvm::Triple::x86_64: |
| 616 | // Win64 passes objects with non-trivial copy ctors indirectly. |
| 617 | if (RD->hasNonTrivialCopyConstructor()) |
| 618 | return RAA_Indirect; |
| 619 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 620 | // If an object has a destructor, we'd really like to pass it indirectly |
| 621 | // because it allows us to elide copies. Unfortunately, MSVC makes that |
| 622 | // impossible for small types, which it will pass in a single register or |
| 623 | // stack slot. Most objects with dtors are large-ish, so handle that early. |
| 624 | // We can't call out all large objects as being indirect because there are |
| 625 | // multiple x64 calling conventions and the C++ ABI code shouldn't dictate |
| 626 | // how we pass large POD types. |
| 627 | if (RD->hasNonTrivialDestructor() && |
| 628 | getContext().getTypeSize(RD->getTypeForDecl()) > 64) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 629 | return RAA_Indirect; |
| 630 | |
| 631 | // We have a trivial copy constructor or no copy constructors, but we have |
| 632 | // to make sure it isn't deleted. |
| 633 | bool CopyDeleted = false; |
| 634 | for (const CXXConstructorDecl *CD : RD->ctors()) { |
| 635 | if (CD->isCopyConstructor()) { |
| 636 | assert(CD->isTrivial()); |
| 637 | // We had at least one undeleted trivial copy ctor. Return directly. |
| 638 | if (!CD->isDeleted()) |
| 639 | return RAA_Default; |
| 640 | CopyDeleted = true; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | // The trivial copy constructor was deleted. Return indirectly. |
| 645 | if (CopyDeleted) |
| 646 | return RAA_Indirect; |
| 647 | |
| 648 | // There were no copy ctors. Return in RAX. |
| 649 | return RAA_Default; |
| 650 | } |
| 651 | |
| 652 | llvm_unreachable("invalid enum"); |
| 653 | } |
| 654 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 655 | void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF, |
| 656 | const CXXDeleteExpr *DE, |
| 657 | llvm::Value *Ptr, |
| 658 | QualType ElementType, |
| 659 | const CXXDestructorDecl *Dtor) { |
| 660 | // FIXME: Provide a source location here even though there's no |
| 661 | // CXXMemberCallExpr for dtor call. |
| 662 | bool UseGlobalDelete = DE->isGlobalDelete(); |
| 663 | CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; |
| 664 | llvm::Value *MDThis = |
| 665 | EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr); |
| 666 | if (UseGlobalDelete) |
| 667 | CGF.EmitDeleteCall(DE->getOperatorDelete(), MDThis, ElementType); |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 670 | static llvm::Function *getRethrowFn(CodeGenModule &CGM) { |
| 671 | // _CxxThrowException takes two pointer width arguments: a value and a context |
| 672 | // object which points to a TypeInfo object. |
| 673 | llvm::Type *ArgTypes[] = {CGM.Int8PtrTy, CGM.Int8PtrTy}; |
| 674 | llvm::FunctionType *FTy = |
| 675 | llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false); |
| 676 | auto *Fn = cast<llvm::Function>( |
| 677 | CGM.CreateRuntimeFunction(FTy, "_CxxThrowException")); |
| 678 | // _CxxThrowException is stdcall on 32-bit x86 platforms. |
| 679 | if (CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) |
| 680 | Fn->setCallingConv(llvm::CallingConv::X86_StdCall); |
| 681 | return Fn; |
| 682 | } |
| 683 | |
| 684 | void MicrosoftCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) { |
| 685 | llvm::Value *Args[] = {llvm::ConstantPointerNull::get(CGM.Int8PtrTy), |
| 686 | llvm::ConstantPointerNull::get(CGM.Int8PtrTy)}; |
| 687 | auto *Fn = getRethrowFn(CGM); |
| 688 | if (isNoReturn) |
| 689 | CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, Args); |
| 690 | else |
| 691 | CGF.EmitRuntimeCallOrInvoke(Fn, Args); |
| 692 | } |
| 693 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 694 | /// \brief Gets the offset to the virtual base that contains the vfptr for |
| 695 | /// MS-ABI polymorphic types. |
| 696 | static llvm::Value *getPolymorphicOffset(CodeGenFunction &CGF, |
| 697 | const CXXRecordDecl *RD, |
| 698 | llvm::Value *Value) { |
| 699 | const ASTContext &Context = RD->getASTContext(); |
| 700 | for (const CXXBaseSpecifier &Base : RD->vbases()) |
| 701 | if (Context.getASTRecordLayout(Base.getType()->getAsCXXRecordDecl()) |
| 702 | .hasExtendableVFPtr()) |
| 703 | return CGF.CGM.getCXXABI().GetVirtualBaseClassOffset( |
| 704 | CGF, Value, RD, Base.getType()->getAsCXXRecordDecl()); |
| 705 | llvm_unreachable("One of our vbases should be polymorphic."); |
| 706 | } |
| 707 | |
| 708 | static std::pair<llvm::Value *, llvm::Value *> |
| 709 | performBaseAdjustment(CodeGenFunction &CGF, llvm::Value *Value, |
| 710 | QualType SrcRecordTy) { |
| 711 | Value = CGF.Builder.CreateBitCast(Value, CGF.Int8PtrTy); |
| 712 | const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); |
| 713 | |
| 714 | if (CGF.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr()) |
| 715 | return std::make_pair(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 716 | |
| 717 | // Perform a base adjustment. |
| 718 | llvm::Value *Offset = getPolymorphicOffset(CGF, SrcDecl, Value); |
| 719 | Value = CGF.Builder.CreateInBoundsGEP(Value, Offset); |
| 720 | Offset = CGF.Builder.CreateTrunc(Offset, CGF.Int32Ty); |
| 721 | return std::make_pair(Value, Offset); |
| 722 | } |
| 723 | |
| 724 | bool MicrosoftCXXABI::shouldTypeidBeNullChecked(bool IsDeref, |
| 725 | QualType SrcRecordTy) { |
| 726 | const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); |
| 727 | return IsDeref && |
| 728 | !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); |
| 729 | } |
| 730 | |
| 731 | static llvm::CallSite emitRTtypeidCall(CodeGenFunction &CGF, |
| 732 | llvm::Value *Argument) { |
| 733 | llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; |
| 734 | llvm::FunctionType *FTy = |
| 735 | llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false); |
| 736 | llvm::Value *Args[] = {Argument}; |
| 737 | llvm::Constant *Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid"); |
| 738 | return CGF.EmitRuntimeCallOrInvoke(Fn, Args); |
| 739 | } |
| 740 | |
| 741 | void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { |
| 742 | llvm::CallSite Call = |
| 743 | emitRTtypeidCall(CGF, llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 744 | Call.setDoesNotReturn(); |
| 745 | CGF.Builder.CreateUnreachable(); |
| 746 | } |
| 747 | |
| 748 | llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF, |
| 749 | QualType SrcRecordTy, |
| 750 | llvm::Value *ThisPtr, |
| 751 | llvm::Type *StdTypeInfoPtrTy) { |
| 752 | llvm::Value *Offset; |
| 753 | std::tie(ThisPtr, Offset) = performBaseAdjustment(CGF, ThisPtr, SrcRecordTy); |
| 754 | return CGF.Builder.CreateBitCast( |
| 755 | emitRTtypeidCall(CGF, ThisPtr).getInstruction(), StdTypeInfoPtrTy); |
| 756 | } |
| 757 | |
| 758 | bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, |
| 759 | QualType SrcRecordTy) { |
| 760 | const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); |
| 761 | return SrcIsPtr && |
| 762 | !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); |
| 763 | } |
| 764 | |
| 765 | llvm::Value *MicrosoftCXXABI::EmitDynamicCastCall( |
| 766 | CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy, |
| 767 | QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { |
| 768 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
| 769 | |
| 770 | llvm::Value *SrcRTTI = |
| 771 | CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); |
| 772 | llvm::Value *DestRTTI = |
| 773 | CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); |
| 774 | |
| 775 | llvm::Value *Offset; |
| 776 | std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy); |
| 777 | |
| 778 | // PVOID __RTDynamicCast( |
| 779 | // PVOID inptr, |
| 780 | // LONG VfDelta, |
| 781 | // PVOID SrcType, |
| 782 | // PVOID TargetType, |
| 783 | // BOOL isReference) |
| 784 | llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy, |
| 785 | CGF.Int8PtrTy, CGF.Int32Ty}; |
| 786 | llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction( |
| 787 | llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), |
| 788 | "__RTDynamicCast"); |
| 789 | llvm::Value *Args[] = { |
| 790 | Value, Offset, SrcRTTI, DestRTTI, |
| 791 | llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())}; |
| 792 | Value = CGF.EmitRuntimeCallOrInvoke(Function, Args).getInstruction(); |
| 793 | return CGF.Builder.CreateBitCast(Value, DestLTy); |
| 794 | } |
| 795 | |
| 796 | llvm::Value * |
| 797 | MicrosoftCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value, |
| 798 | QualType SrcRecordTy, |
| 799 | QualType DestTy) { |
| 800 | llvm::Value *Offset; |
| 801 | std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy); |
| 802 | |
| 803 | // PVOID __RTCastToVoid( |
| 804 | // PVOID inptr) |
| 805 | llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; |
| 806 | llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction( |
| 807 | llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), |
| 808 | "__RTCastToVoid"); |
| 809 | llvm::Value *Args[] = {Value}; |
| 810 | return CGF.EmitRuntimeCall(Function, Args); |
| 811 | } |
| 812 | |
| 813 | bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { |
| 814 | return false; |
| 815 | } |
| 816 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 817 | llvm::Value *MicrosoftCXXABI::GetVirtualBaseClassOffset( |
| 818 | CodeGenFunction &CGF, llvm::Value *This, const CXXRecordDecl *ClassDecl, |
| 819 | const CXXRecordDecl *BaseClassDecl) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 820 | int64_t VBPtrChars = |
| 821 | getContext().getASTRecordLayout(ClassDecl).getVBPtrOffset().getQuantity(); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 822 | llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 823 | CharUnits IntSize = getContext().getTypeSizeInChars(getContext().IntTy); |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 824 | CharUnits VBTableChars = |
| 825 | IntSize * |
| 826 | CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 827 | llvm::Value *VBTableOffset = |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 828 | llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity()); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 829 | |
| 830 | llvm::Value *VBPtrToNewBase = |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 831 | GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 832 | VBPtrToNewBase = |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 833 | CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 834 | return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase); |
| 835 | } |
| 836 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 837 | bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const { |
| 838 | return isa<CXXConstructorDecl>(GD.getDecl()); |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 841 | static bool isDeletingDtor(GlobalDecl GD) { |
| 842 | return isa<CXXDestructorDecl>(GD.getDecl()) && |
| 843 | GD.getDtorType() == Dtor_Deleting; |
| 844 | } |
| 845 | |
| 846 | bool MicrosoftCXXABI::hasMostDerivedReturn(GlobalDecl GD) const { |
| 847 | return isDeletingDtor(GD); |
| 848 | } |
| 849 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 850 | bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const { |
| 851 | const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); |
| 852 | if (!RD) |
| 853 | return false; |
| 854 | |
| 855 | if (FI.isInstanceMethod()) { |
| 856 | // If it's an instance method, aggregates are always returned indirectly via |
| 857 | // the second parameter. |
| 858 | FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 859 | FI.getReturnInfo().setSRetAfterThis(FI.isInstanceMethod()); |
| 860 | return true; |
| 861 | } else if (!RD->isPOD()) { |
| 862 | // If it's a free function, non-POD types are returned indirectly. |
| 863 | FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | // Otherwise, use the C ABI rules. |
| 868 | return false; |
| 869 | } |
| 870 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 871 | llvm::BasicBlock * |
| 872 | MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, |
| 873 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 874 | llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF); |
| 875 | assert(IsMostDerivedClass && |
| 876 | "ctor for a class with virtual bases must have an implicit parameter"); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 877 | llvm::Value *IsCompleteObject = |
| 878 | CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object"); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 879 | |
| 880 | llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases"); |
| 881 | llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases"); |
| 882 | CGF.Builder.CreateCondBr(IsCompleteObject, |
| 883 | CallVbaseCtorsBB, SkipVbaseCtorsBB); |
| 884 | |
| 885 | CGF.EmitBlock(CallVbaseCtorsBB); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 886 | |
| 887 | // Fill in the vbtable pointers here. |
| 888 | EmitVBPtrStores(CGF, RD); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 889 | |
| 890 | // CGF will put the base ctor calls in this basic block for us later. |
| 891 | |
| 892 | return SkipVbaseCtorsBB; |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Timur Iskhodzhanov | 5bd0d44 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 895 | void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers( |
| 896 | CodeGenFunction &CGF, const CXXRecordDecl *RD) { |
| 897 | // In most cases, an override for a vbase virtual method can adjust |
| 898 | // the "this" parameter by applying a constant offset. |
| 899 | // However, this is not enough while a constructor or a destructor of some |
| 900 | // class X is being executed if all the following conditions are met: |
| 901 | // - X has virtual bases, (1) |
| 902 | // - X overrides a virtual method M of a vbase Y, (2) |
| 903 | // - X itself is a vbase of the most derived class. |
| 904 | // |
| 905 | // If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X |
| 906 | // which holds the extra amount of "this" adjustment we must do when we use |
| 907 | // the X vftables (i.e. during X ctor or dtor). |
| 908 | // Outside the ctors and dtors, the values of vtorDisps are zero. |
| 909 | |
| 910 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 911 | typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets; |
| 912 | const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap(); |
| 913 | CGBuilderTy &Builder = CGF.Builder; |
| 914 | |
| 915 | unsigned AS = |
| 916 | cast<llvm::PointerType>(getThisValue(CGF)->getType())->getAddressSpace(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 917 | llvm::Value *Int8This = nullptr; // Initialize lazily. |
Timur Iskhodzhanov | 5bd0d44 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 918 | |
| 919 | for (VBOffsets::const_iterator I = VBaseMap.begin(), E = VBaseMap.end(); |
| 920 | I != E; ++I) { |
| 921 | if (!I->second.hasVtorDisp()) |
| 922 | continue; |
| 923 | |
Timur Iskhodzhanov | 42e68d5 | 2013-11-13 16:03:43 +0000 | [diff] [blame] | 924 | llvm::Value *VBaseOffset = |
| 925 | GetVirtualBaseClassOffset(CGF, getThisValue(CGF), RD, I->first); |
Timur Iskhodzhanov | 5bd0d44 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 926 | // FIXME: it doesn't look right that we SExt in GetVirtualBaseClassOffset() |
| 927 | // just to Trunc back immediately. |
| 928 | VBaseOffset = Builder.CreateTruncOrBitCast(VBaseOffset, CGF.Int32Ty); |
| 929 | uint64_t ConstantVBaseOffset = |
| 930 | Layout.getVBaseClassOffset(I->first).getQuantity(); |
| 931 | |
| 932 | // vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase). |
| 933 | llvm::Value *VtorDispValue = Builder.CreateSub( |
| 934 | VBaseOffset, llvm::ConstantInt::get(CGM.Int32Ty, ConstantVBaseOffset), |
| 935 | "vtordisp.value"); |
| 936 | |
| 937 | if (!Int8This) |
| 938 | Int8This = Builder.CreateBitCast(getThisValue(CGF), |
| 939 | CGF.Int8Ty->getPointerTo(AS)); |
| 940 | llvm::Value *VtorDispPtr = Builder.CreateInBoundsGEP(Int8This, VBaseOffset); |
| 941 | // vtorDisp is always the 32-bits before the vbase in the class layout. |
| 942 | VtorDispPtr = Builder.CreateConstGEP1_32(VtorDispPtr, -4); |
| 943 | VtorDispPtr = Builder.CreateBitCast( |
| 944 | VtorDispPtr, CGF.Int32Ty->getPointerTo(AS), "vtordisp.ptr"); |
| 945 | |
| 946 | Builder.CreateStore(VtorDispValue, VtorDispPtr); |
| 947 | } |
| 948 | } |
| 949 | |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 950 | void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { |
| 951 | // There's only one constructor type in this ABI. |
| 952 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 953 | } |
| 954 | |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 955 | void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF, |
| 956 | const CXXRecordDecl *RD) { |
| 957 | llvm::Value *ThisInt8Ptr = |
| 958 | CGF.Builder.CreateBitCast(getThisValue(CGF), CGM.Int8PtrTy, "this.int8"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 959 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 960 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 961 | const VBTableGlobals &VBGlobals = enumerateVBTables(RD); |
| 962 | for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { |
| 963 | const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; |
| 964 | llvm::GlobalVariable *GV = VBGlobals.Globals[I]; |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 965 | const ASTRecordLayout &SubobjectLayout = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 966 | CGM.getContext().getASTRecordLayout(VBT->BaseWithVPtr); |
| 967 | CharUnits Offs = VBT->NonVirtualOffset; |
| 968 | Offs += SubobjectLayout.getVBPtrOffset(); |
| 969 | if (VBT->getVBaseWithVPtr()) |
| 970 | Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr()); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 971 | llvm::Value *VBPtr = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 972 | CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs.getQuantity()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 973 | llvm::Value *GVPtr = CGF.Builder.CreateConstInBoundsGEP2_32(GV, 0, 0); |
| 974 | VBPtr = CGF.Builder.CreateBitCast(VBPtr, GVPtr->getType()->getPointerTo(0), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 975 | "vbptr." + VBT->ReusingBase->getName()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 976 | CGF.Builder.CreateStore(GVPtr, VBPtr); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 980 | void |
| 981 | MicrosoftCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T, |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 982 | SmallVectorImpl<CanQualType> &ArgTys) { |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 983 | // TODO: 'for base' flag |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 984 | if (T == StructorType::Deleting) { |
Timur Iskhodzhanov | 1f71f39 | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 985 | // The scalar deleting destructor takes an implicit int parameter. |
| 986 | ArgTys.push_back(CGM.getContext().IntTy); |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 987 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 988 | auto *CD = dyn_cast<CXXConstructorDecl>(MD); |
| 989 | if (!CD) |
| 990 | return; |
| 991 | |
| 992 | // All parameters are already in place except is_most_derived, which goes |
| 993 | // after 'this' if it's variadic and last if it's not. |
| 994 | |
| 995 | const CXXRecordDecl *Class = CD->getParent(); |
| 996 | const FunctionProtoType *FPT = CD->getType()->castAs<FunctionProtoType>(); |
| 997 | if (Class->getNumVBases()) { |
| 998 | if (FPT->isVariadic()) |
| 999 | ArgTys.insert(ArgTys.begin() + 1, CGM.getContext().IntTy); |
| 1000 | else |
| 1001 | ArgTys.push_back(CGM.getContext().IntTy); |
| 1002 | } |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1005 | void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { |
| 1006 | // The TU defining a dtor is only guaranteed to emit a base destructor. All |
| 1007 | // other destructor variants are delegating thunks. |
| 1008 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); |
| 1009 | } |
| 1010 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1011 | CharUnits |
| 1012 | MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) { |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1013 | GD = GD.getCanonicalDecl(); |
| 1014 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1015 | |
| 1016 | GlobalDecl LookupGD = GD; |
| 1017 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1018 | // Complete destructors take a pointer to the complete object as a |
| 1019 | // parameter, thus don't need this adjustment. |
| 1020 | if (GD.getDtorType() == Dtor_Complete) |
| 1021 | return CharUnits(); |
| 1022 | |
| 1023 | // There's no Dtor_Base in vftable but it shares the this adjustment with |
| 1024 | // the deleting one, so look it up instead. |
| 1025 | LookupGD = GlobalDecl(DD, Dtor_Deleting); |
| 1026 | } |
| 1027 | |
| 1028 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 1029 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); |
| 1030 | CharUnits Adjustment = ML.VFPtrOffset; |
| 1031 | |
| 1032 | // Normal virtual instance methods need to adjust from the vfptr that first |
| 1033 | // defined the virtual method to the virtual base subobject, but destructors |
| 1034 | // do not. The vector deleting destructor thunk applies this adjustment for |
| 1035 | // us if necessary. |
| 1036 | if (isa<CXXDestructorDecl>(MD)) |
| 1037 | Adjustment = CharUnits::Zero(); |
| 1038 | |
| 1039 | if (ML.VBase) { |
| 1040 | const ASTRecordLayout &DerivedLayout = |
| 1041 | CGM.getContext().getASTRecordLayout(MD->getParent()); |
| 1042 | Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase); |
| 1043 | } |
| 1044 | |
| 1045 | return Adjustment; |
| 1046 | } |
| 1047 | |
| 1048 | llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall( |
| 1049 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This, bool VirtualCall) { |
| 1050 | if (!VirtualCall) { |
| 1051 | // If the call of a virtual function is not virtual, we just have to |
| 1052 | // compensate for the adjustment the virtual function does in its prologue. |
| 1053 | CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD); |
| 1054 | if (Adjustment.isZero()) |
| 1055 | return This; |
| 1056 | |
| 1057 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 1058 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); |
| 1059 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 1060 | assert(Adjustment.isPositive()); |
| 1061 | return CGF.Builder.CreateConstGEP1_32(This, Adjustment.getQuantity()); |
| 1062 | } |
| 1063 | |
| 1064 | GD = GD.getCanonicalDecl(); |
| 1065 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1066 | |
Timur Iskhodzhanov | 8255274 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1067 | GlobalDecl LookupGD = GD; |
| 1068 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1069 | // Complete dtors take a pointer to the complete object, |
| 1070 | // thus don't need adjustment. |
| 1071 | if (GD.getDtorType() == Dtor_Complete) |
| 1072 | return This; |
| 1073 | |
| 1074 | // There's only Dtor_Deleting in vftable but it shares the this adjustment |
| 1075 | // with the base one, so look up the deleting one instead. |
| 1076 | LookupGD = GlobalDecl(DD, Dtor_Deleting); |
| 1077 | } |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1078 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 1079 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1080 | |
| 1081 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 1082 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); |
Timur Iskhodzhanov | 40aa366 | 2013-11-07 13:34:02 +0000 | [diff] [blame] | 1083 | CharUnits StaticOffset = ML.VFPtrOffset; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1084 | |
| 1085 | // Base destructors expect 'this' to point to the beginning of the base |
| 1086 | // subobject, not the first vfptr that happens to contain the virtual dtor. |
| 1087 | // However, we still need to apply the virtual base adjustment. |
| 1088 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 1089 | StaticOffset = CharUnits::Zero(); |
| 1090 | |
Timur Iskhodzhanov | 8255274 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1091 | if (ML.VBase) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1092 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 1093 | llvm::Value *VBaseOffset = |
| 1094 | GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase); |
| 1095 | This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset); |
Timur Iskhodzhanov | 8255274 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1096 | } |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1097 | if (!StaticOffset.isZero()) { |
| 1098 | assert(StaticOffset.isPositive()); |
| 1099 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
Timur Iskhodzhanov | 6d87e65 | 2013-10-22 18:15:24 +0000 | [diff] [blame] | 1100 | if (ML.VBase) { |
| 1101 | // Non-virtual adjustment might result in a pointer outside the allocated |
| 1102 | // object, e.g. if the final overrider class is laid out after the virtual |
| 1103 | // base that declares a method in the most derived class. |
| 1104 | // FIXME: Update the code that emits this adjustment in thunks prologues. |
| 1105 | This = CGF.Builder.CreateConstGEP1_32(This, StaticOffset.getQuantity()); |
| 1106 | } else { |
| 1107 | This = CGF.Builder.CreateConstInBoundsGEP1_32(This, |
| 1108 | StaticOffset.getQuantity()); |
| 1109 | } |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1110 | } |
| 1111 | return This; |
| 1112 | } |
| 1113 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1114 | void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, |
| 1115 | QualType &ResTy, |
| 1116 | FunctionArgList &Params) { |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1117 | ASTContext &Context = getContext(); |
| 1118 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1119 | assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1120 | if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { |
| 1121 | ImplicitParamDecl *IsMostDerived |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1122 | = ImplicitParamDecl::Create(Context, nullptr, |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1123 | CGF.CurGD.getDecl()->getLocation(), |
| 1124 | &Context.Idents.get("is_most_derived"), |
| 1125 | Context.IntTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1126 | // The 'most_derived' parameter goes second if the ctor is variadic and last |
| 1127 | // if it's not. Dtors can't be variadic. |
| 1128 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 1129 | if (FPT->isVariadic()) |
| 1130 | Params.insert(Params.begin() + 1, IsMostDerived); |
| 1131 | else |
| 1132 | Params.push_back(IsMostDerived); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1133 | getStructorImplicitParamDecl(CGF) = IsMostDerived; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1134 | } else if (isDeletingDtor(CGF.CurGD)) { |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1135 | ImplicitParamDecl *ShouldDelete |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1136 | = ImplicitParamDecl::Create(Context, nullptr, |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1137 | CGF.CurGD.getDecl()->getLocation(), |
| 1138 | &Context.Idents.get("should_call_delete"), |
Timur Iskhodzhanov | 1f71f39 | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 1139 | Context.IntTy); |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1140 | Params.push_back(ShouldDelete); |
| 1141 | getStructorImplicitParamDecl(CGF) = ShouldDelete; |
| 1142 | } |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1145 | llvm::Value *MicrosoftCXXABI::adjustThisParameterInVirtualFunctionPrologue( |
| 1146 | CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1147 | // In this ABI, every virtual function takes a pointer to one of the |
| 1148 | // subobjects that first defines it as the 'this' parameter, rather than a |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1149 | // pointer to the final overrider subobject. Thus, we need to adjust it back |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1150 | // to the final overrider subobject before use. |
| 1151 | // See comments in the MicrosoftVFTableContext implementation for the details. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1152 | CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1153 | if (Adjustment.isZero()) |
| 1154 | return This; |
| 1155 | |
| 1156 | unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); |
| 1157 | llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS), |
| 1158 | *thisTy = This->getType(); |
| 1159 | |
| 1160 | This = CGF.Builder.CreateBitCast(This, charPtrTy); |
| 1161 | assert(Adjustment.isPositive()); |
Timur Iskhodzhanov | 6d87e65 | 2013-10-22 18:15:24 +0000 | [diff] [blame] | 1162 | This = |
| 1163 | CGF.Builder.CreateConstInBoundsGEP1_32(This, -Adjustment.getQuantity()); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1164 | return CGF.Builder.CreateBitCast(This, thisTy); |
| 1165 | } |
| 1166 | |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 1167 | void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 1168 | EmitThisParam(CGF); |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1169 | |
| 1170 | /// If this is a function that the ABI specifies returns 'this', initialize |
| 1171 | /// the return slot to 'this' at the start of the function. |
| 1172 | /// |
| 1173 | /// Unlike the setting of return types, this is done within the ABI |
| 1174 | /// implementation instead of by clients of CGCXXABI because: |
| 1175 | /// 1) getThisValue is currently protected |
| 1176 | /// 2) in theory, an ABI could implement 'this' returns some other way; |
| 1177 | /// HasThisReturn only specifies a contract, not the implementation |
| 1178 | if (HasThisReturn(CGF.CurGD)) |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 1179 | CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1180 | else if (hasMostDerivedReturn(CGF.CurGD)) |
| 1181 | CGF.Builder.CreateStore(CGF.EmitCastToVoidPtr(getThisValue(CGF)), |
| 1182 | CGF.ReturnValue); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1183 | |
| 1184 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
| 1185 | if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { |
| 1186 | assert(getStructorImplicitParamDecl(CGF) && |
| 1187 | "no implicit parameter for a constructor with virtual bases?"); |
| 1188 | getStructorImplicitParamValue(CGF) |
| 1189 | = CGF.Builder.CreateLoad( |
| 1190 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), |
| 1191 | "is_most_derived"); |
| 1192 | } |
| 1193 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1194 | if (isDeletingDtor(CGF.CurGD)) { |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1195 | assert(getStructorImplicitParamDecl(CGF) && |
| 1196 | "no implicit parameter for a deleting destructor?"); |
| 1197 | getStructorImplicitParamValue(CGF) |
| 1198 | = CGF.Builder.CreateLoad( |
| 1199 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), |
| 1200 | "should_call_delete"); |
| 1201 | } |
John McCall | bd31574 | 2012-09-25 08:00:39 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1204 | unsigned MicrosoftCXXABI::addImplicitConstructorArgs( |
| 1205 | CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, |
| 1206 | bool ForVirtualBase, bool Delegating, CallArgList &Args) { |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1207 | assert(Type == Ctor_Complete || Type == Ctor_Base); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1208 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1209 | // Check if we need a 'most_derived' parameter. |
| 1210 | if (!D->getParent()->getNumVBases()) |
| 1211 | return 0; |
| 1212 | |
| 1213 | // Add the 'most_derived' argument second if we are variadic or last if not. |
| 1214 | const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); |
| 1215 | llvm::Value *MostDerivedArg = |
| 1216 | llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete); |
| 1217 | RValue RV = RValue::get(MostDerivedArg); |
| 1218 | if (MostDerivedArg) { |
| 1219 | if (FPT->isVariadic()) |
| 1220 | Args.insert(Args.begin() + 1, |
| 1221 | CallArg(RV, getContext().IntTy, /*needscopy=*/false)); |
| 1222 | else |
| 1223 | Args.add(RV, getContext().IntTy); |
| 1224 | } |
| 1225 | |
| 1226 | return 1; // Added one arg. |
| 1227 | } |
| 1228 | |
| 1229 | void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF, |
| 1230 | const CXXDestructorDecl *DD, |
| 1231 | CXXDtorType Type, bool ForVirtualBase, |
| 1232 | bool Delegating, llvm::Value *This) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1233 | llvm::Value *Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1234 | |
| 1235 | if (DD->isVirtual()) { |
| 1236 | assert(Type != CXXDtorType::Dtor_Deleting && |
| 1237 | "The deleting destructor should only be called via a virtual call"); |
| 1238 | This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type), |
| 1239 | This, false); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1242 | CGF.EmitCXXStructorCall(DD, Callee, ReturnValueSlot(), This, |
| 1243 | /*ImplicitParam=*/nullptr, |
| 1244 | /*ImplicitParamTy=*/QualType(), nullptr, |
| 1245 | getFromDtorType(Type)); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1248 | void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, |
| 1249 | const CXXRecordDecl *RD) { |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1250 | MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1251 | const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1252 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1253 | for (VPtrInfo *Info : VFPtrs) { |
| 1254 | llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1255 | if (VTable->hasInitializer()) |
| 1256 | continue; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1257 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1258 | llvm::Constant *RTTI = getContext().getLangOpts().RTTIData |
| 1259 | ? getMSCompleteObjectLocator(RD, Info) |
| 1260 | : nullptr; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1261 | |
| 1262 | const VTableLayout &VTLayout = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1263 | VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1264 | llvm::Constant *Init = CGVT.CreateVTableInitializer( |
| 1265 | RD, VTLayout.vtable_component_begin(), |
| 1266 | VTLayout.getNumVTableComponents(), VTLayout.vtable_thunk_begin(), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1267 | VTLayout.getNumVTableThunks(), RTTI); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1268 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1269 | VTable->setInitializer(Init); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor( |
| 1274 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, |
| 1275 | const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1276 | NeedsVirtualOffset = (NearestVBase != nullptr); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1277 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1278 | (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); |
| 1279 | VFTableIdTy ID(VTableClass, Base.getBaseOffset()); |
| 1280 | llvm::GlobalValue *VTableAddressPoint = VFTablesMap[ID]; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1281 | if (!VTableAddressPoint) { |
| 1282 | assert(Base.getBase()->getNumVBases() && |
| 1283 | !CGM.getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr()); |
| 1284 | } |
| 1285 | return VTableAddressPoint; |
| 1286 | } |
| 1287 | |
Timur Iskhodzhanov | 11f22a3 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 1288 | static void mangleVFTableName(MicrosoftMangleContext &MangleContext, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1289 | const CXXRecordDecl *RD, const VPtrInfo *VFPtr, |
Timur Iskhodzhanov | 11f22a3 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 1290 | SmallString<256> &Name) { |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1291 | llvm::raw_svector_ostream Out(Name); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1292 | MangleContext.mangleCXXVFTable(RD, VFPtr->MangledPath, Out); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr( |
| 1296 | BaseSubobject Base, const CXXRecordDecl *VTableClass) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1297 | (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); |
| 1298 | VFTableIdTy ID(VTableClass, Base.getBaseOffset()); |
| 1299 | llvm::GlobalValue *VFTable = VFTablesMap[ID]; |
| 1300 | assert(VFTable && "Couldn't find a vftable for the given base?"); |
| 1301 | return VFTable; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, |
| 1305 | CharUnits VPtrOffset) { |
| 1306 | // getAddrOfVTable may return 0 if asked to get an address of a vtable which |
| 1307 | // shouldn't be used in the given record type. We want to cache this result in |
| 1308 | // VFTablesMap, thus a simple zero check is not sufficient. |
| 1309 | VFTableIdTy ID(RD, VPtrOffset); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1310 | VTablesMapTy::iterator I; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1311 | bool Inserted; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1312 | std::tie(I, Inserted) = VTablesMap.insert(std::make_pair(ID, nullptr)); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1313 | if (!Inserted) |
| 1314 | return I->second; |
| 1315 | |
| 1316 | llvm::GlobalVariable *&VTable = I->second; |
| 1317 | |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1318 | MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1319 | const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1320 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1321 | if (DeferredVFTables.insert(RD).second) { |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1322 | // We haven't processed this record type before. |
| 1323 | // Queue up this v-table for possible deferred emission. |
| 1324 | CGM.addDeferredVTable(RD); |
| 1325 | |
| 1326 | #ifndef NDEBUG |
| 1327 | // Create all the vftables at once in order to make sure each vftable has |
| 1328 | // a unique mangled name. |
| 1329 | llvm::StringSet<> ObservedMangledNames; |
| 1330 | for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { |
| 1331 | SmallString<256> Name; |
Timur Iskhodzhanov | 11f22a3 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 1332 | mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1333 | if (!ObservedMangledNames.insert(Name.str()).second) |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1334 | llvm_unreachable("Already saw this mangling before?"); |
| 1335 | } |
| 1336 | #endif |
| 1337 | } |
| 1338 | |
| 1339 | for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1340 | if (VFPtrs[J]->FullOffsetInMDC != VPtrOffset) |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1341 | continue; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1342 | SmallString<256> VFTableName; |
| 1343 | mangleVFTableName(getMangleContext(), RD, VFPtrs[J], VFTableName); |
| 1344 | StringRef VTableName = VFTableName; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1345 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1346 | uint64_t NumVTableSlots = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1347 | VTContext.getVFTableLayout(RD, VFPtrs[J]->FullOffsetInMDC) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1348 | .getNumVTableComponents(); |
| 1349 | llvm::GlobalValue::LinkageTypes VTableLinkage = |
| 1350 | llvm::GlobalValue::ExternalLinkage; |
| 1351 | llvm::ArrayType *VTableType = |
| 1352 | llvm::ArrayType::get(CGM.Int8PtrTy, NumVTableSlots); |
| 1353 | if (getContext().getLangOpts().RTTIData) { |
| 1354 | VTableLinkage = llvm::GlobalValue::PrivateLinkage; |
| 1355 | VTableName = ""; |
| 1356 | } |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1357 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1358 | VTable = CGM.getModule().getNamedGlobal(VFTableName); |
| 1359 | if (!VTable) { |
| 1360 | // Create a backing variable for the contents of VTable. The VTable may |
| 1361 | // or may not include space for a pointer to RTTI data. |
| 1362 | llvm::GlobalValue *VFTable = VTable = new llvm::GlobalVariable( |
| 1363 | CGM.getModule(), VTableType, /*isConstant=*/true, VTableLinkage, |
| 1364 | /*Initializer=*/nullptr, VTableName); |
| 1365 | VTable->setUnnamedAddr(true); |
| 1366 | |
| 1367 | // Only insert a pointer into the VFTable for RTTI data if we are not |
| 1368 | // importing it. We never reference the RTTI data directly so there is no |
| 1369 | // need to make room for it. |
| 1370 | if (getContext().getLangOpts().RTTIData && |
| 1371 | !RD->hasAttr<DLLImportAttr>()) { |
| 1372 | llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0), |
| 1373 | llvm::ConstantInt::get(CGM.IntTy, 1)}; |
| 1374 | // Create a GEP which points just after the first entry in the VFTable, |
| 1375 | // this should be the location of the first virtual method. |
| 1376 | llvm::Constant *VTableGEP = |
| 1377 | llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, GEPIndices); |
| 1378 | // The symbol for the VFTable is an alias to the GEP. It is |
| 1379 | // transparent, to other modules, what the nature of this symbol is; all |
| 1380 | // that matters is that the alias be the address of the first virtual |
| 1381 | // method. |
| 1382 | VFTable = llvm::GlobalAlias::create( |
| 1383 | cast<llvm::SequentialType>(VTableGEP->getType())->getElementType(), |
| 1384 | /*AddressSpace=*/0, llvm::GlobalValue::ExternalLinkage, |
| 1385 | VFTableName.str(), VTableGEP, &CGM.getModule()); |
| 1386 | } else { |
| 1387 | // We don't need a GlobalAlias to be a symbol for the VTable if we won't |
| 1388 | // be referencing any RTTI data. The GlobalVariable will end up being |
| 1389 | // an appropriate definition of the VFTable. |
| 1390 | VTable->setName(VFTableName.str()); |
| 1391 | } |
| 1392 | |
| 1393 | VFTable->setUnnamedAddr(true); |
| 1394 | if (RD->hasAttr<DLLImportAttr>()) |
| 1395 | VFTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 1396 | else if (RD->hasAttr<DLLExportAttr>()) |
| 1397 | VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1398 | |
| 1399 | llvm::GlobalValue::LinkageTypes VFTableLinkage = CGM.getVTableLinkage(RD); |
| 1400 | if (VFTable != VTable) { |
| 1401 | if (llvm::GlobalValue::isAvailableExternallyLinkage(VFTableLinkage)) { |
| 1402 | // AvailableExternally implies that we grabbed the data from another |
| 1403 | // executable. No need to stick the alias in a Comdat. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1404 | } else if (llvm::GlobalValue::isInternalLinkage(VFTableLinkage) || |
| 1405 | llvm::GlobalValue::isWeakODRLinkage(VFTableLinkage) || |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1406 | llvm::GlobalValue::isLinkOnceODRLinkage(VFTableLinkage)) { |
| 1407 | // The alias is going to be dropped into a Comdat, no need to make it |
| 1408 | // weak. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1409 | if (!llvm::GlobalValue::isInternalLinkage(VFTableLinkage)) |
| 1410 | VFTableLinkage = llvm::GlobalValue::ExternalLinkage; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1411 | llvm::Comdat *C = |
| 1412 | CGM.getModule().getOrInsertComdat(VFTable->getName()); |
| 1413 | // We must indicate which VFTable is larger to support linking between |
| 1414 | // translation units which do and do not have RTTI data. The largest |
| 1415 | // VFTable contains the RTTI data; translation units which reference |
| 1416 | // the smaller VFTable always reference it relative to the first |
| 1417 | // virtual method. |
| 1418 | C->setSelectionKind(llvm::Comdat::Largest); |
| 1419 | VTable->setComdat(C); |
| 1420 | } else { |
| 1421 | llvm_unreachable("unexpected linkage for vftable!"); |
| 1422 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1423 | } else { |
| 1424 | if (llvm::GlobalValue::isWeakForLinker(VFTableLinkage)) |
| 1425 | VTable->setComdat( |
| 1426 | CGM.getModule().getOrInsertComdat(VTable->getName())); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1427 | } |
| 1428 | VFTable->setLinkage(VFTableLinkage); |
| 1429 | CGM.setGlobalVisibility(VFTable, RD); |
| 1430 | VFTablesMap[ID] = VFTable; |
| 1431 | } |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | return VTable; |
| 1436 | } |
| 1437 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1438 | llvm::Value *MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, |
| 1439 | GlobalDecl GD, |
| 1440 | llvm::Value *This, |
| 1441 | llvm::Type *Ty) { |
| 1442 | GD = GD.getCanonicalDecl(); |
| 1443 | CGBuilderTy &Builder = CGF.Builder; |
| 1444 | |
| 1445 | Ty = Ty->getPointerTo()->getPointerTo(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1446 | llvm::Value *VPtr = |
| 1447 | adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1448 | llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty); |
| 1449 | |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1450 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 1451 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1452 | llvm::Value *VFuncPtr = |
| 1453 | Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); |
| 1454 | return Builder.CreateLoad(VFuncPtr); |
| 1455 | } |
| 1456 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1457 | llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall( |
| 1458 | CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, |
| 1459 | llvm::Value *This, const CXXMemberCallExpr *CE) { |
| 1460 | assert(CE == nullptr || CE->arg_begin() == CE->arg_end()); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1461 | assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); |
| 1462 | |
| 1463 | // We have only one destructor in the vftable but can get both behaviors |
Timur Iskhodzhanov | 1f71f39 | 2013-08-27 10:38:19 +0000 | [diff] [blame] | 1464 | // by passing an implicit int parameter. |
Timur Iskhodzhanov | 8255274 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1465 | GlobalDecl GD(Dtor, Dtor_Deleting); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1466 | const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( |
| 1467 | Dtor, StructorType::Deleting); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1468 | llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); |
Timur Iskhodzhanov | 8255274 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1469 | llvm::Value *Callee = getVirtualFunctionPointer(CGF, GD, This, Ty); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1470 | |
| 1471 | ASTContext &Context = CGF.getContext(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1472 | llvm::Value *ImplicitParam = llvm::ConstantInt::get( |
| 1473 | llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()), |
| 1474 | DtorType == Dtor_Deleting); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1475 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1476 | This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1477 | RValue RV = CGF.EmitCXXStructorCall(Dtor, Callee, ReturnValueSlot(), This, |
| 1478 | ImplicitParam, Context.IntTy, CE, |
| 1479 | StructorType::Deleting); |
| 1480 | return RV.getScalarVal(); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1483 | const VBTableGlobals & |
| 1484 | MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) { |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1485 | // At this layer, we can key the cache off of a single class, which is much |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1486 | // easier than caching each vbtable individually. |
| 1487 | llvm::DenseMap<const CXXRecordDecl*, VBTableGlobals>::iterator Entry; |
| 1488 | bool Added; |
| 1489 | std::tie(Entry, Added) = |
| 1490 | VBTablesMap.insert(std::make_pair(RD, VBTableGlobals())); |
| 1491 | VBTableGlobals &VBGlobals = Entry->second; |
| 1492 | if (!Added) |
| 1493 | return VBGlobals; |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1494 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1495 | MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); |
| 1496 | VBGlobals.VBTables = &Context.enumerateVBTables(RD); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1497 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1498 | // Cache the globals for all vbtables so we don't have to recompute the |
| 1499 | // mangled names. |
| 1500 | llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); |
| 1501 | for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(), |
| 1502 | E = VBGlobals.VBTables->end(); |
| 1503 | I != E; ++I) { |
| 1504 | VBGlobals.Globals.push_back(getAddrOfVBTable(**I, RD, Linkage)); |
| 1505 | } |
| 1506 | |
| 1507 | return VBGlobals; |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1510 | llvm::Function *MicrosoftCXXABI::EmitVirtualMemPtrThunk( |
| 1511 | const CXXMethodDecl *MD, |
| 1512 | const MicrosoftVTableContext::MethodVFTableLocation &ML) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1513 | assert(!isa<CXXConstructorDecl>(MD) && !isa<CXXDestructorDecl>(MD) && |
| 1514 | "can't form pointers to ctors or virtual dtors"); |
| 1515 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1516 | // Calculate the mangled name. |
| 1517 | SmallString<256> ThunkName; |
| 1518 | llvm::raw_svector_ostream Out(ThunkName); |
| 1519 | getMangleContext().mangleVirtualMemPtrThunk(MD, Out); |
| 1520 | Out.flush(); |
| 1521 | |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1522 | // If the thunk has been generated previously, just return it. |
| 1523 | if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName)) |
| 1524 | return cast<llvm::Function>(GV); |
| 1525 | |
| 1526 | // Create the llvm::Function. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1527 | const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSMemberPointerThunk(MD); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1528 | llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo); |
| 1529 | llvm::Function *ThunkFn = |
| 1530 | llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage, |
| 1531 | ThunkName.str(), &CGM.getModule()); |
| 1532 | assert(ThunkFn->getName() == ThunkName && "name was uniqued!"); |
| 1533 | |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1534 | ThunkFn->setLinkage(MD->isExternallyVisible() |
| 1535 | ? llvm::GlobalValue::LinkOnceODRLinkage |
| 1536 | : llvm::GlobalValue::InternalLinkage); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1537 | if (MD->isExternallyVisible()) |
| 1538 | ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName())); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1539 | |
| 1540 | CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn); |
| 1541 | CGM.SetLLVMFunctionAttributesForDefinition(MD, ThunkFn); |
| 1542 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1543 | // Add the "thunk" attribute so that LLVM knows that the return type is |
| 1544 | // meaningless. These thunks can be used to call functions with differing |
| 1545 | // return types, and the caller is required to cast the prototype |
| 1546 | // appropriately to extract the correct value. |
| 1547 | ThunkFn->addFnAttr("thunk"); |
| 1548 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1549 | // These thunks can be compared, so they are not unnamed. |
| 1550 | ThunkFn->setUnnamedAddr(false); |
| 1551 | |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1552 | // Start codegen. |
| 1553 | CodeGenFunction CGF(CGM); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1554 | CGF.CurGD = GlobalDecl(MD); |
| 1555 | CGF.CurFuncIsThunk = true; |
| 1556 | |
| 1557 | // Build FunctionArgs, but only include the implicit 'this' parameter |
| 1558 | // declaration. |
| 1559 | FunctionArgList FunctionArgs; |
| 1560 | buildThisParam(CGF, FunctionArgs); |
| 1561 | |
| 1562 | // Start defining the function. |
| 1563 | CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo, |
| 1564 | FunctionArgs, MD->getLocation(), SourceLocation()); |
| 1565 | EmitThisParam(CGF); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1566 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1567 | // Load the vfptr and then callee from the vftable. The callee should have |
| 1568 | // adjusted 'this' so that the vfptr is at offset zero. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1569 | llvm::Value *VTable = CGF.GetVTablePtr( |
| 1570 | getThisValue(CGF), ThunkTy->getPointerTo()->getPointerTo()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1571 | llvm::Value *VFuncPtr = |
| 1572 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); |
| 1573 | llvm::Value *Callee = CGF.Builder.CreateLoad(VFuncPtr); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1574 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1575 | CGF.EmitMustTailThunk(MD, getThisValue(CGF), Callee); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 1576 | |
| 1577 | return ThunkFn; |
| 1578 | } |
| 1579 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1580 | void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1581 | const VBTableGlobals &VBGlobals = enumerateVBTables(RD); |
| 1582 | for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { |
| 1583 | const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; |
| 1584 | llvm::GlobalVariable *GV = VBGlobals.Globals[I]; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1585 | if (GV->isDeclaration()) |
| 1586 | emitVBTableDefinition(*VBT, RD, GV); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1590 | llvm::GlobalVariable * |
| 1591 | MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, |
| 1592 | llvm::GlobalVariable::LinkageTypes Linkage) { |
| 1593 | SmallString<256> OutName; |
| 1594 | llvm::raw_svector_ostream Out(OutName); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1595 | getMangleContext().mangleCXXVBTable(RD, VBT.MangledPath, Out); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1596 | Out.flush(); |
| 1597 | StringRef Name = OutName.str(); |
| 1598 | |
| 1599 | llvm::ArrayType *VBTableType = |
| 1600 | llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ReusingBase->getNumVBases()); |
| 1601 | |
| 1602 | assert(!CGM.getModule().getNamedGlobal(Name) && |
| 1603 | "vbtable with this name already exists: mangling bug?"); |
| 1604 | llvm::GlobalVariable *GV = |
| 1605 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, VBTableType, Linkage); |
| 1606 | GV->setUnnamedAddr(true); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1607 | |
| 1608 | if (RD->hasAttr<DLLImportAttr>()) |
| 1609 | GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 1610 | else if (RD->hasAttr<DLLExportAttr>()) |
| 1611 | GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1612 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1613 | if (!GV->hasExternalLinkage()) |
| 1614 | emitVBTableDefinition(VBT, RD, GV); |
| 1615 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1616 | return GV; |
| 1617 | } |
| 1618 | |
| 1619 | void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT, |
| 1620 | const CXXRecordDecl *RD, |
| 1621 | llvm::GlobalVariable *GV) const { |
| 1622 | const CXXRecordDecl *ReusingBase = VBT.ReusingBase; |
| 1623 | |
| 1624 | assert(RD->getNumVBases() && ReusingBase->getNumVBases() && |
| 1625 | "should only emit vbtables for classes with vbtables"); |
| 1626 | |
| 1627 | const ASTRecordLayout &BaseLayout = |
| 1628 | CGM.getContext().getASTRecordLayout(VBT.BaseWithVPtr); |
| 1629 | const ASTRecordLayout &DerivedLayout = |
| 1630 | CGM.getContext().getASTRecordLayout(RD); |
| 1631 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1632 | SmallVector<llvm::Constant *, 4> Offsets(1 + ReusingBase->getNumVBases(), |
| 1633 | nullptr); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1634 | |
| 1635 | // The offset from ReusingBase's vbptr to itself always leads. |
| 1636 | CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset(); |
| 1637 | Offsets[0] = llvm::ConstantInt::get(CGM.IntTy, -VBPtrOffset.getQuantity()); |
| 1638 | |
| 1639 | MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); |
| 1640 | for (const auto &I : ReusingBase->vbases()) { |
| 1641 | const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl(); |
| 1642 | CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase); |
| 1643 | assert(!Offset.isNegative()); |
| 1644 | |
| 1645 | // Make it relative to the subobject vbptr. |
| 1646 | CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset; |
| 1647 | if (VBT.getVBaseWithVPtr()) |
| 1648 | CompleteVBPtrOffset += |
| 1649 | DerivedLayout.getVBaseClassOffset(VBT.getVBaseWithVPtr()); |
| 1650 | Offset -= CompleteVBPtrOffset; |
| 1651 | |
| 1652 | unsigned VBIndex = Context.getVBTableIndex(ReusingBase, VBase); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1653 | assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1654 | Offsets[VBIndex] = llvm::ConstantInt::get(CGM.IntTy, Offset.getQuantity()); |
| 1655 | } |
| 1656 | |
| 1657 | assert(Offsets.size() == |
| 1658 | cast<llvm::ArrayType>(cast<llvm::PointerType>(GV->getType()) |
| 1659 | ->getElementType())->getNumElements()); |
| 1660 | llvm::ArrayType *VBTableType = |
| 1661 | llvm::ArrayType::get(CGM.IntTy, Offsets.size()); |
| 1662 | llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, Offsets); |
| 1663 | GV->setInitializer(Init); |
| 1664 | |
| 1665 | // Set the right visibility. |
| 1666 | CGM.setGlobalVisibility(GV, RD); |
| 1667 | } |
| 1668 | |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1669 | llvm::Value *MicrosoftCXXABI::performThisAdjustment(CodeGenFunction &CGF, |
| 1670 | llvm::Value *This, |
| 1671 | const ThisAdjustment &TA) { |
| 1672 | if (TA.isEmpty()) |
| 1673 | return This; |
| 1674 | |
| 1675 | llvm::Value *V = CGF.Builder.CreateBitCast(This, CGF.Int8PtrTy); |
| 1676 | |
Timur Iskhodzhanov | 58b6db7 | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 1677 | if (!TA.Virtual.isEmpty()) { |
| 1678 | assert(TA.Virtual.Microsoft.VtordispOffset < 0); |
| 1679 | // Adjust the this argument based on the vtordisp value. |
| 1680 | llvm::Value *VtorDispPtr = |
| 1681 | CGF.Builder.CreateConstGEP1_32(V, TA.Virtual.Microsoft.VtordispOffset); |
| 1682 | VtorDispPtr = |
| 1683 | CGF.Builder.CreateBitCast(VtorDispPtr, CGF.Int32Ty->getPointerTo()); |
| 1684 | llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp"); |
| 1685 | V = CGF.Builder.CreateGEP(V, CGF.Builder.CreateNeg(VtorDisp)); |
| 1686 | |
| 1687 | if (TA.Virtual.Microsoft.VBPtrOffset) { |
| 1688 | // If the final overrider is defined in a virtual base other than the one |
| 1689 | // that holds the vfptr, we have to use a vtordispex thunk which looks up |
| 1690 | // the vbtable of the derived class. |
| 1691 | assert(TA.Virtual.Microsoft.VBPtrOffset > 0); |
| 1692 | assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0); |
| 1693 | llvm::Value *VBPtr; |
| 1694 | llvm::Value *VBaseOffset = |
| 1695 | GetVBaseOffsetFromVBPtr(CGF, V, -TA.Virtual.Microsoft.VBPtrOffset, |
| 1696 | TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr); |
| 1697 | V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); |
| 1698 | } |
| 1699 | } |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1700 | |
| 1701 | if (TA.NonVirtual) { |
| 1702 | // Non-virtual adjustment might result in a pointer outside the allocated |
| 1703 | // object, e.g. if the final overrider class is laid out after the virtual |
| 1704 | // base that declares a method in the most derived class. |
| 1705 | V = CGF.Builder.CreateConstGEP1_32(V, TA.NonVirtual); |
| 1706 | } |
| 1707 | |
| 1708 | // Don't need to bitcast back, the call CodeGen will handle this. |
| 1709 | return V; |
| 1710 | } |
| 1711 | |
| 1712 | llvm::Value * |
| 1713 | MicrosoftCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
| 1714 | const ReturnAdjustment &RA) { |
| 1715 | if (RA.isEmpty()) |
| 1716 | return Ret; |
| 1717 | |
| 1718 | llvm::Value *V = CGF.Builder.CreateBitCast(Ret, CGF.Int8PtrTy); |
| 1719 | |
| 1720 | if (RA.Virtual.Microsoft.VBIndex) { |
| 1721 | assert(RA.Virtual.Microsoft.VBIndex > 0); |
| 1722 | int32_t IntSize = |
| 1723 | getContext().getTypeSizeInChars(getContext().IntTy).getQuantity(); |
| 1724 | llvm::Value *VBPtr; |
| 1725 | llvm::Value *VBaseOffset = |
| 1726 | GetVBaseOffsetFromVBPtr(CGF, V, RA.Virtual.Microsoft.VBPtrOffset, |
| 1727 | IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr); |
| 1728 | V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset); |
| 1729 | } |
| 1730 | |
| 1731 | if (RA.NonVirtual) |
| 1732 | V = CGF.Builder.CreateConstInBoundsGEP1_32(V, RA.NonVirtual); |
| 1733 | |
| 1734 | // Cast back to the original type. |
| 1735 | return CGF.Builder.CreateBitCast(V, Ret->getType()); |
| 1736 | } |
| 1737 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1738 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr, |
| 1739 | QualType elementType) { |
| 1740 | // Microsoft seems to completely ignore the possibility of a |
| 1741 | // two-argument usual deallocation function. |
| 1742 | return elementType.isDestructedType(); |
| 1743 | } |
| 1744 | |
| 1745 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) { |
| 1746 | // Microsoft seems to completely ignore the possibility of a |
| 1747 | // two-argument usual deallocation function. |
| 1748 | return expr->getAllocatedType().isDestructedType(); |
| 1749 | } |
| 1750 | |
| 1751 | CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) { |
| 1752 | // The array cookie is always a size_t; we then pad that out to the |
| 1753 | // alignment of the element type. |
| 1754 | ASTContext &Ctx = getContext(); |
| 1755 | return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()), |
| 1756 | Ctx.getTypeAlignInChars(type)); |
| 1757 | } |
| 1758 | |
| 1759 | llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 1760 | llvm::Value *allocPtr, |
| 1761 | CharUnits cookieSize) { |
Micah Villmow | 956a5a1 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1762 | unsigned AS = allocPtr->getType()->getPointerAddressSpace(); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1763 | llvm::Value *numElementsPtr = |
| 1764 | CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS)); |
| 1765 | return CGF.Builder.CreateLoad(numElementsPtr); |
| 1766 | } |
| 1767 | |
| 1768 | llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 1769 | llvm::Value *newPtr, |
| 1770 | llvm::Value *numElements, |
| 1771 | const CXXNewExpr *expr, |
| 1772 | QualType elementType) { |
| 1773 | assert(requiresArrayCookie(expr)); |
| 1774 | |
| 1775 | // The size of the cookie. |
| 1776 | CharUnits cookieSize = getArrayCookieSizeImpl(elementType); |
| 1777 | |
| 1778 | // Compute an offset to the cookie. |
| 1779 | llvm::Value *cookiePtr = newPtr; |
| 1780 | |
| 1781 | // Write the number of elements into the appropriate slot. |
Micah Villmow | 956a5a1 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1782 | unsigned AS = newPtr->getType()->getPointerAddressSpace(); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1783 | llvm::Value *numElementsPtr |
| 1784 | = CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS)); |
| 1785 | CGF.Builder.CreateStore(numElements, numElementsPtr); |
| 1786 | |
| 1787 | // Finally, compute a pointer to the actual data buffer by skipping |
| 1788 | // over the cookie completely. |
| 1789 | return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, |
| 1790 | cookieSize.getQuantity()); |
| 1791 | } |
| 1792 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1793 | static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD, |
| 1794 | llvm::Constant *Dtor, |
| 1795 | llvm::Constant *Addr) { |
| 1796 | // Create a function which calls the destructor. |
| 1797 | llvm::Constant *DtorStub = CGF.createAtExitStub(VD, Dtor, Addr); |
| 1798 | |
| 1799 | // extern "C" int __tlregdtor(void (*f)(void)); |
| 1800 | llvm::FunctionType *TLRegDtorTy = llvm::FunctionType::get( |
| 1801 | CGF.IntTy, DtorStub->getType(), /*IsVarArg=*/false); |
| 1802 | |
| 1803 | llvm::Constant *TLRegDtor = |
| 1804 | CGF.CGM.CreateRuntimeFunction(TLRegDtorTy, "__tlregdtor"); |
| 1805 | if (llvm::Function *TLRegDtorFn = dyn_cast<llvm::Function>(TLRegDtor)) |
| 1806 | TLRegDtorFn->setDoesNotThrow(); |
| 1807 | |
| 1808 | CGF.EmitNounwindRuntimeCall(TLRegDtor, DtorStub); |
| 1809 | } |
| 1810 | |
| 1811 | void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, |
| 1812 | llvm::Constant *Dtor, |
| 1813 | llvm::Constant *Addr) { |
| 1814 | if (D.getTLSKind()) |
| 1815 | return emitGlobalDtorWithTLRegDtor(CGF, D, Dtor, Addr); |
| 1816 | |
| 1817 | // The default behavior is to use atexit. |
| 1818 | CGF.registerGlobalDtorWithAtExit(D, Dtor, Addr); |
| 1819 | } |
| 1820 | |
| 1821 | void MicrosoftCXXABI::EmitThreadLocalInitFuncs( |
| 1822 | CodeGenModule &CGM, |
| 1823 | ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> |
| 1824 | CXXThreadLocals, |
| 1825 | ArrayRef<llvm::Function *> CXXThreadLocalInits, |
| 1826 | ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) { |
| 1827 | // This will create a GV in the .CRT$XDU section. It will point to our |
| 1828 | // initialization function. The CRT will call all of these function |
| 1829 | // pointers at start-up time and, eventually, at thread-creation time. |
| 1830 | auto AddToXDU = [&CGM](llvm::Function *InitFunc) { |
| 1831 | llvm::GlobalVariable *InitFuncPtr = new llvm::GlobalVariable( |
| 1832 | CGM.getModule(), InitFunc->getType(), /*IsConstant=*/true, |
| 1833 | llvm::GlobalVariable::InternalLinkage, InitFunc, |
| 1834 | Twine(InitFunc->getName(), "$initializer$")); |
| 1835 | InitFuncPtr->setSection(".CRT$XDU"); |
| 1836 | // This variable has discardable linkage, we have to add it to @llvm.used to |
| 1837 | // ensure it won't get discarded. |
| 1838 | CGM.addUsedGlobal(InitFuncPtr); |
| 1839 | return InitFuncPtr; |
| 1840 | }; |
| 1841 | |
| 1842 | std::vector<llvm::Function *> NonComdatInits; |
| 1843 | for (size_t I = 0, E = CXXThreadLocalInitVars.size(); I != E; ++I) { |
| 1844 | llvm::GlobalVariable *GV = CXXThreadLocalInitVars[I]; |
| 1845 | llvm::Function *F = CXXThreadLocalInits[I]; |
| 1846 | |
| 1847 | // If the GV is already in a comdat group, then we have to join it. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1848 | if (llvm::Comdat *C = GV->getComdat()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1849 | AddToXDU(F)->setComdat(C); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1850 | else |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1851 | NonComdatInits.push_back(F); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | if (!NonComdatInits.empty()) { |
| 1855 | llvm::FunctionType *FTy = |
| 1856 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
| 1857 | llvm::Function *InitFunc = CGM.CreateGlobalInitOrDestructFunction( |
| 1858 | FTy, "__tls_init", SourceLocation(), |
| 1859 | /*TLS=*/true); |
| 1860 | CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, NonComdatInits); |
| 1861 | |
| 1862 | AddToXDU(InitFunc); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | LValue MicrosoftCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, |
| 1867 | const VarDecl *VD, |
| 1868 | QualType LValType) { |
| 1869 | CGF.CGM.ErrorUnsupported(VD, "thread wrappers"); |
| 1870 | return LValue(); |
| 1871 | } |
| 1872 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1873 | void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1874 | llvm::GlobalVariable *GV, |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1875 | bool PerformInit) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1876 | // MSVC only uses guards for static locals. |
| 1877 | if (!D.isStaticLocal()) { |
| 1878 | assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()); |
| 1879 | // GlobalOpt is allowed to discard the initializer, so use linkonce_odr. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1880 | llvm::Function *F = CGF.CurFn; |
| 1881 | F->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); |
| 1882 | F->setComdat(CGM.getModule().getOrInsertComdat(F->getName())); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1883 | CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); |
| 1884 | return; |
| 1885 | } |
| 1886 | |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1887 | // MSVC always uses an i32 bitfield to guard initialization, which is *not* |
| 1888 | // threadsafe. Since the user may be linking in inline functions compiled by |
| 1889 | // cl.exe, there's no reason to provide a false sense of security by using |
| 1890 | // critical sections here. |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1891 | |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1892 | if (D.getTLSKind()) |
| 1893 | CGM.ErrorUnsupported(&D, "dynamic TLS initialization"); |
| 1894 | |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1895 | CGBuilderTy &Builder = CGF.Builder; |
| 1896 | llvm::IntegerType *GuardTy = CGF.Int32Ty; |
| 1897 | llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0); |
| 1898 | |
| 1899 | // Get the guard variable for this function if we have one already. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1900 | GuardInfo *GI = &GuardVariableMap[D.getDeclContext()]; |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1901 | |
| 1902 | unsigned BitIndex; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1903 | if (D.isStaticLocal() && D.isExternallyVisible()) { |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1904 | // Externally visible variables have to be numbered in Sema to properly |
| 1905 | // handle unreachable VarDecls. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1906 | BitIndex = getContext().getStaticLocalNumber(&D); |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1907 | assert(BitIndex > 0); |
| 1908 | BitIndex--; |
| 1909 | } else { |
| 1910 | // Non-externally visible variables are numbered here in CodeGen. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1911 | BitIndex = GI->BitIndex++; |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | if (BitIndex >= 32) { |
| 1915 | if (D.isExternallyVisible()) |
| 1916 | ErrorUnsupportedABI(CGF, "more than 32 guarded initializations"); |
| 1917 | BitIndex %= 32; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1918 | GI->Guard = nullptr; |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | // Lazily create the i32 bitfield for this function. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1922 | if (!GI->Guard) { |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1923 | // Mangle the name for the guard. |
| 1924 | SmallString<256> GuardName; |
| 1925 | { |
| 1926 | llvm::raw_svector_ostream Out(GuardName); |
| 1927 | getMangleContext().mangleStaticGuardVariable(&D, Out); |
| 1928 | Out.flush(); |
| 1929 | } |
| 1930 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1931 | // Create the guard variable with a zero-initializer. Just absorb linkage, |
| 1932 | // visibility and dll storage class from the guarded variable. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1933 | GI->Guard = |
| 1934 | new llvm::GlobalVariable(CGM.getModule(), GuardTy, false, |
| 1935 | GV->getLinkage(), Zero, GuardName.str()); |
| 1936 | GI->Guard->setVisibility(GV->getVisibility()); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1937 | GI->Guard->setDLLStorageClass(GV->getDLLStorageClass()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1938 | if (GI->Guard->isWeakForLinker()) |
| 1939 | GI->Guard->setComdat( |
| 1940 | CGM.getModule().getOrInsertComdat(GI->Guard->getName())); |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1941 | } else { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1942 | assert(GI->Guard->getLinkage() == GV->getLinkage() && |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1943 | "static local from the same function had different linkage"); |
| 1944 | } |
| 1945 | |
| 1946 | // Pseudo code for the test: |
| 1947 | // if (!(GuardVar & MyGuardBit)) { |
| 1948 | // GuardVar |= MyGuardBit; |
| 1949 | // ... initialize the object ...; |
| 1950 | // } |
| 1951 | |
| 1952 | // Test our bit from the guard variable. |
| 1953 | llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1U << BitIndex); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1954 | llvm::LoadInst *LI = Builder.CreateLoad(GI->Guard); |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1955 | llvm::Value *IsInitialized = |
| 1956 | Builder.CreateICmpNE(Builder.CreateAnd(LI, Bit), Zero); |
| 1957 | llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); |
| 1958 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); |
| 1959 | Builder.CreateCondBr(IsInitialized, EndBlock, InitBlock); |
| 1960 | |
| 1961 | // Set our bit in the guard variable and emit the initializer and add a global |
| 1962 | // destructor if appropriate. |
| 1963 | CGF.EmitBlock(InitBlock); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1964 | Builder.CreateStore(Builder.CreateOr(LI, Bit), GI->Guard); |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1965 | CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); |
| 1966 | Builder.CreateBr(EndBlock); |
| 1967 | |
| 1968 | // Continue. |
| 1969 | CGF.EmitBlock(EndBlock); |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1972 | bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 1973 | // Null-ness for function memptrs only depends on the first field, which is |
| 1974 | // the function pointer. The rest don't matter, so we can zero initialize. |
| 1975 | if (MPT->isMemberFunctionPointer()) |
| 1976 | return true; |
| 1977 | |
| 1978 | // The virtual base adjustment field is always -1 for null, so if we have one |
| 1979 | // we can't zero initialize. The field offset is sometimes also -1 if 0 is a |
| 1980 | // valid field offset. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1981 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 1982 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
| 1983 | return (!MSInheritanceAttr::hasVBTableOffsetField(Inheritance) && |
| 1984 | RD->nullFieldOffsetIsZero()); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | llvm::Type * |
| 1988 | MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1989 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 1990 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1991 | llvm::SmallVector<llvm::Type *, 4> fields; |
| 1992 | if (MPT->isMemberFunctionPointer()) |
| 1993 | fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk |
| 1994 | else |
| 1995 | fields.push_back(CGM.IntTy); // FieldOffset |
| 1996 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1997 | if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), |
| 1998 | Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 1999 | fields.push_back(CGM.IntTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2000 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2001 | fields.push_back(CGM.IntTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2002 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2003 | fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset |
| 2004 | |
| 2005 | if (fields.size() == 1) |
| 2006 | return fields[0]; |
| 2007 | return llvm::StructType::get(CGM.getLLVMContext(), fields); |
| 2008 | } |
| 2009 | |
| 2010 | void MicrosoftCXXABI:: |
| 2011 | GetNullMemberPointerFields(const MemberPointerType *MPT, |
| 2012 | llvm::SmallVectorImpl<llvm::Constant *> &fields) { |
| 2013 | assert(fields.empty()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2014 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 2015 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2016 | if (MPT->isMemberFunctionPointer()) { |
| 2017 | // FunctionPointerOrVirtualThunk |
| 2018 | fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 2019 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2020 | if (RD->nullFieldOffsetIsZero()) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2021 | fields.push_back(getZeroInt()); // FieldOffset |
| 2022 | else |
| 2023 | fields.push_back(getAllOnesInt()); // FieldOffset |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2024 | } |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2025 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2026 | if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), |
| 2027 | Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2028 | fields.push_back(getZeroInt()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2029 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2030 | fields.push_back(getZeroInt()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2031 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2032 | fields.push_back(getAllOnesInt()); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | llvm::Constant * |
| 2036 | MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2037 | llvm::SmallVector<llvm::Constant *, 4> fields; |
| 2038 | GetNullMemberPointerFields(MPT, fields); |
| 2039 | if (fields.size() == 1) |
| 2040 | return fields[0]; |
| 2041 | llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields); |
| 2042 | assert(Res->getType() == ConvertMemberPointerType(MPT)); |
| 2043 | return Res; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | llvm::Constant * |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2047 | MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField, |
| 2048 | bool IsMemberFunction, |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2049 | const CXXRecordDecl *RD, |
| 2050 | CharUnits NonVirtualBaseAdjustment) |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2051 | { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2052 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2053 | |
| 2054 | // Single inheritance class member pointer are represented as scalars instead |
| 2055 | // of aggregates. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2056 | if (MSInheritanceAttr::hasOnlyOneField(IsMemberFunction, Inheritance)) |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2057 | return FirstField; |
| 2058 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2059 | llvm::SmallVector<llvm::Constant *, 4> fields; |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2060 | fields.push_back(FirstField); |
| 2061 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2062 | if (MSInheritanceAttr::hasNVOffsetField(IsMemberFunction, Inheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2063 | fields.push_back(llvm::ConstantInt::get( |
| 2064 | CGM.IntTy, NonVirtualBaseAdjustment.getQuantity())); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2065 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2066 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) { |
Reid Kleckner | 42090d6 | 2013-10-15 01:18:02 +0000 | [diff] [blame] | 2067 | CharUnits Offs = CharUnits::Zero(); |
| 2068 | if (RD->getNumVBases()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2069 | Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); |
Reid Kleckner | 42090d6 | 2013-10-15 01:18:02 +0000 | [diff] [blame] | 2070 | fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity())); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2071 | } |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2072 | |
| 2073 | // The rest of the fields are adjusted by conversions to a more derived class. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2074 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2075 | fields.push_back(getZeroInt()); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2076 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2077 | return llvm::ConstantStruct::getAnon(fields); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2080 | llvm::Constant * |
| 2081 | MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, |
| 2082 | CharUnits offset) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2083 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2084 | llvm::Constant *FirstField = |
| 2085 | llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity()); |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2086 | return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD, |
| 2087 | CharUnits::Zero()); |
| 2088 | } |
| 2089 | |
| 2090 | llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
| 2091 | return BuildMemberPointer(MD->getParent(), MD, CharUnits::Zero()); |
| 2092 | } |
| 2093 | |
| 2094 | llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP, |
| 2095 | QualType MPType) { |
| 2096 | const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); |
| 2097 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
| 2098 | if (!MPD) |
| 2099 | return EmitNullMemberPointer(MPT); |
| 2100 | |
| 2101 | CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); |
| 2102 | |
| 2103 | // FIXME PR15713: Support virtual inheritance paths. |
| 2104 | |
| 2105 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2106 | return BuildMemberPointer(MPT->getMostRecentCXXRecordDecl(), MD, |
| 2107 | ThisAdjustment); |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2108 | |
| 2109 | CharUnits FieldOffset = |
| 2110 | getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); |
| 2111 | return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | llvm::Constant * |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2115 | MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD, |
| 2116 | const CXXMethodDecl *MD, |
| 2117 | CharUnits NonVirtualBaseAdjustment) { |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2118 | assert(MD->isInstance() && "Member function must not be static!"); |
| 2119 | MD = MD->getCanonicalDecl(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2120 | RD = RD->getMostRecentDecl(); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2121 | CodeGenTypes &Types = CGM.getTypes(); |
| 2122 | |
| 2123 | llvm::Constant *FirstField; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2124 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2125 | if (!MD->isVirtual()) { |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2126 | llvm::Type *Ty; |
| 2127 | // Check whether the function has a computable LLVM signature. |
| 2128 | if (Types.isFuncTypeConvertible(FPT)) { |
| 2129 | // The function has a computable LLVM signature; use the correct type. |
| 2130 | Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); |
| 2131 | } else { |
| 2132 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 2133 | // function type is incomplete. |
| 2134 | Ty = CGM.PtrDiffTy; |
| 2135 | } |
| 2136 | FirstField = CGM.GetAddrOfFunction(MD, Ty); |
| 2137 | FirstField = llvm::ConstantExpr::getBitCast(FirstField, CGM.VoidPtrTy); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2138 | } else { |
| 2139 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 2140 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2141 | if (!CGM.getTypes().isFuncTypeConvertible( |
| 2142 | MD->getType()->castAs<FunctionType>())) { |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2143 | CGM.ErrorUnsupported(MD, "pointer to virtual member function with " |
| 2144 | "incomplete return or parameter type"); |
| 2145 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2146 | } else if (FPT->getCallConv() == CC_X86FastCall) { |
| 2147 | CGM.ErrorUnsupported(MD, "pointer to fastcall virtual member function"); |
| 2148 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2149 | } else if (ML.VBase) { |
| 2150 | CGM.ErrorUnsupported(MD, "pointer to virtual member function overriding " |
| 2151 | "member function in virtual base class"); |
| 2152 | FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); |
| 2153 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2154 | llvm::Function *Thunk = EmitVirtualMemPtrThunk(MD, ML); |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2155 | FirstField = llvm::ConstantExpr::getBitCast(Thunk, CGM.VoidPtrTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2156 | // Include the vfptr adjustment if the method is in a non-primary vftable. |
| 2157 | NonVirtualBaseAdjustment += ML.VFPtrOffset; |
Hans Wennborg | 93b717a | 2013-11-15 17:24:45 +0000 | [diff] [blame] | 2158 | } |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | // The rest of the fields are common with data member pointers. |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2162 | return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD, |
| 2163 | NonVirtualBaseAdjustment); |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Reid Kleckner | 3d2f000 | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 2166 | /// Member pointers are the same if they're either bitwise identical *or* both |
| 2167 | /// null. Null-ness for function members is determined by the first field, |
| 2168 | /// while for data member pointers we must compare all fields. |
| 2169 | llvm::Value * |
| 2170 | MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 2171 | llvm::Value *L, |
| 2172 | llvm::Value *R, |
| 2173 | const MemberPointerType *MPT, |
| 2174 | bool Inequality) { |
| 2175 | CGBuilderTy &Builder = CGF.Builder; |
| 2176 | |
| 2177 | // Handle != comparisons by switching the sense of all boolean operations. |
| 2178 | llvm::ICmpInst::Predicate Eq; |
| 2179 | llvm::Instruction::BinaryOps And, Or; |
| 2180 | if (Inequality) { |
| 2181 | Eq = llvm::ICmpInst::ICMP_NE; |
| 2182 | And = llvm::Instruction::Or; |
| 2183 | Or = llvm::Instruction::And; |
| 2184 | } else { |
| 2185 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 2186 | And = llvm::Instruction::And; |
| 2187 | Or = llvm::Instruction::Or; |
| 2188 | } |
| 2189 | |
| 2190 | // If this is a single field member pointer (single inheritance), this is a |
| 2191 | // single icmp. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2192 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 2193 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
| 2194 | if (MSInheritanceAttr::hasOnlyOneField(MPT->isMemberFunctionPointer(), |
| 2195 | Inheritance)) |
Reid Kleckner | 3d2f000 | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 2196 | return Builder.CreateICmp(Eq, L, R); |
| 2197 | |
| 2198 | // Compare the first field. |
| 2199 | llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0"); |
| 2200 | llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0"); |
| 2201 | llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first"); |
| 2202 | |
| 2203 | // Compare everything other than the first field. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2204 | llvm::Value *Res = nullptr; |
Reid Kleckner | 3d2f000 | 2013-04-30 20:15:14 +0000 | [diff] [blame] | 2205 | llvm::StructType *LType = cast<llvm::StructType>(L->getType()); |
| 2206 | for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) { |
| 2207 | llvm::Value *LF = Builder.CreateExtractValue(L, I); |
| 2208 | llvm::Value *RF = Builder.CreateExtractValue(R, I); |
| 2209 | llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest"); |
| 2210 | if (Res) |
| 2211 | Res = Builder.CreateBinOp(And, Res, Cmp); |
| 2212 | else |
| 2213 | Res = Cmp; |
| 2214 | } |
| 2215 | |
| 2216 | // Check if the first field is 0 if this is a function pointer. |
| 2217 | if (MPT->isMemberFunctionPointer()) { |
| 2218 | // (l1 == r1 && ...) || l0 == 0 |
| 2219 | llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType()); |
| 2220 | llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero"); |
| 2221 | Res = Builder.CreateBinOp(Or, Res, IsZero); |
| 2222 | } |
| 2223 | |
| 2224 | // Combine the comparison of the first field, which must always be true for |
| 2225 | // this comparison to succeeed. |
| 2226 | return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp"); |
| 2227 | } |
| 2228 | |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2229 | llvm::Value * |
| 2230 | MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 2231 | llvm::Value *MemPtr, |
| 2232 | const MemberPointerType *MPT) { |
| 2233 | CGBuilderTy &Builder = CGF.Builder; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2234 | llvm::SmallVector<llvm::Constant *, 4> fields; |
| 2235 | // We only need one field for member functions. |
| 2236 | if (MPT->isMemberFunctionPointer()) |
| 2237 | fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); |
| 2238 | else |
| 2239 | GetNullMemberPointerFields(MPT, fields); |
| 2240 | assert(!fields.empty()); |
| 2241 | llvm::Value *FirstField = MemPtr; |
| 2242 | if (MemPtr->getType()->isStructTy()) |
| 2243 | FirstField = Builder.CreateExtractValue(MemPtr, 0); |
| 2244 | llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0"); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2245 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2246 | // For function member pointers, we only need to test the function pointer |
| 2247 | // field. The other fields if any can be garbage. |
| 2248 | if (MPT->isMemberFunctionPointer()) |
| 2249 | return Res; |
| 2250 | |
| 2251 | // Otherwise, emit a series of compares and combine the results. |
| 2252 | for (int I = 1, E = fields.size(); I < E; ++I) { |
| 2253 | llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I); |
| 2254 | llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2255 | Res = Builder.CreateOr(Res, Next, "memptr.tobool"); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2256 | } |
| 2257 | return Res; |
| 2258 | } |
| 2259 | |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2260 | bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT, |
| 2261 | llvm::Constant *Val) { |
| 2262 | // Function pointers are null if the pointer in the first field is null. |
| 2263 | if (MPT->isMemberFunctionPointer()) { |
| 2264 | llvm::Constant *FirstField = Val->getType()->isStructTy() ? |
| 2265 | Val->getAggregateElement(0U) : Val; |
| 2266 | return FirstField->isNullValue(); |
| 2267 | } |
| 2268 | |
| 2269 | // If it's not a function pointer and it's zero initializable, we can easily |
| 2270 | // check zero. |
| 2271 | if (isZeroInitializable(MPT) && Val->isNullValue()) |
| 2272 | return true; |
| 2273 | |
| 2274 | // Otherwise, break down all the fields for comparison. Hopefully these |
| 2275 | // little Constants are reused, while a big null struct might not be. |
| 2276 | llvm::SmallVector<llvm::Constant *, 4> Fields; |
| 2277 | GetNullMemberPointerFields(MPT, Fields); |
| 2278 | if (Fields.size() == 1) { |
| 2279 | assert(Val->getType()->isIntegerTy()); |
| 2280 | return Val == Fields[0]; |
| 2281 | } |
| 2282 | |
| 2283 | unsigned I, E; |
| 2284 | for (I = 0, E = Fields.size(); I != E; ++I) { |
| 2285 | if (Val->getAggregateElement(I) != Fields[I]) |
| 2286 | break; |
| 2287 | } |
| 2288 | return I == E; |
| 2289 | } |
| 2290 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2291 | llvm::Value * |
| 2292 | MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, |
| 2293 | llvm::Value *This, |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2294 | llvm::Value *VBPtrOffset, |
Timur Iskhodzhanov | 0e4f559 | 2013-10-27 17:10:27 +0000 | [diff] [blame] | 2295 | llvm::Value *VBTableOffset, |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2296 | llvm::Value **VBPtrOut) { |
| 2297 | CGBuilderTy &Builder = CGF.Builder; |
| 2298 | // Load the vbtable pointer from the vbptr in the instance. |
| 2299 | This = Builder.CreateBitCast(This, CGM.Int8PtrTy); |
| 2300 | llvm::Value *VBPtr = |
| 2301 | Builder.CreateInBoundsGEP(This, VBPtrOffset, "vbptr"); |
| 2302 | if (VBPtrOut) *VBPtrOut = VBPtr; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2303 | VBPtr = Builder.CreateBitCast(VBPtr, |
| 2304 | CGM.Int32Ty->getPointerTo(0)->getPointerTo(0)); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2305 | llvm::Value *VBTable = Builder.CreateLoad(VBPtr, "vbtable"); |
| 2306 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2307 | // Translate from byte offset to table index. It improves analyzability. |
| 2308 | llvm::Value *VBTableIndex = Builder.CreateAShr( |
| 2309 | VBTableOffset, llvm::ConstantInt::get(VBTableOffset->getType(), 2), |
| 2310 | "vbtindex", /*isExact=*/true); |
| 2311 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2312 | // Load an i32 offset from the vb-table. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2313 | llvm::Value *VBaseOffs = Builder.CreateInBoundsGEP(VBTable, VBTableIndex); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2314 | VBaseOffs = Builder.CreateBitCast(VBaseOffs, CGM.Int32Ty->getPointerTo(0)); |
| 2315 | return Builder.CreateLoad(VBaseOffs, "vbase_offs"); |
| 2316 | } |
| 2317 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2318 | // Returns an adjusted base cast to i8*, since we do more address arithmetic on |
| 2319 | // it. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2320 | llvm::Value *MicrosoftCXXABI::AdjustVirtualBase( |
| 2321 | CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD, |
| 2322 | llvm::Value *Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) { |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2323 | CGBuilderTy &Builder = CGF.Builder; |
| 2324 | Base = Builder.CreateBitCast(Base, CGM.Int8PtrTy); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2325 | llvm::BasicBlock *OriginalBB = nullptr; |
| 2326 | llvm::BasicBlock *SkipAdjustBB = nullptr; |
| 2327 | llvm::BasicBlock *VBaseAdjustBB = nullptr; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2328 | |
| 2329 | // In the unspecified inheritance model, there might not be a vbtable at all, |
| 2330 | // in which case we need to skip the virtual base lookup. If there is a |
| 2331 | // vbtable, the first entry is a no-op entry that gives back the original |
| 2332 | // base, so look for a virtual base adjustment offset of zero. |
| 2333 | if (VBPtrOffset) { |
| 2334 | OriginalBB = Builder.GetInsertBlock(); |
| 2335 | VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust"); |
| 2336 | SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust"); |
| 2337 | llvm::Value *IsVirtual = |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2338 | Builder.CreateICmpNE(VBTableOffset, getZeroInt(), |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2339 | "memptr.is_vbase"); |
| 2340 | Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB); |
| 2341 | CGF.EmitBlock(VBaseAdjustBB); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2344 | // If we weren't given a dynamic vbptr offset, RD should be complete and we'll |
| 2345 | // know the vbptr offset. |
| 2346 | if (!VBPtrOffset) { |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 2347 | CharUnits offs = CharUnits::Zero(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2348 | if (!RD->hasDefinition()) { |
| 2349 | DiagnosticsEngine &Diags = CGF.CGM.getDiags(); |
| 2350 | unsigned DiagID = Diags.getCustomDiagID( |
| 2351 | DiagnosticsEngine::Error, |
| 2352 | "member pointer representation requires a " |
| 2353 | "complete class type for %0 to perform this expression"); |
| 2354 | Diags.Report(E->getExprLoc(), DiagID) << RD << E->getSourceRange(); |
| 2355 | } else if (RD->getNumVBases()) |
| 2356 | offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2357 | VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity()); |
| 2358 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2359 | llvm::Value *VBPtr = nullptr; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2360 | llvm::Value *VBaseOffs = |
Timur Iskhodzhanov | 0e4f559 | 2013-10-27 17:10:27 +0000 | [diff] [blame] | 2361 | GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2362 | llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs); |
| 2363 | |
| 2364 | // Merge control flow with the case where we didn't have to adjust. |
| 2365 | if (VBaseAdjustBB) { |
| 2366 | Builder.CreateBr(SkipAdjustBB); |
| 2367 | CGF.EmitBlock(SkipAdjustBB); |
| 2368 | llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base"); |
| 2369 | Phi->addIncoming(Base, OriginalBB); |
| 2370 | Phi->addIncoming(AdjustedBase, VBaseAdjustBB); |
| 2371 | return Phi; |
| 2372 | } |
| 2373 | return AdjustedBase; |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2376 | llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress( |
| 2377 | CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr, |
| 2378 | const MemberPointerType *MPT) { |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2379 | assert(MPT->isMemberDataPointer()); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2380 | unsigned AS = Base->getType()->getPointerAddressSpace(); |
| 2381 | llvm::Type *PType = |
| 2382 | CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); |
| 2383 | CGBuilderTy &Builder = CGF.Builder; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2384 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 2385 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2386 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2387 | // Extract the fields we need, regardless of model. We'll apply them if we |
| 2388 | // have them. |
| 2389 | llvm::Value *FieldOffset = MemPtr; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2390 | llvm::Value *VirtualBaseAdjustmentOffset = nullptr; |
| 2391 | llvm::Value *VBPtrOffset = nullptr; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2392 | if (MemPtr->getType()->isStructTy()) { |
| 2393 | // We need to extract values. |
| 2394 | unsigned I = 0; |
| 2395 | FieldOffset = Builder.CreateExtractValue(MemPtr, I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2396 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2397 | VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2398 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2399 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2402 | if (VirtualBaseAdjustmentOffset) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2403 | Base = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset, |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2404 | VBPtrOffset); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2405 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2406 | |
| 2407 | // Cast to char*. |
| 2408 | Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); |
| 2409 | |
| 2410 | // Apply the offset, which we assume is non-null. |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2411 | llvm::Value *Addr = |
| 2412 | Builder.CreateInBoundsGEP(Base, FieldOffset, "memptr.offset"); |
Reid Kleckner | a8a0f76 | 2013-03-22 19:02:54 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Cast the address to the appropriate pointer type, adopting the address |
| 2415 | // space of the base pointer. |
| 2416 | return Builder.CreateBitCast(Addr, PType); |
| 2417 | } |
| 2418 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2419 | static MSInheritanceAttr::Spelling |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2420 | getInheritanceFromMemptr(const MemberPointerType *MPT) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2421 | return MPT->getMostRecentCXXRecordDecl()->getMSInheritanceModel(); |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | llvm::Value * |
| 2425 | MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 2426 | const CastExpr *E, |
| 2427 | llvm::Value *Src) { |
| 2428 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
| 2429 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 2430 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 2431 | |
| 2432 | // Use constant emission if we can. |
| 2433 | if (isa<llvm::Constant>(Src)) |
| 2434 | return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src)); |
| 2435 | |
| 2436 | // We may be adding or dropping fields from the member pointer, so we need |
| 2437 | // both types and the inheritance models of both records. |
| 2438 | const MemberPointerType *SrcTy = |
| 2439 | E->getSubExpr()->getType()->castAs<MemberPointerType>(); |
| 2440 | const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2441 | bool IsFunc = SrcTy->isMemberFunctionPointer(); |
| 2442 | |
| 2443 | // If the classes use the same null representation, reinterpret_cast is a nop. |
| 2444 | bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2445 | if (IsReinterpret && IsFunc) |
| 2446 | return Src; |
| 2447 | |
| 2448 | CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl(); |
| 2449 | CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl(); |
| 2450 | if (IsReinterpret && |
| 2451 | SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero()) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2452 | return Src; |
| 2453 | |
| 2454 | CGBuilderTy &Builder = CGF.Builder; |
| 2455 | |
| 2456 | // Branch past the conversion if Src is null. |
| 2457 | llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy); |
| 2458 | llvm::Constant *DstNull = EmitNullMemberPointer(DstTy); |
| 2459 | |
| 2460 | // C++ 5.2.10p9: The null member pointer value is converted to the null member |
| 2461 | // pointer value of the destination type. |
| 2462 | if (IsReinterpret) { |
| 2463 | // For reinterpret casts, sema ensures that src and dst are both functions |
| 2464 | // or data and have the same size, which means the LLVM types should match. |
| 2465 | assert(Src->getType() == DstNull->getType()); |
| 2466 | return Builder.CreateSelect(IsNotNull, Src, DstNull); |
| 2467 | } |
| 2468 | |
| 2469 | llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock(); |
| 2470 | llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert"); |
| 2471 | llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted"); |
| 2472 | Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB); |
| 2473 | CGF.EmitBlock(ConvertBB); |
| 2474 | |
| 2475 | // Decompose src. |
| 2476 | llvm::Value *FirstField = Src; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2477 | llvm::Value *NonVirtualBaseAdjustment = nullptr; |
| 2478 | llvm::Value *VirtualBaseAdjustmentOffset = nullptr; |
| 2479 | llvm::Value *VBPtrOffset = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2480 | MSInheritanceAttr::Spelling SrcInheritance = SrcRD->getMSInheritanceModel(); |
| 2481 | if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) { |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2482 | // We need to extract values. |
| 2483 | unsigned I = 0; |
| 2484 | FirstField = Builder.CreateExtractValue(Src, I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2485 | if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2486 | NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2487 | if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2488 | VBPtrOffset = Builder.CreateExtractValue(Src, I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2489 | if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2490 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++); |
| 2491 | } |
| 2492 | |
| 2493 | // For data pointers, we adjust the field offset directly. For functions, we |
| 2494 | // have a separate field. |
| 2495 | llvm::Constant *Adj = getMemberPointerAdjustment(E); |
| 2496 | if (Adj) { |
| 2497 | Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); |
| 2498 | llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField; |
| 2499 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 2500 | if (!NVAdjustField) // If this field didn't exist in src, it's zero. |
| 2501 | NVAdjustField = getZeroInt(); |
| 2502 | if (isDerivedToBase) |
| 2503 | NVAdjustField = Builder.CreateNSWSub(NVAdjustField, Adj, "adj"); |
| 2504 | else |
| 2505 | NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, Adj, "adj"); |
| 2506 | } |
| 2507 | |
| 2508 | // FIXME PR15713: Support conversions through virtually derived classes. |
| 2509 | |
| 2510 | // Recompose dst from the null struct and the adjusted fields from src. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2511 | MSInheritanceAttr::Spelling DstInheritance = DstRD->getMSInheritanceModel(); |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2512 | llvm::Value *Dst; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2513 | if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) { |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2514 | Dst = FirstField; |
| 2515 | } else { |
| 2516 | Dst = llvm::UndefValue::get(DstNull->getType()); |
| 2517 | unsigned Idx = 0; |
| 2518 | Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2519 | if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2520 | Dst = Builder.CreateInsertValue( |
| 2521 | Dst, getValueOrZeroInt(NonVirtualBaseAdjustment), Idx++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2522 | if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2523 | Dst = Builder.CreateInsertValue( |
| 2524 | Dst, getValueOrZeroInt(VBPtrOffset), Idx++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2525 | if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2526 | Dst = Builder.CreateInsertValue( |
| 2527 | Dst, getValueOrZeroInt(VirtualBaseAdjustmentOffset), Idx++); |
| 2528 | } |
| 2529 | Builder.CreateBr(ContinueBB); |
| 2530 | |
| 2531 | // In the continuation, choose between DstNull and Dst. |
| 2532 | CGF.EmitBlock(ContinueBB); |
| 2533 | llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted"); |
| 2534 | Phi->addIncoming(DstNull, OriginalBB); |
| 2535 | Phi->addIncoming(Dst, ConvertBB); |
| 2536 | return Phi; |
| 2537 | } |
| 2538 | |
| 2539 | llvm::Constant * |
| 2540 | MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E, |
| 2541 | llvm::Constant *Src) { |
| 2542 | const MemberPointerType *SrcTy = |
| 2543 | E->getSubExpr()->getType()->castAs<MemberPointerType>(); |
| 2544 | const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); |
| 2545 | |
| 2546 | // If src is null, emit a new null for dst. We can't return src because dst |
| 2547 | // might have a new representation. |
| 2548 | if (MemberPointerConstantIsNull(SrcTy, Src)) |
| 2549 | return EmitNullMemberPointer(DstTy); |
| 2550 | |
| 2551 | // We don't need to do anything for reinterpret_casts of non-null member |
| 2552 | // pointers. We should only get here when the two type representations have |
| 2553 | // the same size. |
| 2554 | if (E->getCastKind() == CK_ReinterpretMemberPointer) |
| 2555 | return Src; |
| 2556 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2557 | MSInheritanceAttr::Spelling SrcInheritance = getInheritanceFromMemptr(SrcTy); |
| 2558 | MSInheritanceAttr::Spelling DstInheritance = getInheritanceFromMemptr(DstTy); |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2559 | |
| 2560 | // Decompose src. |
| 2561 | llvm::Constant *FirstField = Src; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2562 | llvm::Constant *NonVirtualBaseAdjustment = nullptr; |
| 2563 | llvm::Constant *VirtualBaseAdjustmentOffset = nullptr; |
| 2564 | llvm::Constant *VBPtrOffset = nullptr; |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2565 | bool IsFunc = SrcTy->isMemberFunctionPointer(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2566 | if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) { |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2567 | // We need to extract values. |
| 2568 | unsigned I = 0; |
| 2569 | FirstField = Src->getAggregateElement(I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2570 | if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2571 | NonVirtualBaseAdjustment = Src->getAggregateElement(I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2572 | if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2573 | VBPtrOffset = Src->getAggregateElement(I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2574 | if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2575 | VirtualBaseAdjustmentOffset = Src->getAggregateElement(I++); |
| 2576 | } |
| 2577 | |
| 2578 | // For data pointers, we adjust the field offset directly. For functions, we |
| 2579 | // have a separate field. |
| 2580 | llvm::Constant *Adj = getMemberPointerAdjustment(E); |
| 2581 | if (Adj) { |
| 2582 | Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy); |
| 2583 | llvm::Constant *&NVAdjustField = |
| 2584 | IsFunc ? NonVirtualBaseAdjustment : FirstField; |
| 2585 | bool IsDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 2586 | if (!NVAdjustField) // If this field didn't exist in src, it's zero. |
| 2587 | NVAdjustField = getZeroInt(); |
| 2588 | if (IsDerivedToBase) |
| 2589 | NVAdjustField = llvm::ConstantExpr::getNSWSub(NVAdjustField, Adj); |
| 2590 | else |
| 2591 | NVAdjustField = llvm::ConstantExpr::getNSWAdd(NVAdjustField, Adj); |
| 2592 | } |
| 2593 | |
| 2594 | // FIXME PR15713: Support conversions through virtually derived classes. |
| 2595 | |
| 2596 | // Recompose dst from the null struct and the adjusted fields from src. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2597 | if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2598 | return FirstField; |
| 2599 | |
| 2600 | llvm::SmallVector<llvm::Constant *, 4> Fields; |
| 2601 | Fields.push_back(FirstField); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2602 | if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2603 | Fields.push_back(getConstantOrZeroInt(NonVirtualBaseAdjustment)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2604 | if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2605 | Fields.push_back(getConstantOrZeroInt(VBPtrOffset)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2606 | if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance)) |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 2607 | Fields.push_back(getConstantOrZeroInt(VirtualBaseAdjustmentOffset)); |
| 2608 | return llvm::ConstantStruct::getAnon(Fields); |
| 2609 | } |
| 2610 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2611 | llvm::Value *MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer( |
| 2612 | CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, |
| 2613 | llvm::Value *MemPtr, const MemberPointerType *MPT) { |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2614 | assert(MPT->isMemberFunctionPointer()); |
| 2615 | const FunctionProtoType *FPT = |
| 2616 | MPT->getPointeeType()->castAs<FunctionProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2617 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2618 | llvm::FunctionType *FTy = |
| 2619 | CGM.getTypes().GetFunctionType( |
| 2620 | CGM.getTypes().arrangeCXXMethodType(RD, FPT)); |
| 2621 | CGBuilderTy &Builder = CGF.Builder; |
| 2622 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2623 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2624 | |
| 2625 | // Extract the fields we need, regardless of model. We'll apply them if we |
| 2626 | // have them. |
| 2627 | llvm::Value *FunctionPointer = MemPtr; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2628 | llvm::Value *NonVirtualBaseAdjustment = nullptr; |
| 2629 | llvm::Value *VirtualBaseAdjustmentOffset = nullptr; |
| 2630 | llvm::Value *VBPtrOffset = nullptr; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2631 | if (MemPtr->getType()->isStructTy()) { |
| 2632 | // We need to extract values. |
| 2633 | unsigned I = 0; |
| 2634 | FunctionPointer = Builder.CreateExtractValue(MemPtr, I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2635 | if (MSInheritanceAttr::hasNVOffsetField(MPT, Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2636 | NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2637 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) |
Reid Kleckner | 79e0291 | 2013-05-03 01:15:11 +0000 | [diff] [blame] | 2638 | VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2639 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2640 | VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); |
| 2641 | } |
| 2642 | |
| 2643 | if (VirtualBaseAdjustmentOffset) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2644 | This = AdjustVirtualBase(CGF, E, RD, This, VirtualBaseAdjustmentOffset, |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 2645 | VBPtrOffset); |
| 2646 | } |
| 2647 | |
| 2648 | if (NonVirtualBaseAdjustment) { |
| 2649 | // Apply the adjustment and cast back to the original struct type. |
| 2650 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 2651 | Ptr = Builder.CreateInBoundsGEP(Ptr, NonVirtualBaseAdjustment); |
| 2652 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
| 2653 | } |
| 2654 | |
| 2655 | return Builder.CreateBitCast(FunctionPointer, FTy->getPointerTo()); |
| 2656 | } |
| 2657 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 2658 | CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 2659 | return new MicrosoftCXXABI(CGM); |
| 2660 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2661 | |
| 2662 | // MS RTTI Overview: |
| 2663 | // The run time type information emitted by cl.exe contains 5 distinct types of |
| 2664 | // structures. Many of them reference each other. |
| 2665 | // |
| 2666 | // TypeInfo: Static classes that are returned by typeid. |
| 2667 | // |
| 2668 | // CompleteObjectLocator: Referenced by vftables. They contain information |
| 2669 | // required for dynamic casting, including OffsetFromTop. They also contain |
| 2670 | // a reference to the TypeInfo for the type and a reference to the |
| 2671 | // CompleteHierarchyDescriptor for the type. |
| 2672 | // |
| 2673 | // ClassHieararchyDescriptor: Contains information about a class hierarchy. |
| 2674 | // Used during dynamic_cast to walk a class hierarchy. References a base |
| 2675 | // class array and the size of said array. |
| 2676 | // |
| 2677 | // BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is |
| 2678 | // somewhat of a misnomer because the most derived class is also in the list |
| 2679 | // as well as multiple copies of virtual bases (if they occur multiple times |
| 2680 | // in the hiearchy.) The BaseClassArray contains one BaseClassDescriptor for |
| 2681 | // every path in the hierarchy, in pre-order depth first order. Note, we do |
| 2682 | // not declare a specific llvm type for BaseClassArray, it's merely an array |
| 2683 | // of BaseClassDescriptor pointers. |
| 2684 | // |
| 2685 | // BaseClassDescriptor: Contains information about a class in a class hierarchy. |
| 2686 | // BaseClassDescriptor is also somewhat of a misnomer for the same reason that |
| 2687 | // BaseClassArray is. It contains information about a class within a |
| 2688 | // hierarchy such as: is this base is ambiguous and what is its offset in the |
| 2689 | // vbtable. The names of the BaseClassDescriptors have all of their fields |
| 2690 | // mangled into them so they can be aggressively deduplicated by the linker. |
| 2691 | |
| 2692 | static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) { |
| 2693 | StringRef MangledName("\01??_7type_info@@6B@"); |
| 2694 | if (auto VTable = CGM.getModule().getNamedGlobal(MangledName)) |
| 2695 | return VTable; |
| 2696 | return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, |
| 2697 | /*Constant=*/true, |
| 2698 | llvm::GlobalVariable::ExternalLinkage, |
| 2699 | /*Initializer=*/nullptr, MangledName); |
| 2700 | } |
| 2701 | |
| 2702 | namespace { |
| 2703 | |
| 2704 | /// \brief A Helper struct that stores information about a class in a class |
| 2705 | /// hierarchy. The information stored in these structs struct is used during |
| 2706 | /// the generation of ClassHierarchyDescriptors and BaseClassDescriptors. |
| 2707 | // During RTTI creation, MSRTTIClasses are stored in a contiguous array with |
| 2708 | // implicit depth first pre-order tree connectivity. getFirstChild and |
| 2709 | // getNextSibling allow us to walk the tree efficiently. |
| 2710 | struct MSRTTIClass { |
| 2711 | enum { |
| 2712 | IsPrivateOnPath = 1 | 8, |
| 2713 | IsAmbiguous = 2, |
| 2714 | IsPrivate = 4, |
| 2715 | IsVirtual = 16, |
| 2716 | HasHierarchyDescriptor = 64 |
| 2717 | }; |
| 2718 | MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {} |
| 2719 | uint32_t initialize(const MSRTTIClass *Parent, |
| 2720 | const CXXBaseSpecifier *Specifier); |
| 2721 | |
| 2722 | MSRTTIClass *getFirstChild() { return this + 1; } |
| 2723 | static MSRTTIClass *getNextChild(MSRTTIClass *Child) { |
| 2724 | return Child + 1 + Child->NumBases; |
| 2725 | } |
| 2726 | |
| 2727 | const CXXRecordDecl *RD, *VirtualRoot; |
| 2728 | uint32_t Flags, NumBases, OffsetInVBase; |
| 2729 | }; |
| 2730 | |
| 2731 | /// \brief Recursively initialize the base class array. |
| 2732 | uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent, |
| 2733 | const CXXBaseSpecifier *Specifier) { |
| 2734 | Flags = HasHierarchyDescriptor; |
| 2735 | if (!Parent) { |
| 2736 | VirtualRoot = nullptr; |
| 2737 | OffsetInVBase = 0; |
| 2738 | } else { |
| 2739 | if (Specifier->getAccessSpecifier() != AS_public) |
| 2740 | Flags |= IsPrivate | IsPrivateOnPath; |
| 2741 | if (Specifier->isVirtual()) { |
| 2742 | Flags |= IsVirtual; |
| 2743 | VirtualRoot = RD; |
| 2744 | OffsetInVBase = 0; |
| 2745 | } else { |
| 2746 | if (Parent->Flags & IsPrivateOnPath) |
| 2747 | Flags |= IsPrivateOnPath; |
| 2748 | VirtualRoot = Parent->VirtualRoot; |
| 2749 | OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext() |
| 2750 | .getASTRecordLayout(Parent->RD).getBaseClassOffset(RD).getQuantity(); |
| 2751 | } |
| 2752 | } |
| 2753 | NumBases = 0; |
| 2754 | MSRTTIClass *Child = getFirstChild(); |
| 2755 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 2756 | NumBases += Child->initialize(this, &Base) + 1; |
| 2757 | Child = getNextChild(Child); |
| 2758 | } |
| 2759 | return NumBases; |
| 2760 | } |
| 2761 | |
| 2762 | static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) { |
| 2763 | switch (Ty->getLinkage()) { |
| 2764 | case NoLinkage: |
| 2765 | case InternalLinkage: |
| 2766 | case UniqueExternalLinkage: |
| 2767 | return llvm::GlobalValue::InternalLinkage; |
| 2768 | |
| 2769 | case VisibleNoLinkage: |
| 2770 | case ExternalLinkage: |
| 2771 | return llvm::GlobalValue::LinkOnceODRLinkage; |
| 2772 | } |
| 2773 | llvm_unreachable("Invalid linkage!"); |
| 2774 | } |
| 2775 | |
| 2776 | /// \brief An ephemeral helper class for building MS RTTI types. It caches some |
| 2777 | /// calls to the module and information about the most derived class in a |
| 2778 | /// hierarchy. |
| 2779 | struct MSRTTIBuilder { |
| 2780 | enum { |
| 2781 | HasBranchingHierarchy = 1, |
| 2782 | HasVirtualBranchingHierarchy = 2, |
| 2783 | HasAmbiguousBases = 4 |
| 2784 | }; |
| 2785 | |
| 2786 | MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD) |
| 2787 | : CGM(ABI.CGM), Context(CGM.getContext()), |
| 2788 | VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD), |
| 2789 | Linkage(getLinkageForRTTI(CGM.getContext().getTagDeclType(RD))), |
| 2790 | ABI(ABI) {} |
| 2791 | |
| 2792 | llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes); |
| 2793 | llvm::GlobalVariable * |
| 2794 | getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes); |
| 2795 | llvm::GlobalVariable *getClassHierarchyDescriptor(); |
| 2796 | llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo *Info); |
| 2797 | |
| 2798 | CodeGenModule &CGM; |
| 2799 | ASTContext &Context; |
| 2800 | llvm::LLVMContext &VMContext; |
| 2801 | llvm::Module &Module; |
| 2802 | const CXXRecordDecl *RD; |
| 2803 | llvm::GlobalVariable::LinkageTypes Linkage; |
| 2804 | MicrosoftCXXABI &ABI; |
| 2805 | }; |
| 2806 | |
| 2807 | } // namespace |
| 2808 | |
| 2809 | /// \brief Recursively serializes a class hierarchy in pre-order depth first |
| 2810 | /// order. |
| 2811 | static void serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> &Classes, |
| 2812 | const CXXRecordDecl *RD) { |
| 2813 | Classes.push_back(MSRTTIClass(RD)); |
| 2814 | for (const CXXBaseSpecifier &Base : RD->bases()) |
| 2815 | serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl()); |
| 2816 | } |
| 2817 | |
| 2818 | /// \brief Find ambiguity among base classes. |
| 2819 | static void |
| 2820 | detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> &Classes) { |
| 2821 | llvm::SmallPtrSet<const CXXRecordDecl *, 8> VirtualBases; |
| 2822 | llvm::SmallPtrSet<const CXXRecordDecl *, 8> UniqueBases; |
| 2823 | llvm::SmallPtrSet<const CXXRecordDecl *, 8> AmbiguousBases; |
| 2824 | for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) { |
| 2825 | if ((Class->Flags & MSRTTIClass::IsVirtual) && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2826 | !VirtualBases.insert(Class->RD).second) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2827 | Class = MSRTTIClass::getNextChild(Class); |
| 2828 | continue; |
| 2829 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2830 | if (!UniqueBases.insert(Class->RD).second) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2831 | AmbiguousBases.insert(Class->RD); |
| 2832 | Class++; |
| 2833 | } |
| 2834 | if (AmbiguousBases.empty()) |
| 2835 | return; |
| 2836 | for (MSRTTIClass &Class : Classes) |
| 2837 | if (AmbiguousBases.count(Class.RD)) |
| 2838 | Class.Flags |= MSRTTIClass::IsAmbiguous; |
| 2839 | } |
| 2840 | |
| 2841 | llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() { |
| 2842 | SmallString<256> MangledName; |
| 2843 | { |
| 2844 | llvm::raw_svector_ostream Out(MangledName); |
| 2845 | ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(RD, Out); |
| 2846 | } |
| 2847 | |
| 2848 | // Check to see if we've already declared this ClassHierarchyDescriptor. |
| 2849 | if (auto CHD = Module.getNamedGlobal(MangledName)) |
| 2850 | return CHD; |
| 2851 | |
| 2852 | // Serialize the class hierarchy and initialize the CHD Fields. |
| 2853 | SmallVector<MSRTTIClass, 8> Classes; |
| 2854 | serializeClassHierarchy(Classes, RD); |
| 2855 | Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr); |
| 2856 | detectAmbiguousBases(Classes); |
| 2857 | int Flags = 0; |
| 2858 | for (auto Class : Classes) { |
| 2859 | if (Class.RD->getNumBases() > 1) |
| 2860 | Flags |= HasBranchingHierarchy; |
| 2861 | // Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We |
| 2862 | // believe the field isn't actually used. |
| 2863 | if (Class.Flags & MSRTTIClass::IsAmbiguous) |
| 2864 | Flags |= HasAmbiguousBases; |
| 2865 | } |
| 2866 | if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0) |
| 2867 | Flags |= HasVirtualBranchingHierarchy; |
| 2868 | // These gep indices are used to get the address of the first element of the |
| 2869 | // base class array. |
| 2870 | llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0), |
| 2871 | llvm::ConstantInt::get(CGM.IntTy, 0)}; |
| 2872 | |
| 2873 | // Forward-declare the class hierarchy descriptor |
| 2874 | auto Type = ABI.getClassHierarchyDescriptorType(); |
| 2875 | auto CHD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, |
| 2876 | /*Initializer=*/nullptr, |
| 2877 | MangledName.c_str()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2878 | if (CHD->isWeakForLinker()) |
| 2879 | CHD->setComdat(CGM.getModule().getOrInsertComdat(CHD->getName())); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2880 | |
| 2881 | // Initialize the base class ClassHierarchyDescriptor. |
| 2882 | llvm::Constant *Fields[] = { |
| 2883 | llvm::ConstantInt::get(CGM.IntTy, 0), // Unknown |
| 2884 | llvm::ConstantInt::get(CGM.IntTy, Flags), |
| 2885 | llvm::ConstantInt::get(CGM.IntTy, Classes.size()), |
| 2886 | ABI.getImageRelativeConstant(llvm::ConstantExpr::getInBoundsGetElementPtr( |
| 2887 | getBaseClassArray(Classes), |
| 2888 | llvm::ArrayRef<llvm::Value *>(GEPIndices))), |
| 2889 | }; |
| 2890 | CHD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); |
| 2891 | return CHD; |
| 2892 | } |
| 2893 | |
| 2894 | llvm::GlobalVariable * |
| 2895 | MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) { |
| 2896 | SmallString<256> MangledName; |
| 2897 | { |
| 2898 | llvm::raw_svector_ostream Out(MangledName); |
| 2899 | ABI.getMangleContext().mangleCXXRTTIBaseClassArray(RD, Out); |
| 2900 | } |
| 2901 | |
| 2902 | // Forward-declare the base class array. |
| 2903 | // cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit |
| 2904 | // mode) bytes of padding. We provide a pointer sized amount of padding by |
| 2905 | // adding +1 to Classes.size(). The sections have pointer alignment and are |
| 2906 | // marked pick-any so it shouldn't matter. |
| 2907 | llvm::Type *PtrType = ABI.getImageRelativeType( |
| 2908 | ABI.getBaseClassDescriptorType()->getPointerTo()); |
| 2909 | auto *ArrType = llvm::ArrayType::get(PtrType, Classes.size() + 1); |
| 2910 | auto *BCA = new llvm::GlobalVariable( |
| 2911 | Module, ArrType, |
| 2912 | /*Constant=*/true, Linkage, /*Initializer=*/nullptr, MangledName.c_str()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2913 | if (BCA->isWeakForLinker()) |
| 2914 | BCA->setComdat(CGM.getModule().getOrInsertComdat(BCA->getName())); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2915 | |
| 2916 | // Initialize the BaseClassArray. |
| 2917 | SmallVector<llvm::Constant *, 8> BaseClassArrayData; |
| 2918 | for (MSRTTIClass &Class : Classes) |
| 2919 | BaseClassArrayData.push_back( |
| 2920 | ABI.getImageRelativeConstant(getBaseClassDescriptor(Class))); |
| 2921 | BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType)); |
| 2922 | BCA->setInitializer(llvm::ConstantArray::get(ArrType, BaseClassArrayData)); |
| 2923 | return BCA; |
| 2924 | } |
| 2925 | |
| 2926 | llvm::GlobalVariable * |
| 2927 | MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) { |
| 2928 | // Compute the fields for the BaseClassDescriptor. They are computed up front |
| 2929 | // because they are mangled into the name of the object. |
| 2930 | uint32_t OffsetInVBTable = 0; |
| 2931 | int32_t VBPtrOffset = -1; |
| 2932 | if (Class.VirtualRoot) { |
| 2933 | auto &VTableContext = CGM.getMicrosoftVTableContext(); |
| 2934 | OffsetInVBTable = VTableContext.getVBTableIndex(RD, Class.VirtualRoot) * 4; |
| 2935 | VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity(); |
| 2936 | } |
| 2937 | |
| 2938 | SmallString<256> MangledName; |
| 2939 | { |
| 2940 | llvm::raw_svector_ostream Out(MangledName); |
| 2941 | ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor( |
| 2942 | Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable, |
| 2943 | Class.Flags, Out); |
| 2944 | } |
| 2945 | |
| 2946 | // Check to see if we've already declared this object. |
| 2947 | if (auto BCD = Module.getNamedGlobal(MangledName)) |
| 2948 | return BCD; |
| 2949 | |
| 2950 | // Forward-declare the base class descriptor. |
| 2951 | auto Type = ABI.getBaseClassDescriptorType(); |
| 2952 | auto BCD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, |
| 2953 | /*Initializer=*/nullptr, |
| 2954 | MangledName.c_str()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2955 | if (BCD->isWeakForLinker()) |
| 2956 | BCD->setComdat(CGM.getModule().getOrInsertComdat(BCD->getName())); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2957 | |
| 2958 | // Initialize the BaseClassDescriptor. |
| 2959 | llvm::Constant *Fields[] = { |
| 2960 | ABI.getImageRelativeConstant( |
| 2961 | ABI.getAddrOfRTTIDescriptor(Context.getTypeDeclType(Class.RD))), |
| 2962 | llvm::ConstantInt::get(CGM.IntTy, Class.NumBases), |
| 2963 | llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase), |
| 2964 | llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), |
| 2965 | llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable), |
| 2966 | llvm::ConstantInt::get(CGM.IntTy, Class.Flags), |
| 2967 | ABI.getImageRelativeConstant( |
| 2968 | MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()), |
| 2969 | }; |
| 2970 | BCD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); |
| 2971 | return BCD; |
| 2972 | } |
| 2973 | |
| 2974 | llvm::GlobalVariable * |
| 2975 | MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo *Info) { |
| 2976 | SmallString<256> MangledName; |
| 2977 | { |
| 2978 | llvm::raw_svector_ostream Out(MangledName); |
| 2979 | ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info->MangledPath, Out); |
| 2980 | } |
| 2981 | |
| 2982 | // Check to see if we've already computed this complete object locator. |
| 2983 | if (auto COL = Module.getNamedGlobal(MangledName)) |
| 2984 | return COL; |
| 2985 | |
| 2986 | // Compute the fields of the complete object locator. |
| 2987 | int OffsetToTop = Info->FullOffsetInMDC.getQuantity(); |
| 2988 | int VFPtrOffset = 0; |
| 2989 | // The offset includes the vtordisp if one exists. |
| 2990 | if (const CXXRecordDecl *VBase = Info->getVBaseWithVPtr()) |
| 2991 | if (Context.getASTRecordLayout(RD) |
| 2992 | .getVBaseOffsetsMap() |
| 2993 | .find(VBase) |
| 2994 | ->second.hasVtorDisp()) |
| 2995 | VFPtrOffset = Info->NonVirtualOffset.getQuantity() + 4; |
| 2996 | |
| 2997 | // Forward-declare the complete object locator. |
| 2998 | llvm::StructType *Type = ABI.getCompleteObjectLocatorType(); |
| 2999 | auto COL = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, |
| 3000 | /*Initializer=*/nullptr, MangledName.c_str()); |
| 3001 | |
| 3002 | // Initialize the CompleteObjectLocator. |
| 3003 | llvm::Constant *Fields[] = { |
| 3004 | llvm::ConstantInt::get(CGM.IntTy, ABI.isImageRelative()), |
| 3005 | llvm::ConstantInt::get(CGM.IntTy, OffsetToTop), |
| 3006 | llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset), |
| 3007 | ABI.getImageRelativeConstant( |
| 3008 | CGM.GetAddrOfRTTIDescriptor(Context.getTypeDeclType(RD))), |
| 3009 | ABI.getImageRelativeConstant(getClassHierarchyDescriptor()), |
| 3010 | ABI.getImageRelativeConstant(COL), |
| 3011 | }; |
| 3012 | llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields); |
| 3013 | if (!ABI.isImageRelative()) |
| 3014 | FieldsRef = FieldsRef.drop_back(); |
| 3015 | COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef)); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 3016 | if (COL->isWeakForLinker()) |
| 3017 | COL->setComdat(CGM.getModule().getOrInsertComdat(COL->getName())); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3018 | return COL; |
| 3019 | } |
| 3020 | |
| 3021 | /// \brief Gets a TypeDescriptor. Returns a llvm::Constant * rather than a |
| 3022 | /// llvm::GlobalVariable * because different type descriptors have different |
| 3023 | /// types, and need to be abstracted. They are abstracting by casting the |
| 3024 | /// address to an Int8PtrTy. |
| 3025 | llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) { |
| 3026 | SmallString<256> MangledName, TypeInfoString; |
| 3027 | { |
| 3028 | llvm::raw_svector_ostream Out(MangledName); |
| 3029 | getMangleContext().mangleCXXRTTI(Type, Out); |
| 3030 | } |
| 3031 | |
| 3032 | // Check to see if we've already declared this TypeDescriptor. |
| 3033 | if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName)) |
| 3034 | return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); |
| 3035 | |
| 3036 | // Compute the fields for the TypeDescriptor. |
| 3037 | { |
| 3038 | llvm::raw_svector_ostream Out(TypeInfoString); |
| 3039 | getMangleContext().mangleCXXRTTIName(Type, Out); |
| 3040 | } |
| 3041 | |
| 3042 | // Declare and initialize the TypeDescriptor. |
| 3043 | llvm::Constant *Fields[] = { |
| 3044 | getTypeInfoVTable(CGM), // VFPtr |
| 3045 | llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data |
| 3046 | llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)}; |
| 3047 | llvm::StructType *TypeDescriptorType = |
| 3048 | getTypeDescriptorType(TypeInfoString); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 3049 | auto *Var = new llvm::GlobalVariable( |
| 3050 | CGM.getModule(), TypeDescriptorType, /*Constant=*/false, |
| 3051 | getLinkageForRTTI(Type), |
| 3052 | llvm::ConstantStruct::get(TypeDescriptorType, Fields), |
| 3053 | MangledName.c_str()); |
| 3054 | if (Var->isWeakForLinker()) |
| 3055 | Var->setComdat(CGM.getModule().getOrInsertComdat(Var->getName())); |
| 3056 | return llvm::ConstantExpr::getBitCast(Var, CGM.Int8PtrTy); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | /// \brief Gets or a creates a Microsoft CompleteObjectLocator. |
| 3060 | llvm::GlobalVariable * |
| 3061 | MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD, |
| 3062 | const VPtrInfo *Info) { |
| 3063 | return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info); |
| 3064 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3065 | |
| 3066 | static void emitCXXConstructor(CodeGenModule &CGM, |
| 3067 | const CXXConstructorDecl *ctor, |
| 3068 | StructorType ctorType) { |
| 3069 | // There are no constructor variants, always emit the complete destructor. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 3070 | llvm::Function *Fn = CGM.codegenCXXStructor(ctor, StructorType::Complete); |
| 3071 | CGM.maybeSetTrivialComdat(*ctor, *Fn); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | static void emitCXXDestructor(CodeGenModule &CGM, const CXXDestructorDecl *dtor, |
| 3075 | StructorType dtorType) { |
| 3076 | // The complete destructor is equivalent to the base destructor for |
| 3077 | // classes with no virtual bases, so try to emit it as an alias. |
| 3078 | if (!dtor->getParent()->getNumVBases() && |
| 3079 | (dtorType == StructorType::Complete || dtorType == StructorType::Base)) { |
| 3080 | bool ProducedAlias = !CGM.TryEmitDefinitionAsAlias( |
| 3081 | GlobalDecl(dtor, Dtor_Complete), GlobalDecl(dtor, Dtor_Base), true); |
| 3082 | if (ProducedAlias) { |
| 3083 | if (dtorType == StructorType::Complete) |
| 3084 | return; |
| 3085 | if (dtor->isVirtual()) |
| 3086 | CGM.getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete)); |
| 3087 | } |
| 3088 | } |
| 3089 | |
| 3090 | // The base destructor is equivalent to the base destructor of its |
| 3091 | // base class if there is exactly one non-virtual base class with a |
| 3092 | // non-trivial destructor, there are no fields with a non-trivial |
| 3093 | // destructor, and the body of the destructor is trivial. |
| 3094 | if (dtorType == StructorType::Base && !CGM.TryEmitBaseDestructorAsAlias(dtor)) |
| 3095 | return; |
| 3096 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 3097 | llvm::Function *Fn = CGM.codegenCXXStructor(dtor, dtorType); |
| 3098 | if (Fn->isWeakForLinker()) |
| 3099 | Fn->setComdat(CGM.getModule().getOrInsertComdat(Fn->getName())); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | void MicrosoftCXXABI::emitCXXStructor(const CXXMethodDecl *MD, |
| 3103 | StructorType Type) { |
| 3104 | if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { |
| 3105 | emitCXXConstructor(CGM, CD, Type); |
| 3106 | return; |
| 3107 | } |
| 3108 | emitCXXDestructor(CGM, cast<CXXDestructorDecl>(MD), Type); |
| 3109 | } |