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