1f4a2713aSLionel Sambuc //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This contains code dealing with code generation of C++ expressions
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "CodeGenFunction.h"
15f4a2713aSLionel Sambuc #include "CGCUDARuntime.h"
16f4a2713aSLionel Sambuc #include "CGCXXABI.h"
17f4a2713aSLionel Sambuc #include "CGDebugInfo.h"
18f4a2713aSLionel Sambuc #include "CGObjCRuntime.h"
19f4a2713aSLionel Sambuc #include "clang/CodeGen/CGFunctionInfo.h"
20f4a2713aSLionel Sambuc #include "clang/Frontend/CodeGenOptions.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/IR/CallSite.h"
22f4a2713aSLionel Sambuc #include "llvm/IR/Intrinsics.h"
23f4a2713aSLionel Sambuc
24f4a2713aSLionel Sambuc using namespace clang;
25f4a2713aSLionel Sambuc using namespace CodeGen;
26f4a2713aSLionel Sambuc
commonEmitCXXMemberOrOperatorCall(CodeGenFunction & CGF,const CXXMethodDecl * MD,llvm::Value * Callee,ReturnValueSlot ReturnValue,llvm::Value * This,llvm::Value * ImplicitParam,QualType ImplicitParamTy,const CallExpr * CE,CallArgList & Args)27*0a6a1f1dSLionel Sambuc static RequiredArgs commonEmitCXXMemberOrOperatorCall(
28*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, const CXXMethodDecl *MD, llvm::Value *Callee,
29*0a6a1f1dSLionel Sambuc ReturnValueSlot ReturnValue, llvm::Value *This, llvm::Value *ImplicitParam,
30*0a6a1f1dSLionel Sambuc QualType ImplicitParamTy, const CallExpr *CE, CallArgList &Args) {
31*0a6a1f1dSLionel Sambuc assert(CE == nullptr || isa<CXXMemberCallExpr>(CE) ||
32*0a6a1f1dSLionel Sambuc isa<CXXOperatorCallExpr>(CE));
33f4a2713aSLionel Sambuc assert(MD->isInstance() &&
34*0a6a1f1dSLionel Sambuc "Trying to emit a member or operator call expr on a static method!");
35f4a2713aSLionel Sambuc
36f4a2713aSLionel Sambuc // C++11 [class.mfct.non-static]p2:
37f4a2713aSLionel Sambuc // If a non-static member function of a class X is called for an object that
38f4a2713aSLionel Sambuc // is not of type X, or of a type derived from X, the behavior is undefined.
39*0a6a1f1dSLionel Sambuc SourceLocation CallLoc;
40*0a6a1f1dSLionel Sambuc if (CE)
41*0a6a1f1dSLionel Sambuc CallLoc = CE->getExprLoc();
42*0a6a1f1dSLionel Sambuc CGF.EmitTypeCheck(
43*0a6a1f1dSLionel Sambuc isa<CXXConstructorDecl>(MD) ? CodeGenFunction::TCK_ConstructorCall
44*0a6a1f1dSLionel Sambuc : CodeGenFunction::TCK_MemberCall,
45*0a6a1f1dSLionel Sambuc CallLoc, This, CGF.getContext().getRecordType(MD->getParent()));
46f4a2713aSLionel Sambuc
47f4a2713aSLionel Sambuc // Push the this ptr.
48*0a6a1f1dSLionel Sambuc Args.add(RValue::get(This), MD->getThisType(CGF.getContext()));
49f4a2713aSLionel Sambuc
50f4a2713aSLionel Sambuc // If there is an implicit parameter (e.g. VTT), emit it.
51f4a2713aSLionel Sambuc if (ImplicitParam) {
52f4a2713aSLionel Sambuc Args.add(RValue::get(ImplicitParam), ImplicitParamTy);
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
56f4a2713aSLionel Sambuc RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size());
57f4a2713aSLionel Sambuc
58f4a2713aSLionel Sambuc // And the rest of the call args.
59*0a6a1f1dSLionel Sambuc if (CE) {
60*0a6a1f1dSLionel Sambuc // Special case: skip first argument of CXXOperatorCall (it is "this").
61*0a6a1f1dSLionel Sambuc unsigned ArgsToSkip = isa<CXXOperatorCallExpr>(CE) ? 1 : 0;
62*0a6a1f1dSLionel Sambuc CGF.EmitCallArgs(Args, FPT, CE->arg_begin() + ArgsToSkip, CE->arg_end(),
63*0a6a1f1dSLionel Sambuc CE->getDirectCallee());
64*0a6a1f1dSLionel Sambuc } else {
65*0a6a1f1dSLionel Sambuc assert(
66*0a6a1f1dSLionel Sambuc FPT->getNumParams() == 0 &&
67*0a6a1f1dSLionel Sambuc "No CallExpr specified for function with non-zero number of arguments");
68*0a6a1f1dSLionel Sambuc }
69*0a6a1f1dSLionel Sambuc return required;
70*0a6a1f1dSLionel Sambuc }
71f4a2713aSLionel Sambuc
EmitCXXMemberOrOperatorCall(const CXXMethodDecl * MD,llvm::Value * Callee,ReturnValueSlot ReturnValue,llvm::Value * This,llvm::Value * ImplicitParam,QualType ImplicitParamTy,const CallExpr * CE)72*0a6a1f1dSLionel Sambuc RValue CodeGenFunction::EmitCXXMemberOrOperatorCall(
73*0a6a1f1dSLionel Sambuc const CXXMethodDecl *MD, llvm::Value *Callee, ReturnValueSlot ReturnValue,
74*0a6a1f1dSLionel Sambuc llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy,
75*0a6a1f1dSLionel Sambuc const CallExpr *CE) {
76*0a6a1f1dSLionel Sambuc const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
77*0a6a1f1dSLionel Sambuc CallArgList Args;
78*0a6a1f1dSLionel Sambuc RequiredArgs required = commonEmitCXXMemberOrOperatorCall(
79*0a6a1f1dSLionel Sambuc *this, MD, Callee, ReturnValue, This, ImplicitParam, ImplicitParamTy, CE,
80*0a6a1f1dSLionel Sambuc Args);
81f4a2713aSLionel Sambuc return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required),
82f4a2713aSLionel Sambuc Callee, ReturnValue, Args, MD);
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc
EmitCXXStructorCall(const CXXMethodDecl * MD,llvm::Value * Callee,ReturnValueSlot ReturnValue,llvm::Value * This,llvm::Value * ImplicitParam,QualType ImplicitParamTy,const CallExpr * CE,StructorType Type)85*0a6a1f1dSLionel Sambuc RValue CodeGenFunction::EmitCXXStructorCall(
86*0a6a1f1dSLionel Sambuc const CXXMethodDecl *MD, llvm::Value *Callee, ReturnValueSlot ReturnValue,
87*0a6a1f1dSLionel Sambuc llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy,
88*0a6a1f1dSLionel Sambuc const CallExpr *CE, StructorType Type) {
89*0a6a1f1dSLionel Sambuc CallArgList Args;
90*0a6a1f1dSLionel Sambuc commonEmitCXXMemberOrOperatorCall(*this, MD, Callee, ReturnValue, This,
91*0a6a1f1dSLionel Sambuc ImplicitParam, ImplicitParamTy, CE, Args);
92*0a6a1f1dSLionel Sambuc return EmitCall(CGM.getTypes().arrangeCXXStructorDeclaration(MD, Type),
93*0a6a1f1dSLionel Sambuc Callee, ReturnValue, Args, MD);
94*0a6a1f1dSLionel Sambuc }
95*0a6a1f1dSLionel Sambuc
getCXXRecord(const Expr * E)96f4a2713aSLionel Sambuc static CXXRecordDecl *getCXXRecord(const Expr *E) {
97f4a2713aSLionel Sambuc QualType T = E->getType();
98f4a2713aSLionel Sambuc if (const PointerType *PTy = T->getAs<PointerType>())
99f4a2713aSLionel Sambuc T = PTy->getPointeeType();
100f4a2713aSLionel Sambuc const RecordType *Ty = T->castAs<RecordType>();
101f4a2713aSLionel Sambuc return cast<CXXRecordDecl>(Ty->getDecl());
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc // Note: This function also emit constructor calls to support a MSVC
105f4a2713aSLionel Sambuc // extensions allowing explicit constructor function call.
EmitCXXMemberCallExpr(const CXXMemberCallExpr * CE,ReturnValueSlot ReturnValue)106f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
107f4a2713aSLionel Sambuc ReturnValueSlot ReturnValue) {
108f4a2713aSLionel Sambuc const Expr *callee = CE->getCallee()->IgnoreParens();
109f4a2713aSLionel Sambuc
110f4a2713aSLionel Sambuc if (isa<BinaryOperator>(callee))
111f4a2713aSLionel Sambuc return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
112f4a2713aSLionel Sambuc
113f4a2713aSLionel Sambuc const MemberExpr *ME = cast<MemberExpr>(callee);
114f4a2713aSLionel Sambuc const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
115f4a2713aSLionel Sambuc
116f4a2713aSLionel Sambuc if (MD->isStatic()) {
117f4a2713aSLionel Sambuc // The method is static, emit it as we would a regular call.
118f4a2713aSLionel Sambuc llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
119*0a6a1f1dSLionel Sambuc return EmitCall(getContext().getPointerType(MD->getType()), Callee, CE,
120*0a6a1f1dSLionel Sambuc ReturnValue);
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc
123*0a6a1f1dSLionel Sambuc bool HasQualifier = ME->hasQualifier();
124*0a6a1f1dSLionel Sambuc NestedNameSpecifier *Qualifier = HasQualifier ? ME->getQualifier() : nullptr;
125*0a6a1f1dSLionel Sambuc bool IsArrow = ME->isArrow();
126f4a2713aSLionel Sambuc const Expr *Base = ME->getBase();
127f4a2713aSLionel Sambuc
128*0a6a1f1dSLionel Sambuc return EmitCXXMemberOrOperatorMemberCallExpr(
129*0a6a1f1dSLionel Sambuc CE, MD, ReturnValue, HasQualifier, Qualifier, IsArrow, Base);
130*0a6a1f1dSLionel Sambuc }
131*0a6a1f1dSLionel Sambuc
EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr * CE,const CXXMethodDecl * MD,ReturnValueSlot ReturnValue,bool HasQualifier,NestedNameSpecifier * Qualifier,bool IsArrow,const Expr * Base)132*0a6a1f1dSLionel Sambuc RValue CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr(
133*0a6a1f1dSLionel Sambuc const CallExpr *CE, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue,
134*0a6a1f1dSLionel Sambuc bool HasQualifier, NestedNameSpecifier *Qualifier, bool IsArrow,
135*0a6a1f1dSLionel Sambuc const Expr *Base) {
136*0a6a1f1dSLionel Sambuc assert(isa<CXXMemberCallExpr>(CE) || isa<CXXOperatorCallExpr>(CE));
137*0a6a1f1dSLionel Sambuc
138*0a6a1f1dSLionel Sambuc // Compute the object pointer.
139*0a6a1f1dSLionel Sambuc bool CanUseVirtualCall = MD->isVirtual() && !HasQualifier;
140*0a6a1f1dSLionel Sambuc
141*0a6a1f1dSLionel Sambuc const CXXMethodDecl *DevirtualizedMethod = nullptr;
142f4a2713aSLionel Sambuc if (CanUseVirtualCall && CanDevirtualizeMemberFunctionCall(Base, MD)) {
143f4a2713aSLionel Sambuc const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
144f4a2713aSLionel Sambuc DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl);
145f4a2713aSLionel Sambuc assert(DevirtualizedMethod);
146f4a2713aSLionel Sambuc const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent();
147f4a2713aSLionel Sambuc const Expr *Inner = Base->ignoreParenBaseCasts();
148*0a6a1f1dSLionel Sambuc if (DevirtualizedMethod->getReturnType().getCanonicalType() !=
149*0a6a1f1dSLionel Sambuc MD->getReturnType().getCanonicalType())
150*0a6a1f1dSLionel Sambuc // If the return types are not the same, this might be a case where more
151*0a6a1f1dSLionel Sambuc // code needs to run to compensate for it. For example, the derived
152*0a6a1f1dSLionel Sambuc // method might return a type that inherits form from the return
153*0a6a1f1dSLionel Sambuc // type of MD and has a prefix.
154*0a6a1f1dSLionel Sambuc // For now we just avoid devirtualizing these covariant cases.
155*0a6a1f1dSLionel Sambuc DevirtualizedMethod = nullptr;
156*0a6a1f1dSLionel Sambuc else if (getCXXRecord(Inner) == DevirtualizedClass)
157f4a2713aSLionel Sambuc // If the class of the Inner expression is where the dynamic method
158f4a2713aSLionel Sambuc // is defined, build the this pointer from it.
159f4a2713aSLionel Sambuc Base = Inner;
160f4a2713aSLionel Sambuc else if (getCXXRecord(Base) != DevirtualizedClass) {
161f4a2713aSLionel Sambuc // If the method is defined in a class that is not the best dynamic
162f4a2713aSLionel Sambuc // one or the one of the full expression, we would have to build
163f4a2713aSLionel Sambuc // a derived-to-base cast to compute the correct this pointer, but
164f4a2713aSLionel Sambuc // we don't have support for that yet, so do a virtual call.
165*0a6a1f1dSLionel Sambuc DevirtualizedMethod = nullptr;
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc }
168f4a2713aSLionel Sambuc
169f4a2713aSLionel Sambuc llvm::Value *This;
170*0a6a1f1dSLionel Sambuc if (IsArrow)
171f4a2713aSLionel Sambuc This = EmitScalarExpr(Base);
172f4a2713aSLionel Sambuc else
173f4a2713aSLionel Sambuc This = EmitLValue(Base).getAddress();
174f4a2713aSLionel Sambuc
175f4a2713aSLionel Sambuc
176f4a2713aSLionel Sambuc if (MD->isTrivial()) {
177*0a6a1f1dSLionel Sambuc if (isa<CXXDestructorDecl>(MD)) return RValue::get(nullptr);
178f4a2713aSLionel Sambuc if (isa<CXXConstructorDecl>(MD) &&
179f4a2713aSLionel Sambuc cast<CXXConstructorDecl>(MD)->isDefaultConstructor())
180*0a6a1f1dSLionel Sambuc return RValue::get(nullptr);
181f4a2713aSLionel Sambuc
182*0a6a1f1dSLionel Sambuc if (!MD->getParent()->mayInsertExtraPadding()) {
183f4a2713aSLionel Sambuc if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) {
184f4a2713aSLionel Sambuc // We don't like to generate the trivial copy/move assignment operator
185f4a2713aSLionel Sambuc // when it isn't necessary; just produce the proper effect here.
186*0a6a1f1dSLionel Sambuc // Special case: skip first argument of CXXOperatorCall (it is "this").
187*0a6a1f1dSLionel Sambuc unsigned ArgsToSkip = isa<CXXOperatorCallExpr>(CE) ? 1 : 0;
188*0a6a1f1dSLionel Sambuc llvm::Value *RHS =
189*0a6a1f1dSLionel Sambuc EmitLValue(*(CE->arg_begin() + ArgsToSkip)).getAddress();
190f4a2713aSLionel Sambuc EmitAggregateAssign(This, RHS, CE->getType());
191f4a2713aSLionel Sambuc return RValue::get(This);
192f4a2713aSLionel Sambuc }
193f4a2713aSLionel Sambuc
194f4a2713aSLionel Sambuc if (isa<CXXConstructorDecl>(MD) &&
195f4a2713aSLionel Sambuc cast<CXXConstructorDecl>(MD)->isCopyOrMoveConstructor()) {
196f4a2713aSLionel Sambuc // Trivial move and copy ctor are the same.
197*0a6a1f1dSLionel Sambuc assert(CE->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
198f4a2713aSLionel Sambuc llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
199*0a6a1f1dSLionel Sambuc EmitAggregateCopy(This, RHS, CE->arg_begin()->getType());
200f4a2713aSLionel Sambuc return RValue::get(This);
201f4a2713aSLionel Sambuc }
202f4a2713aSLionel Sambuc llvm_unreachable("unknown trivial member function");
203f4a2713aSLionel Sambuc }
204*0a6a1f1dSLionel Sambuc }
205f4a2713aSLionel Sambuc
206f4a2713aSLionel Sambuc // Compute the function type we're calling.
207*0a6a1f1dSLionel Sambuc const CXXMethodDecl *CalleeDecl =
208*0a6a1f1dSLionel Sambuc DevirtualizedMethod ? DevirtualizedMethod : MD;
209*0a6a1f1dSLionel Sambuc const CGFunctionInfo *FInfo = nullptr;
210*0a6a1f1dSLionel Sambuc if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl))
211*0a6a1f1dSLionel Sambuc FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
212*0a6a1f1dSLionel Sambuc Dtor, StructorType::Complete);
213*0a6a1f1dSLionel Sambuc else if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(CalleeDecl))
214*0a6a1f1dSLionel Sambuc FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
215*0a6a1f1dSLionel Sambuc Ctor, StructorType::Complete);
216f4a2713aSLionel Sambuc else
217f4a2713aSLionel Sambuc FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl);
218f4a2713aSLionel Sambuc
219f4a2713aSLionel Sambuc llvm::FunctionType *Ty = CGM.getTypes().GetFunctionType(*FInfo);
220f4a2713aSLionel Sambuc
221f4a2713aSLionel Sambuc // C++ [class.virtual]p12:
222f4a2713aSLionel Sambuc // Explicit qualification with the scope operator (5.1) suppresses the
223f4a2713aSLionel Sambuc // virtual call mechanism.
224f4a2713aSLionel Sambuc //
225f4a2713aSLionel Sambuc // We also don't emit a virtual call if the base expression has a record type
226f4a2713aSLionel Sambuc // because then we know what the type is.
227f4a2713aSLionel Sambuc bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod;
228f4a2713aSLionel Sambuc llvm::Value *Callee;
229f4a2713aSLionel Sambuc
230f4a2713aSLionel Sambuc if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
231f4a2713aSLionel Sambuc assert(CE->arg_begin() == CE->arg_end() &&
232f4a2713aSLionel Sambuc "Destructor shouldn't have explicit parameters");
233f4a2713aSLionel Sambuc assert(ReturnValue.isNull() && "Destructor shouldn't have return value");
234f4a2713aSLionel Sambuc if (UseVirtualCall) {
235*0a6a1f1dSLionel Sambuc CGM.getCXXABI().EmitVirtualDestructorCall(
236*0a6a1f1dSLionel Sambuc *this, Dtor, Dtor_Complete, This, cast<CXXMemberCallExpr>(CE));
237f4a2713aSLionel Sambuc } else {
238*0a6a1f1dSLionel Sambuc if (getLangOpts().AppleKext && MD->isVirtual() && HasQualifier)
239*0a6a1f1dSLionel Sambuc Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty);
240f4a2713aSLionel Sambuc else if (!DevirtualizedMethod)
241*0a6a1f1dSLionel Sambuc Callee =
242*0a6a1f1dSLionel Sambuc CGM.getAddrOfCXXStructor(Dtor, StructorType::Complete, FInfo, Ty);
243f4a2713aSLionel Sambuc else {
244f4a2713aSLionel Sambuc const CXXDestructorDecl *DDtor =
245f4a2713aSLionel Sambuc cast<CXXDestructorDecl>(DevirtualizedMethod);
246f4a2713aSLionel Sambuc Callee = CGM.GetAddrOfFunction(GlobalDecl(DDtor, Dtor_Complete), Ty);
247f4a2713aSLionel Sambuc }
248*0a6a1f1dSLionel Sambuc EmitCXXMemberOrOperatorCall(MD, Callee, ReturnValue, This,
249*0a6a1f1dSLionel Sambuc /*ImplicitParam=*/nullptr, QualType(), CE);
250f4a2713aSLionel Sambuc }
251*0a6a1f1dSLionel Sambuc return RValue::get(nullptr);
252f4a2713aSLionel Sambuc }
253f4a2713aSLionel Sambuc
254f4a2713aSLionel Sambuc if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) {
255f4a2713aSLionel Sambuc Callee = CGM.GetAddrOfFunction(GlobalDecl(Ctor, Ctor_Complete), Ty);
256f4a2713aSLionel Sambuc } else if (UseVirtualCall) {
257f4a2713aSLionel Sambuc Callee = CGM.getCXXABI().getVirtualFunctionPointer(*this, MD, This, Ty);
258f4a2713aSLionel Sambuc } else {
259*0a6a1f1dSLionel Sambuc if (getLangOpts().AppleKext && MD->isVirtual() && HasQualifier)
260*0a6a1f1dSLionel Sambuc Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty);
261f4a2713aSLionel Sambuc else if (!DevirtualizedMethod)
262f4a2713aSLionel Sambuc Callee = CGM.GetAddrOfFunction(MD, Ty);
263f4a2713aSLionel Sambuc else {
264f4a2713aSLionel Sambuc Callee = CGM.GetAddrOfFunction(DevirtualizedMethod, Ty);
265f4a2713aSLionel Sambuc }
266f4a2713aSLionel Sambuc }
267f4a2713aSLionel Sambuc
268*0a6a1f1dSLionel Sambuc if (MD->isVirtual()) {
269*0a6a1f1dSLionel Sambuc This = CGM.getCXXABI().adjustThisArgumentForVirtualFunctionCall(
270*0a6a1f1dSLionel Sambuc *this, MD, This, UseVirtualCall);
271*0a6a1f1dSLionel Sambuc }
272f4a2713aSLionel Sambuc
273*0a6a1f1dSLionel Sambuc return EmitCXXMemberOrOperatorCall(MD, Callee, ReturnValue, This,
274*0a6a1f1dSLionel Sambuc /*ImplicitParam=*/nullptr, QualType(), CE);
275f4a2713aSLionel Sambuc }
276f4a2713aSLionel Sambuc
277f4a2713aSLionel Sambuc RValue
EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr * E,ReturnValueSlot ReturnValue)278f4a2713aSLionel Sambuc CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
279f4a2713aSLionel Sambuc ReturnValueSlot ReturnValue) {
280f4a2713aSLionel Sambuc const BinaryOperator *BO =
281f4a2713aSLionel Sambuc cast<BinaryOperator>(E->getCallee()->IgnoreParens());
282f4a2713aSLionel Sambuc const Expr *BaseExpr = BO->getLHS();
283f4a2713aSLionel Sambuc const Expr *MemFnExpr = BO->getRHS();
284f4a2713aSLionel Sambuc
285f4a2713aSLionel Sambuc const MemberPointerType *MPT =
286f4a2713aSLionel Sambuc MemFnExpr->getType()->castAs<MemberPointerType>();
287f4a2713aSLionel Sambuc
288f4a2713aSLionel Sambuc const FunctionProtoType *FPT =
289f4a2713aSLionel Sambuc MPT->getPointeeType()->castAs<FunctionProtoType>();
290f4a2713aSLionel Sambuc const CXXRecordDecl *RD =
291f4a2713aSLionel Sambuc cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
292f4a2713aSLionel Sambuc
293f4a2713aSLionel Sambuc // Get the member function pointer.
294f4a2713aSLionel Sambuc llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
295f4a2713aSLionel Sambuc
296f4a2713aSLionel Sambuc // Emit the 'this' pointer.
297f4a2713aSLionel Sambuc llvm::Value *This;
298f4a2713aSLionel Sambuc
299f4a2713aSLionel Sambuc if (BO->getOpcode() == BO_PtrMemI)
300f4a2713aSLionel Sambuc This = EmitScalarExpr(BaseExpr);
301f4a2713aSLionel Sambuc else
302f4a2713aSLionel Sambuc This = EmitLValue(BaseExpr).getAddress();
303f4a2713aSLionel Sambuc
304f4a2713aSLionel Sambuc EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This,
305f4a2713aSLionel Sambuc QualType(MPT->getClass(), 0));
306f4a2713aSLionel Sambuc
307f4a2713aSLionel Sambuc // Ask the ABI to load the callee. Note that This is modified.
308f4a2713aSLionel Sambuc llvm::Value *Callee =
309*0a6a1f1dSLionel Sambuc CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, BO, This, MemFnPtr, MPT);
310f4a2713aSLionel Sambuc
311f4a2713aSLionel Sambuc CallArgList Args;
312f4a2713aSLionel Sambuc
313f4a2713aSLionel Sambuc QualType ThisType =
314f4a2713aSLionel Sambuc getContext().getPointerType(getContext().getTagDeclType(RD));
315f4a2713aSLionel Sambuc
316f4a2713aSLionel Sambuc // Push the this ptr.
317f4a2713aSLionel Sambuc Args.add(RValue::get(This), ThisType);
318f4a2713aSLionel Sambuc
319f4a2713aSLionel Sambuc RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1);
320f4a2713aSLionel Sambuc
321f4a2713aSLionel Sambuc // And the rest of the call args
322*0a6a1f1dSLionel Sambuc EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end(), E->getDirectCallee());
323f4a2713aSLionel Sambuc return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required),
324f4a2713aSLionel Sambuc Callee, ReturnValue, Args);
325f4a2713aSLionel Sambuc }
326f4a2713aSLionel Sambuc
327f4a2713aSLionel Sambuc RValue
EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr * E,const CXXMethodDecl * MD,ReturnValueSlot ReturnValue)328f4a2713aSLionel Sambuc CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
329f4a2713aSLionel Sambuc const CXXMethodDecl *MD,
330f4a2713aSLionel Sambuc ReturnValueSlot ReturnValue) {
331f4a2713aSLionel Sambuc assert(MD->isInstance() &&
332f4a2713aSLionel Sambuc "Trying to emit a member call expr on a static method!");
333*0a6a1f1dSLionel Sambuc return EmitCXXMemberOrOperatorMemberCallExpr(
334*0a6a1f1dSLionel Sambuc E, MD, ReturnValue, /*HasQualifier=*/false, /*Qualifier=*/nullptr,
335*0a6a1f1dSLionel Sambuc /*IsArrow=*/false, E->getArg(0));
336f4a2713aSLionel Sambuc }
337f4a2713aSLionel Sambuc
EmitCUDAKernelCallExpr(const CUDAKernelCallExpr * E,ReturnValueSlot ReturnValue)338f4a2713aSLionel Sambuc RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
339f4a2713aSLionel Sambuc ReturnValueSlot ReturnValue) {
340f4a2713aSLionel Sambuc return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue);
341f4a2713aSLionel Sambuc }
342f4a2713aSLionel Sambuc
EmitNullBaseClassInitialization(CodeGenFunction & CGF,llvm::Value * DestPtr,const CXXRecordDecl * Base)343f4a2713aSLionel Sambuc static void EmitNullBaseClassInitialization(CodeGenFunction &CGF,
344f4a2713aSLionel Sambuc llvm::Value *DestPtr,
345f4a2713aSLionel Sambuc const CXXRecordDecl *Base) {
346f4a2713aSLionel Sambuc if (Base->isEmpty())
347f4a2713aSLionel Sambuc return;
348f4a2713aSLionel Sambuc
349f4a2713aSLionel Sambuc DestPtr = CGF.EmitCastToVoidPtr(DestPtr);
350f4a2713aSLionel Sambuc
351f4a2713aSLionel Sambuc const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base);
352f4a2713aSLionel Sambuc CharUnits Size = Layout.getNonVirtualSize();
353*0a6a1f1dSLionel Sambuc CharUnits Align = Layout.getNonVirtualAlignment();
354f4a2713aSLionel Sambuc
355f4a2713aSLionel Sambuc llvm::Value *SizeVal = CGF.CGM.getSize(Size);
356f4a2713aSLionel Sambuc
357f4a2713aSLionel Sambuc // If the type contains a pointer to data member we can't memset it to zero.
358f4a2713aSLionel Sambuc // Instead, create a null constant and copy it to the destination.
359f4a2713aSLionel Sambuc // TODO: there are other patterns besides zero that we can usefully memset,
360f4a2713aSLionel Sambuc // like -1, which happens to be the pattern used by member-pointers.
361f4a2713aSLionel Sambuc // TODO: isZeroInitializable can be over-conservative in the case where a
362f4a2713aSLionel Sambuc // virtual base contains a member pointer.
363f4a2713aSLionel Sambuc if (!CGF.CGM.getTypes().isZeroInitializable(Base)) {
364f4a2713aSLionel Sambuc llvm::Constant *NullConstant = CGF.CGM.EmitNullConstantForBase(Base);
365f4a2713aSLionel Sambuc
366f4a2713aSLionel Sambuc llvm::GlobalVariable *NullVariable =
367f4a2713aSLionel Sambuc new llvm::GlobalVariable(CGF.CGM.getModule(), NullConstant->getType(),
368f4a2713aSLionel Sambuc /*isConstant=*/true,
369f4a2713aSLionel Sambuc llvm::GlobalVariable::PrivateLinkage,
370f4a2713aSLionel Sambuc NullConstant, Twine());
371f4a2713aSLionel Sambuc NullVariable->setAlignment(Align.getQuantity());
372f4a2713aSLionel Sambuc llvm::Value *SrcPtr = CGF.EmitCastToVoidPtr(NullVariable);
373f4a2713aSLionel Sambuc
374f4a2713aSLionel Sambuc // Get and call the appropriate llvm.memcpy overload.
375f4a2713aSLionel Sambuc CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity());
376f4a2713aSLionel Sambuc return;
377f4a2713aSLionel Sambuc }
378f4a2713aSLionel Sambuc
379f4a2713aSLionel Sambuc // Otherwise, just memset the whole thing to zero. This is legal
380f4a2713aSLionel Sambuc // because in LLVM, all default initializers (other than the ones we just
381f4a2713aSLionel Sambuc // handled above) are guaranteed to have a bit pattern of all zeros.
382f4a2713aSLionel Sambuc CGF.Builder.CreateMemSet(DestPtr, CGF.Builder.getInt8(0), SizeVal,
383f4a2713aSLionel Sambuc Align.getQuantity());
384f4a2713aSLionel Sambuc }
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc void
EmitCXXConstructExpr(const CXXConstructExpr * E,AggValueSlot Dest)387f4a2713aSLionel Sambuc CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
388f4a2713aSLionel Sambuc AggValueSlot Dest) {
389f4a2713aSLionel Sambuc assert(!Dest.isIgnored() && "Must have a destination!");
390f4a2713aSLionel Sambuc const CXXConstructorDecl *CD = E->getConstructor();
391f4a2713aSLionel Sambuc
392f4a2713aSLionel Sambuc // If we require zero initialization before (or instead of) calling the
393f4a2713aSLionel Sambuc // constructor, as can be the case with a non-user-provided default
394f4a2713aSLionel Sambuc // constructor, emit the zero initialization now, unless destination is
395f4a2713aSLionel Sambuc // already zeroed.
396f4a2713aSLionel Sambuc if (E->requiresZeroInitialization() && !Dest.isZeroed()) {
397f4a2713aSLionel Sambuc switch (E->getConstructionKind()) {
398f4a2713aSLionel Sambuc case CXXConstructExpr::CK_Delegating:
399f4a2713aSLionel Sambuc case CXXConstructExpr::CK_Complete:
400f4a2713aSLionel Sambuc EmitNullInitialization(Dest.getAddr(), E->getType());
401f4a2713aSLionel Sambuc break;
402f4a2713aSLionel Sambuc case CXXConstructExpr::CK_VirtualBase:
403f4a2713aSLionel Sambuc case CXXConstructExpr::CK_NonVirtualBase:
404f4a2713aSLionel Sambuc EmitNullBaseClassInitialization(*this, Dest.getAddr(), CD->getParent());
405f4a2713aSLionel Sambuc break;
406f4a2713aSLionel Sambuc }
407f4a2713aSLionel Sambuc }
408f4a2713aSLionel Sambuc
409f4a2713aSLionel Sambuc // If this is a call to a trivial default constructor, do nothing.
410f4a2713aSLionel Sambuc if (CD->isTrivial() && CD->isDefaultConstructor())
411f4a2713aSLionel Sambuc return;
412f4a2713aSLionel Sambuc
413f4a2713aSLionel Sambuc // Elide the constructor if we're constructing from a temporary.
414f4a2713aSLionel Sambuc // The temporary check is required because Sema sets this on NRVO
415f4a2713aSLionel Sambuc // returns.
416f4a2713aSLionel Sambuc if (getLangOpts().ElideConstructors && E->isElidable()) {
417f4a2713aSLionel Sambuc assert(getContext().hasSameUnqualifiedType(E->getType(),
418f4a2713aSLionel Sambuc E->getArg(0)->getType()));
419f4a2713aSLionel Sambuc if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
420f4a2713aSLionel Sambuc EmitAggExpr(E->getArg(0), Dest);
421f4a2713aSLionel Sambuc return;
422f4a2713aSLionel Sambuc }
423f4a2713aSLionel Sambuc }
424f4a2713aSLionel Sambuc
425f4a2713aSLionel Sambuc if (const ConstantArrayType *arrayType
426f4a2713aSLionel Sambuc = getContext().getAsConstantArrayType(E->getType())) {
427*0a6a1f1dSLionel Sambuc EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddr(), E);
428f4a2713aSLionel Sambuc } else {
429f4a2713aSLionel Sambuc CXXCtorType Type = Ctor_Complete;
430f4a2713aSLionel Sambuc bool ForVirtualBase = false;
431f4a2713aSLionel Sambuc bool Delegating = false;
432f4a2713aSLionel Sambuc
433f4a2713aSLionel Sambuc switch (E->getConstructionKind()) {
434f4a2713aSLionel Sambuc case CXXConstructExpr::CK_Delegating:
435f4a2713aSLionel Sambuc // We should be emitting a constructor; GlobalDecl will assert this
436f4a2713aSLionel Sambuc Type = CurGD.getCtorType();
437f4a2713aSLionel Sambuc Delegating = true;
438f4a2713aSLionel Sambuc break;
439f4a2713aSLionel Sambuc
440f4a2713aSLionel Sambuc case CXXConstructExpr::CK_Complete:
441f4a2713aSLionel Sambuc Type = Ctor_Complete;
442f4a2713aSLionel Sambuc break;
443f4a2713aSLionel Sambuc
444f4a2713aSLionel Sambuc case CXXConstructExpr::CK_VirtualBase:
445f4a2713aSLionel Sambuc ForVirtualBase = true;
446f4a2713aSLionel Sambuc // fall-through
447f4a2713aSLionel Sambuc
448f4a2713aSLionel Sambuc case CXXConstructExpr::CK_NonVirtualBase:
449f4a2713aSLionel Sambuc Type = Ctor_Base;
450f4a2713aSLionel Sambuc }
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambuc // Call the constructor.
453f4a2713aSLionel Sambuc EmitCXXConstructorCall(CD, Type, ForVirtualBase, Delegating, Dest.getAddr(),
454*0a6a1f1dSLionel Sambuc E);
455f4a2713aSLionel Sambuc }
456f4a2713aSLionel Sambuc }
457f4a2713aSLionel Sambuc
458f4a2713aSLionel Sambuc void
EmitSynthesizedCXXCopyCtor(llvm::Value * Dest,llvm::Value * Src,const Expr * Exp)459f4a2713aSLionel Sambuc CodeGenFunction::EmitSynthesizedCXXCopyCtor(llvm::Value *Dest,
460f4a2713aSLionel Sambuc llvm::Value *Src,
461f4a2713aSLionel Sambuc const Expr *Exp) {
462f4a2713aSLionel Sambuc if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp))
463f4a2713aSLionel Sambuc Exp = E->getSubExpr();
464f4a2713aSLionel Sambuc assert(isa<CXXConstructExpr>(Exp) &&
465f4a2713aSLionel Sambuc "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr");
466f4a2713aSLionel Sambuc const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp);
467f4a2713aSLionel Sambuc const CXXConstructorDecl *CD = E->getConstructor();
468f4a2713aSLionel Sambuc RunCleanupsScope Scope(*this);
469f4a2713aSLionel Sambuc
470f4a2713aSLionel Sambuc // If we require zero initialization before (or instead of) calling the
471f4a2713aSLionel Sambuc // constructor, as can be the case with a non-user-provided default
472f4a2713aSLionel Sambuc // constructor, emit the zero initialization now.
473f4a2713aSLionel Sambuc // FIXME. Do I still need this for a copy ctor synthesis?
474f4a2713aSLionel Sambuc if (E->requiresZeroInitialization())
475f4a2713aSLionel Sambuc EmitNullInitialization(Dest, E->getType());
476f4a2713aSLionel Sambuc
477f4a2713aSLionel Sambuc assert(!getContext().getAsConstantArrayType(E->getType())
478f4a2713aSLionel Sambuc && "EmitSynthesizedCXXCopyCtor - Copied-in Array");
479*0a6a1f1dSLionel Sambuc EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, E);
480f4a2713aSLionel Sambuc }
481f4a2713aSLionel Sambuc
CalculateCookiePadding(CodeGenFunction & CGF,const CXXNewExpr * E)482f4a2713aSLionel Sambuc static CharUnits CalculateCookiePadding(CodeGenFunction &CGF,
483f4a2713aSLionel Sambuc const CXXNewExpr *E) {
484f4a2713aSLionel Sambuc if (!E->isArray())
485f4a2713aSLionel Sambuc return CharUnits::Zero();
486f4a2713aSLionel Sambuc
487f4a2713aSLionel Sambuc // No cookie is required if the operator new[] being used is the
488f4a2713aSLionel Sambuc // reserved placement operator new[].
489f4a2713aSLionel Sambuc if (E->getOperatorNew()->isReservedGlobalPlacementOperator())
490f4a2713aSLionel Sambuc return CharUnits::Zero();
491f4a2713aSLionel Sambuc
492f4a2713aSLionel Sambuc return CGF.CGM.getCXXABI().GetArrayCookieSize(E);
493f4a2713aSLionel Sambuc }
494f4a2713aSLionel Sambuc
EmitCXXNewAllocSize(CodeGenFunction & CGF,const CXXNewExpr * e,unsigned minElements,llvm::Value * & numElements,llvm::Value * & sizeWithoutCookie)495f4a2713aSLionel Sambuc static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF,
496f4a2713aSLionel Sambuc const CXXNewExpr *e,
497f4a2713aSLionel Sambuc unsigned minElements,
498f4a2713aSLionel Sambuc llvm::Value *&numElements,
499f4a2713aSLionel Sambuc llvm::Value *&sizeWithoutCookie) {
500f4a2713aSLionel Sambuc QualType type = e->getAllocatedType();
501f4a2713aSLionel Sambuc
502f4a2713aSLionel Sambuc if (!e->isArray()) {
503f4a2713aSLionel Sambuc CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
504f4a2713aSLionel Sambuc sizeWithoutCookie
505f4a2713aSLionel Sambuc = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity());
506f4a2713aSLionel Sambuc return sizeWithoutCookie;
507f4a2713aSLionel Sambuc }
508f4a2713aSLionel Sambuc
509f4a2713aSLionel Sambuc // The width of size_t.
510f4a2713aSLionel Sambuc unsigned sizeWidth = CGF.SizeTy->getBitWidth();
511f4a2713aSLionel Sambuc
512f4a2713aSLionel Sambuc // Figure out the cookie size.
513f4a2713aSLionel Sambuc llvm::APInt cookieSize(sizeWidth,
514f4a2713aSLionel Sambuc CalculateCookiePadding(CGF, e).getQuantity());
515f4a2713aSLionel Sambuc
516f4a2713aSLionel Sambuc // Emit the array size expression.
517f4a2713aSLionel Sambuc // We multiply the size of all dimensions for NumElements.
518f4a2713aSLionel Sambuc // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
519f4a2713aSLionel Sambuc numElements = CGF.EmitScalarExpr(e->getArraySize());
520f4a2713aSLionel Sambuc assert(isa<llvm::IntegerType>(numElements->getType()));
521f4a2713aSLionel Sambuc
522f4a2713aSLionel Sambuc // The number of elements can be have an arbitrary integer type;
523f4a2713aSLionel Sambuc // essentially, we need to multiply it by a constant factor, add a
524f4a2713aSLionel Sambuc // cookie size, and verify that the result is representable as a
525f4a2713aSLionel Sambuc // size_t. That's just a gloss, though, and it's wrong in one
526f4a2713aSLionel Sambuc // important way: if the count is negative, it's an error even if
527f4a2713aSLionel Sambuc // the cookie size would bring the total size >= 0.
528f4a2713aSLionel Sambuc bool isSigned
529f4a2713aSLionel Sambuc = e->getArraySize()->getType()->isSignedIntegerOrEnumerationType();
530f4a2713aSLionel Sambuc llvm::IntegerType *numElementsType
531f4a2713aSLionel Sambuc = cast<llvm::IntegerType>(numElements->getType());
532f4a2713aSLionel Sambuc unsigned numElementsWidth = numElementsType->getBitWidth();
533f4a2713aSLionel Sambuc
534f4a2713aSLionel Sambuc // Compute the constant factor.
535f4a2713aSLionel Sambuc llvm::APInt arraySizeMultiplier(sizeWidth, 1);
536f4a2713aSLionel Sambuc while (const ConstantArrayType *CAT
537f4a2713aSLionel Sambuc = CGF.getContext().getAsConstantArrayType(type)) {
538f4a2713aSLionel Sambuc type = CAT->getElementType();
539f4a2713aSLionel Sambuc arraySizeMultiplier *= CAT->getSize();
540f4a2713aSLionel Sambuc }
541f4a2713aSLionel Sambuc
542f4a2713aSLionel Sambuc CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
543f4a2713aSLionel Sambuc llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity());
544f4a2713aSLionel Sambuc typeSizeMultiplier *= arraySizeMultiplier;
545f4a2713aSLionel Sambuc
546f4a2713aSLionel Sambuc // This will be a size_t.
547f4a2713aSLionel Sambuc llvm::Value *size;
548f4a2713aSLionel Sambuc
549f4a2713aSLionel Sambuc // If someone is doing 'new int[42]' there is no need to do a dynamic check.
550f4a2713aSLionel Sambuc // Don't bloat the -O0 code.
551f4a2713aSLionel Sambuc if (llvm::ConstantInt *numElementsC =
552f4a2713aSLionel Sambuc dyn_cast<llvm::ConstantInt>(numElements)) {
553f4a2713aSLionel Sambuc const llvm::APInt &count = numElementsC->getValue();
554f4a2713aSLionel Sambuc
555f4a2713aSLionel Sambuc bool hasAnyOverflow = false;
556f4a2713aSLionel Sambuc
557f4a2713aSLionel Sambuc // If 'count' was a negative number, it's an overflow.
558f4a2713aSLionel Sambuc if (isSigned && count.isNegative())
559f4a2713aSLionel Sambuc hasAnyOverflow = true;
560f4a2713aSLionel Sambuc
561f4a2713aSLionel Sambuc // We want to do all this arithmetic in size_t. If numElements is
562f4a2713aSLionel Sambuc // wider than that, check whether it's already too big, and if so,
563f4a2713aSLionel Sambuc // overflow.
564f4a2713aSLionel Sambuc else if (numElementsWidth > sizeWidth &&
565f4a2713aSLionel Sambuc numElementsWidth - sizeWidth > count.countLeadingZeros())
566f4a2713aSLionel Sambuc hasAnyOverflow = true;
567f4a2713aSLionel Sambuc
568f4a2713aSLionel Sambuc // Okay, compute a count at the right width.
569f4a2713aSLionel Sambuc llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth);
570f4a2713aSLionel Sambuc
571f4a2713aSLionel Sambuc // If there is a brace-initializer, we cannot allocate fewer elements than
572f4a2713aSLionel Sambuc // there are initializers. If we do, that's treated like an overflow.
573f4a2713aSLionel Sambuc if (adjustedCount.ult(minElements))
574f4a2713aSLionel Sambuc hasAnyOverflow = true;
575f4a2713aSLionel Sambuc
576f4a2713aSLionel Sambuc // Scale numElements by that. This might overflow, but we don't
577f4a2713aSLionel Sambuc // care because it only overflows if allocationSize does, too, and
578f4a2713aSLionel Sambuc // if that overflows then we shouldn't use this.
579f4a2713aSLionel Sambuc numElements = llvm::ConstantInt::get(CGF.SizeTy,
580f4a2713aSLionel Sambuc adjustedCount * arraySizeMultiplier);
581f4a2713aSLionel Sambuc
582f4a2713aSLionel Sambuc // Compute the size before cookie, and track whether it overflowed.
583f4a2713aSLionel Sambuc bool overflow;
584f4a2713aSLionel Sambuc llvm::APInt allocationSize
585f4a2713aSLionel Sambuc = adjustedCount.umul_ov(typeSizeMultiplier, overflow);
586f4a2713aSLionel Sambuc hasAnyOverflow |= overflow;
587f4a2713aSLionel Sambuc
588f4a2713aSLionel Sambuc // Add in the cookie, and check whether it's overflowed.
589f4a2713aSLionel Sambuc if (cookieSize != 0) {
590f4a2713aSLionel Sambuc // Save the current size without a cookie. This shouldn't be
591f4a2713aSLionel Sambuc // used if there was overflow.
592f4a2713aSLionel Sambuc sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
593f4a2713aSLionel Sambuc
594f4a2713aSLionel Sambuc allocationSize = allocationSize.uadd_ov(cookieSize, overflow);
595f4a2713aSLionel Sambuc hasAnyOverflow |= overflow;
596f4a2713aSLionel Sambuc }
597f4a2713aSLionel Sambuc
598f4a2713aSLionel Sambuc // On overflow, produce a -1 so operator new will fail.
599f4a2713aSLionel Sambuc if (hasAnyOverflow) {
600f4a2713aSLionel Sambuc size = llvm::Constant::getAllOnesValue(CGF.SizeTy);
601f4a2713aSLionel Sambuc } else {
602f4a2713aSLionel Sambuc size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
603f4a2713aSLionel Sambuc }
604f4a2713aSLionel Sambuc
605f4a2713aSLionel Sambuc // Otherwise, we might need to use the overflow intrinsics.
606f4a2713aSLionel Sambuc } else {
607f4a2713aSLionel Sambuc // There are up to five conditions we need to test for:
608f4a2713aSLionel Sambuc // 1) if isSigned, we need to check whether numElements is negative;
609f4a2713aSLionel Sambuc // 2) if numElementsWidth > sizeWidth, we need to check whether
610f4a2713aSLionel Sambuc // numElements is larger than something representable in size_t;
611f4a2713aSLionel Sambuc // 3) if minElements > 0, we need to check whether numElements is smaller
612f4a2713aSLionel Sambuc // than that.
613f4a2713aSLionel Sambuc // 4) we need to compute
614f4a2713aSLionel Sambuc // sizeWithoutCookie := numElements * typeSizeMultiplier
615f4a2713aSLionel Sambuc // and check whether it overflows; and
616f4a2713aSLionel Sambuc // 5) if we need a cookie, we need to compute
617f4a2713aSLionel Sambuc // size := sizeWithoutCookie + cookieSize
618f4a2713aSLionel Sambuc // and check whether it overflows.
619f4a2713aSLionel Sambuc
620*0a6a1f1dSLionel Sambuc llvm::Value *hasOverflow = nullptr;
621f4a2713aSLionel Sambuc
622f4a2713aSLionel Sambuc // If numElementsWidth > sizeWidth, then one way or another, we're
623f4a2713aSLionel Sambuc // going to have to do a comparison for (2), and this happens to
624f4a2713aSLionel Sambuc // take care of (1), too.
625f4a2713aSLionel Sambuc if (numElementsWidth > sizeWidth) {
626f4a2713aSLionel Sambuc llvm::APInt threshold(numElementsWidth, 1);
627f4a2713aSLionel Sambuc threshold <<= sizeWidth;
628f4a2713aSLionel Sambuc
629f4a2713aSLionel Sambuc llvm::Value *thresholdV
630f4a2713aSLionel Sambuc = llvm::ConstantInt::get(numElementsType, threshold);
631f4a2713aSLionel Sambuc
632f4a2713aSLionel Sambuc hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV);
633f4a2713aSLionel Sambuc numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy);
634f4a2713aSLionel Sambuc
635f4a2713aSLionel Sambuc // Otherwise, if we're signed, we want to sext up to size_t.
636f4a2713aSLionel Sambuc } else if (isSigned) {
637f4a2713aSLionel Sambuc if (numElementsWidth < sizeWidth)
638f4a2713aSLionel Sambuc numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy);
639f4a2713aSLionel Sambuc
640f4a2713aSLionel Sambuc // If there's a non-1 type size multiplier, then we can do the
641f4a2713aSLionel Sambuc // signedness check at the same time as we do the multiply
642f4a2713aSLionel Sambuc // because a negative number times anything will cause an
643f4a2713aSLionel Sambuc // unsigned overflow. Otherwise, we have to do it here. But at least
644f4a2713aSLionel Sambuc // in this case, we can subsume the >= minElements check.
645f4a2713aSLionel Sambuc if (typeSizeMultiplier == 1)
646f4a2713aSLionel Sambuc hasOverflow = CGF.Builder.CreateICmpSLT(numElements,
647f4a2713aSLionel Sambuc llvm::ConstantInt::get(CGF.SizeTy, minElements));
648f4a2713aSLionel Sambuc
649f4a2713aSLionel Sambuc // Otherwise, zext up to size_t if necessary.
650f4a2713aSLionel Sambuc } else if (numElementsWidth < sizeWidth) {
651f4a2713aSLionel Sambuc numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy);
652f4a2713aSLionel Sambuc }
653f4a2713aSLionel Sambuc
654f4a2713aSLionel Sambuc assert(numElements->getType() == CGF.SizeTy);
655f4a2713aSLionel Sambuc
656f4a2713aSLionel Sambuc if (minElements) {
657f4a2713aSLionel Sambuc // Don't allow allocation of fewer elements than we have initializers.
658f4a2713aSLionel Sambuc if (!hasOverflow) {
659f4a2713aSLionel Sambuc hasOverflow = CGF.Builder.CreateICmpULT(numElements,
660f4a2713aSLionel Sambuc llvm::ConstantInt::get(CGF.SizeTy, minElements));
661f4a2713aSLionel Sambuc } else if (numElementsWidth > sizeWidth) {
662f4a2713aSLionel Sambuc // The other existing overflow subsumes this check.
663f4a2713aSLionel Sambuc // We do an unsigned comparison, since any signed value < -1 is
664f4a2713aSLionel Sambuc // taken care of either above or below.
665f4a2713aSLionel Sambuc hasOverflow = CGF.Builder.CreateOr(hasOverflow,
666f4a2713aSLionel Sambuc CGF.Builder.CreateICmpULT(numElements,
667f4a2713aSLionel Sambuc llvm::ConstantInt::get(CGF.SizeTy, minElements)));
668f4a2713aSLionel Sambuc }
669f4a2713aSLionel Sambuc }
670f4a2713aSLionel Sambuc
671f4a2713aSLionel Sambuc size = numElements;
672f4a2713aSLionel Sambuc
673f4a2713aSLionel Sambuc // Multiply by the type size if necessary. This multiplier
674f4a2713aSLionel Sambuc // includes all the factors for nested arrays.
675f4a2713aSLionel Sambuc //
676f4a2713aSLionel Sambuc // This step also causes numElements to be scaled up by the
677f4a2713aSLionel Sambuc // nested-array factor if necessary. Overflow on this computation
678f4a2713aSLionel Sambuc // can be ignored because the result shouldn't be used if
679f4a2713aSLionel Sambuc // allocation fails.
680f4a2713aSLionel Sambuc if (typeSizeMultiplier != 1) {
681f4a2713aSLionel Sambuc llvm::Value *umul_with_overflow
682f4a2713aSLionel Sambuc = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy);
683f4a2713aSLionel Sambuc
684f4a2713aSLionel Sambuc llvm::Value *tsmV =
685f4a2713aSLionel Sambuc llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier);
686f4a2713aSLionel Sambuc llvm::Value *result =
687f4a2713aSLionel Sambuc CGF.Builder.CreateCall2(umul_with_overflow, size, tsmV);
688f4a2713aSLionel Sambuc
689f4a2713aSLionel Sambuc llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
690f4a2713aSLionel Sambuc if (hasOverflow)
691f4a2713aSLionel Sambuc hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
692f4a2713aSLionel Sambuc else
693f4a2713aSLionel Sambuc hasOverflow = overflowed;
694f4a2713aSLionel Sambuc
695f4a2713aSLionel Sambuc size = CGF.Builder.CreateExtractValue(result, 0);
696f4a2713aSLionel Sambuc
697f4a2713aSLionel Sambuc // Also scale up numElements by the array size multiplier.
698f4a2713aSLionel Sambuc if (arraySizeMultiplier != 1) {
699f4a2713aSLionel Sambuc // If the base element type size is 1, then we can re-use the
700f4a2713aSLionel Sambuc // multiply we just did.
701f4a2713aSLionel Sambuc if (typeSize.isOne()) {
702f4a2713aSLionel Sambuc assert(arraySizeMultiplier == typeSizeMultiplier);
703f4a2713aSLionel Sambuc numElements = size;
704f4a2713aSLionel Sambuc
705f4a2713aSLionel Sambuc // Otherwise we need a separate multiply.
706f4a2713aSLionel Sambuc } else {
707f4a2713aSLionel Sambuc llvm::Value *asmV =
708f4a2713aSLionel Sambuc llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier);
709f4a2713aSLionel Sambuc numElements = CGF.Builder.CreateMul(numElements, asmV);
710f4a2713aSLionel Sambuc }
711f4a2713aSLionel Sambuc }
712f4a2713aSLionel Sambuc } else {
713f4a2713aSLionel Sambuc // numElements doesn't need to be scaled.
714f4a2713aSLionel Sambuc assert(arraySizeMultiplier == 1);
715f4a2713aSLionel Sambuc }
716f4a2713aSLionel Sambuc
717f4a2713aSLionel Sambuc // Add in the cookie size if necessary.
718f4a2713aSLionel Sambuc if (cookieSize != 0) {
719f4a2713aSLionel Sambuc sizeWithoutCookie = size;
720f4a2713aSLionel Sambuc
721f4a2713aSLionel Sambuc llvm::Value *uadd_with_overflow
722f4a2713aSLionel Sambuc = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy);
723f4a2713aSLionel Sambuc
724f4a2713aSLionel Sambuc llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize);
725f4a2713aSLionel Sambuc llvm::Value *result =
726f4a2713aSLionel Sambuc CGF.Builder.CreateCall2(uadd_with_overflow, size, cookieSizeV);
727f4a2713aSLionel Sambuc
728f4a2713aSLionel Sambuc llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
729f4a2713aSLionel Sambuc if (hasOverflow)
730f4a2713aSLionel Sambuc hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
731f4a2713aSLionel Sambuc else
732f4a2713aSLionel Sambuc hasOverflow = overflowed;
733f4a2713aSLionel Sambuc
734f4a2713aSLionel Sambuc size = CGF.Builder.CreateExtractValue(result, 0);
735f4a2713aSLionel Sambuc }
736f4a2713aSLionel Sambuc
737f4a2713aSLionel Sambuc // If we had any possibility of dynamic overflow, make a select to
738f4a2713aSLionel Sambuc // overwrite 'size' with an all-ones value, which should cause
739f4a2713aSLionel Sambuc // operator new to throw.
740f4a2713aSLionel Sambuc if (hasOverflow)
741f4a2713aSLionel Sambuc size = CGF.Builder.CreateSelect(hasOverflow,
742f4a2713aSLionel Sambuc llvm::Constant::getAllOnesValue(CGF.SizeTy),
743f4a2713aSLionel Sambuc size);
744f4a2713aSLionel Sambuc }
745f4a2713aSLionel Sambuc
746f4a2713aSLionel Sambuc if (cookieSize == 0)
747f4a2713aSLionel Sambuc sizeWithoutCookie = size;
748f4a2713aSLionel Sambuc else
749f4a2713aSLionel Sambuc assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?");
750f4a2713aSLionel Sambuc
751f4a2713aSLionel Sambuc return size;
752f4a2713aSLionel Sambuc }
753f4a2713aSLionel Sambuc
StoreAnyExprIntoOneUnit(CodeGenFunction & CGF,const Expr * Init,QualType AllocType,llvm::Value * NewPtr)754f4a2713aSLionel Sambuc static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init,
755f4a2713aSLionel Sambuc QualType AllocType, llvm::Value *NewPtr) {
756*0a6a1f1dSLionel Sambuc // FIXME: Refactor with EmitExprAsInit.
757f4a2713aSLionel Sambuc CharUnits Alignment = CGF.getContext().getTypeAlignInChars(AllocType);
758f4a2713aSLionel Sambuc switch (CGF.getEvaluationKind(AllocType)) {
759f4a2713aSLionel Sambuc case TEK_Scalar:
760*0a6a1f1dSLionel Sambuc CGF.EmitScalarInit(Init, nullptr,
761*0a6a1f1dSLionel Sambuc CGF.MakeAddrLValue(NewPtr, AllocType, Alignment), false);
762f4a2713aSLionel Sambuc return;
763f4a2713aSLionel Sambuc case TEK_Complex:
764f4a2713aSLionel Sambuc CGF.EmitComplexExprIntoLValue(Init, CGF.MakeAddrLValue(NewPtr, AllocType,
765f4a2713aSLionel Sambuc Alignment),
766f4a2713aSLionel Sambuc /*isInit*/ true);
767f4a2713aSLionel Sambuc return;
768f4a2713aSLionel Sambuc case TEK_Aggregate: {
769f4a2713aSLionel Sambuc AggValueSlot Slot
770f4a2713aSLionel Sambuc = AggValueSlot::forAddr(NewPtr, Alignment, AllocType.getQualifiers(),
771f4a2713aSLionel Sambuc AggValueSlot::IsDestructed,
772f4a2713aSLionel Sambuc AggValueSlot::DoesNotNeedGCBarriers,
773f4a2713aSLionel Sambuc AggValueSlot::IsNotAliased);
774f4a2713aSLionel Sambuc CGF.EmitAggExpr(Init, Slot);
775f4a2713aSLionel Sambuc return;
776f4a2713aSLionel Sambuc }
777f4a2713aSLionel Sambuc }
778f4a2713aSLionel Sambuc llvm_unreachable("bad evaluation kind");
779f4a2713aSLionel Sambuc }
780f4a2713aSLionel Sambuc
781f4a2713aSLionel Sambuc void
EmitNewArrayInitializer(const CXXNewExpr * E,QualType ElementType,llvm::Value * BeginPtr,llvm::Value * NumElements,llvm::Value * AllocSizeWithoutCookie)782f4a2713aSLionel Sambuc CodeGenFunction::EmitNewArrayInitializer(const CXXNewExpr *E,
783*0a6a1f1dSLionel Sambuc QualType ElementType,
784*0a6a1f1dSLionel Sambuc llvm::Value *BeginPtr,
785*0a6a1f1dSLionel Sambuc llvm::Value *NumElements,
786*0a6a1f1dSLionel Sambuc llvm::Value *AllocSizeWithoutCookie) {
787*0a6a1f1dSLionel Sambuc // If we have a type with trivial initialization and no initializer,
788*0a6a1f1dSLionel Sambuc // there's nothing to do.
789f4a2713aSLionel Sambuc if (!E->hasInitializer())
790*0a6a1f1dSLionel Sambuc return;
791f4a2713aSLionel Sambuc
792*0a6a1f1dSLionel Sambuc llvm::Value *CurPtr = BeginPtr;
793f4a2713aSLionel Sambuc
794*0a6a1f1dSLionel Sambuc unsigned InitListElements = 0;
795f4a2713aSLionel Sambuc
796f4a2713aSLionel Sambuc const Expr *Init = E->getInitializer();
797*0a6a1f1dSLionel Sambuc llvm::AllocaInst *EndOfInit = nullptr;
798*0a6a1f1dSLionel Sambuc QualType::DestructionKind DtorKind = ElementType.isDestructedType();
799*0a6a1f1dSLionel Sambuc EHScopeStack::stable_iterator Cleanup;
800*0a6a1f1dSLionel Sambuc llvm::Instruction *CleanupDominator = nullptr;
801*0a6a1f1dSLionel Sambuc
802f4a2713aSLionel Sambuc // If the initializer is an initializer list, first do the explicit elements.
803f4a2713aSLionel Sambuc if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
804*0a6a1f1dSLionel Sambuc InitListElements = ILE->getNumInits();
805f4a2713aSLionel Sambuc
806*0a6a1f1dSLionel Sambuc // If this is a multi-dimensional array new, we will initialize multiple
807*0a6a1f1dSLionel Sambuc // elements with each init list element.
808*0a6a1f1dSLionel Sambuc QualType AllocType = E->getAllocatedType();
809*0a6a1f1dSLionel Sambuc if (const ConstantArrayType *CAT = dyn_cast_or_null<ConstantArrayType>(
810*0a6a1f1dSLionel Sambuc AllocType->getAsArrayTypeUnsafe())) {
811*0a6a1f1dSLionel Sambuc unsigned AS = CurPtr->getType()->getPointerAddressSpace();
812*0a6a1f1dSLionel Sambuc llvm::Type *AllocPtrTy = ConvertTypeForMem(AllocType)->getPointerTo(AS);
813*0a6a1f1dSLionel Sambuc CurPtr = Builder.CreateBitCast(CurPtr, AllocPtrTy);
814*0a6a1f1dSLionel Sambuc InitListElements *= getContext().getConstantArrayElementCount(CAT);
815*0a6a1f1dSLionel Sambuc }
816*0a6a1f1dSLionel Sambuc
817*0a6a1f1dSLionel Sambuc // Enter a partial-destruction Cleanup if necessary.
818*0a6a1f1dSLionel Sambuc if (needsEHCleanup(DtorKind)) {
819*0a6a1f1dSLionel Sambuc // In principle we could tell the Cleanup where we are more
820f4a2713aSLionel Sambuc // directly, but the control flow can get so varied here that it
821f4a2713aSLionel Sambuc // would actually be quite complex. Therefore we go through an
822f4a2713aSLionel Sambuc // alloca.
823*0a6a1f1dSLionel Sambuc EndOfInit = CreateTempAlloca(BeginPtr->getType(), "array.init.end");
824*0a6a1f1dSLionel Sambuc CleanupDominator = Builder.CreateStore(BeginPtr, EndOfInit);
825*0a6a1f1dSLionel Sambuc pushIrregularPartialArrayCleanup(BeginPtr, EndOfInit, ElementType,
826*0a6a1f1dSLionel Sambuc getDestroyer(DtorKind));
827*0a6a1f1dSLionel Sambuc Cleanup = EHStack.stable_begin();
828f4a2713aSLionel Sambuc }
829f4a2713aSLionel Sambuc
830f4a2713aSLionel Sambuc for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) {
831f4a2713aSLionel Sambuc // Tell the cleanup that it needs to destroy up to this
832f4a2713aSLionel Sambuc // element. TODO: some of these stores can be trivially
833f4a2713aSLionel Sambuc // observed to be unnecessary.
834*0a6a1f1dSLionel Sambuc if (EndOfInit)
835*0a6a1f1dSLionel Sambuc Builder.CreateStore(Builder.CreateBitCast(CurPtr, BeginPtr->getType()),
836*0a6a1f1dSLionel Sambuc EndOfInit);
837*0a6a1f1dSLionel Sambuc // FIXME: If the last initializer is an incomplete initializer list for
838*0a6a1f1dSLionel Sambuc // an array, and we have an array filler, we can fold together the two
839*0a6a1f1dSLionel Sambuc // initialization loops.
840*0a6a1f1dSLionel Sambuc StoreAnyExprIntoOneUnit(*this, ILE->getInit(i),
841*0a6a1f1dSLionel Sambuc ILE->getInit(i)->getType(), CurPtr);
842*0a6a1f1dSLionel Sambuc CurPtr = Builder.CreateConstInBoundsGEP1_32(CurPtr, 1, "array.exp.next");
843f4a2713aSLionel Sambuc }
844f4a2713aSLionel Sambuc
845f4a2713aSLionel Sambuc // The remaining elements are filled with the array filler expression.
846f4a2713aSLionel Sambuc Init = ILE->getArrayFiller();
847*0a6a1f1dSLionel Sambuc
848*0a6a1f1dSLionel Sambuc // Extract the initializer for the individual array elements by pulling
849*0a6a1f1dSLionel Sambuc // out the array filler from all the nested initializer lists. This avoids
850*0a6a1f1dSLionel Sambuc // generating a nested loop for the initialization.
851*0a6a1f1dSLionel Sambuc while (Init && Init->getType()->isConstantArrayType()) {
852*0a6a1f1dSLionel Sambuc auto *SubILE = dyn_cast<InitListExpr>(Init);
853*0a6a1f1dSLionel Sambuc if (!SubILE)
854*0a6a1f1dSLionel Sambuc break;
855*0a6a1f1dSLionel Sambuc assert(SubILE->getNumInits() == 0 && "explicit inits in array filler?");
856*0a6a1f1dSLionel Sambuc Init = SubILE->getArrayFiller();
857f4a2713aSLionel Sambuc }
858f4a2713aSLionel Sambuc
859*0a6a1f1dSLionel Sambuc // Switch back to initializing one base element at a time.
860*0a6a1f1dSLionel Sambuc CurPtr = Builder.CreateBitCast(CurPtr, BeginPtr->getType());
861*0a6a1f1dSLionel Sambuc }
862f4a2713aSLionel Sambuc
863*0a6a1f1dSLionel Sambuc // Attempt to perform zero-initialization using memset.
864*0a6a1f1dSLionel Sambuc auto TryMemsetInitialization = [&]() -> bool {
865*0a6a1f1dSLionel Sambuc // FIXME: If the type is a pointer-to-data-member under the Itanium ABI,
866*0a6a1f1dSLionel Sambuc // we can initialize with a memset to -1.
867*0a6a1f1dSLionel Sambuc if (!CGM.getTypes().isZeroInitializable(ElementType))
868*0a6a1f1dSLionel Sambuc return false;
869*0a6a1f1dSLionel Sambuc
870*0a6a1f1dSLionel Sambuc // Optimization: since zero initialization will just set the memory
871*0a6a1f1dSLionel Sambuc // to all zeroes, generate a single memset to do it in one shot.
872*0a6a1f1dSLionel Sambuc
873*0a6a1f1dSLionel Sambuc // Subtract out the size of any elements we've already initialized.
874*0a6a1f1dSLionel Sambuc auto *RemainingSize = AllocSizeWithoutCookie;
875*0a6a1f1dSLionel Sambuc if (InitListElements) {
876*0a6a1f1dSLionel Sambuc // We know this can't overflow; we check this when doing the allocation.
877*0a6a1f1dSLionel Sambuc auto *InitializedSize = llvm::ConstantInt::get(
878*0a6a1f1dSLionel Sambuc RemainingSize->getType(),
879*0a6a1f1dSLionel Sambuc getContext().getTypeSizeInChars(ElementType).getQuantity() *
880*0a6a1f1dSLionel Sambuc InitListElements);
881*0a6a1f1dSLionel Sambuc RemainingSize = Builder.CreateSub(RemainingSize, InitializedSize);
882*0a6a1f1dSLionel Sambuc }
883*0a6a1f1dSLionel Sambuc
884*0a6a1f1dSLionel Sambuc // Create the memset.
885*0a6a1f1dSLionel Sambuc CharUnits Alignment = getContext().getTypeAlignInChars(ElementType);
886*0a6a1f1dSLionel Sambuc Builder.CreateMemSet(CurPtr, Builder.getInt8(0), RemainingSize,
887*0a6a1f1dSLionel Sambuc Alignment.getQuantity(), false);
888*0a6a1f1dSLionel Sambuc return true;
889*0a6a1f1dSLionel Sambuc };
890*0a6a1f1dSLionel Sambuc
891*0a6a1f1dSLionel Sambuc // If all elements have already been initialized, skip any further
892*0a6a1f1dSLionel Sambuc // initialization.
893*0a6a1f1dSLionel Sambuc llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements);
894*0a6a1f1dSLionel Sambuc if (ConstNum && ConstNum->getZExtValue() <= InitListElements) {
895*0a6a1f1dSLionel Sambuc // If there was a Cleanup, deactivate it.
896*0a6a1f1dSLionel Sambuc if (CleanupDominator)
897*0a6a1f1dSLionel Sambuc DeactivateCleanupBlock(Cleanup, CleanupDominator);
898f4a2713aSLionel Sambuc return;
899f4a2713aSLionel Sambuc }
900f4a2713aSLionel Sambuc
901*0a6a1f1dSLionel Sambuc assert(Init && "have trailing elements to initialize but no initializer");
902f4a2713aSLionel Sambuc
903*0a6a1f1dSLionel Sambuc // If this is a constructor call, try to optimize it out, and failing that
904*0a6a1f1dSLionel Sambuc // emit a single loop to initialize all remaining elements.
905*0a6a1f1dSLionel Sambuc if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
906f4a2713aSLionel Sambuc CXXConstructorDecl *Ctor = CCE->getConstructor();
907f4a2713aSLionel Sambuc if (Ctor->isTrivial()) {
908f4a2713aSLionel Sambuc // If new expression did not specify value-initialization, then there
909f4a2713aSLionel Sambuc // is no initialization.
910f4a2713aSLionel Sambuc if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty())
911f4a2713aSLionel Sambuc return;
912f4a2713aSLionel Sambuc
913*0a6a1f1dSLionel Sambuc if (TryMemsetInitialization())
914f4a2713aSLionel Sambuc return;
915f4a2713aSLionel Sambuc }
916f4a2713aSLionel Sambuc
917*0a6a1f1dSLionel Sambuc // Store the new Cleanup position for irregular Cleanups.
918*0a6a1f1dSLionel Sambuc //
919*0a6a1f1dSLionel Sambuc // FIXME: Share this cleanup with the constructor call emission rather than
920*0a6a1f1dSLionel Sambuc // having it create a cleanup of its own.
921*0a6a1f1dSLionel Sambuc if (EndOfInit) Builder.CreateStore(CurPtr, EndOfInit);
922*0a6a1f1dSLionel Sambuc
923*0a6a1f1dSLionel Sambuc // Emit a constructor call loop to initialize the remaining elements.
924*0a6a1f1dSLionel Sambuc if (InitListElements)
925*0a6a1f1dSLionel Sambuc NumElements = Builder.CreateSub(
926*0a6a1f1dSLionel Sambuc NumElements,
927*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(NumElements->getType(), InitListElements));
928*0a6a1f1dSLionel Sambuc EmitCXXAggrConstructorCall(Ctor, NumElements, CurPtr, CCE,
929f4a2713aSLionel Sambuc CCE->requiresZeroInitialization());
930f4a2713aSLionel Sambuc return;
931f4a2713aSLionel Sambuc }
932f4a2713aSLionel Sambuc
933*0a6a1f1dSLionel Sambuc // If this is value-initialization, we can usually use memset.
934*0a6a1f1dSLionel Sambuc ImplicitValueInitExpr IVIE(ElementType);
935*0a6a1f1dSLionel Sambuc if (isa<ImplicitValueInitExpr>(Init)) {
936*0a6a1f1dSLionel Sambuc if (TryMemsetInitialization())
937f4a2713aSLionel Sambuc return;
938f4a2713aSLionel Sambuc
939*0a6a1f1dSLionel Sambuc // Switch to an ImplicitValueInitExpr for the element type. This handles
940*0a6a1f1dSLionel Sambuc // only one case: multidimensional array new of pointers to members. In
941*0a6a1f1dSLionel Sambuc // all other cases, we already have an initializer for the array element.
942*0a6a1f1dSLionel Sambuc Init = &IVIE;
943*0a6a1f1dSLionel Sambuc }
944*0a6a1f1dSLionel Sambuc
945*0a6a1f1dSLionel Sambuc // At this point we should have found an initializer for the individual
946*0a6a1f1dSLionel Sambuc // elements of the array.
947*0a6a1f1dSLionel Sambuc assert(getContext().hasSameUnqualifiedType(ElementType, Init->getType()) &&
948*0a6a1f1dSLionel Sambuc "got wrong type of element to initialize");
949*0a6a1f1dSLionel Sambuc
950*0a6a1f1dSLionel Sambuc // If we have an empty initializer list, we can usually use memset.
951*0a6a1f1dSLionel Sambuc if (auto *ILE = dyn_cast<InitListExpr>(Init))
952*0a6a1f1dSLionel Sambuc if (ILE->getNumInits() == 0 && TryMemsetInitialization())
953*0a6a1f1dSLionel Sambuc return;
954*0a6a1f1dSLionel Sambuc
955*0a6a1f1dSLionel Sambuc // Create the loop blocks.
956*0a6a1f1dSLionel Sambuc llvm::BasicBlock *EntryBB = Builder.GetInsertBlock();
957*0a6a1f1dSLionel Sambuc llvm::BasicBlock *LoopBB = createBasicBlock("new.loop");
958*0a6a1f1dSLionel Sambuc llvm::BasicBlock *ContBB = createBasicBlock("new.loop.end");
959*0a6a1f1dSLionel Sambuc
960*0a6a1f1dSLionel Sambuc // Find the end of the array, hoisted out of the loop.
961*0a6a1f1dSLionel Sambuc llvm::Value *EndPtr =
962*0a6a1f1dSLionel Sambuc Builder.CreateInBoundsGEP(BeginPtr, NumElements, "array.end");
963*0a6a1f1dSLionel Sambuc
964*0a6a1f1dSLionel Sambuc // If the number of elements isn't constant, we have to now check if there is
965*0a6a1f1dSLionel Sambuc // anything left to initialize.
966*0a6a1f1dSLionel Sambuc if (!ConstNum) {
967*0a6a1f1dSLionel Sambuc llvm::Value *IsEmpty = Builder.CreateICmpEQ(CurPtr, EndPtr,
968*0a6a1f1dSLionel Sambuc "array.isempty");
969*0a6a1f1dSLionel Sambuc Builder.CreateCondBr(IsEmpty, ContBB, LoopBB);
970*0a6a1f1dSLionel Sambuc }
971*0a6a1f1dSLionel Sambuc
972*0a6a1f1dSLionel Sambuc // Enter the loop.
973*0a6a1f1dSLionel Sambuc EmitBlock(LoopBB);
974*0a6a1f1dSLionel Sambuc
975*0a6a1f1dSLionel Sambuc // Set up the current-element phi.
976*0a6a1f1dSLionel Sambuc llvm::PHINode *CurPtrPhi =
977*0a6a1f1dSLionel Sambuc Builder.CreatePHI(CurPtr->getType(), 2, "array.cur");
978*0a6a1f1dSLionel Sambuc CurPtrPhi->addIncoming(CurPtr, EntryBB);
979*0a6a1f1dSLionel Sambuc CurPtr = CurPtrPhi;
980*0a6a1f1dSLionel Sambuc
981*0a6a1f1dSLionel Sambuc // Store the new Cleanup position for irregular Cleanups.
982*0a6a1f1dSLionel Sambuc if (EndOfInit) Builder.CreateStore(CurPtr, EndOfInit);
983*0a6a1f1dSLionel Sambuc
984*0a6a1f1dSLionel Sambuc // Enter a partial-destruction Cleanup if necessary.
985*0a6a1f1dSLionel Sambuc if (!CleanupDominator && needsEHCleanup(DtorKind)) {
986*0a6a1f1dSLionel Sambuc pushRegularPartialArrayCleanup(BeginPtr, CurPtr, ElementType,
987*0a6a1f1dSLionel Sambuc getDestroyer(DtorKind));
988*0a6a1f1dSLionel Sambuc Cleanup = EHStack.stable_begin();
989*0a6a1f1dSLionel Sambuc CleanupDominator = Builder.CreateUnreachable();
990*0a6a1f1dSLionel Sambuc }
991*0a6a1f1dSLionel Sambuc
992*0a6a1f1dSLionel Sambuc // Emit the initializer into this element.
993*0a6a1f1dSLionel Sambuc StoreAnyExprIntoOneUnit(*this, Init, Init->getType(), CurPtr);
994*0a6a1f1dSLionel Sambuc
995*0a6a1f1dSLionel Sambuc // Leave the Cleanup if we entered one.
996*0a6a1f1dSLionel Sambuc if (CleanupDominator) {
997*0a6a1f1dSLionel Sambuc DeactivateCleanupBlock(Cleanup, CleanupDominator);
998*0a6a1f1dSLionel Sambuc CleanupDominator->eraseFromParent();
999*0a6a1f1dSLionel Sambuc }
1000*0a6a1f1dSLionel Sambuc
1001*0a6a1f1dSLionel Sambuc // Advance to the next element by adjusting the pointer type as necessary.
1002*0a6a1f1dSLionel Sambuc llvm::Value *NextPtr =
1003*0a6a1f1dSLionel Sambuc Builder.CreateConstInBoundsGEP1_32(CurPtr, 1, "array.next");
1004*0a6a1f1dSLionel Sambuc
1005*0a6a1f1dSLionel Sambuc // Check whether we've gotten to the end of the array and, if so,
1006*0a6a1f1dSLionel Sambuc // exit the loop.
1007*0a6a1f1dSLionel Sambuc llvm::Value *IsEnd = Builder.CreateICmpEQ(NextPtr, EndPtr, "array.atend");
1008*0a6a1f1dSLionel Sambuc Builder.CreateCondBr(IsEnd, ContBB, LoopBB);
1009*0a6a1f1dSLionel Sambuc CurPtrPhi->addIncoming(NextPtr, Builder.GetInsertBlock());
1010*0a6a1f1dSLionel Sambuc
1011*0a6a1f1dSLionel Sambuc EmitBlock(ContBB);
1012*0a6a1f1dSLionel Sambuc }
1013*0a6a1f1dSLionel Sambuc
EmitNewInitializer(CodeGenFunction & CGF,const CXXNewExpr * E,QualType ElementType,llvm::Value * NewPtr,llvm::Value * NumElements,llvm::Value * AllocSizeWithoutCookie)1014*0a6a1f1dSLionel Sambuc static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
1015*0a6a1f1dSLionel Sambuc QualType ElementType,
1016*0a6a1f1dSLionel Sambuc llvm::Value *NewPtr,
1017*0a6a1f1dSLionel Sambuc llvm::Value *NumElements,
1018*0a6a1f1dSLionel Sambuc llvm::Value *AllocSizeWithoutCookie) {
1019*0a6a1f1dSLionel Sambuc ApplyDebugLocation DL(CGF, E->getStartLoc());
1020*0a6a1f1dSLionel Sambuc if (E->isArray())
1021*0a6a1f1dSLionel Sambuc CGF.EmitNewArrayInitializer(E, ElementType, NewPtr, NumElements,
1022*0a6a1f1dSLionel Sambuc AllocSizeWithoutCookie);
1023*0a6a1f1dSLionel Sambuc else if (const Expr *Init = E->getInitializer())
1024f4a2713aSLionel Sambuc StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr);
1025f4a2713aSLionel Sambuc }
1026f4a2713aSLionel Sambuc
1027f4a2713aSLionel Sambuc /// Emit a call to an operator new or operator delete function, as implicitly
1028f4a2713aSLionel Sambuc /// created by new-expressions and delete-expressions.
EmitNewDeleteCall(CodeGenFunction & CGF,const FunctionDecl * Callee,const FunctionProtoType * CalleeType,const CallArgList & Args)1029f4a2713aSLionel Sambuc static RValue EmitNewDeleteCall(CodeGenFunction &CGF,
1030f4a2713aSLionel Sambuc const FunctionDecl *Callee,
1031f4a2713aSLionel Sambuc const FunctionProtoType *CalleeType,
1032f4a2713aSLionel Sambuc const CallArgList &Args) {
1033f4a2713aSLionel Sambuc llvm::Instruction *CallOrInvoke;
1034f4a2713aSLionel Sambuc llvm::Value *CalleeAddr = CGF.CGM.GetAddrOfFunction(Callee);
1035f4a2713aSLionel Sambuc RValue RV =
1036*0a6a1f1dSLionel Sambuc CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall(
1037*0a6a1f1dSLionel Sambuc Args, CalleeType, /*chainCall=*/false),
1038*0a6a1f1dSLionel Sambuc CalleeAddr, ReturnValueSlot(), Args, Callee, &CallOrInvoke);
1039f4a2713aSLionel Sambuc
1040f4a2713aSLionel Sambuc /// C++1y [expr.new]p10:
1041f4a2713aSLionel Sambuc /// [In a new-expression,] an implementation is allowed to omit a call
1042f4a2713aSLionel Sambuc /// to a replaceable global allocation function.
1043f4a2713aSLionel Sambuc ///
1044f4a2713aSLionel Sambuc /// We model such elidable calls with the 'builtin' attribute.
1045f4a2713aSLionel Sambuc llvm::Function *Fn = dyn_cast<llvm::Function>(CalleeAddr);
1046f4a2713aSLionel Sambuc if (Callee->isReplaceableGlobalAllocationFunction() &&
1047f4a2713aSLionel Sambuc Fn && Fn->hasFnAttribute(llvm::Attribute::NoBuiltin)) {
1048f4a2713aSLionel Sambuc // FIXME: Add addAttribute to CallSite.
1049f4a2713aSLionel Sambuc if (llvm::CallInst *CI = dyn_cast<llvm::CallInst>(CallOrInvoke))
1050f4a2713aSLionel Sambuc CI->addAttribute(llvm::AttributeSet::FunctionIndex,
1051f4a2713aSLionel Sambuc llvm::Attribute::Builtin);
1052f4a2713aSLionel Sambuc else if (llvm::InvokeInst *II = dyn_cast<llvm::InvokeInst>(CallOrInvoke))
1053f4a2713aSLionel Sambuc II->addAttribute(llvm::AttributeSet::FunctionIndex,
1054f4a2713aSLionel Sambuc llvm::Attribute::Builtin);
1055f4a2713aSLionel Sambuc else
1056f4a2713aSLionel Sambuc llvm_unreachable("unexpected kind of call instruction");
1057f4a2713aSLionel Sambuc }
1058f4a2713aSLionel Sambuc
1059f4a2713aSLionel Sambuc return RV;
1060f4a2713aSLionel Sambuc }
1061f4a2713aSLionel Sambuc
EmitBuiltinNewDeleteCall(const FunctionProtoType * Type,const Expr * Arg,bool IsDelete)1062*0a6a1f1dSLionel Sambuc RValue CodeGenFunction::EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
1063*0a6a1f1dSLionel Sambuc const Expr *Arg,
1064*0a6a1f1dSLionel Sambuc bool IsDelete) {
1065*0a6a1f1dSLionel Sambuc CallArgList Args;
1066*0a6a1f1dSLionel Sambuc const Stmt *ArgS = Arg;
1067*0a6a1f1dSLionel Sambuc EmitCallArgs(Args, *Type->param_type_begin(),
1068*0a6a1f1dSLionel Sambuc ConstExprIterator(&ArgS), ConstExprIterator(&ArgS + 1));
1069*0a6a1f1dSLionel Sambuc // Find the allocation or deallocation function that we're calling.
1070*0a6a1f1dSLionel Sambuc ASTContext &Ctx = getContext();
1071*0a6a1f1dSLionel Sambuc DeclarationName Name = Ctx.DeclarationNames
1072*0a6a1f1dSLionel Sambuc .getCXXOperatorName(IsDelete ? OO_Delete : OO_New);
1073*0a6a1f1dSLionel Sambuc for (auto *Decl : Ctx.getTranslationUnitDecl()->lookup(Name))
1074*0a6a1f1dSLionel Sambuc if (auto *FD = dyn_cast<FunctionDecl>(Decl))
1075*0a6a1f1dSLionel Sambuc if (Ctx.hasSameType(FD->getType(), QualType(Type, 0)))
1076*0a6a1f1dSLionel Sambuc return EmitNewDeleteCall(*this, cast<FunctionDecl>(Decl), Type, Args);
1077*0a6a1f1dSLionel Sambuc llvm_unreachable("predeclared global operator new/delete is missing");
1078*0a6a1f1dSLionel Sambuc }
1079*0a6a1f1dSLionel Sambuc
1080f4a2713aSLionel Sambuc namespace {
1081f4a2713aSLionel Sambuc /// A cleanup to call the given 'operator delete' function upon
1082f4a2713aSLionel Sambuc /// abnormal exit from a new expression.
1083f4a2713aSLionel Sambuc class CallDeleteDuringNew : public EHScopeStack::Cleanup {
1084f4a2713aSLionel Sambuc size_t NumPlacementArgs;
1085f4a2713aSLionel Sambuc const FunctionDecl *OperatorDelete;
1086f4a2713aSLionel Sambuc llvm::Value *Ptr;
1087f4a2713aSLionel Sambuc llvm::Value *AllocSize;
1088f4a2713aSLionel Sambuc
getPlacementArgs()1089f4a2713aSLionel Sambuc RValue *getPlacementArgs() { return reinterpret_cast<RValue*>(this+1); }
1090f4a2713aSLionel Sambuc
1091f4a2713aSLionel Sambuc public:
getExtraSize(size_t NumPlacementArgs)1092f4a2713aSLionel Sambuc static size_t getExtraSize(size_t NumPlacementArgs) {
1093f4a2713aSLionel Sambuc return NumPlacementArgs * sizeof(RValue);
1094f4a2713aSLionel Sambuc }
1095f4a2713aSLionel Sambuc
CallDeleteDuringNew(size_t NumPlacementArgs,const FunctionDecl * OperatorDelete,llvm::Value * Ptr,llvm::Value * AllocSize)1096f4a2713aSLionel Sambuc CallDeleteDuringNew(size_t NumPlacementArgs,
1097f4a2713aSLionel Sambuc const FunctionDecl *OperatorDelete,
1098f4a2713aSLionel Sambuc llvm::Value *Ptr,
1099f4a2713aSLionel Sambuc llvm::Value *AllocSize)
1100f4a2713aSLionel Sambuc : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
1101f4a2713aSLionel Sambuc Ptr(Ptr), AllocSize(AllocSize) {}
1102f4a2713aSLionel Sambuc
setPlacementArg(unsigned I,RValue Arg)1103f4a2713aSLionel Sambuc void setPlacementArg(unsigned I, RValue Arg) {
1104f4a2713aSLionel Sambuc assert(I < NumPlacementArgs && "index out of range");
1105f4a2713aSLionel Sambuc getPlacementArgs()[I] = Arg;
1106f4a2713aSLionel Sambuc }
1107f4a2713aSLionel Sambuc
Emit(CodeGenFunction & CGF,Flags flags)1108*0a6a1f1dSLionel Sambuc void Emit(CodeGenFunction &CGF, Flags flags) override {
1109f4a2713aSLionel Sambuc const FunctionProtoType *FPT
1110f4a2713aSLionel Sambuc = OperatorDelete->getType()->getAs<FunctionProtoType>();
1111*0a6a1f1dSLionel Sambuc assert(FPT->getNumParams() == NumPlacementArgs + 1 ||
1112*0a6a1f1dSLionel Sambuc (FPT->getNumParams() == 2 && NumPlacementArgs == 0));
1113f4a2713aSLionel Sambuc
1114f4a2713aSLionel Sambuc CallArgList DeleteArgs;
1115f4a2713aSLionel Sambuc
1116f4a2713aSLionel Sambuc // The first argument is always a void*.
1117*0a6a1f1dSLionel Sambuc FunctionProtoType::param_type_iterator AI = FPT->param_type_begin();
1118f4a2713aSLionel Sambuc DeleteArgs.add(RValue::get(Ptr), *AI++);
1119f4a2713aSLionel Sambuc
1120f4a2713aSLionel Sambuc // A member 'operator delete' can take an extra 'size_t' argument.
1121*0a6a1f1dSLionel Sambuc if (FPT->getNumParams() == NumPlacementArgs + 2)
1122f4a2713aSLionel Sambuc DeleteArgs.add(RValue::get(AllocSize), *AI++);
1123f4a2713aSLionel Sambuc
1124f4a2713aSLionel Sambuc // Pass the rest of the arguments, which must match exactly.
1125f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumPlacementArgs; ++I)
1126f4a2713aSLionel Sambuc DeleteArgs.add(getPlacementArgs()[I], *AI++);
1127f4a2713aSLionel Sambuc
1128f4a2713aSLionel Sambuc // Call 'operator delete'.
1129f4a2713aSLionel Sambuc EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs);
1130f4a2713aSLionel Sambuc }
1131f4a2713aSLionel Sambuc };
1132f4a2713aSLionel Sambuc
1133f4a2713aSLionel Sambuc /// A cleanup to call the given 'operator delete' function upon
1134f4a2713aSLionel Sambuc /// abnormal exit from a new expression when the new expression is
1135f4a2713aSLionel Sambuc /// conditional.
1136f4a2713aSLionel Sambuc class CallDeleteDuringConditionalNew : public EHScopeStack::Cleanup {
1137f4a2713aSLionel Sambuc size_t NumPlacementArgs;
1138f4a2713aSLionel Sambuc const FunctionDecl *OperatorDelete;
1139f4a2713aSLionel Sambuc DominatingValue<RValue>::saved_type Ptr;
1140f4a2713aSLionel Sambuc DominatingValue<RValue>::saved_type AllocSize;
1141f4a2713aSLionel Sambuc
getPlacementArgs()1142f4a2713aSLionel Sambuc DominatingValue<RValue>::saved_type *getPlacementArgs() {
1143f4a2713aSLionel Sambuc return reinterpret_cast<DominatingValue<RValue>::saved_type*>(this+1);
1144f4a2713aSLionel Sambuc }
1145f4a2713aSLionel Sambuc
1146f4a2713aSLionel Sambuc public:
getExtraSize(size_t NumPlacementArgs)1147f4a2713aSLionel Sambuc static size_t getExtraSize(size_t NumPlacementArgs) {
1148f4a2713aSLionel Sambuc return NumPlacementArgs * sizeof(DominatingValue<RValue>::saved_type);
1149f4a2713aSLionel Sambuc }
1150f4a2713aSLionel Sambuc
CallDeleteDuringConditionalNew(size_t NumPlacementArgs,const FunctionDecl * OperatorDelete,DominatingValue<RValue>::saved_type Ptr,DominatingValue<RValue>::saved_type AllocSize)1151f4a2713aSLionel Sambuc CallDeleteDuringConditionalNew(size_t NumPlacementArgs,
1152f4a2713aSLionel Sambuc const FunctionDecl *OperatorDelete,
1153f4a2713aSLionel Sambuc DominatingValue<RValue>::saved_type Ptr,
1154f4a2713aSLionel Sambuc DominatingValue<RValue>::saved_type AllocSize)
1155f4a2713aSLionel Sambuc : NumPlacementArgs(NumPlacementArgs), OperatorDelete(OperatorDelete),
1156f4a2713aSLionel Sambuc Ptr(Ptr), AllocSize(AllocSize) {}
1157f4a2713aSLionel Sambuc
setPlacementArg(unsigned I,DominatingValue<RValue>::saved_type Arg)1158f4a2713aSLionel Sambuc void setPlacementArg(unsigned I, DominatingValue<RValue>::saved_type Arg) {
1159f4a2713aSLionel Sambuc assert(I < NumPlacementArgs && "index out of range");
1160f4a2713aSLionel Sambuc getPlacementArgs()[I] = Arg;
1161f4a2713aSLionel Sambuc }
1162f4a2713aSLionel Sambuc
Emit(CodeGenFunction & CGF,Flags flags)1163*0a6a1f1dSLionel Sambuc void Emit(CodeGenFunction &CGF, Flags flags) override {
1164f4a2713aSLionel Sambuc const FunctionProtoType *FPT
1165f4a2713aSLionel Sambuc = OperatorDelete->getType()->getAs<FunctionProtoType>();
1166*0a6a1f1dSLionel Sambuc assert(FPT->getNumParams() == NumPlacementArgs + 1 ||
1167*0a6a1f1dSLionel Sambuc (FPT->getNumParams() == 2 && NumPlacementArgs == 0));
1168f4a2713aSLionel Sambuc
1169f4a2713aSLionel Sambuc CallArgList DeleteArgs;
1170f4a2713aSLionel Sambuc
1171f4a2713aSLionel Sambuc // The first argument is always a void*.
1172*0a6a1f1dSLionel Sambuc FunctionProtoType::param_type_iterator AI = FPT->param_type_begin();
1173f4a2713aSLionel Sambuc DeleteArgs.add(Ptr.restore(CGF), *AI++);
1174f4a2713aSLionel Sambuc
1175f4a2713aSLionel Sambuc // A member 'operator delete' can take an extra 'size_t' argument.
1176*0a6a1f1dSLionel Sambuc if (FPT->getNumParams() == NumPlacementArgs + 2) {
1177f4a2713aSLionel Sambuc RValue RV = AllocSize.restore(CGF);
1178f4a2713aSLionel Sambuc DeleteArgs.add(RV, *AI++);
1179f4a2713aSLionel Sambuc }
1180f4a2713aSLionel Sambuc
1181f4a2713aSLionel Sambuc // Pass the rest of the arguments, which must match exactly.
1182f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumPlacementArgs; ++I) {
1183f4a2713aSLionel Sambuc RValue RV = getPlacementArgs()[I].restore(CGF);
1184f4a2713aSLionel Sambuc DeleteArgs.add(RV, *AI++);
1185f4a2713aSLionel Sambuc }
1186f4a2713aSLionel Sambuc
1187f4a2713aSLionel Sambuc // Call 'operator delete'.
1188f4a2713aSLionel Sambuc EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs);
1189f4a2713aSLionel Sambuc }
1190f4a2713aSLionel Sambuc };
1191f4a2713aSLionel Sambuc }
1192f4a2713aSLionel Sambuc
1193f4a2713aSLionel Sambuc /// Enter a cleanup to call 'operator delete' if the initializer in a
1194f4a2713aSLionel Sambuc /// new-expression throws.
EnterNewDeleteCleanup(CodeGenFunction & CGF,const CXXNewExpr * E,llvm::Value * NewPtr,llvm::Value * AllocSize,const CallArgList & NewArgs)1195f4a2713aSLionel Sambuc static void EnterNewDeleteCleanup(CodeGenFunction &CGF,
1196f4a2713aSLionel Sambuc const CXXNewExpr *E,
1197f4a2713aSLionel Sambuc llvm::Value *NewPtr,
1198f4a2713aSLionel Sambuc llvm::Value *AllocSize,
1199f4a2713aSLionel Sambuc const CallArgList &NewArgs) {
1200f4a2713aSLionel Sambuc // If we're not inside a conditional branch, then the cleanup will
1201f4a2713aSLionel Sambuc // dominate and we can do the easier (and more efficient) thing.
1202f4a2713aSLionel Sambuc if (!CGF.isInConditionalBranch()) {
1203f4a2713aSLionel Sambuc CallDeleteDuringNew *Cleanup = CGF.EHStack
1204f4a2713aSLionel Sambuc .pushCleanupWithExtra<CallDeleteDuringNew>(EHCleanup,
1205f4a2713aSLionel Sambuc E->getNumPlacementArgs(),
1206f4a2713aSLionel Sambuc E->getOperatorDelete(),
1207f4a2713aSLionel Sambuc NewPtr, AllocSize);
1208f4a2713aSLionel Sambuc for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
1209f4a2713aSLionel Sambuc Cleanup->setPlacementArg(I, NewArgs[I+1].RV);
1210f4a2713aSLionel Sambuc
1211f4a2713aSLionel Sambuc return;
1212f4a2713aSLionel Sambuc }
1213f4a2713aSLionel Sambuc
1214f4a2713aSLionel Sambuc // Otherwise, we need to save all this stuff.
1215f4a2713aSLionel Sambuc DominatingValue<RValue>::saved_type SavedNewPtr =
1216f4a2713aSLionel Sambuc DominatingValue<RValue>::save(CGF, RValue::get(NewPtr));
1217f4a2713aSLionel Sambuc DominatingValue<RValue>::saved_type SavedAllocSize =
1218f4a2713aSLionel Sambuc DominatingValue<RValue>::save(CGF, RValue::get(AllocSize));
1219f4a2713aSLionel Sambuc
1220f4a2713aSLionel Sambuc CallDeleteDuringConditionalNew *Cleanup = CGF.EHStack
1221f4a2713aSLionel Sambuc .pushCleanupWithExtra<CallDeleteDuringConditionalNew>(EHCleanup,
1222f4a2713aSLionel Sambuc E->getNumPlacementArgs(),
1223f4a2713aSLionel Sambuc E->getOperatorDelete(),
1224f4a2713aSLionel Sambuc SavedNewPtr,
1225f4a2713aSLionel Sambuc SavedAllocSize);
1226f4a2713aSLionel Sambuc for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I)
1227f4a2713aSLionel Sambuc Cleanup->setPlacementArg(I,
1228f4a2713aSLionel Sambuc DominatingValue<RValue>::save(CGF, NewArgs[I+1].RV));
1229f4a2713aSLionel Sambuc
1230f4a2713aSLionel Sambuc CGF.initFullExprCleanup();
1231f4a2713aSLionel Sambuc }
1232f4a2713aSLionel Sambuc
EmitCXXNewExpr(const CXXNewExpr * E)1233f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
1234f4a2713aSLionel Sambuc // The element type being allocated.
1235f4a2713aSLionel Sambuc QualType allocType = getContext().getBaseElementType(E->getAllocatedType());
1236f4a2713aSLionel Sambuc
1237f4a2713aSLionel Sambuc // 1. Build a call to the allocation function.
1238f4a2713aSLionel Sambuc FunctionDecl *allocator = E->getOperatorNew();
1239f4a2713aSLionel Sambuc const FunctionProtoType *allocatorType =
1240f4a2713aSLionel Sambuc allocator->getType()->castAs<FunctionProtoType>();
1241f4a2713aSLionel Sambuc
1242f4a2713aSLionel Sambuc CallArgList allocatorArgs;
1243f4a2713aSLionel Sambuc
1244f4a2713aSLionel Sambuc // The allocation size is the first argument.
1245f4a2713aSLionel Sambuc QualType sizeType = getContext().getSizeType();
1246f4a2713aSLionel Sambuc
1247f4a2713aSLionel Sambuc // If there is a brace-initializer, cannot allocate fewer elements than inits.
1248f4a2713aSLionel Sambuc unsigned minElements = 0;
1249f4a2713aSLionel Sambuc if (E->isArray() && E->hasInitializer()) {
1250f4a2713aSLionel Sambuc if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer()))
1251f4a2713aSLionel Sambuc minElements = ILE->getNumInits();
1252f4a2713aSLionel Sambuc }
1253f4a2713aSLionel Sambuc
1254*0a6a1f1dSLionel Sambuc llvm::Value *numElements = nullptr;
1255*0a6a1f1dSLionel Sambuc llvm::Value *allocSizeWithoutCookie = nullptr;
1256f4a2713aSLionel Sambuc llvm::Value *allocSize =
1257f4a2713aSLionel Sambuc EmitCXXNewAllocSize(*this, E, minElements, numElements,
1258f4a2713aSLionel Sambuc allocSizeWithoutCookie);
1259f4a2713aSLionel Sambuc
1260f4a2713aSLionel Sambuc allocatorArgs.add(RValue::get(allocSize), sizeType);
1261f4a2713aSLionel Sambuc
1262f4a2713aSLionel Sambuc // We start at 1 here because the first argument (the allocation size)
1263f4a2713aSLionel Sambuc // has already been emitted.
1264*0a6a1f1dSLionel Sambuc EmitCallArgs(allocatorArgs, allocatorType, E->placement_arg_begin(),
1265*0a6a1f1dSLionel Sambuc E->placement_arg_end(), /* CalleeDecl */ nullptr,
1266*0a6a1f1dSLionel Sambuc /*ParamsToSkip*/ 1);
1267f4a2713aSLionel Sambuc
1268f4a2713aSLionel Sambuc // Emit the allocation call. If the allocator is a global placement
1269f4a2713aSLionel Sambuc // operator, just "inline" it directly.
1270f4a2713aSLionel Sambuc RValue RV;
1271f4a2713aSLionel Sambuc if (allocator->isReservedGlobalPlacementOperator()) {
1272f4a2713aSLionel Sambuc assert(allocatorArgs.size() == 2);
1273f4a2713aSLionel Sambuc RV = allocatorArgs[1].RV;
1274f4a2713aSLionel Sambuc // TODO: kill any unnecessary computations done for the size
1275f4a2713aSLionel Sambuc // argument.
1276f4a2713aSLionel Sambuc } else {
1277f4a2713aSLionel Sambuc RV = EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs);
1278f4a2713aSLionel Sambuc }
1279f4a2713aSLionel Sambuc
1280f4a2713aSLionel Sambuc // Emit a null check on the allocation result if the allocation
1281f4a2713aSLionel Sambuc // function is allowed to return null (because it has a non-throwing
1282f4a2713aSLionel Sambuc // exception spec; for this part, we inline
1283f4a2713aSLionel Sambuc // CXXNewExpr::shouldNullCheckAllocation()) and we have an
1284f4a2713aSLionel Sambuc // interesting initializer.
1285f4a2713aSLionel Sambuc bool nullCheck = allocatorType->isNothrow(getContext()) &&
1286f4a2713aSLionel Sambuc (!allocType.isPODType(getContext()) || E->hasInitializer());
1287f4a2713aSLionel Sambuc
1288*0a6a1f1dSLionel Sambuc llvm::BasicBlock *nullCheckBB = nullptr;
1289*0a6a1f1dSLionel Sambuc llvm::BasicBlock *contBB = nullptr;
1290f4a2713aSLionel Sambuc
1291f4a2713aSLionel Sambuc llvm::Value *allocation = RV.getScalarVal();
1292f4a2713aSLionel Sambuc unsigned AS = allocation->getType()->getPointerAddressSpace();
1293f4a2713aSLionel Sambuc
1294f4a2713aSLionel Sambuc // The null-check means that the initializer is conditionally
1295f4a2713aSLionel Sambuc // evaluated.
1296f4a2713aSLionel Sambuc ConditionalEvaluation conditional(*this);
1297f4a2713aSLionel Sambuc
1298f4a2713aSLionel Sambuc if (nullCheck) {
1299f4a2713aSLionel Sambuc conditional.begin(*this);
1300f4a2713aSLionel Sambuc
1301f4a2713aSLionel Sambuc nullCheckBB = Builder.GetInsertBlock();
1302f4a2713aSLionel Sambuc llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull");
1303f4a2713aSLionel Sambuc contBB = createBasicBlock("new.cont");
1304f4a2713aSLionel Sambuc
1305f4a2713aSLionel Sambuc llvm::Value *isNull = Builder.CreateIsNull(allocation, "new.isnull");
1306f4a2713aSLionel Sambuc Builder.CreateCondBr(isNull, contBB, notNullBB);
1307f4a2713aSLionel Sambuc EmitBlock(notNullBB);
1308f4a2713aSLionel Sambuc }
1309f4a2713aSLionel Sambuc
1310f4a2713aSLionel Sambuc // If there's an operator delete, enter a cleanup to call it if an
1311f4a2713aSLionel Sambuc // exception is thrown.
1312f4a2713aSLionel Sambuc EHScopeStack::stable_iterator operatorDeleteCleanup;
1313*0a6a1f1dSLionel Sambuc llvm::Instruction *cleanupDominator = nullptr;
1314f4a2713aSLionel Sambuc if (E->getOperatorDelete() &&
1315f4a2713aSLionel Sambuc !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) {
1316f4a2713aSLionel Sambuc EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocatorArgs);
1317f4a2713aSLionel Sambuc operatorDeleteCleanup = EHStack.stable_begin();
1318f4a2713aSLionel Sambuc cleanupDominator = Builder.CreateUnreachable();
1319f4a2713aSLionel Sambuc }
1320f4a2713aSLionel Sambuc
1321f4a2713aSLionel Sambuc assert((allocSize == allocSizeWithoutCookie) ==
1322f4a2713aSLionel Sambuc CalculateCookiePadding(*this, E).isZero());
1323f4a2713aSLionel Sambuc if (allocSize != allocSizeWithoutCookie) {
1324f4a2713aSLionel Sambuc assert(E->isArray());
1325f4a2713aSLionel Sambuc allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
1326f4a2713aSLionel Sambuc numElements,
1327f4a2713aSLionel Sambuc E, allocType);
1328f4a2713aSLionel Sambuc }
1329f4a2713aSLionel Sambuc
1330f4a2713aSLionel Sambuc llvm::Type *elementPtrTy
1331f4a2713aSLionel Sambuc = ConvertTypeForMem(allocType)->getPointerTo(AS);
1332f4a2713aSLionel Sambuc llvm::Value *result = Builder.CreateBitCast(allocation, elementPtrTy);
1333f4a2713aSLionel Sambuc
1334f4a2713aSLionel Sambuc EmitNewInitializer(*this, E, allocType, result, numElements,
1335f4a2713aSLionel Sambuc allocSizeWithoutCookie);
1336f4a2713aSLionel Sambuc if (E->isArray()) {
1337f4a2713aSLionel Sambuc // NewPtr is a pointer to the base element type. If we're
1338f4a2713aSLionel Sambuc // allocating an array of arrays, we'll need to cast back to the
1339f4a2713aSLionel Sambuc // array pointer type.
1340f4a2713aSLionel Sambuc llvm::Type *resultType = ConvertTypeForMem(E->getType());
1341f4a2713aSLionel Sambuc if (result->getType() != resultType)
1342f4a2713aSLionel Sambuc result = Builder.CreateBitCast(result, resultType);
1343f4a2713aSLionel Sambuc }
1344f4a2713aSLionel Sambuc
1345f4a2713aSLionel Sambuc // Deactivate the 'operator delete' cleanup if we finished
1346f4a2713aSLionel Sambuc // initialization.
1347f4a2713aSLionel Sambuc if (operatorDeleteCleanup.isValid()) {
1348f4a2713aSLionel Sambuc DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator);
1349f4a2713aSLionel Sambuc cleanupDominator->eraseFromParent();
1350f4a2713aSLionel Sambuc }
1351f4a2713aSLionel Sambuc
1352f4a2713aSLionel Sambuc if (nullCheck) {
1353f4a2713aSLionel Sambuc conditional.end(*this);
1354f4a2713aSLionel Sambuc
1355f4a2713aSLionel Sambuc llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
1356f4a2713aSLionel Sambuc EmitBlock(contBB);
1357f4a2713aSLionel Sambuc
1358f4a2713aSLionel Sambuc llvm::PHINode *PHI = Builder.CreatePHI(result->getType(), 2);
1359f4a2713aSLionel Sambuc PHI->addIncoming(result, notNullBB);
1360f4a2713aSLionel Sambuc PHI->addIncoming(llvm::Constant::getNullValue(result->getType()),
1361f4a2713aSLionel Sambuc nullCheckBB);
1362f4a2713aSLionel Sambuc
1363f4a2713aSLionel Sambuc result = PHI;
1364f4a2713aSLionel Sambuc }
1365f4a2713aSLionel Sambuc
1366f4a2713aSLionel Sambuc return result;
1367f4a2713aSLionel Sambuc }
1368f4a2713aSLionel Sambuc
EmitDeleteCall(const FunctionDecl * DeleteFD,llvm::Value * Ptr,QualType DeleteTy)1369f4a2713aSLionel Sambuc void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD,
1370f4a2713aSLionel Sambuc llvm::Value *Ptr,
1371f4a2713aSLionel Sambuc QualType DeleteTy) {
1372f4a2713aSLionel Sambuc assert(DeleteFD->getOverloadedOperator() == OO_Delete);
1373f4a2713aSLionel Sambuc
1374f4a2713aSLionel Sambuc const FunctionProtoType *DeleteFTy =
1375f4a2713aSLionel Sambuc DeleteFD->getType()->getAs<FunctionProtoType>();
1376f4a2713aSLionel Sambuc
1377f4a2713aSLionel Sambuc CallArgList DeleteArgs;
1378f4a2713aSLionel Sambuc
1379f4a2713aSLionel Sambuc // Check if we need to pass the size to the delete operator.
1380*0a6a1f1dSLionel Sambuc llvm::Value *Size = nullptr;
1381f4a2713aSLionel Sambuc QualType SizeTy;
1382*0a6a1f1dSLionel Sambuc if (DeleteFTy->getNumParams() == 2) {
1383*0a6a1f1dSLionel Sambuc SizeTy = DeleteFTy->getParamType(1);
1384f4a2713aSLionel Sambuc CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
1385f4a2713aSLionel Sambuc Size = llvm::ConstantInt::get(ConvertType(SizeTy),
1386f4a2713aSLionel Sambuc DeleteTypeSize.getQuantity());
1387f4a2713aSLionel Sambuc }
1388f4a2713aSLionel Sambuc
1389*0a6a1f1dSLionel Sambuc QualType ArgTy = DeleteFTy->getParamType(0);
1390f4a2713aSLionel Sambuc llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
1391f4a2713aSLionel Sambuc DeleteArgs.add(RValue::get(DeletePtr), ArgTy);
1392f4a2713aSLionel Sambuc
1393f4a2713aSLionel Sambuc if (Size)
1394f4a2713aSLionel Sambuc DeleteArgs.add(RValue::get(Size), SizeTy);
1395f4a2713aSLionel Sambuc
1396f4a2713aSLionel Sambuc // Emit the call to delete.
1397f4a2713aSLionel Sambuc EmitNewDeleteCall(*this, DeleteFD, DeleteFTy, DeleteArgs);
1398f4a2713aSLionel Sambuc }
1399f4a2713aSLionel Sambuc
1400f4a2713aSLionel Sambuc namespace {
1401f4a2713aSLionel Sambuc /// Calls the given 'operator delete' on a single object.
1402f4a2713aSLionel Sambuc struct CallObjectDelete : EHScopeStack::Cleanup {
1403f4a2713aSLionel Sambuc llvm::Value *Ptr;
1404f4a2713aSLionel Sambuc const FunctionDecl *OperatorDelete;
1405f4a2713aSLionel Sambuc QualType ElementType;
1406f4a2713aSLionel Sambuc
CallObjectDelete__anonbe0810ed0311::CallObjectDelete1407f4a2713aSLionel Sambuc CallObjectDelete(llvm::Value *Ptr,
1408f4a2713aSLionel Sambuc const FunctionDecl *OperatorDelete,
1409f4a2713aSLionel Sambuc QualType ElementType)
1410f4a2713aSLionel Sambuc : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
1411f4a2713aSLionel Sambuc
Emit__anonbe0810ed0311::CallObjectDelete1412*0a6a1f1dSLionel Sambuc void Emit(CodeGenFunction &CGF, Flags flags) override {
1413f4a2713aSLionel Sambuc CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
1414f4a2713aSLionel Sambuc }
1415f4a2713aSLionel Sambuc };
1416f4a2713aSLionel Sambuc }
1417f4a2713aSLionel Sambuc
1418*0a6a1f1dSLionel Sambuc void
pushCallObjectDeleteCleanup(const FunctionDecl * OperatorDelete,llvm::Value * CompletePtr,QualType ElementType)1419*0a6a1f1dSLionel Sambuc CodeGenFunction::pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1420*0a6a1f1dSLionel Sambuc llvm::Value *CompletePtr,
1421*0a6a1f1dSLionel Sambuc QualType ElementType) {
1422*0a6a1f1dSLionel Sambuc EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, CompletePtr,
1423*0a6a1f1dSLionel Sambuc OperatorDelete, ElementType);
1424*0a6a1f1dSLionel Sambuc }
1425*0a6a1f1dSLionel Sambuc
1426f4a2713aSLionel Sambuc /// Emit the code for deleting a single object.
EmitObjectDelete(CodeGenFunction & CGF,const CXXDeleteExpr * DE,llvm::Value * Ptr,QualType ElementType)1427f4a2713aSLionel Sambuc static void EmitObjectDelete(CodeGenFunction &CGF,
1428*0a6a1f1dSLionel Sambuc const CXXDeleteExpr *DE,
1429f4a2713aSLionel Sambuc llvm::Value *Ptr,
1430*0a6a1f1dSLionel Sambuc QualType ElementType) {
1431f4a2713aSLionel Sambuc // Find the destructor for the type, if applicable. If the
1432f4a2713aSLionel Sambuc // destructor is virtual, we'll just emit the vcall and return.
1433*0a6a1f1dSLionel Sambuc const CXXDestructorDecl *Dtor = nullptr;
1434f4a2713aSLionel Sambuc if (const RecordType *RT = ElementType->getAs<RecordType>()) {
1435f4a2713aSLionel Sambuc CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1436f4a2713aSLionel Sambuc if (RD->hasDefinition() && !RD->hasTrivialDestructor()) {
1437f4a2713aSLionel Sambuc Dtor = RD->getDestructor();
1438f4a2713aSLionel Sambuc
1439f4a2713aSLionel Sambuc if (Dtor->isVirtual()) {
1440*0a6a1f1dSLionel Sambuc CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
1441*0a6a1f1dSLionel Sambuc Dtor);
1442f4a2713aSLionel Sambuc return;
1443f4a2713aSLionel Sambuc }
1444f4a2713aSLionel Sambuc }
1445f4a2713aSLionel Sambuc }
1446f4a2713aSLionel Sambuc
1447f4a2713aSLionel Sambuc // Make sure that we call delete even if the dtor throws.
1448f4a2713aSLionel Sambuc // This doesn't have to a conditional cleanup because we're going
1449f4a2713aSLionel Sambuc // to pop it off in a second.
1450*0a6a1f1dSLionel Sambuc const FunctionDecl *OperatorDelete = DE->getOperatorDelete();
1451f4a2713aSLionel Sambuc CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1452f4a2713aSLionel Sambuc Ptr, OperatorDelete, ElementType);
1453f4a2713aSLionel Sambuc
1454f4a2713aSLionel Sambuc if (Dtor)
1455f4a2713aSLionel Sambuc CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1456f4a2713aSLionel Sambuc /*ForVirtualBase=*/false,
1457f4a2713aSLionel Sambuc /*Delegating=*/false,
1458f4a2713aSLionel Sambuc Ptr);
1459f4a2713aSLionel Sambuc else if (CGF.getLangOpts().ObjCAutoRefCount &&
1460f4a2713aSLionel Sambuc ElementType->isObjCLifetimeType()) {
1461f4a2713aSLionel Sambuc switch (ElementType.getObjCLifetime()) {
1462f4a2713aSLionel Sambuc case Qualifiers::OCL_None:
1463f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone:
1464f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing:
1465f4a2713aSLionel Sambuc break;
1466f4a2713aSLionel Sambuc
1467f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong: {
1468f4a2713aSLionel Sambuc // Load the pointer value.
1469f4a2713aSLionel Sambuc llvm::Value *PtrValue = CGF.Builder.CreateLoad(Ptr,
1470f4a2713aSLionel Sambuc ElementType.isVolatileQualified());
1471f4a2713aSLionel Sambuc
1472f4a2713aSLionel Sambuc CGF.EmitARCRelease(PtrValue, ARCPreciseLifetime);
1473f4a2713aSLionel Sambuc break;
1474f4a2713aSLionel Sambuc }
1475f4a2713aSLionel Sambuc
1476f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak:
1477f4a2713aSLionel Sambuc CGF.EmitARCDestroyWeak(Ptr);
1478f4a2713aSLionel Sambuc break;
1479f4a2713aSLionel Sambuc }
1480f4a2713aSLionel Sambuc }
1481f4a2713aSLionel Sambuc
1482f4a2713aSLionel Sambuc CGF.PopCleanupBlock();
1483f4a2713aSLionel Sambuc }
1484f4a2713aSLionel Sambuc
1485f4a2713aSLionel Sambuc namespace {
1486f4a2713aSLionel Sambuc /// Calls the given 'operator delete' on an array of objects.
1487f4a2713aSLionel Sambuc struct CallArrayDelete : EHScopeStack::Cleanup {
1488f4a2713aSLionel Sambuc llvm::Value *Ptr;
1489f4a2713aSLionel Sambuc const FunctionDecl *OperatorDelete;
1490f4a2713aSLionel Sambuc llvm::Value *NumElements;
1491f4a2713aSLionel Sambuc QualType ElementType;
1492f4a2713aSLionel Sambuc CharUnits CookieSize;
1493f4a2713aSLionel Sambuc
CallArrayDelete__anonbe0810ed0411::CallArrayDelete1494f4a2713aSLionel Sambuc CallArrayDelete(llvm::Value *Ptr,
1495f4a2713aSLionel Sambuc const FunctionDecl *OperatorDelete,
1496f4a2713aSLionel Sambuc llvm::Value *NumElements,
1497f4a2713aSLionel Sambuc QualType ElementType,
1498f4a2713aSLionel Sambuc CharUnits CookieSize)
1499f4a2713aSLionel Sambuc : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
1500f4a2713aSLionel Sambuc ElementType(ElementType), CookieSize(CookieSize) {}
1501f4a2713aSLionel Sambuc
Emit__anonbe0810ed0411::CallArrayDelete1502*0a6a1f1dSLionel Sambuc void Emit(CodeGenFunction &CGF, Flags flags) override {
1503f4a2713aSLionel Sambuc const FunctionProtoType *DeleteFTy =
1504f4a2713aSLionel Sambuc OperatorDelete->getType()->getAs<FunctionProtoType>();
1505*0a6a1f1dSLionel Sambuc assert(DeleteFTy->getNumParams() == 1 || DeleteFTy->getNumParams() == 2);
1506f4a2713aSLionel Sambuc
1507f4a2713aSLionel Sambuc CallArgList Args;
1508f4a2713aSLionel Sambuc
1509f4a2713aSLionel Sambuc // Pass the pointer as the first argument.
1510*0a6a1f1dSLionel Sambuc QualType VoidPtrTy = DeleteFTy->getParamType(0);
1511f4a2713aSLionel Sambuc llvm::Value *DeletePtr
1512f4a2713aSLionel Sambuc = CGF.Builder.CreateBitCast(Ptr, CGF.ConvertType(VoidPtrTy));
1513f4a2713aSLionel Sambuc Args.add(RValue::get(DeletePtr), VoidPtrTy);
1514f4a2713aSLionel Sambuc
1515f4a2713aSLionel Sambuc // Pass the original requested size as the second argument.
1516*0a6a1f1dSLionel Sambuc if (DeleteFTy->getNumParams() == 2) {
1517*0a6a1f1dSLionel Sambuc QualType size_t = DeleteFTy->getParamType(1);
1518f4a2713aSLionel Sambuc llvm::IntegerType *SizeTy
1519f4a2713aSLionel Sambuc = cast<llvm::IntegerType>(CGF.ConvertType(size_t));
1520f4a2713aSLionel Sambuc
1521f4a2713aSLionel Sambuc CharUnits ElementTypeSize =
1522f4a2713aSLionel Sambuc CGF.CGM.getContext().getTypeSizeInChars(ElementType);
1523f4a2713aSLionel Sambuc
1524f4a2713aSLionel Sambuc // The size of an element, multiplied by the number of elements.
1525f4a2713aSLionel Sambuc llvm::Value *Size
1526f4a2713aSLionel Sambuc = llvm::ConstantInt::get(SizeTy, ElementTypeSize.getQuantity());
1527f4a2713aSLionel Sambuc Size = CGF.Builder.CreateMul(Size, NumElements);
1528f4a2713aSLionel Sambuc
1529f4a2713aSLionel Sambuc // Plus the size of the cookie if applicable.
1530f4a2713aSLionel Sambuc if (!CookieSize.isZero()) {
1531f4a2713aSLionel Sambuc llvm::Value *CookieSizeV
1532f4a2713aSLionel Sambuc = llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity());
1533f4a2713aSLionel Sambuc Size = CGF.Builder.CreateAdd(Size, CookieSizeV);
1534f4a2713aSLionel Sambuc }
1535f4a2713aSLionel Sambuc
1536f4a2713aSLionel Sambuc Args.add(RValue::get(Size), size_t);
1537f4a2713aSLionel Sambuc }
1538f4a2713aSLionel Sambuc
1539f4a2713aSLionel Sambuc // Emit the call to delete.
1540f4a2713aSLionel Sambuc EmitNewDeleteCall(CGF, OperatorDelete, DeleteFTy, Args);
1541f4a2713aSLionel Sambuc }
1542f4a2713aSLionel Sambuc };
1543f4a2713aSLionel Sambuc }
1544f4a2713aSLionel Sambuc
1545f4a2713aSLionel Sambuc /// Emit the code for deleting an array of objects.
EmitArrayDelete(CodeGenFunction & CGF,const CXXDeleteExpr * E,llvm::Value * deletedPtr,QualType elementType)1546f4a2713aSLionel Sambuc static void EmitArrayDelete(CodeGenFunction &CGF,
1547f4a2713aSLionel Sambuc const CXXDeleteExpr *E,
1548f4a2713aSLionel Sambuc llvm::Value *deletedPtr,
1549f4a2713aSLionel Sambuc QualType elementType) {
1550*0a6a1f1dSLionel Sambuc llvm::Value *numElements = nullptr;
1551*0a6a1f1dSLionel Sambuc llvm::Value *allocatedPtr = nullptr;
1552f4a2713aSLionel Sambuc CharUnits cookieSize;
1553f4a2713aSLionel Sambuc CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType,
1554f4a2713aSLionel Sambuc numElements, allocatedPtr, cookieSize);
1555f4a2713aSLionel Sambuc
1556f4a2713aSLionel Sambuc assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer");
1557f4a2713aSLionel Sambuc
1558f4a2713aSLionel Sambuc // Make sure that we call delete even if one of the dtors throws.
1559f4a2713aSLionel Sambuc const FunctionDecl *operatorDelete = E->getOperatorDelete();
1560f4a2713aSLionel Sambuc CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
1561f4a2713aSLionel Sambuc allocatedPtr, operatorDelete,
1562f4a2713aSLionel Sambuc numElements, elementType,
1563f4a2713aSLionel Sambuc cookieSize);
1564f4a2713aSLionel Sambuc
1565f4a2713aSLionel Sambuc // Destroy the elements.
1566f4a2713aSLionel Sambuc if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) {
1567f4a2713aSLionel Sambuc assert(numElements && "no element count for a type with a destructor!");
1568f4a2713aSLionel Sambuc
1569f4a2713aSLionel Sambuc llvm::Value *arrayEnd =
1570f4a2713aSLionel Sambuc CGF.Builder.CreateInBoundsGEP(deletedPtr, numElements, "delete.end");
1571f4a2713aSLionel Sambuc
1572f4a2713aSLionel Sambuc // Note that it is legal to allocate a zero-length array, and we
1573f4a2713aSLionel Sambuc // can never fold the check away because the length should always
1574f4a2713aSLionel Sambuc // come from a cookie.
1575f4a2713aSLionel Sambuc CGF.emitArrayDestroy(deletedPtr, arrayEnd, elementType,
1576f4a2713aSLionel Sambuc CGF.getDestroyer(dtorKind),
1577f4a2713aSLionel Sambuc /*checkZeroLength*/ true,
1578f4a2713aSLionel Sambuc CGF.needsEHCleanup(dtorKind));
1579f4a2713aSLionel Sambuc }
1580f4a2713aSLionel Sambuc
1581f4a2713aSLionel Sambuc // Pop the cleanup block.
1582f4a2713aSLionel Sambuc CGF.PopCleanupBlock();
1583f4a2713aSLionel Sambuc }
1584f4a2713aSLionel Sambuc
EmitCXXDeleteExpr(const CXXDeleteExpr * E)1585f4a2713aSLionel Sambuc void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) {
1586f4a2713aSLionel Sambuc const Expr *Arg = E->getArgument();
1587f4a2713aSLionel Sambuc llvm::Value *Ptr = EmitScalarExpr(Arg);
1588f4a2713aSLionel Sambuc
1589f4a2713aSLionel Sambuc // Null check the pointer.
1590f4a2713aSLionel Sambuc llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
1591f4a2713aSLionel Sambuc llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
1592f4a2713aSLionel Sambuc
1593f4a2713aSLionel Sambuc llvm::Value *IsNull = Builder.CreateIsNull(Ptr, "isnull");
1594f4a2713aSLionel Sambuc
1595f4a2713aSLionel Sambuc Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
1596f4a2713aSLionel Sambuc EmitBlock(DeleteNotNull);
1597f4a2713aSLionel Sambuc
1598f4a2713aSLionel Sambuc // We might be deleting a pointer to array. If so, GEP down to the
1599f4a2713aSLionel Sambuc // first non-array element.
1600f4a2713aSLionel Sambuc // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
1601f4a2713aSLionel Sambuc QualType DeleteTy = Arg->getType()->getAs<PointerType>()->getPointeeType();
1602f4a2713aSLionel Sambuc if (DeleteTy->isConstantArrayType()) {
1603f4a2713aSLionel Sambuc llvm::Value *Zero = Builder.getInt32(0);
1604f4a2713aSLionel Sambuc SmallVector<llvm::Value*,8> GEP;
1605f4a2713aSLionel Sambuc
1606f4a2713aSLionel Sambuc GEP.push_back(Zero); // point at the outermost array
1607f4a2713aSLionel Sambuc
1608f4a2713aSLionel Sambuc // For each layer of array type we're pointing at:
1609f4a2713aSLionel Sambuc while (const ConstantArrayType *Arr
1610f4a2713aSLionel Sambuc = getContext().getAsConstantArrayType(DeleteTy)) {
1611f4a2713aSLionel Sambuc // 1. Unpeel the array type.
1612f4a2713aSLionel Sambuc DeleteTy = Arr->getElementType();
1613f4a2713aSLionel Sambuc
1614f4a2713aSLionel Sambuc // 2. GEP to the first element of the array.
1615f4a2713aSLionel Sambuc GEP.push_back(Zero);
1616f4a2713aSLionel Sambuc }
1617f4a2713aSLionel Sambuc
1618f4a2713aSLionel Sambuc Ptr = Builder.CreateInBoundsGEP(Ptr, GEP, "del.first");
1619f4a2713aSLionel Sambuc }
1620f4a2713aSLionel Sambuc
1621f4a2713aSLionel Sambuc assert(ConvertTypeForMem(DeleteTy) ==
1622f4a2713aSLionel Sambuc cast<llvm::PointerType>(Ptr->getType())->getElementType());
1623f4a2713aSLionel Sambuc
1624f4a2713aSLionel Sambuc if (E->isArrayForm()) {
1625f4a2713aSLionel Sambuc EmitArrayDelete(*this, E, Ptr, DeleteTy);
1626f4a2713aSLionel Sambuc } else {
1627*0a6a1f1dSLionel Sambuc EmitObjectDelete(*this, E, Ptr, DeleteTy);
1628f4a2713aSLionel Sambuc }
1629f4a2713aSLionel Sambuc
1630f4a2713aSLionel Sambuc EmitBlock(DeleteEnd);
1631f4a2713aSLionel Sambuc }
1632f4a2713aSLionel Sambuc
isGLValueFromPointerDeref(const Expr * E)1633*0a6a1f1dSLionel Sambuc static bool isGLValueFromPointerDeref(const Expr *E) {
1634*0a6a1f1dSLionel Sambuc E = E->IgnoreParens();
1635f4a2713aSLionel Sambuc
1636*0a6a1f1dSLionel Sambuc if (const auto *CE = dyn_cast<CastExpr>(E)) {
1637*0a6a1f1dSLionel Sambuc if (!CE->getSubExpr()->isGLValue())
1638*0a6a1f1dSLionel Sambuc return false;
1639*0a6a1f1dSLionel Sambuc return isGLValueFromPointerDeref(CE->getSubExpr());
1640f4a2713aSLionel Sambuc }
1641f4a2713aSLionel Sambuc
1642*0a6a1f1dSLionel Sambuc if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E))
1643*0a6a1f1dSLionel Sambuc return isGLValueFromPointerDeref(OVE->getSourceExpr());
1644*0a6a1f1dSLionel Sambuc
1645*0a6a1f1dSLionel Sambuc if (const auto *BO = dyn_cast<BinaryOperator>(E))
1646*0a6a1f1dSLionel Sambuc if (BO->getOpcode() == BO_Comma)
1647*0a6a1f1dSLionel Sambuc return isGLValueFromPointerDeref(BO->getRHS());
1648*0a6a1f1dSLionel Sambuc
1649*0a6a1f1dSLionel Sambuc if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(E))
1650*0a6a1f1dSLionel Sambuc return isGLValueFromPointerDeref(ACO->getTrueExpr()) ||
1651*0a6a1f1dSLionel Sambuc isGLValueFromPointerDeref(ACO->getFalseExpr());
1652*0a6a1f1dSLionel Sambuc
1653*0a6a1f1dSLionel Sambuc // C++11 [expr.sub]p1:
1654*0a6a1f1dSLionel Sambuc // The expression E1[E2] is identical (by definition) to *((E1)+(E2))
1655*0a6a1f1dSLionel Sambuc if (isa<ArraySubscriptExpr>(E))
1656*0a6a1f1dSLionel Sambuc return true;
1657*0a6a1f1dSLionel Sambuc
1658*0a6a1f1dSLionel Sambuc if (const auto *UO = dyn_cast<UnaryOperator>(E))
1659*0a6a1f1dSLionel Sambuc if (UO->getOpcode() == UO_Deref)
1660*0a6a1f1dSLionel Sambuc return true;
1661*0a6a1f1dSLionel Sambuc
1662*0a6a1f1dSLionel Sambuc return false;
1663f4a2713aSLionel Sambuc }
1664f4a2713aSLionel Sambuc
EmitTypeidFromVTable(CodeGenFunction & CGF,const Expr * E,llvm::Type * StdTypeInfoPtrTy)1665*0a6a1f1dSLionel Sambuc static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E,
1666f4a2713aSLionel Sambuc llvm::Type *StdTypeInfoPtrTy) {
1667f4a2713aSLionel Sambuc // Get the vtable pointer.
1668f4a2713aSLionel Sambuc llvm::Value *ThisPtr = CGF.EmitLValue(E).getAddress();
1669f4a2713aSLionel Sambuc
1670f4a2713aSLionel Sambuc // C++ [expr.typeid]p2:
1671f4a2713aSLionel Sambuc // If the glvalue expression is obtained by applying the unary * operator to
1672f4a2713aSLionel Sambuc // a pointer and the pointer is a null pointer value, the typeid expression
1673f4a2713aSLionel Sambuc // throws the std::bad_typeid exception.
1674*0a6a1f1dSLionel Sambuc //
1675*0a6a1f1dSLionel Sambuc // However, this paragraph's intent is not clear. We choose a very generous
1676*0a6a1f1dSLionel Sambuc // interpretation which implores us to consider comma operators, conditional
1677*0a6a1f1dSLionel Sambuc // operators, parentheses and other such constructs.
1678*0a6a1f1dSLionel Sambuc QualType SrcRecordTy = E->getType();
1679*0a6a1f1dSLionel Sambuc if (CGF.CGM.getCXXABI().shouldTypeidBeNullChecked(
1680*0a6a1f1dSLionel Sambuc isGLValueFromPointerDeref(E), SrcRecordTy)) {
1681f4a2713aSLionel Sambuc llvm::BasicBlock *BadTypeidBlock =
1682f4a2713aSLionel Sambuc CGF.createBasicBlock("typeid.bad_typeid");
1683*0a6a1f1dSLionel Sambuc llvm::BasicBlock *EndBlock = CGF.createBasicBlock("typeid.end");
1684f4a2713aSLionel Sambuc
1685f4a2713aSLionel Sambuc llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr);
1686f4a2713aSLionel Sambuc CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock);
1687f4a2713aSLionel Sambuc
1688f4a2713aSLionel Sambuc CGF.EmitBlock(BadTypeidBlock);
1689*0a6a1f1dSLionel Sambuc CGF.CGM.getCXXABI().EmitBadTypeidCall(CGF);
1690f4a2713aSLionel Sambuc CGF.EmitBlock(EndBlock);
1691f4a2713aSLionel Sambuc }
1692f4a2713aSLionel Sambuc
1693*0a6a1f1dSLionel Sambuc return CGF.CGM.getCXXABI().EmitTypeid(CGF, SrcRecordTy, ThisPtr,
1694*0a6a1f1dSLionel Sambuc StdTypeInfoPtrTy);
1695f4a2713aSLionel Sambuc }
1696f4a2713aSLionel Sambuc
EmitCXXTypeidExpr(const CXXTypeidExpr * E)1697f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) {
1698f4a2713aSLionel Sambuc llvm::Type *StdTypeInfoPtrTy =
1699f4a2713aSLionel Sambuc ConvertType(E->getType())->getPointerTo();
1700f4a2713aSLionel Sambuc
1701f4a2713aSLionel Sambuc if (E->isTypeOperand()) {
1702f4a2713aSLionel Sambuc llvm::Constant *TypeInfo =
1703f4a2713aSLionel Sambuc CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext()));
1704f4a2713aSLionel Sambuc return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy);
1705f4a2713aSLionel Sambuc }
1706f4a2713aSLionel Sambuc
1707f4a2713aSLionel Sambuc // C++ [expr.typeid]p2:
1708f4a2713aSLionel Sambuc // When typeid is applied to a glvalue expression whose type is a
1709f4a2713aSLionel Sambuc // polymorphic class type, the result refers to a std::type_info object
1710f4a2713aSLionel Sambuc // representing the type of the most derived object (that is, the dynamic
1711f4a2713aSLionel Sambuc // type) to which the glvalue refers.
1712f4a2713aSLionel Sambuc if (E->isPotentiallyEvaluated())
1713f4a2713aSLionel Sambuc return EmitTypeidFromVTable(*this, E->getExprOperand(),
1714f4a2713aSLionel Sambuc StdTypeInfoPtrTy);
1715f4a2713aSLionel Sambuc
1716f4a2713aSLionel Sambuc QualType OperandTy = E->getExprOperand()->getType();
1717f4a2713aSLionel Sambuc return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy),
1718f4a2713aSLionel Sambuc StdTypeInfoPtrTy);
1719f4a2713aSLionel Sambuc }
1720f4a2713aSLionel Sambuc
EmitDynamicCastToNull(CodeGenFunction & CGF,QualType DestTy)1721f4a2713aSLionel Sambuc static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF,
1722f4a2713aSLionel Sambuc QualType DestTy) {
1723f4a2713aSLionel Sambuc llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1724f4a2713aSLionel Sambuc if (DestTy->isPointerType())
1725f4a2713aSLionel Sambuc return llvm::Constant::getNullValue(DestLTy);
1726f4a2713aSLionel Sambuc
1727f4a2713aSLionel Sambuc /// C++ [expr.dynamic.cast]p9:
1728f4a2713aSLionel Sambuc /// A failed cast to reference type throws std::bad_cast
1729*0a6a1f1dSLionel Sambuc if (!CGF.CGM.getCXXABI().EmitBadCastCall(CGF))
1730*0a6a1f1dSLionel Sambuc return nullptr;
1731f4a2713aSLionel Sambuc
1732f4a2713aSLionel Sambuc CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end"));
1733f4a2713aSLionel Sambuc return llvm::UndefValue::get(DestLTy);
1734f4a2713aSLionel Sambuc }
1735f4a2713aSLionel Sambuc
EmitDynamicCast(llvm::Value * Value,const CXXDynamicCastExpr * DCE)1736f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::EmitDynamicCast(llvm::Value *Value,
1737f4a2713aSLionel Sambuc const CXXDynamicCastExpr *DCE) {
1738f4a2713aSLionel Sambuc QualType DestTy = DCE->getTypeAsWritten();
1739f4a2713aSLionel Sambuc
1740f4a2713aSLionel Sambuc if (DCE->isAlwaysNull())
1741*0a6a1f1dSLionel Sambuc if (llvm::Value *T = EmitDynamicCastToNull(*this, DestTy))
1742*0a6a1f1dSLionel Sambuc return T;
1743f4a2713aSLionel Sambuc
1744f4a2713aSLionel Sambuc QualType SrcTy = DCE->getSubExpr()->getType();
1745f4a2713aSLionel Sambuc
1746*0a6a1f1dSLionel Sambuc // C++ [expr.dynamic.cast]p7:
1747*0a6a1f1dSLionel Sambuc // If T is "pointer to cv void," then the result is a pointer to the most
1748*0a6a1f1dSLionel Sambuc // derived object pointed to by v.
1749*0a6a1f1dSLionel Sambuc const PointerType *DestPTy = DestTy->getAs<PointerType>();
1750*0a6a1f1dSLionel Sambuc
1751*0a6a1f1dSLionel Sambuc bool isDynamicCastToVoid;
1752*0a6a1f1dSLionel Sambuc QualType SrcRecordTy;
1753*0a6a1f1dSLionel Sambuc QualType DestRecordTy;
1754*0a6a1f1dSLionel Sambuc if (DestPTy) {
1755*0a6a1f1dSLionel Sambuc isDynamicCastToVoid = DestPTy->getPointeeType()->isVoidType();
1756*0a6a1f1dSLionel Sambuc SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType();
1757*0a6a1f1dSLionel Sambuc DestRecordTy = DestPTy->getPointeeType();
1758*0a6a1f1dSLionel Sambuc } else {
1759*0a6a1f1dSLionel Sambuc isDynamicCastToVoid = false;
1760*0a6a1f1dSLionel Sambuc SrcRecordTy = SrcTy;
1761*0a6a1f1dSLionel Sambuc DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType();
1762*0a6a1f1dSLionel Sambuc }
1763*0a6a1f1dSLionel Sambuc
1764*0a6a1f1dSLionel Sambuc assert(SrcRecordTy->isRecordType() && "source type must be a record type!");
1765*0a6a1f1dSLionel Sambuc
1766f4a2713aSLionel Sambuc // C++ [expr.dynamic.cast]p4:
1767f4a2713aSLionel Sambuc // If the value of v is a null pointer value in the pointer case, the result
1768f4a2713aSLionel Sambuc // is the null pointer value of type T.
1769*0a6a1f1dSLionel Sambuc bool ShouldNullCheckSrcValue =
1770*0a6a1f1dSLionel Sambuc CGM.getCXXABI().shouldDynamicCastCallBeNullChecked(SrcTy->isPointerType(),
1771*0a6a1f1dSLionel Sambuc SrcRecordTy);
1772f4a2713aSLionel Sambuc
1773*0a6a1f1dSLionel Sambuc llvm::BasicBlock *CastNull = nullptr;
1774*0a6a1f1dSLionel Sambuc llvm::BasicBlock *CastNotNull = nullptr;
1775f4a2713aSLionel Sambuc llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end");
1776f4a2713aSLionel Sambuc
1777f4a2713aSLionel Sambuc if (ShouldNullCheckSrcValue) {
1778f4a2713aSLionel Sambuc CastNull = createBasicBlock("dynamic_cast.null");
1779f4a2713aSLionel Sambuc CastNotNull = createBasicBlock("dynamic_cast.notnull");
1780f4a2713aSLionel Sambuc
1781f4a2713aSLionel Sambuc llvm::Value *IsNull = Builder.CreateIsNull(Value);
1782f4a2713aSLionel Sambuc Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
1783f4a2713aSLionel Sambuc EmitBlock(CastNotNull);
1784f4a2713aSLionel Sambuc }
1785f4a2713aSLionel Sambuc
1786*0a6a1f1dSLionel Sambuc if (isDynamicCastToVoid) {
1787*0a6a1f1dSLionel Sambuc Value = CGM.getCXXABI().EmitDynamicCastToVoid(*this, Value, SrcRecordTy,
1788*0a6a1f1dSLionel Sambuc DestTy);
1789*0a6a1f1dSLionel Sambuc } else {
1790*0a6a1f1dSLionel Sambuc assert(DestRecordTy->isRecordType() &&
1791*0a6a1f1dSLionel Sambuc "destination type must be a record type!");
1792*0a6a1f1dSLionel Sambuc Value = CGM.getCXXABI().EmitDynamicCastCall(*this, Value, SrcRecordTy,
1793*0a6a1f1dSLionel Sambuc DestTy, DestRecordTy, CastEnd);
1794*0a6a1f1dSLionel Sambuc }
1795f4a2713aSLionel Sambuc
1796f4a2713aSLionel Sambuc if (ShouldNullCheckSrcValue) {
1797f4a2713aSLionel Sambuc EmitBranch(CastEnd);
1798f4a2713aSLionel Sambuc
1799f4a2713aSLionel Sambuc EmitBlock(CastNull);
1800f4a2713aSLionel Sambuc EmitBranch(CastEnd);
1801f4a2713aSLionel Sambuc }
1802f4a2713aSLionel Sambuc
1803f4a2713aSLionel Sambuc EmitBlock(CastEnd);
1804f4a2713aSLionel Sambuc
1805f4a2713aSLionel Sambuc if (ShouldNullCheckSrcValue) {
1806f4a2713aSLionel Sambuc llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
1807f4a2713aSLionel Sambuc PHI->addIncoming(Value, CastNotNull);
1808f4a2713aSLionel Sambuc PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
1809f4a2713aSLionel Sambuc
1810f4a2713aSLionel Sambuc Value = PHI;
1811f4a2713aSLionel Sambuc }
1812f4a2713aSLionel Sambuc
1813f4a2713aSLionel Sambuc return Value;
1814f4a2713aSLionel Sambuc }
1815f4a2713aSLionel Sambuc
EmitLambdaExpr(const LambdaExpr * E,AggValueSlot Slot)1816f4a2713aSLionel Sambuc void CodeGenFunction::EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Slot) {
1817f4a2713aSLionel Sambuc RunCleanupsScope Scope(*this);
1818*0a6a1f1dSLionel Sambuc LValue SlotLV =
1819*0a6a1f1dSLionel Sambuc MakeAddrLValue(Slot.getAddr(), E->getType(), Slot.getAlignment());
1820f4a2713aSLionel Sambuc
1821f4a2713aSLionel Sambuc CXXRecordDecl::field_iterator CurField = E->getLambdaClass()->field_begin();
1822f4a2713aSLionel Sambuc for (LambdaExpr::capture_init_iterator i = E->capture_init_begin(),
1823f4a2713aSLionel Sambuc e = E->capture_init_end();
1824f4a2713aSLionel Sambuc i != e; ++i, ++CurField) {
1825f4a2713aSLionel Sambuc // Emit initialization
1826f4a2713aSLionel Sambuc LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField);
1827*0a6a1f1dSLionel Sambuc if (CurField->hasCapturedVLAType()) {
1828*0a6a1f1dSLionel Sambuc auto VAT = CurField->getCapturedVLAType();
1829*0a6a1f1dSLionel Sambuc EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV);
1830*0a6a1f1dSLionel Sambuc } else {
1831f4a2713aSLionel Sambuc ArrayRef<VarDecl *> ArrayIndexes;
1832f4a2713aSLionel Sambuc if (CurField->getType()->isArrayType())
1833f4a2713aSLionel Sambuc ArrayIndexes = E->getCaptureInitIndexVars(i);
1834f4a2713aSLionel Sambuc EmitInitializerForField(*CurField, LV, *i, ArrayIndexes);
1835f4a2713aSLionel Sambuc }
1836f4a2713aSLionel Sambuc }
1837*0a6a1f1dSLionel Sambuc }
1838