Anders Carlsson | 5b95592 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===// |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ code generation of classes |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 14 | #include "CGBlocks.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Devang Patel | d67ef0e | 2010-08-11 21:04:37 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 17 | #include "CGRecordLayout.h" |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/EvaluatedExprVisitor.h" |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtCXX.h" |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetBuiltins.h" |
Mark Lacey | 8b54999 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 25 | #include "clang/CodeGen/CGFunctionInfo.h" |
Devang Patel | 3ee36af | 2011-02-22 20:55:26 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 27 | #include "llvm/IR/Intrinsics.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 28 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace CodeGen; |
| 31 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 32 | static CharUnits |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 33 | ComputeNonVirtualBaseClassOffset(ASTContext &Context, |
| 34 | const CXXRecordDecl *DerivedClass, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 35 | CastExpr::path_const_iterator Start, |
| 36 | CastExpr::path_const_iterator End) { |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 37 | CharUnits Offset = CharUnits::Zero(); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 38 | |
| 39 | const CXXRecordDecl *RD = DerivedClass; |
| 40 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 41 | for (CastExpr::path_const_iterator I = Start; I != End; ++I) { |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 42 | const CXXBaseSpecifier *Base = *I; |
| 43 | assert(!Base->isVirtual() && "Should not see virtual bases here!"); |
| 44 | |
| 45 | // Get the layout. |
| 46 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 47 | |
| 48 | const CXXRecordDecl *BaseDecl = |
| 49 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 50 | |
| 51 | // Add the offset. |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 52 | Offset += Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 53 | |
| 54 | RD = BaseDecl; |
| 55 | } |
| 56 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 57 | return Offset; |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 58 | } |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 59 | |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 60 | llvm::Constant * |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 61 | CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 62 | CastExpr::path_const_iterator PathBegin, |
| 63 | CastExpr::path_const_iterator PathEnd) { |
| 64 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 65 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 66 | CharUnits Offset = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 67 | ComputeNonVirtualBaseClassOffset(getContext(), ClassDecl, |
| 68 | PathBegin, PathEnd); |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 69 | if (Offset.isZero()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 70 | return nullptr; |
| 71 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 72 | llvm::Type *PtrDiffTy = |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 73 | Types.ConvertType(getContext().getPointerDiffType()); |
| 74 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 75 | return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity()); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 78 | /// Gets the address of a direct base class within a complete object. |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 79 | /// This should only be used for (1) non-virtual bases or (2) virtual bases |
| 80 | /// when the type is known to be complete (e.g. in complete destructors). |
| 81 | /// |
| 82 | /// The object pointed to by 'This' is assumed to be non-null. |
| 83 | llvm::Value * |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 84 | CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(llvm::Value *This, |
| 85 | const CXXRecordDecl *Derived, |
| 86 | const CXXRecordDecl *Base, |
| 87 | bool BaseIsVirtual) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 88 | // 'this' must be a pointer (in some address space) to Derived. |
| 89 | assert(This->getType()->isPointerTy() && |
| 90 | cast<llvm::PointerType>(This->getType())->getElementType() |
| 91 | == ConvertType(Derived)); |
| 92 | |
| 93 | // Compute the offset of the virtual base. |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 94 | CharUnits Offset; |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 95 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 96 | if (BaseIsVirtual) |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 97 | Offset = Layout.getVBaseClassOffset(Base); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 98 | else |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 99 | Offset = Layout.getBaseClassOffset(Base); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 100 | |
| 101 | // Shift and cast down to the base type. |
| 102 | // TODO: for complete types, this should be possible with a GEP. |
| 103 | llvm::Value *V = This; |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 104 | if (Offset.isPositive()) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 105 | V = Builder.CreateBitCast(V, Int8PtrTy); |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 106 | V = Builder.CreateConstInBoundsGEP1_64(V, Offset.getQuantity()); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 107 | } |
| 108 | V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo()); |
| 109 | |
| 110 | return V; |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 111 | } |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 112 | |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 113 | static llvm::Value * |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 114 | ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ptr, |
| 115 | CharUnits nonVirtualOffset, |
| 116 | llvm::Value *virtualOffset) { |
| 117 | // Assert that we have something to do. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 118 | assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr); |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 119 | |
| 120 | // Compute the offset from the static and dynamic components. |
| 121 | llvm::Value *baseOffset; |
| 122 | if (!nonVirtualOffset.isZero()) { |
| 123 | baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy, |
| 124 | nonVirtualOffset.getQuantity()); |
| 125 | if (virtualOffset) { |
| 126 | baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset); |
| 127 | } |
| 128 | } else { |
| 129 | baseOffset = virtualOffset; |
| 130 | } |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 131 | |
| 132 | // Apply the base offset. |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 133 | ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy); |
| 134 | ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr"); |
| 135 | return ptr; |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 138 | llvm::Value *CodeGenFunction::GetAddressOfBaseClass( |
| 139 | llvm::Value *Value, const CXXRecordDecl *Derived, |
| 140 | CastExpr::path_const_iterator PathBegin, |
| 141 | CastExpr::path_const_iterator PathEnd, bool NullCheckValue, |
| 142 | SourceLocation Loc) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 143 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 144 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 145 | CastExpr::path_const_iterator Start = PathBegin; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 146 | const CXXRecordDecl *VBase = nullptr; |
| 147 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 148 | // Sema has done some convenient canonicalization here: if the |
| 149 | // access path involved any virtual steps, the conversion path will |
| 150 | // *start* with a step down to the correct virtual base subobject, |
| 151 | // and hence will not require any further steps. |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 152 | if ((*Start)->isVirtual()) { |
| 153 | VBase = |
| 154 | cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl()); |
| 155 | ++Start; |
| 156 | } |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 157 | |
| 158 | // Compute the static offset of the ultimate destination within its |
| 159 | // allocating subobject (the virtual base, if there is one, or else |
| 160 | // the "complete" object that we see). |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 161 | CharUnits NonVirtualOffset = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 162 | ComputeNonVirtualBaseClassOffset(getContext(), VBase ? VBase : Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 163 | Start, PathEnd); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 164 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 165 | // If there's a virtual step, we can sometimes "devirtualize" it. |
| 166 | // For now, that's limited to when the derived type is final. |
| 167 | // TODO: "devirtualize" this for accesses to known-complete objects. |
| 168 | if (VBase && Derived->hasAttr<FinalAttr>()) { |
| 169 | const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived); |
| 170 | CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase); |
| 171 | NonVirtualOffset += vBaseOffset; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 172 | VBase = nullptr; // we no longer have a virtual step |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 175 | // Get the base pointer type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 176 | llvm::Type *BasePtrTy = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 177 | ConvertType((PathEnd[-1])->getType())->getPointerTo(); |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 178 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 179 | QualType DerivedTy = getContext().getRecordType(Derived); |
| 180 | CharUnits DerivedAlign = getContext().getTypeAlignInChars(DerivedTy); |
| 181 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 182 | // If the static offset is zero and we don't have a virtual step, |
| 183 | // just do a bitcast; null checks are unnecessary. |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 184 | if (NonVirtualOffset.isZero() && !VBase) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 185 | if (sanitizePerformTypeCheck()) { |
| 186 | EmitTypeCheck(TCK_Upcast, Loc, Value, DerivedTy, DerivedAlign, |
| 187 | !NullCheckValue); |
| 188 | } |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 189 | return Builder.CreateBitCast(Value, BasePtrTy); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 190 | } |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 191 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 192 | llvm::BasicBlock *origBB = nullptr; |
| 193 | llvm::BasicBlock *endBB = nullptr; |
| 194 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 195 | // Skip over the offset (and the vtable load) if we're supposed to |
| 196 | // null-check the pointer. |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 197 | if (NullCheckValue) { |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 198 | origBB = Builder.GetInsertBlock(); |
| 199 | llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull"); |
| 200 | endBB = createBasicBlock("cast.end"); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 201 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 202 | llvm::Value *isNull = Builder.CreateIsNull(Value); |
| 203 | Builder.CreateCondBr(isNull, endBB, notNullBB); |
| 204 | EmitBlock(notNullBB); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 207 | if (sanitizePerformTypeCheck()) { |
| 208 | EmitTypeCheck(VBase ? TCK_UpcastToVirtualBase : TCK_Upcast, Loc, Value, |
| 209 | DerivedTy, DerivedAlign, true); |
| 210 | } |
| 211 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 212 | // Compute the virtual offset. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 213 | llvm::Value *VirtualOffset = nullptr; |
Anders Carlsson | 336a7dc | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 214 | if (VBase) { |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 215 | VirtualOffset = |
| 216 | CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase); |
Anders Carlsson | 336a7dc | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 217 | } |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 218 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 219 | // Apply both offsets. |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 220 | Value = ApplyNonVirtualAndVirtualOffset(*this, Value, |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 221 | NonVirtualOffset, |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 222 | VirtualOffset); |
| 223 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 224 | // Cast to the destination type. |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 225 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 226 | |
| 227 | // Build a phi if we needed a null check. |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 228 | if (NullCheckValue) { |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 229 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
| 230 | Builder.CreateBr(endBB); |
| 231 | EmitBlock(endBB); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 232 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 233 | llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result"); |
| 234 | PHI->addIncoming(Value, notNullBB); |
| 235 | PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 236 | Value = PHI; |
| 237 | } |
| 238 | |
| 239 | return Value; |
| 240 | } |
| 241 | |
| 242 | llvm::Value * |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 243 | CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 244 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 245 | CastExpr::path_const_iterator PathBegin, |
| 246 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 247 | bool NullCheckValue) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 248 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 249 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 250 | QualType DerivedTy = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 251 | getContext().getCanonicalType(getContext().getTagDeclType(Derived)); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 252 | llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
Richard Smith | c764830 | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 253 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 254 | llvm::Value *NonVirtualOffset = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 255 | CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd); |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 256 | |
| 257 | if (!NonVirtualOffset) { |
| 258 | // No offset, we can just cast back. |
| 259 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 260 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 261 | |
| 262 | llvm::BasicBlock *CastNull = nullptr; |
| 263 | llvm::BasicBlock *CastNotNull = nullptr; |
| 264 | llvm::BasicBlock *CastEnd = nullptr; |
| 265 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 266 | if (NullCheckValue) { |
| 267 | CastNull = createBasicBlock("cast.null"); |
| 268 | CastNotNull = createBasicBlock("cast.notnull"); |
| 269 | CastEnd = createBasicBlock("cast.end"); |
| 270 | |
Anders Carlsson | b924124 | 2011-04-11 00:30:07 +0000 | [diff] [blame] | 271 | llvm::Value *IsNull = Builder.CreateIsNull(Value); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 272 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 273 | EmitBlock(CastNotNull); |
| 274 | } |
| 275 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 276 | // Apply the offset. |
Eli Friedman | c568543 | 2012-02-28 22:07:56 +0000 | [diff] [blame] | 277 | Value = Builder.CreateBitCast(Value, Int8PtrTy); |
| 278 | Value = Builder.CreateGEP(Value, Builder.CreateNeg(NonVirtualOffset), |
| 279 | "sub.ptr"); |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 280 | |
| 281 | // Just cast. |
| 282 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 283 | |
| 284 | if (NullCheckValue) { |
| 285 | Builder.CreateBr(CastEnd); |
| 286 | EmitBlock(CastNull); |
| 287 | Builder.CreateBr(CastEnd); |
| 288 | EmitBlock(CastEnd); |
| 289 | |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 290 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 291 | PHI->addIncoming(Value, CastNotNull); |
| 292 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 293 | CastNull); |
| 294 | Value = PHI; |
| 295 | } |
| 296 | |
| 297 | return Value; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 298 | } |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 299 | |
| 300 | llvm::Value *CodeGenFunction::GetVTTParameter(GlobalDecl GD, |
| 301 | bool ForVirtualBase, |
| 302 | bool Delegating) { |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 303 | if (!CGM.getCXXABI().NeedsVTTParameter(GD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 304 | // This constructor/destructor does not need a VTT parameter. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 305 | return nullptr; |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 306 | } |
| 307 | |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 308 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent(); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 309 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 310 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 311 | llvm::Value *VTT; |
| 312 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 313 | uint64_t SubVTTIndex; |
| 314 | |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 315 | if (Delegating) { |
| 316 | // If this is a delegating constructor call, just load the VTT. |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 317 | return LoadCXXVTT(); |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 318 | } else if (RD == Base) { |
| 319 | // If the record matches the base, this is the complete ctor/dtor |
| 320 | // variant calling the base variant in a class with virtual bases. |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 321 | assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) && |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 322 | "doing no-op VTT offset in base dtor/ctor?"); |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 323 | assert(!ForVirtualBase && "Can't have same class as virtual base!"); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 324 | SubVTTIndex = 0; |
| 325 | } else { |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 326 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 327 | CharUnits BaseOffset = ForVirtualBase ? |
| 328 | Layout.getVBaseClassOffset(Base) : |
| 329 | Layout.getBaseClassOffset(Base); |
Anders Carlsson | c11bb21 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 330 | |
| 331 | SubVTTIndex = |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 332 | CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset)); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 333 | assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
| 334 | } |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 335 | |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 336 | if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 337 | // A VTT parameter was passed to the constructor, use it. |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 338 | VTT = LoadCXXVTT(); |
| 339 | VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 340 | } else { |
| 341 | // We're the complete constructor, so get the VTT by name. |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 342 | VTT = CGM.getVTables().GetAddrOfVTT(RD); |
| 343 | VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return VTT; |
| 347 | } |
| 348 | |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 349 | namespace { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 350 | /// Call the destructor for a direct base class. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 351 | struct CallBaseDtor : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 352 | const CXXRecordDecl *BaseClass; |
| 353 | bool BaseIsVirtual; |
| 354 | CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual) |
| 355 | : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {} |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 356 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 357 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 358 | const CXXRecordDecl *DerivedClass = |
| 359 | cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent(); |
| 360 | |
| 361 | const CXXDestructorDecl *D = BaseClass->getDestructor(); |
| 362 | llvm::Value *Addr = |
| 363 | CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThis(), |
| 364 | DerivedClass, BaseClass, |
| 365 | BaseIsVirtual); |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 366 | CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual, |
| 367 | /*Delegating=*/false, Addr); |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 368 | } |
| 369 | }; |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 370 | |
| 371 | /// A visitor which checks whether an initializer uses 'this' in a |
| 372 | /// way which requires the vtable to be properly set. |
| 373 | struct DynamicThisUseChecker : EvaluatedExprVisitor<DynamicThisUseChecker> { |
| 374 | typedef EvaluatedExprVisitor<DynamicThisUseChecker> super; |
| 375 | |
| 376 | bool UsesThis; |
| 377 | |
| 378 | DynamicThisUseChecker(ASTContext &C) : super(C), UsesThis(false) {} |
| 379 | |
| 380 | // Black-list all explicit and implicit references to 'this'. |
| 381 | // |
| 382 | // Do we need to worry about external references to 'this' derived |
| 383 | // from arbitrary code? If so, then anything which runs arbitrary |
| 384 | // external code might potentially access the vtable. |
| 385 | void VisitCXXThisExpr(CXXThisExpr *E) { UsesThis = true; } |
| 386 | }; |
| 387 | } |
| 388 | |
| 389 | static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) { |
| 390 | DynamicThisUseChecker Checker(C); |
| 391 | Checker.Visit(const_cast<Expr*>(Init)); |
| 392 | return Checker.UsesThis; |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 395 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
| 396 | const CXXRecordDecl *ClassDecl, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 397 | CXXCtorInitializer *BaseInit, |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 398 | CXXCtorType CtorType) { |
| 399 | assert(BaseInit->isBaseInitializer() && |
| 400 | "Must have base initializer!"); |
| 401 | |
| 402 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 403 | |
| 404 | const Type *BaseType = BaseInit->getBaseClass(); |
| 405 | CXXRecordDecl *BaseClassDecl = |
| 406 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 407 | |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 408 | bool isBaseVirtual = BaseInit->isBaseVirtual(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 409 | |
| 410 | // The base constructor doesn't construct virtual bases. |
| 411 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 412 | return; |
| 413 | |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 414 | // If the initializer for the base (other than the constructor |
| 415 | // itself) accesses 'this' in any way, we need to initialize the |
| 416 | // vtables. |
| 417 | if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit())) |
| 418 | CGF.InitializeVTablePointers(ClassDecl); |
| 419 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 420 | // We can pretend to be a complete class because it only matters for |
| 421 | // virtual bases, and we only do virtual bases for complete ctors. |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 422 | llvm::Value *V = |
| 423 | CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl, |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 424 | BaseClassDecl, |
| 425 | isBaseVirtual); |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 426 | CharUnits Alignment = CGF.getContext().getTypeAlignInChars(BaseType); |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 427 | AggValueSlot AggSlot = |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 428 | AggValueSlot::forAddr(V, Alignment, Qualifiers(), |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 429 | AggValueSlot::IsDestructed, |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 430 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 431 | AggValueSlot::IsNotAliased); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 432 | |
| 433 | CGF.EmitAggExpr(BaseInit->getInit(), AggSlot); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 434 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 435 | if (CGF.CGM.getLangOpts().Exceptions && |
Anders Carlsson | c1cfdf8 | 2011-02-20 00:20:27 +0000 | [diff] [blame] | 436 | !BaseClassDecl->hasTrivialDestructor()) |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 437 | CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl, |
| 438 | isBaseVirtual); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 441 | static void EmitAggMemberInitializer(CodeGenFunction &CGF, |
| 442 | LValue LHS, |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 443 | Expr *Init, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 444 | llvm::Value *ArrayIndexVar, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 445 | QualType T, |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 446 | ArrayRef<VarDecl *> ArrayIndexes, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 447 | unsigned Index) { |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 448 | if (Index == ArrayIndexes.size()) { |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 449 | LValue LV = LHS; |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 450 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 451 | if (ArrayIndexVar) { |
| 452 | // If we have an array index variable, load it and use it as an offset. |
| 453 | // Then, increment the value. |
| 454 | llvm::Value *Dest = LHS.getAddress(); |
| 455 | llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar); |
| 456 | Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress"); |
| 457 | llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1); |
| 458 | Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc"); |
| 459 | CGF.Builder.CreateStore(Next, ArrayIndexVar); |
Sebastian Redl | 924db71 | 2012-02-19 15:41:54 +0000 | [diff] [blame] | 460 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 461 | // Update the LValue. |
| 462 | LV.setAddress(Dest); |
| 463 | CharUnits Align = CGF.getContext().getTypeAlignInChars(T); |
| 464 | LV.setAlignment(std::min(Align, LV.getAlignment())); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 465 | } |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 466 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 467 | switch (CGF.getEvaluationKind(T)) { |
| 468 | case TEK_Scalar: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 469 | CGF.EmitScalarInit(Init, /*decl*/ nullptr, LV, false); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 470 | break; |
| 471 | case TEK_Complex: |
| 472 | CGF.EmitComplexExprIntoLValue(Init, LV, /*isInit*/ true); |
| 473 | break; |
| 474 | case TEK_Aggregate: { |
| 475 | AggValueSlot Slot = |
| 476 | AggValueSlot::forLValue(LV, |
| 477 | AggValueSlot::IsDestructed, |
| 478 | AggValueSlot::DoesNotNeedGCBarriers, |
| 479 | AggValueSlot::IsNotAliased); |
| 480 | |
| 481 | CGF.EmitAggExpr(Init, Slot); |
| 482 | break; |
| 483 | } |
| 484 | } |
Sebastian Redl | 924db71 | 2012-02-19 15:41:54 +0000 | [diff] [blame] | 485 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 486 | return; |
| 487 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 489 | const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T); |
| 490 | assert(Array && "Array initialization without the array type?"); |
| 491 | llvm::Value *IndexVar |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 492 | = CGF.GetAddrOfLocalVar(ArrayIndexes[Index]); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 493 | assert(IndexVar && "Array index variable not loaded"); |
| 494 | |
| 495 | // Initialize this index variable to zero. |
| 496 | llvm::Value* Zero |
| 497 | = llvm::Constant::getNullValue( |
| 498 | CGF.ConvertType(CGF.getContext().getSizeType())); |
| 499 | CGF.Builder.CreateStore(Zero, IndexVar); |
| 500 | |
| 501 | // Start the loop with a block that tests the condition. |
| 502 | llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond"); |
| 503 | llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end"); |
| 504 | |
| 505 | CGF.EmitBlock(CondBlock); |
| 506 | |
| 507 | llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body"); |
| 508 | // Generate: if (loop-index < number-of-elements) fall to the loop body, |
| 509 | // otherwise, go to the block after the for-loop. |
| 510 | uint64_t NumElements = Array->getSize().getZExtValue(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 511 | llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar); |
Chris Lattner | 985f739 | 2010-05-06 06:35:23 +0000 | [diff] [blame] | 512 | llvm::Value *NumElementsPtr = |
| 513 | llvm::ConstantInt::get(Counter->getType(), NumElements); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 514 | llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 515 | "isless"); |
| 516 | |
| 517 | // If the condition is true, execute the body. |
| 518 | CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 519 | |
| 520 | CGF.EmitBlock(ForBody); |
| 521 | llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc"); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 522 | |
| 523 | // Inside the loop body recurse to emit the inner loop or, eventually, the |
| 524 | // constructor call. |
| 525 | EmitAggMemberInitializer(CGF, LHS, Init, ArrayIndexVar, |
| 526 | Array->getElementType(), ArrayIndexes, Index + 1); |
| 527 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 528 | CGF.EmitBlock(ContinueBlock); |
| 529 | |
| 530 | // Emit the increment of the loop counter. |
| 531 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 532 | Counter = CGF.Builder.CreateLoad(IndexVar); |
| 533 | NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc"); |
| 534 | CGF.Builder.CreateStore(NextVal, IndexVar); |
| 535 | |
| 536 | // Finally, branch back up to the condition for the next iteration. |
| 537 | CGF.EmitBranch(CondBlock); |
| 538 | |
| 539 | // Emit the fall-through block. |
| 540 | CGF.EmitBlock(AfterFor, true); |
| 541 | } |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 542 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 543 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 544 | const CXXRecordDecl *ClassDecl, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 545 | CXXCtorInitializer *MemberInit, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 546 | const CXXConstructorDecl *Constructor, |
| 547 | FunctionArgList &Args) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 548 | ApplyDebugLocation Loc(CGF, MemberInit->getSourceLocation()); |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 549 | assert(MemberInit->isAnyMemberInitializer() && |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 550 | "Must have member initializer!"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 551 | assert(MemberInit->getInit() && "Must have initializer!"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 552 | |
| 553 | // non-static data member initializers. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 554 | FieldDecl *Field = MemberInit->getAnyMember(); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 555 | QualType FieldType = Field->getType(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 556 | |
| 557 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 558 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
Eli Friedman | 859c65c | 2012-08-08 03:51:37 +0000 | [diff] [blame] | 559 | LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 560 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 561 | if (MemberInit->isIndirectMemberInitializer()) { |
Eli Friedman | 859c65c | 2012-08-08 03:51:37 +0000 | [diff] [blame] | 562 | // If we are initializing an anonymous union field, drill down to |
| 563 | // the field. |
| 564 | IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 565 | for (const auto *I : IndirectField->chain()) |
| 566 | LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(I)); |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 567 | FieldType = MemberInit->getIndirectMember()->getAnonField()->getType(); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 568 | } else { |
Eli Friedman | 859c65c | 2012-08-08 03:51:37 +0000 | [diff] [blame] | 569 | LHS = CGF.EmitLValueForFieldInitialization(LHS, Field); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 572 | // Special case: if we are in a copy or move constructor, and we are copying |
| 573 | // an array of PODs or classes with trivial copy constructors, ignore the |
| 574 | // AST and perform the copy we know is equivalent. |
| 575 | // FIXME: This is hacky at best... if we had a bit more explicit information |
| 576 | // in the AST, we could generalize it more easily. |
| 577 | const ConstantArrayType *Array |
| 578 | = CGF.getContext().getAsConstantArrayType(FieldType); |
Jordan Rose | a7b8797 | 2013-08-07 16:16:48 +0000 | [diff] [blame] | 579 | if (Array && Constructor->isDefaulted() && |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 580 | Constructor->isCopyOrMoveConstructor()) { |
| 581 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Array); |
Richard Smith | e938536 | 2012-11-07 23:56:21 +0000 | [diff] [blame] | 582 | CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit()); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 583 | if (BaseElementTy.isPODType(CGF.getContext()) || |
Richard Smith | e938536 | 2012-11-07 23:56:21 +0000 | [diff] [blame] | 584 | (CE && CE->getConstructor()->isTrivial())) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 585 | unsigned SrcArgIndex = |
| 586 | CGF.CGM.getCXXABI().getSrcArgforCopyCtor(Constructor, Args); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 587 | llvm::Value *SrcPtr |
| 588 | = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex])); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 589 | LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); |
| 590 | LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 591 | |
| 592 | // Copy the aggregate. |
| 593 | CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 594 | LHS.isVolatileQualified()); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | ArrayRef<VarDecl *> ArrayIndexes; |
| 600 | if (MemberInit->getNumArrayIndices()) |
| 601 | ArrayIndexes = MemberInit->getArrayIndexes(); |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 602 | CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit(), ArrayIndexes); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 605 | void CodeGenFunction::EmitInitializerForField( |
| 606 | FieldDecl *Field, LValue LHS, Expr *Init, |
| 607 | ArrayRef<VarDecl *> ArrayIndexes) { |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 608 | QualType FieldType = Field->getType(); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 609 | switch (getEvaluationKind(FieldType)) { |
| 610 | case TEK_Scalar: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 611 | if (LHS.isSimple()) { |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 612 | EmitExprAsInit(Init, Field, LHS, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 613 | } else { |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 614 | RValue RHS = RValue::get(EmitScalarExpr(Init)); |
| 615 | EmitStoreThroughLValue(RHS, LHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 616 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 617 | break; |
| 618 | case TEK_Complex: |
| 619 | EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true); |
| 620 | break; |
| 621 | case TEK_Aggregate: { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 622 | llvm::Value *ArrayIndexVar = nullptr; |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 623 | if (ArrayIndexes.size()) { |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 624 | llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 625 | |
| 626 | // The LHS is a pointer to the first object we'll be constructing, as |
| 627 | // a flat array. |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 628 | QualType BaseElementTy = getContext().getBaseElementType(FieldType); |
| 629 | llvm::Type *BasePtr = ConvertType(BaseElementTy); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 630 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 631 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(LHS.getAddress(), |
| 632 | BasePtr); |
| 633 | LHS = MakeAddrLValue(BaseAddrPtr, BaseElementTy); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 634 | |
| 635 | // Create an array index that will be used to walk over all of the |
| 636 | // objects we're constructing. |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 637 | ArrayIndexVar = CreateTempAlloca(SizeTy, "object.index"); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 638 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 639 | Builder.CreateStore(Zero, ArrayIndexVar); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 640 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 641 | |
| 642 | // Emit the block variables for the array indices, if any. |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 643 | for (unsigned I = 0, N = ArrayIndexes.size(); I != N; ++I) |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 644 | EmitAutoVarDecl(*ArrayIndexes[I]); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 647 | EmitAggMemberInitializer(*this, LHS, Init, ArrayIndexVar, FieldType, |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 648 | ArrayIndexes, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 649 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 650 | } |
John McCall | 074cae0 | 2013-02-01 05:11:40 +0000 | [diff] [blame] | 651 | |
| 652 | // Ensure that we destroy this object if an exception is thrown |
| 653 | // later in the constructor. |
| 654 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
| 655 | if (needsEHCleanup(dtorKind)) |
| 656 | pushEHDestroy(dtorKind, LHS.getAddress(), FieldType); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 657 | } |
| 658 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 659 | /// Checks whether the given constructor is a valid subject for the |
| 660 | /// complete-to-base constructor delegation optimization, i.e. |
| 661 | /// emitting the complete constructor as a simple call to the base |
| 662 | /// constructor. |
| 663 | static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) { |
| 664 | |
| 665 | // Currently we disable the optimization for classes with virtual |
| 666 | // bases because (1) the addresses of parameter variables need to be |
| 667 | // consistent across all initializers but (2) the delegate function |
| 668 | // call necessarily creates a second copy of the parameter variable. |
| 669 | // |
| 670 | // The limiting example (purely theoretical AFAIK): |
| 671 | // struct A { A(int &c) { c++; } }; |
| 672 | // struct B : virtual A { |
| 673 | // B(int count) : A(count) { printf("%d\n", count); } |
| 674 | // }; |
| 675 | // ...although even this example could in principle be emitted as a |
| 676 | // delegation since the address of the parameter doesn't escape. |
| 677 | if (Ctor->getParent()->getNumVBases()) { |
| 678 | // TODO: white-list trivial vbase initializers. This case wouldn't |
| 679 | // be subject to the restrictions below. |
| 680 | |
| 681 | // TODO: white-list cases where: |
| 682 | // - there are no non-reference parameters to the constructor |
| 683 | // - the initializers don't access any non-reference parameters |
| 684 | // - the initializers don't take the address of non-reference |
| 685 | // parameters |
| 686 | // - etc. |
| 687 | // If we ever add any of the above cases, remember that: |
| 688 | // - function-try-blocks will always blacklist this optimization |
| 689 | // - we need to perform the constructor prologue and cleanup in |
| 690 | // EmitConstructorBody. |
| 691 | |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | // We also disable the optimization for variadic functions because |
| 696 | // it's impossible to "re-pass" varargs. |
| 697 | if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic()) |
| 698 | return false; |
| 699 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 700 | // FIXME: Decide if we can do a delegation of a delegating constructor. |
| 701 | if (Ctor->isDelegatingConstructor()) |
| 702 | return false; |
| 703 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 704 | return true; |
| 705 | } |
| 706 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 707 | // Emit code in ctor (Prologue==true) or dtor (Prologue==false) |
| 708 | // to poison the extra field paddings inserted under |
| 709 | // -fsanitize-address-field-padding=1|2. |
| 710 | void CodeGenFunction::EmitAsanPrologueOrEpilogue(bool Prologue) { |
| 711 | ASTContext &Context = getContext(); |
| 712 | const CXXRecordDecl *ClassDecl = |
| 713 | Prologue ? cast<CXXConstructorDecl>(CurGD.getDecl())->getParent() |
| 714 | : cast<CXXDestructorDecl>(CurGD.getDecl())->getParent(); |
| 715 | if (!ClassDecl->mayInsertExtraPadding()) return; |
| 716 | |
| 717 | struct SizeAndOffset { |
| 718 | uint64_t Size; |
| 719 | uint64_t Offset; |
| 720 | }; |
| 721 | |
| 722 | unsigned PtrSize = CGM.getDataLayout().getPointerSizeInBits(); |
| 723 | const ASTRecordLayout &Info = Context.getASTRecordLayout(ClassDecl); |
| 724 | |
| 725 | // Populate sizes and offsets of fields. |
| 726 | SmallVector<SizeAndOffset, 16> SSV(Info.getFieldCount()); |
| 727 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) |
| 728 | SSV[i].Offset = |
| 729 | Context.toCharUnitsFromBits(Info.getFieldOffset(i)).getQuantity(); |
| 730 | |
| 731 | size_t NumFields = 0; |
| 732 | for (const auto *Field : ClassDecl->fields()) { |
| 733 | const FieldDecl *D = Field; |
| 734 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 735 | Context.getTypeInfoInChars(D->getType()); |
| 736 | CharUnits FieldSize = FieldInfo.first; |
| 737 | assert(NumFields < SSV.size()); |
| 738 | SSV[NumFields].Size = D->isBitField() ? 0 : FieldSize.getQuantity(); |
| 739 | NumFields++; |
| 740 | } |
| 741 | assert(NumFields == SSV.size()); |
| 742 | if (SSV.size() <= 1) return; |
| 743 | |
| 744 | // We will insert calls to __asan_* run-time functions. |
| 745 | // LLVM AddressSanitizer pass may decide to inline them later. |
| 746 | llvm::Type *Args[2] = {IntPtrTy, IntPtrTy}; |
| 747 | llvm::FunctionType *FTy = |
| 748 | llvm::FunctionType::get(CGM.VoidTy, Args, false); |
| 749 | llvm::Constant *F = CGM.CreateRuntimeFunction( |
| 750 | FTy, Prologue ? "__asan_poison_intra_object_redzone" |
| 751 | : "__asan_unpoison_intra_object_redzone"); |
| 752 | |
| 753 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 754 | ThisPtr = Builder.CreatePtrToInt(ThisPtr, IntPtrTy); |
| 755 | uint64_t TypeSize = Info.getNonVirtualSize().getQuantity(); |
| 756 | // For each field check if it has sufficient padding, |
| 757 | // if so (un)poison it with a call. |
| 758 | for (size_t i = 0; i < SSV.size(); i++) { |
| 759 | uint64_t AsanAlignment = 8; |
| 760 | uint64_t NextField = i == SSV.size() - 1 ? TypeSize : SSV[i + 1].Offset; |
| 761 | uint64_t PoisonSize = NextField - SSV[i].Offset - SSV[i].Size; |
| 762 | uint64_t EndOffset = SSV[i].Offset + SSV[i].Size; |
| 763 | if (PoisonSize < AsanAlignment || !SSV[i].Size || |
| 764 | (NextField % AsanAlignment) != 0) |
| 765 | continue; |
| 766 | Builder.CreateCall2( |
| 767 | F, Builder.CreateAdd(ThisPtr, Builder.getIntN(PtrSize, EndOffset)), |
| 768 | Builder.getIntN(PtrSize, PoisonSize)); |
| 769 | } |
| 770 | } |
| 771 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 772 | /// EmitConstructorBody - Emits the body of the current constructor. |
| 773 | void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 774 | EmitAsanPrologueOrEpilogue(true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 775 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
| 776 | CXXCtorType CtorType = CurGD.getCtorType(); |
| 777 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 778 | assert((CGM.getTarget().getCXXABI().hasConstructorVariants() || |
| 779 | CtorType == Ctor_Complete) && |
| 780 | "can only generate complete ctor for this ABI"); |
| 781 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 782 | // Before we go any further, try the complete->base constructor |
| 783 | // delegation optimization. |
Timur Iskhodzhanov | 8560791 | 2012-04-20 08:05:00 +0000 | [diff] [blame] | 784 | if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) && |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 785 | CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 786 | EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getLocEnd()); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 787 | return; |
| 788 | } |
| 789 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 790 | const FunctionDecl *Definition = 0; |
| 791 | Stmt *Body = Ctor->getBody(Definition); |
| 792 | assert(Definition == Ctor && "emitting wrong constructor body"); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 793 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 794 | // Enter the function-try-block before the constructor prologue if |
| 795 | // applicable. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 796 | bool IsTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 797 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 798 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 799 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 800 | RegionCounter Cnt = getPGORegionCounter(Body); |
| 801 | Cnt.beginRegion(Builder); |
| 802 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 803 | RunCleanupsScope RunCleanups(*this); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 804 | |
John McCall | 56ea377 | 2012-03-30 04:25:03 +0000 | [diff] [blame] | 805 | // TODO: in restricted cases, we can emit the vbase initializers of |
| 806 | // a complete ctor and then delegate to the base ctor. |
| 807 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 808 | // Emit the constructor prologue, i.e. the base and member |
| 809 | // initializers. |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 810 | EmitCtorPrologue(Ctor, CtorType, Args); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 811 | |
| 812 | // Emit the body of the statement. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 813 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 814 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 815 | else if (Body) |
| 816 | EmitStmt(Body); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 817 | |
| 818 | // Emit any cleanup blocks associated with the member or base |
| 819 | // initializers, which includes (along the exceptional path) the |
| 820 | // destructors for those members and bases that were fully |
| 821 | // constructed. |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 822 | RunCleanups.ForceCleanup(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 823 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 824 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 825 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 828 | namespace { |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 829 | /// RAII object to indicate that codegen is copying the value representation |
| 830 | /// instead of the object representation. Useful when copying a struct or |
| 831 | /// class which has uninitialized members and we're only performing |
| 832 | /// lvalue-to-rvalue conversion on the object but not its members. |
| 833 | class CopyingValueRepresentation { |
| 834 | public: |
| 835 | explicit CopyingValueRepresentation(CodeGenFunction &CGF) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 836 | : CGF(CGF), OldSanOpts(CGF.SanOpts) { |
| 837 | CGF.SanOpts.set(SanitizerKind::Bool, false); |
| 838 | CGF.SanOpts.set(SanitizerKind::Enum, false); |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 839 | } |
| 840 | ~CopyingValueRepresentation() { |
| 841 | CGF.SanOpts = OldSanOpts; |
| 842 | } |
| 843 | private: |
| 844 | CodeGenFunction &CGF; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 845 | SanitizerSet OldSanOpts; |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 846 | }; |
| 847 | } |
| 848 | |
| 849 | namespace { |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 850 | class FieldMemcpyizer { |
| 851 | public: |
| 852 | FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, |
| 853 | const VarDecl *SrcRec) |
| 854 | : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec), |
| 855 | RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 856 | FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0), |
| 857 | LastFieldOffset(0), LastAddedFieldIndex(0) {} |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 858 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 859 | bool isMemcpyableField(FieldDecl *F) const { |
| 860 | // Never memcpy fields when we are adding poisoned paddings. |
| 861 | if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding) |
| 862 | return false; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 863 | Qualifiers Qual = F->getType().getQualifiers(); |
| 864 | if (Qual.hasVolatile() || Qual.hasObjCLifetime()) |
| 865 | return false; |
| 866 | return true; |
| 867 | } |
| 868 | |
| 869 | void addMemcpyableField(FieldDecl *F) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 870 | if (!FirstField) |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 871 | addInitialField(F); |
| 872 | else |
| 873 | addNextField(F); |
| 874 | } |
| 875 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 876 | CharUnits getMemcpySize(uint64_t FirstByteOffset) const { |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 877 | unsigned LastFieldSize = |
| 878 | LastField->isBitField() ? |
| 879 | LastField->getBitWidthValue(CGF.getContext()) : |
| 880 | CGF.getContext().getTypeSize(LastField->getType()); |
| 881 | uint64_t MemcpySizeBits = |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 882 | LastFieldOffset + LastFieldSize - FirstByteOffset + |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 883 | CGF.getContext().getCharWidth() - 1; |
| 884 | CharUnits MemcpySize = |
| 885 | CGF.getContext().toCharUnitsFromBits(MemcpySizeBits); |
| 886 | return MemcpySize; |
| 887 | } |
| 888 | |
| 889 | void emitMemcpy() { |
| 890 | // Give the subclass a chance to bail out if it feels the memcpy isn't |
| 891 | // worth it (e.g. Hasn't aggregated enough data). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 892 | if (!FirstField) { |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | |
Lang Hames | 5e8577e | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 896 | CharUnits Alignment; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 897 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 898 | uint64_t FirstByteOffset; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 899 | if (FirstField->isBitField()) { |
| 900 | const CGRecordLayout &RL = |
| 901 | CGF.getTypes().getCGRecordLayout(FirstField->getParent()); |
| 902 | const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField); |
Lang Hames | 5e8577e | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 903 | Alignment = CharUnits::fromQuantity(BFInfo.StorageAlignment); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 904 | // FirstFieldOffset is not appropriate for bitfields, |
| 905 | // it won't tell us what the storage offset should be and thus might not |
| 906 | // be properly aligned. |
| 907 | // |
| 908 | // Instead calculate the storage offset using the offset of the field in |
| 909 | // the struct type. |
| 910 | const llvm::DataLayout &DL = CGF.CGM.getDataLayout(); |
| 911 | FirstByteOffset = |
| 912 | DL.getStructLayout(RL.getLLVMType()) |
| 913 | ->getElementOffsetInBits(RL.getLLVMFieldNo(FirstField)); |
Lang Hames | 5e8577e | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 914 | } else { |
Lang Hames | 23742cd | 2013-03-05 20:27:24 +0000 | [diff] [blame] | 915 | Alignment = CGF.getContext().getDeclAlign(FirstField); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 916 | FirstByteOffset = FirstFieldOffset; |
Lang Hames | 5e8577e | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 917 | } |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 918 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 919 | assert((CGF.getContext().toCharUnitsFromBits(FirstByteOffset) % |
Lang Hames | 5e8577e | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 920 | Alignment) == 0 && "Bad field alignment."); |
| 921 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 922 | CharUnits MemcpySize = getMemcpySize(FirstByteOffset); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 923 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
| 924 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 925 | LValue DestLV = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy); |
| 926 | LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField); |
| 927 | llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec)); |
| 928 | LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); |
| 929 | LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField); |
| 930 | |
| 931 | emitMemcpyIR(Dest.isBitField() ? Dest.getBitFieldAddr() : Dest.getAddress(), |
| 932 | Src.isBitField() ? Src.getBitFieldAddr() : Src.getAddress(), |
| 933 | MemcpySize, Alignment); |
| 934 | reset(); |
| 935 | } |
| 936 | |
| 937 | void reset() { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 938 | FirstField = nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | protected: |
| 942 | CodeGenFunction &CGF; |
| 943 | const CXXRecordDecl *ClassDecl; |
| 944 | |
| 945 | private: |
| 946 | |
| 947 | void emitMemcpyIR(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
| 948 | CharUnits Size, CharUnits Alignment) { |
| 949 | llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); |
| 950 | llvm::Type *DBP = |
| 951 | llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace()); |
| 952 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP); |
| 953 | |
| 954 | llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); |
| 955 | llvm::Type *SBP = |
| 956 | llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace()); |
| 957 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP); |
| 958 | |
| 959 | CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity(), |
| 960 | Alignment.getQuantity()); |
| 961 | } |
| 962 | |
| 963 | void addInitialField(FieldDecl *F) { |
| 964 | FirstField = F; |
| 965 | LastField = F; |
| 966 | FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex()); |
| 967 | LastFieldOffset = FirstFieldOffset; |
| 968 | LastAddedFieldIndex = F->getFieldIndex(); |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | void addNextField(FieldDecl *F) { |
John McCall | 402cd22 | 2013-05-07 05:20:46 +0000 | [diff] [blame] | 973 | // For the most part, the following invariant will hold: |
| 974 | // F->getFieldIndex() == LastAddedFieldIndex + 1 |
| 975 | // The one exception is that Sema won't add a copy-initializer for an |
| 976 | // unnamed bitfield, which will show up here as a gap in the sequence. |
| 977 | assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 && |
| 978 | "Cannot aggregate fields out of order."); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 979 | LastAddedFieldIndex = F->getFieldIndex(); |
| 980 | |
| 981 | // The 'first' and 'last' fields are chosen by offset, rather than field |
| 982 | // index. This allows the code to support bitfields, as well as regular |
| 983 | // fields. |
| 984 | uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex()); |
| 985 | if (FOffset < FirstFieldOffset) { |
| 986 | FirstField = F; |
| 987 | FirstFieldOffset = FOffset; |
| 988 | } else if (FOffset > LastFieldOffset) { |
| 989 | LastField = F; |
| 990 | LastFieldOffset = FOffset; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | const VarDecl *SrcRec; |
| 995 | const ASTRecordLayout &RecLayout; |
| 996 | FieldDecl *FirstField; |
| 997 | FieldDecl *LastField; |
| 998 | uint64_t FirstFieldOffset, LastFieldOffset; |
| 999 | unsigned LastAddedFieldIndex; |
| 1000 | }; |
| 1001 | |
| 1002 | class ConstructorMemcpyizer : public FieldMemcpyizer { |
| 1003 | private: |
| 1004 | |
| 1005 | /// Get source argument for copy constructor. Returns null if not a copy |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1006 | /// constructor. |
| 1007 | static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF, |
| 1008 | const CXXConstructorDecl *CD, |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1009 | FunctionArgList &Args) { |
Jordan Rose | a7b8797 | 2013-08-07 16:16:48 +0000 | [diff] [blame] | 1010 | if (CD->isCopyOrMoveConstructor() && CD->isDefaulted()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1011 | return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)]; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1012 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | // Returns true if a CXXCtorInitializer represents a member initialization |
| 1016 | // that can be rolled into a memcpy. |
| 1017 | bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const { |
| 1018 | if (!MemcpyableCtor) |
| 1019 | return false; |
| 1020 | FieldDecl *Field = MemberInit->getMember(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1021 | assert(Field && "No field for member init."); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1022 | QualType FieldType = Field->getType(); |
| 1023 | CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit()); |
| 1024 | |
| 1025 | // Bail out on non-POD, not-trivially-constructable members. |
| 1026 | if (!(CE && CE->getConstructor()->isTrivial()) && |
| 1027 | !(FieldType.isTriviallyCopyableType(CGF.getContext()) || |
| 1028 | FieldType->isReferenceType())) |
| 1029 | return false; |
| 1030 | |
| 1031 | // Bail out on volatile fields. |
| 1032 | if (!isMemcpyableField(Field)) |
| 1033 | return false; |
| 1034 | |
| 1035 | // Otherwise we're good. |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
| 1039 | public: |
| 1040 | ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD, |
| 1041 | FunctionArgList &Args) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1042 | : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CGF, CD, Args)), |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1043 | ConstructorDecl(CD), |
Jordan Rose | a7b8797 | 2013-08-07 16:16:48 +0000 | [diff] [blame] | 1044 | MemcpyableCtor(CD->isDefaulted() && |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1045 | CD->isCopyOrMoveConstructor() && |
| 1046 | CGF.getLangOpts().getGC() == LangOptions::NonGC), |
| 1047 | Args(Args) { } |
| 1048 | |
| 1049 | void addMemberInitializer(CXXCtorInitializer *MemberInit) { |
| 1050 | if (isMemberInitMemcpyable(MemberInit)) { |
| 1051 | AggregatedInits.push_back(MemberInit); |
| 1052 | addMemcpyableField(MemberInit->getMember()); |
| 1053 | } else { |
| 1054 | emitAggregatedInits(); |
| 1055 | EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit, |
| 1056 | ConstructorDecl, Args); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | void emitAggregatedInits() { |
| 1061 | if (AggregatedInits.size() <= 1) { |
| 1062 | // This memcpy is too small to be worthwhile. Fall back on default |
| 1063 | // codegen. |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1064 | if (!AggregatedInits.empty()) { |
| 1065 | CopyingValueRepresentation CVR(CGF); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1066 | EmitMemberInitializer(CGF, ConstructorDecl->getParent(), |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1067 | AggregatedInits[0], ConstructorDecl, Args); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1068 | } |
| 1069 | reset(); |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | pushEHDestructors(); |
| 1074 | emitMemcpy(); |
| 1075 | AggregatedInits.clear(); |
| 1076 | } |
| 1077 | |
| 1078 | void pushEHDestructors() { |
| 1079 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 1080 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
| 1081 | LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy); |
| 1082 | |
| 1083 | for (unsigned i = 0; i < AggregatedInits.size(); ++i) { |
| 1084 | QualType FieldType = AggregatedInits[i]->getMember()->getType(); |
| 1085 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
| 1086 | if (CGF.needsEHCleanup(dtorKind)) |
| 1087 | CGF.pushEHDestroy(dtorKind, LHS.getAddress(), FieldType); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | void finish() { |
| 1092 | emitAggregatedInits(); |
| 1093 | } |
| 1094 | |
| 1095 | private: |
| 1096 | const CXXConstructorDecl *ConstructorDecl; |
| 1097 | bool MemcpyableCtor; |
| 1098 | FunctionArgList &Args; |
| 1099 | SmallVector<CXXCtorInitializer*, 16> AggregatedInits; |
| 1100 | }; |
| 1101 | |
| 1102 | class AssignmentMemcpyizer : public FieldMemcpyizer { |
| 1103 | private: |
| 1104 | |
| 1105 | // Returns the memcpyable field copied by the given statement, if one |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1106 | // exists. Otherwise returns null. |
| 1107 | FieldDecl *getMemcpyableField(Stmt *S) { |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1108 | if (!AssignmentsMemcpyable) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1109 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1110 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) { |
| 1111 | // Recognise trivial assignments. |
| 1112 | if (BO->getOpcode() != BO_Assign) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1113 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1114 | MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS()); |
| 1115 | if (!ME) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1116 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1117 | FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 1118 | if (!Field || !isMemcpyableField(Field)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1119 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1120 | Stmt *RHS = BO->getRHS(); |
| 1121 | if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS)) |
| 1122 | RHS = EC->getSubExpr(); |
| 1123 | if (!RHS) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1124 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1125 | MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS); |
| 1126 | if (dyn_cast<FieldDecl>(ME2->getMemberDecl()) != Field) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1127 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1128 | return Field; |
| 1129 | } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) { |
| 1130 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl()); |
| 1131 | if (!(MD && (MD->isCopyAssignmentOperator() || |
| 1132 | MD->isMoveAssignmentOperator()) && |
| 1133 | MD->isTrivial())) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1134 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1135 | MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument()); |
| 1136 | if (!IOA) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1137 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1138 | FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl()); |
| 1139 | if (!Field || !isMemcpyableField(Field)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1140 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1141 | MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0)); |
| 1142 | if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl())) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1143 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1144 | return Field; |
| 1145 | } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 1146 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); |
| 1147 | if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1148 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1149 | Expr *DstPtr = CE->getArg(0); |
| 1150 | if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr)) |
| 1151 | DstPtr = DC->getSubExpr(); |
| 1152 | UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr); |
| 1153 | if (!DUO || DUO->getOpcode() != UO_AddrOf) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1154 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1155 | MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr()); |
| 1156 | if (!ME) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1157 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1158 | FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 1159 | if (!Field || !isMemcpyableField(Field)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1160 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1161 | Expr *SrcPtr = CE->getArg(1); |
| 1162 | if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr)) |
| 1163 | SrcPtr = SC->getSubExpr(); |
| 1164 | UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr); |
| 1165 | if (!SUO || SUO->getOpcode() != UO_AddrOf) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1166 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1167 | MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr()); |
| 1168 | if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl())) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1169 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1170 | return Field; |
| 1171 | } |
| 1172 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1173 | return nullptr; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | bool AssignmentsMemcpyable; |
| 1177 | SmallVector<Stmt*, 16> AggregatedStmts; |
| 1178 | |
| 1179 | public: |
| 1180 | |
| 1181 | AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD, |
| 1182 | FunctionArgList &Args) |
| 1183 | : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]), |
| 1184 | AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) { |
| 1185 | assert(Args.size() == 2); |
| 1186 | } |
| 1187 | |
| 1188 | void emitAssignment(Stmt *S) { |
| 1189 | FieldDecl *F = getMemcpyableField(S); |
| 1190 | if (F) { |
| 1191 | addMemcpyableField(F); |
| 1192 | AggregatedStmts.push_back(S); |
| 1193 | } else { |
| 1194 | emitAggregatedStmts(); |
| 1195 | CGF.EmitStmt(S); |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | void emitAggregatedStmts() { |
| 1200 | if (AggregatedStmts.size() <= 1) { |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1201 | if (!AggregatedStmts.empty()) { |
| 1202 | CopyingValueRepresentation CVR(CGF); |
| 1203 | CGF.EmitStmt(AggregatedStmts[0]); |
| 1204 | } |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1205 | reset(); |
| 1206 | } |
| 1207 | |
| 1208 | emitMemcpy(); |
| 1209 | AggregatedStmts.clear(); |
| 1210 | } |
| 1211 | |
| 1212 | void finish() { |
| 1213 | emitAggregatedStmts(); |
| 1214 | } |
| 1215 | }; |
| 1216 | |
| 1217 | } |
| 1218 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1219 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 1220 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1221 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1222 | CXXCtorType CtorType, |
| 1223 | FunctionArgList &Args) { |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1224 | if (CD->isDelegatingConstructor()) |
| 1225 | return EmitDelegatingCXXConstructorCall(CD, Args); |
| 1226 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1227 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 1228 | |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1229 | CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 1230 | E = CD->init_end(); |
| 1231 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1232 | llvm::BasicBlock *BaseCtorContinueBB = nullptr; |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1233 | if (ClassDecl->getNumVBases() && |
| 1234 | !CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
| 1235 | // The ABIs that don't have constructor variants need to put a branch |
| 1236 | // before the virtual base initialization code. |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1237 | BaseCtorContinueBB = |
| 1238 | CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1239 | assert(BaseCtorContinueBB); |
| 1240 | } |
| 1241 | |
| 1242 | // Virtual base initializers first. |
| 1243 | for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) { |
| 1244 | EmitBaseInitializer(*this, ClassDecl, *B, CtorType); |
| 1245 | } |
| 1246 | |
| 1247 | if (BaseCtorContinueBB) { |
| 1248 | // Complete object handler should continue to the remaining initializers. |
| 1249 | Builder.CreateBr(BaseCtorContinueBB); |
| 1250 | EmitBlock(BaseCtorContinueBB); |
| 1251 | } |
| 1252 | |
| 1253 | // Then, non-virtual base initializers. |
| 1254 | for (; B != E && (*B)->isBaseInitializer(); B++) { |
| 1255 | assert(!(*B)->isBaseVirtual()); |
| 1256 | EmitBaseInitializer(*this, ClassDecl, *B, CtorType); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1259 | InitializeVTablePointers(ClassDecl); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 1260 | |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1261 | // And finally, initialize class members. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1262 | FieldConstructionScope FCS(*this, CXXThisValue); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1263 | ConstructorMemcpyizer CM(*this, CD, Args); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1264 | for (; B != E; B++) { |
| 1265 | CXXCtorInitializer *Member = (*B); |
| 1266 | assert(!Member->isBaseInitializer()); |
| 1267 | assert(Member->isAnyMemberInitializer() && |
| 1268 | "Delegating initializer on non-delegating constructor"); |
| 1269 | CM.addMemberInitializer(Member); |
| 1270 | } |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1271 | CM.finish(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1274 | static bool |
| 1275 | FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field); |
| 1276 | |
| 1277 | static bool |
| 1278 | HasTrivialDestructorBody(ASTContext &Context, |
| 1279 | const CXXRecordDecl *BaseClassDecl, |
| 1280 | const CXXRecordDecl *MostDerivedClassDecl) |
| 1281 | { |
| 1282 | // If the destructor is trivial we don't have to check anything else. |
| 1283 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1284 | return true; |
| 1285 | |
| 1286 | if (!BaseClassDecl->getDestructor()->hasTrivialBody()) |
| 1287 | return false; |
| 1288 | |
| 1289 | // Check fields. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1290 | for (const auto *Field : BaseClassDecl->fields()) |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1291 | if (!FieldHasTrivialDestructorBody(Context, Field)) |
| 1292 | return false; |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1293 | |
| 1294 | // Check non-virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1295 | for (const auto &I : BaseClassDecl->bases()) { |
| 1296 | if (I.isVirtual()) |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1297 | continue; |
| 1298 | |
| 1299 | const CXXRecordDecl *NonVirtualBase = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1300 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1301 | if (!HasTrivialDestructorBody(Context, NonVirtualBase, |
| 1302 | MostDerivedClassDecl)) |
| 1303 | return false; |
| 1304 | } |
| 1305 | |
| 1306 | if (BaseClassDecl == MostDerivedClassDecl) { |
| 1307 | // Check virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1308 | for (const auto &I : BaseClassDecl->vbases()) { |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1309 | const CXXRecordDecl *VirtualBase = |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1310 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1311 | if (!HasTrivialDestructorBody(Context, VirtualBase, |
| 1312 | MostDerivedClassDecl)) |
| 1313 | return false; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | return true; |
| 1318 | } |
| 1319 | |
| 1320 | static bool |
| 1321 | FieldHasTrivialDestructorBody(ASTContext &Context, |
| 1322 | const FieldDecl *Field) |
| 1323 | { |
| 1324 | QualType FieldBaseElementType = Context.getBaseElementType(Field->getType()); |
| 1325 | |
| 1326 | const RecordType *RT = FieldBaseElementType->getAs<RecordType>(); |
| 1327 | if (!RT) |
| 1328 | return true; |
| 1329 | |
| 1330 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1331 | return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl); |
| 1332 | } |
| 1333 | |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1334 | /// CanSkipVTablePointerInitialization - Check whether we need to initialize |
| 1335 | /// any vtable pointers before calling this destructor. |
| 1336 | static bool CanSkipVTablePointerInitialization(ASTContext &Context, |
Anders Carlsson | e3d6cf2 | 2011-05-16 04:08:36 +0000 | [diff] [blame] | 1337 | const CXXDestructorDecl *Dtor) { |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1338 | if (!Dtor->hasTrivialBody()) |
| 1339 | return false; |
| 1340 | |
| 1341 | // Check the fields. |
| 1342 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1343 | for (const auto *Field : ClassDecl->fields()) |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1344 | if (!FieldHasTrivialDestructorBody(Context, Field)) |
| 1345 | return false; |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1346 | |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1350 | /// EmitDestructorBody - Emits the body of the current destructor. |
| 1351 | void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { |
| 1352 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl()); |
| 1353 | CXXDtorType DtorType = CurGD.getDtorType(); |
| 1354 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1355 | // The call to operator delete in a deleting destructor happens |
| 1356 | // outside of the function-try-block, which means it's always |
| 1357 | // possible to delegate the destructor body to the complete |
| 1358 | // destructor. Do so. |
| 1359 | if (DtorType == Dtor_Deleting) { |
| 1360 | EnterDtorCleanups(Dtor, Dtor_Deleting); |
| 1361 | EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1362 | /*Delegating=*/false, LoadCXXThis()); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1363 | PopCleanupBlock(); |
| 1364 | return; |
| 1365 | } |
| 1366 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1367 | Stmt *Body = Dtor->getBody(); |
| 1368 | |
| 1369 | // If the body is a function-try-block, enter the try before |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1370 | // anything else. |
| 1371 | bool isTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1372 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1373 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1374 | EmitAsanPrologueOrEpilogue(false); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1375 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1376 | // Enter the epilogue cleanups. |
| 1377 | RunCleanupsScope DtorEpilogue(*this); |
| 1378 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1379 | // If this is the complete variant, just invoke the base variant; |
| 1380 | // the epilogue will destruct the virtual bases. But we can't do |
| 1381 | // this optimization if the body is a function-try-block, because |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1382 | // we'd introduce *two* handler blocks. In the Microsoft ABI, we |
| 1383 | // always delegate because we might not have a definition in this TU. |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1384 | switch (DtorType) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1385 | case Dtor_Comdat: |
| 1386 | llvm_unreachable("not expecting a COMDAT"); |
| 1387 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1388 | case Dtor_Deleting: llvm_unreachable("already handled deleting case"); |
| 1389 | |
| 1390 | case Dtor_Complete: |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1391 | assert((Body || getTarget().getCXXABI().isMicrosoft()) && |
| 1392 | "can't emit a dtor without a body for non-Microsoft ABIs"); |
| 1393 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1394 | // Enter the cleanup scopes for virtual bases. |
| 1395 | EnterDtorCleanups(Dtor, Dtor_Complete); |
| 1396 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1397 | if (!isTryBody) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1398 | EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1399 | /*Delegating=*/false, LoadCXXThis()); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1400 | break; |
| 1401 | } |
| 1402 | // Fallthrough: act like we're in the base variant. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1403 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1404 | case Dtor_Base: |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1405 | assert(Body); |
| 1406 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1407 | RegionCounter Cnt = getPGORegionCounter(Body); |
| 1408 | Cnt.beginRegion(Builder); |
| 1409 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1410 | // Enter the cleanup scopes for fields and non-virtual bases. |
| 1411 | EnterDtorCleanups(Dtor, Dtor_Base); |
| 1412 | |
| 1413 | // Initialize the vtable pointers before entering the body. |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1414 | if (!CanSkipVTablePointerInitialization(getContext(), Dtor)) |
| 1415 | InitializeVTablePointers(Dtor->getParent()); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1416 | |
| 1417 | if (isTryBody) |
| 1418 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 1419 | else if (Body) |
| 1420 | EmitStmt(Body); |
| 1421 | else { |
| 1422 | assert(Dtor->isImplicit() && "bodyless dtor not implicit"); |
| 1423 | // nothing to do besides what's in the epilogue |
| 1424 | } |
Fariborz Jahanian | 5abec14 | 2011-02-02 23:12:46 +0000 | [diff] [blame] | 1425 | // -fapple-kext must inline any call to this dtor into |
| 1426 | // the caller's body. |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 1427 | if (getLangOpts().AppleKext) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1428 | CurFn->addFnAttr(llvm::Attribute::AlwaysInline); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1429 | break; |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1432 | // Jump out through the epilogue cleanups. |
| 1433 | DtorEpilogue.ForceCleanup(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1434 | |
| 1435 | // Exit the try if applicable. |
| 1436 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1437 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1440 | void CodeGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &Args) { |
| 1441 | const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl()); |
| 1442 | const Stmt *RootS = AssignOp->getBody(); |
| 1443 | assert(isa<CompoundStmt>(RootS) && |
| 1444 | "Body of an implicit assignment operator should be compound stmt."); |
| 1445 | const CompoundStmt *RootCS = cast<CompoundStmt>(RootS); |
| 1446 | |
| 1447 | LexicalScope Scope(*this, RootCS->getSourceRange()); |
| 1448 | |
| 1449 | AssignmentMemcpyizer AM(*this, AssignOp, Args); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1450 | for (auto *I : RootCS->body()) |
| 1451 | AM.emitAssignment(I); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1452 | AM.finish(); |
| 1453 | } |
| 1454 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1455 | namespace { |
| 1456 | /// Call the operator delete associated with the current destructor. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1457 | struct CallDtorDelete : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1458 | CallDtorDelete() {} |
| 1459 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1460 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1461 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
| 1462 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 1463 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(), |
| 1464 | CGF.getContext().getTagDeclType(ClassDecl)); |
| 1465 | } |
| 1466 | }; |
| 1467 | |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1468 | struct CallDtorDeleteConditional : EHScopeStack::Cleanup { |
| 1469 | llvm::Value *ShouldDeleteCondition; |
| 1470 | public: |
| 1471 | CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition) |
| 1472 | : ShouldDeleteCondition(ShouldDeleteCondition) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1473 | assert(ShouldDeleteCondition != nullptr); |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1476 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1477 | llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete"); |
| 1478 | llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue"); |
| 1479 | llvm::Value *ShouldCallDelete |
| 1480 | = CGF.Builder.CreateIsNull(ShouldDeleteCondition); |
| 1481 | CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB); |
| 1482 | |
| 1483 | CGF.EmitBlock(callDeleteBB); |
| 1484 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
| 1485 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 1486 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(), |
| 1487 | CGF.getContext().getTagDeclType(ClassDecl)); |
| 1488 | CGF.Builder.CreateBr(continueBB); |
| 1489 | |
| 1490 | CGF.EmitBlock(continueBB); |
| 1491 | } |
| 1492 | }; |
| 1493 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1494 | class DestroyField : public EHScopeStack::Cleanup { |
| 1495 | const FieldDecl *field; |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1496 | CodeGenFunction::Destroyer *destroyer; |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1497 | bool useEHCleanupForArray; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1498 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1499 | public: |
| 1500 | DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer, |
| 1501 | bool useEHCleanupForArray) |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1502 | : field(field), destroyer(destroyer), |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1503 | useEHCleanupForArray(useEHCleanupForArray) {} |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1504 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1505 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1506 | // Find the address of the field. |
| 1507 | llvm::Value *thisValue = CGF.LoadCXXThis(); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1508 | QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent()); |
| 1509 | LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy); |
| 1510 | LValue LV = CGF.EmitLValueForField(ThisLV, field); |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1511 | assert(LV.isSimple()); |
| 1512 | |
| 1513 | CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer, |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1514 | flags.isForNormalCleanup() && useEHCleanupForArray); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1515 | } |
| 1516 | }; |
| 1517 | } |
| 1518 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1519 | /// \brief Emit all code that comes at the end of class's |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1520 | /// destructor. This is to call destructors on members and base classes |
| 1521 | /// in reverse order of their construction. |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1522 | void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD, |
| 1523 | CXXDtorType DtorType) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1524 | assert((!DD->isTrivial() || DD->hasAttr<DLLExportAttr>()) && |
| 1525 | "Should not emit dtor epilogue for non-exported trivial dtor!"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1526 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1527 | // The deleting-destructor phase just needs to call the appropriate |
| 1528 | // operator delete that Sema picked up. |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1529 | if (DtorType == Dtor_Deleting) { |
| 1530 | assert(DD->getOperatorDelete() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1531 | "operator delete missing - EnterDtorCleanups"); |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1532 | if (CXXStructorImplicitParamValue) { |
| 1533 | // If there is an implicit param to the deleting dtor, it's a boolean |
| 1534 | // telling whether we should call delete at the end of the dtor. |
| 1535 | EHStack.pushCleanup<CallDtorDeleteConditional>( |
| 1536 | NormalAndEHCleanup, CXXStructorImplicitParamValue); |
| 1537 | } else { |
| 1538 | EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup); |
| 1539 | } |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1540 | return; |
| 1541 | } |
| 1542 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1543 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 1544 | |
Richard Smith | 416f63e | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 1545 | // Unions have no bases and do not call field destructors. |
| 1546 | if (ClassDecl->isUnion()) |
| 1547 | return; |
| 1548 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1549 | // The complete-destructor phase just destructs all the virtual bases. |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1550 | if (DtorType == Dtor_Complete) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1551 | |
| 1552 | // We push them in the forward order so that they'll be popped in |
| 1553 | // the reverse order. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1554 | for (const auto &Base : ClassDecl->vbases()) { |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1555 | CXXRecordDecl *BaseClassDecl |
| 1556 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1557 | |
| 1558 | // Ignore trivial destructors. |
| 1559 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1560 | continue; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1561 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1562 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 1563 | BaseClassDecl, |
| 1564 | /*BaseIsVirtual*/ true); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1565 | } |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1566 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1567 | return; |
| 1568 | } |
| 1569 | |
| 1570 | assert(DtorType == Dtor_Base); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1571 | |
| 1572 | // Destroy non-virtual bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1573 | for (const auto &Base : ClassDecl->bases()) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1574 | // Ignore virtual bases. |
| 1575 | if (Base.isVirtual()) |
| 1576 | continue; |
| 1577 | |
| 1578 | CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl(); |
| 1579 | |
| 1580 | // Ignore trivial destructors. |
| 1581 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1582 | continue; |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1583 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1584 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 1585 | BaseClassDecl, |
| 1586 | /*BaseIsVirtual*/ false); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | // Destroy direct fields. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1590 | for (const auto *Field : ClassDecl->fields()) { |
| 1591 | QualType type = Field->getType(); |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1592 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 1593 | if (!dtorKind) continue; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1594 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 1595 | // Anonymous union members do not have their destructors called. |
| 1596 | const RecordType *RT = type->getAsUnionType(); |
| 1597 | if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue; |
| 1598 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1599 | CleanupKind cleanupKind = getCleanupKind(dtorKind); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1600 | EHStack.pushCleanup<DestroyField>(cleanupKind, Field, |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1601 | getDestroyer(dtorKind), |
| 1602 | cleanupKind & EHCleanup); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1603 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1606 | /// EmitCXXAggrConstructorCall - Emit a loop to call a particular |
| 1607 | /// constructor for each of several members of an array. |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1608 | /// |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1609 | /// \param ctor the constructor to call for each element |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1610 | /// \param arrayType the type of the array to initialize |
| 1611 | /// \param arrayBegin an arrayType* |
| 1612 | /// \param zeroInitialize true if each element should be |
| 1613 | /// zero-initialized before it is constructed |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1614 | void CodeGenFunction::EmitCXXAggrConstructorCall( |
| 1615 | const CXXConstructorDecl *ctor, const ConstantArrayType *arrayType, |
| 1616 | llvm::Value *arrayBegin, const CXXConstructExpr *E, bool zeroInitialize) { |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1617 | QualType elementType; |
| 1618 | llvm::Value *numElements = |
| 1619 | emitArrayLength(arrayType, elementType, arrayBegin); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1620 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1621 | EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, E, zeroInitialize); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1624 | /// EmitCXXAggrConstructorCall - Emit a loop to call a particular |
| 1625 | /// constructor for each of several members of an array. |
| 1626 | /// |
| 1627 | /// \param ctor the constructor to call for each element |
| 1628 | /// \param numElements the number of elements in the array; |
John McCall | dd376ca | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1629 | /// may be zero |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1630 | /// \param arrayBegin a T*, where T is the type constructed by ctor |
| 1631 | /// \param zeroInitialize true if each element should be |
| 1632 | /// zero-initialized before it is constructed |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1633 | void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor, |
| 1634 | llvm::Value *numElements, |
| 1635 | llvm::Value *arrayBegin, |
| 1636 | const CXXConstructExpr *E, |
| 1637 | bool zeroInitialize) { |
John McCall | dd376ca | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1638 | |
| 1639 | // It's legal for numElements to be zero. This can happen both |
| 1640 | // dynamically, because x can be zero in 'new A[x]', and statically, |
| 1641 | // because of GCC extensions that permit zero-length arrays. There |
| 1642 | // are probably legitimate places where we could assume that this |
| 1643 | // doesn't happen, but it's not clear that it's worth it. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1644 | llvm::BranchInst *zeroCheckBranch = nullptr; |
John McCall | dd376ca | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1645 | |
| 1646 | // Optimize for a constant count. |
| 1647 | llvm::ConstantInt *constantCount |
| 1648 | = dyn_cast<llvm::ConstantInt>(numElements); |
| 1649 | if (constantCount) { |
| 1650 | // Just skip out if the constant count is zero. |
| 1651 | if (constantCount->isZero()) return; |
| 1652 | |
| 1653 | // Otherwise, emit the check. |
| 1654 | } else { |
| 1655 | llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop"); |
| 1656 | llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty"); |
| 1657 | zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB); |
| 1658 | EmitBlock(loopBB); |
| 1659 | } |
| 1660 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1661 | // Find the end of the array. |
| 1662 | llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements, |
| 1663 | "arrayctor.end"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1664 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1665 | // Enter the loop, setting up a phi for the current location to initialize. |
| 1666 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); |
| 1667 | llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop"); |
| 1668 | EmitBlock(loopBB); |
| 1669 | llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2, |
| 1670 | "arrayctor.cur"); |
| 1671 | cur->addIncoming(arrayBegin, entryBB); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1672 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1673 | // Inside the loop body, emit the constructor call on the array element. |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1674 | |
| 1675 | QualType type = getContext().getTypeDeclType(ctor->getParent()); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1676 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1677 | // Zero initialize the storage, if requested. |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1678 | if (zeroInitialize) |
| 1679 | EmitNullInitialization(cur, type); |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1680 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1681 | // C++ [class.temporary]p4: |
| 1682 | // There are two contexts in which temporaries are destroyed at a different |
| 1683 | // point than the end of the full-expression. The first context is when a |
| 1684 | // default constructor is called to initialize an element of an array. |
| 1685 | // If the constructor has one or more default arguments, the destruction of |
| 1686 | // every temporary created in a default argument expression is sequenced |
| 1687 | // before the construction of the next array element, if any. |
| 1688 | |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1689 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1690 | RunCleanupsScope Scope(*this); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1691 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1692 | // Evaluate the constructor and its arguments in a regular |
| 1693 | // partial-destroy cleanup. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1694 | if (getLangOpts().Exceptions && |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1695 | !ctor->getParent()->hasTrivialDestructor()) { |
| 1696 | Destroyer *destroyer = destroyCXXObject; |
| 1697 | pushRegularPartialArrayCleanup(arrayBegin, cur, type, *destroyer); |
| 1698 | } |
| 1699 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1700 | EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/false, |
| 1701 | /*Delegating=*/false, cur, E); |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1702 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1703 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1704 | // Go to the next element. |
| 1705 | llvm::Value *next = |
| 1706 | Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1), |
| 1707 | "arrayctor.next"); |
| 1708 | cur->addIncoming(next, Builder.GetInsertBlock()); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1709 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1710 | // Check whether that's the end of the loop. |
| 1711 | llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done"); |
| 1712 | llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont"); |
| 1713 | Builder.CreateCondBr(done, contBB, loopBB); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1714 | |
John McCall | dd376ca | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1715 | // Patch the earlier check to skip over the loop. |
| 1716 | if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB); |
| 1717 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1718 | EmitBlock(contBB); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1721 | void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF, |
| 1722 | llvm::Value *addr, |
| 1723 | QualType type) { |
| 1724 | const RecordType *rtype = type->castAs<RecordType>(); |
| 1725 | const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl()); |
| 1726 | const CXXDestructorDecl *dtor = record->getDestructor(); |
| 1727 | assert(!dtor->isTrivial()); |
| 1728 | CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1729 | /*Delegating=*/false, addr); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1732 | void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 1733 | CXXCtorType Type, |
| 1734 | bool ForVirtualBase, |
| 1735 | bool Delegating, llvm::Value *This, |
| 1736 | const CXXConstructExpr *E) { |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1737 | // If this is a trivial constructor, just emit what's needed. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1738 | if (D->isTrivial() && !D->getParent()->mayInsertExtraPadding()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1739 | if (E->getNumArgs() == 0) { |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1740 | // Trivial default constructor, no codegen required. |
| 1741 | assert(D->isDefaultConstructor() && |
| 1742 | "trivial 0-arg ctor not a default ctor"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1743 | return; |
| 1744 | } |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1745 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1746 | assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor"); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 1747 | assert(D->isCopyOrMoveConstructor() && |
| 1748 | "trivial 1-arg ctor not a copy/move ctor"); |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1749 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1750 | const Expr *Arg = E->getArg(0); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1751 | QualType SrcTy = Arg->getType(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1752 | llvm::Value *Src = EmitLValue(Arg).getAddress(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1753 | QualType DestTy = getContext().getTypeDeclType(D->getParent()); |
| 1754 | EmitAggregateCopyCtor(This, Src, DestTy, SrcTy); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | } |
| 1757 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1758 | // C++11 [class.mfct.non-static]p2: |
| 1759 | // If a non-static member function of a class X is called for an object that |
| 1760 | // is not of type X, or of a type derived from X, the behavior is undefined. |
| 1761 | // FIXME: Provide a source location here. |
| 1762 | EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, SourceLocation(), This, |
| 1763 | getContext().getRecordType(D->getParent())); |
| 1764 | |
| 1765 | CallArgList Args; |
| 1766 | |
| 1767 | // Push the this ptr. |
| 1768 | Args.add(RValue::get(This), D->getThisType(getContext())); |
| 1769 | |
| 1770 | // Add the rest of the user-supplied arguments. |
| 1771 | const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1772 | EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end(), E->getConstructor()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1773 | |
| 1774 | // Insert any ABI-specific implicit constructor arguments. |
| 1775 | unsigned ExtraArgs = CGM.getCXXABI().addImplicitConstructorArgs( |
| 1776 | *this, D, Type, ForVirtualBase, Delegating, Args); |
| 1777 | |
| 1778 | // Emit the call. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1779 | llvm::Value *Callee = CGM.getAddrOfCXXStructor(D, getFromCtorType(Type)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1780 | const CGFunctionInfo &Info = |
| 1781 | CGM.getTypes().arrangeCXXConstructorCall(Args, D, Type, ExtraArgs); |
| 1782 | EmitCall(Info, Callee, ReturnValueSlot(), Args, D); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1785 | void |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1786 | CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, |
| 1787 | llvm::Value *This, llvm::Value *Src, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1788 | const CXXConstructExpr *E) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1789 | if (D->isTrivial() && |
| 1790 | !D->getParent()->mayInsertExtraPadding()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1791 | assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor"); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 1792 | assert(D->isCopyOrMoveConstructor() && |
| 1793 | "trivial 1-arg ctor not a copy/move ctor"); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1794 | EmitAggregateCopyCtor(This, Src, |
| 1795 | getContext().getTypeDeclType(D->getParent()), |
| 1796 | E->arg_begin()->getType()); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1797 | return; |
| 1798 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1799 | llvm::Value *Callee = CGM.getAddrOfCXXStructor(D, StructorType::Complete); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1800 | assert(D->isInstance() && |
| 1801 | "Trying to emit a member call expr on a static method!"); |
| 1802 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1803 | const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1804 | |
| 1805 | CallArgList Args; |
| 1806 | |
| 1807 | // Push the this ptr. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1808 | Args.add(RValue::get(This), D->getThisType(getContext())); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1809 | |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1810 | // Push the src ptr. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1811 | QualType QT = *(FPT->param_type_begin()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1812 | llvm::Type *t = CGM.getTypes().ConvertType(QT); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1813 | Src = Builder.CreateBitCast(Src, t); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1814 | Args.add(RValue::get(Src), QT); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1815 | |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1816 | // Skip over first argument (Src). |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1817 | EmitCallArgs(Args, FPT, E->arg_begin() + 1, E->arg_end(), E->getConstructor(), |
| 1818 | /*ParamsToSkip*/ 1); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1819 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1820 | EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, RequiredArgs::All), |
| 1821 | Callee, ReturnValueSlot(), Args, D); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | void |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1825 | CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1826 | CXXCtorType CtorType, |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1827 | const FunctionArgList &Args, |
| 1828 | SourceLocation Loc) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1829 | CallArgList DelegateArgs; |
| 1830 | |
| 1831 | FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); |
| 1832 | assert(I != E && "no parameters to constructor"); |
| 1833 | |
| 1834 | // this |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1835 | DelegateArgs.add(RValue::get(LoadCXXThis()), (*I)->getType()); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1836 | ++I; |
| 1837 | |
| 1838 | // vtt |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1839 | if (llvm::Value *VTT = GetVTTParameter(GlobalDecl(Ctor, CtorType), |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1840 | /*ForVirtualBase=*/false, |
| 1841 | /*Delegating=*/true)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1842 | QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1843 | DelegateArgs.add(RValue::get(VTT), VoidPP); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1844 | |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 1845 | if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1846 | assert(I != E && "cannot skip vtt parameter, already done with args"); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1847 | assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type"); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1848 | ++I; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | // Explicit arguments. |
| 1853 | for (; I != E; ++I) { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1854 | const VarDecl *param = *I; |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1855 | // FIXME: per-argument source location |
| 1856 | EmitDelegateCallArg(DelegateArgs, param, Loc); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1859 | llvm::Value *Callee = |
| 1860 | CGM.getAddrOfCXXStructor(Ctor, getFromCtorType(CtorType)); |
| 1861 | EmitCall(CGM.getTypes() |
| 1862 | .arrangeCXXStructorDeclaration(Ctor, getFromCtorType(CtorType)), |
Manman Ren | 63fd408 | 2013-03-20 16:59:38 +0000 | [diff] [blame] | 1863 | Callee, ReturnValueSlot(), DelegateArgs, Ctor); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1866 | namespace { |
| 1867 | struct CallDelegatingCtorDtor : EHScopeStack::Cleanup { |
| 1868 | const CXXDestructorDecl *Dtor; |
| 1869 | llvm::Value *Addr; |
| 1870 | CXXDtorType Type; |
| 1871 | |
| 1872 | CallDelegatingCtorDtor(const CXXDestructorDecl *D, llvm::Value *Addr, |
| 1873 | CXXDtorType Type) |
| 1874 | : Dtor(D), Addr(Addr), Type(Type) {} |
| 1875 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1876 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1877 | CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1878 | /*Delegating=*/true, Addr); |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1879 | } |
| 1880 | }; |
| 1881 | } |
| 1882 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1883 | void |
| 1884 | CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1885 | const FunctionArgList &Args) { |
| 1886 | assert(Ctor->isDelegatingConstructor()); |
| 1887 | |
| 1888 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 1889 | |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 1890 | QualType Ty = getContext().getTagDeclType(Ctor->getParent()); |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 1891 | CharUnits Alignment = getContext().getTypeAlignInChars(Ty); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1892 | AggValueSlot AggSlot = |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 1893 | AggValueSlot::forAddr(ThisPtr, Alignment, Qualifiers(), |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 1894 | AggValueSlot::IsDestructed, |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 1895 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1896 | AggValueSlot::IsNotAliased); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1897 | |
| 1898 | EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1899 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1900 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1901 | if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) { |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1902 | CXXDtorType Type = |
| 1903 | CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base; |
| 1904 | |
| 1905 | EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup, |
| 1906 | ClassDecl->getDestructor(), |
| 1907 | ThisPtr, Type); |
| 1908 | } |
| 1909 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1910 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1911 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 1912 | CXXDtorType Type, |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 1913 | bool ForVirtualBase, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1914 | bool Delegating, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1915 | llvm::Value *This) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1916 | CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase, |
| 1917 | Delegating, This); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1920 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1921 | struct CallLocalDtor : EHScopeStack::Cleanup { |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1922 | const CXXDestructorDecl *Dtor; |
| 1923 | llvm::Value *Addr; |
| 1924 | |
| 1925 | CallLocalDtor(const CXXDestructorDecl *D, llvm::Value *Addr) |
| 1926 | : Dtor(D), Addr(Addr) {} |
| 1927 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1928 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1929 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1930 | /*ForVirtualBase=*/false, |
| 1931 | /*Delegating=*/false, Addr); |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1932 | } |
| 1933 | }; |
| 1934 | } |
| 1935 | |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1936 | void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D, |
| 1937 | llvm::Value *Addr) { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1938 | EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr); |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1941 | void CodeGenFunction::PushDestructorCleanup(QualType T, llvm::Value *Addr) { |
| 1942 | CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl(); |
| 1943 | if (!ClassDecl) return; |
| 1944 | if (ClassDecl->hasTrivialDestructor()) return; |
| 1945 | |
| 1946 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); |
John McCall | 642a75f | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1947 | assert(D && D->isUsed() && "destructor not marked as used!"); |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1948 | PushDestructorCleanup(D, Addr); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1951 | void |
| 1952 | CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1953 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1954 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1955 | const CXXRecordDecl *VTableClass) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1956 | const CXXRecordDecl *RD = Base.getBase(); |
| 1957 | |
| 1958 | // Don't initialize the vtable pointer if the class is marked with the |
| 1959 | // 'novtable' attribute. |
| 1960 | if ((RD == VTableClass || RD == NearestVBase) && |
| 1961 | VTableClass->hasAttr<MSNoVTableAttr>()) |
| 1962 | return; |
| 1963 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1964 | // Compute the address point. |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1965 | bool NeedsVirtualOffset; |
| 1966 | llvm::Value *VTableAddressPoint = |
| 1967 | CGM.getCXXABI().getVTableAddressPointInStructor( |
| 1968 | *this, VTableClass, Base, NearestVBase, NeedsVirtualOffset); |
| 1969 | if (!VTableAddressPoint) |
| 1970 | return; |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1971 | |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1972 | // Compute where to store the address point. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1973 | llvm::Value *VirtualOffset = nullptr; |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 1974 | CharUnits NonVirtualOffset = CharUnits::Zero(); |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1975 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1976 | if (NeedsVirtualOffset) { |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1977 | // We need to use the virtual base offset offset because the virtual base |
| 1978 | // might have a different offset in the most derived class. |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1979 | VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(*this, |
| 1980 | LoadCXXThis(), |
| 1981 | VTableClass, |
| 1982 | NearestVBase); |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1983 | NonVirtualOffset = OffsetFromNearestVBase; |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1984 | } else { |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1985 | // We can just use the base offset in the complete class. |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1986 | NonVirtualOffset = Base.getBaseOffset(); |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1987 | } |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1988 | |
| 1989 | // Apply the offsets. |
| 1990 | llvm::Value *VTableField = LoadCXXThis(); |
| 1991 | |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 1992 | if (!NonVirtualOffset.isZero() || VirtualOffset) |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1993 | VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField, |
| 1994 | NonVirtualOffset, |
| 1995 | VirtualOffset); |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1996 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1997 | // Finally, store the address point. Use the same LLVM types as the field to |
| 1998 | // support optimization. |
| 1999 | llvm::Type *VTablePtrTy = |
| 2000 | llvm::FunctionType::get(CGM.Int32Ty, /*isVarArg=*/true) |
| 2001 | ->getPointerTo() |
| 2002 | ->getPointerTo(); |
| 2003 | VTableField = Builder.CreateBitCast(VTableField, VTablePtrTy->getPointerTo()); |
| 2004 | VTableAddressPoint = Builder.CreateBitCast(VTableAddressPoint, VTablePtrTy); |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 2005 | llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField); |
| 2006 | CGM.DecorateInstruction(Store, CGM.getTBAAInfoForVTablePtr()); |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2009 | void |
| 2010 | CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 2011 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 2012 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2013 | bool BaseIsNonVirtualPrimaryBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2014 | const CXXRecordDecl *VTableClass, |
| 2015 | VisitedVirtualBasesSetTy& VBases) { |
| 2016 | // If this base is a non-virtual primary base the address point has already |
| 2017 | // been set. |
| 2018 | if (!BaseIsNonVirtualPrimaryBase) { |
| 2019 | // Initialize the vtable pointer for this base. |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 2020 | InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase, |
Timur Iskhodzhanov | 7f918f9 | 2013-08-21 17:33:16 +0000 | [diff] [blame] | 2021 | VTableClass); |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | const CXXRecordDecl *RD = Base.getBase(); |
| 2025 | |
| 2026 | // Traverse bases. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2027 | for (const auto &I : RD->bases()) { |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2028 | CXXRecordDecl *BaseDecl |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2029 | = cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2030 | |
| 2031 | // Ignore classes without a vtable. |
| 2032 | if (!BaseDecl->isDynamicClass()) |
| 2033 | continue; |
| 2034 | |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 2035 | CharUnits BaseOffset; |
| 2036 | CharUnits BaseOffsetFromNearestVBase; |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 2037 | bool BaseDeclIsNonVirtualPrimaryBase; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2038 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2039 | if (I.isVirtual()) { |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2040 | // Check if we've visited this virtual base before. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2041 | if (!VBases.insert(BaseDecl).second) |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2042 | continue; |
| 2043 | |
| 2044 | const ASTRecordLayout &Layout = |
| 2045 | getContext().getASTRecordLayout(VTableClass); |
| 2046 | |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 2047 | BaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
| 2048 | BaseOffsetFromNearestVBase = CharUnits::Zero(); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 2049 | BaseDeclIsNonVirtualPrimaryBase = false; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2050 | } else { |
| 2051 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 2052 | |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 2053 | BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 2054 | BaseOffsetFromNearestVBase = |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 2055 | OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 2056 | BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 2059 | InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2060 | I.isVirtual() ? BaseDecl : NearestVBase, |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 2061 | BaseOffsetFromNearestVBase, |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 2062 | BaseDeclIsNonVirtualPrimaryBase, |
Timur Iskhodzhanov | 7f918f9 | 2013-08-21 17:33:16 +0000 | [diff] [blame] | 2063 | VTableClass, VBases); |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { |
| 2068 | // Ignore classes without a vtable. |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 2069 | if (!RD->isDynamicClass()) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 2070 | return; |
| 2071 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2072 | // Initialize the vtable pointers for this class and all of its bases. |
| 2073 | VisitedVirtualBasesSetTy VBases; |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 2074 | InitializeVTablePointers(BaseSubobject(RD, CharUnits::Zero()), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2075 | /*NearestVBase=*/nullptr, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 2076 | /*OffsetFromNearestVBase=*/CharUnits::Zero(), |
Timur Iskhodzhanov | 7f918f9 | 2013-08-21 17:33:16 +0000 | [diff] [blame] | 2077 | /*BaseIsNonVirtualPrimaryBase=*/false, RD, VBases); |
Timur Iskhodzhanov | 5bd0d44 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 2078 | |
| 2079 | if (RD->getNumVBases()) |
| 2080 | CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, RD); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 2081 | } |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 2082 | |
| 2083 | llvm::Value *CodeGenFunction::GetVTablePtr(llvm::Value *This, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2084 | llvm::Type *Ty) { |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 2085 | llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo()); |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 2086 | llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable"); |
| 2087 | CGM.DecorateInstruction(VTable, CGM.getTBAAInfoForVTablePtr()); |
| 2088 | return VTable; |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 2089 | } |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2090 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2091 | void CodeGenFunction::EmitVTablePtrCheckForCall(const CXXMethodDecl *MD, |
| 2092 | llvm::Value *VTable) { |
| 2093 | if (!SanOpts.has(SanitizerKind::CFIVptr)) |
| 2094 | return; |
| 2095 | |
| 2096 | const CXXRecordDecl *RD = MD->getParent(); |
| 2097 | // FIXME: Add blacklisting scheme. |
| 2098 | if (RD->isInStdNamespace()) |
| 2099 | return; |
| 2100 | |
| 2101 | std::string OutName; |
| 2102 | llvm::raw_string_ostream Out(OutName); |
| 2103 | CGM.getCXXABI().getMangleContext().mangleCXXVTableBitSet(RD, Out); |
| 2104 | |
| 2105 | llvm::Value *BitSetName = llvm::MetadataAsValue::get( |
| 2106 | getLLVMContext(), llvm::MDString::get(getLLVMContext(), Out.str())); |
| 2107 | |
| 2108 | llvm::Value *BitSetTest = Builder.CreateCall2( |
| 2109 | CGM.getIntrinsic(llvm::Intrinsic::bitset_test), |
| 2110 | Builder.CreateBitCast(VTable, CGM.Int8PtrTy), BitSetName); |
| 2111 | |
| 2112 | llvm::BasicBlock *ContBlock = createBasicBlock("vtable.check.cont"); |
| 2113 | llvm::BasicBlock *TrapBlock = createBasicBlock("vtable.check.trap"); |
| 2114 | |
| 2115 | Builder.CreateCondBr(BitSetTest, ContBlock, TrapBlock); |
| 2116 | |
| 2117 | EmitBlock(TrapBlock); |
| 2118 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::trap)); |
| 2119 | Builder.CreateUnreachable(); |
| 2120 | |
| 2121 | EmitBlock(ContBlock); |
| 2122 | } |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2123 | |
| 2124 | // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do |
| 2125 | // quite what we want. |
| 2126 | static const Expr *skipNoOpCastsAndParens(const Expr *E) { |
| 2127 | while (true) { |
| 2128 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 2129 | E = PE->getSubExpr(); |
| 2130 | continue; |
| 2131 | } |
| 2132 | |
| 2133 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 2134 | if (CE->getCastKind() == CK_NoOp) { |
| 2135 | E = CE->getSubExpr(); |
| 2136 | continue; |
| 2137 | } |
| 2138 | } |
| 2139 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 2140 | if (UO->getOpcode() == UO_Extension) { |
| 2141 | E = UO->getSubExpr(); |
| 2142 | continue; |
| 2143 | } |
| 2144 | } |
| 2145 | return E; |
| 2146 | } |
| 2147 | } |
| 2148 | |
Benjamin Kramer | 9581ed0 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 2149 | bool |
| 2150 | CodeGenFunction::CanDevirtualizeMemberFunctionCall(const Expr *Base, |
| 2151 | const CXXMethodDecl *MD) { |
| 2152 | // When building with -fapple-kext, all calls must go through the vtable since |
| 2153 | // the kernel linker can do runtime patching of vtables. |
| 2154 | if (getLangOpts().AppleKext) |
| 2155 | return false; |
| 2156 | |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2157 | // If the most derived class is marked final, we know that no subclass can |
| 2158 | // override this member function and so we can devirtualize it. For example: |
| 2159 | // |
| 2160 | // struct A { virtual void f(); } |
| 2161 | // struct B final : A { }; |
| 2162 | // |
| 2163 | // void f(B *b) { |
| 2164 | // b->f(); |
| 2165 | // } |
| 2166 | // |
Benjamin Kramer | 9581ed0 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 2167 | const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType(); |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2168 | if (MostDerivedClassDecl->hasAttr<FinalAttr>()) |
| 2169 | return true; |
| 2170 | |
| 2171 | // If the member function is marked 'final', we know that it can't be |
| 2172 | // overridden and can therefore devirtualize it. |
| 2173 | if (MD->hasAttr<FinalAttr>()) |
| 2174 | return true; |
| 2175 | |
| 2176 | // Similarly, if the class itself is marked 'final' it can't be overridden |
| 2177 | // and we can therefore devirtualize the member function call. |
| 2178 | if (MD->getParent()->hasAttr<FinalAttr>()) |
| 2179 | return true; |
| 2180 | |
| 2181 | Base = skipNoOpCastsAndParens(Base); |
| 2182 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 2183 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 2184 | // This is a record decl. We know the type and can devirtualize it. |
| 2185 | return VD->getType()->isRecordType(); |
| 2186 | } |
| 2187 | |
| 2188 | return false; |
| 2189 | } |
Benjamin Kramer | 9581ed0 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 2190 | |
| 2191 | // We can devirtualize calls on an object accessed by a class member access |
| 2192 | // expression, since by C++11 [basic.life]p6 we know that it can't refer to |
| 2193 | // a derived class object constructed in the same location. |
| 2194 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base)) |
| 2195 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ME->getMemberDecl())) |
| 2196 | return VD->getType()->isRecordType(); |
| 2197 | |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2198 | // We can always devirtualize calls on temporary object expressions. |
| 2199 | if (isa<CXXConstructExpr>(Base)) |
| 2200 | return true; |
| 2201 | |
| 2202 | // And calls on bound temporaries. |
| 2203 | if (isa<CXXBindTemporaryExpr>(Base)) |
| 2204 | return true; |
| 2205 | |
| 2206 | // Check if this is a call expr that returns a record type. |
| 2207 | if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2208 | return CE->getCallReturnType(getContext())->isRecordType(); |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2209 | |
| 2210 | // We can't devirtualize the call. |
| 2211 | return false; |
| 2212 | } |
| 2213 | |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2214 | void CodeGenFunction::EmitForwardingCallToLambda( |
| 2215 | const CXXMethodDecl *callOperator, |
| 2216 | CallArgList &callArgs) { |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2217 | // Get the address of the call operator. |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2218 | const CGFunctionInfo &calleeFnInfo = |
| 2219 | CGM.getTypes().arrangeCXXMethodDeclaration(callOperator); |
| 2220 | llvm::Value *callee = |
| 2221 | CGM.GetAddrOfFunction(GlobalDecl(callOperator), |
| 2222 | CGM.getTypes().GetFunctionType(calleeFnInfo)); |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2223 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2224 | // Prepare the return slot. |
| 2225 | const FunctionProtoType *FPT = |
| 2226 | callOperator->getType()->castAs<FunctionProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2227 | QualType resultType = FPT->getReturnType(); |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2228 | ReturnValueSlot returnSlot; |
| 2229 | if (!resultType->isVoidType() && |
| 2230 | calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect && |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2231 | !hasScalarEvaluationKind(calleeFnInfo.getReturnType())) |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2232 | returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified()); |
| 2233 | |
| 2234 | // We don't need to separately arrange the call arguments because |
| 2235 | // the call can't be variadic anyway --- it's impossible to forward |
| 2236 | // variadic arguments. |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2237 | |
| 2238 | // Now emit our call. |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2239 | RValue RV = EmitCall(calleeFnInfo, callee, returnSlot, |
| 2240 | callArgs, callOperator); |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2241 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2242 | // If necessary, copy the returned value into the slot. |
| 2243 | if (!resultType->isVoidType() && returnSlot.isNull()) |
| 2244 | EmitReturnOfRValue(RV, resultType); |
Eli Friedman | 50f089a | 2012-12-13 23:37:17 +0000 | [diff] [blame] | 2245 | else |
| 2246 | EmitBranchThroughCleanup(ReturnBlock); |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2249 | void CodeGenFunction::EmitLambdaBlockInvokeBody() { |
| 2250 | const BlockDecl *BD = BlockInfo->getBlockDecl(); |
| 2251 | const VarDecl *variable = BD->capture_begin()->getVariable(); |
| 2252 | const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl(); |
| 2253 | |
| 2254 | // Start building arguments for forwarding call |
| 2255 | CallArgList CallArgs; |
| 2256 | |
| 2257 | QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); |
| 2258 | llvm::Value *ThisPtr = GetAddrOfBlockDecl(variable, false); |
| 2259 | CallArgs.add(RValue::get(ThisPtr), ThisType); |
| 2260 | |
| 2261 | // Add the rest of the parameters. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2262 | for (auto param : BD->params()) |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2263 | EmitDelegateCallArg(CallArgs, param, param->getLocStart()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2264 | |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2265 | assert(!Lambda->isGenericLambda() && |
| 2266 | "generic lambda interconversion to block not implemented"); |
| 2267 | EmitForwardingCallToLambda(Lambda->getLambdaCallOperator(), CallArgs); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | void CodeGenFunction::EmitLambdaToBlockPointerBody(FunctionArgList &Args) { |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 2271 | if (cast<CXXMethodDecl>(CurCodeDecl)->isVariadic()) { |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2272 | // FIXME: Making this work correctly is nasty because it requires either |
| 2273 | // cloning the body of the call operator or making the call operator forward. |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 2274 | CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function"); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2275 | return; |
| 2276 | } |
| 2277 | |
Richard Smith | 3cebc73 | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 2278 | EmitFunctionBody(Args, cast<FunctionDecl>(CurGD.getDecl())->getBody()); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD) { |
| 2282 | const CXXRecordDecl *Lambda = MD->getParent(); |
| 2283 | |
| 2284 | // Start building arguments for forwarding call |
| 2285 | CallArgList CallArgs; |
| 2286 | |
| 2287 | QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); |
| 2288 | llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType)); |
| 2289 | CallArgs.add(RValue::get(ThisPtr), ThisType); |
| 2290 | |
| 2291 | // Add the rest of the parameters. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2292 | for (auto Param : MD->params()) |
| 2293 | EmitDelegateCallArg(CallArgs, Param, Param->getLocStart()); |
| 2294 | |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2295 | const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 2296 | // For a generic lambda, find the corresponding call operator specialization |
| 2297 | // to which the call to the static-invoker shall be forwarded. |
| 2298 | if (Lambda->isGenericLambda()) { |
| 2299 | assert(MD->isFunctionTemplateSpecialization()); |
| 2300 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
| 2301 | FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2302 | void *InsertPos = nullptr; |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2303 | FunctionDecl *CorrespondingCallOpSpecialization = |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2304 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2305 | assert(CorrespondingCallOpSpecialization); |
| 2306 | CallOp = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
| 2307 | } |
| 2308 | EmitForwardingCallToLambda(CallOp, CallArgs); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 2311 | void CodeGenFunction::EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD) { |
| 2312 | if (MD->isVariadic()) { |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2313 | // FIXME: Making this work correctly is nasty because it requires either |
| 2314 | // cloning the body of the call operator or making the call operator forward. |
| 2315 | CGM.ErrorUnsupported(MD, "lambda conversion to variadic function"); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2316 | return; |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 2319 | EmitLambdaDelegatingInvokeBody(MD); |
Eli Friedman | bd89f8c | 2012-02-16 01:37:33 +0000 | [diff] [blame] | 2320 | } |