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