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