blob: 228d9c6c5e6d9f7c6a0ad10a9ae97a749cfc5113 [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"
John McCall4c40d982010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Fariborz Jahanian842ddd02010-05-20 21:38:57 +000017#include "CGObjCRuntime.h"
Devang Patelc69e1cf2010-09-30 19:05:55 +000018#include "CGDebugInfo.h"
Chris Lattner6c552c12010-07-20 20:19:24 +000019#include "llvm/Intrinsics.h"
Anders Carlssonad3692bb2011-04-13 02:35:36 +000020#include "llvm/Support/CallSite.h"
21
Anders Carlsson16d81b82009-09-22 22:53:17 +000022using namespace clang;
23using namespace CodeGen;
24
Anders Carlsson3b5ad222010-01-01 20:29:01 +000025RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
26 llvm::Value *Callee,
27 ReturnValueSlot ReturnValue,
28 llvm::Value *This,
Anders Carlssonc997d422010-01-02 01:01:18 +000029 llvm::Value *VTT,
Anders Carlsson3b5ad222010-01-01 20:29:01 +000030 CallExpr::const_arg_iterator ArgBeg,
31 CallExpr::const_arg_iterator ArgEnd) {
32 assert(MD->isInstance() &&
33 "Trying to emit a member call expr on a static method!");
34
35 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
36
37 CallArgList Args;
38
39 // Push the this ptr.
40 Args.push_back(std::make_pair(RValue::get(This),
41 MD->getThisType(getContext())));
42
Anders Carlssonc997d422010-01-02 01:01:18 +000043 // If there is a VTT parameter, emit it.
44 if (VTT) {
45 QualType T = getContext().getPointerType(getContext().VoidPtrTy);
46 Args.push_back(std::make_pair(RValue::get(VTT), T));
47 }
48
Anders Carlsson3b5ad222010-01-01 20:29:01 +000049 // And the rest of the call args
50 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
51
John McCall04a67a62010-02-05 21:31:56 +000052 QualType ResultType = FPT->getResultType();
Tilmann Scheller9c6082f2011-03-02 21:36:49 +000053 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
54 FPT->getExtInfo()),
Rafael Espindola264ba482010-03-30 20:24:48 +000055 Callee, ReturnValue, Args, MD);
Anders Carlsson3b5ad222010-01-01 20:29:01 +000056}
57
Anders Carlsson1679f5a2011-01-29 03:52:01 +000058static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) {
Anders Carlsson268ab8c2011-01-29 05:04:11 +000059 const Expr *E = Base;
60
61 while (true) {
62 E = E->IgnoreParens();
63 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
64 if (CE->getCastKind() == CK_DerivedToBase ||
65 CE->getCastKind() == CK_UncheckedDerivedToBase ||
66 CE->getCastKind() == CK_NoOp) {
67 E = CE->getSubExpr();
68 continue;
69 }
70 }
71
72 break;
73 }
74
75 QualType DerivedType = E->getType();
Anders Carlsson1679f5a2011-01-29 03:52:01 +000076 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
77 DerivedType = PTy->getPointeeType();
78
79 return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl());
80}
81
Anders Carlssoncd0b32e2011-04-10 18:20:53 +000082// FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
83// quite what we want.
84static const Expr *skipNoOpCastsAndParens(const Expr *E) {
85 while (true) {
86 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
87 E = PE->getSubExpr();
88 continue;
89 }
90
91 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
92 if (CE->getCastKind() == CK_NoOp) {
93 E = CE->getSubExpr();
94 continue;
95 }
96 }
97 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
98 if (UO->getOpcode() == UO_Extension) {
99 E = UO->getSubExpr();
100 continue;
101 }
102 }
103 return E;
104 }
105}
106
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000107/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
108/// expr can be devirtualized.
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +0000109static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context,
110 const Expr *Base,
Anders Carlssonbd2bfae2010-10-27 13:28:46 +0000111 const CXXMethodDecl *MD) {
112
Anders Carlsson1679f5a2011-01-29 03:52:01 +0000113 // When building with -fapple-kext, all calls must go through the vtable since
114 // the kernel linker can do runtime patching of vtables.
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +0000115 if (Context.getLangOptions().AppleKext)
116 return false;
117
Anders Carlsson1679f5a2011-01-29 03:52:01 +0000118 // If the most derived class is marked final, we know that no subclass can
119 // override this member function and so we can devirtualize it. For example:
120 //
121 // struct A { virtual void f(); }
122 // struct B final : A { };
123 //
124 // void f(B *b) {
125 // b->f();
126 // }
127 //
128 const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base);
129 if (MostDerivedClassDecl->hasAttr<FinalAttr>())
130 return true;
131
Anders Carlssonf89e0422011-01-23 21:07:30 +0000132 // If the member function is marked 'final', we know that it can't be
Anders Carlssond66f4282010-10-27 13:34:43 +0000133 // overridden and can therefore devirtualize it.
Anders Carlssoncb88a1f2011-01-24 16:26:15 +0000134 if (MD->hasAttr<FinalAttr>())
Anders Carlssonbd2bfae2010-10-27 13:28:46 +0000135 return true;
Anders Carlssond66f4282010-10-27 13:34:43 +0000136
Anders Carlssonf89e0422011-01-23 21:07:30 +0000137 // Similarly, if the class itself is marked 'final' it can't be overridden
138 // and we can therefore devirtualize the member function call.
Anders Carlssoncb88a1f2011-01-24 16:26:15 +0000139 if (MD->getParent()->hasAttr<FinalAttr>())
Anders Carlssond66f4282010-10-27 13:34:43 +0000140 return true;
141
Anders Carlssoncd0b32e2011-04-10 18:20:53 +0000142 Base = skipNoOpCastsAndParens(Base);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000143 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
144 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
145 // This is a record decl. We know the type and can devirtualize it.
146 return VD->getType()->isRecordType();
147 }
148
149 return false;
150 }
151
152 // We can always devirtualize calls on temporary object expressions.
Eli Friedman6997aae2010-01-31 20:58:15 +0000153 if (isa<CXXConstructExpr>(Base))
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000154 return true;
155
156 // And calls on bound temporaries.
157 if (isa<CXXBindTemporaryExpr>(Base))
158 return true;
159
160 // Check if this is a call expr that returns a record type.
161 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
162 return CE->getCallReturnType()->isRecordType();
Anders Carlssonbd2bfae2010-10-27 13:28:46 +0000163
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000164 // We can't devirtualize the call.
165 return false;
166}
167
Francois Pichetdbee3412011-01-18 05:04:39 +0000168// Note: This function also emit constructor calls to support a MSVC
169// extensions allowing explicit constructor function call.
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000170RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
171 ReturnValueSlot ReturnValue) {
John McCall379b5152011-04-11 07:02:50 +0000172 const Expr *callee = CE->getCallee()->IgnoreParens();
173
174 if (isa<BinaryOperator>(callee))
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000175 return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
John McCall379b5152011-04-11 07:02:50 +0000176
177 const MemberExpr *ME = cast<MemberExpr>(callee);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000178 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
179
Devang Patelc69e1cf2010-09-30 19:05:55 +0000180 CGDebugInfo *DI = getDebugInfo();
Devang Patel68020272010-10-22 18:56:27 +0000181 if (DI && CGM.getCodeGenOpts().LimitDebugInfo
182 && !isa<CallExpr>(ME->getBase())) {
Devang Patelc69e1cf2010-09-30 19:05:55 +0000183 QualType PQTy = ME->getBase()->IgnoreParenImpCasts()->getType();
184 if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) {
185 DI->getOrCreateRecordType(PTy->getPointeeType(),
186 MD->getParent()->getLocation());
187 }
188 }
189
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000190 if (MD->isStatic()) {
191 // The method is static, emit it as we would a regular call.
192 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
193 return EmitCall(getContext().getPointerType(MD->getType()), Callee,
194 ReturnValue, CE->arg_begin(), CE->arg_end());
195 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000196
John McCallfc400282010-09-03 01:26:39 +0000197 // Compute the object pointer.
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000198 llvm::Value *This;
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000199 if (ME->isArrow())
200 This = EmitScalarExpr(ME->getBase());
John McCall0e800c92010-12-04 08:14:53 +0000201 else
202 This = EmitLValue(ME->getBase()).getAddress();
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000203
John McCallfc400282010-09-03 01:26:39 +0000204 if (MD->isTrivial()) {
205 if (isa<CXXDestructorDecl>(MD)) return RValue::get(0);
Francois Pichetdbee3412011-01-18 05:04:39 +0000206 if (isa<CXXConstructorDecl>(MD) &&
207 cast<CXXConstructorDecl>(MD)->isDefaultConstructor())
208 return RValue::get(0);
John McCallfc400282010-09-03 01:26:39 +0000209
Francois Pichetdbee3412011-01-18 05:04:39 +0000210 if (MD->isCopyAssignmentOperator()) {
211 // We don't like to generate the trivial copy assignment operator when
212 // it isn't necessary; just produce the proper effect here.
213 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
214 EmitAggregateCopy(This, RHS, CE->getType());
215 return RValue::get(This);
216 }
217
218 if (isa<CXXConstructorDecl>(MD) &&
219 cast<CXXConstructorDecl>(MD)->isCopyConstructor()) {
220 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
221 EmitSynthesizedCXXCopyCtorCall(cast<CXXConstructorDecl>(MD), This, RHS,
222 CE->arg_begin(), CE->arg_end());
223 return RValue::get(This);
224 }
225 llvm_unreachable("unknown trivial member function");
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000226 }
227
John McCallfc400282010-09-03 01:26:39 +0000228 // Compute the function type we're calling.
Francois Pichetdbee3412011-01-18 05:04:39 +0000229 const CGFunctionInfo *FInfo = 0;
230 if (isa<CXXDestructorDecl>(MD))
231 FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
232 Dtor_Complete);
233 else if (isa<CXXConstructorDecl>(MD))
234 FInfo = &CGM.getTypes().getFunctionInfo(cast<CXXConstructorDecl>(MD),
235 Ctor_Complete);
236 else
237 FInfo = &CGM.getTypes().getFunctionInfo(MD);
John McCallfc400282010-09-03 01:26:39 +0000238
239 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
240 const llvm::Type *Ty
Francois Pichetdbee3412011-01-18 05:04:39 +0000241 = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic());
John McCallfc400282010-09-03 01:26:39 +0000242
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000243 // C++ [class.virtual]p12:
244 // Explicit qualification with the scope operator (5.1) suppresses the
245 // virtual call mechanism.
246 //
247 // We also don't emit a virtual call if the base expression has a record type
248 // because then we know what the type is.
Fariborz Jahanian27262672011-01-20 17:19:02 +0000249 bool UseVirtualCall;
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +0000250 UseVirtualCall = MD->isVirtual() && !ME->hasQualifier()
251 && !canDevirtualizeMemberFunctionCalls(getContext(),
252 ME->getBase(), MD);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000253 llvm::Value *Callee;
John McCallfc400282010-09-03 01:26:39 +0000254 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
255 if (UseVirtualCall) {
256 Callee = BuildVirtualCall(Dtor, Dtor_Complete, This, Ty);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000257 } else {
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000258 if (getContext().getLangOptions().AppleKext &&
259 MD->isVirtual() &&
260 ME->hasQualifier())
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000261 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000262 else
263 Callee = CGM.GetAddrOfFunction(GlobalDecl(Dtor, Dtor_Complete), Ty);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000264 }
Francois Pichetdbee3412011-01-18 05:04:39 +0000265 } else if (const CXXConstructorDecl *Ctor =
266 dyn_cast<CXXConstructorDecl>(MD)) {
267 Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty);
John McCallfc400282010-09-03 01:26:39 +0000268 } else if (UseVirtualCall) {
Fariborz Jahanian27262672011-01-20 17:19:02 +0000269 Callee = BuildVirtualCall(MD, This, Ty);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000270 } else {
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +0000271 if (getContext().getLangOptions().AppleKext &&
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +0000272 MD->isVirtual() &&
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +0000273 ME->hasQualifier())
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000274 Callee = BuildAppleKextVirtualCall(MD, ME->getQualifier(), Ty);
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +0000275 else
276 Callee = CGM.GetAddrOfFunction(MD, Ty);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000277 }
278
Anders Carlssonc997d422010-01-02 01:01:18 +0000279 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000280 CE->arg_begin(), CE->arg_end());
281}
282
283RValue
284CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
285 ReturnValueSlot ReturnValue) {
286 const BinaryOperator *BO =
287 cast<BinaryOperator>(E->getCallee()->IgnoreParens());
288 const Expr *BaseExpr = BO->getLHS();
289 const Expr *MemFnExpr = BO->getRHS();
290
291 const MemberPointerType *MPT =
292 MemFnExpr->getType()->getAs<MemberPointerType>();
John McCall93d557b2010-08-22 00:05:51 +0000293
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000294 const FunctionProtoType *FPT =
295 MPT->getPointeeType()->getAs<FunctionProtoType>();
296 const CXXRecordDecl *RD =
297 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
298
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000299 // Get the member function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000300 llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000301
302 // Emit the 'this' pointer.
303 llvm::Value *This;
304
John McCall2de56d12010-08-25 11:45:40 +0000305 if (BO->getOpcode() == BO_PtrMemI)
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000306 This = EmitScalarExpr(BaseExpr);
307 else
308 This = EmitLValue(BaseExpr).getAddress();
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000309
John McCall93d557b2010-08-22 00:05:51 +0000310 // Ask the ABI to load the callee. Note that This is modified.
311 llvm::Value *Callee =
John McCalld16c2cf2011-02-08 08:22:06 +0000312 CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, This, MemFnPtr, MPT);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000313
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000314 CallArgList Args;
315
316 QualType ThisType =
317 getContext().getPointerType(getContext().getTagDeclType(RD));
318
319 // Push the this ptr.
320 Args.push_back(std::make_pair(RValue::get(This), ThisType));
321
322 // And the rest of the call args
323 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
John McCall04a67a62010-02-05 21:31:56 +0000324 const FunctionType *BO_FPT = BO->getType()->getAs<FunctionProtoType>();
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000325 return EmitCall(CGM.getTypes().getFunctionInfo(Args, BO_FPT), Callee,
326 ReturnValue, Args);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000327}
328
329RValue
330CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
331 const CXXMethodDecl *MD,
332 ReturnValueSlot ReturnValue) {
333 assert(MD->isInstance() &&
334 "Trying to emit a member call expr on a static method!");
John McCall0e800c92010-12-04 08:14:53 +0000335 LValue LV = EmitLValue(E->getArg(0));
336 llvm::Value *This = LV.getAddress();
337
Douglas Gregor3e9438b2010-09-27 22:37:28 +0000338 if (MD->isCopyAssignmentOperator()) {
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000339 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
340 if (ClassDecl->hasTrivialCopyAssignment()) {
341 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
342 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000343 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
344 QualType Ty = E->getType();
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000345 EmitAggregateCopy(This, Src, Ty);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000346 return RValue::get(This);
347 }
348 }
349
350 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
351 const llvm::Type *Ty =
352 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
353 FPT->isVariadic());
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000354 llvm::Value *Callee;
Fariborz Jahanian27262672011-01-20 17:19:02 +0000355 if (MD->isVirtual() &&
Fariborz Jahanian7ac0ff22011-01-21 01:04:41 +0000356 !canDevirtualizeMemberFunctionCalls(getContext(),
357 E->getArg(0), MD))
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000358 Callee = BuildVirtualCall(MD, This, Ty);
359 else
360 Callee = CGM.GetAddrOfFunction(MD, Ty);
361
Anders Carlssonc997d422010-01-02 01:01:18 +0000362 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, /*VTT=*/0,
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000363 E->arg_begin() + 1, E->arg_end());
364}
365
366void
John McCall558d2ab2010-09-15 10:14:12 +0000367CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
368 AggValueSlot Dest) {
369 assert(!Dest.isIgnored() && "Must have a destination!");
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000370 const CXXConstructorDecl *CD = E->getConstructor();
Douglas Gregor759e41b2010-08-22 16:15:35 +0000371
372 // If we require zero initialization before (or instead of) calling the
373 // constructor, as can be the case with a non-user-provided default
374 // constructor, emit the zero initialization now.
375 if (E->requiresZeroInitialization())
John McCall558d2ab2010-09-15 10:14:12 +0000376 EmitNullInitialization(Dest.getAddr(), E->getType());
Douglas Gregor759e41b2010-08-22 16:15:35 +0000377
378 // If this is a call to a trivial default constructor, do nothing.
379 if (CD->isTrivial() && CD->isDefaultConstructor())
380 return;
381
John McCallfc1e6c72010-09-18 00:58:34 +0000382 // Elide the constructor if we're constructing from a temporary.
383 // The temporary check is required because Sema sets this on NRVO
384 // returns.
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000385 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
John McCallfc1e6c72010-09-18 00:58:34 +0000386 assert(getContext().hasSameUnqualifiedType(E->getType(),
387 E->getArg(0)->getType()));
John McCall558d2ab2010-09-15 10:14:12 +0000388 if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
389 EmitAggExpr(E->getArg(0), Dest);
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000390 return;
391 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000392 }
Douglas Gregor759e41b2010-08-22 16:15:35 +0000393
394 const ConstantArrayType *Array
395 = getContext().getAsConstantArrayType(E->getType());
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000396 if (Array) {
397 QualType BaseElementTy = getContext().getBaseElementType(Array);
398 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
399 BasePtr = llvm::PointerType::getUnqual(BasePtr);
400 llvm::Value *BaseAddrPtr =
John McCall558d2ab2010-09-15 10:14:12 +0000401 Builder.CreateBitCast(Dest.getAddr(), BasePtr);
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000402
403 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
404 E->arg_begin(), E->arg_end());
405 }
Anders Carlsson155ed4a2010-05-02 23:20:53 +0000406 else {
407 CXXCtorType Type =
408 (E->getConstructionKind() == CXXConstructExpr::CK_Complete)
409 ? Ctor_Complete : Ctor_Base;
410 bool ForVirtualBase =
411 E->getConstructionKind() == CXXConstructExpr::CK_VirtualBase;
412
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000413 // Call the constructor.
John McCall558d2ab2010-09-15 10:14:12 +0000414 EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(),
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000415 E->arg_begin(), E->arg_end());
Anders Carlsson155ed4a2010-05-02 23:20:53 +0000416 }
Anders Carlsson3b5ad222010-01-01 20:29:01 +0000417}
418
Fariborz Jahanian34999872010-11-13 21:53:34 +0000419void
420CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest,
421 llvm::Value *Src,
Fariborz Jahanian830937b2010-12-02 17:02:11 +0000422 const Expr *Exp) {
John McCall4765fa02010-12-06 08:20:24 +0000423 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp))
Fariborz Jahanian34999872010-11-13 21:53:34 +0000424 Exp = E->getSubExpr();
425 assert(isa<CXXConstructExpr>(Exp) &&
426 "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr");
427 const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp);
428 const CXXConstructorDecl *CD = E->getConstructor();
429 RunCleanupsScope Scope(*this);
430
431 // If we require zero initialization before (or instead of) calling the
432 // constructor, as can be the case with a non-user-provided default
433 // constructor, emit the zero initialization now.
434 // FIXME. Do I still need this for a copy ctor synthesis?
435 if (E->requiresZeroInitialization())
436 EmitNullInitialization(Dest, E->getType());
437
Chandler Carruth858a5462010-11-15 13:54:43 +0000438 assert(!getContext().getAsConstantArrayType(E->getType())
439 && "EmitSynthesizedCXXCopyCtor - Copied-in Array");
Fariborz Jahanian34999872010-11-13 21:53:34 +0000440 EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src,
441 E->arg_begin(), E->arg_end());
442}
443
John McCall5172ed92010-08-23 01:17:59 +0000444/// Check whether the given operator new[] is the global placement
445/// operator new[].
446static bool IsPlacementOperatorNewArray(ASTContext &Ctx,
447 const FunctionDecl *Fn) {
448 // Must be in global scope. Note that allocation functions can't be
449 // declared in namespaces.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000450 if (!Fn->getDeclContext()->getRedeclContext()->isFileContext())
John McCall5172ed92010-08-23 01:17:59 +0000451 return false;
452
453 // Signature must be void *operator new[](size_t, void*).
454 // The size_t is common to all operator new[]s.
455 if (Fn->getNumParams() != 2)
456 return false;
457
458 CanQualType ParamType = Ctx.getCanonicalType(Fn->getParamDecl(1)->getType());
459 return (ParamType == Ctx.VoidPtrTy);
460}
461
John McCall1e7fe752010-09-02 09:58:18 +0000462static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
463 const CXXNewExpr *E) {
Anders Carlsson871d0782009-12-13 20:04:38 +0000464 if (!E->isArray())
Ken Dyckcaf647c2010-01-26 19:44:24 +0000465 return CharUnits::Zero();
Anders Carlsson871d0782009-12-13 20:04:38 +0000466
Anders Carlssondd937552009-12-13 20:34:34 +0000467 // No cookie is required if the new operator being used is
468 // ::operator new[](size_t, void*).
469 const FunctionDecl *OperatorNew = E->getOperatorNew();
John McCall1e7fe752010-09-02 09:58:18 +0000470 if (IsPlacementOperatorNewArray(CGF.getContext(), OperatorNew))
John McCall5172ed92010-08-23 01:17:59 +0000471 return CharUnits::Zero();
472
John McCall6ec278d2011-01-27 09:37:56 +0000473 return CGF.CGM.getCXXABI().GetArrayCookieSize(E);
Anders Carlssona4d4c012009-09-23 16:07:23 +0000474}
475
Fariborz Jahanianceb43b62010-03-24 16:57:01 +0000476static llvm::Value *EmitCXXNewAllocSize(ASTContext &Context,
Chris Lattnerdefe8b22010-07-20 18:45:57 +0000477 CodeGenFunction &CGF,
Anders Carlssona4d4c012009-09-23 16:07:23 +0000478 const CXXNewExpr *E,
Douglas Gregor59174c02010-07-21 01:10:17 +0000479 llvm::Value *&NumElements,
480 llvm::Value *&SizeWithoutCookie) {
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000481 QualType ElemType = E->getAllocatedType();
John McCall1e7fe752010-09-02 09:58:18 +0000482
483 const llvm::IntegerType *SizeTy =
484 cast<llvm::IntegerType>(CGF.ConvertType(CGF.getContext().getSizeType()));
Anders Carlssona4d4c012009-09-23 16:07:23 +0000485
John McCall1e7fe752010-09-02 09:58:18 +0000486 CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(ElemType);
487
Douglas Gregor59174c02010-07-21 01:10:17 +0000488 if (!E->isArray()) {
489 SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
490 return SizeWithoutCookie;
491 }
Anders Carlssona4d4c012009-09-23 16:07:23 +0000492
John McCall1e7fe752010-09-02 09:58:18 +0000493 // Figure out the cookie size.
494 CharUnits CookieSize = CalculateCookiePadding(CGF, E);
495
Anders Carlssona4d4c012009-09-23 16:07:23 +0000496 // Emit the array size expression.
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000497 // We multiply the size of all dimensions for NumElements.
498 // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
Anders Carlssona4d4c012009-09-23 16:07:23 +0000499 NumElements = CGF.EmitScalarExpr(E->getArraySize());
John McCall1e7fe752010-09-02 09:58:18 +0000500 assert(NumElements->getType() == SizeTy && "element count not a size_t");
501
502 uint64_t ArraySizeMultiplier = 1;
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000503 while (const ConstantArrayType *CAT
504 = CGF.getContext().getAsConstantArrayType(ElemType)) {
505 ElemType = CAT->getElementType();
John McCall1e7fe752010-09-02 09:58:18 +0000506 ArraySizeMultiplier *= CAT->getSize().getZExtValue();
Argyrios Kyrtzidise7ab92e2010-08-26 15:23:38 +0000507 }
508
John McCall1e7fe752010-09-02 09:58:18 +0000509 llvm::Value *Size;
Chris Lattner83252dc2010-07-20 21:07:09 +0000510
Chris Lattner806941e2010-07-20 21:55:52 +0000511 // If someone is doing 'new int[42]' there is no need to do a dynamic check.
512 // Don't bloat the -O0 code.
513 if (llvm::ConstantInt *NumElementsC =
514 dyn_cast<llvm::ConstantInt>(NumElements)) {
Chris Lattner806941e2010-07-20 21:55:52 +0000515 llvm::APInt NEC = NumElementsC->getValue();
John McCall1e7fe752010-09-02 09:58:18 +0000516 unsigned SizeWidth = NEC.getBitWidth();
517
518 // Determine if there is an overflow here by doing an extended multiply.
Jay Foad9f71a8f2010-12-07 08:25:34 +0000519 NEC = NEC.zext(SizeWidth*2);
John McCall1e7fe752010-09-02 09:58:18 +0000520 llvm::APInt SC(SizeWidth*2, TypeSize.getQuantity());
Chris Lattner806941e2010-07-20 21:55:52 +0000521 SC *= NEC;
John McCall1e7fe752010-09-02 09:58:18 +0000522
523 if (!CookieSize.isZero()) {
524 // Save the current size without a cookie. We don't care if an
525 // overflow's already happened because SizeWithoutCookie isn't
526 // used if the allocator returns null or throws, as it should
527 // always do on an overflow.
Jay Foad9f71a8f2010-12-07 08:25:34 +0000528 llvm::APInt SWC = SC.trunc(SizeWidth);
John McCall1e7fe752010-09-02 09:58:18 +0000529 SizeWithoutCookie = llvm::ConstantInt::get(SizeTy, SWC);
530
531 // Add the cookie size.
532 SC += llvm::APInt(SizeWidth*2, CookieSize.getQuantity());
Chris Lattner806941e2010-07-20 21:55:52 +0000533 }
534
John McCall1e7fe752010-09-02 09:58:18 +0000535 if (SC.countLeadingZeros() >= SizeWidth) {
Jay Foad9f71a8f2010-12-07 08:25:34 +0000536 SC = SC.trunc(SizeWidth);
John McCall1e7fe752010-09-02 09:58:18 +0000537 Size = llvm::ConstantInt::get(SizeTy, SC);
538 } else {
539 // On overflow, produce a -1 so operator new throws.
540 Size = llvm::Constant::getAllOnesValue(SizeTy);
541 }
Anders Carlssona4d4c012009-09-23 16:07:23 +0000542
John McCall1e7fe752010-09-02 09:58:18 +0000543 // Scale NumElements while we're at it.
544 uint64_t N = NEC.getZExtValue() * ArraySizeMultiplier;
545 NumElements = llvm::ConstantInt::get(SizeTy, N);
546
547 // Otherwise, we don't need to do an overflow-checked multiplication if
548 // we're multiplying by one.
549 } else if (TypeSize.isOne()) {
550 assert(ArraySizeMultiplier == 1);
551
552 Size = NumElements;
553
554 // If we need a cookie, add its size in with an overflow check.
555 // This is maybe a little paranoid.
556 if (!CookieSize.isZero()) {
557 SizeWithoutCookie = Size;
558
559 llvm::Value *CookieSizeV
560 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
561
562 const llvm::Type *Types[] = { SizeTy };
563 llvm::Value *UAddF
564 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
565 llvm::Value *AddRes
566 = CGF.Builder.CreateCall2(UAddF, Size, CookieSizeV);
567
568 Size = CGF.Builder.CreateExtractValue(AddRes, 0);
569 llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
570 Size = CGF.Builder.CreateSelect(DidOverflow,
571 llvm::ConstantInt::get(SizeTy, -1),
572 Size);
573 }
574
575 // Otherwise use the int.umul.with.overflow intrinsic.
576 } else {
577 llvm::Value *OutermostElementSize
578 = llvm::ConstantInt::get(SizeTy, TypeSize.getQuantity());
579
580 llvm::Value *NumOutermostElements = NumElements;
581
582 // Scale NumElements by the array size multiplier. This might
583 // overflow, but only if the multiplication below also overflows,
584 // in which case this multiplication isn't used.
585 if (ArraySizeMultiplier != 1)
586 NumElements = CGF.Builder.CreateMul(NumElements,
587 llvm::ConstantInt::get(SizeTy, ArraySizeMultiplier));
588
589 // The requested size of the outermost array is non-constant.
590 // Multiply that by the static size of the elements of that array;
591 // on unsigned overflow, set the size to -1 to trigger an
592 // exception from the allocation routine. This is sufficient to
593 // prevent buffer overruns from the allocator returning a
594 // seemingly valid pointer to insufficient space. This idea comes
595 // originally from MSVC, and GCC has an open bug requesting
596 // similar behavior:
597 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19351
598 //
599 // This will not be sufficient for C++0x, which requires a
600 // specific exception class (std::bad_array_new_length).
601 // That will require ABI support that has not yet been specified.
602 const llvm::Type *Types[] = { SizeTy };
603 llvm::Value *UMulF
604 = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, Types, 1);
605 llvm::Value *MulRes = CGF.Builder.CreateCall2(UMulF, NumOutermostElements,
606 OutermostElementSize);
607
608 // The overflow bit.
609 llvm::Value *DidOverflow = CGF.Builder.CreateExtractValue(MulRes, 1);
610
611 // The result of the multiplication.
612 Size = CGF.Builder.CreateExtractValue(MulRes, 0);
613
614 // If we have a cookie, we need to add that size in, too.
615 if (!CookieSize.isZero()) {
616 SizeWithoutCookie = Size;
617
618 llvm::Value *CookieSizeV
619 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
620 llvm::Value *UAddF
621 = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, Types, 1);
622 llvm::Value *AddRes
623 = CGF.Builder.CreateCall2(UAddF, SizeWithoutCookie, CookieSizeV);
624
625 Size = CGF.Builder.CreateExtractValue(AddRes, 0);
626
627 llvm::Value *AddDidOverflow = CGF.Builder.CreateExtractValue(AddRes, 1);
Eli Friedman5536daa2011-04-09 19:54:33 +0000628 DidOverflow = CGF.Builder.CreateOr(DidOverflow, AddDidOverflow);
John McCall1e7fe752010-09-02 09:58:18 +0000629 }
630
631 Size = CGF.Builder.CreateSelect(DidOverflow,
632 llvm::ConstantInt::get(SizeTy, -1),
633 Size);
Chris Lattner806941e2010-07-20 21:55:52 +0000634 }
John McCall1e7fe752010-09-02 09:58:18 +0000635
636 if (CookieSize.isZero())
637 SizeWithoutCookie = Size;
638 else
639 assert(SizeWithoutCookie && "didn't set SizeWithoutCookie?");
640
Chris Lattner806941e2010-07-20 21:55:52 +0000641 return Size;
Anders Carlssona4d4c012009-09-23 16:07:23 +0000642}
643
Fariborz Jahanianef668722010-06-25 18:26:07 +0000644static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E,
645 llvm::Value *NewPtr) {
Fariborz Jahanianef668722010-06-25 18:26:07 +0000646
647 assert(E->getNumConstructorArgs() == 1 &&
648 "Can only have one argument to initializer of POD type.");
649
650 const Expr *Init = E->getConstructorArg(0);
651 QualType AllocType = E->getAllocatedType();
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000652
653 unsigned Alignment =
654 CGF.getContext().getTypeAlignInChars(AllocType).getQuantity();
Fariborz Jahanianef668722010-06-25 18:26:07 +0000655 if (!CGF.hasAggregateLLVMType(AllocType))
656 CGF.EmitStoreOfScalar(CGF.EmitScalarExpr(Init), NewPtr,
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000657 AllocType.isVolatileQualified(), Alignment,
658 AllocType);
Fariborz Jahanianef668722010-06-25 18:26:07 +0000659 else if (AllocType->isAnyComplexType())
660 CGF.EmitComplexExprIntoAddr(Init, NewPtr,
661 AllocType.isVolatileQualified());
John McCall558d2ab2010-09-15 10:14:12 +0000662 else {
663 AggValueSlot Slot
664 = AggValueSlot::forAddr(NewPtr, AllocType.isVolatileQualified(), true);
665 CGF.EmitAggExpr(Init, Slot);
666 }
Fariborz Jahanianef668722010-06-25 18:26:07 +0000667}
668
669void
670CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
671 llvm::Value *NewPtr,
672 llvm::Value *NumElements) {
Fariborz Jahanian5304c952010-06-25 20:01:13 +0000673 // We have a POD type.
674 if (E->getNumConstructorArgs() == 0)
675 return;
676
Fariborz Jahanianef668722010-06-25 18:26:07 +0000677 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
678
679 // Create a temporary for the loop index and initialize it with 0.
680 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
681 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
682 Builder.CreateStore(Zero, IndexPtr);
683
684 // Start the loop with a block that tests the condition.
685 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
686 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
687
688 EmitBlock(CondBlock);
689
690 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
691
692 // Generate: if (loop-index < number-of-elements fall to the loop body,
693 // otherwise, go to the block after the for-loop.
694 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
695 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
696 // If the condition is true, execute the body.
697 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
698
699 EmitBlock(ForBody);
700
701 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
702 // Inside the loop body, emit the constructor call on the array element.
703 Counter = Builder.CreateLoad(IndexPtr);
704 llvm::Value *Address = Builder.CreateInBoundsGEP(NewPtr, Counter,
705 "arrayidx");
706 StoreAnyExprIntoOneUnit(*this, E, Address);
707
708 EmitBlock(ContinueBlock);
709
710 // Emit the increment of the loop counter.
711 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
712 Counter = Builder.CreateLoad(IndexPtr);
713 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
714 Builder.CreateStore(NextVal, IndexPtr);
715
716 // Finally, branch back up to the condition for the next iteration.
717 EmitBranch(CondBlock);
718
719 // Emit the fall-through block.
720 EmitBlock(AfterFor, true);
721}
722
Douglas Gregor59174c02010-07-21 01:10:17 +0000723static void EmitZeroMemSet(CodeGenFunction &CGF, QualType T,
724 llvm::Value *NewPtr, llvm::Value *Size) {
John McCalld16c2cf2011-02-08 08:22:06 +0000725 CGF.EmitCastToVoidPtr(NewPtr);
Ken Dyckfe710082011-01-19 01:58:38 +0000726 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(T);
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000727 CGF.Builder.CreateMemSet(NewPtr, CGF.Builder.getInt8(0), Size,
Ken Dyckfe710082011-01-19 01:58:38 +0000728 Alignment.getQuantity(), false);
Douglas Gregor59174c02010-07-21 01:10:17 +0000729}
730
Anders Carlssona4d4c012009-09-23 16:07:23 +0000731static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
732 llvm::Value *NewPtr,
Douglas Gregor59174c02010-07-21 01:10:17 +0000733 llvm::Value *NumElements,
734 llvm::Value *AllocSizeWithoutCookie) {
Anders Carlsson5d4d9462009-11-24 18:43:52 +0000735 if (E->isArray()) {
Anders Carlssone99bdb62010-05-03 15:09:17 +0000736 if (CXXConstructorDecl *Ctor = E->getConstructor()) {
Douglas Gregor59174c02010-07-21 01:10:17 +0000737 bool RequiresZeroInitialization = false;
738 if (Ctor->getParent()->hasTrivialConstructor()) {
739 // If new expression did not specify value-initialization, then there
740 // is no initialization.
741 if (!E->hasInitializer() || Ctor->getParent()->isEmpty())
742 return;
743
John McCallf16aa102010-08-22 21:01:12 +0000744 if (CGF.CGM.getTypes().isZeroInitializable(E->getAllocatedType())) {
Douglas Gregor59174c02010-07-21 01:10:17 +0000745 // Optimization: since zero initialization will just set the memory
746 // to all zeroes, generate a single memset to do it in one shot.
747 EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
748 AllocSizeWithoutCookie);
749 return;
750 }
751
752 RequiresZeroInitialization = true;
753 }
754
755 CGF.EmitCXXAggrConstructorCall(Ctor, NumElements, NewPtr,
756 E->constructor_arg_begin(),
757 E->constructor_arg_end(),
758 RequiresZeroInitialization);
Anders Carlssone99bdb62010-05-03 15:09:17 +0000759 return;
Douglas Gregor59174c02010-07-21 01:10:17 +0000760 } else if (E->getNumConstructorArgs() == 1 &&
761 isa<ImplicitValueInitExpr>(E->getConstructorArg(0))) {
762 // Optimization: since zero initialization will just set the memory
763 // to all zeroes, generate a single memset to do it in one shot.
764 EmitZeroMemSet(CGF, E->getAllocatedType(), NewPtr,
765 AllocSizeWithoutCookie);
766 return;
767 } else {
Fariborz Jahanianef668722010-06-25 18:26:07 +0000768 CGF.EmitNewArrayInitializer(E, NewPtr, NumElements);
769 return;
770 }
Anders Carlssona4d4c012009-09-23 16:07:23 +0000771 }
Anders Carlsson5d4d9462009-11-24 18:43:52 +0000772
773 if (CXXConstructorDecl *Ctor = E->getConstructor()) {
Douglas Gregored8abf12010-07-08 06:14:04 +0000774 // Per C++ [expr.new]p15, if we have an initializer, then we're performing
775 // direct initialization. C++ [dcl.init]p5 requires that we
776 // zero-initialize storage if there are no user-declared constructors.
777 if (E->hasInitializer() &&
778 !Ctor->getParent()->hasUserDeclaredConstructor() &&
779 !Ctor->getParent()->isEmpty())
780 CGF.EmitNullInitialization(NewPtr, E->getAllocatedType());
781
Douglas Gregor84745672010-07-07 23:37:33 +0000782 CGF.EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false,
783 NewPtr, E->constructor_arg_begin(),
784 E->constructor_arg_end());
Anders Carlsson5d4d9462009-11-24 18:43:52 +0000785
786 return;
787 }
Fariborz Jahanian5304c952010-06-25 20:01:13 +0000788 // We have a POD type.
789 if (E->getNumConstructorArgs() == 0)
790 return;
791
Fariborz Jahanianef668722010-06-25 18:26:07 +0000792 StoreAnyExprIntoOneUnit(CGF, E, NewPtr);
Anders Carlssona4d4c012009-09-23 16:07:23 +0000793}
794
John McCall7d8647f2010-09-14 07:57:04 +0000795namespace {
796 /// A cleanup to call the given 'operator delete' function upon
797 /// abnormal exit from a new expression.
798 class CallDeleteDuringNew : public EHScopeStack::Cleanup {
799 size_t NumPlacementArgs;
800 const FunctionDecl *OperatorDelete;
801 llvm::Value *Ptr;
802 llvm::Value *AllocSize;
803
804 RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); }
805
806 public:
807 static size_t getExtraSize(size_t NumPlacementArgs) {
808 return NumPlacementArgs * sizeof(RValue);
809 }
810
811 CallDeleteDuringNew(size_t NumPlacementArgs,
812 const FunctionDecl *OperatorDelete,
813 llvm::Value *Ptr,
814 llvm::Value *AllocSize)
815 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
816 Ptr(Ptr), AllocSize(AllocSize) {}
817
818 void setPlacementArg(unsigned I, RValue Arg) {
819 assert(I < NumPlacementArgs && "index out of range");
820 getPlacementArgs()[I] = Arg;
821 }
822
823 void Emit(CodeGenFunction &CGF, bool IsForEH) {
824 const FunctionProtoType *FPT
825 = OperatorDelete->getType()->getAs<FunctionProtoType>();
826 assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
John McCallc3846362010-09-14 21:45:42 +0000827 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
John McCall7d8647f2010-09-14 07:57:04 +0000828
829 CallArgList DeleteArgs;
830
831 // The first argument is always a void*.
832 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
833 DeleteArgs.push_back(std::make_pair(RValue::get(Ptr), *AI++));
834
835 // A member 'operator delete' can take an extra 'size_t' argument.
836 if (FPT->getNumArgs() == NumPlacementArgs + 2)
837 DeleteArgs.push_back(std::make_pair(RValue::get(AllocSize), *AI++));
838
839 // Pass the rest of the arguments, which must match exactly.
840 for (unsigned I = 0; I != NumPlacementArgs; ++I)
841 DeleteArgs.push_back(std::make_pair(getPlacementArgs()[I], *AI++));
842
843 // Call 'operator delete'.
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000844 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT),
John McCall7d8647f2010-09-14 07:57:04 +0000845 CGF.CGM.GetAddrOfFunction(OperatorDelete),
846 ReturnValueSlot(), DeleteArgs, OperatorDelete);
847 }
848 };
John McCall3019c442010-09-17 00:50:28 +0000849
850 /// A cleanup to call the given 'operator delete' function upon
851 /// abnormal exit from a new expression when the new expression is
852 /// conditional.
853 class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup {
854 size_t NumPlacementArgs;
855 const FunctionDecl *OperatorDelete;
John McCall804b8072011-01-28 10:53:53 +0000856 DominatingValue<RValue>::saved_type Ptr;
857 DominatingValue<RValue>::saved_type AllocSize;
John McCall3019c442010-09-17 00:50:28 +0000858
John McCall804b8072011-01-28 10:53:53 +0000859 DominatingValue<RValue>::saved_type *getPlacementArgs() {
860 return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1);
John McCall3019c442010-09-17 00:50:28 +0000861 }
862
863 public:
864 static size_t getExtraSize(size_t NumPlacementArgs) {
John McCall804b8072011-01-28 10:53:53 +0000865 return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type);
John McCall3019c442010-09-17 00:50:28 +0000866 }
867
868 CallDeleteDuringConditionalNew(size_t NumPlacementArgs,
869 const FunctionDecl *OperatorDelete,
John McCall804b8072011-01-28 10:53:53 +0000870 DominatingValue<RValue>::saved_type Ptr,
871 DominatingValue<RValue>::saved_type AllocSize)
John McCall3019c442010-09-17 00:50:28 +0000872 : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
873 Ptr(Ptr), AllocSize(AllocSize) {}
874
John McCall804b8072011-01-28 10:53:53 +0000875 void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) {
John McCall3019c442010-09-17 00:50:28 +0000876 assert(I < NumPlacementArgs && "index out of range");
877 getPlacementArgs()[I] = Arg;
878 }
879
880 void Emit(CodeGenFunction &CGF, bool IsForEH) {
881 const FunctionProtoType *FPT
882 = OperatorDelete->getType()->getAs<FunctionProtoType>();
883 assert(FPT->getNumArgs() == NumPlacementArgs + 1 ||
884 (FPT->getNumArgs() == 2 && NumPlacementArgs == 0));
885
886 CallArgList DeleteArgs;
887
888 // The first argument is always a void*.
889 FunctionProtoType::arg_type_iterator AI = FPT->arg_type_begin();
John McCall804b8072011-01-28 10:53:53 +0000890 DeleteArgs.push_back(std::make_pair(Ptr.restore(CGF), *AI++));
John McCall3019c442010-09-17 00:50:28 +0000891
892 // A member 'operator delete' can take an extra 'size_t' argument.
893 if (FPT->getNumArgs() == NumPlacementArgs + 2) {
John McCall804b8072011-01-28 10:53:53 +0000894 RValue RV = AllocSize.restore(CGF);
John McCall3019c442010-09-17 00:50:28 +0000895 DeleteArgs.push_back(std::make_pair(RV, *AI++));
896 }
897
898 // Pass the rest of the arguments, which must match exactly.
899 for (unsigned I = 0; I != NumPlacementArgs; ++I) {
John McCall804b8072011-01-28 10:53:53 +0000900 RValue RV = getPlacementArgs()[I].restore(CGF);
John McCall3019c442010-09-17 00:50:28 +0000901 DeleteArgs.push_back(std::make_pair(RV, *AI++));
902 }
903
904 // Call 'operator delete'.
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000905 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(DeleteArgs, FPT),
John McCall3019c442010-09-17 00:50:28 +0000906 CGF.CGM.GetAddrOfFunction(OperatorDelete),
907 ReturnValueSlot(), DeleteArgs, OperatorDelete);
908 }
909 };
910}
911
912/// Enter a cleanup to call 'operator delete' if the initializer in a
913/// new-expression throws.
914static void EnterNewDeleteCleanup(CodeGenFunction &CGF,
915 const CXXNewExpr *E,
916 llvm::Value *NewPtr,
917 llvm::Value *AllocSize,
918 const CallArgList &NewArgs) {
919 // If we're not inside a conditional branch, then the cleanup will
920 // dominate and we can do the easier (and more efficient) thing.
921 if (!CGF.isInConditionalBranch()) {
922 CallDeleteDuringNew *Cleanup = CGF.EHStack
923 .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup,
924 E->getNumPlacementArgs(),
925 E->getOperatorDelete(),
926 NewPtr, AllocSize);
927 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
928 Cleanup->setPlacementArg(I, NewArgs[I+1].first);
929
930 return;
931 }
932
933 // Otherwise, we need to save all this stuff.
John McCall804b8072011-01-28 10:53:53 +0000934 DominatingValue<RValue>::saved_type SavedNewPtr =
935 DominatingValue<RValue>::save(CGF, RValue::get(NewPtr));
936 DominatingValue<RValue>::saved_type SavedAllocSize =
937 DominatingValue<RValue>::save(CGF, RValue::get(AllocSize));
John McCall3019c442010-09-17 00:50:28 +0000938
939 CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack
940 .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(InactiveEHCleanup,
941 E->getNumPlacementArgs(),
942 E->getOperatorDelete(),
943 SavedNewPtr,
944 SavedAllocSize);
945 for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
John McCall804b8072011-01-28 10:53:53 +0000946 Cleanup->setPlacementArg(I,
947 DominatingValue<RValue>::save(CGF, NewArgs[I+1].first));
John McCall3019c442010-09-17 00:50:28 +0000948
949 CGF.ActivateCleanupBlock(CGF.EHStack.stable_begin());
John McCall7d8647f2010-09-14 07:57:04 +0000950}
951
Anders Carlsson16d81b82009-09-22 22:53:17 +0000952llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
John McCallc2f3e7f2011-03-07 03:12:35 +0000953 // The element type being allocated.
954 QualType allocType = getContext().getBaseElementType(E->getAllocatedType());
John McCall1e7fe752010-09-02 09:58:18 +0000955
John McCallc2f3e7f2011-03-07 03:12:35 +0000956 // 1. Build a call to the allocation function.
957 FunctionDecl *allocator = E->getOperatorNew();
958 const FunctionProtoType *allocatorType =
959 allocator->getType()->castAs<FunctionProtoType>();
Anders Carlsson16d81b82009-09-22 22:53:17 +0000960
John McCallc2f3e7f2011-03-07 03:12:35 +0000961 CallArgList allocatorArgs;
Anders Carlsson16d81b82009-09-22 22:53:17 +0000962
963 // The allocation size is the first argument.
John McCallc2f3e7f2011-03-07 03:12:35 +0000964 QualType sizeType = getContext().getSizeType();
Anders Carlsson16d81b82009-09-22 22:53:17 +0000965
John McCallc2f3e7f2011-03-07 03:12:35 +0000966 llvm::Value *numElements = 0;
967 llvm::Value *allocSizeWithoutCookie = 0;
968 llvm::Value *allocSize =
969 EmitCXXNewAllocSize(getContext(), *this, E, numElements,
970 allocSizeWithoutCookie);
Anders Carlssona4d4c012009-09-23 16:07:23 +0000971
John McCallc2f3e7f2011-03-07 03:12:35 +0000972 allocatorArgs.push_back(std::make_pair(RValue::get(allocSize), sizeType));
Anders Carlsson16d81b82009-09-22 22:53:17 +0000973
974 // Emit the rest of the arguments.
975 // FIXME: Ideally, this should just use EmitCallArgs.
John McCallc2f3e7f2011-03-07 03:12:35 +0000976 CXXNewExpr::const_arg_iterator placementArg = E->placement_arg_begin();
Anders Carlsson16d81b82009-09-22 22:53:17 +0000977
978 // First, use the types from the function type.
979 // We start at 1 here because the first argument (the allocation size)
980 // has already been emitted.
John McCallc2f3e7f2011-03-07 03:12:35 +0000981 for (unsigned i = 1, e = allocatorType->getNumArgs(); i != e;
982 ++i, ++placementArg) {
983 QualType argType = allocatorType->getArgType(i);
Anders Carlsson16d81b82009-09-22 22:53:17 +0000984
John McCallc2f3e7f2011-03-07 03:12:35 +0000985 assert(getContext().hasSameUnqualifiedType(argType.getNonReferenceType(),
986 placementArg->getType()) &&
Anders Carlsson16d81b82009-09-22 22:53:17 +0000987 "type mismatch in call argument!");
988
John McCall413ebdb2011-03-11 20:59:21 +0000989 EmitCallArg(allocatorArgs, *placementArg, argType);
Anders Carlsson16d81b82009-09-22 22:53:17 +0000990 }
991
992 // Either we've emitted all the call args, or we have a call to a
993 // variadic function.
John McCallc2f3e7f2011-03-07 03:12:35 +0000994 assert((placementArg == E->placement_arg_end() ||
995 allocatorType->isVariadic()) &&
996 "Extra arguments to non-variadic function!");
Anders Carlsson16d81b82009-09-22 22:53:17 +0000997
998 // If we still have any arguments, emit them using the type of the argument.
John McCallc2f3e7f2011-03-07 03:12:35 +0000999 for (CXXNewExpr::const_arg_iterator placementArgsEnd = E->placement_arg_end();
1000 placementArg != placementArgsEnd; ++placementArg) {
John McCall413ebdb2011-03-11 20:59:21 +00001001 EmitCallArg(allocatorArgs, *placementArg, placementArg->getType());
Anders Carlsson16d81b82009-09-22 22:53:17 +00001002 }
1003
John McCallc2f3e7f2011-03-07 03:12:35 +00001004 // Emit the allocation call.
Anders Carlsson16d81b82009-09-22 22:53:17 +00001005 RValue RV =
John McCallc2f3e7f2011-03-07 03:12:35 +00001006 EmitCall(CGM.getTypes().getFunctionInfo(allocatorArgs, allocatorType),
1007 CGM.GetAddrOfFunction(allocator), ReturnValueSlot(),
1008 allocatorArgs, allocator);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001009
John McCallc2f3e7f2011-03-07 03:12:35 +00001010 // Emit a null check on the allocation result if the allocation
1011 // function is allowed to return null (because it has a non-throwing
1012 // exception spec; for this part, we inline
1013 // CXXNewExpr::shouldNullCheckAllocation()) and we have an
1014 // interesting initializer.
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001015 bool nullCheck = allocatorType->isNothrow(getContext()) &&
John McCallc2f3e7f2011-03-07 03:12:35 +00001016 !(allocType->isPODType() && !E->hasInitializer());
Anders Carlsson16d81b82009-09-22 22:53:17 +00001017
John McCallc2f3e7f2011-03-07 03:12:35 +00001018 llvm::BasicBlock *nullCheckBB = 0;
1019 llvm::BasicBlock *contBB = 0;
Anders Carlsson16d81b82009-09-22 22:53:17 +00001020
John McCallc2f3e7f2011-03-07 03:12:35 +00001021 llvm::Value *allocation = RV.getScalarVal();
1022 unsigned AS =
1023 cast<llvm::PointerType>(allocation->getType())->getAddressSpace();
Anders Carlsson16d81b82009-09-22 22:53:17 +00001024
John McCalla7f633f2011-03-07 01:52:56 +00001025 // The null-check means that the initializer is conditionally
1026 // evaluated.
1027 ConditionalEvaluation conditional(*this);
1028
John McCallc2f3e7f2011-03-07 03:12:35 +00001029 if (nullCheck) {
John McCalla7f633f2011-03-07 01:52:56 +00001030 conditional.begin(*this);
John McCallc2f3e7f2011-03-07 03:12:35 +00001031
1032 nullCheckBB = Builder.GetInsertBlock();
1033 llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull");
1034 contBB = createBasicBlock("new.cont");
1035
1036 llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull");
1037 Builder.CreateCondBr(isNull, contBB, notNullBB);
1038 EmitBlock(notNullBB);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001039 }
Ken Dyckcaf647c2010-01-26 19:44:24 +00001040
John McCallc2f3e7f2011-03-07 03:12:35 +00001041 assert((allocSize == allocSizeWithoutCookie) ==
John McCall1e7fe752010-09-02 09:58:18 +00001042 CalculateCookiePadding(*this, E).isZero());
John McCallc2f3e7f2011-03-07 03:12:35 +00001043 if (allocSize != allocSizeWithoutCookie) {
John McCall1e7fe752010-09-02 09:58:18 +00001044 assert(E->isArray());
John McCallc2f3e7f2011-03-07 03:12:35 +00001045 allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
1046 numElements,
1047 E, allocType);
John McCall1e7fe752010-09-02 09:58:18 +00001048 }
Anders Carlsson6ac5fc42009-09-23 18:59:48 +00001049
John McCall7d8647f2010-09-14 07:57:04 +00001050 // If there's an operator delete, enter a cleanup to call it if an
1051 // exception is thrown.
John McCallc2f3e7f2011-03-07 03:12:35 +00001052 EHScopeStack::stable_iterator operatorDeleteCleanup;
John McCall7d8647f2010-09-14 07:57:04 +00001053 if (E->getOperatorDelete()) {
John McCallc2f3e7f2011-03-07 03:12:35 +00001054 EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs);
1055 operatorDeleteCleanup = EHStack.stable_begin();
John McCall7d8647f2010-09-14 07:57:04 +00001056 }
1057
John McCallc2f3e7f2011-03-07 03:12:35 +00001058 const llvm::Type *elementPtrTy
1059 = ConvertTypeForMem(allocType)->getPointerTo(AS);
1060 llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy);
John McCall7d8647f2010-09-14 07:57:04 +00001061
John McCall1e7fe752010-09-02 09:58:18 +00001062 if (E->isArray()) {
John McCallc2f3e7f2011-03-07 03:12:35 +00001063 EmitNewInitializer(*this, E, result, numElements, allocSizeWithoutCookie);
John McCall1e7fe752010-09-02 09:58:18 +00001064
1065 // NewPtr is a pointer to the base element type. If we're
1066 // allocating an array of arrays, we'll need to cast back to the
1067 // array pointer type.
John McCallc2f3e7f2011-03-07 03:12:35 +00001068 const llvm::Type *resultType = ConvertTypeForMem(E->getType());
1069 if (result->getType() != resultType)
1070 result = Builder.CreateBitCast(result, resultType);
John McCall1e7fe752010-09-02 09:58:18 +00001071 } else {
John McCallc2f3e7f2011-03-07 03:12:35 +00001072 EmitNewInitializer(*this, E, result, numElements, allocSizeWithoutCookie);
Fariborz Jahanianceb43b62010-03-24 16:57:01 +00001073 }
John McCall7d8647f2010-09-14 07:57:04 +00001074
1075 // Deactivate the 'operator delete' cleanup if we finished
1076 // initialization.
John McCallc2f3e7f2011-03-07 03:12:35 +00001077 if (operatorDeleteCleanup.isValid())
1078 DeactivateCleanupBlock(operatorDeleteCleanup);
Fariborz Jahanianceb43b62010-03-24 16:57:01 +00001079
John McCallc2f3e7f2011-03-07 03:12:35 +00001080 if (nullCheck) {
John McCalla7f633f2011-03-07 01:52:56 +00001081 conditional.end(*this);
1082
John McCallc2f3e7f2011-03-07 03:12:35 +00001083 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
1084 EmitBlock(contBB);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001085
Jay Foadbbf3bac2011-03-30 11:28:58 +00001086 llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2);
John McCallc2f3e7f2011-03-07 03:12:35 +00001087 PHI->addIncoming(result, notNullBB);
1088 PHI->addIncoming(llvm::Constant::getNullValue(result->getType()),
1089 nullCheckBB);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001090
John McCallc2f3e7f2011-03-07 03:12:35 +00001091 result = PHI;
Anders Carlsson16d81b82009-09-22 22:53:17 +00001092 }
John McCall1e7fe752010-09-02 09:58:18 +00001093
John McCallc2f3e7f2011-03-07 03:12:35 +00001094 return result;
Anders Carlsson16d81b82009-09-22 22:53:17 +00001095}
1096
Eli Friedman5fe05982009-11-18 00:50:08 +00001097void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
1098 llvm::Value *Ptr,
1099 QualType DeleteTy) {
John McCall1e7fe752010-09-02 09:58:18 +00001100 assert(DeleteFD->getOverloadedOperator() == OO_Delete);
1101
Eli Friedman5fe05982009-11-18 00:50:08 +00001102 const FunctionProtoType *DeleteFTy =
1103 DeleteFD->getType()->getAs<FunctionProtoType>();
1104
1105 CallArgList DeleteArgs;
1106
Anders Carlsson871d0782009-12-13 20:04:38 +00001107 // Check if we need to pass the size to the delete operator.
1108 llvm::Value *Size = 0;
1109 QualType SizeTy;
1110 if (DeleteFTy->getNumArgs() == 2) {
1111 SizeTy = DeleteFTy->getArgType(1);
Ken Dyck4f122ef2010-01-26 19:59:28 +00001112 CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
1113 Size = llvm::ConstantInt::get(ConvertType(SizeTy),
1114 DeleteTypeSize.getQuantity());
Anders Carlsson871d0782009-12-13 20:04:38 +00001115 }
1116
Eli Friedman5fe05982009-11-18 00:50:08 +00001117 QualType ArgTy = DeleteFTy->getArgType(0);
1118 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
1119 DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
1120
Anders Carlsson871d0782009-12-13 20:04:38 +00001121 if (Size)
Eli Friedman5fe05982009-11-18 00:50:08 +00001122 DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy));
Eli Friedman5fe05982009-11-18 00:50:08 +00001123
1124 // Emit the call to delete.
Tilmann Scheller9c6082f2011-03-02 21:36:49 +00001125 EmitCall(CGM.getTypes().getFunctionInfo(DeleteArgs, DeleteFTy),
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001126 CGM.GetAddrOfFunction(DeleteFD), ReturnValueSlot(),
Eli Friedman5fe05982009-11-18 00:50:08 +00001127 DeleteArgs, DeleteFD);
1128}
1129
John McCall1e7fe752010-09-02 09:58:18 +00001130namespace {
1131 /// Calls the given 'operator delete' on a single object.
1132 struct CallObjectDelete : EHScopeStack::Cleanup {
1133 llvm::Value *Ptr;
1134 const FunctionDecl *OperatorDelete;
1135 QualType ElementType;
1136
1137 CallObjectDelete(llvm::Value *Ptr,
1138 const FunctionDecl *OperatorDelete,
1139 QualType ElementType)
1140 : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
1141
1142 void Emit(CodeGenFunction &CGF, bool IsForEH) {
1143 CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
1144 }
1145 };
1146}
1147
1148/// Emit the code for deleting a single object.
1149static void EmitObjectDelete(CodeGenFunction &CGF,
1150 const FunctionDecl *OperatorDelete,
1151 llvm::Value *Ptr,
1152 QualType ElementType) {
1153 // Find the destructor for the type, if applicable. If the
1154 // destructor is virtual, we'll just emit the vcall and return.
1155 const CXXDestructorDecl *Dtor = 0;
1156 if (const RecordType *RT = ElementType->getAs<RecordType>()) {
1157 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1158 if (!RD->hasTrivialDestructor()) {
1159 Dtor = RD->getDestructor();
1160
1161 if (Dtor->isVirtual()) {
1162 const llvm::Type *Ty =
John McCallfc400282010-09-03 01:26:39 +00001163 CGF.getTypes().GetFunctionType(CGF.getTypes().getFunctionInfo(Dtor,
1164 Dtor_Complete),
John McCall1e7fe752010-09-02 09:58:18 +00001165 /*isVariadic=*/false);
1166
1167 llvm::Value *Callee
1168 = CGF.BuildVirtualCall(Dtor, Dtor_Deleting, Ptr, Ty);
1169 CGF.EmitCXXMemberCall(Dtor, Callee, ReturnValueSlot(), Ptr, /*VTT=*/0,
1170 0, 0);
1171
1172 // The dtor took care of deleting the object.
1173 return;
1174 }
1175 }
1176 }
1177
1178 // Make sure that we call delete even if the dtor throws.
John McCall3ad32c82011-01-28 08:37:24 +00001179 // This doesn't have to a conditional cleanup because we're going
1180 // to pop it off in a second.
John McCall1e7fe752010-09-02 09:58:18 +00001181 CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1182 Ptr, OperatorDelete, ElementType);
1183
1184 if (Dtor)
1185 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1186 /*ForVirtualBase=*/false, Ptr);
1187
1188 CGF.PopCleanupBlock();
1189}
1190
1191namespace {
1192 /// Calls the given 'operator delete' on an array of objects.
1193 struct CallArrayDelete : EHScopeStack::Cleanup {
1194 llvm::Value *Ptr;
1195 const FunctionDecl *OperatorDelete;
1196 llvm::Value *NumElements;
1197 QualType ElementType;
1198 CharUnits CookieSize;
1199
1200 CallArrayDelete(llvm::Value *Ptr,
1201 const FunctionDecl *OperatorDelete,
1202 llvm::Value *NumElements,
1203 QualType ElementType,
1204 CharUnits CookieSize)
1205 : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
1206 ElementType(ElementType), CookieSize(CookieSize) {}
1207
1208 void Emit(CodeGenFunction &CGF, bool IsForEH) {
1209 const FunctionProtoType *DeleteFTy =
1210 OperatorDelete->getType()->getAs<FunctionProtoType>();
1211 assert(DeleteFTy->getNumArgs() == 1 || DeleteFTy->getNumArgs() == 2);
1212
1213 CallArgList Args;
1214
1215 // Pass the pointer as the first argument.
1216 QualType VoidPtrTy = DeleteFTy->getArgType(0);
1217 llvm::Value *DeletePtr
1218 = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
1219 Args.push_back(std::make_pair(RValue::get(DeletePtr), VoidPtrTy));
1220
1221 // Pass the original requested size as the second argument.
1222 if (DeleteFTy->getNumArgs() == 2) {
1223 QualType size_t = DeleteFTy->getArgType(1);
1224 const llvm::IntegerType *SizeTy
1225 = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
1226
1227 CharUnits ElementTypeSize =
1228 CGF.CGM.getContext().getTypeSizeInChars(ElementType);
1229
1230 // The size of an element, multiplied by the number of elements.
1231 llvm::Value *Size
1232 = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
1233 Size = CGF.Builder.CreateMul(Size, NumElements);
1234
1235 // Plus the size of the cookie if applicable.
1236 if (!CookieSize.isZero()) {
1237 llvm::Value *CookieSizeV
1238 = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
1239 Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
1240 }
1241
1242 Args.push_back(std::make_pair(RValue::get(Size), size_t));
1243 }
1244
1245 // Emit the call to delete.
Tilmann Scheller9c6082f2011-03-02 21:36:49 +00001246 CGF.EmitCall(CGF.getTypes().getFunctionInfo(Args, DeleteFTy),
John McCall1e7fe752010-09-02 09:58:18 +00001247 CGF.CGM.GetAddrOfFunction(OperatorDelete),
1248 ReturnValueSlot(), Args, OperatorDelete);
1249 }
1250 };
1251}
1252
1253/// Emit the code for deleting an array of objects.
1254static void EmitArrayDelete(CodeGenFunction &CGF,
John McCall6ec278d2011-01-27 09:37:56 +00001255 const CXXDeleteExpr *E,
John McCall1e7fe752010-09-02 09:58:18 +00001256 llvm::Value *Ptr,
1257 QualType ElementType) {
1258 llvm::Value *NumElements = 0;
1259 llvm::Value *AllocatedPtr = 0;
1260 CharUnits CookieSize;
John McCall6ec278d2011-01-27 09:37:56 +00001261 CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr, E, ElementType,
John McCall1e7fe752010-09-02 09:58:18 +00001262 NumElements, AllocatedPtr, CookieSize);
1263
1264 assert(AllocatedPtr && "ReadArrayCookie didn't set AllocatedPtr");
1265
1266 // Make sure that we call delete even if one of the dtors throws.
John McCall6ec278d2011-01-27 09:37:56 +00001267 const FunctionDecl *OperatorDelete = E->getOperatorDelete();
John McCall1e7fe752010-09-02 09:58:18 +00001268 CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
1269 AllocatedPtr, OperatorDelete,
1270 NumElements, ElementType,
1271 CookieSize);
1272
1273 if (const CXXRecordDecl *RD = ElementType->getAsCXXRecordDecl()) {
1274 if (!RD->hasTrivialDestructor()) {
1275 assert(NumElements && "ReadArrayCookie didn't find element count"
1276 " for a class with destructor");
1277 CGF.EmitCXXAggrDestructorCall(RD->getDestructor(), NumElements, Ptr);
1278 }
1279 }
1280
1281 CGF.PopCleanupBlock();
1282}
1283
Anders Carlsson16d81b82009-09-22 22:53:17 +00001284void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
Fariborz Jahanian72c21532009-11-13 19:27:47 +00001285
Douglas Gregor90916562009-09-29 18:16:17 +00001286 // Get at the argument before we performed the implicit conversion
1287 // to void*.
1288 const Expr *Arg = E->getArgument();
1289 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00001290 if (ICE->getCastKind() != CK_UserDefinedConversion &&
Douglas Gregor90916562009-09-29 18:16:17 +00001291 ICE->getType()->isVoidPointerType())
1292 Arg = ICE->getSubExpr();
Douglas Gregord69dd782009-10-01 05:49:51 +00001293 else
1294 break;
Douglas Gregor90916562009-09-29 18:16:17 +00001295 }
Anders Carlsson16d81b82009-09-22 22:53:17 +00001296
Douglas Gregor90916562009-09-29 18:16:17 +00001297 llvm::Value *Ptr = EmitScalarExpr(Arg);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001298
1299 // Null check the pointer.
1300 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
1301 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
1302
Anders Carlssonb9241242011-04-11 00:30:07 +00001303 llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull");
Anders Carlsson16d81b82009-09-22 22:53:17 +00001304
1305 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
1306 EmitBlock(DeleteNotNull);
Anders Carlsson566abee2009-11-13 04:45:41 +00001307
John McCall1e7fe752010-09-02 09:58:18 +00001308 // We might be deleting a pointer to array. If so, GEP down to the
1309 // first non-array element.
1310 // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1311 QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1312 if (DeleteTy->isConstantArrayType()) {
1313 llvm::Value *Zero = Builder.getInt32(0);
1314 llvm::SmallVector<llvm::Value*,8> GEP;
1315
1316 GEP.push_back(Zero); // point at the outermost array
1317
1318 // For each layer of array type we're pointing at:
1319 while (const ConstantArrayType *Arr
1320 = getContext().getAsConstantArrayType(DeleteTy)) {
1321 // 1. Unpeel the array type.
1322 DeleteTy = Arr->getElementType();
1323
1324 // 2. GEP to the first element of the array.
1325 GEP.push_back(Zero);
Anders Carlsson16d81b82009-09-22 22:53:17 +00001326 }
John McCall1e7fe752010-09-02 09:58:18 +00001327
1328 Ptr = Builder.CreateInBoundsGEP(Ptr, GEP.begin(), GEP.end(), "del.first");
Anders Carlsson16d81b82009-09-22 22:53:17 +00001329 }
1330
Douglas Gregoreede61a2010-09-02 17:38:50 +00001331 assert(ConvertTypeForMem(DeleteTy) ==
1332 cast<llvm::PointerType>(Ptr->getType())->getElementType());
John McCall1e7fe752010-09-02 09:58:18 +00001333
1334 if (E->isArrayForm()) {
John McCall6ec278d2011-01-27 09:37:56 +00001335 EmitArrayDelete(*this, E, Ptr, DeleteTy);
John McCall1e7fe752010-09-02 09:58:18 +00001336 } else {
1337 EmitObjectDelete(*this, E->getOperatorDelete(), Ptr, DeleteTy);
1338 }
Anders Carlsson16d81b82009-09-22 22:53:17 +00001339
Anders Carlsson16d81b82009-09-22 22:53:17 +00001340 EmitBlock(DeleteEnd);
1341}
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001342
Anders Carlsson4bdbc0c2011-04-11 14:13:40 +00001343static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1344 // void __cxa_bad_typeid();
1345
1346 const llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext());
1347 const llvm::FunctionType *FTy =
1348 llvm::FunctionType::get(VoidTy, false);
1349
1350 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1351}
1352
1353static void EmitBadTypeidCall(CodeGenFunction &CGF) {
Anders Carlssonad3692bb2011-04-13 02:35:36 +00001354 llvm::Value *Fn = getBadTypeidFn(CGF);
1355 CGF.EmitCallOrInvoke(Fn, 0, 0).setDoesNotReturn();
Anders Carlsson4bdbc0c2011-04-11 14:13:40 +00001356 CGF.Builder.CreateUnreachable();
1357}
1358
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001359static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF,
1360 const Expr *E,
1361 const llvm::Type *StdTypeInfoPtrTy) {
1362 // Get the vtable pointer.
1363 llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress();
1364
1365 // C++ [expr.typeid]p2:
1366 // If the glvalue expression is obtained by applying the unary * operator to
1367 // a pointer and the pointer is a null pointer value, the typeid expression
1368 // throws the std::bad_typeid exception.
1369 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) {
1370 if (UO->getOpcode() == UO_Deref) {
1371 llvm::BasicBlock *BadTypeidBlock =
1372 CGF.createBasicBlock("typeid.bad_typeid");
1373 llvm::BasicBlock *EndBlock =
1374 CGF.createBasicBlock("typeid.end");
1375
1376 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr);
1377 CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock);
1378
1379 CGF.EmitBlock(BadTypeidBlock);
1380 EmitBadTypeidCall(CGF);
1381 CGF.EmitBlock(EndBlock);
1382 }
1383 }
1384
1385 llvm::Value *Value = CGF.GetVTablePtr(ThisPtr,
1386 StdTypeInfoPtrTy->getPointerTo());
1387
1388 // Load the type info.
1389 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
1390 return CGF.Builder.CreateLoad(Value);
1391}
1392
John McCall3ad32c82011-01-28 08:37:24 +00001393llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001394 const llvm::Type *StdTypeInfoPtrTy =
1395 ConvertType(E->getType())->getPointerTo();
Anders Carlsson31b7f522009-12-11 02:46:30 +00001396
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001397 if (E->isTypeOperand()) {
1398 llvm::Constant *TypeInfo =
1399 CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand());
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001400 return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy);
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001401 }
Anders Carlsson4bdbc0c2011-04-11 14:13:40 +00001402
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001403 // C++ [expr.typeid]p2:
1404 // When typeid is applied to a glvalue expression whose type is a
1405 // polymorphic class type, the result refers to a std::type_info object
1406 // representing the type of the most derived object (that is, the dynamic
1407 // type) to which the glvalue refers.
1408 if (E->getExprOperand()->isGLValue()) {
1409 if (const RecordType *RT =
1410 E->getExprOperand()->getType()->getAs<RecordType>()) {
1411 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1412 if (RD->isPolymorphic())
1413 return EmitTypeidFromVTable(*this, E->getExprOperand(),
1414 StdTypeInfoPtrTy);
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001415 }
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001416 }
Anders Carlsson3f6c5e12011-04-18 00:57:03 +00001417
1418 QualType OperandTy = E->getExprOperand()->getType();
1419 return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy),
1420 StdTypeInfoPtrTy);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001421}
Mike Stumpc849c052009-11-16 06:50:58 +00001422
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001423static llvm::Constant *getDynamicCastFn(CodeGenFunction &CGF) {
1424 // void *__dynamic_cast(const void *sub,
1425 // const abi::__class_type_info *src,
1426 // const abi::__class_type_info *dst,
1427 // std::ptrdiff_t src2dst_offset);
1428
1429 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
1430 const llvm::Type *PtrDiffTy =
1431 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1432
1433 const llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
1434
1435 const llvm::FunctionType *FTy =
1436 llvm::FunctionType::get(Int8PtrTy, Args, false);
1437
1438 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast");
1439}
1440
1441static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1442 // void __cxa_bad_cast();
1443
1444 const llvm::Type *VoidTy = llvm::Type::getVoidTy(CGF.getLLVMContext());
1445 const llvm::FunctionType *FTy =
1446 llvm::FunctionType::get(VoidTy, false);
1447
1448 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1449}
1450
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001451static void EmitBadCastCall(CodeGenFunction &CGF) {
Anders Carlssonad3692bb2011-04-13 02:35:36 +00001452 llvm::Value *Fn = getBadCastFn(CGF);
1453 CGF.EmitCallOrInvoke(Fn, 0, 0).setDoesNotReturn();
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001454 CGF.Builder.CreateUnreachable();
1455}
1456
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001457static llvm::Value *
1458EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
1459 QualType SrcTy, QualType DestTy,
1460 llvm::BasicBlock *CastEnd) {
1461 const llvm::Type *PtrDiffLTy =
1462 CGF.ConvertType(CGF.getContext().getPointerDiffType());
1463 const llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1464
1465 if (const PointerType *PTy = DestTy->getAs<PointerType>()) {
1466 if (PTy->getPointeeType()->isVoidType()) {
1467 // C++ [expr.dynamic.cast]p7:
1468 // If T is "pointer to cv void," then the result is a pointer to the
1469 // most derived object pointed to by v.
1470
1471 // Get the vtable pointer.
1472 llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo());
1473
1474 // Get the offset-to-top from the vtable.
1475 llvm::Value *OffsetToTop =
1476 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
1477 OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top");
1478
1479 // Finally, add the offset to the pointer.
1480 Value = CGF.EmitCastToVoidPtr(Value);
1481 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1482
1483 return CGF.Builder.CreateBitCast(Value, DestLTy);
1484 }
1485 }
1486
1487 QualType SrcRecordTy;
1488 QualType DestRecordTy;
1489
1490 if (const PointerType *DestPTy = DestTy->getAs<PointerType>()) {
1491 SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType();
1492 DestRecordTy = DestPTy->getPointeeType();
1493 } else {
1494 SrcRecordTy = SrcTy;
1495 DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType();
1496 }
1497
1498 assert(SrcRecordTy->isRecordType() && "source type must be a record type!");
1499 assert(DestRecordTy->isRecordType() && "dest type must be a record type!");
1500
1501 llvm::Value *SrcRTTI =
1502 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1503 llvm::Value *DestRTTI =
1504 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1505
1506 // FIXME: Actually compute a hint here.
1507 llvm::Value *OffsetHint = llvm::ConstantInt::get(PtrDiffLTy, -1ULL);
1508
1509 // Emit the call to __dynamic_cast.
1510 Value = CGF.EmitCastToVoidPtr(Value);
1511 Value = CGF.Builder.CreateCall4(getDynamicCastFn(CGF), Value,
1512 SrcRTTI, DestRTTI, OffsetHint);
1513 Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1514
1515 /// C++ [expr.dynamic.cast]p9:
1516 /// A failed cast to reference type throws std::bad_cast
1517 if (DestTy->isReferenceType()) {
1518 llvm::BasicBlock *BadCastBlock =
1519 CGF.createBasicBlock("dynamic_cast.bad_cast");
1520
1521 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1522 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1523
1524 CGF.EmitBlock(BadCastBlock);
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001525 EmitBadCastCall(CGF);
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001526 }
1527
1528 return Value;
1529}
1530
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001531static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF,
1532 QualType DestTy) {
1533 const llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1534 if (DestTy->isPointerType())
1535 return llvm::Constant::getNullValue(DestLTy);
1536
1537 /// C++ [expr.dynamic.cast]p9:
1538 /// A failed cast to reference type throws std::bad_cast
1539 EmitBadCastCall(CGF);
1540
1541 CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end"));
1542 return llvm::UndefValue::get(DestLTy);
1543}
1544
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001545llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value,
Mike Stumpc849c052009-11-16 06:50:58 +00001546 const CXXDynamicCastExpr *DCE) {
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001547 QualType DestTy = DCE->getTypeAsWritten();
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001548
Anders Carlsson3ddcdd52011-04-11 01:45:29 +00001549 if (DCE->isAlwaysNull())
1550 return EmitDynamicCastToNull(*this, DestTy);
1551
1552 QualType SrcTy = DCE->getSubExpr()->getType();
1553
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001554 // C++ [expr.dynamic.cast]p4:
1555 // If the value of v is a null pointer value in the pointer case, the result
1556 // is the null pointer value of type T.
1557 bool ShouldNullCheckSrcValue = SrcTy->isPointerType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001558
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001559 llvm::BasicBlock *CastNull = 0;
1560 llvm::BasicBlock *CastNotNull = 0;
1561 llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end");
Mike Stumpc849c052009-11-16 06:50:58 +00001562
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001563 if (ShouldNullCheckSrcValue) {
1564 CastNull = createBasicBlock("dynamic_cast.null");
1565 CastNotNull = createBasicBlock("dynamic_cast.notnull");
1566
1567 llvm::Value *IsNull = Builder.CreateIsNull(Value);
1568 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
1569 EmitBlock(CastNotNull);
Mike Stumpc849c052009-11-16 06:50:58 +00001570 }
1571
Anders Carlssonf0cb4a62011-04-11 00:46:40 +00001572 Value = EmitDynamicCastCall(*this, Value, SrcTy, DestTy, CastEnd);
1573
1574 if (ShouldNullCheckSrcValue) {
1575 EmitBranch(CastEnd);
1576
1577 EmitBlock(CastNull);
1578 EmitBranch(CastEnd);
1579 }
1580
1581 EmitBlock(CastEnd);
1582
1583 if (ShouldNullCheckSrcValue) {
1584 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
1585 PHI->addIncoming(Value, CastNotNull);
1586 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
1587
1588 Value = PHI;
1589 }
1590
1591 return Value;
Mike Stumpc849c052009-11-16 06:50:58 +00001592}