clang  10.0.0git
CGExprCXX.cpp
Go to the documentation of this file.
1 //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code dealing with code generation of C++ expressions
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGCUDARuntime.h"
14 #include "CGCXXABI.h"
15 #include "CGDebugInfo.h"
16 #include "CGObjCRuntime.h"
17 #include "CodeGenFunction.h"
18 #include "ConstantEmitter.h"
19 #include "TargetInfo.h"
22 #include "llvm/IR/Intrinsics.h"
23 
24 using namespace clang;
25 using namespace CodeGen;
26 
27 namespace {
28 struct MemberCallInfo {
29  RequiredArgs ReqArgs;
30  // Number of prefix arguments for the call. Ignores the `this` pointer.
31  unsigned PrefixSize;
32 };
33 }
34 
35 static MemberCallInfo
37  llvm::Value *This, llvm::Value *ImplicitParam,
38  QualType ImplicitParamTy, const CallExpr *CE,
39  CallArgList &Args, CallArgList *RtlArgs) {
40  assert(CE == nullptr || isa<CXXMemberCallExpr>(CE) ||
41  isa<CXXOperatorCallExpr>(CE));
42  assert(MD->isInstance() &&
43  "Trying to emit a member or operator call expr on a static method!");
44 
45  // Push the this ptr.
46  const CXXRecordDecl *RD =
48  Args.add(RValue::get(This), CGF.getTypes().DeriveThisType(RD, MD));
49 
50  // If there is an implicit parameter (e.g. VTT), emit it.
51  if (ImplicitParam) {
52  Args.add(RValue::get(ImplicitParam), ImplicitParamTy);
53  }
54 
55  const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
56  RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size());
57  unsigned PrefixSize = Args.size() - 1;
58 
59  // And the rest of the call args.
60  if (RtlArgs) {
61  // Special case: if the caller emitted the arguments right-to-left already
62  // (prior to emitting the *this argument), we're done. This happens for
63  // assignment operators.
64  Args.addFrom(*RtlArgs);
65  } else if (CE) {
66  // Special case: skip first argument of CXXOperatorCall (it is "this").
67  unsigned ArgsToSkip = isa<CXXOperatorCallExpr>(CE) ? 1 : 0;
68  CGF.EmitCallArgs(Args, FPT, drop_begin(CE->arguments(), ArgsToSkip),
69  CE->getDirectCallee());
70  } else {
71  assert(
72  FPT->getNumParams() == 0 &&
73  "No CallExpr specified for function with non-zero number of arguments");
74  }
75  return {required, PrefixSize};
76 }
77 
79  const CXXMethodDecl *MD, const CGCallee &Callee,
81  llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy,
82  const CallExpr *CE, CallArgList *RtlArgs) {
83  const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
84  CallArgList Args;
85  MemberCallInfo CallInfo = commonEmitCXXMemberOrOperatorCall(
86  *this, MD, This, ImplicitParam, ImplicitParamTy, CE, Args, RtlArgs);
87  auto &FnInfo = CGM.getTypes().arrangeCXXMethodCall(
88  Args, FPT, CallInfo.ReqArgs, CallInfo.PrefixSize);
89  return EmitCall(FnInfo, Callee, ReturnValue, Args, nullptr,
90  CE ? CE->getExprLoc() : SourceLocation());
91 }
92 
94  GlobalDecl Dtor, const CGCallee &Callee, llvm::Value *This, QualType ThisTy,
95  llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE) {
96  const CXXMethodDecl *DtorDecl = cast<CXXMethodDecl>(Dtor.getDecl());
97 
98  assert(!ThisTy.isNull());
99  assert(ThisTy->getAsCXXRecordDecl() == DtorDecl->getParent() &&
100  "Pointer/Object mixup");
101 
102  LangAS SrcAS = ThisTy.getAddressSpace();
103  LangAS DstAS = DtorDecl->getMethodQualifiers().getAddressSpace();
104  if (SrcAS != DstAS) {
105  QualType DstTy = DtorDecl->getThisType();
106  llvm::Type *NewType = CGM.getTypes().ConvertType(DstTy);
107  This = getTargetHooks().performAddrSpaceCast(*this, This, SrcAS, DstAS,
108  NewType);
109  }
110 
111  CallArgList Args;
112  commonEmitCXXMemberOrOperatorCall(*this, DtorDecl, This, ImplicitParam,
113  ImplicitParamTy, CE, Args, nullptr);
114  return EmitCall(CGM.getTypes().arrangeCXXStructorDeclaration(Dtor), Callee,
115  ReturnValueSlot(), Args);
116 }
117 
119  const CXXPseudoDestructorExpr *E) {
120  QualType DestroyedType = E->getDestroyedType();
121  if (DestroyedType.hasStrongOrWeakObjCLifetime()) {
122  // Automatic Reference Counting:
123  // If the pseudo-expression names a retainable object with weak or
124  // strong lifetime, the object shall be released.
125  Expr *BaseExpr = E->getBase();
126  Address BaseValue = Address::invalid();
127  Qualifiers BaseQuals;
128 
129  // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
130  if (E->isArrow()) {
131  BaseValue = EmitPointerWithAlignment(BaseExpr);
132  const auto *PTy = BaseExpr->getType()->castAs<PointerType>();
133  BaseQuals = PTy->getPointeeType().getQualifiers();
134  } else {
135  LValue BaseLV = EmitLValue(BaseExpr);
136  BaseValue = BaseLV.getAddress(*this);
137  QualType BaseTy = BaseExpr->getType();
138  BaseQuals = BaseTy.getQualifiers();
139  }
140 
141  switch (DestroyedType.getObjCLifetime()) {
145  break;
146 
148  EmitARCRelease(Builder.CreateLoad(BaseValue,
149  DestroyedType.isVolatileQualified()),
151  break;
152 
154  EmitARCDestroyWeak(BaseValue);
155  break;
156  }
157  } else {
158  // C++ [expr.pseudo]p1:
159  // The result shall only be used as the operand for the function call
160  // operator (), and the result of such a call has type void. The only
161  // effect is the evaluation of the postfix-expression before the dot or
162  // arrow.
163  EmitIgnoredExpr(E->getBase());
164  }
165 
166  return RValue::get(nullptr);
167 }
168 
169 static CXXRecordDecl *getCXXRecord(const Expr *E) {
170  QualType T = E->getType();
171  if (const PointerType *PTy = T->getAs<PointerType>())
172  T = PTy->getPointeeType();
173  const RecordType *Ty = T->castAs<RecordType>();
174  return cast<CXXRecordDecl>(Ty->getDecl());
175 }
176 
177 // Note: This function also emit constructor calls to support a MSVC
178 // extensions allowing explicit constructor function call.
181  const Expr *callee = CE->getCallee()->IgnoreParens();
182 
183  if (isa<BinaryOperator>(callee))
184  return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
185 
186  const MemberExpr *ME = cast<MemberExpr>(callee);
187  const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
188 
189  if (MD->isStatic()) {
190  // The method is static, emit it as we would a regular call.
191  CGCallee callee =
192  CGCallee::forDirect(CGM.GetAddrOfFunction(MD), GlobalDecl(MD));
193  return EmitCall(getContext().getPointerType(MD->getType()), callee, CE,
194  ReturnValue);
195  }
196 
197  bool HasQualifier = ME->hasQualifier();
198  NestedNameSpecifier *Qualifier = HasQualifier ? ME->getQualifier() : nullptr;
199  bool IsArrow = ME->isArrow();
200  const Expr *Base = ME->getBase();
201 
202  return EmitCXXMemberOrOperatorMemberCallExpr(
203  CE, MD, ReturnValue, HasQualifier, Qualifier, IsArrow, Base);
204 }
205 
207  const CallExpr *CE, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue,
208  bool HasQualifier, NestedNameSpecifier *Qualifier, bool IsArrow,
209  const Expr *Base) {
210  assert(isa<CXXMemberCallExpr>(CE) || isa<CXXOperatorCallExpr>(CE));
211 
212  // Compute the object pointer.
213  bool CanUseVirtualCall = MD->isVirtual() && !HasQualifier;
214 
215  const CXXMethodDecl *DevirtualizedMethod = nullptr;
216  if (CanUseVirtualCall &&
217  MD->getDevirtualizedMethod(Base, getLangOpts().AppleKext)) {
218  const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
219  DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl);
220  assert(DevirtualizedMethod);
221  const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent();
222  const Expr *Inner = Base->ignoreParenBaseCasts();
223  if (DevirtualizedMethod->getReturnType().getCanonicalType() !=
225  // If the return types are not the same, this might be a case where more
226  // code needs to run to compensate for it. For example, the derived
227  // method might return a type that inherits form from the return
228  // type of MD and has a prefix.
229  // For now we just avoid devirtualizing these covariant cases.
230  DevirtualizedMethod = nullptr;
231  else if (getCXXRecord(Inner) == DevirtualizedClass)
232  // If the class of the Inner expression is where the dynamic method
233  // is defined, build the this pointer from it.
234  Base = Inner;
235  else if (getCXXRecord(Base) != DevirtualizedClass) {
236  // If the method is defined in a class that is not the best dynamic
237  // one or the one of the full expression, we would have to build
238  // a derived-to-base cast to compute the correct this pointer, but
239  // we don't have support for that yet, so do a virtual call.
240  DevirtualizedMethod = nullptr;
241  }
242  }
243 
244  bool TrivialForCodegen =
245  MD->isTrivial() || (MD->isDefaulted() && MD->getParent()->isUnion());
246  bool TrivialAssignment =
247  TrivialForCodegen &&
250 
251  // C++17 demands that we evaluate the RHS of a (possibly-compound) assignment
252  // operator before the LHS.
253  CallArgList RtlArgStorage;
254  CallArgList *RtlArgs = nullptr;
255  LValue TrivialAssignmentRHS;
256  if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(CE)) {
257  if (OCE->isAssignmentOp()) {
258  if (TrivialAssignment) {
259  TrivialAssignmentRHS = EmitLValue(CE->getArg(1));
260  } else {
261  RtlArgs = &RtlArgStorage;
262  EmitCallArgs(*RtlArgs, MD->getType()->castAs<FunctionProtoType>(),
263  drop_begin(CE->arguments(), 1), CE->getDirectCallee(),
264  /*ParamsToSkip*/0, EvaluationOrder::ForceRightToLeft);
265  }
266  }
267  }
268 
269  LValue This;
270  if (IsArrow) {
271  LValueBaseInfo BaseInfo;
272  TBAAAccessInfo TBAAInfo;
273  Address ThisValue = EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo);
274  This = MakeAddrLValue(ThisValue, Base->getType(), BaseInfo, TBAAInfo);
275  } else {
276  This = EmitLValue(Base);
277  }
278 
279  if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) {
280  // This is the MSVC p->Ctor::Ctor(...) extension. We assume that's
281  // constructing a new complete object of type Ctor.
282  assert(!RtlArgs);
283  assert(ReturnValue.isNull() && "Constructor shouldn't have return value");
284  CallArgList Args;
286  *this, Ctor, This.getPointer(*this), /*ImplicitParam=*/nullptr,
287  /*ImplicitParamTy=*/QualType(), CE, Args, nullptr);
288 
289  EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false,
290  /*Delegating=*/false, This.getAddress(*this), Args,
292  /*NewPointerIsChecked=*/false);
293  return RValue::get(nullptr);
294  }
295 
296  if (TrivialForCodegen) {
297  if (isa<CXXDestructorDecl>(MD))
298  return RValue::get(nullptr);
299 
300  if (TrivialAssignment) {
301  // We don't like to generate the trivial copy/move assignment operator
302  // when it isn't necessary; just produce the proper effect here.
303  // It's important that we use the result of EmitLValue here rather than
304  // emitting call arguments, in order to preserve TBAA information from
305  // the RHS.
306  LValue RHS = isa<CXXOperatorCallExpr>(CE)
307  ? TrivialAssignmentRHS
308  : EmitLValue(*CE->arg_begin());
309  EmitAggregateAssign(This, RHS, CE->getType());
310  return RValue::get(This.getPointer(*this));
311  }
312 
313  assert(MD->getParent()->mayInsertExtraPadding() &&
314  "unknown trivial member function");
315  }
316 
317  // Compute the function type we're calling.
318  const CXXMethodDecl *CalleeDecl =
319  DevirtualizedMethod ? DevirtualizedMethod : MD;
320  const CGFunctionInfo *FInfo = nullptr;
321  if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl))
322  FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
323  GlobalDecl(Dtor, Dtor_Complete));
324  else
325  FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl);
326 
327  llvm::FunctionType *Ty = CGM.getTypes().GetFunctionType(*FInfo);
328 
329  // C++11 [class.mfct.non-static]p2:
330  // If a non-static member function of a class X is called for an object that
331  // is not of type X, or of a type derived from X, the behavior is undefined.
332  SourceLocation CallLoc;
333  ASTContext &C = getContext();
334  if (CE)
335  CallLoc = CE->getExprLoc();
336 
337  SanitizerSet SkippedChecks;
338  if (const auto *CMCE = dyn_cast<CXXMemberCallExpr>(CE)) {
339  auto *IOA = CMCE->getImplicitObjectArgument();
340  bool IsImplicitObjectCXXThis = IsWrappedCXXThis(IOA);
341  if (IsImplicitObjectCXXThis)
342  SkippedChecks.set(SanitizerKind::Alignment, true);
343  if (IsImplicitObjectCXXThis || isa<DeclRefExpr>(IOA))
344  SkippedChecks.set(SanitizerKind::Null, true);
345  }
346  EmitTypeCheck(CodeGenFunction::TCK_MemberCall, CallLoc,
347  This.getPointer(*this),
348  C.getRecordType(CalleeDecl->getParent()),
349  /*Alignment=*/CharUnits::Zero(), SkippedChecks);
350 
351  // C++ [class.virtual]p12:
352  // Explicit qualification with the scope operator (5.1) suppresses the
353  // virtual call mechanism.
354  //
355  // We also don't emit a virtual call if the base expression has a record type
356  // because then we know what the type is.
357  bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod;
358 
359  if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(CalleeDecl)) {
360  assert(CE->arg_begin() == CE->arg_end() &&
361  "Destructor shouldn't have explicit parameters");
362  assert(ReturnValue.isNull() && "Destructor shouldn't have return value");
363  if (UseVirtualCall) {
364  CGM.getCXXABI().EmitVirtualDestructorCall(*this, Dtor, Dtor_Complete,
365  This.getAddress(*this),
366  cast<CXXMemberCallExpr>(CE));
367  } else {
368  GlobalDecl GD(Dtor, Dtor_Complete);
369  CGCallee Callee;
370  if (getLangOpts().AppleKext && Dtor->isVirtual() && HasQualifier)
371  Callee = BuildAppleKextVirtualCall(Dtor, Qualifier, Ty);
372  else if (!DevirtualizedMethod)
373  Callee =
374  CGCallee::forDirect(CGM.getAddrOfCXXStructor(GD, FInfo, Ty), GD);
375  else {
376  Callee = CGCallee::forDirect(CGM.GetAddrOfFunction(GD, Ty), GD);
377  }
378 
379  QualType ThisTy =
380  IsArrow ? Base->getType()->getPointeeType() : Base->getType();
381  EmitCXXDestructorCall(GD, Callee, This.getPointer(*this), ThisTy,
382  /*ImplicitParam=*/nullptr,
383  /*ImplicitParamTy=*/QualType(), nullptr);
384  }
385  return RValue::get(nullptr);
386  }
387 
388  // FIXME: Uses of 'MD' past this point need to be audited. We may need to use
389  // 'CalleeDecl' instead.
390 
391  CGCallee Callee;
392  if (UseVirtualCall) {
393  Callee = CGCallee::forVirtual(CE, MD, This.getAddress(*this), Ty);
394  } else {
395  if (SanOpts.has(SanitizerKind::CFINVCall) &&
396  MD->getParent()->isDynamicClass()) {
397  llvm::Value *VTable;
398  const CXXRecordDecl *RD;
399  std::tie(VTable, RD) = CGM.getCXXABI().LoadVTablePtr(
400  *this, This.getAddress(*this), CalleeDecl->getParent());
401  EmitVTablePtrCheckForCall(RD, VTable, CFITCK_NVCall, CE->getBeginLoc());
402  }
403 
404  if (getLangOpts().AppleKext && MD->isVirtual() && HasQualifier)
405  Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty);
406  else if (!DevirtualizedMethod)
407  Callee =
408  CGCallee::forDirect(CGM.GetAddrOfFunction(MD, Ty), GlobalDecl(MD));
409  else {
410  Callee =
411  CGCallee::forDirect(CGM.GetAddrOfFunction(DevirtualizedMethod, Ty),
412  GlobalDecl(DevirtualizedMethod));
413  }
414  }
415 
416  if (MD->isVirtual()) {
417  Address NewThisAddr =
418  CGM.getCXXABI().adjustThisArgumentForVirtualFunctionCall(
419  *this, CalleeDecl, This.getAddress(*this), UseVirtualCall);
420  This.setAddress(NewThisAddr);
421  }
422 
423  return EmitCXXMemberOrOperatorCall(
424  CalleeDecl, Callee, ReturnValue, This.getPointer(*this),
425  /*ImplicitParam=*/nullptr, QualType(), CE, RtlArgs);
426 }
427 
428 RValue
431  const BinaryOperator *BO =
432  cast<BinaryOperator>(E->getCallee()->IgnoreParens());
433  const Expr *BaseExpr = BO->getLHS();
434  const Expr *MemFnExpr = BO->getRHS();
435 
436  const auto *MPT = MemFnExpr->getType()->castAs<MemberPointerType>();
437  const auto *FPT = MPT->getPointeeType()->castAs<FunctionProtoType>();
438  const auto *RD =
439  cast<CXXRecordDecl>(MPT->getClass()->castAs<RecordType>()->getDecl());
440 
441  // Emit the 'this' pointer.
443  if (BO->getOpcode() == BO_PtrMemI)
444  This = EmitPointerWithAlignment(BaseExpr);
445  else
446  This = EmitLValue(BaseExpr).getAddress(*this);
447 
448  EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This.getPointer(),
449  QualType(MPT->getClass(), 0));
450 
451  // Get the member function pointer.
452  llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr);
453 
454  // Ask the ABI to load the callee. Note that This is modified.
455  llvm::Value *ThisPtrForCall = nullptr;
456  CGCallee Callee =
457  CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, BO, This,
458  ThisPtrForCall, MemFnPtr, MPT);
459 
460  CallArgList Args;
461 
462  QualType ThisType =
463  getContext().getPointerType(getContext().getTagDeclType(RD));
464 
465  // Push the this ptr.
466  Args.add(RValue::get(ThisPtrForCall), ThisType);
467 
468  RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1);
469 
470  // And the rest of the call args
471  EmitCallArgs(Args, FPT, E->arguments());
472  return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required,
473  /*PrefixSize=*/0),
474  Callee, ReturnValue, Args, nullptr, E->getExprLoc());
475 }
476 
477 RValue
479  const CXXMethodDecl *MD,
481  assert(MD->isInstance() &&
482  "Trying to emit a member call expr on a static method!");
483  return EmitCXXMemberOrOperatorMemberCallExpr(
484  E, MD, ReturnValue, /*HasQualifier=*/false, /*Qualifier=*/nullptr,
485  /*IsArrow=*/false, E->getArg(0));
486 }
487 
490  return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue);
491 }
492 
494  Address DestPtr,
495  const CXXRecordDecl *Base) {
496  if (Base->isEmpty())
497  return;
498 
499  DestPtr = CGF.Builder.CreateElementBitCast(DestPtr, CGF.Int8Ty);
500 
501  const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base);
502  CharUnits NVSize = Layout.getNonVirtualSize();
503 
504  // We cannot simply zero-initialize the entire base sub-object if vbptrs are
505  // present, they are initialized by the most derived class before calling the
506  // constructor.
508  Stores.emplace_back(CharUnits::Zero(), NVSize);
509 
510  // Each store is split by the existence of a vbptr.
511  CharUnits VBPtrWidth = CGF.getPointerSize();
512  std::vector<CharUnits> VBPtrOffsets =
513  CGF.CGM.getCXXABI().getVBPtrOffsets(Base);
514  for (CharUnits VBPtrOffset : VBPtrOffsets) {
515  // Stop before we hit any virtual base pointers located in virtual bases.
516  if (VBPtrOffset >= NVSize)
517  break;
518  std::pair<CharUnits, CharUnits> LastStore = Stores.pop_back_val();
519  CharUnits LastStoreOffset = LastStore.first;
520  CharUnits LastStoreSize = LastStore.second;
521 
522  CharUnits SplitBeforeOffset = LastStoreOffset;
523  CharUnits SplitBeforeSize = VBPtrOffset - SplitBeforeOffset;
524  assert(!SplitBeforeSize.isNegative() && "negative store size!");
525  if (!SplitBeforeSize.isZero())
526  Stores.emplace_back(SplitBeforeOffset, SplitBeforeSize);
527 
528  CharUnits SplitAfterOffset = VBPtrOffset + VBPtrWidth;
529  CharUnits SplitAfterSize = LastStoreSize - SplitAfterOffset;
530  assert(!SplitAfterSize.isNegative() && "negative store size!");
531  if (!SplitAfterSize.isZero())
532  Stores.emplace_back(SplitAfterOffset, SplitAfterSize);
533  }
534 
535  // If the type contains a pointer to data member we can't memset it to zero.
536  // Instead, create a null constant and copy it to the destination.
537  // TODO: there are other patterns besides zero that we can usefully memset,
538  // like -1, which happens to be the pattern used by member-pointers.
539  // TODO: isZeroInitializable can be over-conservative in the case where a
540  // virtual base contains a member pointer.
541  llvm::Constant *NullConstantForBase = CGF.CGM.EmitNullConstantForBase(Base);
542  if (!NullConstantForBase->isNullValue()) {
543  llvm::GlobalVariable *NullVariable = new llvm::GlobalVariable(
544  CGF.CGM.getModule(), NullConstantForBase->getType(),
545  /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage,
546  NullConstantForBase, Twine());
547 
548  CharUnits Align = std::max(Layout.getNonVirtualAlignment(),
549  DestPtr.getAlignment());
550  NullVariable->setAlignment(Align.getAsAlign());
551 
552  Address SrcPtr = Address(CGF.EmitCastToVoidPtr(NullVariable), Align);
553 
554  // Get and call the appropriate llvm.memcpy overload.
555  for (std::pair<CharUnits, CharUnits> Store : Stores) {
556  CharUnits StoreOffset = Store.first;
557  CharUnits StoreSize = Store.second;
558  llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize);
559  CGF.Builder.CreateMemCpy(
560  CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset),
561  CGF.Builder.CreateConstInBoundsByteGEP(SrcPtr, StoreOffset),
562  StoreSizeVal);
563  }
564 
565  // Otherwise, just memset the whole thing to zero. This is legal
566  // because in LLVM, all default initializers (other than the ones we just
567  // handled above) are guaranteed to have a bit pattern of all zeros.
568  } else {
569  for (std::pair<CharUnits, CharUnits> Store : Stores) {
570  CharUnits StoreOffset = Store.first;
571  CharUnits StoreSize = Store.second;
572  llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize);
573  CGF.Builder.CreateMemSet(
574  CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset),
575  CGF.Builder.getInt8(0), StoreSizeVal);
576  }
577  }
578 }
579 
580 void
582  AggValueSlot Dest) {
583  assert(!Dest.isIgnored() && "Must have a destination!");
584  const CXXConstructorDecl *CD = E->getConstructor();
585 
586  // If we require zero initialization before (or instead of) calling the
587  // constructor, as can be the case with a non-user-provided default
588  // constructor, emit the zero initialization now, unless destination is
589  // already zeroed.
590  if (E->requiresZeroInitialization() && !Dest.isZeroed()) {
591  switch (E->getConstructionKind()) {
594  EmitNullInitialization(Dest.getAddress(), E->getType());
595  break;
599  CD->getParent());
600  break;
601  }
602  }
603 
604  // If this is a call to a trivial default constructor, do nothing.
605  if (CD->isTrivial() && CD->isDefaultConstructor())
606  return;
607 
608  // Elide the constructor if we're constructing from a temporary.
609  // The temporary check is required because Sema sets this on NRVO
610  // returns.
611  if (getLangOpts().ElideConstructors && E->isElidable()) {
612  assert(getContext().hasSameUnqualifiedType(E->getType(),
613  E->getArg(0)->getType()));
614  if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) {
615  EmitAggExpr(E->getArg(0), Dest);
616  return;
617  }
618  }
619 
620  if (const ArrayType *arrayType
621  = getContext().getAsArrayType(E->getType())) {
622  EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddress(), E,
623  Dest.isSanitizerChecked());
624  } else {
626  bool ForVirtualBase = false;
627  bool Delegating = false;
628 
629  switch (E->getConstructionKind()) {
631  // We should be emitting a constructor; GlobalDecl will assert this
632  Type = CurGD.getCtorType();
633  Delegating = true;
634  break;
635 
637  Type = Ctor_Complete;
638  break;
639 
641  ForVirtualBase = true;
642  LLVM_FALLTHROUGH;
643 
645  Type = Ctor_Base;
646  }
647 
648  // Call the constructor.
649  EmitCXXConstructorCall(CD, Type, ForVirtualBase, Delegating, Dest, E);
650  }
651 }
652 
654  const Expr *Exp) {
655  if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(Exp))
656  Exp = E->getSubExpr();
657  assert(isa<CXXConstructExpr>(Exp) &&
658  "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr");
659  const CXXConstructExpr* E = cast<CXXConstructExpr>(Exp);
660  const CXXConstructorDecl *CD = E->getConstructor();
661  RunCleanupsScope Scope(*this);
662 
663  // If we require zero initialization before (or instead of) calling the
664  // constructor, as can be the case with a non-user-provided default
665  // constructor, emit the zero initialization now.
666  // FIXME. Do I still need this for a copy ctor synthesis?
668  EmitNullInitialization(Dest, E->getType());
669 
670  assert(!getContext().getAsConstantArrayType(E->getType())
671  && "EmitSynthesizedCXXCopyCtor - Copied-in Array");
672  EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, E);
673 }
674 
676  const CXXNewExpr *E) {
677  if (!E->isArray())
678  return CharUnits::Zero();
679 
680  // No cookie is required if the operator new[] being used is the
681  // reserved placement operator new[].
683  return CharUnits::Zero();
684 
685  return CGF.CGM.getCXXABI().GetArrayCookieSize(E);
686 }
687 
689  const CXXNewExpr *e,
690  unsigned minElements,
691  llvm::Value *&numElements,
692  llvm::Value *&sizeWithoutCookie) {
694 
695  if (!e->isArray()) {
696  CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
697  sizeWithoutCookie
698  = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity());
699  return sizeWithoutCookie;
700  }
701 
702  // The width of size_t.
703  unsigned sizeWidth = CGF.SizeTy->getBitWidth();
704 
705  // Figure out the cookie size.
706  llvm::APInt cookieSize(sizeWidth,
707  CalculateCookiePadding(CGF, e).getQuantity());
708 
709  // Emit the array size expression.
710  // We multiply the size of all dimensions for NumElements.
711  // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6.
712  numElements =
714  if (!numElements)
715  numElements = CGF.EmitScalarExpr(*e->getArraySize());
716  assert(isa<llvm::IntegerType>(numElements->getType()));
717 
718  // The number of elements can be have an arbitrary integer type;
719  // essentially, we need to multiply it by a constant factor, add a
720  // cookie size, and verify that the result is representable as a
721  // size_t. That's just a gloss, though, and it's wrong in one
722  // important way: if the count is negative, it's an error even if
723  // the cookie size would bring the total size >= 0.
724  bool isSigned
725  = (*e->getArraySize())->getType()->isSignedIntegerOrEnumerationType();
726  llvm::IntegerType *numElementsType
727  = cast<llvm::IntegerType>(numElements->getType());
728  unsigned numElementsWidth = numElementsType->getBitWidth();
729 
730  // Compute the constant factor.
731  llvm::APInt arraySizeMultiplier(sizeWidth, 1);
732  while (const ConstantArrayType *CAT
733  = CGF.getContext().getAsConstantArrayType(type)) {
734  type = CAT->getElementType();
735  arraySizeMultiplier *= CAT->getSize();
736  }
737 
738  CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type);
739  llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity());
740  typeSizeMultiplier *= arraySizeMultiplier;
741 
742  // This will be a size_t.
743  llvm::Value *size;
744 
745  // If someone is doing 'new int[42]' there is no need to do a dynamic check.
746  // Don't bloat the -O0 code.
747  if (llvm::ConstantInt *numElementsC =
748  dyn_cast<llvm::ConstantInt>(numElements)) {
749  const llvm::APInt &count = numElementsC->getValue();
750 
751  bool hasAnyOverflow = false;
752 
753  // If 'count' was a negative number, it's an overflow.
754  if (isSigned && count.isNegative())
755  hasAnyOverflow = true;
756 
757  // We want to do all this arithmetic in size_t. If numElements is
758  // wider than that, check whether it's already too big, and if so,
759  // overflow.
760  else if (numElementsWidth > sizeWidth &&
761  numElementsWidth - sizeWidth > count.countLeadingZeros())
762  hasAnyOverflow = true;
763 
764  // Okay, compute a count at the right width.
765  llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth);
766 
767  // If there is a brace-initializer, we cannot allocate fewer elements than
768  // there are initializers. If we do, that's treated like an overflow.
769  if (adjustedCount.ult(minElements))
770  hasAnyOverflow = true;
771 
772  // Scale numElements by that. This might overflow, but we don't
773  // care because it only overflows if allocationSize does, too, and
774  // if that overflows then we shouldn't use this.
775  numElements = llvm::ConstantInt::get(CGF.SizeTy,
776  adjustedCount * arraySizeMultiplier);
777 
778  // Compute the size before cookie, and track whether it overflowed.
779  bool overflow;
780  llvm::APInt allocationSize
781  = adjustedCount.umul_ov(typeSizeMultiplier, overflow);
782  hasAnyOverflow |= overflow;
783 
784  // Add in the cookie, and check whether it's overflowed.
785  if (cookieSize != 0) {
786  // Save the current size without a cookie. This shouldn't be
787  // used if there was overflow.
788  sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
789 
790  allocationSize = allocationSize.uadd_ov(cookieSize, overflow);
791  hasAnyOverflow |= overflow;
792  }
793 
794  // On overflow, produce a -1 so operator new will fail.
795  if (hasAnyOverflow) {
796  size = llvm::Constant::getAllOnesValue(CGF.SizeTy);
797  } else {
798  size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize);
799  }
800 
801  // Otherwise, we might need to use the overflow intrinsics.
802  } else {
803  // There are up to five conditions we need to test for:
804  // 1) if isSigned, we need to check whether numElements is negative;
805  // 2) if numElementsWidth > sizeWidth, we need to check whether
806  // numElements is larger than something representable in size_t;
807  // 3) if minElements > 0, we need to check whether numElements is smaller
808  // than that.
809  // 4) we need to compute
810  // sizeWithoutCookie := numElements * typeSizeMultiplier
811  // and check whether it overflows; and
812  // 5) if we need a cookie, we need to compute
813  // size := sizeWithoutCookie + cookieSize
814  // and check whether it overflows.
815 
816  llvm::Value *hasOverflow = nullptr;
817 
818  // If numElementsWidth > sizeWidth, then one way or another, we're
819  // going to have to do a comparison for (2), and this happens to
820  // take care of (1), too.
821  if (numElementsWidth > sizeWidth) {
822  llvm::APInt threshold(numElementsWidth, 1);
823  threshold <<= sizeWidth;
824 
825  llvm::Value *thresholdV
826  = llvm::ConstantInt::get(numElementsType, threshold);
827 
828  hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV);
829  numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy);
830 
831  // Otherwise, if we're signed, we want to sext up to size_t.
832  } else if (isSigned) {
833  if (numElementsWidth < sizeWidth)
834  numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy);
835 
836  // If there's a non-1 type size multiplier, then we can do the
837  // signedness check at the same time as we do the multiply
838  // because a negative number times anything will cause an
839  // unsigned overflow. Otherwise, we have to do it here. But at least
840  // in this case, we can subsume the >= minElements check.
841  if (typeSizeMultiplier == 1)
842  hasOverflow = CGF.Builder.CreateICmpSLT(numElements,
843  llvm::ConstantInt::get(CGF.SizeTy, minElements));
844 
845  // Otherwise, zext up to size_t if necessary.
846  } else if (numElementsWidth < sizeWidth) {
847  numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy);
848  }
849 
850  assert(numElements->getType() == CGF.SizeTy);
851 
852  if (minElements) {
853  // Don't allow allocation of fewer elements than we have initializers.
854  if (!hasOverflow) {
855  hasOverflow = CGF.Builder.CreateICmpULT(numElements,
856  llvm::ConstantInt::get(CGF.SizeTy, minElements));
857  } else if (numElementsWidth > sizeWidth) {
858  // The other existing overflow subsumes this check.
859  // We do an unsigned comparison, since any signed value < -1 is
860  // taken care of either above or below.
861  hasOverflow = CGF.Builder.CreateOr(hasOverflow,
862  CGF.Builder.CreateICmpULT(numElements,
863  llvm::ConstantInt::get(CGF.SizeTy, minElements)));
864  }
865  }
866 
867  size = numElements;
868 
869  // Multiply by the type size if necessary. This multiplier
870  // includes all the factors for nested arrays.
871  //
872  // This step also causes numElements to be scaled up by the
873  // nested-array factor if necessary. Overflow on this computation
874  // can be ignored because the result shouldn't be used if
875  // allocation fails.
876  if (typeSizeMultiplier != 1) {
877  llvm::Function *umul_with_overflow
878  = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy);
879 
880  llvm::Value *tsmV =
881  llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier);
882  llvm::Value *result =
883  CGF.Builder.CreateCall(umul_with_overflow, {size, tsmV});
884 
885  llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
886  if (hasOverflow)
887  hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
888  else
889  hasOverflow = overflowed;
890 
891  size = CGF.Builder.CreateExtractValue(result, 0);
892 
893  // Also scale up numElements by the array size multiplier.
894  if (arraySizeMultiplier != 1) {
895  // If the base element type size is 1, then we can re-use the
896  // multiply we just did.
897  if (typeSize.isOne()) {
898  assert(arraySizeMultiplier == typeSizeMultiplier);
899  numElements = size;
900 
901  // Otherwise we need a separate multiply.
902  } else {
903  llvm::Value *asmV =
904  llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier);
905  numElements = CGF.Builder.CreateMul(numElements, asmV);
906  }
907  }
908  } else {
909  // numElements doesn't need to be scaled.
910  assert(arraySizeMultiplier == 1);
911  }
912 
913  // Add in the cookie size if necessary.
914  if (cookieSize != 0) {
915  sizeWithoutCookie = size;
916 
917  llvm::Function *uadd_with_overflow
918  = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy);
919 
920  llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize);
921  llvm::Value *result =
922  CGF.Builder.CreateCall(uadd_with_overflow, {size, cookieSizeV});
923 
924  llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1);
925  if (hasOverflow)
926  hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed);
927  else
928  hasOverflow = overflowed;
929 
930  size = CGF.Builder.CreateExtractValue(result, 0);
931  }
932 
933  // If we had any possibility of dynamic overflow, make a select to
934  // overwrite 'size' with an all-ones value, which should cause
935  // operator new to throw.
936  if (hasOverflow)
937  size = CGF.Builder.CreateSelect(hasOverflow,
938  llvm::Constant::getAllOnesValue(CGF.SizeTy),
939  size);
940  }
941 
942  if (cookieSize == 0)
943  sizeWithoutCookie = size;
944  else
945  assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?");
946 
947  return size;
948 }
949 
950 static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init,
951  QualType AllocType, Address NewPtr,
952  AggValueSlot::Overlap_t MayOverlap) {
953  // FIXME: Refactor with EmitExprAsInit.
954  switch (CGF.getEvaluationKind(AllocType)) {
955  case TEK_Scalar:
956  CGF.EmitScalarInit(Init, nullptr,
957  CGF.MakeAddrLValue(NewPtr, AllocType), false);
958  return;
959  case TEK_Complex:
960  CGF.EmitComplexExprIntoLValue(Init, CGF.MakeAddrLValue(NewPtr, AllocType),
961  /*isInit*/ true);
962  return;
963  case TEK_Aggregate: {
964  AggValueSlot Slot
965  = AggValueSlot::forAddr(NewPtr, AllocType.getQualifiers(),
969  MayOverlap, AggValueSlot::IsNotZeroed,
971  CGF.EmitAggExpr(Init, Slot);
972  return;
973  }
974  }
975  llvm_unreachable("bad evaluation kind");
976 }
977 
979  const CXXNewExpr *E, QualType ElementType, llvm::Type *ElementTy,
980  Address BeginPtr, llvm::Value *NumElements,
981  llvm::Value *AllocSizeWithoutCookie) {
982  // If we have a type with trivial initialization and no initializer,
983  // there's nothing to do.
984  if (!E->hasInitializer())
985  return;
986 
987  Address CurPtr = BeginPtr;
988 
989  unsigned InitListElements = 0;
990 
991  const Expr *Init = E->getInitializer();
992  Address EndOfInit = Address::invalid();
993  QualType::DestructionKind DtorKind = ElementType.isDestructedType();
995  llvm::Instruction *CleanupDominator = nullptr;
996 
997  CharUnits ElementSize = getContext().getTypeSizeInChars(ElementType);
998  CharUnits ElementAlign =
999  BeginPtr.getAlignment().alignmentOfArrayElement(ElementSize);
1000 
1001  // Attempt to perform zero-initialization using memset.
1002  auto TryMemsetInitialization = [&]() -> bool {
1003  // FIXME: If the type is a pointer-to-data-member under the Itanium ABI,
1004  // we can initialize with a memset to -1.
1005  if (!CGM.getTypes().isZeroInitializable(ElementType))
1006  return false;
1007 
1008  // Optimization: since zero initialization will just set the memory
1009  // to all zeroes, generate a single memset to do it in one shot.
1010 
1011  // Subtract out the size of any elements we've already initialized.
1012  auto *RemainingSize = AllocSizeWithoutCookie;
1013  if (InitListElements) {
1014  // We know this can't overflow; we check this when doing the allocation.
1015  auto *InitializedSize = llvm::ConstantInt::get(
1016  RemainingSize->getType(),
1017  getContext().getTypeSizeInChars(ElementType).getQuantity() *
1018  InitListElements);
1019  RemainingSize = Builder.CreateSub(RemainingSize, InitializedSize);
1020  }
1021 
1022  // Create the memset.
1023  Builder.CreateMemSet(CurPtr, Builder.getInt8(0), RemainingSize, false);
1024  return true;
1025  };
1026 
1027  // If the initializer is an initializer list, first do the explicit elements.
1028  if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
1029  // Initializing from a (braced) string literal is a special case; the init
1030  // list element does not initialize a (single) array element.
1031  if (ILE->isStringLiteralInit()) {
1032  // Initialize the initial portion of length equal to that of the string
1033  // literal. The allocation must be for at least this much; we emitted a
1034  // check for that earlier.
1035  AggValueSlot Slot =
1036  AggValueSlot::forAddr(CurPtr, ElementType.getQualifiers(),
1043  EmitAggExpr(ILE->getInit(0), Slot);
1044 
1045  // Move past these elements.
1046  InitListElements =
1047  cast<ConstantArrayType>(ILE->getType()->getAsArrayTypeUnsafe())
1048  ->getSize().getZExtValue();
1049  CurPtr =
1050  Address(Builder.CreateInBoundsGEP(CurPtr.getPointer(),
1051  Builder.getSize(InitListElements),
1052  "string.init.end"),
1053  CurPtr.getAlignment().alignmentAtOffset(InitListElements *
1054  ElementSize));
1055 
1056  // Zero out the rest, if any remain.
1057  llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements);
1058  if (!ConstNum || !ConstNum->equalsInt(InitListElements)) {
1059  bool OK = TryMemsetInitialization();
1060  (void)OK;
1061  assert(OK && "couldn't memset character type?");
1062  }
1063  return;
1064  }
1065 
1066  InitListElements = ILE->getNumInits();
1067 
1068  // If this is a multi-dimensional array new, we will initialize multiple
1069  // elements with each init list element.
1070  QualType AllocType = E->getAllocatedType();
1071  if (const ConstantArrayType *CAT = dyn_cast_or_null<ConstantArrayType>(
1072  AllocType->getAsArrayTypeUnsafe())) {
1073  ElementTy = ConvertTypeForMem(AllocType);
1074  CurPtr = Builder.CreateElementBitCast(CurPtr, ElementTy);
1075  InitListElements *= getContext().getConstantArrayElementCount(CAT);
1076  }
1077 
1078  // Enter a partial-destruction Cleanup if necessary.
1079  if (needsEHCleanup(DtorKind)) {
1080  // In principle we could tell the Cleanup where we are more
1081  // directly, but the control flow can get so varied here that it
1082  // would actually be quite complex. Therefore we go through an
1083  // alloca.
1084  EndOfInit = CreateTempAlloca(BeginPtr.getType(), getPointerAlign(),
1085  "array.init.end");
1086  CleanupDominator = Builder.CreateStore(BeginPtr.getPointer(), EndOfInit);
1087  pushIrregularPartialArrayCleanup(BeginPtr.getPointer(), EndOfInit,
1088  ElementType, ElementAlign,
1089  getDestroyer(DtorKind));
1090  Cleanup = EHStack.stable_begin();
1091  }
1092 
1093  CharUnits StartAlign = CurPtr.getAlignment();
1094  for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) {
1095  // Tell the cleanup that it needs to destroy up to this
1096  // element. TODO: some of these stores can be trivially
1097  // observed to be unnecessary.
1098  if (EndOfInit.isValid()) {
1099  auto FinishedPtr =
1100  Builder.CreateBitCast(CurPtr.getPointer(), BeginPtr.getType());
1101  Builder.CreateStore(FinishedPtr, EndOfInit);
1102  }
1103  // FIXME: If the last initializer is an incomplete initializer list for
1104  // an array, and we have an array filler, we can fold together the two
1105  // initialization loops.
1106  StoreAnyExprIntoOneUnit(*this, ILE->getInit(i),
1107  ILE->getInit(i)->getType(), CurPtr,
1109  CurPtr = Address(Builder.CreateInBoundsGEP(CurPtr.getPointer(),
1110  Builder.getSize(1),
1111  "array.exp.next"),
1112  StartAlign.alignmentAtOffset((i + 1) * ElementSize));
1113  }
1114 
1115  // The remaining elements are filled with the array filler expression.
1116  Init = ILE->getArrayFiller();
1117 
1118  // Extract the initializer for the individual array elements by pulling
1119  // out the array filler from all the nested initializer lists. This avoids
1120  // generating a nested loop for the initialization.
1121  while (Init && Init->getType()->isConstantArrayType()) {
1122  auto *SubILE = dyn_cast<InitListExpr>(Init);
1123  if (!SubILE)
1124  break;
1125  assert(SubILE->getNumInits() == 0 && "explicit inits in array filler?");
1126  Init = SubILE->getArrayFiller();
1127  }
1128 
1129  // Switch back to initializing one base element at a time.
1130  CurPtr = Builder.CreateBitCast(CurPtr, BeginPtr.getType());
1131  }
1132 
1133  // If all elements have already been initialized, skip any further
1134  // initialization.
1135  llvm::ConstantInt *ConstNum = dyn_cast<llvm::ConstantInt>(NumElements);
1136  if (ConstNum && ConstNum->getZExtValue() <= InitListElements) {
1137  // If there was a Cleanup, deactivate it.
1138  if (CleanupDominator)
1139  DeactivateCleanupBlock(Cleanup, CleanupDominator);
1140  return;
1141  }
1142 
1143  assert(Init && "have trailing elements to initialize but no initializer");
1144 
1145  // If this is a constructor call, try to optimize it out, and failing that
1146  // emit a single loop to initialize all remaining elements.
1147  if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
1148  CXXConstructorDecl *Ctor = CCE->getConstructor();
1149  if (Ctor->isTrivial()) {
1150  // If new expression did not specify value-initialization, then there
1151  // is no initialization.
1152  if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty())
1153  return;
1154 
1155  if (TryMemsetInitialization())
1156  return;
1157  }
1158 
1159  // Store the new Cleanup position for irregular Cleanups.
1160  //
1161  // FIXME: Share this cleanup with the constructor call emission rather than
1162  // having it create a cleanup of its own.
1163  if (EndOfInit.isValid())
1164  Builder.CreateStore(CurPtr.getPointer(), EndOfInit);
1165 
1166  // Emit a constructor call loop to initialize the remaining elements.
1167  if (InitListElements)
1168  NumElements = Builder.CreateSub(
1169  NumElements,
1170  llvm::ConstantInt::get(NumElements->getType(), InitListElements));
1171  EmitCXXAggrConstructorCall(Ctor, NumElements, CurPtr, CCE,
1172  /*NewPointerIsChecked*/true,
1173  CCE->requiresZeroInitialization());
1174  return;
1175  }
1176 
1177  // If this is value-initialization, we can usually use memset.
1178  ImplicitValueInitExpr IVIE(ElementType);
1179  if (isa<ImplicitValueInitExpr>(Init)) {
1180  if (TryMemsetInitialization())
1181  return;
1182 
1183  // Switch to an ImplicitValueInitExpr for the element type. This handles
1184  // only one case: multidimensional array new of pointers to members. In
1185  // all other cases, we already have an initializer for the array element.
1186  Init = &IVIE;
1187  }
1188 
1189  // At this point we should have found an initializer for the individual
1190  // elements of the array.
1191  assert(getContext().hasSameUnqualifiedType(ElementType, Init->getType()) &&
1192  "got wrong type of element to initialize");
1193 
1194  // If we have an empty initializer list, we can usually use memset.
1195  if (auto *ILE = dyn_cast<InitListExpr>(Init))
1196  if (ILE->getNumInits() == 0 && TryMemsetInitialization())
1197  return;
1198 
1199  // If we have a struct whose every field is value-initialized, we can
1200  // usually use memset.
1201  if (auto *ILE = dyn_cast<InitListExpr>(Init)) {
1202  if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
1203  if (RType->getDecl()->isStruct()) {
1204  unsigned NumElements = 0;
1205  if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RType->getDecl()))
1206  NumElements = CXXRD->getNumBases();
1207  for (auto *Field : RType->getDecl()->fields())
1208  if (!Field->isUnnamedBitfield())
1209  ++NumElements;
1210  // FIXME: Recurse into nested InitListExprs.
1211  if (ILE->getNumInits() == NumElements)
1212  for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
1213  if (!isa<ImplicitValueInitExpr>(ILE->getInit(i)))
1214  --NumElements;
1215  if (ILE->getNumInits() == NumElements && TryMemsetInitialization())
1216  return;
1217  }
1218  }
1219  }
1220 
1221  // Create the loop blocks.
1222  llvm::BasicBlock *EntryBB = Builder.GetInsertBlock();
1223  llvm::BasicBlock *LoopBB = createBasicBlock("new.loop");
1224  llvm::BasicBlock *ContBB = createBasicBlock("new.loop.end");
1225 
1226  // Find the end of the array, hoisted out of the loop.
1227  llvm::Value *EndPtr =
1228  Builder.CreateInBoundsGEP(BeginPtr.getPointer(), NumElements, "array.end");
1229 
1230  // If the number of elements isn't constant, we have to now check if there is
1231  // anything left to initialize.
1232  if (!ConstNum) {
1233  llvm::Value *IsEmpty =
1234  Builder.CreateICmpEQ(CurPtr.getPointer(), EndPtr, "array.isempty");
1235  Builder.CreateCondBr(IsEmpty, ContBB, LoopBB);
1236  }
1237 
1238  // Enter the loop.
1239  EmitBlock(LoopBB);
1240 
1241  // Set up the current-element phi.
1242  llvm::PHINode *CurPtrPhi =
1243  Builder.CreatePHI(CurPtr.getType(), 2, "array.cur");
1244  CurPtrPhi->addIncoming(CurPtr.getPointer(), EntryBB);
1245 
1246  CurPtr = Address(CurPtrPhi, ElementAlign);
1247 
1248  // Store the new Cleanup position for irregular Cleanups.
1249  if (EndOfInit.isValid())
1250  Builder.CreateStore(CurPtr.getPointer(), EndOfInit);
1251 
1252  // Enter a partial-destruction Cleanup if necessary.
1253  if (!CleanupDominator && needsEHCleanup(DtorKind)) {
1254  pushRegularPartialArrayCleanup(BeginPtr.getPointer(), CurPtr.getPointer(),
1255  ElementType, ElementAlign,
1256  getDestroyer(DtorKind));
1257  Cleanup = EHStack.stable_begin();
1258  CleanupDominator = Builder.CreateUnreachable();
1259  }
1260 
1261  // Emit the initializer into this element.
1262  StoreAnyExprIntoOneUnit(*this, Init, Init->getType(), CurPtr,
1264 
1265  // Leave the Cleanup if we entered one.
1266  if (CleanupDominator) {
1267  DeactivateCleanupBlock(Cleanup, CleanupDominator);
1268  CleanupDominator->eraseFromParent();
1269  }
1270 
1271  // Advance to the next element by adjusting the pointer type as necessary.
1272  llvm::Value *NextPtr =
1273  Builder.CreateConstInBoundsGEP1_32(ElementTy, CurPtr.getPointer(), 1,
1274  "array.next");
1275 
1276  // Check whether we've gotten to the end of the array and, if so,
1277  // exit the loop.
1278  llvm::Value *IsEnd = Builder.CreateICmpEQ(NextPtr, EndPtr, "array.atend");
1279  Builder.CreateCondBr(IsEnd, ContBB, LoopBB);
1280  CurPtrPhi->addIncoming(NextPtr, Builder.GetInsertBlock());
1281 
1282  EmitBlock(ContBB);
1283 }
1284 
1285 static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E,
1286  QualType ElementType, llvm::Type *ElementTy,
1287  Address NewPtr, llvm::Value *NumElements,
1288  llvm::Value *AllocSizeWithoutCookie) {
1289  ApplyDebugLocation DL(CGF, E);
1290  if (E->isArray())
1291  CGF.EmitNewArrayInitializer(E, ElementType, ElementTy, NewPtr, NumElements,
1292  AllocSizeWithoutCookie);
1293  else if (const Expr *Init = E->getInitializer())
1294  StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr,
1296 }
1297 
1298 /// Emit a call to an operator new or operator delete function, as implicitly
1299 /// created by new-expressions and delete-expressions.
1301  const FunctionDecl *CalleeDecl,
1302  const FunctionProtoType *CalleeType,
1303  const CallArgList &Args) {
1304  llvm::CallBase *CallOrInvoke;
1305  llvm::Constant *CalleePtr = CGF.CGM.GetAddrOfFunction(CalleeDecl);
1306  CGCallee Callee = CGCallee::forDirect(CalleePtr, GlobalDecl(CalleeDecl));
1307  RValue RV =
1309  Args, CalleeType, /*ChainCall=*/false),
1310  Callee, ReturnValueSlot(), Args, &CallOrInvoke);
1311 
1312  /// C++1y [expr.new]p10:
1313  /// [In a new-expression,] an implementation is allowed to omit a call
1314  /// to a replaceable global allocation function.
1315  ///
1316  /// We model such elidable calls with the 'builtin' attribute.
1317  llvm::Function *Fn = dyn_cast<llvm::Function>(CalleePtr);
1318  if (CalleeDecl->isReplaceableGlobalAllocationFunction() &&
1319  Fn && Fn->hasFnAttribute(llvm::Attribute::NoBuiltin)) {
1320  CallOrInvoke->addAttribute(llvm::AttributeList::FunctionIndex,
1321  llvm::Attribute::Builtin);
1322  }
1323 
1324  return RV;
1325 }
1326 
1328  const CallExpr *TheCall,
1329  bool IsDelete) {
1330  CallArgList Args;
1331  EmitCallArgs(Args, Type->getParamTypes(), TheCall->arguments());
1332  // Find the allocation or deallocation function that we're calling.
1333  ASTContext &Ctx = getContext();
1334  DeclarationName Name = Ctx.DeclarationNames
1335  .getCXXOperatorName(IsDelete ? OO_Delete : OO_New);
1336 
1337  for (auto *Decl : Ctx.getTranslationUnitDecl()->lookup(Name))
1338  if (auto *FD = dyn_cast<FunctionDecl>(Decl))
1339  if (Ctx.hasSameType(FD->getType(), QualType(Type, 0)))
1340  return EmitNewDeleteCall(*this, FD, Type, Args);
1341  llvm_unreachable("predeclared global operator new/delete is missing");
1342 }
1343 
1344 namespace {
1345 /// The parameters to pass to a usual operator delete.
1346 struct UsualDeleteParams {
1347  bool DestroyingDelete = false;
1348  bool Size = false;
1349  bool Alignment = false;
1350 };
1351 }
1352 
1353 static UsualDeleteParams getUsualDeleteParams(const FunctionDecl *FD) {
1354  UsualDeleteParams Params;
1355 
1356  const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>();
1357  auto AI = FPT->param_type_begin(), AE = FPT->param_type_end();
1358 
1359  // The first argument is always a void*.
1360  ++AI;
1361 
1362  // The next parameter may be a std::destroying_delete_t.
1363  if (FD->isDestroyingOperatorDelete()) {
1364  Params.DestroyingDelete = true;
1365  assert(AI != AE);
1366  ++AI;
1367  }
1368 
1369  // Figure out what other parameters we should be implicitly passing.
1370  if (AI != AE && (*AI)->isIntegerType()) {
1371  Params.Size = true;
1372  ++AI;
1373  }
1374 
1375  if (AI != AE && (*AI)->isAlignValT()) {
1376  Params.Alignment = true;
1377  ++AI;
1378  }
1379 
1380  assert(AI == AE && "unexpected usual deallocation function parameter");
1381  return Params;
1382 }
1383 
1384 namespace {
1385  /// A cleanup to call the given 'operator delete' function upon abnormal
1386  /// exit from a new expression. Templated on a traits type that deals with
1387  /// ensuring that the arguments dominate the cleanup if necessary.
1388  template<typename Traits>
1389  class CallDeleteDuringNew final : public EHScopeStack::Cleanup {
1390  /// Type used to hold llvm::Value*s.
1391  typedef typename Traits::ValueTy ValueTy;
1392  /// Type used to hold RValues.
1393  typedef typename Traits::RValueTy RValueTy;
1394  struct PlacementArg {
1395  RValueTy ArgValue;
1396  QualType ArgType;
1397  };
1398 
1399  unsigned NumPlacementArgs : 31;
1400  unsigned PassAlignmentToPlacementDelete : 1;
1401  const FunctionDecl *OperatorDelete;
1402  ValueTy Ptr;
1403  ValueTy AllocSize;
1404  CharUnits AllocAlign;
1405 
1406  PlacementArg *getPlacementArgs() {
1407  return reinterpret_cast<PlacementArg *>(this + 1);
1408  }
1409 
1410  public:
1411  static size_t getExtraSize(size_t NumPlacementArgs) {
1412  return NumPlacementArgs * sizeof(PlacementArg);
1413  }
1414 
1415  CallDeleteDuringNew(size_t NumPlacementArgs,
1416  const FunctionDecl *OperatorDelete, ValueTy Ptr,
1417  ValueTy AllocSize, bool PassAlignmentToPlacementDelete,
1418  CharUnits AllocAlign)
1419  : NumPlacementArgs(NumPlacementArgs),
1420  PassAlignmentToPlacementDelete(PassAlignmentToPlacementDelete),
1421  OperatorDelete(OperatorDelete), Ptr(Ptr), AllocSize(AllocSize),
1422  AllocAlign(AllocAlign) {}
1423 
1424  void setPlacementArg(unsigned I, RValueTy Arg, QualType Type) {
1425  assert(I < NumPlacementArgs && "index out of range");
1426  getPlacementArgs()[I] = {Arg, Type};
1427  }
1428 
1429  void Emit(CodeGenFunction &CGF, Flags flags) override {
1430  const auto *FPT = OperatorDelete->getType()->castAs<FunctionProtoType>();
1431  CallArgList DeleteArgs;
1432 
1433  // The first argument is always a void* (or C* for a destroying operator
1434  // delete for class type C).
1435  DeleteArgs.add(Traits::get(CGF, Ptr), FPT->getParamType(0));
1436 
1437  // Figure out what other parameters we should be implicitly passing.
1438  UsualDeleteParams Params;
1439  if (NumPlacementArgs) {
1440  // A placement deallocation function is implicitly passed an alignment
1441  // if the placement allocation function was, but is never passed a size.
1442  Params.Alignment = PassAlignmentToPlacementDelete;
1443  } else {
1444  // For a non-placement new-expression, 'operator delete' can take a
1445  // size and/or an alignment if it has the right parameters.
1446  Params = getUsualDeleteParams(OperatorDelete);
1447  }
1448 
1449  assert(!Params.DestroyingDelete &&
1450  "should not call destroying delete in a new-expression");
1451 
1452  // The second argument can be a std::size_t (for non-placement delete).
1453  if (Params.Size)
1454  DeleteArgs.add(Traits::get(CGF, AllocSize),
1455  CGF.getContext().getSizeType());
1456 
1457  // The next (second or third) argument can be a std::align_val_t, which
1458  // is an enum whose underlying type is std::size_t.
1459  // FIXME: Use the right type as the parameter type. Note that in a call
1460  // to operator delete(size_t, ...), we may not have it available.
1461  if (Params.Alignment)
1462  DeleteArgs.add(RValue::get(llvm::ConstantInt::get(
1463  CGF.SizeTy, AllocAlign.getQuantity())),
1464  CGF.getContext().getSizeType());
1465 
1466  // Pass the rest of the arguments, which must match exactly.
1467  for (unsigned I = 0; I != NumPlacementArgs; ++I) {
1468  auto Arg = getPlacementArgs()[I];
1469  DeleteArgs.add(Traits::get(CGF, Arg.ArgValue), Arg.ArgType);
1470  }
1471 
1472  // Call 'operator delete'.
1473  EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs);
1474  }
1475  };
1476 }
1477 
1478 /// Enter a cleanup to call 'operator delete' if the initializer in a
1479 /// new-expression throws.
1481  const CXXNewExpr *E,
1482  Address NewPtr,
1483  llvm::Value *AllocSize,
1484  CharUnits AllocAlign,
1485  const CallArgList &NewArgs) {
1486  unsigned NumNonPlacementArgs = E->passAlignment() ? 2 : 1;
1487 
1488  // If we're not inside a conditional branch, then the cleanup will
1489  // dominate and we can do the easier (and more efficient) thing.
1490  if (!CGF.isInConditionalBranch()) {
1491  struct DirectCleanupTraits {
1492  typedef llvm::Value *ValueTy;
1493  typedef RValue RValueTy;
1494  static RValue get(CodeGenFunction &, ValueTy V) { return RValue::get(V); }
1495  static RValue get(CodeGenFunction &, RValueTy V) { return V; }
1496  };
1497 
1498  typedef CallDeleteDuringNew<DirectCleanupTraits> DirectCleanup;
1499 
1500  DirectCleanup *Cleanup = CGF.EHStack
1501  .pushCleanupWithExtra<DirectCleanup>(EHCleanup,
1502  E->getNumPlacementArgs(),
1503  E->getOperatorDelete(),
1504  NewPtr.getPointer(),
1505  AllocSize,
1506  E->passAlignment(),
1507  AllocAlign);
1508  for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) {
1509  auto &Arg = NewArgs[I + NumNonPlacementArgs];
1510  Cleanup->setPlacementArg(I, Arg.getRValue(CGF), Arg.Ty);
1511  }
1512 
1513  return;
1514  }
1515 
1516  // Otherwise, we need to save all this stuff.
1519  DominatingValue<RValue>::saved_type SavedAllocSize =
1520  DominatingValue<RValue>::save(CGF, RValue::get(AllocSize));
1521 
1522  struct ConditionalCleanupTraits {
1523  typedef DominatingValue<RValue>::saved_type ValueTy;
1524  typedef DominatingValue<RValue>::saved_type RValueTy;
1525  static RValue get(CodeGenFunction &CGF, ValueTy V) {
1526  return V.restore(CGF);
1527  }
1528  };
1529  typedef CallDeleteDuringNew<ConditionalCleanupTraits> ConditionalCleanup;
1530 
1531  ConditionalCleanup *Cleanup = CGF.EHStack
1532  .pushCleanupWithExtra<ConditionalCleanup>(EHCleanup,
1533  E->getNumPlacementArgs(),
1534  E->getOperatorDelete(),
1535  SavedNewPtr,
1536  SavedAllocSize,
1537  E->passAlignment(),
1538  AllocAlign);
1539  for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) {
1540  auto &Arg = NewArgs[I + NumNonPlacementArgs];
1541  Cleanup->setPlacementArg(
1542  I, DominatingValue<RValue>::save(CGF, Arg.getRValue(CGF)), Arg.Ty);
1543  }
1544 
1545  CGF.initFullExprCleanup();
1546 }
1547 
1549  // The element type being allocated.
1550  QualType allocType = getContext().getBaseElementType(E->getAllocatedType());
1551 
1552  // 1. Build a call to the allocation function.
1553  FunctionDecl *allocator = E->getOperatorNew();
1554 
1555  // If there is a brace-initializer, cannot allocate fewer elements than inits.
1556  unsigned minElements = 0;
1557  if (E->isArray() && E->hasInitializer()) {
1558  const InitListExpr *ILE = dyn_cast<InitListExpr>(E->getInitializer());
1559  if (ILE && ILE->isStringLiteralInit())
1560  minElements =
1561  cast<ConstantArrayType>(ILE->getType()->getAsArrayTypeUnsafe())
1562  ->getSize().getZExtValue();
1563  else if (ILE)
1564  minElements = ILE->getNumInits();
1565  }
1566 
1567  llvm::Value *numElements = nullptr;
1568  llvm::Value *allocSizeWithoutCookie = nullptr;
1569  llvm::Value *allocSize =
1570  EmitCXXNewAllocSize(*this, E, minElements, numElements,
1571  allocSizeWithoutCookie);
1572  CharUnits allocAlign = getContext().getTypeAlignInChars(allocType);
1573 
1574  // Emit the allocation call. If the allocator is a global placement
1575  // operator, just "inline" it directly.
1576  Address allocation = Address::invalid();
1577  CallArgList allocatorArgs;
1578  if (allocator->isReservedGlobalPlacementOperator()) {
1579  assert(E->getNumPlacementArgs() == 1);
1580  const Expr *arg = *E->placement_arguments().begin();
1581 
1582  LValueBaseInfo BaseInfo;
1583  allocation = EmitPointerWithAlignment(arg, &BaseInfo);
1584 
1585  // The pointer expression will, in many cases, be an opaque void*.
1586  // In these cases, discard the computed alignment and use the
1587  // formal alignment of the allocated type.
1588  if (BaseInfo.getAlignmentSource() != AlignmentSource::Decl)
1589  allocation = Address(allocation.getPointer(), allocAlign);
1590 
1591  // Set up allocatorArgs for the call to operator delete if it's not
1592  // the reserved global operator.
1593  if (E->getOperatorDelete() &&
1595  allocatorArgs.add(RValue::get(allocSize), getContext().getSizeType());
1596  allocatorArgs.add(RValue::get(allocation.getPointer()), arg->getType());
1597  }
1598 
1599  } else {
1600  const FunctionProtoType *allocatorType =
1601  allocator->getType()->castAs<FunctionProtoType>();
1602  unsigned ParamsToSkip = 0;
1603 
1604  // The allocation size is the first argument.
1605  QualType sizeType = getContext().getSizeType();
1606  allocatorArgs.add(RValue::get(allocSize), sizeType);
1607  ++ParamsToSkip;
1608 
1609  if (allocSize != allocSizeWithoutCookie) {
1610  CharUnits cookieAlign = getSizeAlign(); // FIXME: Ask the ABI.
1611  allocAlign = std::max(allocAlign, cookieAlign);
1612  }
1613 
1614  // The allocation alignment may be passed as the second argument.
1615  if (E->passAlignment()) {
1616  QualType AlignValT = sizeType;
1617  if (allocatorType->getNumParams() > 1) {
1618  AlignValT = allocatorType->getParamType(1);
1619  assert(getContext().hasSameUnqualifiedType(
1620  AlignValT->castAs<EnumType>()->getDecl()->getIntegerType(),
1621  sizeType) &&
1622  "wrong type for alignment parameter");
1623  ++ParamsToSkip;
1624  } else {
1625  // Corner case, passing alignment to 'operator new(size_t, ...)'.
1626  assert(allocator->isVariadic() && "can't pass alignment to allocator");
1627  }
1628  allocatorArgs.add(
1629  RValue::get(llvm::ConstantInt::get(SizeTy, allocAlign.getQuantity())),
1630  AlignValT);
1631  }
1632 
1633  // FIXME: Why do we not pass a CalleeDecl here?
1634  EmitCallArgs(allocatorArgs, allocatorType, E->placement_arguments(),
1635  /*AC*/AbstractCallee(), /*ParamsToSkip*/ParamsToSkip);
1636 
1637  RValue RV =
1638  EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs);
1639 
1640  // If this was a call to a global replaceable allocation function that does
1641  // not take an alignment argument, the allocator is known to produce
1642  // storage that's suitably aligned for any object that fits, up to a known
1643  // threshold. Otherwise assume it's suitably aligned for the allocated type.
1644  CharUnits allocationAlign = allocAlign;
1645  if (!E->passAlignment() &&
1647  unsigned AllocatorAlign = llvm::PowerOf2Floor(std::min<uint64_t>(
1648  Target.getNewAlign(), getContext().getTypeSize(allocType)));
1649  allocationAlign = std::max(
1650  allocationAlign, getContext().toCharUnitsFromBits(AllocatorAlign));
1651  }
1652 
1653  allocation = Address(RV.getScalarVal(), allocationAlign);
1654  }
1655 
1656  // Emit a null check on the allocation result if the allocation
1657  // function is allowed to return null (because it has a non-throwing
1658  // exception spec or is the reserved placement new) and we have an
1659  // interesting initializer will be running sanitizers on the initialization.
1660  bool nullCheck = E->shouldNullCheckAllocation() &&
1661  (!allocType.isPODType(getContext()) || E->hasInitializer() ||
1662  sanitizePerformTypeCheck());
1663 
1664  llvm::BasicBlock *nullCheckBB = nullptr;
1665  llvm::BasicBlock *contBB = nullptr;
1666 
1667  // The null-check means that the initializer is conditionally
1668  // evaluated.
1669  ConditionalEvaluation conditional(*this);
1670 
1671  if (nullCheck) {
1672  conditional.begin(*this);
1673 
1674  nullCheckBB = Builder.GetInsertBlock();
1675  llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull");
1676  contBB = createBasicBlock("new.cont");
1677 
1678  llvm::Value *isNull =
1679  Builder.CreateIsNull(allocation.getPointer(), "new.isnull");
1680  Builder.CreateCondBr(isNull, contBB, notNullBB);
1681  EmitBlock(notNullBB);
1682  }
1683 
1684  // If there's an operator delete, enter a cleanup to call it if an
1685  // exception is thrown.
1686  EHScopeStack::stable_iterator operatorDeleteCleanup;
1687  llvm::Instruction *cleanupDominator = nullptr;
1688  if (E->getOperatorDelete() &&
1690  EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocAlign,
1691  allocatorArgs);
1692  operatorDeleteCleanup = EHStack.stable_begin();
1693  cleanupDominator = Builder.CreateUnreachable();
1694  }
1695 
1696  assert((allocSize == allocSizeWithoutCookie) ==
1697  CalculateCookiePadding(*this, E).isZero());
1698  if (allocSize != allocSizeWithoutCookie) {
1699  assert(E->isArray());
1700  allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation,
1701  numElements,
1702  E, allocType);
1703  }
1704 
1705  llvm::Type *elementTy = ConvertTypeForMem(allocType);
1706  Address result = Builder.CreateElementBitCast(allocation, elementTy);
1707 
1708  // Passing pointer through launder.invariant.group to avoid propagation of
1709  // vptrs information which may be included in previous type.
1710  // To not break LTO with different optimizations levels, we do it regardless
1711  // of optimization level.
1712  if (CGM.getCodeGenOpts().StrictVTablePointers &&
1713  allocator->isReservedGlobalPlacementOperator())
1714  result = Address(Builder.CreateLaunderInvariantGroup(result.getPointer()),
1715  result.getAlignment());
1716 
1717  // Emit sanitizer checks for pointer value now, so that in the case of an
1718  // array it was checked only once and not at each constructor call. We may
1719  // have already checked that the pointer is non-null.
1720  // FIXME: If we have an array cookie and a potentially-throwing allocator,
1721  // we'll null check the wrong pointer here.
1722  SanitizerSet SkippedChecks;
1723  SkippedChecks.set(SanitizerKind::Null, nullCheck);
1726  result.getPointer(), allocType, result.getAlignment(),
1727  SkippedChecks, numElements);
1728 
1729  EmitNewInitializer(*this, E, allocType, elementTy, result, numElements,
1730  allocSizeWithoutCookie);
1731  if (E->isArray()) {
1732  // NewPtr is a pointer to the base element type. If we're
1733  // allocating an array of arrays, we'll need to cast back to the
1734  // array pointer type.
1735  llvm::Type *resultType = ConvertTypeForMem(E->getType());
1736  if (result.getType() != resultType)
1737  result = Builder.CreateBitCast(result, resultType);
1738  }
1739 
1740  // Deactivate the 'operator delete' cleanup if we finished
1741  // initialization.
1742  if (operatorDeleteCleanup.isValid()) {
1743  DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator);
1744  cleanupDominator->eraseFromParent();
1745  }
1746 
1747  llvm::Value *resultPtr = result.getPointer();
1748  if (nullCheck) {
1749  conditional.end(*this);
1750 
1751  llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
1752  EmitBlock(contBB);
1753 
1754  llvm::PHINode *PHI = Builder.CreatePHI(resultPtr->getType(), 2);
1755  PHI->addIncoming(resultPtr, notNullBB);
1756  PHI->addIncoming(llvm::Constant::getNullValue(resultPtr->getType()),
1757  nullCheckBB);
1758 
1759  resultPtr = PHI;
1760  }
1761 
1762  return resultPtr;
1763 }
1764 
1766  llvm::Value *Ptr, QualType DeleteTy,
1767  llvm::Value *NumElements,
1768  CharUnits CookieSize) {
1769  assert((!NumElements && CookieSize.isZero()) ||
1770  DeleteFD->getOverloadedOperator() == OO_Array_Delete);
1771 
1772  const auto *DeleteFTy = DeleteFD->getType()->castAs<FunctionProtoType>();
1773  CallArgList DeleteArgs;
1774 
1775  auto Params = getUsualDeleteParams(DeleteFD);
1776  auto ParamTypeIt = DeleteFTy->param_type_begin();
1777 
1778  // Pass the pointer itself.
1779  QualType ArgTy = *ParamTypeIt++;
1780  llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy));
1781  DeleteArgs.add(RValue::get(DeletePtr), ArgTy);
1782 
1783  // Pass the std::destroying_delete tag if present.
1784  if (Params.DestroyingDelete) {
1785  QualType DDTag = *ParamTypeIt++;
1786  // Just pass an 'undef'. We expect the tag type to be an empty struct.
1787  auto *V = llvm::UndefValue::get(getTypes().ConvertType(DDTag));
1788  DeleteArgs.add(RValue::get(V), DDTag);
1789  }
1790 
1791  // Pass the size if the delete function has a size_t parameter.
1792  if (Params.Size) {
1793  QualType SizeType = *ParamTypeIt++;
1794  CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy);
1795  llvm::Value *Size = llvm::ConstantInt::get(ConvertType(SizeType),
1796  DeleteTypeSize.getQuantity());
1797 
1798  // For array new, multiply by the number of elements.
1799  if (NumElements)
1800  Size = Builder.CreateMul(Size, NumElements);
1801 
1802  // If there is a cookie, add the cookie size.
1803  if (!CookieSize.isZero())
1804  Size = Builder.CreateAdd(
1805  Size, llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity()));
1806 
1807  DeleteArgs.add(RValue::get(Size), SizeType);
1808  }
1809 
1810  // Pass the alignment if the delete function has an align_val_t parameter.
1811  if (Params.Alignment) {
1812  QualType AlignValType = *ParamTypeIt++;
1813  CharUnits DeleteTypeAlign = getContext().toCharUnitsFromBits(
1814  getContext().getTypeAlignIfKnown(DeleteTy));
1815  llvm::Value *Align = llvm::ConstantInt::get(ConvertType(AlignValType),
1816  DeleteTypeAlign.getQuantity());
1817  DeleteArgs.add(RValue::get(Align), AlignValType);
1818  }
1819 
1820  assert(ParamTypeIt == DeleteFTy->param_type_end() &&
1821  "unknown parameter to usual delete function");
1822 
1823  // Emit the call to delete.
1824  EmitNewDeleteCall(*this, DeleteFD, DeleteFTy, DeleteArgs);
1825 }
1826 
1827 namespace {
1828  /// Calls the given 'operator delete' on a single object.
1829  struct CallObjectDelete final : EHScopeStack::Cleanup {
1830  llvm::Value *Ptr;
1831  const FunctionDecl *OperatorDelete;
1832  QualType ElementType;
1833 
1834  CallObjectDelete(llvm::Value *Ptr,
1835  const FunctionDecl *OperatorDelete,
1836  QualType ElementType)
1837  : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {}
1838 
1839  void Emit(CodeGenFunction &CGF, Flags flags) override {
1840  CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType);
1841  }
1842  };
1843 }
1844 
1845 void
1847  llvm::Value *CompletePtr,
1848  QualType ElementType) {
1849  EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup, CompletePtr,
1850  OperatorDelete, ElementType);
1851 }
1852 
1853 /// Emit the code for deleting a single object with a destroying operator
1854 /// delete. If the element type has a non-virtual destructor, Ptr has already
1855 /// been converted to the type of the parameter of 'operator delete'. Otherwise
1856 /// Ptr points to an object of the static type.
1858  const CXXDeleteExpr *DE, Address Ptr,
1859  QualType ElementType) {
1860  auto *Dtor = ElementType->getAsCXXRecordDecl()->getDestructor();
1861  if (Dtor && Dtor->isVirtual())
1862  CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
1863  Dtor);
1864  else
1865  CGF.EmitDeleteCall(DE->getOperatorDelete(), Ptr.getPointer(), ElementType);
1866 }
1867 
1868 /// Emit the code for deleting a single object.
1870  const CXXDeleteExpr *DE,
1871  Address Ptr,
1872  QualType ElementType) {
1873  // C++11 [expr.delete]p3:
1874  // If the static type of the object to be deleted is different from its
1875  // dynamic type, the static type shall be a base class of the dynamic type
1876  // of the object to be deleted and the static type shall have a virtual
1877  // destructor or the behavior is undefined.
1879  DE->getExprLoc(), Ptr.getPointer(),
1880  ElementType);
1881 
1882  const FunctionDecl *OperatorDelete = DE->getOperatorDelete();
1883  assert(!OperatorDelete->isDestroyingOperatorDelete());
1884 
1885  // Find the destructor for the type, if applicable. If the
1886  // destructor is virtual, we'll just emit the vcall and return.
1887  const CXXDestructorDecl *Dtor = nullptr;
1888  if (const RecordType *RT = ElementType->getAs<RecordType>()) {
1889  CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1890  if (RD->hasDefinition() && !RD->hasTrivialDestructor()) {
1891  Dtor = RD->getDestructor();
1892 
1893  if (Dtor->isVirtual()) {
1894  bool UseVirtualCall = true;
1895  const Expr *Base = DE->getArgument();
1896  if (auto *DevirtualizedDtor =
1897  dyn_cast_or_null<const CXXDestructorDecl>(
1898  Dtor->getDevirtualizedMethod(
1899  Base, CGF.CGM.getLangOpts().AppleKext))) {
1900  UseVirtualCall = false;
1901  const CXXRecordDecl *DevirtualizedClass =
1902  DevirtualizedDtor->getParent();
1903  if (declaresSameEntity(getCXXRecord(Base), DevirtualizedClass)) {
1904  // Devirtualized to the class of the base type (the type of the
1905  // whole expression).
1906  Dtor = DevirtualizedDtor;
1907  } else {
1908  // Devirtualized to some other type. Would need to cast the this
1909  // pointer to that type but we don't have support for that yet, so
1910  // do a virtual call. FIXME: handle the case where it is
1911  // devirtualized to the derived type (the type of the inner
1912  // expression) as in EmitCXXMemberOrOperatorMemberCallExpr.
1913  UseVirtualCall = true;
1914  }
1915  }
1916  if (UseVirtualCall) {
1917  CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
1918  Dtor);
1919  return;
1920  }
1921  }
1922  }
1923  }
1924 
1925  // Make sure that we call delete even if the dtor throws.
1926  // This doesn't have to a conditional cleanup because we're going
1927  // to pop it off in a second.
1928  CGF.EHStack.pushCleanup<CallObjectDelete>(NormalAndEHCleanup,
1929  Ptr.getPointer(),
1930  OperatorDelete, ElementType);
1931 
1932  if (Dtor)
1934  /*ForVirtualBase=*/false,
1935  /*Delegating=*/false,
1936  Ptr, ElementType);
1937  else if (auto Lifetime = ElementType.getObjCLifetime()) {
1938  switch (Lifetime) {
1939  case Qualifiers::OCL_None:
1942  break;
1943 
1946  break;
1947 
1948  case Qualifiers::OCL_Weak:
1949  CGF.EmitARCDestroyWeak(Ptr);
1950  break;
1951  }
1952  }
1953 
1954  CGF.PopCleanupBlock();
1955 }
1956 
1957 namespace {
1958  /// Calls the given 'operator delete' on an array of objects.
1959  struct CallArrayDelete final : EHScopeStack::Cleanup {
1960  llvm::Value *Ptr;
1961  const FunctionDecl *OperatorDelete;
1962  llvm::Value *NumElements;
1963  QualType ElementType;
1964  CharUnits CookieSize;
1965 
1966  CallArrayDelete(llvm::Value *Ptr,
1967  const FunctionDecl *OperatorDelete,
1968  llvm::Value *NumElements,
1969  QualType ElementType,
1970  CharUnits CookieSize)
1971  : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements),
1972  ElementType(ElementType), CookieSize(CookieSize) {}
1973 
1974  void Emit(CodeGenFunction &CGF, Flags flags) override {
1975  CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType, NumElements,
1976  CookieSize);
1977  }
1978  };
1979 }
1980 
1981 /// Emit the code for deleting an array of objects.
1983  const CXXDeleteExpr *E,
1984  Address deletedPtr,
1985  QualType elementType) {
1986  llvm::Value *numElements = nullptr;
1987  llvm::Value *allocatedPtr = nullptr;
1988  CharUnits cookieSize;
1989  CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType,
1990  numElements, allocatedPtr, cookieSize);
1991 
1992  assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer");
1993 
1994  // Make sure that we call delete even if one of the dtors throws.
1995  const FunctionDecl *operatorDelete = E->getOperatorDelete();
1996  CGF.EHStack.pushCleanup<CallArrayDelete>(NormalAndEHCleanup,
1997  allocatedPtr, operatorDelete,
1998  numElements, elementType,
1999  cookieSize);
2000 
2001  // Destroy the elements.
2002  if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) {
2003  assert(numElements && "no element count for a type with a destructor!");
2004 
2005  CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType);
2006  CharUnits elementAlign =
2007  deletedPtr.getAlignment().alignmentOfArrayElement(elementSize);
2008 
2009  llvm::Value *arrayBegin = deletedPtr.getPointer();
2010  llvm::Value *arrayEnd =
2011  CGF.Builder.CreateInBoundsGEP(arrayBegin, numElements, "delete.end");
2012 
2013  // Note that it is legal to allocate a zero-length array, and we
2014  // can never fold the check away because the length should always
2015  // come from a cookie.
2016  CGF.emitArrayDestroy(arrayBegin, arrayEnd, elementType, elementAlign,
2017  CGF.getDestroyer(dtorKind),
2018  /*checkZeroLength*/ true,
2019  CGF.needsEHCleanup(dtorKind));
2020  }
2021 
2022  // Pop the cleanup block.
2023  CGF.PopCleanupBlock();
2024 }
2025 
2027  const Expr *Arg = E->getArgument();
2028  Address Ptr = EmitPointerWithAlignment(Arg);
2029 
2030  // Null check the pointer.
2031  llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull");
2032  llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end");
2033 
2034  llvm::Value *IsNull = Builder.CreateIsNull(Ptr.getPointer(), "isnull");
2035 
2036  Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull);
2037  EmitBlock(DeleteNotNull);
2038 
2039  QualType DeleteTy = E->getDestroyedType();
2040 
2041  // A destroying operator delete overrides the entire operation of the
2042  // delete expression.
2044  EmitDestroyingObjectDelete(*this, E, Ptr, DeleteTy);
2045  EmitBlock(DeleteEnd);
2046  return;
2047  }
2048 
2049  // We might be deleting a pointer to array. If so, GEP down to the
2050  // first non-array element.
2051  // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*)
2052  if (DeleteTy->isConstantArrayType()) {
2053  llvm::Value *Zero = Builder.getInt32(0);
2055 
2056  GEP.push_back(Zero); // point at the outermost array
2057 
2058  // For each layer of array type we're pointing at:
2059  while (const ConstantArrayType *Arr
2060  = getContext().getAsConstantArrayType(DeleteTy)) {
2061  // 1. Unpeel the array type.
2062  DeleteTy = Arr->getElementType();
2063 
2064  // 2. GEP to the first element of the array.
2065  GEP.push_back(Zero);
2066  }
2067 
2068  Ptr = Address(Builder.CreateInBoundsGEP(Ptr.getPointer(), GEP, "del.first"),
2069  Ptr.getAlignment());
2070  }
2071 
2072  assert(ConvertTypeForMem(DeleteTy) == Ptr.getElementType());
2073 
2074  if (E->isArrayForm()) {
2075  EmitArrayDelete(*this, E, Ptr, DeleteTy);
2076  } else {
2077  EmitObjectDelete(*this, E, Ptr, DeleteTy);
2078  }
2079 
2080  EmitBlock(DeleteEnd);
2081 }
2082 
2083 static bool isGLValueFromPointerDeref(const Expr *E) {
2084  E = E->IgnoreParens();
2085 
2086  if (const auto *CE = dyn_cast<CastExpr>(E)) {
2087  if (!CE->getSubExpr()->isGLValue())
2088  return false;
2089  return isGLValueFromPointerDeref(CE->getSubExpr());
2090  }
2091 
2092  if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E))
2093  return isGLValueFromPointerDeref(OVE->getSourceExpr());
2094 
2095  if (const auto *BO = dyn_cast<BinaryOperator>(E))
2096  if (BO->getOpcode() == BO_Comma)
2097  return isGLValueFromPointerDeref(BO->getRHS());
2098 
2099  if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(E))
2100  return isGLValueFromPointerDeref(ACO->getTrueExpr()) ||
2101  isGLValueFromPointerDeref(ACO->getFalseExpr());
2102 
2103  // C++11 [expr.sub]p1:
2104  // The expression E1[E2] is identical (by definition) to *((E1)+(E2))
2105  if (isa<ArraySubscriptExpr>(E))
2106  return true;
2107 
2108  if (const auto *UO = dyn_cast<UnaryOperator>(E))
2109  if (UO->getOpcode() == UO_Deref)
2110  return true;
2111 
2112  return false;
2113 }
2114 
2116  llvm::Type *StdTypeInfoPtrTy) {
2117  // Get the vtable pointer.
2118  Address ThisPtr = CGF.EmitLValue(E).getAddress(CGF);
2119 
2120  QualType SrcRecordTy = E->getType();
2121 
2122  // C++ [class.cdtor]p4:
2123  // If the operand of typeid refers to the object under construction or
2124  // destruction and the static type of the operand is neither the constructor
2125  // or destructor’s class nor one of its bases, the behavior is undefined.
2127  ThisPtr.getPointer(), SrcRecordTy);
2128 
2129  // C++ [expr.typeid]p2:
2130  // If the glvalue expression is obtained by applying the unary * operator to
2131  // a pointer and the pointer is a null pointer value, the typeid expression
2132  // throws the std::bad_typeid exception.
2133  //
2134  // However, this paragraph's intent is not clear. We choose a very generous
2135  // interpretation which implores us to consider comma operators, conditional
2136  // operators, parentheses and other such constructs.
2138  isGLValueFromPointerDeref(E), SrcRecordTy)) {
2139  llvm::BasicBlock *BadTypeidBlock =
2140  CGF.createBasicBlock("typeid.bad_typeid");
2141  llvm::BasicBlock *EndBlock = CGF.createBasicBlock("typeid.end");
2142 
2143  llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr.getPointer());
2144  CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock);
2145 
2146  CGF.EmitBlock(BadTypeidBlock);
2147  CGF.CGM.getCXXABI().EmitBadTypeidCall(CGF);
2148  CGF.EmitBlock(EndBlock);
2149  }
2150 
2151  return CGF.CGM.getCXXABI().EmitTypeid(CGF, SrcRecordTy, ThisPtr,
2152  StdTypeInfoPtrTy);
2153 }
2154 
2156  llvm::Type *StdTypeInfoPtrTy =
2157  ConvertType(E->getType())->getPointerTo();
2158 
2159  if (E->isTypeOperand()) {
2160  llvm::Constant *TypeInfo =
2161  CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext()));
2162  return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy);
2163  }
2164 
2165  // C++ [expr.typeid]p2:
2166  // When typeid is applied to a glvalue expression whose type is a
2167  // polymorphic class type, the result refers to a std::type_info object
2168  // representing the type of the most derived object (that is, the dynamic
2169  // type) to which the glvalue refers.
2170  if (E->isPotentiallyEvaluated())
2171  return EmitTypeidFromVTable(*this, E->getExprOperand(),
2172  StdTypeInfoPtrTy);
2173 
2174  QualType OperandTy = E->getExprOperand()->getType();
2175  return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy),
2176  StdTypeInfoPtrTy);
2177 }
2178 
2180  QualType DestTy) {
2181  llvm::Type *DestLTy = CGF.ConvertType(DestTy);
2182  if (DestTy->isPointerType())
2183  return llvm::Constant::getNullValue(DestLTy);
2184 
2185  /// C++ [expr.dynamic.cast]p9:
2186  /// A failed cast to reference type throws std::bad_cast
2187  if (!CGF.CGM.getCXXABI().EmitBadCastCall(CGF))
2188  return nullptr;
2189 
2190  CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end"));
2191  return llvm::UndefValue::get(DestLTy);
2192 }
2193 
2195  const CXXDynamicCastExpr *DCE) {
2196  CGM.EmitExplicitCastExprType(DCE, this);
2197  QualType DestTy = DCE->getTypeAsWritten();
2198 
2199  QualType SrcTy = DCE->getSubExpr()->getType();
2200 
2201  // C++ [expr.dynamic.cast]p7:
2202  // If T is "pointer to cv void," then the result is a pointer to the most
2203  // derived object pointed to by v.
2204  const PointerType *DestPTy = DestTy->getAs<PointerType>();
2205 
2206  bool isDynamicCastToVoid;
2207  QualType SrcRecordTy;
2208  QualType DestRecordTy;
2209  if (DestPTy) {
2210  isDynamicCastToVoid = DestPTy->getPointeeType()->isVoidType();
2211  SrcRecordTy = SrcTy->castAs<PointerType>()->getPointeeType();
2212  DestRecordTy = DestPTy->getPointeeType();
2213  } else {
2214  isDynamicCastToVoid = false;
2215  SrcRecordTy = SrcTy;
2216  DestRecordTy = DestTy->castAs<ReferenceType>()->getPointeeType();
2217  }
2218 
2219  // C++ [class.cdtor]p5:
2220  // If the operand of the dynamic_cast refers to the object under
2221  // construction or destruction and the static type of the operand is not a
2222  // pointer to or object of the constructor or destructor’s own class or one
2223  // of its bases, the dynamic_cast results in undefined behavior.
2224  EmitTypeCheck(TCK_DynamicOperation, DCE->getExprLoc(), ThisAddr.getPointer(),
2225  SrcRecordTy);
2226 
2227  if (DCE->isAlwaysNull())
2228  if (llvm::Value *T = EmitDynamicCastToNull(*this, DestTy))
2229  return T;
2230 
2231  assert(SrcRecordTy->isRecordType() && "source type must be a record type!");
2232 
2233  // C++ [expr.dynamic.cast]p4:
2234  // If the value of v is a null pointer value in the pointer case, the result
2235  // is the null pointer value of type T.
2236  bool ShouldNullCheckSrcValue =
2237  CGM.getCXXABI().shouldDynamicCastCallBeNullChecked(SrcTy->isPointerType(),
2238  SrcRecordTy);
2239 
2240  llvm::BasicBlock *CastNull = nullptr;
2241  llvm::BasicBlock *CastNotNull = nullptr;
2242  llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end");
2243 
2244  if (ShouldNullCheckSrcValue) {
2245  CastNull = createBasicBlock("dynamic_cast.null");
2246  CastNotNull = createBasicBlock("dynamic_cast.notnull");
2247 
2248  llvm::Value *IsNull = Builder.CreateIsNull(ThisAddr.getPointer());
2249  Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
2250  EmitBlock(CastNotNull);
2251  }
2252 
2253  llvm::Value *Value;
2254  if (isDynamicCastToVoid) {
2255  Value = CGM.getCXXABI().EmitDynamicCastToVoid(*this, ThisAddr, SrcRecordTy,
2256  DestTy);
2257  } else {
2258  assert(DestRecordTy->isRecordType() &&
2259  "destination type must be a record type!");
2260  Value = CGM.getCXXABI().EmitDynamicCastCall(*this, ThisAddr, SrcRecordTy,
2261  DestTy, DestRecordTy, CastEnd);
2262  CastNotNull = Builder.GetInsertBlock();
2263  }
2264 
2265  if (ShouldNullCheckSrcValue) {
2266  EmitBranch(CastEnd);
2267 
2268  EmitBlock(CastNull);
2269  EmitBranch(CastEnd);
2270  }
2271 
2272  EmitBlock(CastEnd);
2273 
2274  if (ShouldNullCheckSrcValue) {
2275  llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
2276  PHI->addIncoming(Value, CastNotNull);
2277  PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
2278 
2279  Value = PHI;
2280  }
2281 
2282  return Value;
2283 }
A call to an overloaded operator written using operator syntax.
Definition: ExprCXX.h:78
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:359
virtual void EmitBadTypeidCall(CodeGenFunction &CGF)=0
CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD)
Derives the &#39;this&#39; type for codegen purposes, i.e.
Definition: CGCall.cpp:74
Represents a function declaration or definition.
Definition: Decl.h:1783
bool isReservedGlobalPlacementOperator() const
Determines whether this operator new or delete is one of the reserved global placement operators: voi...
Definition: Decl.cpp:2961
Address getAddress() const
Definition: CGValue.h:583
Complete object ctor.
Definition: ABI.h:25
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2614
QualType getPointeeType() const
Definition: Type.h:2627
llvm::iterator_range< arg_iterator > placement_arguments()
Definition: ExprCXX.h:2305
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition: CGDecl.cpp:2028
A (possibly-)qualified type.
Definition: Type.h:654
static RValue EmitNewDeleteCall(CodeGenFunction &CGF, const FunctionDecl *CalleeDecl, const FunctionProtoType *CalleeType, const CallArgList &Args)
Emit a call to an operator new or operator delete function, as implicitly created by new-expressions ...
Definition: CGExprCXX.cpp:1300
bool isPODType(const ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition: Type.cpp:2185
static CGCallee BuildAppleKextVirtualCall(CodeGenFunction &CGF, GlobalDecl GD, llvm::Type *Ty, const CXXRecordDecl *RD)
Definition: CGCXX.cpp:247
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:2919
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition: Expr.h:2702
void EmitARCDestroyWeak(Address addr)
void @objc_destroyWeak(i8** addr) Essentially objc_storeWeak(addr, nil).
Definition: CGObjC.cpp:2451
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType, llvm::Type *ElementTy, Address NewPtr, llvm::Value *NumElements, llvm::Value *AllocSizeWithoutCookie)
Definition: CGExprCXX.cpp:978
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:557
virtual bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy)=0
QualType getThisType() const
Return the type of the this pointer.
Definition: DeclCXX.cpp:2352
Checking the &#39;this&#39; pointer for a constructor call.
bool isRecordType() const
Definition: Type.h:6594
Expr * getBase() const
Definition: Expr.h:2913
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
bool isVirtual() const
Definition: DeclCXX.h:1976
FunctionDecl * getOperatorNew() const
Definition: ExprCXX.h:2218
Opcode getOpcode() const
Definition: Expr.h:3469
bool hasQualifier() const
Determines whether this member expression actually had a C++ nested-name-specifier prior to the name ...
Definition: Expr.h:2933
The base class of the type hierarchy.
Definition: Type.h:1450
bool requiresZeroInitialization() const
Whether this construction first requires zero-initialization before the initializer is called...
Definition: ExprCXX.h:1533
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
EmitComplexExprIntoLValue - Emit the given expression of complex type and place its result into the s...
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2889
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1422
RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E, ReturnValueSlot ReturnValue)
Definition: CGExprCXX.cpp:179
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2383
Expr * ignoreParenBaseCasts() LLVM_READONLY
Skip past any parentheses and derived-to-base casts until reaching a fixed point. ...
Definition: Expr.cpp:3017
static llvm::Value * EmitCXXNewAllocSize(CodeGenFunction &CGF, const CXXNewExpr *e, unsigned minElements, llvm::Value *&numElements, llvm::Value *&sizeWithoutCookie)
Definition: CGExprCXX.cpp:688
bool ReturnValue(const T &V, APValue &R)
Convers a value to an APValue.
Definition: Interp.h:41
static saved_type save(CodeGenFunction &CGF, type value)
Definition: EHScopeStack.h:59
QualType getReturnType() const
Definition: Decl.h:2445
unsigned getNumParams() const
Definition: Type.h:3964
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:7002
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:827
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
Definition: StoreRef.h:27
LangAS
Defines the address space values used by the address space qualifier of QualType. ...
Definition: AddressSpaces.h:25
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This, QualType ThisTy)
Definition: CGClass.cpp:2414
IsZeroed_t isZeroed() const
Definition: CGValue.h:616
Address CreateConstInBoundsByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Given a pointer to i8, adjust it by a given constant offset.
Definition: CGBuilder.h:244
llvm::Value * getPointer() const
Definition: Address.h:37
unsigned getNumPlacementArgs() const
Definition: ExprCXX.h:2236
static MemberCallInfo commonEmitCXXMemberOrOperatorCall(CodeGenFunction &CGF, const CXXMethodDecl *MD, llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE, CallArgList &Args, CallArgList *RtlArgs)
Definition: CGExprCXX.cpp:36
RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, ReturnValueSlot ReturnValue)
Definition: CGExprCXX.cpp:488
bool hasDefinition() const
Definition: DeclCXX.h:540
Expr * getExprOperand() const
Definition: ExprCXX.h:821
Represents an expression – generally a full-expression – that introduces cleanups to be run at the ...
Definition: ExprCXX.h:3306
RValue EmitCXXMemberOrOperatorCall(const CXXMethodDecl *Method, const CGCallee &Callee, ReturnValueSlot ReturnValue, llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *E, CallArgList *RtlArgs)
Definition: CGExprCXX.cpp:78
The collection of all-type qualifiers we support.
Definition: Type.h:143
void add(RValue rvalue, QualType type)
Definition: CGCall.h:285
bool mayInsertExtraPadding(bool EmitRemark=false) const
Whether we are allowed to insert extra padding between fields.
Definition: Decl.cpp:4472
bool isMoveAssignmentOperator() const
Determine whether this is a move assignment operator.
Definition: DeclCXX.cpp:2281
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition: DeclCXX.h:1099
An object to manage conditionally-evaluated expressions.
llvm::Value * EmitCXXNewExpr(const CXXNewExpr *E)
Definition: CGExprCXX.cpp:1548
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
ArrayRef< QualType > getParamTypes() const
Definition: Type.h:3971
llvm::Value * EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE)
Definition: CGExprCXX.cpp:2194
static llvm::Value * EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E, llvm::Type *StdTypeInfoPtrTy)
Definition: CGExprCXX.cpp:2115
bool Zero(InterpState &S, CodePtr OpPC)
Definition: Interp.h:812
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:80
QualType getDestroyedType() const
Retrieve the type being destroyed.
Definition: ExprCXX.cpp:369
static AggValueSlot forAddr(Address addr, Qualifiers quals, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
forAddr - Make a slot for an aggregate value.
Definition: CGValue.h:516
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
A metaprogramming class for ensuring that a value will dominate an arbitrary position in a function...
Definition: EHScopeStack.h:65
__DEVICE__ int max(int __a, int __b)
Expr * getSubExpr()
Definition: Expr.h:3202
bool isReplaceableGlobalAllocationFunction(bool *IsAligned=nullptr) const
Determines whether this function is one of the replaceable global allocation functions: void *operato...
Definition: Decl.cpp:2984
Describes an C or C++ initializer list.
Definition: Expr.h:4403
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition: ExprCXX.h:764
Optional< Expr * > getArraySize()
Definition: ExprCXX.h:2225
FunctionDecl * getOperatorDelete() const
Definition: ExprCXX.h:2399
Base object ctor.
Definition: ABI.h:26
bool isElidable() const
Whether this construction is elidable.
Definition: ExprCXX.h:1500
Address CreateElementBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Cast the element type of the given address to a different type, preserving information like the align...
Definition: CGBuilder.h:156
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
void EmitARCDestroyStrong(Address addr, ARCPreciseLifetime_t precise)
Destroy a __strong variable.
Definition: CGObjC.cpp:2277
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type...
Definition: Type.h:7053
bool isOne() const
isOne - Test whether the quantity equals one.
Definition: CharUnits.h:119
Expr * getInitializer()
The initializer of this new-expression.
Definition: ExprCXX.h:2275
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:66
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3434
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:6326
Checking the operand of a dynamic_cast or a typeid expression.
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind...
LangAS getAddressSpace() const
Definition: Type.h:359
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:274
bool isArrow() const
Definition: Expr.h:3020
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, QualType DeleteTy, llvm::Value *NumElements=nullptr, CharUnits CookieSize=CharUnits())
Definition: CGExprCXX.cpp:1765
param_type_iterator param_type_begin() const
Definition: Type.h:4123
bool declaresSameEntity(const Decl *D1, const Decl *D2)
Determine whether two declarations declare the same entity.
Definition: DeclBase.h:1166
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, Address DestPtr, const CXXRecordDecl *Base)
Definition: CGExprCXX.cpp:493
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1269
bool isInstance() const
Definition: DeclCXX.h:1959
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1770
CXXMethodDecl * getCorrespondingMethodInClass(const CXXRecordDecl *RD, bool MayBeBase=false)
Find the method in RD that corresponds to this one.
Definition: DeclCXX.cpp:2036
bool isNegative() const
isNegative - Test whether the quantity is less than zero.
Definition: CharUnits.h:125
This object can be modified without requiring retains or releases.
Definition: Type.h:164
arg_iterator arg_end()
Definition: Expr.h:2747
Checking the &#39;this&#39; pointer for a call to a non-static member function.
TypeSourceInfo * getAllocatedTypeSourceInfo() const
Definition: ExprCXX.h:2197
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
Definition: DeclBase.cpp:1612
bool isArrayForm() const
Definition: ExprCXX.h:2386
QualType getTypeAsWritten() const
getTypeAsWritten - Returns the type that this expression is casting to, as written in the source code...
Definition: Expr.h:3360
bool isValid() const
Definition: Address.h:35
void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest)
Definition: CGExprCXX.cpp:581
Represents a C++ pseudo-destructor (C++ [expr.pseudo]).
Definition: ExprCXX.h:2479
void addFrom(const CallArgList &other)
Add all the arguments from another CallArgList to this one.
Definition: CGCall.h:294
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1690
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3754
bool isDynamicClass() const
Definition: DeclCXX.h:553
virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr)
Returns the extra size required in order to store the array cookie for the given new-expression.
Definition: CGCXXABI.cpp:162
OverloadedOperatorKind getOverloadedOperator() const
getOverloadedOperator - Which C++ overloaded operator this function represents, if any...
Definition: Decl.cpp:3501
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition: ExprCXX.h:1494
NestedNameSpecifier * getQualifier() const
If the member name was qualified, retrieves the nested-name-specifier that precedes the member name...
Definition: Expr.h:2947
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:39
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
SourceLocation getBeginLoc() const
Get the begin source location.
Definition: TypeLoc.cpp:191
This represents one expression.
Definition: Expr.h:108
bool isVariadic() const
Whether this function is variadic.
Definition: Decl.cpp:2827
bool isDefaulted() const
Whether this function is defaulted per C++0x.
Definition: Decl.h:2130
static Address invalid()
Definition: Address.h:34
static void EnterNewDeleteCleanup(CodeGenFunction &CGF, const CXXNewExpr *E, Address NewPtr, llvm::Value *AllocSize, CharUnits AllocAlign, const CallArgList &NewArgs)
Enter a cleanup to call &#39;operator delete&#39; if the initializer in a new-expression throws.
Definition: CGExprCXX.cpp:1480
Address getAddress(CodeGenFunction &CGF) const
Definition: CGValue.h:327
virtual void ReadArrayCookie(CodeGenFunction &CGF, Address Ptr, const CXXDeleteExpr *expr, QualType ElementType, llvm::Value *&NumElements, llvm::Value *&AllocPtr, CharUnits &CookieSize)
Reads the array cookie associated with the given pointer, if it has one.
Definition: CGCXXABI.cpp:202
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
RValue EmitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E)
Definition: CGExprCXX.cpp:118
void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, llvm::Value *CompletePtr, QualType ElementType)
Definition: CGExprCXX.cpp:1846
void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
EmitCallArgs - Emit call arguments for a function.
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7067
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:133
SourceLocation getExprLoc() const LLVM_READONLY
Definition: ExprCXX.h:210
#define V(N, I)
Definition: ASTContext.h:2941
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2649
Expr * getCallee()
Definition: Expr.h:2663
bool isSanitizerChecked() const
Definition: CGValue.h:603
unsigned getNumInits() const
Definition: Expr.h:4433
bool isArrow() const
Determine whether this pseudo-destructor expression was written using an &#39;->&#39; (otherwise, it used a &#39;.
Definition: ExprCXX.h:2542
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:43
virtual bool EmitBadCastCall(CodeGenFunction &CGF)=0
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition: Expr.h:2681
const AstTypeMatcher< ArrayType > arrayType
Matches all kinds of arrays.
A class for recording the number of arguments that a function signature requires. ...
bool isTemporaryObject(ASTContext &Ctx, const CXXRecordDecl *TempTy) const
Determine whether the result of this expression is a temporary object of the given class type...
Definition: Expr.cpp:3095
QualType getType() const
Definition: Expr.h:137
RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue, bool HasQualifier, NestedNameSpecifier *Qualifier, bool IsArrow, const Expr *Base)
Definition: CGExprCXX.cpp:206
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V, QualType Type, CharUnits Alignment=CharUnits::Zero(), SanitizerSet SkippedChecks=SanitizerSet(), llvm::Value *ArraySize=nullptr)
Emit a check that V is the address of storage of the appropriate size and alignment for an object of ...
Definition: CGExpr.cpp:653
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition: CharUnits.h:201
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1784
bool hasInitializer() const
Whether this new-expression has any initializer at all.
Definition: ExprCXX.h:2262
QualType getRecordType(const RecordDecl *Decl) const
static void EmitObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, Address Ptr, QualType ElementType)
Emit the code for deleting a single object.
Definition: CGExprCXX.cpp:1869
llvm::CallInst * CreateMemSet(Address Dest, llvm::Value *Value, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:296
llvm::Value * EmitCastToVoidPtr(llvm::Value *value)
Emit a cast to void* in the appropriate address space.
Definition: CGExpr.cpp:50
QualType getTypeOperand(ASTContext &Context) const
Retrieves the type operand of this typeid() expression after various required adjustments (removing r...
Definition: ExprCXX.cpp:147
void emitArrayDestroy(llvm::Value *begin, llvm::Value *end, QualType elementType, CharUnits elementAlign, Destroyer *destroyer, bool checkZeroLength, bool useEHCleanup)
emitArrayDestroy - Destroys all the elements of the given array, beginning from last to first...
Definition: CGDecl.cpp:2142
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2122
QualType getDestroyedType() const
Retrieve the type being destroyed.
Definition: ExprCXX.cpp:301
const LangOptions & getLangOpts() const
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:719
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:40
The l-value was considered opaque, so the alignment was determined from a type.
RecordDecl * getDecl() const
Definition: Type.h:4505
Expr * getArgument()
Definition: ExprCXX.h:2401
bool isAlignValT() const
Definition: Type.cpp:2542
There is no lifetime qualification on this type.
Definition: Type.h:160
A C++ dynamic_cast expression (C++ [expr.dynamic.cast]).
Definition: ExprCXX.h:445
void set(SanitizerMask K, bool Value)
Enable or disable a certain (single) sanitizer.
Definition: Sanitizers.h:162
Assigning into this object requires the old value to be released and the new value to be retained...
Definition: Type.h:171
QualType getCanonicalType() const
Definition: Type.h:6295
void initFullExprCleanup()
Set up the last cleanup that was pushed as a conditional full-expression cleanup. ...
llvm::Value * EmitCXXTypeidExpr(const CXXTypeidExpr *E)
Definition: CGExprCXX.cpp:2155
Encodes a location in the source.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums...
Definition: Type.h:4521
LangAS getAddressSpace() const
Return the address space of this type.
Definition: Type.h:6377
virtual llvm::Value * EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, Address ThisPtr, llvm::Type *StdTypeInfoPtrTy)=0
A saved depth on the scope stack.
Definition: EHScopeStack.h:106
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **callOrInvoke, SourceLocation Loc)
EmitCall - Generate a call of the given function, expecting the given result type, and using the given argument list which specifies both the LLVM arguments and the types they were derived from.
Definition: CGCall.cpp:3814
bool shouldNullCheckAllocation() const
True if the allocation result needs to be null-checked.
Definition: ExprCXX.cpp:292
static CXXRecordDecl * getCXXRecord(const Expr *E)
Definition: CGExprCXX.cpp:169
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
Definition: ExprCXX.h:2100
Represents a call to a member function that may be written either with member call syntax (e...
Definition: ExprCXX.h:171
const Decl * getDecl() const
Definition: GlobalDecl.h:77
An aggregate value slot.
Definition: CGValue.h:439
A scoped helper to set the current debug location to the specified location or preferred location of ...
Definition: CGDebugInfo.h:737
static void EmitArrayDelete(CodeGenFunction &CGF, const CXXDeleteExpr *E, Address deletedPtr, QualType elementType)
Emit the code for deleting an array of objects.
Definition: CGExprCXX.cpp:1982
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1931
const ConstantArrayType * getAsConstantArrayType(QualType T) const
Definition: ASTContext.h:2466
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
llvm::Constant * EmitNullConstantForBase(const CXXRecordDecl *Record)
Return a null constant appropriate for zero-initializing a base class with the given type...
QualType getAllocatedType() const
Definition: ExprCXX.h:2193
bool isArray() const
Definition: ExprCXX.h:2223
arg_range arguments()
Definition: Expr.h:2739
virtual void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, Address Ptr, QualType ElementType, const CXXDestructorDecl *Dtor)=0
An aligned address.
Definition: Address.h:24
llvm::APInt APInt
Definition: Integral.h:27
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after...
Definition: Type.h:1174
All available information about a concrete callee.
Definition: CGCall.h:66
const CXXRecordDecl * getBestDynamicClassType() const
For an expression of class type or pointer to class type, return the most derived class decl the expr...
Definition: Expr.cpp:62
Complete object dtor.
Definition: ABI.h:35
EnumDecl * getDecl() const
Definition: Type.h:4528
RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, ReturnValueSlot ReturnValue)
Definition: CGExprCXX.cpp:429
Assigning into this object requires a lifetime extension.
Definition: Type.h:177
static void EmitDestroyingObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, Address Ptr, QualType ElementType)
Emit the code for deleting a single object with a destroying operator delete.
Definition: CGExprCXX.cpp:1857
bool passAlignment() const
Indicates whether the required alignment should be implicitly passed to the allocation function...
Definition: ExprCXX.h:2293
FunctionDecl * getOperatorDelete() const
Definition: ExprCXX.h:2220
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:224
Expr * getLHS() const
Definition: Expr.h:3474
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, const CXXNewExpr *E)
Definition: CGExprCXX.cpp:675
CharUnits getNonVirtualAlignment() const
getNonVirtualSize - Get the non-virtual alignment (in chars) of an object, which is the alignment of ...
Definition: RecordLayout.h:210
CGFunctionInfo - Class to encapsulate the information about a function definition.
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset...
Definition: CharUnits.h:194
bool isTypeOperand() const
Definition: ExprCXX.h:804
Dataflow Directional Tag Classes.
Represents a delete expression for memory deallocation and destructor calls, e.g. ...
Definition: ExprCXX.h:2359
Qualifiers getMethodQualifiers() const
Definition: DeclCXX.h:2076
bool isInConditionalBranch() const
isInConditionalBranch - Return true if we&#39;re currently emitting one branch or the other of a conditio...
The name of a declaration.
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:2046
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type *> Tys=None)
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2833
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition: DeclCXX.cpp:2260
bool isDestroyingOperatorDelete() const
Determine whether this is a destroying operator delete.
Definition: Decl.cpp:3057
llvm::Module & getModule() const
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type. ...
Definition: CGExprAgg.cpp:1839
bool isStringLiteralInit() const
Definition: Expr.cpp:2289
static llvm::Value * EmitDynamicCastToNull(CodeGenFunction &CGF, QualType DestTy)
Definition: CGExprCXX.cpp:2179
AlignmentSource getAlignmentSource() const
Definition: CGValue.h:156
static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype, unsigned additional)
Compute the arguments required by the given formal prototype, given that there may be some additional...
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4495
CXXMethodDecl * getDevirtualizedMethod(const Expr *Base, bool IsAppleKext)
If it&#39;s possible to devirtualize a call to this method, return the called function.
Definition: DeclCXX.cpp:2094
CodeGenTypes & getTypes() const
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition: Type.h:6811
arg_iterator arg_begin()
Definition: Expr.h:2744
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:51
Represents a call to a CUDA kernel function.
Definition: ExprCXX.h:224
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Definition: CGDecl.cpp:743
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition: ExprCXX.h:1573
Base for LValueReferenceType and RValueReferenceType.
Definition: Type.h:2750
void EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, const Expr *Exp)
Definition: CGExprCXX.cpp:653
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the given function.
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:473
bool isConstantArrayType() const
Definition: Type.h:6574
static bool isGLValueFromPointerDeref(const Expr *E)
Definition: CGExprCXX.cpp:2083
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition: TypeLoc.h:244
Reading or writing from this object requires a barrier call.
Definition: Type.h:174
QualType getParamType(unsigned i) const
Definition: Type.h:3966
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:2836
Represents a C++ struct/union/class.
Definition: DeclCXX.h:253
virtual const CXXRecordDecl * getThisArgumentTypeForMethod(const CXXMethodDecl *MD)
Get the type of the implicit "this" parameter used by a method.
Definition: CGCXXABI.h:333
bool isVoidType() const
Definition: Type.h:6777
bool hasStrongOrWeakObjCLifetime() const
Definition: Type.h:1088
static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, QualType ElementType, llvm::Type *ElementTy, Address NewPtr, llvm::Value *NumElements, llvm::Value *AllocSizeWithoutCookie)
Definition: CGExprCXX.cpp:1285
llvm::Type * ConvertType(QualType T)
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6283
RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, const CallExpr *TheCallExpr, bool IsDelete)
Definition: CGExprCXX.cpp:1327
void EmitCXXDeleteExpr(const CXXDeleteExpr *E)
Definition: CGExprCXX.cpp:2026
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression...
Definition: CGExpr.cpp:1246
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
Definition: RecordLayout.h:202
T * pushCleanupWithExtra(CleanupKind Kind, size_t N, As... A)
Push a cleanup with non-constant storage requirements on the stack.
Definition: EHScopeStack.h:304
static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, llvm::FunctionType *FTy)
Definition: CGCall.h:143
static UsualDeleteParams getUsualDeleteParams(const FunctionDecl *FD)
Definition: CGExprCXX.cpp:1353
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2546
QualType getIntegerType() const
Return the integer type this enum decl corresponds to.
Definition: Decl.h:3635
llvm::Constant * tryEmitAbstract(const Expr *E, QualType T)
Try to emit the result of the given expression as an abstract constant.
CGCXXABI & getCXXABI() const
bool isAlwaysNull() const
isAlwaysNull - Return whether the result of the dynamic_cast is proven to always be null...
Definition: ExprCXX.cpp:832
static RValue get(llvm::Value *V)
Definition: CGValue.h:86
bool isUnion() const
Definition: Decl.h:3407
Expr * getRHS() const
Definition: Expr.h:3476
bool isPointerType() const
Definition: Type.h:6504
static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init, QualType AllocType, Address NewPtr, AggValueSlot::Overlap_t MayOverlap)
Definition: CGExprCXX.cpp:950
const CGFunctionInfo & arrangeFreeFunctionCall(const CallArgList &Args, const FunctionType *Ty, bool ChainCall)
Figure out the rules for calling a function with the given formal type using the given arguments...
Definition: CGCall.cpp:611
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
bool Null(InterpState &S, CodePtr OpPC)
Definition: Interp.h:818
virtual std::vector< CharUnits > getVBPtrOffsets(const CXXRecordDecl *RD)
Gets the offsets of all the virtual base pointers in a given class.
Definition: CGCXXABI.cpp:313
QualType getType() const
Definition: Decl.h:630
LValue - This represents an lvalue references.
Definition: CGValue.h:167
An abstract representation of regular/ObjC call/message targets.
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:146
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.cpp:1531
bool isPotentiallyEvaluated() const
Determine whether this typeid has a type operand which is potentially evaluated, per C++11 [expr...
Definition: ExprCXX.cpp:132
RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue)
Definition: CGExprCXX.cpp:478
TranslationUnitDecl * getTranslationUnitDecl()
Definition: DeclBase.cpp:362
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:261
void PopCleanupBlock(bool FallThroughIsBranchThrough=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
Definition: CGCleanup.cpp:644
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:2935
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:5117
QualType getPointeeType() const
Definition: Type.h:2853
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:2991
ConstructionKind getConstructionKind() const
Determine whether this constructor is actually constructing a base class (rather than a complete obje...
Definition: ExprCXX.h:1542
CanQualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition: Type.h:1080
param_type_iterator param_type_end() const
Definition: Type.h:4127