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