clang 8.0.0

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