blob: 72869d8ca9b3c4ff27125ff2d674c281634d8f89 [file] [log] [blame]
Anders Carlsson59486a22009-11-24 05:51:11 +00001//===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===//
Anders Carlsson9a57c5a2009-09-12 04:27:24 +00002//
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 Friedman2495ab02012-02-25 02:48:22 +000014#include "CGBlocks.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000015#include "CGCXXABI.h"
Devang Pateld76c1db2010-08-11 21:04:37 +000016#include "CGDebugInfo.h"
Lang Hamesbf122742013-02-17 07:22:09 +000017#include "CGRecordLayout.h"
Anders Carlsson9a57c5a2009-09-12 04:27:24 +000018#include "CodeGenFunction.h"
Anders Carlssonc6d171e2009-10-06 22:43:30 +000019#include "clang/AST/CXXInheritance.h"
Faisal Vali571df122013-09-29 08:45:24 +000020#include "clang/AST/DeclTemplate.h"
John McCall769250e2010-09-17 02:31:44 +000021#include "clang/AST/EvaluatedExprVisitor.h"
Anders Carlsson9a57c5a2009-09-12 04:27:24 +000022#include "clang/AST/RecordLayout.h"
John McCallb81884d2010-02-19 09:25:03 +000023#include "clang/AST/StmtCXX.h"
Lang Hamesbf122742013-02-17 07:22:09 +000024#include "clang/Basic/TargetBuiltins.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000025#include "clang/CodeGen/CGFunctionInfo.h"
Devang Patelb6ed3692011-02-22 20:55:26 +000026#include "clang/Frontend/CodeGenOptions.h"
Anders Carlssonc6d171e2009-10-06 22:43:30 +000027
Anders Carlsson9a57c5a2009-09-12 04:27:24 +000028using namespace clang;
29using namespace CodeGen;
30
Ken Dycka1a4ae32011-03-22 00:53:26 +000031static CharUnits
Anders Carlssond829a022010-04-24 21:06:20 +000032ComputeNonVirtualBaseClassOffset(ASTContext &Context,
33 const CXXRecordDecl *DerivedClass,
John McCallcf142162010-08-07 06:22:56 +000034 CastExpr::path_const_iterator Start,
35 CastExpr::path_const_iterator End) {
Ken Dycka1a4ae32011-03-22 00:53:26 +000036 CharUnits Offset = CharUnits::Zero();
Anders Carlssond829a022010-04-24 21:06:20 +000037
38 const CXXRecordDecl *RD = DerivedClass;
39
John McCallcf142162010-08-07 06:22:56 +000040 for (CastExpr::path_const_iterator I = Start; I != End; ++I) {
Anders Carlssond829a022010-04-24 21:06:20 +000041 const CXXBaseSpecifier *Base = *I;
42 assert(!Base->isVirtual() && "Should not see virtual bases here!");
43
44 // Get the layout.
45 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
46
47 const CXXRecordDecl *BaseDecl =
48 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
49
50 // Add the offset.
Ken Dycka1a4ae32011-03-22 00:53:26 +000051 Offset += Layout.getBaseClassOffset(BaseDecl);
Anders Carlssond829a022010-04-24 21:06:20 +000052
53 RD = BaseDecl;
54 }
55
Ken Dycka1a4ae32011-03-22 00:53:26 +000056 return Offset;
Anders Carlssond829a022010-04-24 21:06:20 +000057}
Anders Carlsson9a57c5a2009-09-12 04:27:24 +000058
Anders Carlsson9150a2a2009-09-29 03:13:20 +000059llvm::Constant *
Anders Carlsson8a64c1c2010-04-24 21:23:59 +000060CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallcf142162010-08-07 06:22:56 +000061 CastExpr::path_const_iterator PathBegin,
62 CastExpr::path_const_iterator PathEnd) {
63 assert(PathBegin != PathEnd && "Base path should not be empty!");
Anders Carlsson8a64c1c2010-04-24 21:23:59 +000064
Ken Dycka1a4ae32011-03-22 00:53:26 +000065 CharUnits Offset =
John McCallcf142162010-08-07 06:22:56 +000066 ComputeNonVirtualBaseClassOffset(getContext(), ClassDecl,
67 PathBegin, PathEnd);
Ken Dycka1a4ae32011-03-22 00:53:26 +000068 if (Offset.isZero())
Craig Topper8a13c412014-05-21 05:09:00 +000069 return nullptr;
70
Chris Lattner2192fe52011-07-18 04:24:23 +000071 llvm::Type *PtrDiffTy =
Anders Carlsson8a64c1c2010-04-24 21:23:59 +000072 Types.ConvertType(getContext().getPointerDiffType());
73
Ken Dycka1a4ae32011-03-22 00:53:26 +000074 return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
Anders Carlsson9150a2a2009-09-29 03:13:20 +000075}
76
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +000077/// Gets the address of a direct base class within a complete object.
John McCall6ce74722010-02-16 04:15:37 +000078/// This should only be used for (1) non-virtual bases or (2) virtual bases
79/// when the type is known to be complete (e.g. in complete destructors).
80///
81/// The object pointed to by 'This' is assumed to be non-null.
82llvm::Value *
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +000083CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(llvm::Value *This,
84 const CXXRecordDecl *Derived,
85 const CXXRecordDecl *Base,
86 bool BaseIsVirtual) {
John McCall6ce74722010-02-16 04:15:37 +000087 // 'this' must be a pointer (in some address space) to Derived.
88 assert(This->getType()->isPointerTy() &&
89 cast<llvm::PointerType>(This->getType())->getElementType()
90 == ConvertType(Derived));
91
92 // Compute the offset of the virtual base.
Ken Dyck6aa767c2011-03-22 01:21:15 +000093 CharUnits Offset;
John McCall6ce74722010-02-16 04:15:37 +000094 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +000095 if (BaseIsVirtual)
Ken Dyck6aa767c2011-03-22 01:21:15 +000096 Offset = Layout.getVBaseClassOffset(Base);
John McCall6ce74722010-02-16 04:15:37 +000097 else
Ken Dyck6aa767c2011-03-22 01:21:15 +000098 Offset = Layout.getBaseClassOffset(Base);
John McCall6ce74722010-02-16 04:15:37 +000099
100 // Shift and cast down to the base type.
101 // TODO: for complete types, this should be possible with a GEP.
102 llvm::Value *V = This;
Ken Dyck6aa767c2011-03-22 01:21:15 +0000103 if (Offset.isPositive()) {
John McCall6ce74722010-02-16 04:15:37 +0000104 V = Builder.CreateBitCast(V, Int8PtrTy);
Ken Dyck6aa767c2011-03-22 01:21:15 +0000105 V = Builder.CreateConstInBoundsGEP1_64(V, Offset.getQuantity());
John McCall6ce74722010-02-16 04:15:37 +0000106 }
107 V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo());
108
109 return V;
Anders Carlssone87fae92010-03-28 19:40:00 +0000110}
John McCall6ce74722010-02-16 04:15:37 +0000111
Anders Carlsson53cebd12010-04-20 16:03:35 +0000112static llvm::Value *
John McCall13a39c62012-08-01 05:04:58 +0000113ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ptr,
114 CharUnits nonVirtualOffset,
115 llvm::Value *virtualOffset) {
116 // Assert that we have something to do.
Craig Topper8a13c412014-05-21 05:09:00 +0000117 assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr);
John McCall13a39c62012-08-01 05:04:58 +0000118
119 // Compute the offset from the static and dynamic components.
120 llvm::Value *baseOffset;
121 if (!nonVirtualOffset.isZero()) {
122 baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy,
123 nonVirtualOffset.getQuantity());
124 if (virtualOffset) {
125 baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset);
126 }
127 } else {
128 baseOffset = virtualOffset;
129 }
Anders Carlsson53cebd12010-04-20 16:03:35 +0000130
131 // Apply the base offset.
John McCall13a39c62012-08-01 05:04:58 +0000132 ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
133 ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr");
134 return ptr;
Anders Carlsson53cebd12010-04-20 16:03:35 +0000135}
136
Anders Carlsson9a57c5a2009-09-12 04:27:24 +0000137llvm::Value *
Anders Carlssond829a022010-04-24 21:06:20 +0000138CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value,
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000139 const CXXRecordDecl *Derived,
John McCallcf142162010-08-07 06:22:56 +0000140 CastExpr::path_const_iterator PathBegin,
141 CastExpr::path_const_iterator PathEnd,
Anders Carlssond829a022010-04-24 21:06:20 +0000142 bool NullCheckValue) {
John McCallcf142162010-08-07 06:22:56 +0000143 assert(PathBegin != PathEnd && "Base path should not be empty!");
Anders Carlssond829a022010-04-24 21:06:20 +0000144
John McCallcf142162010-08-07 06:22:56 +0000145 CastExpr::path_const_iterator Start = PathBegin;
Craig Topper8a13c412014-05-21 05:09:00 +0000146 const CXXRecordDecl *VBase = nullptr;
147
John McCall13a39c62012-08-01 05:04:58 +0000148 // Sema has done some convenient canonicalization here: if the
149 // access path involved any virtual steps, the conversion path will
150 // *start* with a step down to the correct virtual base subobject,
151 // and hence will not require any further steps.
Anders Carlssond829a022010-04-24 21:06:20 +0000152 if ((*Start)->isVirtual()) {
153 VBase =
154 cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl());
155 ++Start;
156 }
John McCall13a39c62012-08-01 05:04:58 +0000157
158 // Compute the static offset of the ultimate destination within its
159 // allocating subobject (the virtual base, if there is one, or else
160 // the "complete" object that we see).
Ken Dycka1a4ae32011-03-22 00:53:26 +0000161 CharUnits NonVirtualOffset =
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000162 ComputeNonVirtualBaseClassOffset(getContext(), VBase ? VBase : Derived,
John McCallcf142162010-08-07 06:22:56 +0000163 Start, PathEnd);
Anders Carlssond829a022010-04-24 21:06:20 +0000164
John McCall13a39c62012-08-01 05:04:58 +0000165 // If there's a virtual step, we can sometimes "devirtualize" it.
166 // For now, that's limited to when the derived type is final.
167 // TODO: "devirtualize" this for accesses to known-complete objects.
168 if (VBase && Derived->hasAttr<FinalAttr>()) {
169 const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived);
170 CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase);
171 NonVirtualOffset += vBaseOffset;
Craig Topper8a13c412014-05-21 05:09:00 +0000172 VBase = nullptr; // we no longer have a virtual step
John McCall13a39c62012-08-01 05:04:58 +0000173 }
174
Anders Carlssond829a022010-04-24 21:06:20 +0000175 // Get the base pointer type.
Chris Lattner2192fe52011-07-18 04:24:23 +0000176 llvm::Type *BasePtrTy =
John McCallcf142162010-08-07 06:22:56 +0000177 ConvertType((PathEnd[-1])->getType())->getPointerTo();
John McCall13a39c62012-08-01 05:04:58 +0000178
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 Dycka1a4ae32011-03-22 00:53:26 +0000181 if (NonVirtualOffset.isZero() && !VBase) {
Anders Carlssond829a022010-04-24 21:06:20 +0000182 return Builder.CreateBitCast(Value, BasePtrTy);
Craig Topper8a13c412014-05-21 05:09:00 +0000183 }
John McCall13a39c62012-08-01 05:04:58 +0000184
Craig Topper8a13c412014-05-21 05:09:00 +0000185 llvm::BasicBlock *origBB = nullptr;
186 llvm::BasicBlock *endBB = nullptr;
187
John McCall13a39c62012-08-01 05:04:58 +0000188 // Skip over the offset (and the vtable load) if we're supposed to
189 // null-check the pointer.
Anders Carlssond829a022010-04-24 21:06:20 +0000190 if (NullCheckValue) {
John McCall13a39c62012-08-01 05:04:58 +0000191 origBB = Builder.GetInsertBlock();
192 llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull");
193 endBB = createBasicBlock("cast.end");
Anders Carlssond829a022010-04-24 21:06:20 +0000194
John McCall13a39c62012-08-01 05:04:58 +0000195 llvm::Value *isNull = Builder.CreateIsNull(Value);
196 Builder.CreateCondBr(isNull, endBB, notNullBB);
197 EmitBlock(notNullBB);
Anders Carlssond829a022010-04-24 21:06:20 +0000198 }
199
John McCall13a39c62012-08-01 05:04:58 +0000200 // Compute the virtual offset.
Craig Topper8a13c412014-05-21 05:09:00 +0000201 llvm::Value *VirtualOffset = nullptr;
Anders Carlssona376b532011-01-29 03:18:56 +0000202 if (VBase) {
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000203 VirtualOffset =
204 CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase);
Anders Carlssona376b532011-01-29 03:18:56 +0000205 }
Anders Carlssond829a022010-04-24 21:06:20 +0000206
John McCall13a39c62012-08-01 05:04:58 +0000207 // Apply both offsets.
Ken Dycka1a4ae32011-03-22 00:53:26 +0000208 Value = ApplyNonVirtualAndVirtualOffset(*this, Value,
Ken Dyckcfc332c2011-03-23 00:45:26 +0000209 NonVirtualOffset,
Anders Carlssond829a022010-04-24 21:06:20 +0000210 VirtualOffset);
211
John McCall13a39c62012-08-01 05:04:58 +0000212 // Cast to the destination type.
Anders Carlssond829a022010-04-24 21:06:20 +0000213 Value = Builder.CreateBitCast(Value, BasePtrTy);
John McCall13a39c62012-08-01 05:04:58 +0000214
215 // Build a phi if we needed a null check.
Anders Carlssond829a022010-04-24 21:06:20 +0000216 if (NullCheckValue) {
John McCall13a39c62012-08-01 05:04:58 +0000217 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
218 Builder.CreateBr(endBB);
219 EmitBlock(endBB);
Anders Carlssond829a022010-04-24 21:06:20 +0000220
John McCall13a39c62012-08-01 05:04:58 +0000221 llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result");
222 PHI->addIncoming(Value, notNullBB);
223 PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB);
Anders Carlssond829a022010-04-24 21:06:20 +0000224 Value = PHI;
225 }
226
227 return Value;
228}
229
230llvm::Value *
Anders Carlsson8c793172009-11-23 17:57:54 +0000231CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value,
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000232 const CXXRecordDecl *Derived,
John McCallcf142162010-08-07 06:22:56 +0000233 CastExpr::path_const_iterator PathBegin,
234 CastExpr::path_const_iterator PathEnd,
Anders Carlsson8c793172009-11-23 17:57:54 +0000235 bool NullCheckValue) {
John McCallcf142162010-08-07 06:22:56 +0000236 assert(PathBegin != PathEnd && "Base path should not be empty!");
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000237
Anders Carlsson8c793172009-11-23 17:57:54 +0000238 QualType DerivedTy =
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000239 getContext().getCanonicalType(getContext().getTagDeclType(Derived));
Chris Lattner2192fe52011-07-18 04:24:23 +0000240 llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo();
Richard Smith2c5868c2013-02-13 21:18:23 +0000241
Anders Carlsson600f7372010-01-31 01:43:37 +0000242 llvm::Value *NonVirtualOffset =
John McCallcf142162010-08-07 06:22:56 +0000243 CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd);
Anders Carlsson600f7372010-01-31 01:43:37 +0000244
245 if (!NonVirtualOffset) {
246 // No offset, we can just cast back.
247 return Builder.CreateBitCast(Value, DerivedPtrTy);
248 }
Craig Topper8a13c412014-05-21 05:09:00 +0000249
250 llvm::BasicBlock *CastNull = nullptr;
251 llvm::BasicBlock *CastNotNull = nullptr;
252 llvm::BasicBlock *CastEnd = nullptr;
253
Anders Carlsson8c793172009-11-23 17:57:54 +0000254 if (NullCheckValue) {
255 CastNull = createBasicBlock("cast.null");
256 CastNotNull = createBasicBlock("cast.notnull");
257 CastEnd = createBasicBlock("cast.end");
258
Anders Carlsson98981b12011-04-11 00:30:07 +0000259 llvm::Value *IsNull = Builder.CreateIsNull(Value);
Anders Carlsson8c793172009-11-23 17:57:54 +0000260 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
261 EmitBlock(CastNotNull);
262 }
263
Anders Carlsson600f7372010-01-31 01:43:37 +0000264 // Apply the offset.
Eli Friedman87549262012-02-28 22:07:56 +0000265 Value = Builder.CreateBitCast(Value, Int8PtrTy);
266 Value = Builder.CreateGEP(Value, Builder.CreateNeg(NonVirtualOffset),
267 "sub.ptr");
Anders Carlsson600f7372010-01-31 01:43:37 +0000268
269 // Just cast.
270 Value = Builder.CreateBitCast(Value, DerivedPtrTy);
Anders Carlsson8c793172009-11-23 17:57:54 +0000271
272 if (NullCheckValue) {
273 Builder.CreateBr(CastEnd);
274 EmitBlock(CastNull);
275 Builder.CreateBr(CastEnd);
276 EmitBlock(CastEnd);
277
Jay Foad20c0f022011-03-30 11:28:58 +0000278 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
Anders Carlsson8c793172009-11-23 17:57:54 +0000279 PHI->addIncoming(Value, CastNotNull);
280 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()),
281 CastNull);
282 Value = PHI;
283 }
284
285 return Value;
Anders Carlsson9a57c5a2009-09-12 04:27:24 +0000286}
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000287
288llvm::Value *CodeGenFunction::GetVTTParameter(GlobalDecl GD,
289 bool ForVirtualBase,
290 bool Delegating) {
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000291 if (!CGM.getCXXABI().NeedsVTTParameter(GD)) {
Anders Carlssone36a6b32010-01-02 01:01:18 +0000292 // This constructor/destructor does not need a VTT parameter.
Craig Topper8a13c412014-05-21 05:09:00 +0000293 return nullptr;
Anders Carlssone36a6b32010-01-02 01:01:18 +0000294 }
295
John McCalldec348f72013-05-03 07:33:41 +0000296 const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent();
Anders Carlssone36a6b32010-01-02 01:01:18 +0000297 const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent();
John McCall5c60a6f2010-02-18 19:59:28 +0000298
Anders Carlssone36a6b32010-01-02 01:01:18 +0000299 llvm::Value *VTT;
300
John McCall5c60a6f2010-02-18 19:59:28 +0000301 uint64_t SubVTTIndex;
302
Douglas Gregor61535002013-01-31 05:50:40 +0000303 if (Delegating) {
304 // If this is a delegating constructor call, just load the VTT.
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000305 return LoadCXXVTT();
Douglas Gregor61535002013-01-31 05:50:40 +0000306 } else if (RD == Base) {
307 // If the record matches the base, this is the complete ctor/dtor
308 // variant calling the base variant in a class with virtual bases.
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000309 assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) &&
John McCall5c60a6f2010-02-18 19:59:28 +0000310 "doing no-op VTT offset in base dtor/ctor?");
Anders Carlsson4d205ba2010-05-02 23:33:10 +0000311 assert(!ForVirtualBase && "Can't have same class as virtual base!");
John McCall5c60a6f2010-02-18 19:59:28 +0000312 SubVTTIndex = 0;
313 } else {
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000314 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Ken Dyck16ffcac2011-03-24 01:21:01 +0000315 CharUnits BaseOffset = ForVirtualBase ?
316 Layout.getVBaseClassOffset(Base) :
317 Layout.getBaseClassOffset(Base);
Anders Carlsson859b3062010-05-02 23:53:25 +0000318
319 SubVTTIndex =
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000320 CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset));
John McCall5c60a6f2010-02-18 19:59:28 +0000321 assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
322 }
Anders Carlssone36a6b32010-01-02 01:01:18 +0000323
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000324 if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) {
Anders Carlssone36a6b32010-01-02 01:01:18 +0000325 // A VTT parameter was passed to the constructor, use it.
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000326 VTT = LoadCXXVTT();
327 VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex);
Anders Carlssone36a6b32010-01-02 01:01:18 +0000328 } else {
329 // We're the complete constructor, so get the VTT by name.
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000330 VTT = CGM.getVTables().GetAddrOfVTT(RD);
331 VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex);
Anders Carlssone36a6b32010-01-02 01:01:18 +0000332 }
333
334 return VTT;
335}
336
John McCall1d987562010-07-21 01:23:41 +0000337namespace {
John McCallf99a6312010-07-21 05:30:47 +0000338 /// Call the destructor for a direct base class.
John McCallcda666c2010-07-21 07:22:38 +0000339 struct CallBaseDtor : EHScopeStack::Cleanup {
John McCallf99a6312010-07-21 05:30:47 +0000340 const CXXRecordDecl *BaseClass;
341 bool BaseIsVirtual;
342 CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual)
343 : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
John McCall1d987562010-07-21 01:23:41 +0000344
Craig Topper4f12f102014-03-12 06:41:41 +0000345 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCallf99a6312010-07-21 05:30:47 +0000346 const CXXRecordDecl *DerivedClass =
347 cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
348
349 const CXXDestructorDecl *D = BaseClass->getDestructor();
350 llvm::Value *Addr =
351 CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThis(),
352 DerivedClass, BaseClass,
353 BaseIsVirtual);
Douglas Gregor61535002013-01-31 05:50:40 +0000354 CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual,
355 /*Delegating=*/false, Addr);
John McCall1d987562010-07-21 01:23:41 +0000356 }
357 };
John McCall769250e2010-09-17 02:31:44 +0000358
359 /// A visitor which checks whether an initializer uses 'this' in a
360 /// way which requires the vtable to be properly set.
361 struct DynamicThisUseChecker : EvaluatedExprVisitor<DynamicThisUseChecker> {
362 typedef EvaluatedExprVisitor<DynamicThisUseChecker> super;
363
364 bool UsesThis;
365
366 DynamicThisUseChecker(ASTContext &C) : super(C), UsesThis(false) {}
367
368 // Black-list all explicit and implicit references to 'this'.
369 //
370 // Do we need to worry about external references to 'this' derived
371 // from arbitrary code? If so, then anything which runs arbitrary
372 // external code might potentially access the vtable.
373 void VisitCXXThisExpr(CXXThisExpr *E) { UsesThis = true; }
374 };
375}
376
377static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) {
378 DynamicThisUseChecker Checker(C);
379 Checker.Visit(const_cast<Expr*>(Init));
380 return Checker.UsesThis;
John McCall1d987562010-07-21 01:23:41 +0000381}
382
Anders Carlssonfb404882009-12-24 22:46:43 +0000383static void EmitBaseInitializer(CodeGenFunction &CGF,
384 const CXXRecordDecl *ClassDecl,
Alexis Hunt1d792652011-01-08 20:30:50 +0000385 CXXCtorInitializer *BaseInit,
Anders Carlssonfb404882009-12-24 22:46:43 +0000386 CXXCtorType CtorType) {
387 assert(BaseInit->isBaseInitializer() &&
388 "Must have base initializer!");
389
390 llvm::Value *ThisPtr = CGF.LoadCXXThis();
391
392 const Type *BaseType = BaseInit->getBaseClass();
393 CXXRecordDecl *BaseClassDecl =
394 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
395
Anders Carlsson1c0f8bb2010-04-12 00:51:03 +0000396 bool isBaseVirtual = BaseInit->isBaseVirtual();
Anders Carlssonfb404882009-12-24 22:46:43 +0000397
398 // The base constructor doesn't construct virtual bases.
399 if (CtorType == Ctor_Base && isBaseVirtual)
400 return;
401
John McCall769250e2010-09-17 02:31:44 +0000402 // If the initializer for the base (other than the constructor
403 // itself) accesses 'this' in any way, we need to initialize the
404 // vtables.
405 if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit()))
406 CGF.InitializeVTablePointers(ClassDecl);
407
John McCall6ce74722010-02-16 04:15:37 +0000408 // We can pretend to be a complete class because it only matters for
409 // virtual bases, and we only do virtual bases for complete ctors.
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000410 llvm::Value *V =
411 CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl,
John McCallf99a6312010-07-21 05:30:47 +0000412 BaseClassDecl,
413 isBaseVirtual);
Eli Friedman38cd36d2011-12-03 02:13:40 +0000414 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(BaseType);
John McCall8d6fc952011-08-25 20:40:09 +0000415 AggValueSlot AggSlot =
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000416 AggValueSlot::forAddr(V, Alignment, Qualifiers(),
John McCall8d6fc952011-08-25 20:40:09 +0000417 AggValueSlot::IsDestructed,
John McCalla5efa732011-08-25 23:04:34 +0000418 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier615ed1a2012-03-29 17:37:10 +0000419 AggValueSlot::IsNotAliased);
John McCall7a626f62010-09-15 10:14:12 +0000420
421 CGF.EmitAggExpr(BaseInit->getInit(), AggSlot);
Anders Carlsson5ade5d32010-02-06 20:00:21 +0000422
David Blaikiebbafb8a2012-03-11 07:00:24 +0000423 if (CGF.CGM.getLangOpts().Exceptions &&
Anders Carlsson08ce5ed2011-02-20 00:20:27 +0000424 !BaseClassDecl->hasTrivialDestructor())
John McCallcda666c2010-07-21 07:22:38 +0000425 CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl,
426 isBaseVirtual);
Anders Carlssonfb404882009-12-24 22:46:43 +0000427}
428
Douglas Gregor94f9a482010-05-05 05:51:00 +0000429static void EmitAggMemberInitializer(CodeGenFunction &CGF,
430 LValue LHS,
Eli Friedman6ae63022012-02-14 02:15:49 +0000431 Expr *Init,
Douglas Gregor94f9a482010-05-05 05:51:00 +0000432 llvm::Value *ArrayIndexVar,
Douglas Gregor94f9a482010-05-05 05:51:00 +0000433 QualType T,
Eli Friedman6ae63022012-02-14 02:15:49 +0000434 ArrayRef<VarDecl *> ArrayIndexes,
Douglas Gregor94f9a482010-05-05 05:51:00 +0000435 unsigned Index) {
Eli Friedman6ae63022012-02-14 02:15:49 +0000436 if (Index == ArrayIndexes.size()) {
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000437 LValue LV = LHS;
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000438
Richard Smithcc1b96d2013-06-12 22:31:48 +0000439 if (ArrayIndexVar) {
440 // If we have an array index variable, load it and use it as an offset.
441 // Then, increment the value.
442 llvm::Value *Dest = LHS.getAddress();
443 llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar);
444 Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress");
445 llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1);
446 Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc");
447 CGF.Builder.CreateStore(Next, ArrayIndexVar);
Sebastian Redl4e04dd12012-02-19 15:41:54 +0000448
Richard Smithcc1b96d2013-06-12 22:31:48 +0000449 // Update the LValue.
450 LV.setAddress(Dest);
451 CharUnits Align = CGF.getContext().getTypeAlignInChars(T);
452 LV.setAlignment(std::min(Align, LV.getAlignment()));
Douglas Gregor94f9a482010-05-05 05:51:00 +0000453 }
John McCall7a626f62010-09-15 10:14:12 +0000454
Richard Smithcc1b96d2013-06-12 22:31:48 +0000455 switch (CGF.getEvaluationKind(T)) {
456 case TEK_Scalar:
Craig Topper8a13c412014-05-21 05:09:00 +0000457 CGF.EmitScalarInit(Init, /*decl*/ nullptr, LV, false);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000458 break;
459 case TEK_Complex:
460 CGF.EmitComplexExprIntoLValue(Init, LV, /*isInit*/ true);
461 break;
462 case TEK_Aggregate: {
463 AggValueSlot Slot =
464 AggValueSlot::forLValue(LV,
465 AggValueSlot::IsDestructed,
466 AggValueSlot::DoesNotNeedGCBarriers,
467 AggValueSlot::IsNotAliased);
468
469 CGF.EmitAggExpr(Init, Slot);
470 break;
471 }
472 }
Sebastian Redl4e04dd12012-02-19 15:41:54 +0000473
Douglas Gregor94f9a482010-05-05 05:51:00 +0000474 return;
475 }
Richard Smithcc1b96d2013-06-12 22:31:48 +0000476
Douglas Gregor94f9a482010-05-05 05:51:00 +0000477 const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T);
478 assert(Array && "Array initialization without the array type?");
479 llvm::Value *IndexVar
Eli Friedman6ae63022012-02-14 02:15:49 +0000480 = CGF.GetAddrOfLocalVar(ArrayIndexes[Index]);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000481 assert(IndexVar && "Array index variable not loaded");
482
483 // Initialize this index variable to zero.
484 llvm::Value* Zero
485 = llvm::Constant::getNullValue(
486 CGF.ConvertType(CGF.getContext().getSizeType()));
487 CGF.Builder.CreateStore(Zero, IndexVar);
488
489 // Start the loop with a block that tests the condition.
490 llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond");
491 llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end");
492
493 CGF.EmitBlock(CondBlock);
494
495 llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body");
496 // Generate: if (loop-index < number-of-elements) fall to the loop body,
497 // otherwise, go to the block after the for-loop.
498 uint64_t NumElements = Array->getSize().getZExtValue();
Douglas Gregor94f9a482010-05-05 05:51:00 +0000499 llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar);
Chris Lattner44456d22010-05-06 06:35:23 +0000500 llvm::Value *NumElementsPtr =
501 llvm::ConstantInt::get(Counter->getType(), NumElements);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000502 llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr,
503 "isless");
504
505 // If the condition is true, execute the body.
506 CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor);
507
508 CGF.EmitBlock(ForBody);
509 llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc");
Richard Smithcc1b96d2013-06-12 22:31:48 +0000510
511 // Inside the loop body recurse to emit the inner loop or, eventually, the
512 // constructor call.
513 EmitAggMemberInitializer(CGF, LHS, Init, ArrayIndexVar,
514 Array->getElementType(), ArrayIndexes, Index + 1);
515
Douglas Gregor94f9a482010-05-05 05:51:00 +0000516 CGF.EmitBlock(ContinueBlock);
517
518 // Emit the increment of the loop counter.
519 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
520 Counter = CGF.Builder.CreateLoad(IndexVar);
521 NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc");
522 CGF.Builder.CreateStore(NextVal, IndexVar);
523
524 // Finally, branch back up to the condition for the next iteration.
525 CGF.EmitBranch(CondBlock);
526
527 // Emit the fall-through block.
528 CGF.EmitBlock(AfterFor, true);
529}
John McCall1d987562010-07-21 01:23:41 +0000530
Anders Carlssonfb404882009-12-24 22:46:43 +0000531static void EmitMemberInitializer(CodeGenFunction &CGF,
532 const CXXRecordDecl *ClassDecl,
Alexis Hunt1d792652011-01-08 20:30:50 +0000533 CXXCtorInitializer *MemberInit,
Douglas Gregor94f9a482010-05-05 05:51:00 +0000534 const CXXConstructorDecl *Constructor,
535 FunctionArgList &Args) {
Francois Pichetd583da02010-12-04 09:14:42 +0000536 assert(MemberInit->isAnyMemberInitializer() &&
Anders Carlssonfb404882009-12-24 22:46:43 +0000537 "Must have member initializer!");
Richard Smith938f40b2011-06-11 17:19:42 +0000538 assert(MemberInit->getInit() && "Must have initializer!");
Anders Carlssonfb404882009-12-24 22:46:43 +0000539
540 // non-static data member initializers.
Francois Pichetd583da02010-12-04 09:14:42 +0000541 FieldDecl *Field = MemberInit->getAnyMember();
Eli Friedman6ae63022012-02-14 02:15:49 +0000542 QualType FieldType = Field->getType();
Anders Carlssonfb404882009-12-24 22:46:43 +0000543
544 llvm::Value *ThisPtr = CGF.LoadCXXThis();
Eli Friedman7f1ff602012-04-16 03:54:45 +0000545 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
Eli Friedmanf6d21842012-08-08 03:51:37 +0000546 LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
Eli Friedman7f1ff602012-04-16 03:54:45 +0000547
Francois Pichetd583da02010-12-04 09:14:42 +0000548 if (MemberInit->isIndirectMemberInitializer()) {
Eli Friedmanf6d21842012-08-08 03:51:37 +0000549 // If we are initializing an anonymous union field, drill down to
550 // the field.
551 IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember();
Aaron Ballman29c94602014-03-07 18:36:15 +0000552 for (const auto *I : IndirectField->chain())
Aaron Ballman13916082014-03-07 18:11:58 +0000553 LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(I));
Francois Pichetd583da02010-12-04 09:14:42 +0000554 FieldType = MemberInit->getIndirectMember()->getAnonField()->getType();
John McCallc4094932010-05-21 01:18:57 +0000555 } else {
Eli Friedmanf6d21842012-08-08 03:51:37 +0000556 LHS = CGF.EmitLValueForFieldInitialization(LHS, Field);
Anders Carlssonfb404882009-12-24 22:46:43 +0000557 }
558
Eli Friedman6ae63022012-02-14 02:15:49 +0000559 // Special case: if we are in a copy or move constructor, and we are copying
560 // an array of PODs or classes with trivial copy constructors, ignore the
561 // AST and perform the copy we know is equivalent.
562 // FIXME: This is hacky at best... if we had a bit more explicit information
563 // in the AST, we could generalize it more easily.
564 const ConstantArrayType *Array
565 = CGF.getContext().getAsConstantArrayType(FieldType);
Jordan Rose54533f72013-08-07 16:16:48 +0000566 if (Array && Constructor->isDefaulted() &&
Eli Friedman6ae63022012-02-14 02:15:49 +0000567 Constructor->isCopyOrMoveConstructor()) {
568 QualType BaseElementTy = CGF.getContext().getBaseElementType(Array);
Richard Smith993f25a2012-11-07 23:56:21 +0000569 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
Eli Friedman6ae63022012-02-14 02:15:49 +0000570 if (BaseElementTy.isPODType(CGF.getContext()) ||
Richard Smith993f25a2012-11-07 23:56:21 +0000571 (CE && CE->getConstructor()->isTrivial())) {
572 // Find the source pointer. We know it's the last argument because
573 // we know we're in an implicit copy constructor.
Eli Friedman6ae63022012-02-14 02:15:49 +0000574 unsigned SrcArgIndex = Args.size() - 1;
575 llvm::Value *SrcPtr
576 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex]));
Eli Friedman7f1ff602012-04-16 03:54:45 +0000577 LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
578 LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field);
Eli Friedman6ae63022012-02-14 02:15:49 +0000579
580 // Copy the aggregate.
581 CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType,
Chad Rosier615ed1a2012-03-29 17:37:10 +0000582 LHS.isVolatileQualified());
Eli Friedman6ae63022012-02-14 02:15:49 +0000583 return;
584 }
585 }
586
587 ArrayRef<VarDecl *> ArrayIndexes;
588 if (MemberInit->getNumArrayIndices())
589 ArrayIndexes = MemberInit->getArrayIndexes();
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000590 CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit(), ArrayIndexes);
Eli Friedman6ae63022012-02-14 02:15:49 +0000591}
592
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000593void CodeGenFunction::EmitInitializerForField(FieldDecl *Field,
594 LValue LHS, Expr *Init,
595 ArrayRef<VarDecl *> ArrayIndexes) {
Eli Friedman6ae63022012-02-14 02:15:49 +0000596 QualType FieldType = Field->getType();
John McCall47fb9502013-03-07 21:37:08 +0000597 switch (getEvaluationKind(FieldType)) {
598 case TEK_Scalar:
John McCall31168b02011-06-15 23:02:42 +0000599 if (LHS.isSimple()) {
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000600 EmitExprAsInit(Init, Field, LHS, false);
John McCall31168b02011-06-15 23:02:42 +0000601 } else {
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000602 RValue RHS = RValue::get(EmitScalarExpr(Init));
603 EmitStoreThroughLValue(RHS, LHS);
John McCall31168b02011-06-15 23:02:42 +0000604 }
John McCall47fb9502013-03-07 21:37:08 +0000605 break;
606 case TEK_Complex:
607 EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true);
608 break;
609 case TEK_Aggregate: {
Craig Topper8a13c412014-05-21 05:09:00 +0000610 llvm::Value *ArrayIndexVar = nullptr;
Eli Friedman6ae63022012-02-14 02:15:49 +0000611 if (ArrayIndexes.size()) {
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000612 llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Douglas Gregor94f9a482010-05-05 05:51:00 +0000613
614 // The LHS is a pointer to the first object we'll be constructing, as
615 // a flat array.
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000616 QualType BaseElementTy = getContext().getBaseElementType(FieldType);
617 llvm::Type *BasePtr = ConvertType(BaseElementTy);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000618 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000619 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(LHS.getAddress(),
620 BasePtr);
621 LHS = MakeAddrLValue(BaseAddrPtr, BaseElementTy);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000622
623 // Create an array index that will be used to walk over all of the
624 // objects we're constructing.
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000625 ArrayIndexVar = CreateTempAlloca(SizeTy, "object.index");
Douglas Gregor94f9a482010-05-05 05:51:00 +0000626 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000627 Builder.CreateStore(Zero, ArrayIndexVar);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000628
Douglas Gregor94f9a482010-05-05 05:51:00 +0000629
630 // Emit the block variables for the array indices, if any.
Eli Friedman6ae63022012-02-14 02:15:49 +0000631 for (unsigned I = 0, N = ArrayIndexes.size(); I != N; ++I)
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000632 EmitAutoVarDecl(*ArrayIndexes[I]);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000633 }
634
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000635 EmitAggMemberInitializer(*this, LHS, Init, ArrayIndexVar, FieldType,
Eli Friedman6ae63022012-02-14 02:15:49 +0000636 ArrayIndexes, 0);
Anders Carlssonfb404882009-12-24 22:46:43 +0000637 }
John McCall47fb9502013-03-07 21:37:08 +0000638 }
John McCall12cc42a2013-02-01 05:11:40 +0000639
640 // Ensure that we destroy this object if an exception is thrown
641 // later in the constructor.
642 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
643 if (needsEHCleanup(dtorKind))
644 pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
Anders Carlssonfb404882009-12-24 22:46:43 +0000645}
646
John McCallf8ff7b92010-02-23 00:48:20 +0000647/// Checks whether the given constructor is a valid subject for the
648/// complete-to-base constructor delegation optimization, i.e.
649/// emitting the complete constructor as a simple call to the base
650/// constructor.
651static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) {
652
653 // Currently we disable the optimization for classes with virtual
654 // bases because (1) the addresses of parameter variables need to be
655 // consistent across all initializers but (2) the delegate function
656 // call necessarily creates a second copy of the parameter variable.
657 //
658 // The limiting example (purely theoretical AFAIK):
659 // struct A { A(int &c) { c++; } };
660 // struct B : virtual A {
661 // B(int count) : A(count) { printf("%d\n", count); }
662 // };
663 // ...although even this example could in principle be emitted as a
664 // delegation since the address of the parameter doesn't escape.
665 if (Ctor->getParent()->getNumVBases()) {
666 // TODO: white-list trivial vbase initializers. This case wouldn't
667 // be subject to the restrictions below.
668
669 // TODO: white-list cases where:
670 // - there are no non-reference parameters to the constructor
671 // - the initializers don't access any non-reference parameters
672 // - the initializers don't take the address of non-reference
673 // parameters
674 // - etc.
675 // If we ever add any of the above cases, remember that:
676 // - function-try-blocks will always blacklist this optimization
677 // - we need to perform the constructor prologue and cleanup in
678 // EmitConstructorBody.
679
680 return false;
681 }
682
683 // We also disable the optimization for variadic functions because
684 // it's impossible to "re-pass" varargs.
685 if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic())
686 return false;
687
Alexis Hunt61bc1732011-05-01 07:04:31 +0000688 // FIXME: Decide if we can do a delegation of a delegating constructor.
689 if (Ctor->isDelegatingConstructor())
690 return false;
691
John McCallf8ff7b92010-02-23 00:48:20 +0000692 return true;
693}
694
John McCallb81884d2010-02-19 09:25:03 +0000695/// EmitConstructorBody - Emits the body of the current constructor.
696void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) {
697 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl());
698 CXXCtorType CtorType = CurGD.getCtorType();
699
Reid Kleckner340ad862014-01-13 22:57:31 +0000700 assert((CGM.getTarget().getCXXABI().hasConstructorVariants() ||
701 CtorType == Ctor_Complete) &&
702 "can only generate complete ctor for this ABI");
703
John McCallf8ff7b92010-02-23 00:48:20 +0000704 // Before we go any further, try the complete->base constructor
705 // delegation optimization.
Timur Iskhodzhanovf32a3772012-04-20 08:05:00 +0000706 if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) &&
John McCallc8e01702013-04-16 22:48:15 +0000707 CGM.getTarget().getCXXABI().hasConstructorVariants()) {
Devang Pateld76c1db2010-08-11 21:04:37 +0000708 if (CGDebugInfo *DI = getDebugInfo())
Eric Christopher7cdf9482011-10-13 21:45:18 +0000709 DI->EmitLocation(Builder, Ctor->getLocEnd());
Nick Lewycky2d84e842013-10-02 02:29:49 +0000710 EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getLocEnd());
John McCallf8ff7b92010-02-23 00:48:20 +0000711 return;
712 }
713
Richard Smith46bb5812014-08-01 01:56:39 +0000714 const FunctionDecl *Definition = 0;
715 Stmt *Body = Ctor->getBody(Definition);
716 assert(Definition == Ctor && "emitting wrong constructor body");
John McCallb81884d2010-02-19 09:25:03 +0000717
John McCallf8ff7b92010-02-23 00:48:20 +0000718 // Enter the function-try-block before the constructor prologue if
719 // applicable.
John McCallf8ff7b92010-02-23 00:48:20 +0000720 bool IsTryBody = (Body && isa<CXXTryStmt>(Body));
John McCallf8ff7b92010-02-23 00:48:20 +0000721 if (IsTryBody)
John McCallb609d3f2010-07-07 06:56:46 +0000722 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
John McCallb81884d2010-02-19 09:25:03 +0000723
Justin Bogner81c22c22014-01-23 02:54:27 +0000724 RegionCounter Cnt = getPGORegionCounter(Body);
725 Cnt.beginRegion(Builder);
726
Richard Smithcc1b96d2013-06-12 22:31:48 +0000727 RunCleanupsScope RunCleanups(*this);
John McCallb81884d2010-02-19 09:25:03 +0000728
John McCall88313032012-03-30 04:25:03 +0000729 // TODO: in restricted cases, we can emit the vbase initializers of
730 // a complete ctor and then delegate to the base ctor.
731
John McCallf8ff7b92010-02-23 00:48:20 +0000732 // Emit the constructor prologue, i.e. the base and member
733 // initializers.
Douglas Gregor94f9a482010-05-05 05:51:00 +0000734 EmitCtorPrologue(Ctor, CtorType, Args);
John McCallb81884d2010-02-19 09:25:03 +0000735
736 // Emit the body of the statement.
John McCallf8ff7b92010-02-23 00:48:20 +0000737 if (IsTryBody)
John McCallb81884d2010-02-19 09:25:03 +0000738 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
739 else if (Body)
740 EmitStmt(Body);
John McCallb81884d2010-02-19 09:25:03 +0000741
742 // Emit any cleanup blocks associated with the member or base
743 // initializers, which includes (along the exceptional path) the
744 // destructors for those members and bases that were fully
745 // constructed.
Richard Smithcc1b96d2013-06-12 22:31:48 +0000746 RunCleanups.ForceCleanup();
John McCallb81884d2010-02-19 09:25:03 +0000747
John McCallf8ff7b92010-02-23 00:48:20 +0000748 if (IsTryBody)
John McCallb609d3f2010-07-07 06:56:46 +0000749 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
John McCallb81884d2010-02-19 09:25:03 +0000750}
751
Lang Hamesbf122742013-02-17 07:22:09 +0000752namespace {
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000753 /// RAII object to indicate that codegen is copying the value representation
754 /// instead of the object representation. Useful when copying a struct or
755 /// class which has uninitialized members and we're only performing
756 /// lvalue-to-rvalue conversion on the object but not its members.
757 class CopyingValueRepresentation {
758 public:
759 explicit CopyingValueRepresentation(CodeGenFunction &CGF)
760 : CGF(CGF), SO(*CGF.SanOpts), OldSanOpts(CGF.SanOpts) {
761 SO.Bool = false;
762 SO.Enum = false;
763 CGF.SanOpts = &SO;
764 }
765 ~CopyingValueRepresentation() {
766 CGF.SanOpts = OldSanOpts;
767 }
768 private:
769 CodeGenFunction &CGF;
770 SanitizerOptions SO;
771 const SanitizerOptions *OldSanOpts;
772 };
773}
774
775namespace {
Lang Hamesbf122742013-02-17 07:22:09 +0000776 class FieldMemcpyizer {
777 public:
778 FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl,
779 const VarDecl *SrcRec)
780 : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec),
781 RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)),
Craig Topper8a13c412014-05-21 05:09:00 +0000782 FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0),
783 LastFieldOffset(0), LastAddedFieldIndex(0) {}
Lang Hamesbf122742013-02-17 07:22:09 +0000784
785 static bool isMemcpyableField(FieldDecl *F) {
786 Qualifiers Qual = F->getType().getQualifiers();
787 if (Qual.hasVolatile() || Qual.hasObjCLifetime())
788 return false;
789 return true;
790 }
791
792 void addMemcpyableField(FieldDecl *F) {
Craig Topper8a13c412014-05-21 05:09:00 +0000793 if (!FirstField)
Lang Hamesbf122742013-02-17 07:22:09 +0000794 addInitialField(F);
795 else
796 addNextField(F);
797 }
798
799 CharUnits getMemcpySize() const {
800 unsigned LastFieldSize =
801 LastField->isBitField() ?
802 LastField->getBitWidthValue(CGF.getContext()) :
803 CGF.getContext().getTypeSize(LastField->getType());
804 uint64_t MemcpySizeBits =
805 LastFieldOffset + LastFieldSize - FirstFieldOffset +
806 CGF.getContext().getCharWidth() - 1;
807 CharUnits MemcpySize =
808 CGF.getContext().toCharUnitsFromBits(MemcpySizeBits);
809 return MemcpySize;
810 }
811
812 void emitMemcpy() {
813 // Give the subclass a chance to bail out if it feels the memcpy isn't
814 // worth it (e.g. Hasn't aggregated enough data).
Craig Topper8a13c412014-05-21 05:09:00 +0000815 if (!FirstField) {
Lang Hamesbf122742013-02-17 07:22:09 +0000816 return;
817 }
818
Lang Hames1694e0d2013-02-27 04:14:49 +0000819 CharUnits Alignment;
Lang Hamesbf122742013-02-17 07:22:09 +0000820
821 if (FirstField->isBitField()) {
822 const CGRecordLayout &RL =
823 CGF.getTypes().getCGRecordLayout(FirstField->getParent());
824 const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField);
Lang Hames1694e0d2013-02-27 04:14:49 +0000825 Alignment = CharUnits::fromQuantity(BFInfo.StorageAlignment);
826 } else {
Lang Hames224ae882013-03-05 20:27:24 +0000827 Alignment = CGF.getContext().getDeclAlign(FirstField);
Lang Hames1694e0d2013-02-27 04:14:49 +0000828 }
Lang Hamesbf122742013-02-17 07:22:09 +0000829
Lang Hames1694e0d2013-02-27 04:14:49 +0000830 assert((CGF.getContext().toCharUnitsFromBits(FirstFieldOffset) %
831 Alignment) == 0 && "Bad field alignment.");
832
Lang Hamesbf122742013-02-17 07:22:09 +0000833 CharUnits MemcpySize = getMemcpySize();
834 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
835 llvm::Value *ThisPtr = CGF.LoadCXXThis();
836 LValue DestLV = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
837 LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField);
838 llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec));
839 LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
840 LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField);
841
842 emitMemcpyIR(Dest.isBitField() ? Dest.getBitFieldAddr() : Dest.getAddress(),
843 Src.isBitField() ? Src.getBitFieldAddr() : Src.getAddress(),
844 MemcpySize, Alignment);
845 reset();
846 }
847
848 void reset() {
Craig Topper8a13c412014-05-21 05:09:00 +0000849 FirstField = nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +0000850 }
851
852 protected:
853 CodeGenFunction &CGF;
854 const CXXRecordDecl *ClassDecl;
855
856 private:
857
858 void emitMemcpyIR(llvm::Value *DestPtr, llvm::Value *SrcPtr,
859 CharUnits Size, CharUnits Alignment) {
860 llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
861 llvm::Type *DBP =
862 llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace());
863 DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP);
864
865 llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
866 llvm::Type *SBP =
867 llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace());
868 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP);
869
870 CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity(),
871 Alignment.getQuantity());
872 }
873
874 void addInitialField(FieldDecl *F) {
875 FirstField = F;
876 LastField = F;
877 FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex());
878 LastFieldOffset = FirstFieldOffset;
879 LastAddedFieldIndex = F->getFieldIndex();
880 return;
881 }
882
883 void addNextField(FieldDecl *F) {
John McCall6054d5a2013-05-07 05:20:46 +0000884 // For the most part, the following invariant will hold:
885 // F->getFieldIndex() == LastAddedFieldIndex + 1
886 // The one exception is that Sema won't add a copy-initializer for an
887 // unnamed bitfield, which will show up here as a gap in the sequence.
888 assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 &&
889 "Cannot aggregate fields out of order.");
Lang Hamesbf122742013-02-17 07:22:09 +0000890 LastAddedFieldIndex = F->getFieldIndex();
891
892 // The 'first' and 'last' fields are chosen by offset, rather than field
893 // index. This allows the code to support bitfields, as well as regular
894 // fields.
895 uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex());
896 if (FOffset < FirstFieldOffset) {
897 FirstField = F;
898 FirstFieldOffset = FOffset;
899 } else if (FOffset > LastFieldOffset) {
900 LastField = F;
901 LastFieldOffset = FOffset;
902 }
903 }
904
905 const VarDecl *SrcRec;
906 const ASTRecordLayout &RecLayout;
907 FieldDecl *FirstField;
908 FieldDecl *LastField;
909 uint64_t FirstFieldOffset, LastFieldOffset;
910 unsigned LastAddedFieldIndex;
911 };
912
913 class ConstructorMemcpyizer : public FieldMemcpyizer {
914 private:
915
916 /// Get source argument for copy constructor. Returns null if not a copy
David Majnemer196ac332014-09-11 23:05:02 +0000917 /// constructor.
918 static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF,
919 const CXXConstructorDecl *CD,
Lang Hamesbf122742013-02-17 07:22:09 +0000920 FunctionArgList &Args) {
Jordan Rose54533f72013-08-07 16:16:48 +0000921 if (CD->isCopyOrMoveConstructor() && CD->isDefaulted())
David Majnemer196ac332014-09-11 23:05:02 +0000922 return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)];
Craig Topper8a13c412014-05-21 05:09:00 +0000923 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +0000924 }
925
926 // Returns true if a CXXCtorInitializer represents a member initialization
927 // that can be rolled into a memcpy.
928 bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const {
929 if (!MemcpyableCtor)
930 return false;
931 FieldDecl *Field = MemberInit->getMember();
Craig Topper8a13c412014-05-21 05:09:00 +0000932 assert(Field && "No field for member init.");
Lang Hamesbf122742013-02-17 07:22:09 +0000933 QualType FieldType = Field->getType();
934 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
935
936 // Bail out on non-POD, not-trivially-constructable members.
937 if (!(CE && CE->getConstructor()->isTrivial()) &&
938 !(FieldType.isTriviallyCopyableType(CGF.getContext()) ||
939 FieldType->isReferenceType()))
940 return false;
941
942 // Bail out on volatile fields.
943 if (!isMemcpyableField(Field))
944 return false;
945
946 // Otherwise we're good.
947 return true;
948 }
949
950 public:
951 ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD,
952 FunctionArgList &Args)
David Majnemer196ac332014-09-11 23:05:02 +0000953 : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CGF, CD, Args)),
Lang Hamesbf122742013-02-17 07:22:09 +0000954 ConstructorDecl(CD),
Jordan Rose54533f72013-08-07 16:16:48 +0000955 MemcpyableCtor(CD->isDefaulted() &&
Lang Hamesbf122742013-02-17 07:22:09 +0000956 CD->isCopyOrMoveConstructor() &&
957 CGF.getLangOpts().getGC() == LangOptions::NonGC),
958 Args(Args) { }
959
960 void addMemberInitializer(CXXCtorInitializer *MemberInit) {
961 if (isMemberInitMemcpyable(MemberInit)) {
962 AggregatedInits.push_back(MemberInit);
963 addMemcpyableField(MemberInit->getMember());
964 } else {
965 emitAggregatedInits();
966 EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit,
967 ConstructorDecl, Args);
968 }
969 }
970
971 void emitAggregatedInits() {
972 if (AggregatedInits.size() <= 1) {
973 // This memcpy is too small to be worthwhile. Fall back on default
974 // codegen.
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000975 if (!AggregatedInits.empty()) {
976 CopyingValueRepresentation CVR(CGF);
Lang Hamesbf122742013-02-17 07:22:09 +0000977 EmitMemberInitializer(CGF, ConstructorDecl->getParent(),
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000978 AggregatedInits[0], ConstructorDecl, Args);
Lang Hamesbf122742013-02-17 07:22:09 +0000979 }
980 reset();
981 return;
982 }
983
984 pushEHDestructors();
985 emitMemcpy();
986 AggregatedInits.clear();
987 }
988
989 void pushEHDestructors() {
990 llvm::Value *ThisPtr = CGF.LoadCXXThis();
991 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
992 LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
993
994 for (unsigned i = 0; i < AggregatedInits.size(); ++i) {
995 QualType FieldType = AggregatedInits[i]->getMember()->getType();
996 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
997 if (CGF.needsEHCleanup(dtorKind))
998 CGF.pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
999 }
1000 }
1001
1002 void finish() {
1003 emitAggregatedInits();
1004 }
1005
1006 private:
1007 const CXXConstructorDecl *ConstructorDecl;
1008 bool MemcpyableCtor;
1009 FunctionArgList &Args;
1010 SmallVector<CXXCtorInitializer*, 16> AggregatedInits;
1011 };
1012
1013 class AssignmentMemcpyizer : public FieldMemcpyizer {
1014 private:
1015
1016 // Returns the memcpyable field copied by the given statement, if one
Nick Lewycky8b4e3792013-09-11 02:03:20 +00001017 // exists. Otherwise returns null.
1018 FieldDecl *getMemcpyableField(Stmt *S) {
Lang Hamesbf122742013-02-17 07:22:09 +00001019 if (!AssignmentsMemcpyable)
Craig Topper8a13c412014-05-21 05:09:00 +00001020 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001021 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) {
1022 // Recognise trivial assignments.
1023 if (BO->getOpcode() != BO_Assign)
Craig Topper8a13c412014-05-21 05:09:00 +00001024 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001025 MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS());
1026 if (!ME)
Craig Topper8a13c412014-05-21 05:09:00 +00001027 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001028 FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1029 if (!Field || !isMemcpyableField(Field))
Craig Topper8a13c412014-05-21 05:09:00 +00001030 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001031 Stmt *RHS = BO->getRHS();
1032 if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS))
1033 RHS = EC->getSubExpr();
1034 if (!RHS)
Craig Topper8a13c412014-05-21 05:09:00 +00001035 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001036 MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS);
1037 if (dyn_cast<FieldDecl>(ME2->getMemberDecl()) != Field)
Craig Topper8a13c412014-05-21 05:09:00 +00001038 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001039 return Field;
1040 } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) {
1041 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl());
1042 if (!(MD && (MD->isCopyAssignmentOperator() ||
1043 MD->isMoveAssignmentOperator()) &&
1044 MD->isTrivial()))
Craig Topper8a13c412014-05-21 05:09:00 +00001045 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001046 MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument());
1047 if (!IOA)
Craig Topper8a13c412014-05-21 05:09:00 +00001048 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001049 FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl());
1050 if (!Field || !isMemcpyableField(Field))
Craig Topper8a13c412014-05-21 05:09:00 +00001051 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001052 MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0));
1053 if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl()))
Craig Topper8a13c412014-05-21 05:09:00 +00001054 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001055 return Field;
1056 } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
1057 FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
1058 if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy)
Craig Topper8a13c412014-05-21 05:09:00 +00001059 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001060 Expr *DstPtr = CE->getArg(0);
1061 if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr))
1062 DstPtr = DC->getSubExpr();
1063 UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr);
1064 if (!DUO || DUO->getOpcode() != UO_AddrOf)
Craig Topper8a13c412014-05-21 05:09:00 +00001065 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001066 MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr());
1067 if (!ME)
Craig Topper8a13c412014-05-21 05:09:00 +00001068 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001069 FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1070 if (!Field || !isMemcpyableField(Field))
Craig Topper8a13c412014-05-21 05:09:00 +00001071 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001072 Expr *SrcPtr = CE->getArg(1);
1073 if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr))
1074 SrcPtr = SC->getSubExpr();
1075 UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr);
1076 if (!SUO || SUO->getOpcode() != UO_AddrOf)
Craig Topper8a13c412014-05-21 05:09:00 +00001077 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001078 MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr());
1079 if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl()))
Craig Topper8a13c412014-05-21 05:09:00 +00001080 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001081 return Field;
1082 }
1083
Craig Topper8a13c412014-05-21 05:09:00 +00001084 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001085 }
1086
1087 bool AssignmentsMemcpyable;
1088 SmallVector<Stmt*, 16> AggregatedStmts;
1089
1090 public:
1091
1092 AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD,
1093 FunctionArgList &Args)
1094 : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]),
1095 AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) {
1096 assert(Args.size() == 2);
1097 }
1098
1099 void emitAssignment(Stmt *S) {
1100 FieldDecl *F = getMemcpyableField(S);
1101 if (F) {
1102 addMemcpyableField(F);
1103 AggregatedStmts.push_back(S);
1104 } else {
1105 emitAggregatedStmts();
1106 CGF.EmitStmt(S);
1107 }
1108 }
1109
1110 void emitAggregatedStmts() {
1111 if (AggregatedStmts.size() <= 1) {
Nick Lewycky8b4e3792013-09-11 02:03:20 +00001112 if (!AggregatedStmts.empty()) {
1113 CopyingValueRepresentation CVR(CGF);
1114 CGF.EmitStmt(AggregatedStmts[0]);
1115 }
Lang Hamesbf122742013-02-17 07:22:09 +00001116 reset();
1117 }
1118
1119 emitMemcpy();
1120 AggregatedStmts.clear();
1121 }
1122
1123 void finish() {
1124 emitAggregatedStmts();
1125 }
1126 };
1127
1128}
1129
Anders Carlssonfb404882009-12-24 22:46:43 +00001130/// EmitCtorPrologue - This routine generates necessary code to initialize
1131/// base classes and non-static data members belonging to this constructor.
Anders Carlssonfb404882009-12-24 22:46:43 +00001132void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
Douglas Gregor94f9a482010-05-05 05:51:00 +00001133 CXXCtorType CtorType,
1134 FunctionArgList &Args) {
Alexis Hunt61bc1732011-05-01 07:04:31 +00001135 if (CD->isDelegatingConstructor())
1136 return EmitDelegatingCXXConstructorCall(CD, Args);
1137
Anders Carlssonfb404882009-12-24 22:46:43 +00001138 const CXXRecordDecl *ClassDecl = CD->getParent();
Anders Carlsson5dc86332010-02-02 19:58:43 +00001139
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001140 CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
1141 E = CD->init_end();
1142
Craig Topper8a13c412014-05-21 05:09:00 +00001143 llvm::BasicBlock *BaseCtorContinueBB = nullptr;
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001144 if (ClassDecl->getNumVBases() &&
1145 !CGM.getTarget().getCXXABI().hasConstructorVariants()) {
1146 // The ABIs that don't have constructor variants need to put a branch
1147 // before the virtual base initialization code.
Reid Kleckner7810af02013-06-19 15:20:38 +00001148 BaseCtorContinueBB =
1149 CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001150 assert(BaseCtorContinueBB);
1151 }
1152
1153 // Virtual base initializers first.
1154 for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) {
1155 EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
1156 }
1157
1158 if (BaseCtorContinueBB) {
1159 // Complete object handler should continue to the remaining initializers.
1160 Builder.CreateBr(BaseCtorContinueBB);
1161 EmitBlock(BaseCtorContinueBB);
1162 }
1163
1164 // Then, non-virtual base initializers.
1165 for (; B != E && (*B)->isBaseInitializer(); B++) {
1166 assert(!(*B)->isBaseVirtual());
1167 EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
Anders Carlssonfb404882009-12-24 22:46:43 +00001168 }
1169
Anders Carlssond5895932010-03-28 21:07:49 +00001170 InitializeVTablePointers(ClassDecl);
Anders Carlsson5dc86332010-02-02 19:58:43 +00001171
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001172 // And finally, initialize class members.
Richard Smith852c9db2013-04-20 22:23:05 +00001173 FieldConstructionScope FCS(*this, CXXThisValue);
Lang Hamesbf122742013-02-17 07:22:09 +00001174 ConstructorMemcpyizer CM(*this, CD, Args);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001175 for (; B != E; B++) {
1176 CXXCtorInitializer *Member = (*B);
1177 assert(!Member->isBaseInitializer());
1178 assert(Member->isAnyMemberInitializer() &&
1179 "Delegating initializer on non-delegating constructor");
1180 CM.addMemberInitializer(Member);
1181 }
Lang Hamesbf122742013-02-17 07:22:09 +00001182 CM.finish();
Anders Carlssonfb404882009-12-24 22:46:43 +00001183}
1184
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001185static bool
1186FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field);
1187
1188static bool
1189HasTrivialDestructorBody(ASTContext &Context,
1190 const CXXRecordDecl *BaseClassDecl,
1191 const CXXRecordDecl *MostDerivedClassDecl)
1192{
1193 // If the destructor is trivial we don't have to check anything else.
1194 if (BaseClassDecl->hasTrivialDestructor())
1195 return true;
1196
1197 if (!BaseClassDecl->getDestructor()->hasTrivialBody())
1198 return false;
1199
1200 // Check fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001201 for (const auto *Field : BaseClassDecl->fields())
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001202 if (!FieldHasTrivialDestructorBody(Context, Field))
1203 return false;
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001204
1205 // Check non-virtual bases.
Aaron Ballman574705e2014-03-13 15:41:46 +00001206 for (const auto &I : BaseClassDecl->bases()) {
1207 if (I.isVirtual())
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001208 continue;
1209
1210 const CXXRecordDecl *NonVirtualBase =
Aaron Ballman574705e2014-03-13 15:41:46 +00001211 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001212 if (!HasTrivialDestructorBody(Context, NonVirtualBase,
1213 MostDerivedClassDecl))
1214 return false;
1215 }
1216
1217 if (BaseClassDecl == MostDerivedClassDecl) {
1218 // Check virtual bases.
Aaron Ballman445a9392014-03-13 16:15:17 +00001219 for (const auto &I : BaseClassDecl->vbases()) {
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001220 const CXXRecordDecl *VirtualBase =
Aaron Ballman445a9392014-03-13 16:15:17 +00001221 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001222 if (!HasTrivialDestructorBody(Context, VirtualBase,
1223 MostDerivedClassDecl))
1224 return false;
1225 }
1226 }
1227
1228 return true;
1229}
1230
1231static bool
1232FieldHasTrivialDestructorBody(ASTContext &Context,
1233 const FieldDecl *Field)
1234{
1235 QualType FieldBaseElementType = Context.getBaseElementType(Field->getType());
1236
1237 const RecordType *RT = FieldBaseElementType->getAs<RecordType>();
1238 if (!RT)
1239 return true;
1240
1241 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1242 return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
1243}
1244
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001245/// CanSkipVTablePointerInitialization - Check whether we need to initialize
1246/// any vtable pointers before calling this destructor.
1247static bool CanSkipVTablePointerInitialization(ASTContext &Context,
Anders Carlssond6f15182011-05-16 04:08:36 +00001248 const CXXDestructorDecl *Dtor) {
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001249 if (!Dtor->hasTrivialBody())
1250 return false;
1251
1252 // Check the fields.
1253 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001254 for (const auto *Field : ClassDecl->fields())
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001255 if (!FieldHasTrivialDestructorBody(Context, Field))
1256 return false;
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001257
1258 return true;
1259}
1260
John McCallb81884d2010-02-19 09:25:03 +00001261/// EmitDestructorBody - Emits the body of the current destructor.
1262void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) {
1263 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl());
1264 CXXDtorType DtorType = CurGD.getDtorType();
1265
John McCallf99a6312010-07-21 05:30:47 +00001266 // The call to operator delete in a deleting destructor happens
1267 // outside of the function-try-block, which means it's always
1268 // possible to delegate the destructor body to the complete
1269 // destructor. Do so.
1270 if (DtorType == Dtor_Deleting) {
1271 EnterDtorCleanups(Dtor, Dtor_Deleting);
1272 EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
Douglas Gregor61535002013-01-31 05:50:40 +00001273 /*Delegating=*/false, LoadCXXThis());
John McCallf99a6312010-07-21 05:30:47 +00001274 PopCleanupBlock();
1275 return;
1276 }
1277
John McCallb81884d2010-02-19 09:25:03 +00001278 Stmt *Body = Dtor->getBody();
1279
1280 // If the body is a function-try-block, enter the try before
John McCallf99a6312010-07-21 05:30:47 +00001281 // anything else.
1282 bool isTryBody = (Body && isa<CXXTryStmt>(Body));
John McCallb81884d2010-02-19 09:25:03 +00001283 if (isTryBody)
John McCallb609d3f2010-07-07 06:56:46 +00001284 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
John McCallb81884d2010-02-19 09:25:03 +00001285
John McCallf99a6312010-07-21 05:30:47 +00001286 // Enter the epilogue cleanups.
1287 RunCleanupsScope DtorEpilogue(*this);
1288
John McCallb81884d2010-02-19 09:25:03 +00001289 // If this is the complete variant, just invoke the base variant;
1290 // the epilogue will destruct the virtual bases. But we can't do
1291 // this optimization if the body is a function-try-block, because
Reid Klecknere7de47e2013-07-22 13:51:44 +00001292 // we'd introduce *two* handler blocks. In the Microsoft ABI, we
1293 // always delegate because we might not have a definition in this TU.
John McCallf99a6312010-07-21 05:30:47 +00001294 switch (DtorType) {
1295 case Dtor_Deleting: llvm_unreachable("already handled deleting case");
1296
1297 case Dtor_Complete:
Reid Klecknere7de47e2013-07-22 13:51:44 +00001298 assert((Body || getTarget().getCXXABI().isMicrosoft()) &&
1299 "can't emit a dtor without a body for non-Microsoft ABIs");
1300
John McCallf99a6312010-07-21 05:30:47 +00001301 // Enter the cleanup scopes for virtual bases.
1302 EnterDtorCleanups(Dtor, Dtor_Complete);
1303
Reid Klecknere7de47e2013-07-22 13:51:44 +00001304 if (!isTryBody) {
John McCallf99a6312010-07-21 05:30:47 +00001305 EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false,
Douglas Gregor61535002013-01-31 05:50:40 +00001306 /*Delegating=*/false, LoadCXXThis());
John McCallf99a6312010-07-21 05:30:47 +00001307 break;
1308 }
1309 // Fallthrough: act like we're in the base variant.
John McCallb81884d2010-02-19 09:25:03 +00001310
John McCallf99a6312010-07-21 05:30:47 +00001311 case Dtor_Base:
Reid Klecknere7de47e2013-07-22 13:51:44 +00001312 assert(Body);
1313
Justin Bogner81c22c22014-01-23 02:54:27 +00001314 RegionCounter Cnt = getPGORegionCounter(Body);
1315 Cnt.beginRegion(Builder);
1316
John McCallf99a6312010-07-21 05:30:47 +00001317 // Enter the cleanup scopes for fields and non-virtual bases.
1318 EnterDtorCleanups(Dtor, Dtor_Base);
1319
1320 // Initialize the vtable pointers before entering the body.
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001321 if (!CanSkipVTablePointerInitialization(getContext(), Dtor))
1322 InitializeVTablePointers(Dtor->getParent());
John McCallf99a6312010-07-21 05:30:47 +00001323
1324 if (isTryBody)
1325 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
1326 else if (Body)
1327 EmitStmt(Body);
1328 else {
1329 assert(Dtor->isImplicit() && "bodyless dtor not implicit");
1330 // nothing to do besides what's in the epilogue
1331 }
Fariborz Jahanian0c12ed12011-02-02 23:12:46 +00001332 // -fapple-kext must inline any call to this dtor into
1333 // the caller's body.
Richard Smith9c6890a2012-11-01 22:30:59 +00001334 if (getLangOpts().AppleKext)
Bill Wendling207f0532012-12-20 19:27:06 +00001335 CurFn->addFnAttr(llvm::Attribute::AlwaysInline);
John McCallf99a6312010-07-21 05:30:47 +00001336 break;
John McCallb81884d2010-02-19 09:25:03 +00001337 }
1338
John McCallf99a6312010-07-21 05:30:47 +00001339 // Jump out through the epilogue cleanups.
1340 DtorEpilogue.ForceCleanup();
John McCallb81884d2010-02-19 09:25:03 +00001341
1342 // Exit the try if applicable.
1343 if (isTryBody)
John McCallb609d3f2010-07-07 06:56:46 +00001344 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
John McCallb81884d2010-02-19 09:25:03 +00001345}
1346
Lang Hamesbf122742013-02-17 07:22:09 +00001347void CodeGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &Args) {
1348 const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl());
1349 const Stmt *RootS = AssignOp->getBody();
1350 assert(isa<CompoundStmt>(RootS) &&
1351 "Body of an implicit assignment operator should be compound stmt.");
1352 const CompoundStmt *RootCS = cast<CompoundStmt>(RootS);
1353
1354 LexicalScope Scope(*this, RootCS->getSourceRange());
1355
1356 AssignmentMemcpyizer AM(*this, AssignOp, Args);
Aaron Ballmanc7e4e212014-03-17 14:19:37 +00001357 for (auto *I : RootCS->body())
1358 AM.emitAssignment(I);
Lang Hamesbf122742013-02-17 07:22:09 +00001359 AM.finish();
1360}
1361
John McCallf99a6312010-07-21 05:30:47 +00001362namespace {
1363 /// Call the operator delete associated with the current destructor.
John McCallcda666c2010-07-21 07:22:38 +00001364 struct CallDtorDelete : EHScopeStack::Cleanup {
John McCallf99a6312010-07-21 05:30:47 +00001365 CallDtorDelete() {}
1366
Craig Topper4f12f102014-03-12 06:41:41 +00001367 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCallf99a6312010-07-21 05:30:47 +00001368 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1369 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1370 CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
1371 CGF.getContext().getTagDeclType(ClassDecl));
1372 }
1373 };
1374
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001375 struct CallDtorDeleteConditional : EHScopeStack::Cleanup {
1376 llvm::Value *ShouldDeleteCondition;
1377 public:
1378 CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition)
1379 : ShouldDeleteCondition(ShouldDeleteCondition) {
Craig Topper8a13c412014-05-21 05:09:00 +00001380 assert(ShouldDeleteCondition != nullptr);
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001381 }
1382
Craig Topper4f12f102014-03-12 06:41:41 +00001383 void Emit(CodeGenFunction &CGF, Flags flags) override {
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001384 llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete");
1385 llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue");
1386 llvm::Value *ShouldCallDelete
1387 = CGF.Builder.CreateIsNull(ShouldDeleteCondition);
1388 CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB);
1389
1390 CGF.EmitBlock(callDeleteBB);
1391 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1392 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1393 CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
1394 CGF.getContext().getTagDeclType(ClassDecl));
1395 CGF.Builder.CreateBr(continueBB);
1396
1397 CGF.EmitBlock(continueBB);
1398 }
1399 };
1400
John McCall4bd0fb12011-07-12 16:41:08 +00001401 class DestroyField : public EHScopeStack::Cleanup {
1402 const FieldDecl *field;
Peter Collingbourne1425b452012-01-26 03:33:36 +00001403 CodeGenFunction::Destroyer *destroyer;
John McCall4bd0fb12011-07-12 16:41:08 +00001404 bool useEHCleanupForArray;
John McCallf99a6312010-07-21 05:30:47 +00001405
John McCall4bd0fb12011-07-12 16:41:08 +00001406 public:
1407 DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer,
1408 bool useEHCleanupForArray)
Peter Collingbourne1425b452012-01-26 03:33:36 +00001409 : field(field), destroyer(destroyer),
John McCall4bd0fb12011-07-12 16:41:08 +00001410 useEHCleanupForArray(useEHCleanupForArray) {}
John McCallf99a6312010-07-21 05:30:47 +00001411
Craig Topper4f12f102014-03-12 06:41:41 +00001412 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall4bd0fb12011-07-12 16:41:08 +00001413 // Find the address of the field.
1414 llvm::Value *thisValue = CGF.LoadCXXThis();
Eli Friedman7f1ff602012-04-16 03:54:45 +00001415 QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent());
1416 LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy);
1417 LValue LV = CGF.EmitLValueForField(ThisLV, field);
John McCall4bd0fb12011-07-12 16:41:08 +00001418 assert(LV.isSimple());
1419
1420 CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer,
John McCall30317fd2011-07-12 20:27:29 +00001421 flags.isForNormalCleanup() && useEHCleanupForArray);
John McCallf99a6312010-07-21 05:30:47 +00001422 }
1423 };
1424}
1425
Hans Wennborgdeff7032013-12-18 01:39:59 +00001426/// \brief Emit all code that comes at the end of class's
Anders Carlssonfb404882009-12-24 22:46:43 +00001427/// destructor. This is to call destructors on members and base classes
1428/// in reverse order of their construction.
John McCallf99a6312010-07-21 05:30:47 +00001429void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD,
1430 CXXDtorType DtorType) {
Hans Wennborg853ae942014-05-30 16:59:42 +00001431 assert((!DD->isTrivial() || DD->hasAttr<DLLExportAttr>()) &&
1432 "Should not emit dtor epilogue for non-exported trivial dtor!");
Anders Carlssonfb404882009-12-24 22:46:43 +00001433
John McCallf99a6312010-07-21 05:30:47 +00001434 // The deleting-destructor phase just needs to call the appropriate
1435 // operator delete that Sema picked up.
John McCall5c60a6f2010-02-18 19:59:28 +00001436 if (DtorType == Dtor_Deleting) {
1437 assert(DD->getOperatorDelete() &&
Hans Wennborgdeff7032013-12-18 01:39:59 +00001438 "operator delete missing - EnterDtorCleanups");
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001439 if (CXXStructorImplicitParamValue) {
1440 // If there is an implicit param to the deleting dtor, it's a boolean
1441 // telling whether we should call delete at the end of the dtor.
1442 EHStack.pushCleanup<CallDtorDeleteConditional>(
1443 NormalAndEHCleanup, CXXStructorImplicitParamValue);
1444 } else {
1445 EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup);
1446 }
John McCall5c60a6f2010-02-18 19:59:28 +00001447 return;
1448 }
1449
John McCallf99a6312010-07-21 05:30:47 +00001450 const CXXRecordDecl *ClassDecl = DD->getParent();
1451
Richard Smith20104042011-09-18 12:11:43 +00001452 // Unions have no bases and do not call field destructors.
1453 if (ClassDecl->isUnion())
1454 return;
1455
John McCallf99a6312010-07-21 05:30:47 +00001456 // The complete-destructor phase just destructs all the virtual bases.
John McCall5c60a6f2010-02-18 19:59:28 +00001457 if (DtorType == Dtor_Complete) {
John McCallf99a6312010-07-21 05:30:47 +00001458
1459 // We push them in the forward order so that they'll be popped in
1460 // the reverse order.
Aaron Ballman445a9392014-03-13 16:15:17 +00001461 for (const auto &Base : ClassDecl->vbases()) {
John McCall5c60a6f2010-02-18 19:59:28 +00001462 CXXRecordDecl *BaseClassDecl
1463 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1464
1465 // Ignore trivial destructors.
1466 if (BaseClassDecl->hasTrivialDestructor())
1467 continue;
John McCallf99a6312010-07-21 05:30:47 +00001468
John McCallcda666c2010-07-21 07:22:38 +00001469 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1470 BaseClassDecl,
1471 /*BaseIsVirtual*/ true);
John McCall5c60a6f2010-02-18 19:59:28 +00001472 }
John McCallf99a6312010-07-21 05:30:47 +00001473
John McCall5c60a6f2010-02-18 19:59:28 +00001474 return;
1475 }
1476
1477 assert(DtorType == Dtor_Base);
John McCallf99a6312010-07-21 05:30:47 +00001478
1479 // Destroy non-virtual bases.
Aaron Ballman574705e2014-03-13 15:41:46 +00001480 for (const auto &Base : ClassDecl->bases()) {
John McCallf99a6312010-07-21 05:30:47 +00001481 // Ignore virtual bases.
1482 if (Base.isVirtual())
1483 continue;
1484
1485 CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl();
1486
1487 // Ignore trivial destructors.
1488 if (BaseClassDecl->hasTrivialDestructor())
1489 continue;
John McCall5c60a6f2010-02-18 19:59:28 +00001490
John McCallcda666c2010-07-21 07:22:38 +00001491 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1492 BaseClassDecl,
1493 /*BaseIsVirtual*/ false);
John McCallf99a6312010-07-21 05:30:47 +00001494 }
1495
1496 // Destroy direct fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001497 for (const auto *Field : ClassDecl->fields()) {
1498 QualType type = Field->getType();
John McCall4bd0fb12011-07-12 16:41:08 +00001499 QualType::DestructionKind dtorKind = type.isDestructedType();
1500 if (!dtorKind) continue;
John McCallf99a6312010-07-21 05:30:47 +00001501
Richard Smith921bd202012-02-26 09:11:52 +00001502 // Anonymous union members do not have their destructors called.
1503 const RecordType *RT = type->getAsUnionType();
1504 if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue;
1505
John McCall4bd0fb12011-07-12 16:41:08 +00001506 CleanupKind cleanupKind = getCleanupKind(dtorKind);
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001507 EHStack.pushCleanup<DestroyField>(cleanupKind, Field,
John McCall4bd0fb12011-07-12 16:41:08 +00001508 getDestroyer(dtorKind),
1509 cleanupKind & EHCleanup);
Anders Carlssonfb404882009-12-24 22:46:43 +00001510 }
Anders Carlssonfb404882009-12-24 22:46:43 +00001511}
1512
John McCallf677a8e2011-07-13 06:10:41 +00001513/// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1514/// constructor for each of several members of an array.
Douglas Gregor05fc5be2010-07-21 01:10:17 +00001515///
John McCallf677a8e2011-07-13 06:10:41 +00001516/// \param ctor the constructor to call for each element
John McCallf677a8e2011-07-13 06:10:41 +00001517/// \param arrayType the type of the array to initialize
1518/// \param arrayBegin an arrayType*
1519/// \param zeroInitialize true if each element should be
1520/// zero-initialized before it is constructed
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001521void CodeGenFunction::EmitCXXAggrConstructorCall(
1522 const CXXConstructorDecl *ctor, const ConstantArrayType *arrayType,
1523 llvm::Value *arrayBegin, const CXXConstructExpr *E, bool zeroInitialize) {
John McCallf677a8e2011-07-13 06:10:41 +00001524 QualType elementType;
1525 llvm::Value *numElements =
1526 emitArrayLength(arrayType, elementType, arrayBegin);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001527
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001528 EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, E, zeroInitialize);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001529}
1530
John McCallf677a8e2011-07-13 06:10:41 +00001531/// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1532/// constructor for each of several members of an array.
1533///
1534/// \param ctor the constructor to call for each element
1535/// \param numElements the number of elements in the array;
John McCall6549b312011-07-13 07:37:11 +00001536/// may be zero
John McCallf677a8e2011-07-13 06:10:41 +00001537/// \param arrayBegin a T*, where T is the type constructed by ctor
1538/// \param zeroInitialize true if each element should be
1539/// zero-initialized before it is constructed
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001540void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
1541 llvm::Value *numElements,
1542 llvm::Value *arrayBegin,
1543 const CXXConstructExpr *E,
1544 bool zeroInitialize) {
John McCall6549b312011-07-13 07:37:11 +00001545
1546 // It's legal for numElements to be zero. This can happen both
1547 // dynamically, because x can be zero in 'new A[x]', and statically,
1548 // because of GCC extensions that permit zero-length arrays. There
1549 // are probably legitimate places where we could assume that this
1550 // doesn't happen, but it's not clear that it's worth it.
Craig Topper8a13c412014-05-21 05:09:00 +00001551 llvm::BranchInst *zeroCheckBranch = nullptr;
John McCall6549b312011-07-13 07:37:11 +00001552
1553 // Optimize for a constant count.
1554 llvm::ConstantInt *constantCount
1555 = dyn_cast<llvm::ConstantInt>(numElements);
1556 if (constantCount) {
1557 // Just skip out if the constant count is zero.
1558 if (constantCount->isZero()) return;
1559
1560 // Otherwise, emit the check.
1561 } else {
1562 llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop");
1563 llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty");
1564 zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB);
1565 EmitBlock(loopBB);
1566 }
1567
John McCallf677a8e2011-07-13 06:10:41 +00001568 // Find the end of the array.
1569 llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements,
1570 "arrayctor.end");
Anders Carlsson27da15b2010-01-01 20:29:01 +00001571
John McCallf677a8e2011-07-13 06:10:41 +00001572 // Enter the loop, setting up a phi for the current location to initialize.
1573 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1574 llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop");
1575 EmitBlock(loopBB);
1576 llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2,
1577 "arrayctor.cur");
1578 cur->addIncoming(arrayBegin, entryBB);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001579
Anders Carlsson27da15b2010-01-01 20:29:01 +00001580 // Inside the loop body, emit the constructor call on the array element.
John McCallf677a8e2011-07-13 06:10:41 +00001581
1582 QualType type = getContext().getTypeDeclType(ctor->getParent());
Anders Carlsson27da15b2010-01-01 20:29:01 +00001583
Douglas Gregor05fc5be2010-07-21 01:10:17 +00001584 // Zero initialize the storage, if requested.
John McCallf677a8e2011-07-13 06:10:41 +00001585 if (zeroInitialize)
1586 EmitNullInitialization(cur, type);
Douglas Gregor05fc5be2010-07-21 01:10:17 +00001587
Anders Carlsson27da15b2010-01-01 20:29:01 +00001588 // C++ [class.temporary]p4:
1589 // There are two contexts in which temporaries are destroyed at a different
1590 // point than the end of the full-expression. The first context is when a
1591 // default constructor is called to initialize an element of an array.
1592 // If the constructor has one or more default arguments, the destruction of
1593 // every temporary created in a default argument expression is sequenced
1594 // before the construction of the next array element, if any.
1595
Anders Carlssonb9fd57f2010-03-30 03:14:41 +00001596 {
John McCallbd309292010-07-06 01:34:17 +00001597 RunCleanupsScope Scope(*this);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001598
John McCallf677a8e2011-07-13 06:10:41 +00001599 // Evaluate the constructor and its arguments in a regular
1600 // partial-destroy cleanup.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001601 if (getLangOpts().Exceptions &&
John McCallf677a8e2011-07-13 06:10:41 +00001602 !ctor->getParent()->hasTrivialDestructor()) {
1603 Destroyer *destroyer = destroyCXXObject;
1604 pushRegularPartialArrayCleanup(arrayBegin, cur, type, *destroyer);
1605 }
1606
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001607 EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/false,
1608 /*Delegating=*/false, cur, E);
Anders Carlssonb9fd57f2010-03-30 03:14:41 +00001609 }
Anders Carlsson27da15b2010-01-01 20:29:01 +00001610
John McCallf677a8e2011-07-13 06:10:41 +00001611 // Go to the next element.
1612 llvm::Value *next =
1613 Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1),
1614 "arrayctor.next");
1615 cur->addIncoming(next, Builder.GetInsertBlock());
Anders Carlsson27da15b2010-01-01 20:29:01 +00001616
John McCallf677a8e2011-07-13 06:10:41 +00001617 // Check whether that's the end of the loop.
1618 llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done");
1619 llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont");
1620 Builder.CreateCondBr(done, contBB, loopBB);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001621
John McCall6549b312011-07-13 07:37:11 +00001622 // Patch the earlier check to skip over the loop.
1623 if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
1624
John McCallf677a8e2011-07-13 06:10:41 +00001625 EmitBlock(contBB);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001626}
1627
John McCall82fe67b2011-07-09 01:37:26 +00001628void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF,
1629 llvm::Value *addr,
1630 QualType type) {
1631 const RecordType *rtype = type->castAs<RecordType>();
1632 const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl());
1633 const CXXDestructorDecl *dtor = record->getDestructor();
1634 assert(!dtor->isTrivial());
1635 CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false,
Douglas Gregor61535002013-01-31 05:50:40 +00001636 /*Delegating=*/false, addr);
John McCall82fe67b2011-07-09 01:37:26 +00001637}
1638
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001639void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
1640 CXXCtorType Type,
1641 bool ForVirtualBase,
1642 bool Delegating, llvm::Value *This,
1643 const CXXConstructExpr *E) {
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001644 // If this is a trivial constructor, just emit what's needed.
John McCallca972cd2010-02-06 00:25:16 +00001645 if (D->isTrivial()) {
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001646 if (E->getNumArgs() == 0) {
John McCallca972cd2010-02-06 00:25:16 +00001647 // Trivial default constructor, no codegen required.
1648 assert(D->isDefaultConstructor() &&
1649 "trivial 0-arg ctor not a default ctor");
Anders Carlsson27da15b2010-01-01 20:29:01 +00001650 return;
1651 }
John McCallca972cd2010-02-06 00:25:16 +00001652
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001653 assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
Sebastian Redl22653ba2011-08-30 19:58:05 +00001654 assert(D->isCopyOrMoveConstructor() &&
1655 "trivial 1-arg ctor not a copy/move ctor");
John McCallca972cd2010-02-06 00:25:16 +00001656
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001657 const Expr *Arg = E->getArg(0);
1658 QualType Ty = Arg->getType();
1659 llvm::Value *Src = EmitLValue(Arg).getAddress();
John McCallca972cd2010-02-06 00:25:16 +00001660 EmitAggregateCopy(This, Src, Ty);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001661 return;
1662 }
1663
Reid Kleckner89077a12013-12-17 19:46:40 +00001664 // C++11 [class.mfct.non-static]p2:
1665 // If a non-static member function of a class X is called for an object that
1666 // is not of type X, or of a type derived from X, the behavior is undefined.
1667 // FIXME: Provide a source location here.
1668 EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, SourceLocation(), This,
1669 getContext().getRecordType(D->getParent()));
1670
1671 CallArgList Args;
1672
1673 // Push the this ptr.
1674 Args.add(RValue::get(This), D->getThisType(getContext()));
1675
1676 // Add the rest of the user-supplied arguments.
1677 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00001678 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end(), E->getConstructor());
Reid Kleckner89077a12013-12-17 19:46:40 +00001679
1680 // Insert any ABI-specific implicit constructor arguments.
1681 unsigned ExtraArgs = CGM.getCXXABI().addImplicitConstructorArgs(
1682 *this, D, Type, ForVirtualBase, Delegating, Args);
1683
1684 // Emit the call.
Rafael Espindola1ac0ec82014-09-11 15:42:06 +00001685 llvm::Value *Callee = CGM.getAddrOfCXXStructor(D, getFromCtorType(Type));
Reid Kleckner89077a12013-12-17 19:46:40 +00001686 const CGFunctionInfo &Info =
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001687 CGM.getTypes().arrangeCXXConstructorCall(Args, D, Type, ExtraArgs);
Reid Kleckner89077a12013-12-17 19:46:40 +00001688 EmitCall(Info, Callee, ReturnValueSlot(), Args, D);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001689}
1690
John McCallf8ff7b92010-02-23 00:48:20 +00001691void
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001692CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1693 llvm::Value *This, llvm::Value *Src,
Alexey Samsonov525bf652014-08-25 21:58:56 +00001694 const CXXConstructExpr *E) {
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001695 if (D->isTrivial()) {
Alexey Samsonov96fd0a42014-08-26 20:18:26 +00001696 assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
Sebastian Redl22653ba2011-08-30 19:58:05 +00001697 assert(D->isCopyOrMoveConstructor() &&
1698 "trivial 1-arg ctor not a copy/move ctor");
Alexey Samsonov525bf652014-08-25 21:58:56 +00001699 EmitAggregateCopy(This, Src, E->arg_begin()->getType());
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001700 return;
1701 }
Rafael Espindola1ac0ec82014-09-11 15:42:06 +00001702 llvm::Value *Callee = CGM.getAddrOfCXXStructor(D, StructorType::Complete);
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001703 assert(D->isInstance() &&
1704 "Trying to emit a member call expr on a static method!");
1705
Reid Kleckner739756c2013-12-04 19:23:12 +00001706 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001707
1708 CallArgList Args;
1709
1710 // Push the this ptr.
Eli Friedman43dca6a2011-05-02 17:57:46 +00001711 Args.add(RValue::get(This), D->getThisType(getContext()));
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001712
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001713 // Push the src ptr.
Alp Toker9cacbab2014-01-20 20:26:09 +00001714 QualType QT = *(FPT->param_type_begin());
Chris Lattner2192fe52011-07-18 04:24:23 +00001715 llvm::Type *t = CGM.getTypes().ConvertType(QT);
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001716 Src = Builder.CreateBitCast(Src, t);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001717 Args.add(RValue::get(Src), QT);
Reid Kleckner739756c2013-12-04 19:23:12 +00001718
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001719 // Skip over first argument (Src).
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00001720 EmitCallArgs(Args, FPT, E->arg_begin() + 1, E->arg_end(), E->getConstructor(),
1721 /*ParamsToSkip*/ 1);
Reid Kleckner739756c2013-12-04 19:23:12 +00001722
John McCall8dda7b22012-07-07 06:41:13 +00001723 EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, RequiredArgs::All),
1724 Callee, ReturnValueSlot(), Args, D);
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00001725}
1726
1727void
John McCallf8ff7b92010-02-23 00:48:20 +00001728CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1729 CXXCtorType CtorType,
Nick Lewycky2d84e842013-10-02 02:29:49 +00001730 const FunctionArgList &Args,
1731 SourceLocation Loc) {
John McCallf8ff7b92010-02-23 00:48:20 +00001732 CallArgList DelegateArgs;
1733
1734 FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
1735 assert(I != E && "no parameters to constructor");
1736
1737 // this
Eli Friedman43dca6a2011-05-02 17:57:46 +00001738 DelegateArgs.add(RValue::get(LoadCXXThis()), (*I)->getType());
John McCallf8ff7b92010-02-23 00:48:20 +00001739 ++I;
1740
1741 // vtt
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001742 if (llvm::Value *VTT = GetVTTParameter(GlobalDecl(Ctor, CtorType),
Douglas Gregor61535002013-01-31 05:50:40 +00001743 /*ForVirtualBase=*/false,
1744 /*Delegating=*/true)) {
John McCallf8ff7b92010-02-23 00:48:20 +00001745 QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001746 DelegateArgs.add(RValue::get(VTT), VoidPP);
John McCallf8ff7b92010-02-23 00:48:20 +00001747
Peter Collingbourne66f82e62013-06-28 20:45:28 +00001748 if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) {
John McCallf8ff7b92010-02-23 00:48:20 +00001749 assert(I != E && "cannot skip vtt parameter, already done with args");
John McCalla738c252011-03-09 04:27:21 +00001750 assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type");
John McCallf8ff7b92010-02-23 00:48:20 +00001751 ++I;
1752 }
1753 }
1754
1755 // Explicit arguments.
1756 for (; I != E; ++I) {
John McCall32ea9692011-03-11 20:59:21 +00001757 const VarDecl *param = *I;
Nick Lewycky2d84e842013-10-02 02:29:49 +00001758 // FIXME: per-argument source location
1759 EmitDelegateCallArg(DelegateArgs, param, Loc);
John McCallf8ff7b92010-02-23 00:48:20 +00001760 }
1761
Rafael Espindola1ac0ec82014-09-11 15:42:06 +00001762 llvm::Value *Callee =
1763 CGM.getAddrOfCXXStructor(Ctor, getFromCtorType(CtorType));
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001764 EmitCall(CGM.getTypes()
1765 .arrangeCXXStructorDeclaration(Ctor, getFromCtorType(CtorType)),
Manman Ren01754612013-03-20 16:59:38 +00001766 Callee, ReturnValueSlot(), DelegateArgs, Ctor);
John McCallf8ff7b92010-02-23 00:48:20 +00001767}
1768
Alexis Hunt9d47faf2011-05-03 23:05:34 +00001769namespace {
1770 struct CallDelegatingCtorDtor : EHScopeStack::Cleanup {
1771 const CXXDestructorDecl *Dtor;
1772 llvm::Value *Addr;
1773 CXXDtorType Type;
1774
1775 CallDelegatingCtorDtor(const CXXDestructorDecl *D, llvm::Value *Addr,
1776 CXXDtorType Type)
1777 : Dtor(D), Addr(Addr), Type(Type) {}
1778
Craig Topper4f12f102014-03-12 06:41:41 +00001779 void Emit(CodeGenFunction &CGF, Flags flags) override {
Alexis Hunt9d47faf2011-05-03 23:05:34 +00001780 CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false,
Douglas Gregor61535002013-01-31 05:50:40 +00001781 /*Delegating=*/true, Addr);
Alexis Hunt9d47faf2011-05-03 23:05:34 +00001782 }
1783 };
1784}
1785
Alexis Hunt61bc1732011-05-01 07:04:31 +00001786void
1787CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1788 const FunctionArgList &Args) {
1789 assert(Ctor->isDelegatingConstructor());
1790
1791 llvm::Value *ThisPtr = LoadCXXThis();
1792
Eli Friedmanc1d85b92011-12-03 00:54:26 +00001793 QualType Ty = getContext().getTagDeclType(Ctor->getParent());
Eli Friedman38cd36d2011-12-03 02:13:40 +00001794 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
John McCall31168b02011-06-15 23:02:42 +00001795 AggValueSlot AggSlot =
Eli Friedmanc1d85b92011-12-03 00:54:26 +00001796 AggValueSlot::forAddr(ThisPtr, Alignment, Qualifiers(),
John McCall8d6fc952011-08-25 20:40:09 +00001797 AggValueSlot::IsDestructed,
John McCalla5efa732011-08-25 23:04:34 +00001798 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier615ed1a2012-03-29 17:37:10 +00001799 AggValueSlot::IsNotAliased);
Alexis Hunt61bc1732011-05-01 07:04:31 +00001800
1801 EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot);
Alexis Hunt61bc1732011-05-01 07:04:31 +00001802
Alexis Hunt9d47faf2011-05-03 23:05:34 +00001803 const CXXRecordDecl *ClassDecl = Ctor->getParent();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001804 if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) {
Alexis Hunt9d47faf2011-05-03 23:05:34 +00001805 CXXDtorType Type =
1806 CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base;
1807
1808 EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup,
1809 ClassDecl->getDestructor(),
1810 ThisPtr, Type);
1811 }
1812}
Alexis Hunt61bc1732011-05-01 07:04:31 +00001813
Anders Carlsson27da15b2010-01-01 20:29:01 +00001814void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD,
1815 CXXDtorType Type,
Anders Carlssonf8a71f02010-05-02 23:29:11 +00001816 bool ForVirtualBase,
Douglas Gregor61535002013-01-31 05:50:40 +00001817 bool Delegating,
Anders Carlsson27da15b2010-01-01 20:29:01 +00001818 llvm::Value *This) {
Reid Kleckner6fe771a2013-12-13 00:53:54 +00001819 CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase,
1820 Delegating, This);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001821}
1822
John McCall53cad2e2010-07-21 01:41:18 +00001823namespace {
John McCallcda666c2010-07-21 07:22:38 +00001824 struct CallLocalDtor : EHScopeStack::Cleanup {
John McCall53cad2e2010-07-21 01:41:18 +00001825 const CXXDestructorDecl *Dtor;
1826 llvm::Value *Addr;
1827
1828 CallLocalDtor(const CXXDestructorDecl *D, llvm::Value *Addr)
1829 : Dtor(D), Addr(Addr) {}
1830
Craig Topper4f12f102014-03-12 06:41:41 +00001831 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall53cad2e2010-07-21 01:41:18 +00001832 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
Douglas Gregor61535002013-01-31 05:50:40 +00001833 /*ForVirtualBase=*/false,
1834 /*Delegating=*/false, Addr);
John McCall53cad2e2010-07-21 01:41:18 +00001835 }
1836 };
1837}
1838
John McCall8680f872010-07-21 06:29:51 +00001839void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D,
1840 llvm::Value *Addr) {
John McCallcda666c2010-07-21 07:22:38 +00001841 EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr);
John McCall8680f872010-07-21 06:29:51 +00001842}
1843
John McCallbd309292010-07-06 01:34:17 +00001844void CodeGenFunction::PushDestructorCleanup(QualType T, llvm::Value *Addr) {
1845 CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl();
1846 if (!ClassDecl) return;
1847 if (ClassDecl->hasTrivialDestructor()) return;
1848
1849 const CXXDestructorDecl *D = ClassDecl->getDestructor();
John McCalla85af562011-04-28 02:15:35 +00001850 assert(D && D->isUsed() && "destructor not marked as used!");
John McCall8680f872010-07-21 06:29:51 +00001851 PushDestructorCleanup(D, Addr);
John McCallbd309292010-07-06 01:34:17 +00001852}
1853
Anders Carlssone87fae92010-03-28 19:40:00 +00001854void
1855CodeGenFunction::InitializeVTablePointer(BaseSubobject Base,
Anders Carlsson652758c2010-04-20 05:22:15 +00001856 const CXXRecordDecl *NearestVBase,
Ken Dyck3fb4c892011-03-23 01:04:18 +00001857 CharUnits OffsetFromNearestVBase,
Anders Carlssone87fae92010-03-28 19:40:00 +00001858 const CXXRecordDecl *VTableClass) {
Anders Carlssone87fae92010-03-28 19:40:00 +00001859 // Compute the address point.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001860 bool NeedsVirtualOffset;
1861 llvm::Value *VTableAddressPoint =
1862 CGM.getCXXABI().getVTableAddressPointInStructor(
1863 *this, VTableClass, Base, NearestVBase, NeedsVirtualOffset);
1864 if (!VTableAddressPoint)
1865 return;
Anders Carlssone87fae92010-03-28 19:40:00 +00001866
Anders Carlsson6a0227d2010-04-20 16:22:16 +00001867 // Compute where to store the address point.
Craig Topper8a13c412014-05-21 05:09:00 +00001868 llvm::Value *VirtualOffset = nullptr;
Ken Dyckcfc332c2011-03-23 00:45:26 +00001869 CharUnits NonVirtualOffset = CharUnits::Zero();
Anders Carlsson91baecf2010-04-20 18:05:10 +00001870
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001871 if (NeedsVirtualOffset) {
Anders Carlsson91baecf2010-04-20 18:05:10 +00001872 // We need to use the virtual base offset offset because the virtual base
1873 // might have a different offset in the most derived class.
Reid Klecknerd8cbeec2013-05-29 18:02:47 +00001874 VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(*this,
1875 LoadCXXThis(),
1876 VTableClass,
1877 NearestVBase);
Ken Dyck3fb4c892011-03-23 01:04:18 +00001878 NonVirtualOffset = OffsetFromNearestVBase;
Anders Carlsson91baecf2010-04-20 18:05:10 +00001879 } else {
Anders Carlssonc58fb552010-05-03 00:29:58 +00001880 // We can just use the base offset in the complete class.
Ken Dyck16ffcac2011-03-24 01:21:01 +00001881 NonVirtualOffset = Base.getBaseOffset();
Anders Carlsson91baecf2010-04-20 18:05:10 +00001882 }
Anders Carlssonc58fb552010-05-03 00:29:58 +00001883
1884 // Apply the offsets.
1885 llvm::Value *VTableField = LoadCXXThis();
1886
Ken Dyckcfc332c2011-03-23 00:45:26 +00001887 if (!NonVirtualOffset.isZero() || VirtualOffset)
Anders Carlssonc58fb552010-05-03 00:29:58 +00001888 VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField,
1889 NonVirtualOffset,
1890 VirtualOffset);
Anders Carlsson6a0227d2010-04-20 16:22:16 +00001891
Anders Carlssone87fae92010-03-28 19:40:00 +00001892 // Finally, store the address point.
Chris Lattner2192fe52011-07-18 04:24:23 +00001893 llvm::Type *AddressPointPtrTy =
Anders Carlssone87fae92010-03-28 19:40:00 +00001894 VTableAddressPoint->getType()->getPointerTo();
1895 VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +00001896 llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField);
1897 CGM.DecorateInstruction(Store, CGM.getTBAAInfoForVTablePtr());
Anders Carlssone87fae92010-03-28 19:40:00 +00001898}
1899
Anders Carlssond5895932010-03-28 21:07:49 +00001900void
1901CodeGenFunction::InitializeVTablePointers(BaseSubobject Base,
Anders Carlsson652758c2010-04-20 05:22:15 +00001902 const CXXRecordDecl *NearestVBase,
Ken Dyck3fb4c892011-03-23 01:04:18 +00001903 CharUnits OffsetFromNearestVBase,
Anders Carlssond5895932010-03-28 21:07:49 +00001904 bool BaseIsNonVirtualPrimaryBase,
Anders Carlssond5895932010-03-28 21:07:49 +00001905 const CXXRecordDecl *VTableClass,
1906 VisitedVirtualBasesSetTy& VBases) {
1907 // If this base is a non-virtual primary base the address point has already
1908 // been set.
1909 if (!BaseIsNonVirtualPrimaryBase) {
1910 // Initialize the vtable pointer for this base.
Anders Carlssonc4d0d0f2010-05-03 00:07:07 +00001911 InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase,
Timur Iskhodzhanovd8fa10d2013-08-21 17:33:16 +00001912 VTableClass);
Anders Carlssond5895932010-03-28 21:07:49 +00001913 }
1914
1915 const CXXRecordDecl *RD = Base.getBase();
1916
1917 // Traverse bases.
Aaron Ballman574705e2014-03-13 15:41:46 +00001918 for (const auto &I : RD->bases()) {
Anders Carlssond5895932010-03-28 21:07:49 +00001919 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001920 = cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Anders Carlssond5895932010-03-28 21:07:49 +00001921
1922 // Ignore classes without a vtable.
1923 if (!BaseDecl->isDynamicClass())
1924 continue;
1925
Ken Dyck3fb4c892011-03-23 01:04:18 +00001926 CharUnits BaseOffset;
1927 CharUnits BaseOffsetFromNearestVBase;
Anders Carlsson948d3f42010-03-29 01:16:41 +00001928 bool BaseDeclIsNonVirtualPrimaryBase;
Anders Carlssond5895932010-03-28 21:07:49 +00001929
Aaron Ballman574705e2014-03-13 15:41:46 +00001930 if (I.isVirtual()) {
Anders Carlssond5895932010-03-28 21:07:49 +00001931 // Check if we've visited this virtual base before.
1932 if (!VBases.insert(BaseDecl))
1933 continue;
1934
1935 const ASTRecordLayout &Layout =
1936 getContext().getASTRecordLayout(VTableClass);
1937
Ken Dyck3fb4c892011-03-23 01:04:18 +00001938 BaseOffset = Layout.getVBaseClassOffset(BaseDecl);
1939 BaseOffsetFromNearestVBase = CharUnits::Zero();
Anders Carlsson948d3f42010-03-29 01:16:41 +00001940 BaseDeclIsNonVirtualPrimaryBase = false;
Anders Carlssond5895932010-03-28 21:07:49 +00001941 } else {
1942 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1943
Ken Dyck16ffcac2011-03-24 01:21:01 +00001944 BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl);
Anders Carlssonc4d0d0f2010-05-03 00:07:07 +00001945 BaseOffsetFromNearestVBase =
Ken Dyck3fb4c892011-03-23 01:04:18 +00001946 OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson948d3f42010-03-29 01:16:41 +00001947 BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl;
Anders Carlssond5895932010-03-28 21:07:49 +00001948 }
1949
Ken Dyck16ffcac2011-03-24 01:21:01 +00001950 InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset),
Aaron Ballman574705e2014-03-13 15:41:46 +00001951 I.isVirtual() ? BaseDecl : NearestVBase,
Anders Carlssonc4d0d0f2010-05-03 00:07:07 +00001952 BaseOffsetFromNearestVBase,
Anders Carlsson948d3f42010-03-29 01:16:41 +00001953 BaseDeclIsNonVirtualPrimaryBase,
Timur Iskhodzhanovd8fa10d2013-08-21 17:33:16 +00001954 VTableClass, VBases);
Anders Carlssond5895932010-03-28 21:07:49 +00001955 }
1956}
1957
1958void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) {
1959 // Ignore classes without a vtable.
Anders Carlsson1f9348c2010-03-26 04:39:42 +00001960 if (!RD->isDynamicClass())
Anders Carlsson27da15b2010-01-01 20:29:01 +00001961 return;
1962
Anders Carlssond5895932010-03-28 21:07:49 +00001963 // Initialize the vtable pointers for this class and all of its bases.
1964 VisitedVirtualBasesSetTy VBases;
Ken Dyck16ffcac2011-03-24 01:21:01 +00001965 InitializeVTablePointers(BaseSubobject(RD, CharUnits::Zero()),
Craig Topper8a13c412014-05-21 05:09:00 +00001966 /*NearestVBase=*/nullptr,
Ken Dyck3fb4c892011-03-23 01:04:18 +00001967 /*OffsetFromNearestVBase=*/CharUnits::Zero(),
Timur Iskhodzhanovd8fa10d2013-08-21 17:33:16 +00001968 /*BaseIsNonVirtualPrimaryBase=*/false, RD, VBases);
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +00001969
1970 if (RD->getNumVBases())
1971 CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, RD);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001972}
Dan Gohman8fc50c22010-10-26 18:44:08 +00001973
1974llvm::Value *CodeGenFunction::GetVTablePtr(llvm::Value *This,
Chris Lattner2192fe52011-07-18 04:24:23 +00001975 llvm::Type *Ty) {
Dan Gohman8fc50c22010-10-26 18:44:08 +00001976 llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo());
Kostya Serebryany141e46f2012-03-26 17:03:51 +00001977 llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable");
1978 CGM.DecorateInstruction(VTable, CGM.getTBAAInfoForVTablePtr());
1979 return VTable;
Dan Gohman8fc50c22010-10-26 18:44:08 +00001980}
Anders Carlssonc36783e2011-05-08 20:32:23 +00001981
Anders Carlssonc36783e2011-05-08 20:32:23 +00001982
1983// FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
1984// quite what we want.
1985static const Expr *skipNoOpCastsAndParens(const Expr *E) {
1986 while (true) {
1987 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
1988 E = PE->getSubExpr();
1989 continue;
1990 }
1991
1992 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
1993 if (CE->getCastKind() == CK_NoOp) {
1994 E = CE->getSubExpr();
1995 continue;
1996 }
1997 }
1998 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1999 if (UO->getOpcode() == UO_Extension) {
2000 E = UO->getSubExpr();
2001 continue;
2002 }
2003 }
2004 return E;
2005 }
2006}
2007
Benjamin Kramer7463ed72013-08-25 22:46:27 +00002008bool
2009CodeGenFunction::CanDevirtualizeMemberFunctionCall(const Expr *Base,
2010 const CXXMethodDecl *MD) {
2011 // When building with -fapple-kext, all calls must go through the vtable since
2012 // the kernel linker can do runtime patching of vtables.
2013 if (getLangOpts().AppleKext)
2014 return false;
2015
Anders Carlssonc36783e2011-05-08 20:32:23 +00002016 // If the most derived class is marked final, we know that no subclass can
2017 // override this member function and so we can devirtualize it. For example:
2018 //
2019 // struct A { virtual void f(); }
2020 // struct B final : A { };
2021 //
2022 // void f(B *b) {
2023 // b->f();
2024 // }
2025 //
Benjamin Kramer7463ed72013-08-25 22:46:27 +00002026 const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType();
Anders Carlssonc36783e2011-05-08 20:32:23 +00002027 if (MostDerivedClassDecl->hasAttr<FinalAttr>())
2028 return true;
2029
2030 // If the member function is marked 'final', we know that it can't be
2031 // overridden and can therefore devirtualize it.
2032 if (MD->hasAttr<FinalAttr>())
2033 return true;
2034
2035 // Similarly, if the class itself is marked 'final' it can't be overridden
2036 // and we can therefore devirtualize the member function call.
2037 if (MD->getParent()->hasAttr<FinalAttr>())
2038 return true;
2039
2040 Base = skipNoOpCastsAndParens(Base);
2041 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
2042 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
2043 // This is a record decl. We know the type and can devirtualize it.
2044 return VD->getType()->isRecordType();
2045 }
2046
2047 return false;
2048 }
Benjamin Kramer7463ed72013-08-25 22:46:27 +00002049
2050 // We can devirtualize calls on an object accessed by a class member access
2051 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2052 // a derived class object constructed in the same location.
2053 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base))
2054 if (const ValueDecl *VD = dyn_cast<ValueDecl>(ME->getMemberDecl()))
2055 return VD->getType()->isRecordType();
2056
Anders Carlssonc36783e2011-05-08 20:32:23 +00002057 // We can always devirtualize calls on temporary object expressions.
2058 if (isa<CXXConstructExpr>(Base))
2059 return true;
2060
2061 // And calls on bound temporaries.
2062 if (isa<CXXBindTemporaryExpr>(Base))
2063 return true;
2064
2065 // Check if this is a call expr that returns a record type.
2066 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
2067 return CE->getCallReturnType()->isRecordType();
2068
2069 // We can't devirtualize the call.
2070 return false;
2071}
2072
Anders Carlssonc36783e2011-05-08 20:32:23 +00002073llvm::Value *
2074CodeGenFunction::EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E,
2075 const CXXMethodDecl *MD,
2076 llvm::Value *This) {
John McCalla729c622012-02-17 03:33:10 +00002077 llvm::FunctionType *fnType =
2078 CGM.getTypes().GetFunctionType(
2079 CGM.getTypes().arrangeCXXMethodDeclaration(MD));
Anders Carlssonc36783e2011-05-08 20:32:23 +00002080
Benjamin Kramer7463ed72013-08-25 22:46:27 +00002081 if (MD->isVirtual() && !CanDevirtualizeMemberFunctionCall(E->getArg(0), MD))
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00002082 return CGM.getCXXABI().getVirtualFunctionPointer(*this, MD, This, fnType);
Anders Carlssonc36783e2011-05-08 20:32:23 +00002083
John McCalla729c622012-02-17 03:33:10 +00002084 return CGM.GetAddrOfFunction(MD, fnType);
Anders Carlssonc36783e2011-05-08 20:32:23 +00002085}
Eli Friedman5a6d5072012-02-16 01:37:33 +00002086
Faisal Vali571df122013-09-29 08:45:24 +00002087void CodeGenFunction::EmitForwardingCallToLambda(
2088 const CXXMethodDecl *callOperator,
2089 CallArgList &callArgs) {
Eli Friedman5b446882012-02-16 03:47:28 +00002090 // Get the address of the call operator.
John McCall8dda7b22012-07-07 06:41:13 +00002091 const CGFunctionInfo &calleeFnInfo =
2092 CGM.getTypes().arrangeCXXMethodDeclaration(callOperator);
2093 llvm::Value *callee =
2094 CGM.GetAddrOfFunction(GlobalDecl(callOperator),
2095 CGM.getTypes().GetFunctionType(calleeFnInfo));
Eli Friedman5b446882012-02-16 03:47:28 +00002096
John McCall8dda7b22012-07-07 06:41:13 +00002097 // Prepare the return slot.
2098 const FunctionProtoType *FPT =
2099 callOperator->getType()->castAs<FunctionProtoType>();
Alp Toker314cc812014-01-25 16:55:45 +00002100 QualType resultType = FPT->getReturnType();
John McCall8dda7b22012-07-07 06:41:13 +00002101 ReturnValueSlot returnSlot;
2102 if (!resultType->isVoidType() &&
2103 calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
John McCall47fb9502013-03-07 21:37:08 +00002104 !hasScalarEvaluationKind(calleeFnInfo.getReturnType()))
John McCall8dda7b22012-07-07 06:41:13 +00002105 returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified());
2106
2107 // We don't need to separately arrange the call arguments because
2108 // the call can't be variadic anyway --- it's impossible to forward
2109 // variadic arguments.
Eli Friedman5b446882012-02-16 03:47:28 +00002110
2111 // Now emit our call.
John McCall8dda7b22012-07-07 06:41:13 +00002112 RValue RV = EmitCall(calleeFnInfo, callee, returnSlot,
2113 callArgs, callOperator);
Eli Friedman5b446882012-02-16 03:47:28 +00002114
John McCall8dda7b22012-07-07 06:41:13 +00002115 // If necessary, copy the returned value into the slot.
2116 if (!resultType->isVoidType() && returnSlot.isNull())
2117 EmitReturnOfRValue(RV, resultType);
Eli Friedmanf5f4d2f2012-12-13 23:37:17 +00002118 else
2119 EmitBranchThroughCleanup(ReturnBlock);
Eli Friedman5b446882012-02-16 03:47:28 +00002120}
2121
Eli Friedman2495ab02012-02-25 02:48:22 +00002122void CodeGenFunction::EmitLambdaBlockInvokeBody() {
2123 const BlockDecl *BD = BlockInfo->getBlockDecl();
2124 const VarDecl *variable = BD->capture_begin()->getVariable();
2125 const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl();
2126
2127 // Start building arguments for forwarding call
2128 CallArgList CallArgs;
2129
2130 QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
2131 llvm::Value *ThisPtr = GetAddrOfBlockDecl(variable, false);
2132 CallArgs.add(RValue::get(ThisPtr), ThisType);
2133
2134 // Add the rest of the parameters.
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00002135 for (auto param : BD->params())
Nick Lewycky2d84e842013-10-02 02:29:49 +00002136 EmitDelegateCallArg(CallArgs, param, param->getLocStart());
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00002137
Faisal Vali571df122013-09-29 08:45:24 +00002138 assert(!Lambda->isGenericLambda() &&
2139 "generic lambda interconversion to block not implemented");
2140 EmitForwardingCallToLambda(Lambda->getLambdaCallOperator(), CallArgs);
Eli Friedman2495ab02012-02-25 02:48:22 +00002141}
2142
2143void CodeGenFunction::EmitLambdaToBlockPointerBody(FunctionArgList &Args) {
John McCalldec348f72013-05-03 07:33:41 +00002144 if (cast<CXXMethodDecl>(CurCodeDecl)->isVariadic()) {
Eli Friedman2495ab02012-02-25 02:48:22 +00002145 // FIXME: Making this work correctly is nasty because it requires either
2146 // cloning the body of the call operator or making the call operator forward.
John McCalldec348f72013-05-03 07:33:41 +00002147 CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function");
Eli Friedman2495ab02012-02-25 02:48:22 +00002148 return;
2149 }
2150
Richard Smithb47c36f2013-11-05 09:12:18 +00002151 EmitFunctionBody(Args, cast<FunctionDecl>(CurGD.getDecl())->getBody());
Eli Friedman2495ab02012-02-25 02:48:22 +00002152}
2153
2154void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD) {
2155 const CXXRecordDecl *Lambda = MD->getParent();
2156
2157 // Start building arguments for forwarding call
2158 CallArgList CallArgs;
2159
2160 QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
2161 llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType));
2162 CallArgs.add(RValue::get(ThisPtr), ThisType);
2163
2164 // Add the rest of the parameters.
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002165 for (auto Param : MD->params())
2166 EmitDelegateCallArg(CallArgs, Param, Param->getLocStart());
2167
Faisal Vali571df122013-09-29 08:45:24 +00002168 const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
2169 // For a generic lambda, find the corresponding call operator specialization
2170 // to which the call to the static-invoker shall be forwarded.
2171 if (Lambda->isGenericLambda()) {
2172 assert(MD->isFunctionTemplateSpecialization());
2173 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
2174 FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate();
Craig Topper8a13c412014-05-21 05:09:00 +00002175 void *InsertPos = nullptr;
Faisal Vali571df122013-09-29 08:45:24 +00002176 FunctionDecl *CorrespondingCallOpSpecialization =
Craig Topper7e0daca2014-06-26 04:58:53 +00002177 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
Faisal Vali571df122013-09-29 08:45:24 +00002178 assert(CorrespondingCallOpSpecialization);
2179 CallOp = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization);
2180 }
2181 EmitForwardingCallToLambda(CallOp, CallArgs);
Eli Friedman2495ab02012-02-25 02:48:22 +00002182}
2183
Douglas Gregor355efbb2012-02-17 03:02:34 +00002184void CodeGenFunction::EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD) {
2185 if (MD->isVariadic()) {
Eli Friedman5b446882012-02-16 03:47:28 +00002186 // FIXME: Making this work correctly is nasty because it requires either
2187 // cloning the body of the call operator or making the call operator forward.
2188 CGM.ErrorUnsupported(MD, "lambda conversion to variadic function");
Eli Friedman2495ab02012-02-25 02:48:22 +00002189 return;
Eli Friedman5b446882012-02-16 03:47:28 +00002190 }
2191
Douglas Gregor355efbb2012-02-17 03:02:34 +00002192 EmitLambdaDelegatingInvokeBody(MD);
Eli Friedman5a6d5072012-02-16 01:37:33 +00002193}