blob: 7276440a47a81c3dde1868b2fcd1aed14777dd39 [file] [log] [blame]
Anders Carlsson5b955922009-11-24 05:51:11 +00001//===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===//
Anders Carlsson16d81b82009-09-22 22:53:17 +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 code generation of C++ expressions
11//
12//===----------------------------------------------------------------------===//
13
Devang Patelc69e1cf2010-09-30 19:05:55 +000014#include "clang/Frontend/CodeGenOptions.h"
Anders Carlsson16d81b82009-09-22 22:53:17 +000015#include "CodeGenFunction.h"
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +000016#include "CGCUDARuntime.h"
John McCall4c40d982010-08-31 07:33:07 +000017#include "CGCXXABI.h"
Fariborz Jahanian842ddd02010-05-20 21:38:57 +000018#include "CGObjCRuntime.h"
Devang Patelc69e1cf2010-09-30 19:05:55 +000019#include "CGDebugInfo.h"
Chris Lattner6c552c12010-07-20 20:19:24 +000020#include "llvm/Intrinsics.h"
Anders Carlssonad3692bb2011-04-13 02:35:36 +000021#include "llvm/Support/CallSite.h"
22
Anders Carlsson16d81b82009-09-22 22:53:17 +000023using namespace clang;
24using namespace CodeGen;
25
Anders Carlsson3b5ad222010-01-01 20:29:01 +000026RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
27 llvm::Value *Callee,
28 ReturnValueSlot ReturnValue,
29 llvm::Value *This,
Anders Carlssonc997d422010-01-02 01:01:18 +000030 llvm::Value *VTT,
Anders Carlsson3b5ad222010-01-01 20:29:01 +000031 CallExpr::const_arg_iterator ArgBeg,
32 CallExpr::const_arg_iterator ArgEnd) {
33 assert(MD->isInstance() &&
34 "Trying to emit a member call expr on a static method!");
35
Richard Smith2c9f87c2012-08-24 00:54:33 +000036 // C++11 [class.mfct.non-static]p2:
37 // If a non-static member function of a class X is called for an object that
38 // is not of type X, or of a type derived from X, the behavior is undefined.
Richard Smith7ac9ef12012-09-08 02:08:36 +000039 EmitTypeCheck(TCK_MemberCall, This,
40 getContext().getRecordType(MD->getParent()));
Richard Smith2c9f87c2012-08-24 00:54:33 +000041
Anders Carlsson3b5ad222010-01-01 20:29:01 +000042 CallArgList Args;
43
44 // Push the this ptr.
Eli Friedman04c9a492011-05-02 17:57:46 +000045 Args.add(RValue::get(This), MD->getThisType(getContext()));
Anders Carlsson3b5ad222010-01-01 20:29:01 +000046
Anders Carlssonc997d422010-01-02 01:01:18 +000047 // If there is a VTT parameter, emit it.
48 if (VTT) {
49 QualType T = getContext().getPointerType(getContext().VoidPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +000050 Args.add(RValue::get(VTT), T);
Anders Carlssonc997d422010-01-02 01:01:18 +000051 }
John McCallde5d3c72012-02-17 03:33:10 +000052
53 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
54 RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size());
Anders Carlssonc997d422010-01-02 01:01:18 +000055
John McCallde5d3c72012-02-17 03:33:10 +000056 // And the rest of the call args.
Anders Carlsson3b5ad222010-01-01 20:29:01 +000057 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
58
John McCall0f3d0972012-07-07 06:41:13 +000059 return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required),
Rafael Espindola264ba482010-03-30 20:24:48 +000060 Callee, ReturnValue, Args, MD);
Anders Carlsson3b5ad222010-01-01 20:29:01 +000061}
62
Anders Carlssoncd0b32e2011-04-10 18:20:53 +000063// FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
64// quite what we want.
65static const Expr *skipNoOpCastsAndParens(const Expr *E) {
66 while (true) {
67 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
68 E = PE->getSubExpr();
69 continue;
70 }
71
72 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
73 if (CE->getCastKind() == CK_NoOp) {
74 E = CE->getSubExpr();
75 continue;
76 }
77 }
78 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
79 if (UO->getOpcode() == UO_Extension) {
80 E = UO->getSubExpr();
81 continue;
82 }
83 }
84 return E;
85 }
86}
87
Anders Carlsson3b5ad222010-01-01 20:29:01 +000088/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
89/// expr can be devirtualized.
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +000090static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context,
91 const Expr *Base,
Anders Carlssonbd2bfae2010-10-27 13:28:46 +000092 const CXXMethodDecl *MD) {
93
Anders Carlsson1679f5a2011-01-29 03:52:01 +000094 // When building with -fapple-kext, all calls must go through the vtable since
95 // the kernel linker can do runtime patching of vtables.
David Blaikie4e4d0842012-03-11 07:00:24 +000096 if (Context.getLangOpts().AppleKext)
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +000097 return false;
98
Anders Carlsson1679f5a2011-01-29 03:52:01 +000099 // If the most derived class is marked final, we know that no subclass can
100 // override this member function and so we can devirtualize it. For example:
101 //
102 // struct A { virtual void f(); }
103 // struct B final : A { };
104 //
105 // void f(B *b) {
106 // b->f();
107 // }
108 //
Rafael Espindola8d852e32012-06-27 18:18:05 +0000109 const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType();
Anders Carlsson1679f5a2011-01-29 03:52:01 +0000110 if (MostDerivedClassDecl->hasAttr<FinalAttr>())
111 return true;
112
Anders Carlssonf89e0422011-01-23 21:07:30 +0000113 // If the member function is marked 'final', we know that it can't be
Anders Carlssond66f4282010-10-27 13:34:43 +0000114 // overridden and can therefore devirtualize it.
Anders Carlssoncb88a1f2011-01-24 16:26:15 +0000115 if (MD->hasAttr<FinalAttr>())
Anders Carlssonbd2bfae2010-10-27 13:28:46 +0000116 return true;
Anders Carlssond66f4282010-10-27 13:34:43 +0000117
Anders Carlssonf89e0422011-01-23 21:07:30 +0000118 // Similarly, if the class itself is marked 'final' it can't be overridden
119 // and we can therefore devirtualize the member function call.
Anders Carlssoncb88a1f2011-01-24 16:26:15 +0000120 if (MD->getParent()->hasAttr<FinalAttr>())
Anders Carlssond66f4282010-10-27 13:34:43 +0000121 return true;
122
Anders Carlssoncd0b32e2011-04-10 18:20:53 +0000123 Base = skipNoOpCastsAndParens(Base);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000124 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
125 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
126 // This is a record decl. We know the type and can devirtualize it.
127 return VD->getType()->isRecordType();
128 }
129
130 return false;
131 }
Richard Smithac452932012-08-15 22:59:28 +0000132
133 // We can devirtualize calls on an object accessed by a class member access
134 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
135 // a derived class object constructed in the same location.
136 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base))
137 if (const ValueDecl *VD = dyn_cast<ValueDecl>(ME->getMemberDecl()))
138 return VD->getType()->isRecordType();
139
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000140 // We can always devirtualize calls on temporary object expressions.
Eli Friedman6997aae2010-01-31 20:58:15 +0000141 if (isa<CXXConstructExpr>(Base))
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000142 return true;
143
144 // And calls on bound temporaries.
145 if (isa<CXXBindTemporaryExpr>(Base))
146 return true;
147
148 // Check if this is a call expr that returns a record type.
149 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
150 return CE->getCallReturnType()->isRecordType();
Anders Carlssonbd2bfae2010-10-27 13:28:46 +0000151
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000152 // We can't devirtualize the call.
153 return false;
154}
155
Rafael Espindolaea01d762012-06-28 14:28:57 +0000156static CXXRecordDecl *getCXXRecord(const Expr *E) {
157 QualType T = E->getType();
158 if (const PointerType *PTy = T->getAs<PointerType>())
159 T = PTy->getPointeeType();
160 const RecordType *Ty = T->castAs<RecordType>();
161 return cast<CXXRecordDecl>(Ty->getDecl());
162}
163
Francois Pichetdbee3412011-01-18 05:04:39 +0000164// Note: This function also emit constructor calls to support a MSVC
165// extensions allowing explicit constructor function call.
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000166RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
167 ReturnValueSlot ReturnValue) {
John McCall379b5152011-04-11 07:02:50 +0000168 const Expr *callee = CE->getCallee()->IgnoreParens();
169
170 if (isa<BinaryOperator>(callee))
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000171 return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
John McCall379b5152011-04-11 07:02:50 +0000172
173 const MemberExpr *ME = cast<MemberExpr>(callee);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000174 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
175
Devang Patelc69e1cf2010-09-30 19:05:55 +0000176 CGDebugInfo *DI = getDebugInfo();
Alexey Samsonov3a70cd62012-04-27 07:24:20 +0000177 if (DI && CGM.getCodeGenOpts().DebugInfo == CodeGenOptions::LimitedDebugInfo
Devang Patel68020272010-10-22 18:56:27 +0000178 && !isa<CallExpr>(ME->getBase())) {
Devang Patelc69e1cf2010-09-30 19:05:55 +0000179 QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType();
180 if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) {
181 DI->getOrCreateRecordType(PTy->getPointeeType(),
182 MD->getParent()->getLocation());
183 }
184 }
185
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000186 if (MD->isStatic()) {
187 // The method is static, emit it as we would a regular call.
188 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
189 return EmitCall(getContext().getPointerType(MD->getType()), Callee,
190 ReturnValue, CE->arg_begin(), CE->arg_end());
191 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000192
John McCallfc400282010-09-03 01:26:39 +0000193 // Compute the object pointer.
Rafael Espindola632fbaa2012-06-28 01:56:38 +0000194 const Expr *Base = ME->getBase();
195 bool CanUseVirtualCall = MD->isVirtual() && !ME->hasQualifier();
Rafael Espindola632fbaa2012-06-28 01:56:38 +0000196
Rafael Espindolaea01d762012-06-28 14:28:57 +0000197 const CXXMethodDecl *DevirtualizedMethod = NULL;
198 if (CanUseVirtualCall &&
199 canDevirtualizeMemberFunctionCalls(getContext(), Base, MD)) {
200 const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
201 DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl);
202 assert(DevirtualizedMethod);
203 const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent();
204 const Expr *Inner = Base->ignoreParenBaseCasts();
205 if (getCXXRecord(Inner) == DevirtualizedClass)
206 // If the class of the Inner expression is where the dynamic method
207 // is defined, build the this pointer from it.
208 Base = Inner;
209 else if (getCXXRecord(Base) != DevirtualizedClass) {
210 // If the method is defined in a class that is not the best dynamic
211 // one or the one of the full expression, we would have to build
212 // a derived-to-base cast to compute the correct this pointer, but
213 // we don't have support for that yet, so do a virtual call.
214 DevirtualizedMethod = NULL;
215 }
Rafael Espindola80bc96e2012-06-28 17:57:36 +0000216 // If the return types are not the same, this might be a case where more
217 // code needs to run to compensate for it. For example, the derived
218 // method might return a type that inherits form from the return
219 // type of MD and has a prefix.
220 // For now we just avoid devirtualizing these covariant cases.
221 if (DevirtualizedMethod &&
222 DevirtualizedMethod->getResultType().getCanonicalType() !=
223 MD->getResultType().getCanonicalType())
Rafael Espindola4a889e42012-06-28 15:11:39 +0000224 DevirtualizedMethod = NULL;
Rafael Espindolaea01d762012-06-28 14:28:57 +0000225 }
Rafael Espindola632fbaa2012-06-28 01:56:38 +0000226
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000227 llvm::Value *This;
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000228 if (ME->isArrow())
Rafael Espindolaea01d762012-06-28 14:28:57 +0000229 This = EmitScalarExpr(Base);
John McCall0e800c92010-12-04 08:14:53 +0000230 else
Rafael Espindolaea01d762012-06-28 14:28:57 +0000231 This = EmitLValue(Base).getAddress();
Rafael Espindola632fbaa2012-06-28 01:56:38 +0000232
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000233
John McCallfc400282010-09-03 01:26:39 +0000234 if (MD->isTrivial()) {
235 if (isa<CXXDestructorDecl>(MD)) return RValue::get(0);
Francois Pichetdbee3412011-01-18 05:04:39 +0000236 if (isa<CXXConstructorDecl>(MD) &&
237 cast<CXXConstructorDecl>(MD)->isDefaultConstructor())
238 return RValue::get(0);
John McCallfc400282010-09-03 01:26:39 +0000239
Sebastian Redl85ea7aa2011-08-30 19:58:05 +0000240 if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) {
241 // We don't like to generate the trivial copy/move assignment operator
242 // when it isn't necessary; just produce the proper effect here.
Francois Pichetdbee3412011-01-18 05:04:39 +0000243 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
244 EmitAggregateCopy(This, RHS, CE->getType());
245 return RValue::get(This);
246 }
247
248 if (isa<CXXConstructorDecl>(MD) &&
Sebastian Redl85ea7aa2011-08-30 19:58:05 +0000249 cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) {
250 // Trivial move and copy ctor are the same.
Francois Pichetdbee3412011-01-18 05:04:39 +0000251 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
252 EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS,
253 CE->arg_begin(), CE->arg_end());
254 return RValue::get(This);
255 }
256 llvm_unreachable("unknown trivial member function");
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000257 }
258
John McCallfc400282010-09-03 01:26:39 +0000259 // Compute the function type we're calling.
Francois Pichetdbee3412011-01-18 05:04:39 +0000260 const CGFunctionInfo *FInfo = 0;
261 if (isa<CXXDestructorDecl>(MD))
John McCallde5d3c72012-02-17 03:33:10 +0000262 FInfo = &CGM.getTypes().arrangeCXXDestructor(cast<CXXDestructorDecl>(MD),
263 Dtor_Complete);
Francois Pichetdbee3412011-01-18 05:04:39 +0000264 else if (isa<CXXConstructorDecl>(MD))
John McCallde5d3c72012-02-17 03:33:10 +0000265 FInfo = &CGM.getTypes().arrangeCXXConstructorDeclaration(
266 cast<CXXConstructorDecl>(MD),
267 Ctor_Complete);
Francois Pichetdbee3412011-01-18 05:04:39 +0000268 else
John McCallde5d3c72012-02-17 03:33:10 +0000269 FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(MD);
John McCallfc400282010-09-03 01:26:39 +0000270
John McCallde5d3c72012-02-17 03:33:10 +0000271 llvm::Type *Ty = CGM.getTypes().GetFunctionType(*FInfo);
John McCallfc400282010-09-03 01:26:39 +0000272
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000273 // C++ [class.virtual]p12:
274 // Explicit qualification with the scope operator (5.1) suppresses the
275 // virtual call mechanism.
276 //
277 // We also don't emit a virtual call if the base expression has a record type
278 // because then we know what the type is.
Rafael Espindolaea01d762012-06-28 14:28:57 +0000279 bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod;
Rafael Espindola0b4fe502012-06-26 17:45:31 +0000280
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000281 llvm::Value *Callee;
John McCallfc400282010-09-03 01:26:39 +0000282 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
283 if (UseVirtualCall) {
284 Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000285 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +0000286 if (getContext().getLangOpts().AppleKext &&
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000287 MD->isVirtual() &&
288 ME->hasQualifier())
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000289 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
Rafael Espindolaea01d762012-06-28 14:28:57 +0000290 else if (!DevirtualizedMethod)
Rafael Espindola12582bd2012-06-26 19:18:25 +0000291 Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty);
Rafael Espindola0b4fe502012-06-26 17:45:31 +0000292 else {
Rafael Espindolaea01d762012-06-28 14:28:57 +0000293 const CXXDestructorDecl *DDtor =
294 cast<CXXDestructorDecl>(DevirtualizedMethod);
Rafael Espindola0b4fe502012-06-26 17:45:31 +0000295 Callee = CGM.GetAddrOfFunction(GlobalDecl(DDtor, Dtor_Complete), Ty);
296 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000297 }
Francois Pichetdbee3412011-01-18 05:04:39 +0000298 } else if (const CXXConstructorDecl *Ctor =
299 dyn_cast<CXXConstructorDecl>(MD)) {
300 Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty);
John McCallfc400282010-09-03 01:26:39 +0000301 } else if (UseVirtualCall) {
Fariborz Jahanian27262672011-01-20 17:19:02 +0000302 Callee = BuildVirtualCall(MD, This, Ty);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000303 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +0000304 if (getContext().getLangOpts().AppleKext &&
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +0000305 MD->isVirtual() &&
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +0000306 ME->hasQualifier())
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000307 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
Rafael Espindolaea01d762012-06-28 14:28:57 +0000308 else if (!DevirtualizedMethod)
Rafael Espindola12582bd2012-06-26 19:18:25 +0000309 Callee = CGM.GetAddrOfFunction(MD, Ty);
Rafael Espindola0b4fe502012-06-26 17:45:31 +0000310 else {
Rafael Espindolaea01d762012-06-28 14:28:57 +0000311 Callee = CGM.GetAddrOfFunction(DevirtualizedMethod, Ty);
Rafael Espindola0b4fe502012-06-26 17:45:31 +0000312 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000313 }
314
Anders Carlssonc997d422010-01-02 01:01:18 +0000315 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000316 CE->arg_begin(), CE->arg_end());
317}
318
319RValue
320CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
321 ReturnValueSlot ReturnValue) {
322 const BinaryOperator *BO =
323 cast<BinaryOperator>(E->getCallee()->IgnoreParens());
324 const Expr *BaseExpr = BO->getLHS();
325 const Expr *MemFnExpr = BO->getRHS();
326
327 const MemberPointerType *MPT =
John McCall864c0412011-04-26 20:42:42 +0000328 MemFnExpr->getType()->castAs<MemberPointerType>();
John McCall93d557b2010-08-22 00:05:51 +0000329
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000330 const FunctionProtoType *FPT =
John McCall864c0412011-04-26 20:42:42 +0000331 MPT->getPointeeType()->castAs<FunctionProtoType>();
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000332 const CXXRecordDecl *RD =
333 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
334
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000335 // Get the member function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000336 llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000337
338 // Emit the 'this' pointer.
339 llvm::Value *This;
340
John McCall2de56d12010-08-25 11:45:40 +0000341 if (BO->getOpcode() == BO_PtrMemI)
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000342 This = EmitScalarExpr(BaseExpr);
343 else
344 This = EmitLValue(BaseExpr).getAddress();
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000345
Richard Smith7ac9ef12012-09-08 02:08:36 +0000346 EmitTypeCheck(TCK_MemberCall, This, QualType(MPT->getClass(), 0));
Richard Smith2c9f87c2012-08-24 00:54:33 +0000347
John McCall93d557b2010-08-22 00:05:51 +0000348 // Ask the ABI to load the callee. Note that This is modified.
349 llvm::Value *Callee =
John McCalld16c2cf2011-02-08 08:22:06 +0000350 CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000351
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000352 CallArgList Args;
353
354 QualType ThisType =
355 getContext().getPointerType(getContext().getTagDeclType(RD));
356
357 // Push the this ptr.
Eli Friedman04c9a492011-05-02 17:57:46 +0000358 Args.add(RValue::get(This), ThisType);
John McCall0f3d0972012-07-07 06:41:13 +0000359
360 RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000361
362 // And the rest of the call args
363 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
John McCall0f3d0972012-07-07 06:41:13 +0000364 return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required), Callee,
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000365 ReturnValue, Args);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000366}
367
368RValue
369CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
370 const CXXMethodDecl *MD,
371 ReturnValueSlot ReturnValue) {
372 assert(MD->isInstance() &&
373 "Trying to emit a member call expr on a static method!");
John McCall0e800c92010-12-04 08:14:53 +0000374 LValue LV = EmitLValue(E->getArg(0));
375 llvm::Value *This = LV.getAddress();
376
Douglas Gregorb2b56582011-09-06 16:26:56 +0000377 if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) &&
378 MD->isTrivial()) {
379 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
380 QualType Ty = E->getType();
381 EmitAggregateCopy(This, Src, Ty);
382 return RValue::get(This);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000383 }
384
Anders Carlssona2447e02011-05-08 20:32:23 +0000385 llvm::Value *Callee = EmitCXXOperatorMemberCallee(E, MD, This);
Anders Carlssonc997d422010-01-02 01:01:18 +0000386 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000387 E->arg_begin() + 1, E->arg_end());
388}
389
Peter Collingbourne6c0aa5f2011-10-06 18:29:37 +0000390RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
391 ReturnValueSlot ReturnValue) {
392 return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue);
393}
394
Eli Friedman2ed7cb62011-10-14 02:27:24 +0000395static void EmitNullBaseClassInitialization(CodeGenFunction &CGF,
396 llvm::Value *DestPtr,
397 const CXXRecordDecl *Base) {
398 if (Base->isEmpty())
399 return;
400
401 DestPtr = CGF.EmitCastToVoidPtr(DestPtr);
402
403 const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base);
404 CharUnits Size = Layout.getNonVirtualSize();
405 CharUnits Align = Layout.getNonVirtualAlign();
406
407 llvm::Value *SizeVal = CGF.CGM.getSize(Size);
408
409 // If the type contains a pointer to data member we can't memset it to zero.
410 // Instead, create a null constant and copy it to the destination.
411 // TODO: there are other patterns besides zero that we can usefully memset,
412 // like -1, which happens to be the pattern used by member-pointers.
413 // TODO: isZeroInitializable can be over-conservative in the case where a
414 // virtual base contains a member pointer.
415 if (!CGF.CGM.getTypes().isZeroInitializable(Base)) {
416 llvm::Constant *NullConstant = CGF.CGM.EmitNullConstantForBase(Base);
417
418 llvm::GlobalVariable *NullVariable =
419 new llvm::GlobalVariable(CGF.CGM.getModule(), NullConstant->getType(),
420 /*isConstant=*/true,
421 llvm::GlobalVariable::PrivateLinkage,
422 NullConstant, Twine());
423 NullVariable->setAlignment(Align.getQuantity());
424 llvm::Value *SrcPtr = CGF.EmitCastToVoidPtr(NullVariable);
425
426 // Get and call the appropriate llvm.memcpy overload.
427 CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity());
428 return;
429 }
430
431 // Otherwise, just memset the whole thing to zero. This is legal
432 // because in LLVM, all default initializers (other than the ones we just
433 // handled above) are guaranteed to have a bit pattern of all zeros.
434 CGF.Builder.CreateMemSet(DestPtr, CGF.Builder.getInt8(0), SizeVal,
435 Align.getQuantity());
436}
437
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000438void
John McCall558d2ab2010-09-15 10:14:12 +0000439CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
440 AggValueSlot Dest) {
441 assert(!Dest.isIgnored() && "Must have a destination!");
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000442 const CXXConstructorDecl *CD = E->getConstructor();
Douglas Gregor759e41b2010-08-22 16:15:35 +0000443
444 // If we require zero initialization before (or instead of) calling the
445 // constructor, as can be the case with a non-user-provided default
Argyrios Kyrtzidis657baf12011-04-28 22:57:55 +0000446 // constructor, emit the zero initialization now, unless destination is
447 // already zeroed.
Eli Friedman2ed7cb62011-10-14 02:27:24 +0000448 if (E->requiresZeroInitialization() && !Dest.isZeroed()) {
449 switch (E->getConstructionKind()) {
450 case CXXConstructExpr::CK_Delegating:
Eli Friedman2ed7cb62011-10-14 02:27:24 +0000451 case CXXConstructExpr::CK_Complete:
452 EmitNullInitialization(Dest.getAddr(), E->getType());
453 break;
454 case CXXConstructExpr::CK_VirtualBase:
455 case CXXConstructExpr::CK_NonVirtualBase:
456 EmitNullBaseClassInitialization(*this, Dest.getAddr(), CD->getParent());
457 break;
458 }
459 }
Douglas Gregor759e41b2010-08-22 16:15:35 +0000460
461 // If this is a call to a trivial default constructor, do nothing.
462 if (CD->isTrivial() && CD->isDefaultConstructor())
463 return;
464
John McCallfc1e6c72010-09-18 00:58:34 +0000465 // Elide the constructor if we're constructing from a temporary.
466 // The temporary check is required because Sema sets this on NRVO
467 // returns.
David Blaikie4e4d0842012-03-11 07:00:24 +0000468 if (getContext().getLangOpts().ElideConstructors && E->isElidable()) {
John McCallfc1e6c72010-09-18 00:58:34 +0000469 assert(getContext().hasSameUnqualifiedType(E->getType(),
470 E->getArg(0)->getType()));
John McCall558d2ab2010-09-15 10:14:12 +0000471 if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
472 EmitAggExpr(E->getArg(0), Dest);
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000473 return;
474 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000475 }
Douglas Gregor759e41b2010-08-22 16:15:35 +0000476
John McCallc3c07662011-07-13 06:10:41 +0000477 if (const ConstantArrayType *arrayType
478 = getContext().getAsConstantArrayType(E->getType())) {
479 EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddr(),
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000480 E->arg_begin(), E->arg_end());
John McCallc3c07662011-07-13 06:10:41 +0000481 } else {
Cameron Esfahani6bd2f6a2011-05-06 21:28:42 +0000482 CXXCtorType Type = Ctor_Complete;
Sean Huntd49bd552011-05-03 20:19:28 +0000483 bool ForVirtualBase = false;
484
485 switch (E->getConstructionKind()) {
486 case CXXConstructExpr::CK_Delegating:
Sean Hunt059ce0d2011-05-01 07:04:31 +0000487 // We should be emitting a constructor; GlobalDecl will assert this
488 Type = CurGD.getCtorType();
Sean Huntd49bd552011-05-03 20:19:28 +0000489 break;
Sean Hunt059ce0d2011-05-01 07:04:31 +0000490
Sean Huntd49bd552011-05-03 20:19:28 +0000491 case CXXConstructExpr::CK_Complete:
492 Type = Ctor_Complete;
493 break;
494
495 case CXXConstructExpr::CK_VirtualBase:
496 ForVirtualBase = true;
497 // fall-through
498
499 case CXXConstructExpr::CK_NonVirtualBase:
500 Type = Ctor_Base;
501 }
Anders Carlsson155ed4a2010-05-02 23:20:53 +0000502
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000503 // Call the constructor.
John McCall558d2ab2010-09-15 10:14:12 +0000504 EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(),
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000505 E->arg_begin(), E->arg_end());
Anders Carlsson155ed4a2010-05-02 23:20:53 +0000506 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000507}
508
Fariborz Jahanian34999872010-11-13 21:53:34 +0000509void
510CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest,
511 llvm::Value *Src,
Fariborz Jahanian830937b2010-12-02 17:02:11 +0000512 const Expr *Exp) {
John McCall4765fa02010-12-06 08:20:24 +0000513 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp))
Fariborz Jahanian34999872010-11-13 21:53:34 +0000514 Exp = E->getSubExpr();
515 assert(isa<CXXConstructExpr>(Exp) &&
516 "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr");
517 const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp);
518 const CXXConstructorDecl *CD = E->getConstructor();
519 RunCleanupsScope Scope(*this);
520
521 // If we require zero initialization before (or instead of) calling the
522 // constructor, as can be the case with a non-user-provided default
523 // constructor, emit the zero initialization now.
524 // FIXME. Do I still need this for a copy ctor synthesis?
525 if (E->requiresZeroInitialization())
526 EmitNullInitialization(Dest, E->getType());
527
Chandler Carruth858a5462010-11-15 13:54:43 +0000528 assert(!getContext().getAsConstantArrayType(E->getType())
529 && "EmitSynthesizedCXXCopyCtor - Copied-in Array");
Fariborz Jahanian34999872010-11-13 21:53:34 +0000530 EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src,
531 E->arg_begin(), E->arg_end());
532}
533
John McCall1e7fe752010-09-02 09:58:18 +0000534static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
535 const CXXNewExpr *E) {
Anders Carlsson871d0782009-12-13 20:04:38 +0000536 if (!E->isArray())
Ken Dyckcaf647c2010-01-26 19:44:24 +0000537 return CharUnits::Zero();
Anders Carlsson871d0782009-12-13 20:04:38 +0000538
John McCallb1c98a32011-05-16 01:05:12 +0000539 // No cookie is required if the operator new[] being used is the
540 // reserved placement operator new[].
541 if (E->getOperatorNew()->isReservedGlobalPlacementOperator())
John McCall5172ed92010-08-23 01:17:59 +0000542 return CharUnits::Zero();
543
John McCall6ec278d2011-01-27 09:37:56 +0000544 return CGF.CGM.getCXXABI().GetArrayCookieSize(E);
Anders Carlssona4d4c012009-09-23 16:07:23 +0000545}
546
John McCall7d166272011-05-15 07:14:44 +0000547static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF,
548 const CXXNewExpr *e,
Sebastian Redl92036472012-02-22 17:37:52 +0000549 unsigned minElements,
John McCall7d166272011-05-15 07:14:44 +0000550 llvm::Value *&numElements,
551 llvm::Value *&sizeWithoutCookie) {
552 QualType type = e->getAllocatedType();
John McCall1e7fe752010-09-02 09:58:18 +0000553
John McCall7d166272011-05-15 07:14:44 +0000554 if (!e->isArray()) {
555 CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
556 sizeWithoutCookie
557 = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity());
558 return sizeWithoutCookie;
Douglas Gregor59174c02010-07-21 01:10:17 +0000559 }
Anders Carlssona4d4c012009-09-23 16:07:23 +0000560
John McCall7d166272011-05-15 07:14:44 +0000561 // The width of size_t.
562 unsigned sizeWidth = CGF.SizeTy->getBitWidth();
563
John McCall1e7fe752010-09-02 09:58:18 +0000564 // Figure out the cookie size.
John McCall7d166272011-05-15 07:14:44 +0000565 llvm::APInt cookieSize(sizeWidth,
566 CalculateCookiePadding(CGF, e).getQuantity());
John McCall1e7fe752010-09-02 09:58:18 +0000567
Anders Carlssona4d4c012009-09-23 16:07:23 +0000568 // Emit the array size expression.
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000569 // We multiply the size of all dimensions for NumElements.
570 // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
John McCall7d166272011-05-15 07:14:44 +0000571 numElements = CGF.EmitScalarExpr(e->getArraySize());
572 assert(isa<llvm::IntegerType>(numElements->getType()));
John McCall1e7fe752010-09-02 09:58:18 +0000573
John McCall7d166272011-05-15 07:14:44 +0000574 // The number of elements can be have an arbitrary integer type;
575 // essentially, we need to multiply it by a constant factor, add a
576 // cookie size, and verify that the result is representable as a
577 // size_t. That's just a gloss, though, and it's wrong in one
578 // important way: if the count is negative, it's an error even if
579 // the cookie size would bring the total size >= 0.
Douglas Gregor575a1c92011-05-20 16:38:50 +0000580 bool isSigned
581 = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType();
Chris Lattner2acc6e32011-07-18 04:24:23 +0000582 llvm::IntegerType *numElementsType
John McCall7d166272011-05-15 07:14:44 +0000583 = cast<llvm::IntegerType>(numElements->getType());
584 unsigned numElementsWidth = numElementsType->getBitWidth();
585
586 // Compute the constant factor.
587 llvm::APInt arraySizeMultiplier(sizeWidth, 1);
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000588 while (const ConstantArrayType *CAT
John McCall7d166272011-05-15 07:14:44 +0000589 = CGF.getContext().getAsConstantArrayType(type)) {
590 type = CAT->getElementType();
591 arraySizeMultiplier *= CAT->getSize();
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000592 }
593
John McCall7d166272011-05-15 07:14:44 +0000594 CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
595 llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity());
596 typeSizeMultiplier *= arraySizeMultiplier;
597
598 // This will be a size_t.
599 llvm::Value *size;
Chris Lattner83252dc2010-07-20 21:07:09 +0000600
Chris Lattner806941e2010-07-20 21:55:52 +0000601 // If someone is doing 'new int[42]' there is no need to do a dynamic check.
602 // Don't bloat the -O0 code.
John McCall7d166272011-05-15 07:14:44 +0000603 if (llvm::ConstantInt *numElementsC =
604 dyn_cast<llvm::ConstantInt>(numElements)) {
605 const llvm::APInt &count = numElementsC->getValue();
John McCall1e7fe752010-09-02 09:58:18 +0000606
John McCall7d166272011-05-15 07:14:44 +0000607 bool hasAnyOverflow = false;
John McCall1e7fe752010-09-02 09:58:18 +0000608
John McCall7d166272011-05-15 07:14:44 +0000609 // If 'count' was a negative number, it's an overflow.
610 if (isSigned && count.isNegative())
611 hasAnyOverflow = true;
John McCall1e7fe752010-09-02 09:58:18 +0000612
John McCall7d166272011-05-15 07:14:44 +0000613 // We want to do all this arithmetic in size_t. If numElements is
614 // wider than that, check whether it's already too big, and if so,
615 // overflow.
616 else if (numElementsWidth > sizeWidth &&
617 numElementsWidth - sizeWidth > count.countLeadingZeros())
618 hasAnyOverflow = true;
619
620 // Okay, compute a count at the right width.
621 llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth);
622
Sebastian Redl92036472012-02-22 17:37:52 +0000623 // If there is a brace-initializer, we cannot allocate fewer elements than
624 // there are initializers. If we do, that's treated like an overflow.
625 if (adjustedCount.ult(minElements))
626 hasAnyOverflow = true;
627
John McCall7d166272011-05-15 07:14:44 +0000628 // Scale numElements by that. This might overflow, but we don't
629 // care because it only overflows if allocationSize does, too, and
630 // if that overflows then we shouldn't use this.
631 numElements = llvm::ConstantInt::get(CGF.SizeTy,
632 adjustedCount * arraySizeMultiplier);
633
634 // Compute the size before cookie, and track whether it overflowed.
635 bool overflow;
636 llvm::APInt allocationSize
637 = adjustedCount.umul_ov(typeSizeMultiplier, overflow);
638 hasAnyOverflow |= overflow;
639
640 // Add in the cookie, and check whether it's overflowed.
641 if (cookieSize != 0) {
642 // Save the current size without a cookie. This shouldn't be
643 // used if there was overflow.
644 sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
645
646 allocationSize = allocationSize.uadd_ov(cookieSize, overflow);
647 hasAnyOverflow |= overflow;
648 }
649
650 // On overflow, produce a -1 so operator new will fail.
651 if (hasAnyOverflow) {
652 size = llvm::Constant::getAllOnesValue(CGF.SizeTy);
653 } else {
654 size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
655 }
656
657 // Otherwise, we might need to use the overflow intrinsics.
658 } else {
Sebastian Redl92036472012-02-22 17:37:52 +0000659 // There are up to five conditions we need to test for:
John McCall7d166272011-05-15 07:14:44 +0000660 // 1) if isSigned, we need to check whether numElements is negative;
661 // 2) if numElementsWidth > sizeWidth, we need to check whether
662 // numElements is larger than something representable in size_t;
Sebastian Redl92036472012-02-22 17:37:52 +0000663 // 3) if minElements > 0, we need to check whether numElements is smaller
664 // than that.
665 // 4) we need to compute
John McCall7d166272011-05-15 07:14:44 +0000666 // sizeWithoutCookie := numElements * typeSizeMultiplier
667 // and check whether it overflows; and
Sebastian Redl92036472012-02-22 17:37:52 +0000668 // 5) if we need a cookie, we need to compute
John McCall7d166272011-05-15 07:14:44 +0000669 // size := sizeWithoutCookie + cookieSize
670 // and check whether it overflows.
671
672 llvm::Value *hasOverflow = 0;
673
674 // If numElementsWidth > sizeWidth, then one way or another, we're
675 // going to have to do a comparison for (2), and this happens to
676 // take care of (1), too.
677 if (numElementsWidth > sizeWidth) {
678 llvm::APInt threshold(numElementsWidth, 1);
679 threshold <<= sizeWidth;
680
681 llvm::Value *thresholdV
682 = llvm::ConstantInt::get(numElementsType, threshold);
683
684 hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV);
685 numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy);
686
687 // Otherwise, if we're signed, we want to sext up to size_t.
688 } else if (isSigned) {
689 if (numElementsWidth < sizeWidth)
690 numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy);
691
692 // If there's a non-1 type size multiplier, then we can do the
693 // signedness check at the same time as we do the multiply
694 // because a negative number times anything will cause an
Sebastian Redl92036472012-02-22 17:37:52 +0000695 // unsigned overflow. Otherwise, we have to do it here. But at least
696 // in this case, we can subsume the >= minElements check.
John McCall7d166272011-05-15 07:14:44 +0000697 if (typeSizeMultiplier == 1)
698 hasOverflow = CGF.Builder.CreateICmpSLT(numElements,
Sebastian Redl92036472012-02-22 17:37:52 +0000699 llvm::ConstantInt::get(CGF.SizeTy, minElements));
John McCall7d166272011-05-15 07:14:44 +0000700
701 // Otherwise, zext up to size_t if necessary.
702 } else if (numElementsWidth < sizeWidth) {
703 numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy);
704 }
705
706 assert(numElements->getType() == CGF.SizeTy);
707
Sebastian Redl92036472012-02-22 17:37:52 +0000708 if (minElements) {
709 // Don't allow allocation of fewer elements than we have initializers.
710 if (!hasOverflow) {
711 hasOverflow = CGF.Builder.CreateICmpULT(numElements,
712 llvm::ConstantInt::get(CGF.SizeTy, minElements));
713 } else if (numElementsWidth > sizeWidth) {
714 // The other existing overflow subsumes this check.
715 // We do an unsigned comparison, since any signed value < -1 is
716 // taken care of either above or below.
717 hasOverflow = CGF.Builder.CreateOr(hasOverflow,
718 CGF.Builder.CreateICmpULT(numElements,
719 llvm::ConstantInt::get(CGF.SizeTy, minElements)));
720 }
721 }
722
John McCall7d166272011-05-15 07:14:44 +0000723 size = numElements;
724
725 // Multiply by the type size if necessary. This multiplier
726 // includes all the factors for nested arrays.
727 //
728 // This step also causes numElements to be scaled up by the
729 // nested-array factor if necessary. Overflow on this computation
730 // can be ignored because the result shouldn't be used if
731 // allocation fails.
732 if (typeSizeMultiplier != 1) {
John McCall7d166272011-05-15 07:14:44 +0000733 llvm::Value *umul_with_overflow
Benjamin Kramer8dd55a32011-07-14 17:45:50 +0000734 = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy);
John McCall7d166272011-05-15 07:14:44 +0000735
736 llvm::Value *tsmV =
737 llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier);
738 llvm::Value *result =
739 CGF.Builder.CreateCall2(umul_with_overflow, size, tsmV);
740
741 llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
742 if (hasOverflow)
743 hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
744 else
745 hasOverflow = overflowed;
746
747 size = CGF.Builder.CreateExtractValue(result, 0);
748
749 // Also scale up numElements by the array size multiplier.
750 if (arraySizeMultiplier != 1) {
751 // If the base element type size is 1, then we can re-use the
752 // multiply we just did.
753 if (typeSize.isOne()) {
754 assert(arraySizeMultiplier == typeSizeMultiplier);
755 numElements = size;
756
757 // Otherwise we need a separate multiply.
758 } else {
759 llvm::Value *asmV =
760 llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier);
761 numElements = CGF.Builder.CreateMul(numElements, asmV);
762 }
763 }
764 } else {
765 // numElements doesn't need to be scaled.
766 assert(arraySizeMultiplier == 1);
Chris Lattner806941e2010-07-20 21:55:52 +0000767 }
768
John McCall7d166272011-05-15 07:14:44 +0000769 // Add in the cookie size if necessary.
770 if (cookieSize != 0) {
771 sizeWithoutCookie = size;
772
John McCall7d166272011-05-15 07:14:44 +0000773 llvm::Value *uadd_with_overflow
Benjamin Kramer8dd55a32011-07-14 17:45:50 +0000774 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy);
John McCall7d166272011-05-15 07:14:44 +0000775
776 llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize);
777 llvm::Value *result =
778 CGF.Builder.CreateCall2(uadd_with_overflow, size, cookieSizeV);
779
780 llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
781 if (hasOverflow)
782 hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
783 else
784 hasOverflow = overflowed;
785
786 size = CGF.Builder.CreateExtractValue(result, 0);
John McCall1e7fe752010-09-02 09:58:18 +0000787 }
Anders Carlssona4d4c012009-09-23 16:07:23 +0000788
John McCall7d166272011-05-15 07:14:44 +0000789 // If we had any possibility of dynamic overflow, make a select to
790 // overwrite 'size' with an all-ones value, which should cause
791 // operator new to throw.
792 if (hasOverflow)
793 size = CGF.Builder.CreateSelect(hasOverflow,
794 llvm::Constant::getAllOnesValue(CGF.SizeTy),
795 size);
Chris Lattner806941e2010-07-20 21:55:52 +0000796 }
John McCall1e7fe752010-09-02 09:58:18 +0000797
John McCall7d166272011-05-15 07:14:44 +0000798 if (cookieSize == 0)
799 sizeWithoutCookie = size;
John McCall1e7fe752010-09-02 09:58:18 +0000800 else
John McCall7d166272011-05-15 07:14:44 +0000801 assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?");
John McCall1e7fe752010-09-02 09:58:18 +0000802
John McCall7d166272011-05-15 07:14:44 +0000803 return size;
Anders Carlssona4d4c012009-09-23 16:07:23 +0000804}
805
Sebastian Redl92036472012-02-22 17:37:52 +0000806static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init,
807 QualType AllocType, llvm::Value *NewPtr) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000808
Eli Friedmand7722d92011-12-03 02:13:40 +0000809 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(AllocType);
John McCalla07398e2011-06-16 04:16:24 +0000810 if (!CGF.hasAggregateLLVMType(AllocType))
Eli Friedmand7722d92011-12-03 02:13:40 +0000811 CGF.EmitScalarInit(Init, 0, CGF.MakeAddrLValue(NewPtr, AllocType,
Eli Friedman6da2c712011-12-03 04:14:32 +0000812 Alignment),
John McCalla07398e2011-06-16 04:16:24 +0000813 false);
Fariborz Jahanianef668722010-06-25 18:26:07 +0000814 else if (AllocType->isAnyComplexType())
815 CGF.EmitComplexExprIntoAddr(Init, NewPtr,
816 AllocType.isVolatileQualified());
John McCall558d2ab2010-09-15 10:14:12 +0000817 else {
818 AggValueSlot Slot
Eli Friedmanf3940782011-12-03 00:54:26 +0000819 = AggValueSlot::forAddr(NewPtr, Alignment, AllocType.getQualifiers(),
John McCall7c2349b2011-08-25 20:40:09 +0000820 AggValueSlot::IsDestructed,
John McCall44184392011-08-26 07:31:35 +0000821 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier649b4a12012-03-29 17:37:10 +0000822 AggValueSlot::IsNotAliased);
John McCall558d2ab2010-09-15 10:14:12 +0000823 CGF.EmitAggExpr(Init, Slot);
Sebastian Redl972edf02012-02-19 16:03:09 +0000824
825 CGF.MaybeEmitStdInitializerListCleanup(NewPtr, Init);
John McCall558d2ab2010-09-15 10:14:12 +0000826 }
Fariborz Jahanianef668722010-06-25 18:26:07 +0000827}
828
829void
830CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
John McCall19705672011-09-15 06:49:18 +0000831 QualType elementType,
832 llvm::Value *beginPtr,
833 llvm::Value *numElements) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000834 if (!E->hasInitializer())
835 return; // We have a POD type.
John McCall19705672011-09-15 06:49:18 +0000836
Sebastian Redl92036472012-02-22 17:37:52 +0000837 llvm::Value *explicitPtr = beginPtr;
John McCall19705672011-09-15 06:49:18 +0000838 // Find the end of the array, hoisted out of the loop.
839 llvm::Value *endPtr =
840 Builder.CreateInBoundsGEP(beginPtr, numElements, "array.end");
841
Sebastian Redl92036472012-02-22 17:37:52 +0000842 unsigned initializerElements = 0;
843
844 const Expr *Init = E->getInitializer();
Chad Rosier577fb5b2012-02-24 00:13:55 +0000845 llvm::AllocaInst *endOfInit = 0;
846 QualType::DestructionKind dtorKind = elementType.isDestructedType();
847 EHScopeStack::stable_iterator cleanup;
848 llvm::Instruction *cleanupDominator = 0;
Sebastian Redl92036472012-02-22 17:37:52 +0000849 // If the initializer is an initializer list, first do the explicit elements.
850 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
851 initializerElements = ILE->getNumInits();
Chad Rosier577fb5b2012-02-24 00:13:55 +0000852
853 // Enter a partial-destruction cleanup if necessary.
854 if (needsEHCleanup(dtorKind)) {
855 // In principle we could tell the cleanup where we are more
856 // directly, but the control flow can get so varied here that it
857 // would actually be quite complex. Therefore we go through an
858 // alloca.
859 endOfInit = CreateTempAlloca(beginPtr->getType(), "array.endOfInit");
860 cleanupDominator = Builder.CreateStore(beginPtr, endOfInit);
861 pushIrregularPartialArrayCleanup(beginPtr, endOfInit, elementType,
862 getDestroyer(dtorKind));
863 cleanup = EHStack.stable_begin();
864 }
865
Sebastian Redl92036472012-02-22 17:37:52 +0000866 for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) {
Chad Rosier577fb5b2012-02-24 00:13:55 +0000867 // Tell the cleanup that it needs to destroy up to this
868 // element. TODO: some of these stores can be trivially
869 // observed to be unnecessary.
870 if (endOfInit) Builder.CreateStore(explicitPtr, endOfInit);
Sebastian Redl92036472012-02-22 17:37:52 +0000871 StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), elementType, explicitPtr);
872 explicitPtr =Builder.CreateConstGEP1_32(explicitPtr, 1, "array.exp.next");
873 }
874
875 // The remaining elements are filled with the array filler expression.
876 Init = ILE->getArrayFiller();
877 }
878
John McCall19705672011-09-15 06:49:18 +0000879 // Create the continuation block.
880 llvm::BasicBlock *contBB = createBasicBlock("new.loop.end");
881
Sebastian Redl92036472012-02-22 17:37:52 +0000882 // If the number of elements isn't constant, we have to now check if there is
883 // anything left to initialize.
884 if (llvm::ConstantInt *constNum = dyn_cast<llvm::ConstantInt>(numElements)) {
885 // If all elements have already been initialized, skip the whole loop.
Chad Rosier577fb5b2012-02-24 00:13:55 +0000886 if (constNum->getZExtValue() <= initializerElements) {
887 // If there was a cleanup, deactivate it.
888 if (cleanupDominator)
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000889 DeactivateCleanupBlock(cleanup, cleanupDominator);
Chad Rosier577fb5b2012-02-24 00:13:55 +0000890 return;
891 }
Sebastian Redl92036472012-02-22 17:37:52 +0000892 } else {
John McCall19705672011-09-15 06:49:18 +0000893 llvm::BasicBlock *nonEmptyBB = createBasicBlock("new.loop.nonempty");
Sebastian Redl92036472012-02-22 17:37:52 +0000894 llvm::Value *isEmpty = Builder.CreateICmpEQ(explicitPtr, endPtr,
John McCall19705672011-09-15 06:49:18 +0000895 "array.isempty");
896 Builder.CreateCondBr(isEmpty, contBB, nonEmptyBB);
897 EmitBlock(nonEmptyBB);
898 }
899
900 // Enter the loop.
901 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
902 llvm::BasicBlock *loopBB = createBasicBlock("new.loop");
903
904 EmitBlock(loopBB);
905
906 // Set up the current-element phi.
907 llvm::PHINode *curPtr =
Sebastian Redl92036472012-02-22 17:37:52 +0000908 Builder.CreatePHI(explicitPtr->getType(), 2, "array.cur");
909 curPtr->addIncoming(explicitPtr, entryBB);
John McCall19705672011-09-15 06:49:18 +0000910
Chad Rosier577fb5b2012-02-24 00:13:55 +0000911 // Store the new cleanup position for irregular cleanups.
912 if (endOfInit) Builder.CreateStore(curPtr, endOfInit);
913
John McCall19705672011-09-15 06:49:18 +0000914 // Enter a partial-destruction cleanup if necessary.
Chad Rosier577fb5b2012-02-24 00:13:55 +0000915 if (!cleanupDominator && needsEHCleanup(dtorKind)) {
John McCall19705672011-09-15 06:49:18 +0000916 pushRegularPartialArrayCleanup(beginPtr, curPtr, elementType,
917 getDestroyer(dtorKind));
918 cleanup = EHStack.stable_begin();
John McCall6f103ba2011-11-10 10:43:54 +0000919 cleanupDominator = Builder.CreateUnreachable();
John McCall19705672011-09-15 06:49:18 +0000920 }
921
922 // Emit the initializer into this element.
Sebastian Redl92036472012-02-22 17:37:52 +0000923 StoreAnyExprIntoOneUnit(*this, Init, E->getAllocatedType(), curPtr);
John McCall19705672011-09-15 06:49:18 +0000924
925 // Leave the cleanup if we entered one.
Eli Friedman40563cd2011-12-09 23:05:37 +0000926 if (cleanupDominator) {
John McCall6f103ba2011-11-10 10:43:54 +0000927 DeactivateCleanupBlock(cleanup, cleanupDominator);
928 cleanupDominator->eraseFromParent();
929 }
John McCall19705672011-09-15 06:49:18 +0000930
931 // Advance to the next element.
932 llvm::Value *nextPtr = Builder.CreateConstGEP1_32(curPtr, 1, "array.next");
933
934 // Check whether we've gotten to the end of the array and, if so,
935 // exit the loop.
936 llvm::Value *isEnd = Builder.CreateICmpEQ(nextPtr, endPtr, "array.atend");
937 Builder.CreateCondBr(isEnd, contBB, loopBB);
938 curPtr->addIncoming(nextPtr, Builder.GetInsertBlock());
939
940 EmitBlock(contBB);
Fariborz Jahanianef668722010-06-25 18:26:07 +0000941}
942
Douglas Gregor59174c02010-07-21 01:10:17 +0000943static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T,
944 llvm::Value *NewPtr, llvm::Value *Size) {
John McCalld16c2cf2011-02-08 08:22:06 +0000945 CGF.EmitCastToVoidPtr(NewPtr);
Ken Dyckfe710082011-01-19 01:58:38 +0000946 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T);
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000947 CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size,
Ken Dyckfe710082011-01-19 01:58:38 +0000948 Alignment.getQuantity(), false);
Douglas Gregor59174c02010-07-21 01:10:17 +0000949}
950
Anders Carlssona4d4c012009-09-23 16:07:23 +0000951static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
John McCall19705672011-09-15 06:49:18 +0000952 QualType ElementType,
Anders Carlssona4d4c012009-09-23 16:07:23 +0000953 llvm::Value *NewPtr,
Douglas Gregor59174c02010-07-21 01:10:17 +0000954 llvm::Value *NumElements,
955 llvm::Value *AllocSizeWithoutCookie) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000956 const Expr *Init = E->getInitializer();
Anders Carlsson5d4d9462009-11-24 18:43:52 +0000957 if (E->isArray()) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000958 if (const CXXConstructExpr *CCE = dyn_cast_or_null<CXXConstructExpr>(Init)){
959 CXXConstructorDecl *Ctor = CCE->getConstructor();
Douglas Gregor887ddf32012-02-23 17:07:43 +0000960 if (Ctor->isTrivial()) {
Douglas Gregor59174c02010-07-21 01:10:17 +0000961 // If new expression did not specify value-initialization, then there
962 // is no initialization.
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000963 if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty())
Douglas Gregor59174c02010-07-21 01:10:17 +0000964 return;
965
John McCall19705672011-09-15 06:49:18 +0000966 if (CGF.CGM.getTypes().isZeroInitializable(ElementType)) {
Douglas Gregor59174c02010-07-21 01:10:17 +0000967 // Optimization: since zero initialization will just set the memory
968 // to all zeroes, generate a single memset to do it in one shot.
John McCall19705672011-09-15 06:49:18 +0000969 EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie);
Douglas Gregor59174c02010-07-21 01:10:17 +0000970 return;
971 }
Douglas Gregor59174c02010-07-21 01:10:17 +0000972 }
John McCallc3c07662011-07-13 06:10:41 +0000973
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000974 CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr,
975 CCE->arg_begin(), CCE->arg_end(),
Eli Friedmanb41ba1a2012-08-25 07:11:29 +0000976 CCE->requiresZeroInitialization());
Anders Carlssone99bdb62010-05-03 15:09:17 +0000977 return;
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000978 } else if (Init && isa<ImplicitValueInitExpr>(Init) &&
Eli Friedman40563cd2011-12-09 23:05:37 +0000979 CGF.CGM.getTypes().isZeroInitializable(ElementType)) {
Douglas Gregor59174c02010-07-21 01:10:17 +0000980 // Optimization: since zero initialization will just set the memory
981 // to all zeroes, generate a single memset to do it in one shot.
John McCall19705672011-09-15 06:49:18 +0000982 EmitZeroMemSet(CGF, ElementType, NewPtr, AllocSizeWithoutCookie);
983 return;
Fariborz Jahanianef668722010-06-25 18:26:07 +0000984 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000985 CGF.EmitNewArrayInitializer(E, ElementType, NewPtr, NumElements);
986 return;
Anders Carlssona4d4c012009-09-23 16:07:23 +0000987 }
Anders Carlsson5d4d9462009-11-24 18:43:52 +0000988
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000989 if (!Init)
Fariborz Jahanian5304c952010-06-25 20:01:13 +0000990 return;
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000991
Sebastian Redl92036472012-02-22 17:37:52 +0000992 StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr);
Anders Carlssona4d4c012009-09-23 16:07:23 +0000993}
994
John McCall7d8647f2010-09-14 07:57:04 +0000995namespace {
996 /// A cleanup to call the given 'operator delete' function upon
997 /// abnormal exit from a new expression.
998 class CallDeleteDuringNew : public EHScopeStack::Cleanup {
999 size_t NumPlacementArgs;
1000 const FunctionDecl *OperatorDelete;
1001 llvm::Value *Ptr;
1002 llvm::Value *AllocSize;
1003
1004 RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); }
1005
1006 public:
1007 static size_t getExtraSize(size_t NumPlacementArgs) {
1008 return NumPlacementArgs * sizeof(RValue);
1009 }
1010
1011 CallDeleteDuringNew(size_t NumPlacementArgs,
1012 const FunctionDecl *OperatorDelete,
1013 llvm::Value *Ptr,
1014 llvm::Value *AllocSize)
1015 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
1016 Ptr(Ptr), AllocSize(AllocSize) {}
1017
1018 void setPlacementArg(unsigned I, RValue Arg) {
1019 assert(I < NumPlacementArgs && "index out of range");
1020 getPlacementArgs()[I] = Arg;
1021 }
1022
John McCallad346f42011-07-12 20:27:29 +00001023 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall7d8647f2010-09-14 07:57:04 +00001024 const FunctionProtoType *FPT
1025 = OperatorDelete->getType()->getAs<FunctionProtoType>();
1026 assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
John McCallc3846362010-09-14 21:45:42 +00001027 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
John McCall7d8647f2010-09-14 07:57:04 +00001028
1029 CallArgList DeleteArgs;
1030
1031 // The first argument is always a void*.
1032 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
Eli Friedman04c9a492011-05-02 17:57:46 +00001033 DeleteArgs.add(RValue::get(Ptr), *AI++);
John McCall7d8647f2010-09-14 07:57:04 +00001034
1035 // A member 'operator delete' can take an extra 'size_t' argument.
1036 if (FPT->getNumArgs() == NumPlacementArgs + 2)
Eli Friedman04c9a492011-05-02 17:57:46 +00001037 DeleteArgs.add(RValue::get(AllocSize), *AI++);
John McCall7d8647f2010-09-14 07:57:04 +00001038
1039 // Pass the rest of the arguments, which must match exactly.
1040 for (unsigned I = 0; I != NumPlacementArgs; ++I)
Eli Friedman04c9a492011-05-02 17:57:46 +00001041 DeleteArgs.add(getPlacementArgs()[I], *AI++);
John McCall7d8647f2010-09-14 07:57:04 +00001042
1043 // Call 'operator delete'.
John McCall0f3d0972012-07-07 06:41:13 +00001044 CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT),
John McCall7d8647f2010-09-14 07:57:04 +00001045 CGF.CGM.GetAddrOfFunction(OperatorDelete),
1046 ReturnValueSlot(), DeleteArgs, OperatorDelete);
1047 }
1048 };
John McCall3019c442010-09-17 00:50:28 +00001049
1050 /// A cleanup to call the given 'operator delete' function upon
1051 /// abnormal exit from a new expression when the new expression is
1052 /// conditional.
1053 class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup {
1054 size_t NumPlacementArgs;
1055 const FunctionDecl *OperatorDelete;
John McCall804b8072011-01-28 10:53:53 +00001056 DominatingValue<RValue>::saved_type Ptr;
1057 DominatingValue<RValue>::saved_type AllocSize;
John McCall3019c442010-09-17 00:50:28 +00001058
John McCall804b8072011-01-28 10:53:53 +00001059 DominatingValue<RValue>::saved_type *getPlacementArgs() {
1060 return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1);
John McCall3019c442010-09-17 00:50:28 +00001061 }
1062
1063 public:
1064 static size_t getExtraSize(size_t NumPlacementArgs) {
John McCall804b8072011-01-28 10:53:53 +00001065 return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type);
John McCall3019c442010-09-17 00:50:28 +00001066 }
1067
1068 CallDeleteDuringConditionalNew(size_t NumPlacementArgs,
1069 const FunctionDecl *OperatorDelete,
John McCall804b8072011-01-28 10:53:53 +00001070 DominatingValue<RValue>::saved_type Ptr,
1071 DominatingValue<RValue>::saved_type AllocSize)
John McCall3019c442010-09-17 00:50:28 +00001072 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
1073 Ptr(Ptr), AllocSize(AllocSize) {}
1074
John McCall804b8072011-01-28 10:53:53 +00001075 void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) {
John McCall3019c442010-09-17 00:50:28 +00001076 assert(I < NumPlacementArgs && "index out of range");
1077 getPlacementArgs()[I] = Arg;
1078 }
1079
John McCallad346f42011-07-12 20:27:29 +00001080 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall3019c442010-09-17 00:50:28 +00001081 const FunctionProtoType *FPT
1082 = OperatorDelete->getType()->getAs<FunctionProtoType>();
1083 assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
1084 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
1085
1086 CallArgList DeleteArgs;
1087
1088 // The first argument is always a void*.
1089 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
Eli Friedman04c9a492011-05-02 17:57:46 +00001090 DeleteArgs.add(Ptr.restore(CGF), *AI++);
John McCall3019c442010-09-17 00:50:28 +00001091
1092 // A member 'operator delete' can take an extra 'size_t' argument.
1093 if (FPT->getNumArgs() == NumPlacementArgs + 2) {
John McCall804b8072011-01-28 10:53:53 +00001094 RValue RV = AllocSize.restore(CGF);
Eli Friedman04c9a492011-05-02 17:57:46 +00001095 DeleteArgs.add(RV, *AI++);
John McCall3019c442010-09-17 00:50:28 +00001096 }
1097
1098 // Pass the rest of the arguments, which must match exactly.
1099 for (unsigned I = 0; I != NumPlacementArgs; ++I) {
John McCall804b8072011-01-28 10:53:53 +00001100 RValue RV = getPlacementArgs()[I].restore(CGF);
Eli Friedman04c9a492011-05-02 17:57:46 +00001101 DeleteArgs.add(RV, *AI++);
John McCall3019c442010-09-17 00:50:28 +00001102 }
1103
1104 // Call 'operator delete'.
John McCall0f3d0972012-07-07 06:41:13 +00001105 CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, FPT),
John McCall3019c442010-09-17 00:50:28 +00001106 CGF.CGM.GetAddrOfFunction(OperatorDelete),
1107 ReturnValueSlot(), DeleteArgs, OperatorDelete);
1108 }
1109 };
1110}
1111
1112/// Enter a cleanup to call 'operator delete' if the initializer in a
1113/// new-expression throws.
1114static void EnterNewDeleteCleanup(CodeGenFunction &CGF,
1115 const CXXNewExpr *E,
1116 llvm::Value *NewPtr,
1117 llvm::Value *AllocSize,
1118 const CallArgList &NewArgs) {
1119 // If we're not inside a conditional branch, then the cleanup will
1120 // dominate and we can do the easier (and more efficient) thing.
1121 if (!CGF.isInConditionalBranch()) {
1122 CallDeleteDuringNew *Cleanup = CGF.EHStack
1123 .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup,
1124 E->getNumPlacementArgs(),
1125 E->getOperatorDelete(),
1126 NewPtr, AllocSize);
1127 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
Eli Friedmanc6d07822011-05-02 18:05:27 +00001128 Cleanup->setPlacementArg(I, NewArgs[I+1].RV);
John McCall3019c442010-09-17 00:50:28 +00001129
1130 return;
1131 }
1132
1133 // Otherwise, we need to save all this stuff.
John McCall804b8072011-01-28 10:53:53 +00001134 DominatingValue<RValue>::saved_type SavedNewPtr =
1135 DominatingValue<RValue>::save(CGF, RValue::get(NewPtr));
1136 DominatingValue<RValue>::saved_type SavedAllocSize =
1137 DominatingValue<RValue>::save(CGF, RValue::get(AllocSize));
John McCall3019c442010-09-17 00:50:28 +00001138
1139 CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack
John McCall6f103ba2011-11-10 10:43:54 +00001140 .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup,
John McCall3019c442010-09-17 00:50:28 +00001141 E->getNumPlacementArgs(),
1142 E->getOperatorDelete(),
1143 SavedNewPtr,
1144 SavedAllocSize);
1145 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
John McCall804b8072011-01-28 10:53:53 +00001146 Cleanup->setPlacementArg(I,
Eli Friedmanc6d07822011-05-02 18:05:27 +00001147 DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV));
John McCall3019c442010-09-17 00:50:28 +00001148
John McCall6f103ba2011-11-10 10:43:54 +00001149 CGF.initFullExprCleanup();
John McCall7d8647f2010-09-14 07:57:04 +00001150}
1151
Anders Carlsson16d81b82009-09-22 22:53:17 +00001152llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
John McCallc2f3e7f2011-03-07 03:12:35 +00001153 // The element type being allocated.
1154 QualType allocType = getContext().getBaseElementType(E->getAllocatedType());
John McCall1e7fe752010-09-02 09:58:18 +00001155
John McCallc2f3e7f2011-03-07 03:12:35 +00001156 // 1. Build a call to the allocation function.
1157 FunctionDecl *allocator = E->getOperatorNew();
1158 const FunctionProtoType *allocatorType =
1159 allocator->getType()->castAs<FunctionProtoType>();
Anders Carlsson16d81b82009-09-22 22:53:17 +00001160
John McCallc2f3e7f2011-03-07 03:12:35 +00001161 CallArgList allocatorArgs;
Anders Carlsson16d81b82009-09-22 22:53:17 +00001162
1163 // The allocation size is the first argument.
John McCallc2f3e7f2011-03-07 03:12:35 +00001164 QualType sizeType = getContext().getSizeType();
Anders Carlsson16d81b82009-09-22 22:53:17 +00001165
Sebastian Redl92036472012-02-22 17:37:52 +00001166 // If there is a brace-initializer, cannot allocate fewer elements than inits.
1167 unsigned minElements = 0;
1168 if (E->isArray() && E->hasInitializer()) {
1169 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer()))
1170 minElements = ILE->getNumInits();
1171 }
1172
John McCallc2f3e7f2011-03-07 03:12:35 +00001173 llvm::Value *numElements = 0;
1174 llvm::Value *allocSizeWithoutCookie = 0;
1175 llvm::Value *allocSize =
Sebastian Redl92036472012-02-22 17:37:52 +00001176 EmitCXXNewAllocSize(*this, E, minElements, numElements,
1177 allocSizeWithoutCookie);
Anders Carlssona4d4c012009-09-23 16:07:23 +00001178
Eli Friedman04c9a492011-05-02 17:57:46 +00001179 allocatorArgs.add(RValue::get(allocSize), sizeType);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001180
1181 // Emit the rest of the arguments.
1182 // FIXME: Ideally, this should just use EmitCallArgs.
John McCallc2f3e7f2011-03-07 03:12:35 +00001183 CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin();
Anders Carlsson16d81b82009-09-22 22:53:17 +00001184
1185 // First, use the types from the function type.
1186 // We start at 1 here because the first argument (the allocation size)
1187 // has already been emitted.
John McCallc2f3e7f2011-03-07 03:12:35 +00001188 for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e;
1189 ++i, ++placementArg) {
1190 QualType argType = allocatorType->getArgType(i);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001191
John McCallc2f3e7f2011-03-07 03:12:35 +00001192 assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(),
1193 placementArg->getType()) &&
Anders Carlsson16d81b82009-09-22 22:53:17 +00001194 "type mismatch in call argument!");
1195
John McCall413ebdb2011-03-11 20:59:21 +00001196 EmitCallArg(allocatorArgs, *placementArg, argType);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001197 }
1198
1199 // Either we've emitted all the call args, or we have a call to a
1200 // variadic function.
John McCallc2f3e7f2011-03-07 03:12:35 +00001201 assert((placementArg == E->placement_arg_end() ||
1202 allocatorType->isVariadic()) &&
1203 "Extra arguments to non-variadic function!");
Anders Carlsson16d81b82009-09-22 22:53:17 +00001204
1205 // If we still have any arguments, emit them using the type of the argument.
John McCallc2f3e7f2011-03-07 03:12:35 +00001206 for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end();
1207 placementArg != placementArgsEnd; ++placementArg) {
John McCall413ebdb2011-03-11 20:59:21 +00001208 EmitCallArg(allocatorArgs, *placementArg, placementArg->getType());
Anders Carlsson16d81b82009-09-22 22:53:17 +00001209 }
1210
John McCallb1c98a32011-05-16 01:05:12 +00001211 // Emit the allocation call. If the allocator is a global placement
1212 // operator, just "inline" it directly.
1213 RValue RV;
1214 if (allocator->isReservedGlobalPlacementOperator()) {
1215 assert(allocatorArgs.size() == 2);
1216 RV = allocatorArgs[1].RV;
1217 // TODO: kill any unnecessary computations done for the size
1218 // argument.
1219 } else {
John McCall0f3d0972012-07-07 06:41:13 +00001220 RV = EmitCall(CGM.getTypes().arrangeFreeFunctionCall(allocatorArgs,
1221 allocatorType),
John McCallb1c98a32011-05-16 01:05:12 +00001222 CGM.GetAddrOfFunction(allocator), ReturnValueSlot(),
1223 allocatorArgs, allocator);
1224 }
Anders Carlsson16d81b82009-09-22 22:53:17 +00001225
John McCallc2f3e7f2011-03-07 03:12:35 +00001226 // Emit a null check on the allocation result if the allocation
1227 // function is allowed to return null (because it has a non-throwing
1228 // exception spec; for this part, we inline
1229 // CXXNewExpr::shouldNullCheckAllocation()) and we have an
1230 // interesting initializer.
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001231 bool nullCheck = allocatorType->isNothrow(getContext()) &&
Sebastian Redl2aed8b82012-02-16 12:22:20 +00001232 (!allocType.isPODType(getContext()) || E->hasInitializer());
Anders Carlsson16d81b82009-09-22 22:53:17 +00001233
John McCallc2f3e7f2011-03-07 03:12:35 +00001234 llvm::BasicBlock *nullCheckBB = 0;
1235 llvm::BasicBlock *contBB = 0;
Anders Carlsson16d81b82009-09-22 22:53:17 +00001236
John McCallc2f3e7f2011-03-07 03:12:35 +00001237 llvm::Value *allocation = RV.getScalarVal();
1238 unsigned AS =
1239 cast<llvm::PointerType>(allocation->getType())->getAddressSpace();
Anders Carlsson16d81b82009-09-22 22:53:17 +00001240
John McCalla7f633f2011-03-07 01:52:56 +00001241 // The null-check means that the initializer is conditionally
1242 // evaluated.
1243 ConditionalEvaluation conditional(*this);
1244
John McCallc2f3e7f2011-03-07 03:12:35 +00001245 if (nullCheck) {
John McCalla7f633f2011-03-07 01:52:56 +00001246 conditional.begin(*this);
John McCallc2f3e7f2011-03-07 03:12:35 +00001247
1248 nullCheckBB = Builder.GetInsertBlock();
1249 llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull");
1250 contBB = createBasicBlock("new.cont");
1251
1252 llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull");
1253 Builder.CreateCondBr(isNull, contBB, notNullBB);
1254 EmitBlock(notNullBB);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001255 }
Anders Carlsson6ac5fc42009-09-23 18:59:48 +00001256
John McCall7d8647f2010-09-14 07:57:04 +00001257 // If there's an operator delete, enter a cleanup to call it if an
1258 // exception is thrown.
John McCallc2f3e7f2011-03-07 03:12:35 +00001259 EHScopeStack::stable_iterator operatorDeleteCleanup;
John McCall6f103ba2011-11-10 10:43:54 +00001260 llvm::Instruction *cleanupDominator = 0;
John McCallb1c98a32011-05-16 01:05:12 +00001261 if (E->getOperatorDelete() &&
1262 !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) {
John McCallc2f3e7f2011-03-07 03:12:35 +00001263 EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs);
1264 operatorDeleteCleanup = EHStack.stable_begin();
John McCall6f103ba2011-11-10 10:43:54 +00001265 cleanupDominator = Builder.CreateUnreachable();
John McCall7d8647f2010-09-14 07:57:04 +00001266 }
1267
Eli Friedman576cf172011-09-06 18:53:03 +00001268 assert((allocSize == allocSizeWithoutCookie) ==
1269 CalculateCookiePadding(*this, E).isZero());
1270 if (allocSize != allocSizeWithoutCookie) {
1271 assert(E->isArray());
1272 allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
1273 numElements,
1274 E, allocType);
1275 }
1276
Chris Lattner2acc6e32011-07-18 04:24:23 +00001277 llvm::Type *elementPtrTy
John McCallc2f3e7f2011-03-07 03:12:35 +00001278 = ConvertTypeForMem(allocType)->getPointerTo(AS);
1279 llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy);
John McCall7d8647f2010-09-14 07:57:04 +00001280
John McCall19705672011-09-15 06:49:18 +00001281 EmitNewInitializer(*this, E, allocType, result, numElements,
1282 allocSizeWithoutCookie);
John McCall1e7fe752010-09-02 09:58:18 +00001283 if (E->isArray()) {
John McCall1e7fe752010-09-02 09:58:18 +00001284 // NewPtr is a pointer to the base element type. If we're
1285 // allocating an array of arrays, we'll need to cast back to the
1286 // array pointer type.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001287 llvm::Type *resultType = ConvertTypeForMem(E->getType());
John McCallc2f3e7f2011-03-07 03:12:35 +00001288 if (result->getType() != resultType)
1289 result = Builder.CreateBitCast(result, resultType);
Fariborz Jahanianceb43b62010-03-24 16:57:01 +00001290 }
John McCall7d8647f2010-09-14 07:57:04 +00001291
1292 // Deactivate the 'operator delete' cleanup if we finished
1293 // initialization.
John McCall6f103ba2011-11-10 10:43:54 +00001294 if (operatorDeleteCleanup.isValid()) {
1295 DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator);
1296 cleanupDominator->eraseFromParent();
1297 }
Sebastian Redl2aed8b82012-02-16 12:22:20 +00001298
John McCallc2f3e7f2011-03-07 03:12:35 +00001299 if (nullCheck) {
John McCalla7f633f2011-03-07 01:52:56 +00001300 conditional.end(*this);
1301
John McCallc2f3e7f2011-03-07 03:12:35 +00001302 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
1303 EmitBlock(contBB);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001304
Jay Foadbbf3bac2011-03-30 11:28:58 +00001305 llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2);
John McCallc2f3e7f2011-03-07 03:12:35 +00001306 PHI->addIncoming(result, notNullBB);
1307 PHI->addIncoming(llvm::Constant::getNullValue(result->getType()),
1308 nullCheckBB);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001309
John McCallc2f3e7f2011-03-07 03:12:35 +00001310 result = PHI;
Anders Carlsson16d81b82009-09-22 22:53:17 +00001311 }
John McCall1e7fe752010-09-02 09:58:18 +00001312
John McCallc2f3e7f2011-03-07 03:12:35 +00001313 return result;
Anders Carlsson16d81b82009-09-22 22:53:17 +00001314}
1315
Eli Friedman5fe05982009-11-18 00:50:08 +00001316void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
1317 llvm::Value *Ptr,
1318 QualType DeleteTy) {
John McCall1e7fe752010-09-02 09:58:18 +00001319 assert(DeleteFD->getOverloadedOperator() == OO_Delete);
1320
Eli Friedman5fe05982009-11-18 00:50:08 +00001321 const FunctionProtoType *DeleteFTy =
1322 DeleteFD->getType()->getAs<FunctionProtoType>();
1323
1324 CallArgList DeleteArgs;
1325
Anders Carlsson871d0782009-12-13 20:04:38 +00001326 // Check if we need to pass the size to the delete operator.
1327 llvm::Value *Size = 0;
1328 QualType SizeTy;
1329 if (DeleteFTy->getNumArgs() == 2) {
1330 SizeTy = DeleteFTy->getArgType(1);
Ken Dyck4f122ef2010-01-26 19:59:28 +00001331 CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
1332 Size = llvm::ConstantInt::get(ConvertType(SizeTy),
1333 DeleteTypeSize.getQuantity());
Anders Carlsson871d0782009-12-13 20:04:38 +00001334 }
1335
Eli Friedman5fe05982009-11-18 00:50:08 +00001336 QualType ArgTy = DeleteFTy->getArgType(0);
1337 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
Eli Friedman04c9a492011-05-02 17:57:46 +00001338 DeleteArgs.add(RValue::get(DeletePtr), ArgTy);
Eli Friedman5fe05982009-11-18 00:50:08 +00001339
Anders Carlsson871d0782009-12-13 20:04:38 +00001340 if (Size)
Eli Friedman04c9a492011-05-02 17:57:46 +00001341 DeleteArgs.add(RValue::get(Size), SizeTy);
Eli Friedman5fe05982009-11-18 00:50:08 +00001342
1343 // Emit the call to delete.
John McCall0f3d0972012-07-07 06:41:13 +00001344 EmitCall(CGM.getTypes().arrangeFreeFunctionCall(DeleteArgs, DeleteFTy),
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001345 CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
Eli Friedman5fe05982009-11-18 00:50:08 +00001346 DeleteArgs, DeleteFD);
1347}
1348
John McCall1e7fe752010-09-02 09:58:18 +00001349namespace {
1350 /// Calls the given 'operator delete' on a single object.
1351 struct CallObjectDelete : EHScopeStack::Cleanup {
1352 llvm::Value *Ptr;
1353 const FunctionDecl *OperatorDelete;
1354 QualType ElementType;
1355
1356 CallObjectDelete(llvm::Value *Ptr,
1357 const FunctionDecl *OperatorDelete,
1358 QualType ElementType)
1359 : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
1360
John McCallad346f42011-07-12 20:27:29 +00001361 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall1e7fe752010-09-02 09:58:18 +00001362 CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
1363 }
1364 };
1365}
1366
1367/// Emit the code for deleting a single object.
1368static void EmitObjectDelete(CodeGenFunction &CGF,
1369 const FunctionDecl *OperatorDelete,
1370 llvm::Value *Ptr,
Douglas Gregora8b20f72011-07-13 00:54:47 +00001371 QualType ElementType,
1372 bool UseGlobalDelete) {
John McCall1e7fe752010-09-02 09:58:18 +00001373 // Find the destructor for the type, if applicable. If the
1374 // destructor is virtual, we'll just emit the vcall and return.
1375 const CXXDestructorDecl *Dtor = 0;
1376 if (const RecordType *RT = ElementType->getAs<RecordType>()) {
1377 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Eli Friedmanaebab722011-08-02 18:05:30 +00001378 if (RD->hasDefinition() && !RD->hasTrivialDestructor()) {
John McCall1e7fe752010-09-02 09:58:18 +00001379 Dtor = RD->getDestructor();
1380
1381 if (Dtor->isVirtual()) {
Douglas Gregora8b20f72011-07-13 00:54:47 +00001382 if (UseGlobalDelete) {
1383 // If we're supposed to call the global delete, make sure we do so
1384 // even if the destructor throws.
John McCallecd03b42012-09-25 10:10:39 +00001385
1386 // Derive the complete-object pointer, which is what we need
1387 // to pass to the deallocation function.
1388 llvm::Value *completePtr =
1389 CGF.CGM.getCXXABI().adjustToCompleteObject(CGF, Ptr, ElementType);
1390
Douglas Gregora8b20f72011-07-13 00:54:47 +00001391 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
John McCallecd03b42012-09-25 10:10:39 +00001392 completePtr, OperatorDelete,
Douglas Gregora8b20f72011-07-13 00:54:47 +00001393 ElementType);
1394 }
1395
Chris Lattner2acc6e32011-07-18 04:24:23 +00001396 llvm::Type *Ty =
John McCallde5d3c72012-02-17 03:33:10 +00001397 CGF.getTypes().GetFunctionType(
1398 CGF.getTypes().arrangeCXXDestructor(Dtor, Dtor_Complete));
John McCall1e7fe752010-09-02 09:58:18 +00001399
1400 llvm::Value *Callee
Douglas Gregora8b20f72011-07-13 00:54:47 +00001401 = CGF.BuildVirtualCall(Dtor,
1402 UseGlobalDelete? Dtor_Complete : Dtor_Deleting,
1403 Ptr, Ty);
John McCall1e7fe752010-09-02 09:58:18 +00001404 CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0,
1405 0, 0);
1406
Douglas Gregora8b20f72011-07-13 00:54:47 +00001407 if (UseGlobalDelete) {
1408 CGF.PopCleanupBlock();
1409 }
1410
John McCall1e7fe752010-09-02 09:58:18 +00001411 return;
1412 }
1413 }
1414 }
1415
1416 // Make sure that we call delete even if the dtor throws.
John McCall3ad32c82011-01-28 08:37:24 +00001417 // This doesn't have to a conditional cleanup because we're going
1418 // to pop it off in a second.
John McCall1e7fe752010-09-02 09:58:18 +00001419 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1420 Ptr, OperatorDelete, ElementType);
1421
1422 if (Dtor)
1423 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1424 /*ForVirtualBase=*/false, Ptr);
David Blaikie4e4d0842012-03-11 07:00:24 +00001425 else if (CGF.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001426 ElementType->isObjCLifetimeType()) {
1427 switch (ElementType.getObjCLifetime()) {
1428 case Qualifiers::OCL_None:
1429 case Qualifiers::OCL_ExplicitNone:
1430 case Qualifiers::OCL_Autoreleasing:
1431 break;
John McCall1e7fe752010-09-02 09:58:18 +00001432
John McCallf85e1932011-06-15 23:02:42 +00001433 case Qualifiers::OCL_Strong: {
1434 // Load the pointer value.
1435 llvm::Value *PtrValue = CGF.Builder.CreateLoad(Ptr,
1436 ElementType.isVolatileQualified());
1437
1438 CGF.EmitARCRelease(PtrValue, /*precise*/ true);
1439 break;
1440 }
1441
1442 case Qualifiers::OCL_Weak:
1443 CGF.EmitARCDestroyWeak(Ptr);
1444 break;
1445 }
1446 }
1447
John McCall1e7fe752010-09-02 09:58:18 +00001448 CGF.PopCleanupBlock();
1449}
1450
1451namespace {
1452 /// Calls the given 'operator delete' on an array of objects.
1453 struct CallArrayDelete : EHScopeStack::Cleanup {
1454 llvm::Value *Ptr;
1455 const FunctionDecl *OperatorDelete;
1456 llvm::Value *NumElements;
1457 QualType ElementType;
1458 CharUnits CookieSize;
1459
1460 CallArrayDelete(llvm::Value *Ptr,
1461 const FunctionDecl *OperatorDelete,
1462 llvm::Value *NumElements,
1463 QualType ElementType,
1464 CharUnits CookieSize)
1465 : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
1466 ElementType(ElementType), CookieSize(CookieSize) {}
1467
John McCallad346f42011-07-12 20:27:29 +00001468 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall1e7fe752010-09-02 09:58:18 +00001469 const FunctionProtoType *DeleteFTy =
1470 OperatorDelete->getType()->getAs<FunctionProtoType>();
1471 assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
1472
1473 CallArgList Args;
1474
1475 // Pass the pointer as the first argument.
1476 QualType VoidPtrTy = DeleteFTy->getArgType(0);
1477 llvm::Value *DeletePtr
1478 = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
Eli Friedman04c9a492011-05-02 17:57:46 +00001479 Args.add(RValue::get(DeletePtr), VoidPtrTy);
John McCall1e7fe752010-09-02 09:58:18 +00001480
1481 // Pass the original requested size as the second argument.
1482 if (DeleteFTy->getNumArgs() == 2) {
1483 QualType size_t = DeleteFTy->getArgType(1);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001484 llvm::IntegerType *SizeTy
John McCall1e7fe752010-09-02 09:58:18 +00001485 = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
1486
1487 CharUnits ElementTypeSize =
1488 CGF.CGM.getContext().getTypeSizeInChars(ElementType);
1489
1490 // The size of an element, multiplied by the number of elements.
1491 llvm::Value *Size
1492 = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
1493 Size = CGF.Builder.CreateMul(Size, NumElements);
1494
1495 // Plus the size of the cookie if applicable.
1496 if (!CookieSize.isZero()) {
1497 llvm::Value *CookieSizeV
1498 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
1499 Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
1500 }
1501
Eli Friedman04c9a492011-05-02 17:57:46 +00001502 Args.add(RValue::get(Size), size_t);
John McCall1e7fe752010-09-02 09:58:18 +00001503 }
1504
1505 // Emit the call to delete.
John McCall0f3d0972012-07-07 06:41:13 +00001506 CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Args, DeleteFTy),
John McCall1e7fe752010-09-02 09:58:18 +00001507 CGF.CGM.GetAddrOfFunction(OperatorDelete),
1508 ReturnValueSlot(), Args, OperatorDelete);
1509 }
1510 };
1511}
1512
1513/// Emit the code for deleting an array of objects.
1514static void EmitArrayDelete(CodeGenFunction &CGF,
John McCall6ec278d2011-01-27 09:37:56 +00001515 const CXXDeleteExpr *E,
John McCall7cfd76c2011-07-13 01:41:37 +00001516 llvm::Value *deletedPtr,
1517 QualType elementType) {
1518 llvm::Value *numElements = 0;
1519 llvm::Value *allocatedPtr = 0;
1520 CharUnits cookieSize;
1521 CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType,
1522 numElements, allocatedPtr, cookieSize);
John McCall1e7fe752010-09-02 09:58:18 +00001523
John McCall7cfd76c2011-07-13 01:41:37 +00001524 assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer");
John McCall1e7fe752010-09-02 09:58:18 +00001525
1526 // Make sure that we call delete even if one of the dtors throws.
John McCall7cfd76c2011-07-13 01:41:37 +00001527 const FunctionDecl *operatorDelete = E->getOperatorDelete();
John McCall1e7fe752010-09-02 09:58:18 +00001528 CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
John McCall7cfd76c2011-07-13 01:41:37 +00001529 allocatedPtr, operatorDelete,
1530 numElements, elementType,
1531 cookieSize);
John McCall1e7fe752010-09-02 09:58:18 +00001532
John McCall7cfd76c2011-07-13 01:41:37 +00001533 // Destroy the elements.
1534 if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) {
1535 assert(numElements && "no element count for a type with a destructor!");
1536
John McCall7cfd76c2011-07-13 01:41:37 +00001537 llvm::Value *arrayEnd =
1538 CGF.Builder.CreateInBoundsGEP(deletedPtr, numElements, "delete.end");
John McCallfbf780a2011-07-13 08:09:46 +00001539
1540 // Note that it is legal to allocate a zero-length array, and we
1541 // can never fold the check away because the length should always
1542 // come from a cookie.
John McCall7cfd76c2011-07-13 01:41:37 +00001543 CGF.emitArrayDestroy(deletedPtr, arrayEnd, elementType,
1544 CGF.getDestroyer(dtorKind),
John McCallfbf780a2011-07-13 08:09:46 +00001545 /*checkZeroLength*/ true,
John McCall7cfd76c2011-07-13 01:41:37 +00001546 CGF.needsEHCleanup(dtorKind));
John McCall1e7fe752010-09-02 09:58:18 +00001547 }
1548
John McCall7cfd76c2011-07-13 01:41:37 +00001549 // Pop the cleanup block.
John McCall1e7fe752010-09-02 09:58:18 +00001550 CGF.PopCleanupBlock();
1551}
1552
Anders Carlsson16d81b82009-09-22 22:53:17 +00001553void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
Douglas Gregor90916562009-09-29 18:16:17 +00001554 const Expr *Arg = E->getArgument();
Douglas Gregor90916562009-09-29 18:16:17 +00001555 llvm::Value *Ptr = EmitScalarExpr(Arg);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001556
1557 // Null check the pointer.
1558 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
1559 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
1560
Anders Carlssonb9241242011-04-11 00:30:07 +00001561 llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull");
Anders Carlsson16d81b82009-09-22 22:53:17 +00001562
1563 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
1564 EmitBlock(DeleteNotNull);
Anders Carlsson566abee2009-11-13 04:45:41 +00001565
John McCall1e7fe752010-09-02 09:58:18 +00001566 // We might be deleting a pointer to array. If so, GEP down to the
1567 // first non-array element.
1568 // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1569 QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1570 if (DeleteTy->isConstantArrayType()) {
1571 llvm::Value *Zero = Builder.getInt32(0);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001572 SmallVector<llvm::Value*,8> GEP;
John McCall1e7fe752010-09-02 09:58:18 +00001573
1574 GEP.push_back(Zero); // point at the outermost array
1575
1576 // For each layer of array type we're pointing at:
1577 while (const ConstantArrayType *Arr
1578 = getContext().getAsConstantArrayType(DeleteTy)) {
1579 // 1. Unpeel the array type.
1580 DeleteTy = Arr->getElementType();
1581
1582 // 2. GEP to the first element of the array.
1583 GEP.push_back(Zero);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001584 }
John McCall1e7fe752010-09-02 09:58:18 +00001585
Jay Foad0f6ac7c2011-07-22 08:16:57 +00001586 Ptr = Builder.CreateInBoundsGEP(Ptr, GEP, "del.first");
Anders Carlsson16d81b82009-09-22 22:53:17 +00001587 }
1588
Douglas Gregoreede61a2010-09-02 17:38:50 +00001589 assert(ConvertTypeForMem(DeleteTy) ==
1590 cast<llvm::PointerType>(Ptr->getType())->getElementType());
John McCall1e7fe752010-09-02 09:58:18 +00001591
1592 if (E->isArrayForm()) {
John McCall6ec278d2011-01-27 09:37:56 +00001593 EmitArrayDelete(*this, E, Ptr, DeleteTy);
John McCall1e7fe752010-09-02 09:58:18 +00001594 } else {
Douglas Gregora8b20f72011-07-13 00:54:47 +00001595 EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy,
1596 E->isGlobalDelete());
John McCall1e7fe752010-09-02 09:58:18 +00001597 }
Anders Carlsson16d81b82009-09-22 22:53:17 +00001598
Anders Carlsson16d81b82009-09-22 22:53:17 +00001599 EmitBlock(DeleteEnd);
1600}
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001601
Anders Carlsson4bdbc0c2011-04-11 14:13:40 +00001602static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1603 // void __cxa_bad_typeid();
Chris Lattner8b418682012-02-07 00:39:47 +00001604 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
Anders Carlsson4bdbc0c2011-04-11 14:13:40 +00001605
1606 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1607}
1608
1609static void EmitBadTypeidCall(CodeGenFunction &CGF) {
Anders Carlssonad3692bb2011-04-13 02:35:36 +00001610 llvm::Value *Fn = getBadTypeidFn(CGF);
Jay Foad4c7d9f12011-07-15 08:37:34 +00001611 CGF.EmitCallOrInvoke(Fn).setDoesNotReturn();
Anders Carlsson4bdbc0c2011-04-11 14:13:40 +00001612 CGF.Builder.CreateUnreachable();
1613}
1614
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001615static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF,
1616 const Expr *E,
Chris Lattner2acc6e32011-07-18 04:24:23 +00001617 llvm::Type *StdTypeInfoPtrTy) {
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001618 // Get the vtable pointer.
1619 llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress();
1620
1621 // C++ [expr.typeid]p2:
1622 // If the glvalue expression is obtained by applying the unary * operator to
1623 // a pointer and the pointer is a null pointer value, the typeid expression
1624 // throws the std::bad_typeid exception.
1625 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) {
1626 if (UO->getOpcode() == UO_Deref) {
1627 llvm::BasicBlock *BadTypeidBlock =
1628 CGF.createBasicBlock("typeid.bad_typeid");
1629 llvm::BasicBlock *EndBlock =
1630 CGF.createBasicBlock("typeid.end");
1631
1632 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr);
1633 CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock);
1634
1635 CGF.EmitBlock(BadTypeidBlock);
1636 EmitBadTypeidCall(CGF);
1637 CGF.EmitBlock(EndBlock);
1638 }
1639 }
1640
1641 llvm::Value *Value = CGF.GetVTablePtr(ThisPtr,
1642 StdTypeInfoPtrTy->getPointerTo());
1643
1644 // Load the type info.
1645 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
1646 return CGF.Builder.CreateLoad(Value);
1647}
1648
John McCall3ad32c82011-01-28 08:37:24 +00001649llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00001650 llvm::Type *StdTypeInfoPtrTy =
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001651 ConvertType(E->getType())->getPointerTo();
Anders Carlsson31b7f522009-12-11 02:46:30 +00001652
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001653 if (E->isTypeOperand()) {
1654 llvm::Constant *TypeInfo =
1655 CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001656 return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy);
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001657 }
Anders Carlsson4bdbc0c2011-04-11 14:13:40 +00001658
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001659 // C++ [expr.typeid]p2:
1660 // When typeid is applied to a glvalue expression whose type is a
1661 // polymorphic class type, the result refers to a std::type_info object
1662 // representing the type of the most derived object (that is, the dynamic
1663 // type) to which the glvalue refers.
Richard Smith0d729102012-08-13 20:08:14 +00001664 if (E->isPotentiallyEvaluated())
1665 return EmitTypeidFromVTable(*this, E->getExprOperand(),
1666 StdTypeInfoPtrTy);
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001667
1668 QualType OperandTy = E->getExprOperand()->getType();
1669 return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy),
1670 StdTypeInfoPtrTy);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001671}
Mike Stumpc849c052009-11-16 06:50:58 +00001672
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001673static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) {
1674 // void *__dynamic_cast(const void *sub,
1675 // const abi::__class_type_info *src,
1676 // const abi::__class_type_info *dst,
1677 // std::ptrdiff_t src2dst_offset);
1678
Chris Lattner8b418682012-02-07 00:39:47 +00001679 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001680 llvm::Type *PtrDiffTy =
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001681 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1682
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001683 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001684
Chris Lattner2acc6e32011-07-18 04:24:23 +00001685 llvm::FunctionType *FTy =
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001686 llvm::FunctionType::get(Int8PtrTy, Args, false);
1687
1688 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast");
1689}
1690
1691static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1692 // void __cxa_bad_cast();
Chris Lattner8b418682012-02-07 00:39:47 +00001693 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001694 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1695}
1696
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001697static void EmitBadCastCall(CodeGenFunction &CGF) {
Anders Carlssonad3692bb2011-04-13 02:35:36 +00001698 llvm::Value *Fn = getBadCastFn(CGF);
Jay Foad4c7d9f12011-07-15 08:37:34 +00001699 CGF.EmitCallOrInvoke(Fn).setDoesNotReturn();
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001700 CGF.Builder.CreateUnreachable();
1701}
1702
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001703static llvm::Value *
1704EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
1705 QualType SrcTy, QualType DestTy,
1706 llvm::BasicBlock *CastEnd) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00001707 llvm::Type *PtrDiffLTy =
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001708 CGF.ConvertType(CGF.getContext().getPointerDiffType());
Chris Lattner2acc6e32011-07-18 04:24:23 +00001709 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001710
1711 if (const PointerType *PTy = DestTy->getAs<PointerType>()) {
1712 if (PTy->getPointeeType()->isVoidType()) {
1713 // C++ [expr.dynamic.cast]p7:
1714 // If T is "pointer to cv void," then the result is a pointer to the
1715 // most derived object pointed to by v.
1716
1717 // Get the vtable pointer.
1718 llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo());
1719
1720 // Get the offset-to-top from the vtable.
1721 llvm::Value *OffsetToTop =
1722 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
1723 OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top");
1724
1725 // Finally, add the offset to the pointer.
1726 Value = CGF.EmitCastToVoidPtr(Value);
1727 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1728
1729 return CGF.Builder.CreateBitCast(Value, DestLTy);
1730 }
1731 }
1732
1733 QualType SrcRecordTy;
1734 QualType DestRecordTy;
1735
1736 if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) {
1737 SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType();
1738 DestRecordTy = DestPTy->getPointeeType();
1739 } else {
1740 SrcRecordTy = SrcTy;
1741 DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType();
1742 }
1743
1744 assert(SrcRecordTy->isRecordType() && "source type must be a record type!");
1745 assert(DestRecordTy->isRecordType() && "dest type must be a record type!");
1746
1747 llvm::Value *SrcRTTI =
1748 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1749 llvm::Value *DestRTTI =
1750 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1751
1752 // FIXME: Actually compute a hint here.
1753 llvm::Value *OffsetHint = llvm::ConstantInt::get(PtrDiffLTy, -1ULL);
1754
1755 // Emit the call to __dynamic_cast.
1756 Value = CGF.EmitCastToVoidPtr(Value);
1757 Value = CGF.Builder.CreateCall4(getDynamicCastFn(CGF), Value,
1758 SrcRTTI, DestRTTI, OffsetHint);
1759 Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1760
1761 /// C++ [expr.dynamic.cast]p9:
1762 /// A failed cast to reference type throws std::bad_cast
1763 if (DestTy->isReferenceType()) {
1764 llvm::BasicBlock *BadCastBlock =
1765 CGF.createBasicBlock("dynamic_cast.bad_cast");
1766
1767 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1768 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1769
1770 CGF.EmitBlock(BadCastBlock);
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001771 EmitBadCastCall(CGF);
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001772 }
1773
1774 return Value;
1775}
1776
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001777static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF,
1778 QualType DestTy) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00001779 llvm::Type *DestLTy = CGF.ConvertType(DestTy);
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001780 if (DestTy->isPointerType())
1781 return llvm::Constant::getNullValue(DestLTy);
1782
1783 /// C++ [expr.dynamic.cast]p9:
1784 /// A failed cast to reference type throws std::bad_cast
1785 EmitBadCastCall(CGF);
1786
1787 CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end"));
1788 return llvm::UndefValue::get(DestLTy);
1789}
1790
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001791llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value,
Mike Stumpc849c052009-11-16 06:50:58 +00001792 const CXXDynamicCastExpr *DCE) {
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001793 QualType DestTy = DCE->getTypeAsWritten();
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001794
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001795 if (DCE->isAlwaysNull())
1796 return EmitDynamicCastToNull(*this, DestTy);
1797
1798 QualType SrcTy = DCE->getSubExpr()->getType();
1799
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001800 // C++ [expr.dynamic.cast]p4:
1801 // If the value of v is a null pointer value in the pointer case, the result
1802 // is the null pointer value of type T.
1803 bool ShouldNullCheckSrcValue = SrcTy->isPointerType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001804
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001805 llvm::BasicBlock *CastNull = 0;
1806 llvm::BasicBlock *CastNotNull = 0;
1807 llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end");
Mike Stumpc849c052009-11-16 06:50:58 +00001808
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001809 if (ShouldNullCheckSrcValue) {
1810 CastNull = createBasicBlock("dynamic_cast.null");
1811 CastNotNull = createBasicBlock("dynamic_cast.notnull");
1812
1813 llvm::Value *IsNull = Builder.CreateIsNull(Value);
1814 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
1815 EmitBlock(CastNotNull);
Mike Stumpc849c052009-11-16 06:50:58 +00001816 }
1817
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001818 Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd);
1819
1820 if (ShouldNullCheckSrcValue) {
1821 EmitBranch(CastEnd);
1822
1823 EmitBlock(CastNull);
1824 EmitBranch(CastEnd);
1825 }
1826
1827 EmitBlock(CastEnd);
1828
1829 if (ShouldNullCheckSrcValue) {
1830 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
1831 PHI->addIncoming(Value, CastNotNull);
1832 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
1833
1834 Value = PHI;
1835 }
1836
1837 return Value;
Mike Stumpc849c052009-11-16 06:50:58 +00001838}
Eli Friedman4c5d8af2012-02-09 03:32:31 +00001839
Eli Friedman4c5d8af2012-02-09 03:32:31 +00001840void CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) {
Eli Friedmanf8823e72012-02-09 03:47:20 +00001841 RunCleanupsScope Scope(*this);
Eli Friedman377ecc72012-04-16 03:54:45 +00001842 LValue SlotLV = MakeAddrLValue(Slot.getAddr(), E->getType(),
1843 Slot.getAlignment());
Eli Friedmanf8823e72012-02-09 03:47:20 +00001844
Eli Friedman4c5d8af2012-02-09 03:32:31 +00001845 CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin();
1846 for (LambdaExpr::capture_init_iterator i = E->capture_init_begin(),
1847 e = E->capture_init_end();
Eric Christopherc07b18e2012-02-29 03:25:18 +00001848 i != e; ++i, ++CurField) {
Eli Friedman4c5d8af2012-02-09 03:32:31 +00001849 // Emit initialization
Eli Friedman377ecc72012-04-16 03:54:45 +00001850
David Blaikie581deb32012-06-06 20:45:41 +00001851 LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField);
Eli Friedmanb74ed082012-02-14 02:31:03 +00001852 ArrayRef<VarDecl *> ArrayIndexes;
1853 if (CurField->getType()->isArrayType())
1854 ArrayIndexes = E->getCaptureInitIndexVars(i);
David Blaikie581deb32012-06-06 20:45:41 +00001855 EmitInitializerForField(*CurField, LV, *i, ArrayIndexes);
Eli Friedman4c5d8af2012-02-09 03:32:31 +00001856 }
Eli Friedman4c5d8af2012-02-09 03:32:31 +00001857}