clang 18.0.0git
CGClass.cpp
Go to the documentation of this file.
1//===--- CGClass.cpp - Emit LLVM Code for C++ classes -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code dealing with C++ code generation of classes
10//
11//===----------------------------------------------------------------------===//
12
13#include "CGBlocks.h"
14#include "CGCXXABI.h"
15#include "CGDebugInfo.h"
16#include "CGRecordLayout.h"
17#include "CodeGenFunction.h"
18#include "TargetInfo.h"
19#include "clang/AST/Attr.h"
21#include "clang/AST/CharUnits.h"
25#include "clang/AST/StmtCXX.h"
29#include "llvm/IR/Intrinsics.h"
30#include "llvm/IR/Metadata.h"
31#include "llvm/Support/SaveAndRestore.h"
32#include "llvm/Transforms/Utils/SanitizerStats.h"
33#include <optional>
34
35using namespace clang;
36using namespace CodeGen;
37
38/// Return the best known alignment for an unknown pointer to a
39/// particular class.
41 if (!RD->hasDefinition())
42 return CharUnits::One(); // Hopefully won't be used anywhere.
43
44 auto &layout = getContext().getASTRecordLayout(RD);
45
46 // If the class is final, then we know that the pointer points to an
47 // object of that type and can use the full alignment.
48 if (RD->isEffectivelyFinal())
49 return layout.getAlignment();
50
51 // Otherwise, we have to assume it could be a subclass.
52 return layout.getNonVirtualAlignment();
53}
54
55/// Return the smallest possible amount of storage that might be allocated
56/// starting from the beginning of an object of a particular class.
57///
58/// This may be smaller than sizeof(RD) if RD has virtual base classes.
60 if (!RD->hasDefinition())
61 return CharUnits::One();
62
63 auto &layout = getContext().getASTRecordLayout(RD);
64
65 // If the class is final, then we know that the pointer points to an
66 // object of that type and can use the full alignment.
67 if (RD->isEffectivelyFinal())
68 return layout.getSize();
69
70 // Otherwise, we have to assume it could be a subclass.
71 return std::max(layout.getNonVirtualSize(), CharUnits::One());
72}
73
74/// Return the best known alignment for a pointer to a virtual base,
75/// given the alignment of a pointer to the derived class.
77 const CXXRecordDecl *derivedClass,
78 const CXXRecordDecl *vbaseClass) {
79 // The basic idea here is that an underaligned derived pointer might
80 // indicate an underaligned base pointer.
81
82 assert(vbaseClass->isCompleteDefinition());
83 auto &baseLayout = getContext().getASTRecordLayout(vbaseClass);
84 CharUnits expectedVBaseAlign = baseLayout.getNonVirtualAlignment();
85
86 return getDynamicOffsetAlignment(actualDerivedAlign, derivedClass,
87 expectedVBaseAlign);
88}
89
92 const CXXRecordDecl *baseDecl,
93 CharUnits expectedTargetAlign) {
94 // If the base is an incomplete type (which is, alas, possible with
95 // member pointers), be pessimistic.
96 if (!baseDecl->isCompleteDefinition())
97 return std::min(actualBaseAlign, expectedTargetAlign);
98
99 auto &baseLayout = getContext().getASTRecordLayout(baseDecl);
100 CharUnits expectedBaseAlign = baseLayout.getNonVirtualAlignment();
101
102 // If the class is properly aligned, assume the target offset is, too.
103 //
104 // This actually isn't necessarily the right thing to do --- if the
105 // class is a complete object, but it's only properly aligned for a
106 // base subobject, then the alignments of things relative to it are
107 // probably off as well. (Note that this requires the alignment of
108 // the target to be greater than the NV alignment of the derived
109 // class.)
110 //
111 // However, our approach to this kind of under-alignment can only
112 // ever be best effort; after all, we're never going to propagate
113 // alignments through variables or parameters. Note, in particular,
114 // that constructing a polymorphic type in an address that's less
115 // than pointer-aligned will generally trap in the constructor,
116 // unless we someday add some sort of attribute to change the
117 // assumed alignment of 'this'. So our goal here is pretty much
118 // just to allow the user to explicitly say that a pointer is
119 // under-aligned and then safely access its fields and vtables.
120 if (actualBaseAlign >= expectedBaseAlign) {
121 return expectedTargetAlign;
122 }
123
124 // Otherwise, we might be offset by an arbitrary multiple of the
125 // actual alignment. The correct adjustment is to take the min of
126 // the two alignments.
127 return std::min(actualBaseAlign, expectedTargetAlign);
128}
129
131 assert(CurFuncDecl && "loading 'this' without a func declaration?");
132 auto *MD = cast<CXXMethodDecl>(CurFuncDecl);
133
134 // Lazily compute CXXThisAlignment.
135 if (CXXThisAlignment.isZero()) {
136 // Just use the best known alignment for the parent.
137 // TODO: if we're currently emitting a complete-object ctor/dtor,
138 // we can always use the complete-object alignment.
139 CXXThisAlignment = CGM.getClassPointerAlignment(MD->getParent());
140 }
141
142 llvm::Type *Ty = ConvertType(MD->getFunctionObjectParameterType());
143 return Address(LoadCXXThis(), Ty, CXXThisAlignment, KnownNonNull);
144}
145
146/// Emit the address of a field using a member data pointer.
147///
148/// \param E Only used for emergency diagnostics
151 llvm::Value *memberPtr,
152 const MemberPointerType *memberPtrType,
153 LValueBaseInfo *BaseInfo,
154 TBAAAccessInfo *TBAAInfo) {
155 // Ask the ABI to compute the actual address.
156 llvm::Value *ptr =
158 memberPtr, memberPtrType);
159
160 QualType memberType = memberPtrType->getPointeeType();
161 CharUnits memberAlign =
162 CGM.getNaturalTypeAlignment(memberType, BaseInfo, TBAAInfo);
163 memberAlign =
165 memberPtrType->getClass()->getAsCXXRecordDecl(),
166 memberAlign);
167 return Address(ptr, ConvertTypeForMem(memberPtrType->getPointeeType()),
168 memberAlign);
169}
170
172 const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start,
174 CharUnits Offset = CharUnits::Zero();
175
176 const ASTContext &Context = getContext();
177 const CXXRecordDecl *RD = DerivedClass;
178
179 for (CastExpr::path_const_iterator I = Start; I != End; ++I) {
180 const CXXBaseSpecifier *Base = *I;
181 assert(!Base->isVirtual() && "Should not see virtual bases here!");
182
183 // Get the layout.
184 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
185
186 const auto *BaseDecl =
187 cast<CXXRecordDecl>(Base->getType()->castAs<RecordType>()->getDecl());
188
189 // Add the offset.
190 Offset += Layout.getBaseClassOffset(BaseDecl);
191
192 RD = BaseDecl;
193 }
194
195 return Offset;
196}
197
198llvm::Constant *
202 assert(PathBegin != PathEnd && "Base path should not be empty!");
203
204 CharUnits Offset =
205 computeNonVirtualBaseClassOffset(ClassDecl, PathBegin, PathEnd);
206 if (Offset.isZero())
207 return nullptr;
208
209 llvm::Type *PtrDiffTy =
210 Types.ConvertType(getContext().getPointerDiffType());
211
212 return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
213}
214
215/// Gets the address of a direct base class within a complete object.
216/// This should only be used for (1) non-virtual bases or (2) virtual bases
217/// when the type is known to be complete (e.g. in complete destructors).
218///
219/// The object pointed to by 'This' is assumed to be non-null.
222 const CXXRecordDecl *Derived,
223 const CXXRecordDecl *Base,
224 bool BaseIsVirtual) {
225 // 'this' must be a pointer (in some address space) to Derived.
226 assert(This.getElementType() == ConvertType(Derived));
227
228 // Compute the offset of the virtual base.
229 CharUnits Offset;
230 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
231 if (BaseIsVirtual)
232 Offset = Layout.getVBaseClassOffset(Base);
233 else
234 Offset = Layout.getBaseClassOffset(Base);
235
236 // Shift and cast down to the base type.
237 // TODO: for complete types, this should be possible with a GEP.
238 Address V = This;
239 if (!Offset.isZero()) {
240 V = V.withElementType(Int8Ty);
242 }
243 return V.withElementType(ConvertType(Base));
244}
245
246static Address
248 CharUnits nonVirtualOffset,
249 llvm::Value *virtualOffset,
250 const CXXRecordDecl *derivedClass,
251 const CXXRecordDecl *nearestVBase) {
252 // Assert that we have something to do.
253 assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr);
254
255 // Compute the offset from the static and dynamic components.
256 llvm::Value *baseOffset;
257 if (!nonVirtualOffset.isZero()) {
258 llvm::Type *OffsetType =
261 ? CGF.Int32Ty
262 : CGF.PtrDiffTy;
263 baseOffset =
264 llvm::ConstantInt::get(OffsetType, nonVirtualOffset.getQuantity());
265 if (virtualOffset) {
266 baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset);
267 }
268 } else {
269 baseOffset = virtualOffset;
270 }
271
272 // Apply the base offset.
273 llvm::Value *ptr = addr.getPointer();
274 ptr = CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, ptr, baseOffset, "add.ptr");
275
276 // If we have a virtual component, the alignment of the result will
277 // be relative only to the known alignment of that vbase.
278 CharUnits alignment;
279 if (virtualOffset) {
280 assert(nearestVBase && "virtual offset without vbase?");
281 alignment = CGF.CGM.getVBaseAlignment(addr.getAlignment(),
282 derivedClass, nearestVBase);
283 } else {
284 alignment = addr.getAlignment();
285 }
286 alignment = alignment.alignmentAtOffset(nonVirtualOffset);
287
288 return Address(ptr, CGF.Int8Ty, alignment);
289}
290
292 Address Value, const CXXRecordDecl *Derived,
294 CastExpr::path_const_iterator PathEnd, bool NullCheckValue,
295 SourceLocation Loc) {
296 assert(PathBegin != PathEnd && "Base path should not be empty!");
297
298 CastExpr::path_const_iterator Start = PathBegin;
299 const CXXRecordDecl *VBase = nullptr;
300
301 // Sema has done some convenient canonicalization here: if the
302 // access path involved any virtual steps, the conversion path will
303 // *start* with a step down to the correct virtual base subobject,
304 // and hence will not require any further steps.
305 if ((*Start)->isVirtual()) {
306 VBase = cast<CXXRecordDecl>(
307 (*Start)->getType()->castAs<RecordType>()->getDecl());
308 ++Start;
309 }
310
311 // Compute the static offset of the ultimate destination within its
312 // allocating subobject (the virtual base, if there is one, or else
313 // the "complete" object that we see).
315 VBase ? VBase : Derived, Start, PathEnd);
316
317 // If there's a virtual step, we can sometimes "devirtualize" it.
318 // For now, that's limited to when the derived type is final.
319 // TODO: "devirtualize" this for accesses to known-complete objects.
320 if (VBase && Derived->hasAttr<FinalAttr>()) {
321 const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived);
322 CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase);
323 NonVirtualOffset += vBaseOffset;
324 VBase = nullptr; // we no longer have a virtual step
325 }
326
327 // Get the base pointer type.
328 llvm::Type *BaseValueTy = ConvertType((PathEnd[-1])->getType());
329 llvm::Type *PtrTy = llvm::PointerType::get(
330 CGM.getLLVMContext(), Value.getType()->getPointerAddressSpace());
331
332 QualType DerivedTy = getContext().getRecordType(Derived);
333 CharUnits DerivedAlign = CGM.getClassPointerAlignment(Derived);
334
335 // If the static offset is zero and we don't have a virtual step,
336 // just do a bitcast; null checks are unnecessary.
337 if (NonVirtualOffset.isZero() && !VBase) {
339 SanitizerSet SkippedChecks;
340 SkippedChecks.set(SanitizerKind::Null, !NullCheckValue);
341 EmitTypeCheck(TCK_Upcast, Loc, Value.getPointer(),
342 DerivedTy, DerivedAlign, SkippedChecks);
343 }
344 return Value.withElementType(BaseValueTy);
345 }
346
347 llvm::BasicBlock *origBB = nullptr;
348 llvm::BasicBlock *endBB = nullptr;
349
350 // Skip over the offset (and the vtable load) if we're supposed to
351 // null-check the pointer.
352 if (NullCheckValue) {
353 origBB = Builder.GetInsertBlock();
354 llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull");
355 endBB = createBasicBlock("cast.end");
356
357 llvm::Value *isNull = Builder.CreateIsNull(Value.getPointer());
358 Builder.CreateCondBr(isNull, endBB, notNullBB);
359 EmitBlock(notNullBB);
360 }
361
363 SanitizerSet SkippedChecks;
364 SkippedChecks.set(SanitizerKind::Null, true);
366 Value.getPointer(), DerivedTy, DerivedAlign, SkippedChecks);
367 }
368
369 // Compute the virtual offset.
370 llvm::Value *VirtualOffset = nullptr;
371 if (VBase) {
372 VirtualOffset =
373 CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase);
374 }
375
376 // Apply both offsets.
377 Value = ApplyNonVirtualAndVirtualOffset(*this, Value, NonVirtualOffset,
378 VirtualOffset, Derived, VBase);
379
380 // Cast to the destination type.
381 Value = Value.withElementType(BaseValueTy);
382
383 // Build a phi if we needed a null check.
384 if (NullCheckValue) {
385 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
386 Builder.CreateBr(endBB);
387 EmitBlock(endBB);
388
389 llvm::PHINode *PHI = Builder.CreatePHI(PtrTy, 2, "cast.result");
390 PHI->addIncoming(Value.getPointer(), notNullBB);
391 PHI->addIncoming(llvm::Constant::getNullValue(PtrTy), origBB);
392 Value = Value.withPointer(PHI, NotKnownNonNull);
393 }
394
395 return Value;
396}
397
400 const CXXRecordDecl *Derived,
403 bool NullCheckValue) {
404 assert(PathBegin != PathEnd && "Base path should not be empty!");
405
406 QualType DerivedTy =
407 getContext().getCanonicalType(getContext().getTagDeclType(Derived));
408 llvm::Type *DerivedValueTy = ConvertType(DerivedTy);
409
410 llvm::Value *NonVirtualOffset =
411 CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd);
412
413 if (!NonVirtualOffset) {
414 // No offset, we can just cast back.
415 return BaseAddr.withElementType(DerivedValueTy);
416 }
417
418 llvm::BasicBlock *CastNull = nullptr;
419 llvm::BasicBlock *CastNotNull = nullptr;
420 llvm::BasicBlock *CastEnd = nullptr;
421
422 if (NullCheckValue) {
423 CastNull = createBasicBlock("cast.null");
424 CastNotNull = createBasicBlock("cast.notnull");
425 CastEnd = createBasicBlock("cast.end");
426
427 llvm::Value *IsNull = Builder.CreateIsNull(BaseAddr.getPointer());
428 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
429 EmitBlock(CastNotNull);
430 }
431
432 // Apply the offset.
433 llvm::Value *Value = BaseAddr.getPointer();
434 Value = Builder.CreateInBoundsGEP(
435 Int8Ty, Value, Builder.CreateNeg(NonVirtualOffset), "sub.ptr");
436
437 // Produce a PHI if we had a null-check.
438 if (NullCheckValue) {
439 Builder.CreateBr(CastEnd);
440 EmitBlock(CastNull);
441 Builder.CreateBr(CastEnd);
442 EmitBlock(CastEnd);
443
444 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
445 PHI->addIncoming(Value, CastNotNull);
446 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
447 Value = PHI;
448 }
449
450 return Address(Value, DerivedValueTy, CGM.getClassPointerAlignment(Derived));
451}
452
454 bool ForVirtualBase,
455 bool Delegating) {
456 if (!CGM.getCXXABI().NeedsVTTParameter(GD)) {
457 // This constructor/destructor does not need a VTT parameter.
458 return nullptr;
459 }
460
461 const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent();
462 const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent();
463
464 uint64_t SubVTTIndex;
465
466 if (Delegating) {
467 // If this is a delegating constructor call, just load the VTT.
468 return LoadCXXVTT();
469 } else if (RD == Base) {
470 // If the record matches the base, this is the complete ctor/dtor
471 // variant calling the base variant in a class with virtual bases.
473 "doing no-op VTT offset in base dtor/ctor?");
474 assert(!ForVirtualBase && "Can't have same class as virtual base!");
475 SubVTTIndex = 0;
476 } else {
477 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
478 CharUnits BaseOffset = ForVirtualBase ?
479 Layout.getVBaseClassOffset(Base) :
480 Layout.getBaseClassOffset(Base);
481
482 SubVTTIndex =
483 CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset));
484 assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
485 }
486
488 // A VTT parameter was passed to the constructor, use it.
489 llvm::Value *VTT = LoadCXXVTT();
490 return Builder.CreateConstInBoundsGEP1_64(VoidPtrTy, VTT, SubVTTIndex);
491 } else {
492 // We're the complete constructor, so get the VTT by name.
493 llvm::GlobalValue *VTT = CGM.getVTables().GetAddrOfVTT(RD);
494 return Builder.CreateConstInBoundsGEP2_64(
495 VTT->getValueType(), VTT, 0, SubVTTIndex);
496 }
497}
498
499namespace {
500 /// Call the destructor for a direct base class.
501 struct CallBaseDtor final : EHScopeStack::Cleanup {
502 const CXXRecordDecl *BaseClass;
503 bool BaseIsVirtual;
504 CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual)
505 : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
506
507 void Emit(CodeGenFunction &CGF, Flags flags) override {
508 const CXXRecordDecl *DerivedClass =
509 cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
510
511 const CXXDestructorDecl *D = BaseClass->getDestructor();
512 // We are already inside a destructor, so presumably the object being
513 // destroyed should have the expected type.
515 Address Addr =
517 DerivedClass, BaseClass,
518 BaseIsVirtual);
519 CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual,
520 /*Delegating=*/false, Addr, ThisTy);
521 }
522 };
523
524 /// A visitor which checks whether an initializer uses 'this' in a
525 /// way which requires the vtable to be properly set.
526 struct DynamicThisUseChecker : ConstEvaluatedExprVisitor<DynamicThisUseChecker> {
528
529 bool UsesThis;
530
531 DynamicThisUseChecker(const ASTContext &C) : super(C), UsesThis(false) {}
532
533 // Black-list all explicit and implicit references to 'this'.
534 //
535 // Do we need to worry about external references to 'this' derived
536 // from arbitrary code? If so, then anything which runs arbitrary
537 // external code might potentially access the vtable.
538 void VisitCXXThisExpr(const CXXThisExpr *E) { UsesThis = true; }
539 };
540} // end anonymous namespace
541
543 DynamicThisUseChecker Checker(C);
544 Checker.Visit(Init);
545 return Checker.UsesThis;
546}
547
549 const CXXRecordDecl *ClassDecl,
550 CXXCtorInitializer *BaseInit) {
551 assert(BaseInit->isBaseInitializer() &&
552 "Must have base initializer!");
553
554 Address ThisPtr = CGF.LoadCXXThisAddress();
555
556 const Type *BaseType = BaseInit->getBaseClass();
557 const auto *BaseClassDecl =
558 cast<CXXRecordDecl>(BaseType->castAs<RecordType>()->getDecl());
559
560 bool isBaseVirtual = BaseInit->isBaseVirtual();
561
562 // If the initializer for the base (other than the constructor
563 // itself) accesses 'this' in any way, we need to initialize the
564 // vtables.
565 if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit()))
566 CGF.InitializeVTablePointers(ClassDecl);
567
568 // We can pretend to be a complete class because it only matters for
569 // virtual bases, and we only do virtual bases for complete ctors.
570 Address V =
571 CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl,
572 BaseClassDecl,
573 isBaseVirtual);
574 AggValueSlot AggSlot =
576 V, Qualifiers(),
580 CGF.getOverlapForBaseInit(ClassDecl, BaseClassDecl, isBaseVirtual));
581
582 CGF.EmitAggExpr(BaseInit->getInit(), AggSlot);
583
584 if (CGF.CGM.getLangOpts().Exceptions &&
585 !BaseClassDecl->hasTrivialDestructor())
586 CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl,
587 isBaseVirtual);
588}
589
591 auto *CD = dyn_cast<CXXConstructorDecl>(D);
592 if (!(CD && CD->isCopyOrMoveConstructor()) &&
594 return false;
595
596 // We can emit a memcpy for a trivial copy or move constructor/assignment.
597 if (D->isTrivial() && !D->getParent()->mayInsertExtraPadding())
598 return true;
599
600 // We *must* emit a memcpy for a defaulted union copy or move op.
601 if (D->getParent()->isUnion() && D->isDefaulted())
602 return true;
603
604 return false;
605}
606
608 CXXCtorInitializer *MemberInit,
609 LValue &LHS) {
610 FieldDecl *Field = MemberInit->getAnyMember();
611 if (MemberInit->isIndirectMemberInitializer()) {
612 // If we are initializing an anonymous union field, drill down to the field.
613 IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember();
614 for (const auto *I : IndirectField->chain())
615 LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(I));
616 } else {
617 LHS = CGF.EmitLValueForFieldInitialization(LHS, Field);
618 }
619}
620
622 const CXXRecordDecl *ClassDecl,
623 CXXCtorInitializer *MemberInit,
624 const CXXConstructorDecl *Constructor,
625 FunctionArgList &Args) {
626 ApplyDebugLocation Loc(CGF, MemberInit->getSourceLocation());
627 assert(MemberInit->isAnyMemberInitializer() &&
628 "Must have member initializer!");
629 assert(MemberInit->getInit() && "Must have initializer!");
630
631 // non-static data member initializers.
632 FieldDecl *Field = MemberInit->getAnyMember();
633 QualType FieldType = Field->getType();
634
635 llvm::Value *ThisPtr = CGF.LoadCXXThis();
636 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
637 LValue LHS;
638
639 // If a base constructor is being emitted, create an LValue that has the
640 // non-virtual alignment.
641 if (CGF.CurGD.getCtorType() == Ctor_Base)
642 LHS = CGF.MakeNaturalAlignPointeeAddrLValue(ThisPtr, RecordTy);
643 else
644 LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
645
646 EmitLValueForAnyFieldInitialization(CGF, MemberInit, LHS);
647
648 // Special case: if we are in a copy or move constructor, and we are copying
649 // an array of PODs or classes with trivial copy constructors, ignore the
650 // AST and perform the copy we know is equivalent.
651 // FIXME: This is hacky at best... if we had a bit more explicit information
652 // in the AST, we could generalize it more easily.
653 const ConstantArrayType *Array
654 = CGF.getContext().getAsConstantArrayType(FieldType);
655 if (Array && Constructor->isDefaulted() &&
656 Constructor->isCopyOrMoveConstructor()) {
657 QualType BaseElementTy = CGF.getContext().getBaseElementType(Array);
658 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
659 if (BaseElementTy.isPODType(CGF.getContext()) ||
661 unsigned SrcArgIndex =
662 CGF.CGM.getCXXABI().getSrcArgforCopyCtor(Constructor, Args);
663 llvm::Value *SrcPtr
664 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex]));
665 LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
666 LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field);
667
668 // Copy the aggregate.
669 CGF.EmitAggregateCopy(LHS, Src, FieldType, CGF.getOverlapForFieldInit(Field),
670 LHS.isVolatileQualified());
671 // Ensure that we destroy the objects if an exception is thrown later in
672 // the constructor.
673 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
674 if (CGF.needsEHCleanup(dtorKind))
675 CGF.pushEHDestroy(dtorKind, LHS.getAddress(CGF), FieldType);
676 return;
677 }
678 }
679
680 CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit());
681}
682
684 Expr *Init) {
685 QualType FieldType = Field->getType();
686 switch (getEvaluationKind(FieldType)) {
687 case TEK_Scalar:
688 if (LHS.isSimple()) {
689 EmitExprAsInit(Init, Field, LHS, false);
690 } else {
692 EmitStoreThroughLValue(RHS, LHS);
693 }
694 break;
695 case TEK_Complex:
696 EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true);
697 break;
698 case TEK_Aggregate: {
700 LHS, *this, AggValueSlot::IsDestructed,
703 // Checks are made by the code that calls constructor.
705 EmitAggExpr(Init, Slot);
706 break;
707 }
708 }
709
710 // Ensure that we destroy this object if an exception is thrown
711 // later in the constructor.
712 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
713 if (needsEHCleanup(dtorKind))
714 pushEHDestroy(dtorKind, LHS.getAddress(*this), FieldType);
715}
716
717/// Checks whether the given constructor is a valid subject for the
718/// complete-to-base constructor delegation optimization, i.e.
719/// emitting the complete constructor as a simple call to the base
720/// constructor.
722 const CXXConstructorDecl *Ctor) {
723
724 // Currently we disable the optimization for classes with virtual
725 // bases because (1) the addresses of parameter variables need to be
726 // consistent across all initializers but (2) the delegate function
727 // call necessarily creates a second copy of the parameter variable.
728 //
729 // The limiting example (purely theoretical AFAIK):
730 // struct A { A(int &c) { c++; } };
731 // struct B : virtual A {
732 // B(int count) : A(count) { printf("%d\n", count); }
733 // };
734 // ...although even this example could in principle be emitted as a
735 // delegation since the address of the parameter doesn't escape.
736 if (Ctor->getParent()->getNumVBases()) {
737 // TODO: white-list trivial vbase initializers. This case wouldn't
738 // be subject to the restrictions below.
739
740 // TODO: white-list cases where:
741 // - there are no non-reference parameters to the constructor
742 // - the initializers don't access any non-reference parameters
743 // - the initializers don't take the address of non-reference
744 // parameters
745 // - etc.
746 // If we ever add any of the above cases, remember that:
747 // - function-try-blocks will always exclude this optimization
748 // - we need to perform the constructor prologue and cleanup in
749 // EmitConstructorBody.
750
751 return false;
752 }
753
754 // We also disable the optimization for variadic functions because
755 // it's impossible to "re-pass" varargs.
756 if (Ctor->getType()->castAs<FunctionProtoType>()->isVariadic())
757 return false;
758
759 // FIXME: Decide if we can do a delegation of a delegating constructor.
760 if (Ctor->isDelegatingConstructor())
761 return false;
762
763 return true;
764}
765
766// Emit code in ctor (Prologue==true) or dtor (Prologue==false)
767// to poison the extra field paddings inserted under
768// -fsanitize-address-field-padding=1|2.
770 ASTContext &Context = getContext();
771 const CXXRecordDecl *ClassDecl =
772 Prologue ? cast<CXXConstructorDecl>(CurGD.getDecl())->getParent()
773 : cast<CXXDestructorDecl>(CurGD.getDecl())->getParent();
774 if (!ClassDecl->mayInsertExtraPadding()) return;
775
776 struct SizeAndOffset {
778 uint64_t Offset;
779 };
780
781 unsigned PtrSize = CGM.getDataLayout().getPointerSizeInBits();
782 const ASTRecordLayout &Info = Context.getASTRecordLayout(ClassDecl);
783
784 // Populate sizes and offsets of fields.
786 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i)
787 SSV[i].Offset =
789
790 size_t NumFields = 0;
791 for (const auto *Field : ClassDecl->fields()) {
792 const FieldDecl *D = Field;
793 auto FieldInfo = Context.getTypeInfoInChars(D->getType());
794 CharUnits FieldSize = FieldInfo.Width;
795 assert(NumFields < SSV.size());
796 SSV[NumFields].Size = D->isBitField() ? 0 : FieldSize.getQuantity();
797 NumFields++;
798 }
799 assert(NumFields == SSV.size());
800 if (SSV.size() <= 1) return;
801
802 // We will insert calls to __asan_* run-time functions.
803 // LLVM AddressSanitizer pass may decide to inline them later.
804 llvm::Type *Args[2] = {IntPtrTy, IntPtrTy};
805 llvm::FunctionType *FTy =
806 llvm::FunctionType::get(CGM.VoidTy, Args, false);
807 llvm::FunctionCallee F = CGM.CreateRuntimeFunction(
808 FTy, Prologue ? "__asan_poison_intra_object_redzone"
809 : "__asan_unpoison_intra_object_redzone");
810
811 llvm::Value *ThisPtr = LoadCXXThis();
812 ThisPtr = Builder.CreatePtrToInt(ThisPtr, IntPtrTy);
813 uint64_t TypeSize = Info.getNonVirtualSize().getQuantity();
814 // For each field check if it has sufficient padding,
815 // if so (un)poison it with a call.
816 for (size_t i = 0; i < SSV.size(); i++) {
817 uint64_t AsanAlignment = 8;
818 uint64_t NextField = i == SSV.size() - 1 ? TypeSize : SSV[i + 1].Offset;
819 uint64_t PoisonSize = NextField - SSV[i].Offset - SSV[i].Size;
820 uint64_t EndOffset = SSV[i].Offset + SSV[i].Size;
821 if (PoisonSize < AsanAlignment || !SSV[i].Size ||
822 (NextField % AsanAlignment) != 0)
823 continue;
824 Builder.CreateCall(
825 F, {Builder.CreateAdd(ThisPtr, Builder.getIntN(PtrSize, EndOffset)),
826 Builder.getIntN(PtrSize, PoisonSize)});
827 }
828}
829
830/// EmitConstructorBody - Emits the body of the current constructor.
833 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl());
834 CXXCtorType CtorType = CurGD.getCtorType();
835
837 CtorType == Ctor_Complete) &&
838 "can only generate complete ctor for this ABI");
839
840 // Before we go any further, try the complete->base constructor
841 // delegation optimization.
842 if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) &&
845 return;
846 }
847
848 const FunctionDecl *Definition = nullptr;
849 Stmt *Body = Ctor->getBody(Definition);
850 assert(Definition == Ctor && "emitting wrong constructor body");
851
852 // Enter the function-try-block before the constructor prologue if
853 // applicable.
854 bool IsTryBody = (Body && isa<CXXTryStmt>(Body));
855 if (IsTryBody)
856 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
857
859
860 RunCleanupsScope RunCleanups(*this);
861
862 // TODO: in restricted cases, we can emit the vbase initializers of
863 // a complete ctor and then delegate to the base ctor.
864
865 // Emit the constructor prologue, i.e. the base and member
866 // initializers.
867 EmitCtorPrologue(Ctor, CtorType, Args);
868
869 // Emit the body of the statement.
870 if (IsTryBody)
871 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
872 else if (Body)
873 EmitStmt(Body);
874
875 // Emit any cleanup blocks associated with the member or base
876 // initializers, which includes (along the exceptional path) the
877 // destructors for those members and bases that were fully
878 // constructed.
879 RunCleanups.ForceCleanup();
880
881 if (IsTryBody)
882 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
883}
884
885namespace {
886 /// RAII object to indicate that codegen is copying the value representation
887 /// instead of the object representation. Useful when copying a struct or
888 /// class which has uninitialized members and we're only performing
889 /// lvalue-to-rvalue conversion on the object but not its members.
890 class CopyingValueRepresentation {
891 public:
892 explicit CopyingValueRepresentation(CodeGenFunction &CGF)
893 : CGF(CGF), OldSanOpts(CGF.SanOpts) {
894 CGF.SanOpts.set(SanitizerKind::Bool, false);
895 CGF.SanOpts.set(SanitizerKind::Enum, false);
896 }
897 ~CopyingValueRepresentation() {
898 CGF.SanOpts = OldSanOpts;
899 }
900 private:
901 CodeGenFunction &CGF;
902 SanitizerSet OldSanOpts;
903 };
904} // end anonymous namespace
905
906namespace {
907 class FieldMemcpyizer {
908 public:
909 FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl,
910 const VarDecl *SrcRec)
911 : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec),
912 RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)),
913 FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0),
914 LastFieldOffset(0), LastAddedFieldIndex(0) {}
915
916 bool isMemcpyableField(FieldDecl *F) const {
917 // Never memcpy fields when we are adding poisoned paddings.
918 if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding)
919 return false;
920 Qualifiers Qual = F->getType().getQualifiers();
921 if (Qual.hasVolatile() || Qual.hasObjCLifetime())
922 return false;
923 return true;
924 }
925
926 void addMemcpyableField(FieldDecl *F) {
927 if (F->isZeroSize(CGF.getContext()))
928 return;
929 if (!FirstField)
930 addInitialField(F);
931 else
932 addNextField(F);
933 }
934
935 CharUnits getMemcpySize(uint64_t FirstByteOffset) const {
936 ASTContext &Ctx = CGF.getContext();
937 unsigned LastFieldSize =
938 LastField->isBitField()
939 ? LastField->getBitWidthValue(Ctx)
940 : Ctx.toBits(
941 Ctx.getTypeInfoDataSizeInChars(LastField->getType()).Width);
942 uint64_t MemcpySizeBits = LastFieldOffset + LastFieldSize -
943 FirstByteOffset + Ctx.getCharWidth() - 1;
944 CharUnits MemcpySize = Ctx.toCharUnitsFromBits(MemcpySizeBits);
945 return MemcpySize;
946 }
947
948 void emitMemcpy() {
949 // Give the subclass a chance to bail out if it feels the memcpy isn't
950 // worth it (e.g. Hasn't aggregated enough data).
951 if (!FirstField) {
952 return;
953 }
954
955 uint64_t FirstByteOffset;
956 if (FirstField->isBitField()) {
957 const CGRecordLayout &RL =
958 CGF.getTypes().getCGRecordLayout(FirstField->getParent());
959 const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField);
960 // FirstFieldOffset is not appropriate for bitfields,
961 // we need to use the storage offset instead.
962 FirstByteOffset = CGF.getContext().toBits(BFInfo.StorageOffset);
963 } else {
964 FirstByteOffset = FirstFieldOffset;
965 }
966
967 CharUnits MemcpySize = getMemcpySize(FirstByteOffset);
968 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
969 Address ThisPtr = CGF.LoadCXXThisAddress();
970 LValue DestLV = CGF.MakeAddrLValue(ThisPtr, RecordTy);
971 LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField);
972 llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec));
973 LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
974 LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField);
975
976 emitMemcpyIR(
977 Dest.isBitField() ? Dest.getBitFieldAddress() : Dest.getAddress(CGF),
978 Src.isBitField() ? Src.getBitFieldAddress() : Src.getAddress(CGF),
979 MemcpySize);
980 reset();
981 }
982
983 void reset() {
984 FirstField = nullptr;
985 }
986
987 protected:
988 CodeGenFunction &CGF;
989 const CXXRecordDecl *ClassDecl;
990
991 private:
992 void emitMemcpyIR(Address DestPtr, Address SrcPtr, CharUnits Size) {
993 DestPtr = DestPtr.withElementType(CGF.Int8Ty);
994 SrcPtr = SrcPtr.withElementType(CGF.Int8Ty);
995 CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity());
996 }
997
998 void addInitialField(FieldDecl *F) {
999 FirstField = F;
1000 LastField = F;
1001 FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex());
1002 LastFieldOffset = FirstFieldOffset;
1003 LastAddedFieldIndex = F->getFieldIndex();
1004 }
1005
1006 void addNextField(FieldDecl *F) {
1007 // For the most part, the following invariant will hold:
1008 // F->getFieldIndex() == LastAddedFieldIndex + 1
1009 // The one exception is that Sema won't add a copy-initializer for an
1010 // unnamed bitfield, which will show up here as a gap in the sequence.
1011 assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 &&
1012 "Cannot aggregate fields out of order.");
1013 LastAddedFieldIndex = F->getFieldIndex();
1014
1015 // The 'first' and 'last' fields are chosen by offset, rather than field
1016 // index. This allows the code to support bitfields, as well as regular
1017 // fields.
1018 uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex());
1019 if (FOffset < FirstFieldOffset) {
1020 FirstField = F;
1021 FirstFieldOffset = FOffset;
1022 } else if (FOffset >= LastFieldOffset) {
1023 LastField = F;
1024 LastFieldOffset = FOffset;
1025 }
1026 }
1027
1028 const VarDecl *SrcRec;
1029 const ASTRecordLayout &RecLayout;
1030 FieldDecl *FirstField;
1031 FieldDecl *LastField;
1032 uint64_t FirstFieldOffset, LastFieldOffset;
1033 unsigned LastAddedFieldIndex;
1034 };
1035
1036 class ConstructorMemcpyizer : public FieldMemcpyizer {
1037 private:
1038 /// Get source argument for copy constructor. Returns null if not a copy
1039 /// constructor.
1040 static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF,
1041 const CXXConstructorDecl *CD,
1042 FunctionArgList &Args) {
1043 if (CD->isCopyOrMoveConstructor() && CD->isDefaulted())
1044 return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)];
1045 return nullptr;
1046 }
1047
1048 // Returns true if a CXXCtorInitializer represents a member initialization
1049 // that can be rolled into a memcpy.
1050 bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const {
1051 if (!MemcpyableCtor)
1052 return false;
1053 FieldDecl *Field = MemberInit->getMember();
1054 assert(Field && "No field for member init.");
1055 QualType FieldType = Field->getType();
1056 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
1057
1058 // Bail out on non-memcpyable, not-trivially-copyable members.
1059 if (!(CE && isMemcpyEquivalentSpecialMember(CE->getConstructor())) &&
1060 !(FieldType.isTriviallyCopyableType(CGF.getContext()) ||
1061 FieldType->isReferenceType()))
1062 return false;
1063
1064 // Bail out on volatile fields.
1065 if (!isMemcpyableField(Field))
1066 return false;
1067
1068 // Otherwise we're good.
1069 return true;
1070 }
1071
1072 public:
1073 ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD,
1074 FunctionArgList &Args)
1075 : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CGF, CD, Args)),
1076 ConstructorDecl(CD),
1077 MemcpyableCtor(CD->isDefaulted() &&
1078 CD->isCopyOrMoveConstructor() &&
1079 CGF.getLangOpts().getGC() == LangOptions::NonGC),
1080 Args(Args) { }
1081
1082 void addMemberInitializer(CXXCtorInitializer *MemberInit) {
1083 if (isMemberInitMemcpyable(MemberInit)) {
1084 AggregatedInits.push_back(MemberInit);
1085 addMemcpyableField(MemberInit->getMember());
1086 } else {
1087 emitAggregatedInits();
1088 EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit,
1089 ConstructorDecl, Args);
1090 }
1091 }
1092
1093 void emitAggregatedInits() {
1094 if (AggregatedInits.size() <= 1) {
1095 // This memcpy is too small to be worthwhile. Fall back on default
1096 // codegen.
1097 if (!AggregatedInits.empty()) {
1098 CopyingValueRepresentation CVR(CGF);
1099 EmitMemberInitializer(CGF, ConstructorDecl->getParent(),
1100 AggregatedInits[0], ConstructorDecl, Args);
1101 AggregatedInits.clear();
1102 }
1103 reset();
1104 return;
1105 }
1106
1107 pushEHDestructors();
1108 emitMemcpy();
1109 AggregatedInits.clear();
1110 }
1111
1112 void pushEHDestructors() {
1113 Address ThisPtr = CGF.LoadCXXThisAddress();
1114 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
1115 LValue LHS = CGF.MakeAddrLValue(ThisPtr, RecordTy);
1116
1117 for (unsigned i = 0; i < AggregatedInits.size(); ++i) {
1118 CXXCtorInitializer *MemberInit = AggregatedInits[i];
1119 QualType FieldType = MemberInit->getAnyMember()->getType();
1120 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
1121 if (!CGF.needsEHCleanup(dtorKind))
1122 continue;
1123 LValue FieldLHS = LHS;
1124 EmitLValueForAnyFieldInitialization(CGF, MemberInit, FieldLHS);
1125 CGF.pushEHDestroy(dtorKind, FieldLHS.getAddress(CGF), FieldType);
1126 }
1127 }
1128
1129 void finish() {
1130 emitAggregatedInits();
1131 }
1132
1133 private:
1134 const CXXConstructorDecl *ConstructorDecl;
1135 bool MemcpyableCtor;
1136 FunctionArgList &Args;
1138 };
1139
1140 class AssignmentMemcpyizer : public FieldMemcpyizer {
1141 private:
1142 // Returns the memcpyable field copied by the given statement, if one
1143 // exists. Otherwise returns null.
1144 FieldDecl *getMemcpyableField(Stmt *S) {
1145 if (!AssignmentsMemcpyable)
1146 return nullptr;
1147 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) {
1148 // Recognise trivial assignments.
1149 if (BO->getOpcode() != BO_Assign)
1150 return nullptr;
1151 MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS());
1152 if (!ME)
1153 return nullptr;
1154 FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1155 if (!Field || !isMemcpyableField(Field))
1156 return nullptr;
1157 Stmt *RHS = BO->getRHS();
1158 if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS))
1159 RHS = EC->getSubExpr();
1160 if (!RHS)
1161 return nullptr;
1162 if (MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS)) {
1163 if (ME2->getMemberDecl() == Field)
1164 return Field;
1165 }
1166 return nullptr;
1167 } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) {
1168 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl());
1169 if (!(MD && isMemcpyEquivalentSpecialMember(MD)))
1170 return nullptr;
1171 MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument());
1172 if (!IOA)
1173 return nullptr;
1174 FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl());
1175 if (!Field || !isMemcpyableField(Field))
1176 return nullptr;
1177 MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0));
1178 if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl()))
1179 return nullptr;
1180 return Field;
1181 } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
1182 FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
1183 if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy)
1184 return nullptr;
1185 Expr *DstPtr = CE->getArg(0);
1186 if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr))
1187 DstPtr = DC->getSubExpr();
1188 UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr);
1189 if (!DUO || DUO->getOpcode() != UO_AddrOf)
1190 return nullptr;
1191 MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr());
1192 if (!ME)
1193 return nullptr;
1194 FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1195 if (!Field || !isMemcpyableField(Field))
1196 return nullptr;
1197 Expr *SrcPtr = CE->getArg(1);
1198 if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr))
1199 SrcPtr = SC->getSubExpr();
1200 UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr);
1201 if (!SUO || SUO->getOpcode() != UO_AddrOf)
1202 return nullptr;
1203 MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr());
1204 if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl()))
1205 return nullptr;
1206 return Field;
1207 }
1208
1209 return nullptr;
1210 }
1211
1212 bool AssignmentsMemcpyable;
1213 SmallVector<Stmt*, 16> AggregatedStmts;
1214
1215 public:
1216 AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD,
1217 FunctionArgList &Args)
1218 : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]),
1219 AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) {
1220 assert(Args.size() == 2);
1221 }
1222
1223 void emitAssignment(Stmt *S) {
1224 FieldDecl *F = getMemcpyableField(S);
1225 if (F) {
1226 addMemcpyableField(F);
1227 AggregatedStmts.push_back(S);
1228 } else {
1229 emitAggregatedStmts();
1230 CGF.EmitStmt(S);
1231 }
1232 }
1233
1234 void emitAggregatedStmts() {
1235 if (AggregatedStmts.size() <= 1) {
1236 if (!AggregatedStmts.empty()) {
1237 CopyingValueRepresentation CVR(CGF);
1238 CGF.EmitStmt(AggregatedStmts[0]);
1239 }
1240 reset();
1241 }
1242
1243 emitMemcpy();
1244 AggregatedStmts.clear();
1245 }
1246
1247 void finish() {
1248 emitAggregatedStmts();
1249 }
1250 };
1251} // end anonymous namespace
1252
1254 const Type *BaseType = BaseInit->getBaseClass();
1255 const auto *BaseClassDecl =
1256 cast<CXXRecordDecl>(BaseType->castAs<RecordType>()->getDecl());
1257 return BaseClassDecl->isDynamicClass();
1258}
1259
1260/// EmitCtorPrologue - This routine generates necessary code to initialize
1261/// base classes and non-static data members belonging to this constructor.
1263 CXXCtorType CtorType,
1264 FunctionArgList &Args) {
1265 if (CD->isDelegatingConstructor())
1266 return EmitDelegatingCXXConstructorCall(CD, Args);
1267
1268 const CXXRecordDecl *ClassDecl = CD->getParent();
1269
1271 E = CD->init_end();
1272
1273 // Virtual base initializers first, if any. They aren't needed if:
1274 // - This is a base ctor variant
1275 // - There are no vbases
1276 // - The class is abstract, so a complete object of it cannot be constructed
1277 //
1278 // The check for an abstract class is necessary because sema may not have
1279 // marked virtual base destructors referenced.
1280 bool ConstructVBases = CtorType != Ctor_Base &&
1281 ClassDecl->getNumVBases() != 0 &&
1282 !ClassDecl->isAbstract();
1283
1284 // In the Microsoft C++ ABI, there are no constructor variants. Instead, the
1285 // constructor of a class with virtual bases takes an additional parameter to
1286 // conditionally construct the virtual bases. Emit that check here.
1287 llvm::BasicBlock *BaseCtorContinueBB = nullptr;
1288 if (ConstructVBases &&
1290 BaseCtorContinueBB =
1291 CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl);
1292 assert(BaseCtorContinueBB);
1293 }
1294
1295 for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) {
1296 if (!ConstructVBases)
1297 continue;
1298 SaveAndRestore ThisRAII(CXXThisValue);
1299 if (CGM.getCodeGenOpts().StrictVTablePointers &&
1300 CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1303 EmitBaseInitializer(*this, ClassDecl, *B);
1304 }
1305
1306 if (BaseCtorContinueBB) {
1307 // Complete object handler should continue to the remaining initializers.
1308 Builder.CreateBr(BaseCtorContinueBB);
1309 EmitBlock(BaseCtorContinueBB);
1310 }
1311
1312 // Then, non-virtual base initializers.
1313 for (; B != E && (*B)->isBaseInitializer(); B++) {
1314 assert(!(*B)->isBaseVirtual());
1315 SaveAndRestore ThisRAII(CXXThisValue);
1316 if (CGM.getCodeGenOpts().StrictVTablePointers &&
1317 CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1320 EmitBaseInitializer(*this, ClassDecl, *B);
1321 }
1322
1323 InitializeVTablePointers(ClassDecl);
1324
1325 // And finally, initialize class members.
1326 FieldConstructionScope FCS(*this, LoadCXXThisAddress());
1327 ConstructorMemcpyizer CM(*this, CD, Args);
1328 for (; B != E; B++) {
1329 CXXCtorInitializer *Member = (*B);
1330 assert(!Member->isBaseInitializer());
1331 assert(Member->isAnyMemberInitializer() &&
1332 "Delegating initializer on non-delegating constructor");
1333 CM.addMemberInitializer(Member);
1334 }
1335 CM.finish();
1336}
1337
1338static bool
1339FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field);
1340
1341static bool
1343 const CXXRecordDecl *BaseClassDecl,
1344 const CXXRecordDecl *MostDerivedClassDecl)
1345{
1346 // If the destructor is trivial we don't have to check anything else.
1347 if (BaseClassDecl->hasTrivialDestructor())
1348 return true;
1349
1350 if (!BaseClassDecl->getDestructor()->hasTrivialBody())
1351 return false;
1352
1353 // Check fields.
1354 for (const auto *Field : BaseClassDecl->fields())
1355 if (!FieldHasTrivialDestructorBody(Context, Field))
1356 return false;
1357
1358 // Check non-virtual bases.
1359 for (const auto &I : BaseClassDecl->bases()) {
1360 if (I.isVirtual())
1361 continue;
1362
1364 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1366 MostDerivedClassDecl))
1367 return false;
1368 }
1369
1370 if (BaseClassDecl == MostDerivedClassDecl) {
1371 // Check virtual bases.
1372 for (const auto &I : BaseClassDecl->vbases()) {
1373 const CXXRecordDecl *VirtualBase =
1374 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1376 MostDerivedClassDecl))
1377 return false;
1378 }
1379 }
1380
1381 return true;
1382}
1383
1384static bool
1386 const FieldDecl *Field)
1387{
1388 QualType FieldBaseElementType = Context.getBaseElementType(Field->getType());
1389
1390 const RecordType *RT = FieldBaseElementType->getAs<RecordType>();
1391 if (!RT)
1392 return true;
1393
1394 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1395
1396 // The destructor for an implicit anonymous union member is never invoked.
1397 if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
1398 return false;
1399
1400 return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
1401}
1402
1403/// CanSkipVTablePointerInitialization - Check whether we need to initialize
1404/// any vtable pointers before calling this destructor.
1406 const CXXDestructorDecl *Dtor) {
1407 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1408 if (!ClassDecl->isDynamicClass())
1409 return true;
1410
1411 // For a final class, the vtable pointer is known to already point to the
1412 // class's vtable.
1413 if (ClassDecl->isEffectivelyFinal())
1414 return true;
1415
1416 if (!Dtor->hasTrivialBody())
1417 return false;
1418
1419 // Check the fields.
1420 for (const auto *Field : ClassDecl->fields())
1421 if (!FieldHasTrivialDestructorBody(CGF.getContext(), Field))
1422 return false;
1423
1424 return true;
1425}
1426
1427/// EmitDestructorBody - Emits the body of the current destructor.
1429 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl());
1430 CXXDtorType DtorType = CurGD.getDtorType();
1431
1432 // For an abstract class, non-base destructors are never used (and can't
1433 // be emitted in general, because vbase dtors may not have been validated
1434 // by Sema), but the Itanium ABI doesn't make them optional and Clang may
1435 // in fact emit references to them from other compilations, so emit them
1436 // as functions containing a trap instruction.
1437 if (DtorType != Dtor_Base && Dtor->getParent()->isAbstract()) {
1438 llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap);
1439 TrapCall->setDoesNotReturn();
1440 TrapCall->setDoesNotThrow();
1441 Builder.CreateUnreachable();
1442 Builder.ClearInsertionPoint();
1443 return;
1444 }
1445
1446 Stmt *Body = Dtor->getBody();
1447 if (Body)
1449
1450 // The call to operator delete in a deleting destructor happens
1451 // outside of the function-try-block, which means it's always
1452 // possible to delegate the destructor body to the complete
1453 // destructor. Do so.
1454 if (DtorType == Dtor_Deleting) {
1455 RunCleanupsScope DtorEpilogue(*this);
1457 if (HaveInsertPoint()) {
1459 EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
1460 /*Delegating=*/false, LoadCXXThisAddress(), ThisTy);
1461 }
1462 return;
1463 }
1464
1465 // If the body is a function-try-block, enter the try before
1466 // anything else.
1467 bool isTryBody = (Body && isa<CXXTryStmt>(Body));
1468 if (isTryBody)
1469 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
1471
1472 // Enter the epilogue cleanups.
1473 RunCleanupsScope DtorEpilogue(*this);
1474
1475 // If this is the complete variant, just invoke the base variant;
1476 // the epilogue will destruct the virtual bases. But we can't do
1477 // this optimization if the body is a function-try-block, because
1478 // we'd introduce *two* handler blocks. In the Microsoft ABI, we
1479 // always delegate because we might not have a definition in this TU.
1480 switch (DtorType) {
1481 case Dtor_Comdat: llvm_unreachable("not expecting a COMDAT");
1482 case Dtor_Deleting: llvm_unreachable("already handled deleting case");
1483
1484 case Dtor_Complete:
1485 assert((Body || getTarget().getCXXABI().isMicrosoft()) &&
1486 "can't emit a dtor without a body for non-Microsoft ABIs");
1487
1488 // Enter the cleanup scopes for virtual bases.
1490
1491 if (!isTryBody) {
1493 EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false,
1494 /*Delegating=*/false, LoadCXXThisAddress(), ThisTy);
1495 break;
1496 }
1497
1498 // Fallthrough: act like we're in the base variant.
1499 [[fallthrough]];
1500
1501 case Dtor_Base:
1502 assert(Body);
1503
1504 // Enter the cleanup scopes for fields and non-virtual bases.
1506
1507 // Initialize the vtable pointers before entering the body.
1508 if (!CanSkipVTablePointerInitialization(*this, Dtor)) {
1509 // Insert the llvm.launder.invariant.group intrinsic before initializing
1510 // the vptrs to cancel any previous assumptions we might have made.
1511 if (CGM.getCodeGenOpts().StrictVTablePointers &&
1512 CGM.getCodeGenOpts().OptimizationLevel > 0)
1515 }
1516
1517 if (isTryBody)
1518 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
1519 else if (Body)
1520 EmitStmt(Body);
1521 else {
1522 assert(Dtor->isImplicit() && "bodyless dtor not implicit");
1523 // nothing to do besides what's in the epilogue
1524 }
1525 // -fapple-kext must inline any call to this dtor into
1526 // the caller's body.
1527 if (getLangOpts().AppleKext)
1528 CurFn->addFnAttr(llvm::Attribute::AlwaysInline);
1529
1530 break;
1531 }
1532
1533 // Jump out through the epilogue cleanups.
1534 DtorEpilogue.ForceCleanup();
1535
1536 // Exit the try if applicable.
1537 if (isTryBody)
1538 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
1539}
1540
1542 const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl());
1543 const Stmt *RootS = AssignOp->getBody();
1544 assert(isa<CompoundStmt>(RootS) &&
1545 "Body of an implicit assignment operator should be compound stmt.");
1546 const CompoundStmt *RootCS = cast<CompoundStmt>(RootS);
1547
1548 LexicalScope Scope(*this, RootCS->getSourceRange());
1549
1551 AssignmentMemcpyizer AM(*this, AssignOp, Args);
1552 for (auto *I : RootCS->body())
1553 AM.emitAssignment(I);
1554 AM.finish();
1555}
1556
1557namespace {
1558 llvm::Value *LoadThisForDtorDelete(CodeGenFunction &CGF,
1559 const CXXDestructorDecl *DD) {
1560 if (Expr *ThisArg = DD->getOperatorDeleteThisArg())
1561 return CGF.EmitScalarExpr(ThisArg);
1562 return CGF.LoadCXXThis();
1563 }
1564
1565 /// Call the operator delete associated with the current destructor.
1566 struct CallDtorDelete final : EHScopeStack::Cleanup {
1567 CallDtorDelete() {}
1568
1569 void Emit(CodeGenFunction &CGF, Flags flags) override {
1570 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1571 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1573 LoadThisForDtorDelete(CGF, Dtor),
1574 CGF.getContext().getTagDeclType(ClassDecl));
1575 }
1576 };
1577
1578 void EmitConditionalDtorDeleteCall(CodeGenFunction &CGF,
1579 llvm::Value *ShouldDeleteCondition,
1580 bool ReturnAfterDelete) {
1581 llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete");
1582 llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue");
1583 llvm::Value *ShouldCallDelete
1584 = CGF.Builder.CreateIsNull(ShouldDeleteCondition);
1585 CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB);
1586
1587 CGF.EmitBlock(callDeleteBB);
1588 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1589 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1591 LoadThisForDtorDelete(CGF, Dtor),
1592 CGF.getContext().getTagDeclType(ClassDecl));
1594 ReturnAfterDelete &&
1595 "unexpected value for ReturnAfterDelete");
1596 if (ReturnAfterDelete)
1598 else
1599 CGF.Builder.CreateBr(continueBB);
1600
1601 CGF.EmitBlock(continueBB);
1602 }
1603
1604 struct CallDtorDeleteConditional final : EHScopeStack::Cleanup {
1605 llvm::Value *ShouldDeleteCondition;
1606
1607 public:
1608 CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition)
1609 : ShouldDeleteCondition(ShouldDeleteCondition) {
1610 assert(ShouldDeleteCondition != nullptr);
1611 }
1612
1613 void Emit(CodeGenFunction &CGF, Flags flags) override {
1614 EmitConditionalDtorDeleteCall(CGF, ShouldDeleteCondition,
1615 /*ReturnAfterDelete*/false);
1616 }
1617 };
1618
1619 class DestroyField final : public EHScopeStack::Cleanup {
1620 const FieldDecl *field;
1621 CodeGenFunction::Destroyer *destroyer;
1622 bool useEHCleanupForArray;
1623
1624 public:
1625 DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer,
1626 bool useEHCleanupForArray)
1627 : field(field), destroyer(destroyer),
1628 useEHCleanupForArray(useEHCleanupForArray) {}
1629
1630 void Emit(CodeGenFunction &CGF, Flags flags) override {
1631 // Find the address of the field.
1632 Address thisValue = CGF.LoadCXXThisAddress();
1633 QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent());
1634 LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy);
1635 LValue LV = CGF.EmitLValueForField(ThisLV, field);
1636 assert(LV.isSimple());
1637
1638 CGF.emitDestroy(LV.getAddress(CGF), field->getType(), destroyer,
1639 flags.isForNormalCleanup() && useEHCleanupForArray);
1640 }
1641 };
1642
1643 class DeclAsInlineDebugLocation {
1644 CGDebugInfo *DI;
1645 llvm::MDNode *InlinedAt;
1646 std::optional<ApplyDebugLocation> Location;
1647
1648 public:
1649 DeclAsInlineDebugLocation(CodeGenFunction &CGF, const NamedDecl &Decl)
1650 : DI(CGF.getDebugInfo()) {
1651 if (!DI)
1652 return;
1653 InlinedAt = DI->getInlinedAt();
1654 DI->setInlinedAt(CGF.Builder.getCurrentDebugLocation());
1655 Location.emplace(CGF, Decl.getLocation());
1656 }
1657
1658 ~DeclAsInlineDebugLocation() {
1659 if (!DI)
1660 return;
1661 Location.reset();
1662 DI->setInlinedAt(InlinedAt);
1663 }
1664 };
1665
1666 static void EmitSanitizerDtorCallback(
1667 CodeGenFunction &CGF, StringRef Name, llvm::Value *Ptr,
1668 std::optional<CharUnits::QuantityType> PoisonSize = {}) {
1669 CodeGenFunction::SanitizerScope SanScope(&CGF);
1670 // Pass in void pointer and size of region as arguments to runtime
1671 // function
1672 SmallVector<llvm::Value *, 2> Args = {Ptr};
1673 SmallVector<llvm::Type *, 2> ArgTypes = {CGF.VoidPtrTy};
1674
1675 if (PoisonSize.has_value()) {
1676 Args.emplace_back(llvm::ConstantInt::get(CGF.SizeTy, *PoisonSize));
1677 ArgTypes.emplace_back(CGF.SizeTy);
1678 }
1679
1680 llvm::FunctionType *FnType =
1681 llvm::FunctionType::get(CGF.VoidTy, ArgTypes, false);
1682 llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(FnType, Name);
1683
1684 CGF.EmitNounwindRuntimeCall(Fn, Args);
1685 }
1686
1687 static void
1688 EmitSanitizerDtorFieldsCallback(CodeGenFunction &CGF, llvm::Value *Ptr,
1689 CharUnits::QuantityType PoisonSize) {
1690 EmitSanitizerDtorCallback(CGF, "__sanitizer_dtor_callback_fields", Ptr,
1691 PoisonSize);
1692 }
1693
1694 /// Poison base class with a trivial destructor.
1695 struct SanitizeDtorTrivialBase final : EHScopeStack::Cleanup {
1696 const CXXRecordDecl *BaseClass;
1697 bool BaseIsVirtual;
1698 SanitizeDtorTrivialBase(const CXXRecordDecl *Base, bool BaseIsVirtual)
1699 : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
1700
1701 void Emit(CodeGenFunction &CGF, Flags flags) override {
1702 const CXXRecordDecl *DerivedClass =
1703 cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
1704
1706 CGF.LoadCXXThisAddress(), DerivedClass, BaseClass, BaseIsVirtual);
1707
1708 const ASTRecordLayout &BaseLayout =
1709 CGF.getContext().getASTRecordLayout(BaseClass);
1710 CharUnits BaseSize = BaseLayout.getSize();
1711
1712 if (!BaseSize.isPositive())
1713 return;
1714
1715 // Use the base class declaration location as inline DebugLocation. All
1716 // fields of the class are destroyed.
1717 DeclAsInlineDebugLocation InlineHere(CGF, *BaseClass);
1718 EmitSanitizerDtorFieldsCallback(CGF, Addr.getPointer(),
1719 BaseSize.getQuantity());
1720
1721 // Prevent the current stack frame from disappearing from the stack trace.
1722 CGF.CurFn->addFnAttr("disable-tail-calls", "true");
1723 }
1724 };
1725
1726 class SanitizeDtorFieldRange final : public EHScopeStack::Cleanup {
1727 const CXXDestructorDecl *Dtor;
1728 unsigned StartIndex;
1729 unsigned EndIndex;
1730
1731 public:
1732 SanitizeDtorFieldRange(const CXXDestructorDecl *Dtor, unsigned StartIndex,
1733 unsigned EndIndex)
1734 : Dtor(Dtor), StartIndex(StartIndex), EndIndex(EndIndex) {}
1735
1736 // Generate function call for handling object poisoning.
1737 // Disables tail call elimination, to prevent the current stack frame
1738 // from disappearing from the stack trace.
1739 void Emit(CodeGenFunction &CGF, Flags flags) override {
1740 const ASTContext &Context = CGF.getContext();
1741 const ASTRecordLayout &Layout =
1742 Context.getASTRecordLayout(Dtor->getParent());
1743
1744 // It's a first trivial field so it should be at the begining of a char,
1745 // still round up start offset just in case.
1746 CharUnits PoisonStart = Context.toCharUnitsFromBits(
1747 Layout.getFieldOffset(StartIndex) + Context.getCharWidth() - 1);
1748 llvm::ConstantInt *OffsetSizePtr =
1749 llvm::ConstantInt::get(CGF.SizeTy, PoisonStart.getQuantity());
1750
1751 llvm::Value *OffsetPtr =
1752 CGF.Builder.CreateGEP(CGF.Int8Ty, CGF.LoadCXXThis(), OffsetSizePtr);
1753
1754 CharUnits PoisonEnd;
1755 if (EndIndex >= Layout.getFieldCount()) {
1756 PoisonEnd = Layout.getNonVirtualSize();
1757 } else {
1758 PoisonEnd =
1759 Context.toCharUnitsFromBits(Layout.getFieldOffset(EndIndex));
1760 }
1761 CharUnits PoisonSize = PoisonEnd - PoisonStart;
1762 if (!PoisonSize.isPositive())
1763 return;
1764
1765 // Use the top field declaration location as inline DebugLocation.
1766 DeclAsInlineDebugLocation InlineHere(
1767 CGF, **std::next(Dtor->getParent()->field_begin(), StartIndex));
1768 EmitSanitizerDtorFieldsCallback(CGF, OffsetPtr, PoisonSize.getQuantity());
1769
1770 // Prevent the current stack frame from disappearing from the stack trace.
1771 CGF.CurFn->addFnAttr("disable-tail-calls", "true");
1772 }
1773 };
1774
1775 class SanitizeDtorVTable final : public EHScopeStack::Cleanup {
1776 const CXXDestructorDecl *Dtor;
1777
1778 public:
1779 SanitizeDtorVTable(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {}
1780
1781 // Generate function call for handling vtable pointer poisoning.
1782 void Emit(CodeGenFunction &CGF, Flags flags) override {
1783 assert(Dtor->getParent()->isDynamicClass());
1784 (void)Dtor;
1785 // Poison vtable and vtable ptr if they exist for this class.
1786 llvm::Value *VTablePtr = CGF.LoadCXXThis();
1787
1788 // Pass in void pointer and size of region as arguments to runtime
1789 // function
1790 EmitSanitizerDtorCallback(CGF, "__sanitizer_dtor_callback_vptr",
1791 VTablePtr);
1792 }
1793 };
1794
1795 class SanitizeDtorCleanupBuilder {
1796 ASTContext &Context;
1797 EHScopeStack &EHStack;
1798 const CXXDestructorDecl *DD;
1799 std::optional<unsigned> StartIndex;
1800
1801 public:
1802 SanitizeDtorCleanupBuilder(ASTContext &Context, EHScopeStack &EHStack,
1803 const CXXDestructorDecl *DD)
1804 : Context(Context), EHStack(EHStack), DD(DD), StartIndex(std::nullopt) {}
1805 void PushCleanupForField(const FieldDecl *Field) {
1806 if (Field->isZeroSize(Context))
1807 return;
1808 unsigned FieldIndex = Field->getFieldIndex();
1809 if (FieldHasTrivialDestructorBody(Context, Field)) {
1810 if (!StartIndex)
1811 StartIndex = FieldIndex;
1812 } else if (StartIndex) {
1813 EHStack.pushCleanup<SanitizeDtorFieldRange>(NormalAndEHCleanup, DD,
1814 *StartIndex, FieldIndex);
1815 StartIndex = std::nullopt;
1816 }
1817 }
1818 void End() {
1819 if (StartIndex)
1820 EHStack.pushCleanup<SanitizeDtorFieldRange>(NormalAndEHCleanup, DD,
1821 *StartIndex, -1);
1822 }
1823 };
1824} // end anonymous namespace
1825
1826/// Emit all code that comes at the end of class's
1827/// destructor. This is to call destructors on members and base classes
1828/// in reverse order of their construction.
1829///
1830/// For a deleting destructor, this also handles the case where a destroying
1831/// operator delete completely overrides the definition.
1833 CXXDtorType DtorType) {
1834 assert((!DD->isTrivial() || DD->hasAttr<DLLExportAttr>()) &&
1835 "Should not emit dtor epilogue for non-exported trivial dtor!");
1836
1837 // The deleting-destructor phase just needs to call the appropriate
1838 // operator delete that Sema picked up.
1839 if (DtorType == Dtor_Deleting) {
1840 assert(DD->getOperatorDelete() &&
1841 "operator delete missing - EnterDtorCleanups");
1842 if (CXXStructorImplicitParamValue) {
1843 // If there is an implicit param to the deleting dtor, it's a boolean
1844 // telling whether this is a deleting destructor.
1846 EmitConditionalDtorDeleteCall(*this, CXXStructorImplicitParamValue,
1847 /*ReturnAfterDelete*/true);
1848 else
1849 EHStack.pushCleanup<CallDtorDeleteConditional>(
1850 NormalAndEHCleanup, CXXStructorImplicitParamValue);
1851 } else {
1853 const CXXRecordDecl *ClassDecl = DD->getParent();
1855 LoadThisForDtorDelete(*this, DD),
1856 getContext().getTagDeclType(ClassDecl));
1858 } else {
1859 EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup);
1860 }
1861 }
1862 return;
1863 }
1864
1865 const CXXRecordDecl *ClassDecl = DD->getParent();
1866
1867 // Unions have no bases and do not call field destructors.
1868 if (ClassDecl->isUnion())
1869 return;
1870
1871 // The complete-destructor phase just destructs all the virtual bases.
1872 if (DtorType == Dtor_Complete) {
1873 // Poison the vtable pointer such that access after the base
1874 // and member destructors are invoked is invalid.
1875 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1876 SanOpts.has(SanitizerKind::Memory) && ClassDecl->getNumVBases() &&
1877 ClassDecl->isPolymorphic())
1878 EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD);
1879
1880 // We push them in the forward order so that they'll be popped in
1881 // the reverse order.
1882 for (const auto &Base : ClassDecl->vbases()) {
1883 auto *BaseClassDecl =
1884 cast<CXXRecordDecl>(Base.getType()->castAs<RecordType>()->getDecl());
1885
1886 if (BaseClassDecl->hasTrivialDestructor()) {
1887 // Under SanitizeMemoryUseAfterDtor, poison the trivial base class
1888 // memory. For non-trival base classes the same is done in the class
1889 // destructor.
1890 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1891 SanOpts.has(SanitizerKind::Memory) && !BaseClassDecl->isEmpty())
1892 EHStack.pushCleanup<SanitizeDtorTrivialBase>(NormalAndEHCleanup,
1893 BaseClassDecl,
1894 /*BaseIsVirtual*/ true);
1895 } else {
1896 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, BaseClassDecl,
1897 /*BaseIsVirtual*/ true);
1898 }
1899 }
1900
1901 return;
1902 }
1903
1904 assert(DtorType == Dtor_Base);
1905 // Poison the vtable pointer if it has no virtual bases, but inherits
1906 // virtual functions.
1907 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1908 SanOpts.has(SanitizerKind::Memory) && !ClassDecl->getNumVBases() &&
1909 ClassDecl->isPolymorphic())
1910 EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD);
1911
1912 // Destroy non-virtual bases.
1913 for (const auto &Base : ClassDecl->bases()) {
1914 // Ignore virtual bases.
1915 if (Base.isVirtual())
1916 continue;
1917
1918 CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl();
1919
1920 if (BaseClassDecl->hasTrivialDestructor()) {
1921 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1922 SanOpts.has(SanitizerKind::Memory) && !BaseClassDecl->isEmpty())
1923 EHStack.pushCleanup<SanitizeDtorTrivialBase>(NormalAndEHCleanup,
1924 BaseClassDecl,
1925 /*BaseIsVirtual*/ false);
1926 } else {
1927 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, BaseClassDecl,
1928 /*BaseIsVirtual*/ false);
1929 }
1930 }
1931
1932 // Poison fields such that access after their destructors are
1933 // invoked, and before the base class destructor runs, is invalid.
1934 bool SanitizeFields = CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1935 SanOpts.has(SanitizerKind::Memory);
1936 SanitizeDtorCleanupBuilder SanitizeBuilder(getContext(), EHStack, DD);
1937
1938 // Destroy direct fields.
1939 for (const auto *Field : ClassDecl->fields()) {
1940 if (SanitizeFields)
1941 SanitizeBuilder.PushCleanupForField(Field);
1942
1943 QualType type = Field->getType();
1944 QualType::DestructionKind dtorKind = type.isDestructedType();
1945 if (!dtorKind)
1946 continue;
1947
1948 // Anonymous union members do not have their destructors called.
1949 const RecordType *RT = type->getAsUnionType();
1950 if (RT && RT->getDecl()->isAnonymousStructOrUnion())
1951 continue;
1952
1953 CleanupKind cleanupKind = getCleanupKind(dtorKind);
1954 EHStack.pushCleanup<DestroyField>(
1955 cleanupKind, Field, getDestroyer(dtorKind), cleanupKind & EHCleanup);
1956 }
1957
1958 if (SanitizeFields)
1959 SanitizeBuilder.End();
1960}
1961
1962/// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1963/// constructor for each of several members of an array.
1964///
1965/// \param ctor the constructor to call for each element
1966/// \param arrayType the type of the array to initialize
1967/// \param arrayBegin an arrayType*
1968/// \param zeroInitialize true if each element should be
1969/// zero-initialized before it is constructed
1971 const CXXConstructorDecl *ctor, const ArrayType *arrayType,
1972 Address arrayBegin, const CXXConstructExpr *E, bool NewPointerIsChecked,
1973 bool zeroInitialize) {
1974 QualType elementType;
1975 llvm::Value *numElements =
1976 emitArrayLength(arrayType, elementType, arrayBegin);
1977
1978 EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, E,
1979 NewPointerIsChecked, zeroInitialize);
1980}
1981
1982/// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1983/// constructor for each of several members of an array.
1984///
1985/// \param ctor the constructor to call for each element
1986/// \param numElements the number of elements in the array;
1987/// may be zero
1988/// \param arrayBase a T*, where T is the type constructed by ctor
1989/// \param zeroInitialize true if each element should be
1990/// zero-initialized before it is constructed
1992 llvm::Value *numElements,
1993 Address arrayBase,
1994 const CXXConstructExpr *E,
1995 bool NewPointerIsChecked,
1996 bool zeroInitialize) {
1997 // It's legal for numElements to be zero. This can happen both
1998 // dynamically, because x can be zero in 'new A[x]', and statically,
1999 // because of GCC extensions that permit zero-length arrays. There
2000 // are probably legitimate places where we could assume that this
2001 // doesn't happen, but it's not clear that it's worth it.
2002 llvm::BranchInst *zeroCheckBranch = nullptr;
2003
2004 // Optimize for a constant count.
2005 llvm::ConstantInt *constantCount
2006 = dyn_cast<llvm::ConstantInt>(numElements);
2007 if (constantCount) {
2008 // Just skip out if the constant count is zero.
2009 if (constantCount->isZero()) return;
2010
2011 // Otherwise, emit the check.
2012 } else {
2013 llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop");
2014 llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty");
2015 zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB);
2016 EmitBlock(loopBB);
2017 }
2018
2019 // Find the end of the array.
2020 llvm::Type *elementType = arrayBase.getElementType();
2021 llvm::Value *arrayBegin = arrayBase.getPointer();
2022 llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(
2023 elementType, arrayBegin, numElements, "arrayctor.end");
2024
2025 // Enter the loop, setting up a phi for the current location to initialize.
2026 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
2027 llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop");
2028 EmitBlock(loopBB);
2029 llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2,
2030 "arrayctor.cur");
2031 cur->addIncoming(arrayBegin, entryBB);
2032
2033 // Inside the loop body, emit the constructor call on the array element.
2034
2035 // The alignment of the base, adjusted by the size of a single element,
2036 // provides a conservative estimate of the alignment of every element.
2037 // (This assumes we never start tracking offsetted alignments.)
2038 //
2039 // Note that these are complete objects and so we don't need to
2040 // use the non-virtual size or alignment.
2042 CharUnits eltAlignment =
2043 arrayBase.getAlignment()
2044 .alignmentOfArrayElement(getContext().getTypeSizeInChars(type));
2045 Address curAddr = Address(cur, elementType, eltAlignment);
2046
2047 // Zero initialize the storage, if requested.
2048 if (zeroInitialize)
2049 EmitNullInitialization(curAddr, type);
2050
2051 // C++ [class.temporary]p4:
2052 // There are two contexts in which temporaries are destroyed at a different
2053 // point than the end of the full-expression. The first context is when a
2054 // default constructor is called to initialize an element of an array.
2055 // If the constructor has one or more default arguments, the destruction of
2056 // every temporary created in a default argument expression is sequenced
2057 // before the construction of the next array element, if any.
2058
2059 {
2060 RunCleanupsScope Scope(*this);
2061
2062 // Evaluate the constructor and its arguments in a regular
2063 // partial-destroy cleanup.
2064 if (getLangOpts().Exceptions &&
2065 !ctor->getParent()->hasTrivialDestructor()) {
2066 Destroyer *destroyer = destroyCXXObject;
2067 pushRegularPartialArrayCleanup(arrayBegin, cur, type, eltAlignment,
2068 *destroyer);
2069 }
2070 auto currAVS = AggValueSlot::forAddr(
2071 curAddr, type.getQualifiers(), AggValueSlot::IsDestructed,
2074 NewPointerIsChecked ? AggValueSlot::IsSanitizerChecked
2076 EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/false,
2077 /*Delegating=*/false, currAVS, E);
2078 }
2079
2080 // Go to the next element.
2081 llvm::Value *next = Builder.CreateInBoundsGEP(
2082 elementType, cur, llvm::ConstantInt::get(SizeTy, 1), "arrayctor.next");
2083 cur->addIncoming(next, Builder.GetInsertBlock());
2084
2085 // Check whether that's the end of the loop.
2086 llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done");
2087 llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont");
2088 Builder.CreateCondBr(done, contBB, loopBB);
2089
2090 // Patch the earlier check to skip over the loop.
2091 if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
2092
2093 EmitBlock(contBB);
2094}
2095
2097 Address addr,
2098 QualType type) {
2099 const RecordType *rtype = type->castAs<RecordType>();
2100 const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl());
2101 const CXXDestructorDecl *dtor = record->getDestructor();
2102 assert(!dtor->isTrivial());
2103 CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false,
2104 /*Delegating=*/false, addr, type);
2105}
2106
2109 bool ForVirtualBase,
2110 bool Delegating,
2111 AggValueSlot ThisAVS,
2112 const CXXConstructExpr *E) {
2113 CallArgList Args;
2114 Address This = ThisAVS.getAddress();
2115 LangAS SlotAS = ThisAVS.getQualifiers().getAddressSpace();
2117 llvm::Value *ThisPtr = This.getPointer();
2118
2119 if (SlotAS != ThisAS) {
2120 unsigned TargetThisAS = getContext().getTargetAddressSpace(ThisAS);
2121 llvm::Type *NewType =
2122 llvm::PointerType::get(getLLVMContext(), TargetThisAS);
2123 ThisPtr = getTargetHooks().performAddrSpaceCast(*this, This.getPointer(),
2124 ThisAS, SlotAS, NewType);
2125 }
2126
2127 // Push the this ptr.
2128 Args.add(RValue::get(ThisPtr), D->getThisType());
2129
2130 // If this is a trivial constructor, emit a memcpy now before we lose
2131 // the alignment information on the argument.
2132 // FIXME: It would be better to preserve alignment information into CallArg.
2134 assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
2135
2136 const Expr *Arg = E->getArg(0);
2137 LValue Src = EmitLValue(Arg);
2139 LValue Dest = MakeAddrLValue(This, DestTy);
2140 EmitAggregateCopyCtor(Dest, Src, ThisAVS.mayOverlap());
2141 return;
2142 }
2143
2144 // Add the rest of the user-supplied arguments.
2145 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
2149 EmitCallArgs(Args, FPT, E->arguments(), E->getConstructor(),
2150 /*ParamsToSkip*/ 0, Order);
2151
2152 EmitCXXConstructorCall(D, Type, ForVirtualBase, Delegating, This, Args,
2153 ThisAVS.mayOverlap(), E->getExprLoc(),
2154 ThisAVS.isSanitizerChecked());
2155}
2156
2158 const CXXConstructorDecl *Ctor,
2159 CXXCtorType Type, CallArgList &Args) {
2160 // We can't forward a variadic call.
2161 if (Ctor->isVariadic())
2162 return false;
2163
2165 // If the parameters are callee-cleanup, it's not safe to forward.
2166 for (auto *P : Ctor->parameters())
2167 if (P->needsDestruction(CGF.getContext()))
2168 return false;
2169
2170 // Likewise if they're inalloca.
2171 const CGFunctionInfo &Info =
2172 CGF.CGM.getTypes().arrangeCXXConstructorCall(Args, Ctor, Type, 0, 0);
2173 if (Info.usesInAlloca())
2174 return false;
2175 }
2176
2177 // Anything else should be OK.
2178 return true;
2179}
2180
2183 bool ForVirtualBase,
2184 bool Delegating,
2185 Address This,
2186 CallArgList &Args,
2188 SourceLocation Loc,
2189 bool NewPointerIsChecked) {
2190 const CXXRecordDecl *ClassDecl = D->getParent();
2191
2192 if (!NewPointerIsChecked)
2194 getContext().getRecordType(ClassDecl), CharUnits::Zero());
2195
2196 if (D->isTrivial() && D->isDefaultConstructor()) {
2197 assert(Args.size() == 1 && "trivial default ctor with args");
2198 return;
2199 }
2200
2201 // If this is a trivial constructor, just emit what's needed. If this is a
2202 // union copy constructor, we must emit a memcpy, because the AST does not
2203 // model that copy.
2205 assert(Args.size() == 2 && "unexpected argcount for trivial ctor");
2206
2208 Address Src = Address(Args[1].getRValue(*this).getScalarVal(), ConvertTypeForMem(SrcTy),
2210 LValue SrcLVal = MakeAddrLValue(Src, SrcTy);
2211 QualType DestTy = getContext().getTypeDeclType(ClassDecl);
2212 LValue DestLVal = MakeAddrLValue(This, DestTy);
2213 EmitAggregateCopyCtor(DestLVal, SrcLVal, Overlap);
2214 return;
2215 }
2216
2217 bool PassPrototypeArgs = true;
2218 // Check whether we can actually emit the constructor before trying to do so.
2219 if (auto Inherited = D->getInheritedConstructor()) {
2220 PassPrototypeArgs = getTypes().inheritingCtorHasParams(Inherited, Type);
2221 if (PassPrototypeArgs && !canEmitDelegateCallArgs(*this, D, Type, Args)) {
2223 Delegating, Args);
2224 return;
2225 }
2226 }
2227
2228 // Insert any ABI-specific implicit constructor arguments.
2230 CGM.getCXXABI().addImplicitConstructorArgs(*this, D, Type, ForVirtualBase,
2231 Delegating, Args);
2232
2233 // Emit the call.
2234 llvm::Constant *CalleePtr = CGM.getAddrOfCXXStructor(GlobalDecl(D, Type));
2236 Args, D, Type, ExtraArgs.Prefix, ExtraArgs.Suffix, PassPrototypeArgs);
2238 EmitCall(Info, Callee, ReturnValueSlot(), Args, nullptr, false, Loc);
2239
2240 // Generate vtable assumptions if we're constructing a complete object
2241 // with a vtable. We don't do this for base subobjects for two reasons:
2242 // first, it's incorrect for classes with virtual bases, and second, we're
2243 // about to overwrite the vptrs anyway.
2244 // We also have to make sure if we can refer to vtable:
2245 // - Otherwise we can refer to vtable if it's safe to speculatively emit.
2246 // FIXME: If vtable is used by ctor/dtor, or if vtable is external and we are
2247 // sure that definition of vtable is not hidden,
2248 // then we are always safe to refer to it.
2249 // FIXME: It looks like InstCombine is very inefficient on dealing with
2250 // assumes. Make assumption loads require -fstrict-vtable-pointers temporarily.
2251 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2252 ClassDecl->isDynamicClass() && Type != Ctor_Base &&
2254 CGM.getCodeGenOpts().StrictVTablePointers)
2255 EmitVTableAssumptionLoads(ClassDecl, This);
2256}
2257
2259 const CXXConstructorDecl *D, bool ForVirtualBase, Address This,
2260 bool InheritedFromVBase, const CXXInheritedCtorInitExpr *E) {
2261 CallArgList Args;
2262 CallArg ThisArg(RValue::get(This.getPointer()), D->getThisType());
2263
2264 // Forward the parameters.
2265 if (InheritedFromVBase &&
2267 // Nothing to do; this construction is not responsible for constructing
2268 // the base class containing the inherited constructor.
2269 // FIXME: Can we just pass undef's for the remaining arguments if we don't
2270 // have constructor variants?
2271 Args.push_back(ThisArg);
2272 } else if (!CXXInheritedCtorInitExprArgs.empty()) {
2273 // The inheriting constructor was inlined; just inject its arguments.
2274 assert(CXXInheritedCtorInitExprArgs.size() >= D->getNumParams() &&
2275 "wrong number of parameters for inherited constructor call");
2276 Args = CXXInheritedCtorInitExprArgs;
2277 Args[0] = ThisArg;
2278 } else {
2279 // The inheriting constructor was not inlined. Emit delegating arguments.
2280 Args.push_back(ThisArg);
2281 const auto *OuterCtor = cast<CXXConstructorDecl>(CurCodeDecl);
2282 assert(OuterCtor->getNumParams() == D->getNumParams());
2283 assert(!OuterCtor->isVariadic() && "should have been inlined");
2284
2285 for (const auto *Param : OuterCtor->parameters()) {
2286 assert(getContext().hasSameUnqualifiedType(
2287 OuterCtor->getParamDecl(Param->getFunctionScopeIndex())->getType(),
2288 Param->getType()));
2289 EmitDelegateCallArg(Args, Param, E->getLocation());
2290
2291 // Forward __attribute__(pass_object_size).
2292 if (Param->hasAttr<PassObjectSizeAttr>()) {
2293 auto *POSParam = SizeArguments[Param];
2294 assert(POSParam && "missing pass_object_size value for forwarding");
2295 EmitDelegateCallArg(Args, POSParam, E->getLocation());
2296 }
2297 }
2298 }
2299
2300 EmitCXXConstructorCall(D, Ctor_Base, ForVirtualBase, /*Delegating*/false,
2301 This, Args, AggValueSlot::MayOverlap,
2302 E->getLocation(), /*NewPointerIsChecked*/true);
2303}
2304
2306 const CXXConstructorDecl *Ctor, CXXCtorType CtorType, bool ForVirtualBase,
2307 bool Delegating, CallArgList &Args) {
2308 GlobalDecl GD(Ctor, CtorType);
2309 InlinedInheritingConstructorScope Scope(*this, GD);
2310 ApplyInlineDebugLocation DebugScope(*this, GD);
2311 RunCleanupsScope RunCleanups(*this);
2312
2313 // Save the arguments to be passed to the inherited constructor.
2314 CXXInheritedCtorInitExprArgs = Args;
2315
2316 FunctionArgList Params;
2317 QualType RetType = BuildFunctionArgList(CurGD, Params);
2318 FnRetTy = RetType;
2319
2320 // Insert any ABI-specific implicit constructor arguments.
2321 CGM.getCXXABI().addImplicitConstructorArgs(*this, Ctor, CtorType,
2322 ForVirtualBase, Delegating, Args);
2323
2324 // Emit a simplified prolog. We only need to emit the implicit params.
2325 assert(Args.size() >= Params.size() && "too few arguments for call");
2326 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
2327 if (I < Params.size() && isa<ImplicitParamDecl>(Params[I])) {
2328 const RValue &RV = Args[I].getRValue(*this);
2329 assert(!RV.isComplex() && "complex indirect params not supported");
2330 ParamValue Val = RV.isScalar()
2332 : ParamValue::forIndirect(RV.getAggregateAddress());
2333 EmitParmDecl(*Params[I], Val, I + 1);
2334 }
2335 }
2336
2337 // Create a return value slot if the ABI implementation wants one.
2338 // FIXME: This is dumb, we should ask the ABI not to try to set the return
2339 // value instead.
2340 if (!RetType->isVoidType())
2341 ReturnValue = CreateIRTemp(RetType, "retval.inhctor");
2342
2344 CXXThisValue = CXXABIThisValue;
2345
2346 // Directly emit the constructor initializers.
2347 EmitCtorPrologue(Ctor, CtorType, Params);
2348}
2349
2350void CodeGenFunction::EmitVTableAssumptionLoad(const VPtr &Vptr, Address This) {
2351 llvm::Value *VTableGlobal =
2352 CGM.getCXXABI().getVTableAddressPoint(Vptr.Base, Vptr.VTableClass);
2353 if (!VTableGlobal)
2354 return;
2355
2356 // We can just use the base offset in the complete class.
2357 CharUnits NonVirtualOffset = Vptr.Base.getBaseOffset();
2358
2359 if (!NonVirtualOffset.isZero())
2360 This =
2361 ApplyNonVirtualAndVirtualOffset(*this, This, NonVirtualOffset, nullptr,
2362 Vptr.VTableClass, Vptr.NearestVBase);
2363
2364 llvm::Value *VPtrValue =
2365 GetVTablePtr(This, VTableGlobal->getType(), Vptr.VTableClass);
2366 llvm::Value *Cmp =
2367 Builder.CreateICmpEQ(VPtrValue, VTableGlobal, "cmp.vtables");
2368 Builder.CreateAssumption(Cmp);
2369}
2370
2372 Address This) {
2373 if (CGM.getCXXABI().doStructorsInitializeVPtrs(ClassDecl))
2374 for (const VPtr &Vptr : getVTablePointers(ClassDecl))
2375 EmitVTableAssumptionLoad(Vptr, This);
2376}
2377
2378void
2380 Address This, Address Src,
2381 const CXXConstructExpr *E) {
2382 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
2383
2384 CallArgList Args;
2385
2386 // Push the this ptr.
2387 Args.add(RValue::get(This.getPointer()), D->getThisType());
2388
2389 // Push the src ptr.
2390 QualType QT = *(FPT->param_type_begin());
2391 llvm::Type *t = CGM.getTypes().ConvertType(QT);
2392 llvm::Value *SrcVal = Builder.CreateBitCast(Src.getPointer(), t);
2393 Args.add(RValue::get(SrcVal), QT);
2394
2395 // Skip over first argument (Src).
2396 EmitCallArgs(Args, FPT, drop_begin(E->arguments(), 1), E->getConstructor(),
2397 /*ParamsToSkip*/ 1);
2398
2399 EmitCXXConstructorCall(D, Ctor_Complete, /*ForVirtualBase*/false,
2400 /*Delegating*/false, This, Args,
2402 /*NewPointerIsChecked*/false);
2403}
2404
2405void
2407 CXXCtorType CtorType,
2408 const FunctionArgList &Args,
2409 SourceLocation Loc) {
2410 CallArgList DelegateArgs;
2411
2412 FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
2413 assert(I != E && "no parameters to constructor");
2414
2415 // this
2417 DelegateArgs.add(RValue::get(This.getPointer()), (*I)->getType());
2418 ++I;
2419
2420 // FIXME: The location of the VTT parameter in the parameter list is
2421 // specific to the Itanium ABI and shouldn't be hardcoded here.
2423 assert(I != E && "cannot skip vtt parameter, already done with args");
2424 assert((*I)->getType()->isPointerType() &&
2425 "skipping parameter not of vtt type");
2426 ++I;
2427 }
2428
2429 // Explicit arguments.
2430 for (; I != E; ++I) {
2431 const VarDecl *param = *I;
2432 // FIXME: per-argument source location
2433 EmitDelegateCallArg(DelegateArgs, param, Loc);
2434 }
2435
2436 EmitCXXConstructorCall(Ctor, CtorType, /*ForVirtualBase=*/false,
2437 /*Delegating=*/true, This, DelegateArgs,
2439 /*NewPointerIsChecked=*/true);
2440}
2441
2442namespace {
2443 struct CallDelegatingCtorDtor final : EHScopeStack::Cleanup {
2444 const CXXDestructorDecl *Dtor;
2445 Address Addr;
2447
2448 CallDelegatingCtorDtor(const CXXDestructorDecl *D, Address Addr,
2450 : Dtor(D), Addr(Addr), Type(Type) {}
2451
2452 void Emit(CodeGenFunction &CGF, Flags flags) override {
2453 // We are calling the destructor from within the constructor.
2454 // Therefore, "this" should have the expected type.
2455 QualType ThisTy = Dtor->getFunctionObjectParameterType();
2456 CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false,
2457 /*Delegating=*/true, Addr, ThisTy);
2458 }
2459 };
2460} // end anonymous namespace
2461
2462void
2464 const FunctionArgList &Args) {
2465 assert(Ctor->isDelegatingConstructor());
2466
2467 Address ThisPtr = LoadCXXThisAddress();
2468
2469 AggValueSlot AggSlot =
2476 // Checks are made by the code that calls constructor.
2478
2479 EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot);
2480
2481 const CXXRecordDecl *ClassDecl = Ctor->getParent();
2482 if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) {
2485
2486 EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup,
2487 ClassDecl->getDestructor(),
2488 ThisPtr, Type);
2489 }
2490}
2491
2494 bool ForVirtualBase,
2495 bool Delegating, Address This,
2496 QualType ThisTy) {
2497 CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase,
2498 Delegating, This, ThisTy);
2499}
2500
2501namespace {
2502 struct CallLocalDtor final : EHScopeStack::Cleanup {
2503 const CXXDestructorDecl *Dtor;
2504 Address Addr;
2505 QualType Ty;
2506
2507 CallLocalDtor(const CXXDestructorDecl *D, Address Addr, QualType Ty)
2508 : Dtor(D), Addr(Addr), Ty(Ty) {}
2509
2510 void Emit(CodeGenFunction &CGF, Flags flags) override {
2512 /*ForVirtualBase=*/false,
2513 /*Delegating=*/false, Addr, Ty);
2514 }
2515 };
2516} // end anonymous namespace
2517
2519 QualType T, Address Addr) {
2520 EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr, T);
2521}
2522
2524 CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl();
2525 if (!ClassDecl) return;
2526 if (ClassDecl->hasTrivialDestructor()) return;
2527
2528 const CXXDestructorDecl *D = ClassDecl->getDestructor();
2529 assert(D && D->isUsed() && "destructor not marked as used!");
2530 PushDestructorCleanup(D, T, Addr);
2531}
2532
2533void CodeGenFunction::InitializeVTablePointer(const VPtr &Vptr) {
2534 // Compute the address point.
2535 llvm::Value *VTableAddressPoint =
2537 *this, Vptr.VTableClass, Vptr.Base, Vptr.NearestVBase);
2538
2539 if (!VTableAddressPoint)
2540 return;
2541
2542 // Compute where to store the address point.
2543 llvm::Value *VirtualOffset = nullptr;
2544 CharUnits NonVirtualOffset = CharUnits::Zero();
2545
2547 // We need to use the virtual base offset offset because the virtual base
2548 // might have a different offset in the most derived class.
2549
2550 VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(
2551 *this, LoadCXXThisAddress(), Vptr.VTableClass, Vptr.NearestVBase);
2552 NonVirtualOffset = Vptr.OffsetFromNearestVBase;
2553 } else {
2554 // We can just use the base offset in the complete class.
2555 NonVirtualOffset = Vptr.Base.getBaseOffset();
2556 }
2557
2558 // Apply the offsets.
2559 Address VTableField = LoadCXXThisAddress();
2560 if (!NonVirtualOffset.isZero() || VirtualOffset)
2561 VTableField = ApplyNonVirtualAndVirtualOffset(
2562 *this, VTableField, NonVirtualOffset, VirtualOffset, Vptr.VTableClass,
2563 Vptr.NearestVBase);
2564
2565 // Finally, store the address point. Use the same LLVM types as the field to
2566 // support optimization.
2567 unsigned GlobalsAS = CGM.getDataLayout().getDefaultGlobalsAddressSpace();
2568 llvm::Type *PtrTy = llvm::PointerType::get(CGM.getLLVMContext(), GlobalsAS);
2569 // vtable field is derived from `this` pointer, therefore they should be in
2570 // the same addr space. Note that this might not be LLVM address space 0.
2571 VTableField = VTableField.withElementType(PtrTy);
2572
2573 llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField);
2575 CGM.DecorateInstructionWithTBAA(Store, TBAAInfo);
2576 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2577 CGM.getCodeGenOpts().StrictVTablePointers)
2578 CGM.DecorateInstructionWithInvariantGroup(Store, Vptr.VTableClass);
2579}
2580
2583 CodeGenFunction::VPtrsVector VPtrsResult;
2586 /*NearestVBase=*/nullptr,
2587 /*OffsetFromNearestVBase=*/CharUnits::Zero(),
2588 /*BaseIsNonVirtualPrimaryBase=*/false, VTableClass, VBases,
2589 VPtrsResult);
2590 return VPtrsResult;
2591}
2592
2594 const CXXRecordDecl *NearestVBase,
2595 CharUnits OffsetFromNearestVBase,
2596 bool BaseIsNonVirtualPrimaryBase,
2597 const CXXRecordDecl *VTableClass,
2598 VisitedVirtualBasesSetTy &VBases,
2599 VPtrsVector &Vptrs) {
2600 // If this base is a non-virtual primary base the address point has already
2601 // been set.
2602 if (!BaseIsNonVirtualPrimaryBase) {
2603 // Initialize the vtable pointer for this base.
2604 VPtr Vptr = {Base, NearestVBase, OffsetFromNearestVBase, VTableClass};
2605 Vptrs.push_back(Vptr);
2606 }
2607
2608 const CXXRecordDecl *RD = Base.getBase();
2609
2610 // Traverse bases.
2611 for (const auto &I : RD->bases()) {
2612 auto *BaseDecl =
2613 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2614
2615 // Ignore classes without a vtable.
2616 if (!BaseDecl->isDynamicClass())
2617 continue;
2618
2619 CharUnits BaseOffset;
2620 CharUnits BaseOffsetFromNearestVBase;
2621 bool BaseDeclIsNonVirtualPrimaryBase;
2622
2623 if (I.isVirtual()) {
2624 // Check if we've visited this virtual base before.
2625 if (!VBases.insert(BaseDecl).second)
2626 continue;
2627
2628 const ASTRecordLayout &Layout =
2629 getContext().getASTRecordLayout(VTableClass);
2630
2631 BaseOffset = Layout.getVBaseClassOffset(BaseDecl);
2632 BaseOffsetFromNearestVBase = CharUnits::Zero();
2633 BaseDeclIsNonVirtualPrimaryBase = false;
2634 } else {
2635 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2636
2637 BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl);
2638 BaseOffsetFromNearestVBase =
2639 OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl);
2640 BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl;
2641 }
2642
2644 BaseSubobject(BaseDecl, BaseOffset),
2645 I.isVirtual() ? BaseDecl : NearestVBase, BaseOffsetFromNearestVBase,
2646 BaseDeclIsNonVirtualPrimaryBase, VTableClass, VBases, Vptrs);
2647 }
2648}
2649
2651 // Ignore classes without a vtable.
2652 if (!RD->isDynamicClass())
2653 return;
2654
2655 // Initialize the vtable pointers for this class and all of its bases.
2657 for (const VPtr &Vptr : getVTablePointers(RD))
2659
2660 if (RD->getNumVBases())
2662}
2663
2664llvm::Value *CodeGenFunction::GetVTablePtr(Address This,
2665 llvm::Type *VTableTy,
2666 const CXXRecordDecl *RD) {
2667 Address VTablePtrSrc = This.withElementType(VTableTy);
2668 llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable");
2669 TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTableTy);
2670 CGM.DecorateInstructionWithTBAA(VTable, TBAAInfo);
2671
2672 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2673 CGM.getCodeGenOpts().StrictVTablePointers)
2675
2676 return VTable;
2677}
2678
2679// If a class has a single non-virtual base and does not introduce or override
2680// virtual member functions or fields, it will have the same layout as its base.
2681// This function returns the least derived such class.
2682//
2683// Casting an instance of a base class to such a derived class is technically
2684// undefined behavior, but it is a relatively common hack for introducing member
2685// functions on class instances with specific properties (e.g. llvm::Operator)
2686// that works under most compilers and should not have security implications, so
2687// we allow it by default. It can be disabled with -fsanitize=cfi-cast-strict.
2688static const CXXRecordDecl *
2690 if (!RD->field_empty())
2691 return RD;
2692
2693 if (RD->getNumVBases() != 0)
2694 return RD;
2695
2696 if (RD->getNumBases() != 1)
2697 return RD;
2698
2699 for (const CXXMethodDecl *MD : RD->methods()) {
2700 if (MD->isVirtual()) {
2701 // Virtual member functions are only ok if they are implicit destructors
2702 // because the implicit destructor will have the same semantics as the
2703 // base class's destructor if no fields are added.
2704 if (isa<CXXDestructorDecl>(MD) && MD->isImplicit())
2705 continue;
2706 return RD;
2707 }
2708 }
2709
2712}
2713
2715 llvm::Value *VTable,
2716 SourceLocation Loc) {
2717 if (SanOpts.has(SanitizerKind::CFIVCall))
2719 else if (CGM.getCodeGenOpts().WholeProgramVTables &&
2720 // Don't insert type test assumes if we are forcing public
2721 // visibility.
2723 QualType Ty = QualType(RD->getTypeForDecl(), 0);
2724 llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(Ty);
2725 llvm::Value *TypeId =
2726 llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD);
2727
2728 // If we already know that the call has hidden LTO visibility, emit
2729 // @llvm.type.test(). Otherwise emit @llvm.public.type.test(), which WPD
2730 // will convert to @llvm.type.test() if we assert at link time that we have
2731 // whole program visibility.
2732 llvm::Intrinsic::ID IID = CGM.HasHiddenLTOVisibility(RD)
2733 ? llvm::Intrinsic::type_test
2734 : llvm::Intrinsic::public_type_test;
2735 llvm::Value *TypeTest =
2736 Builder.CreateCall(CGM.getIntrinsic(IID), {VTable, TypeId});
2737 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::assume), TypeTest);
2738 }
2739}
2740
2742 llvm::Value *VTable,
2743 CFITypeCheckKind TCK,
2744 SourceLocation Loc) {
2745 if (!SanOpts.has(SanitizerKind::CFICastStrict))
2747
2748 EmitVTablePtrCheck(RD, VTable, TCK, Loc);
2749}
2750
2752 bool MayBeNull,
2753 CFITypeCheckKind TCK,
2754 SourceLocation Loc) {
2755 if (!getLangOpts().CPlusPlus)
2756 return;
2757
2758 auto *ClassTy = T->getAs<RecordType>();
2759 if (!ClassTy)
2760 return;
2761
2762 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassTy->getDecl());
2763
2764 if (!ClassDecl->isCompleteDefinition() || !ClassDecl->isDynamicClass())
2765 return;
2766
2767 if (!SanOpts.has(SanitizerKind::CFICastStrict))
2768 ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl);
2769
2770 llvm::BasicBlock *ContBlock = nullptr;
2771
2772 if (MayBeNull) {
2773 llvm::Value *DerivedNotNull =
2774 Builder.CreateIsNotNull(Derived.getPointer(), "cast.nonnull");
2775
2776 llvm::BasicBlock *CheckBlock = createBasicBlock("cast.check");
2777 ContBlock = createBasicBlock("cast.cont");
2778
2779 Builder.CreateCondBr(DerivedNotNull, CheckBlock, ContBlock);
2780
2781 EmitBlock(CheckBlock);
2782 }
2783
2784 llvm::Value *VTable;
2785 std::tie(VTable, ClassDecl) =
2786 CGM.getCXXABI().LoadVTablePtr(*this, Derived, ClassDecl);
2787
2788 EmitVTablePtrCheck(ClassDecl, VTable, TCK, Loc);
2789
2790 if (MayBeNull) {
2791 Builder.CreateBr(ContBlock);
2792 EmitBlock(ContBlock);
2793 }
2794}
2795
2797 llvm::Value *VTable,
2798 CFITypeCheckKind TCK,
2799 SourceLocation Loc) {
2800 if (!CGM.getCodeGenOpts().SanitizeCfiCrossDso &&
2802 return;
2803
2804 SanitizerMask M;
2805 llvm::SanitizerStatKind SSK;
2806 switch (TCK) {
2807 case CFITCK_VCall:
2808 M = SanitizerKind::CFIVCall;
2809 SSK = llvm::SanStat_CFI_VCall;
2810 break;
2811 case CFITCK_NVCall:
2812 M = SanitizerKind::CFINVCall;
2813 SSK = llvm::SanStat_CFI_NVCall;
2814 break;
2815 case CFITCK_DerivedCast:
2816 M = SanitizerKind::CFIDerivedCast;
2817 SSK = llvm::SanStat_CFI_DerivedCast;
2818 break;
2820 M = SanitizerKind::CFIUnrelatedCast;
2821 SSK = llvm::SanStat_CFI_UnrelatedCast;
2822 break;
2823 case CFITCK_ICall:
2824 case CFITCK_NVMFCall:
2825 case CFITCK_VMFCall:
2826 llvm_unreachable("unexpected sanitizer kind");
2827 }
2828
2829 std::string TypeName = RD->getQualifiedNameAsString();
2830 if (getContext().getNoSanitizeList().containsType(M, TypeName))
2831 return;
2832
2833 SanitizerScope SanScope(this);
2835
2836 llvm::Metadata *MD =
2838 llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);
2839
2840 llvm::Value *TypeTest = Builder.CreateCall(
2841 CGM.getIntrinsic(llvm::Intrinsic::type_test), {VTable, TypeId});
2842
2843 llvm::Constant *StaticData[] = {
2844 llvm::ConstantInt::get(Int8Ty, TCK),
2847 };
2848
2849 auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
2850 if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
2851 EmitCfiSlowPathCheck(M, TypeTest, CrossDsoTypeId, VTable, StaticData);
2852 return;
2853 }
2854
2856 EmitTrapCheck(TypeTest, SanitizerHandler::CFICheckFail);
2857 return;
2858 }
2859
2860 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
2862 llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
2863 llvm::Value *ValidVtable = Builder.CreateCall(
2864 CGM.getIntrinsic(llvm::Intrinsic::type_test), {VTable, AllVtables});
2865 EmitCheck(std::make_pair(TypeTest, M), SanitizerHandler::CFICheckFail,
2866 StaticData, {VTable, ValidVtable});
2867}
2868
2870 if (!CGM.getCodeGenOpts().WholeProgramVTables ||
2872 return false;
2873
2874 if (CGM.getCodeGenOpts().VirtualFunctionElimination)
2875 return true;
2876
2877 if (!SanOpts.has(SanitizerKind::CFIVCall) ||
2878 !CGM.getCodeGenOpts().SanitizeTrap.has(SanitizerKind::CFIVCall))
2879 return false;
2880
2881 std::string TypeName = RD->getQualifiedNameAsString();
2882 return !getContext().getNoSanitizeList().containsType(SanitizerKind::CFIVCall,
2883 TypeName);
2884}
2885
2887 const CXXRecordDecl *RD, llvm::Value *VTable, llvm::Type *VTableTy,
2888 uint64_t VTableByteOffset) {
2889 SanitizerScope SanScope(this);
2890
2891 EmitSanitizerStatReport(llvm::SanStat_CFI_VCall);
2892
2893 llvm::Metadata *MD =
2895 llvm::Value *TypeId = llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD);
2896
2897 llvm::Value *CheckedLoad = Builder.CreateCall(
2898 CGM.getIntrinsic(llvm::Intrinsic::type_checked_load),
2899 {VTable, llvm::ConstantInt::get(Int32Ty, VTableByteOffset), TypeId});
2900 llvm::Value *CheckResult = Builder.CreateExtractValue(CheckedLoad, 1);
2901
2902 std::string TypeName = RD->getQualifiedNameAsString();
2903 if (SanOpts.has(SanitizerKind::CFIVCall) &&
2904 !getContext().getNoSanitizeList().containsType(SanitizerKind::CFIVCall,
2905 TypeName)) {
2906 EmitCheck(std::make_pair(CheckResult, SanitizerKind::CFIVCall),
2907 SanitizerHandler::CFICheckFail, {}, {});
2908 }
2909
2910 return Builder.CreateBitCast(Builder.CreateExtractValue(CheckedLoad, 0),
2911 VTableTy);
2912}
2913
2915 const CXXMethodDecl *callOperator, CallArgList &callArgs,
2916 const CGFunctionInfo *calleeFnInfo, llvm::Constant *calleePtr) {
2917 // Get the address of the call operator.
2918 if (!calleeFnInfo)
2919 calleeFnInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(callOperator);
2920
2921 if (!calleePtr)
2922 calleePtr =
2923 CGM.GetAddrOfFunction(GlobalDecl(callOperator),
2924 CGM.getTypes().GetFunctionType(*calleeFnInfo));
2925
2926 // Prepare the return slot.
2927 const FunctionProtoType *FPT =
2928 callOperator->getType()->castAs<FunctionProtoType>();
2929 QualType resultType = FPT->getReturnType();
2930 ReturnValueSlot returnSlot;
2931 if (!resultType->isVoidType() &&
2932 calleeFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
2933 !hasScalarEvaluationKind(calleeFnInfo->getReturnType()))
2934 returnSlot =
2935 ReturnValueSlot(ReturnValue, resultType.isVolatileQualified(),
2936 /*IsUnused=*/false, /*IsExternallyDestructed=*/true);
2937
2938 // We don't need to separately arrange the call arguments because
2939 // the call can't be variadic anyway --- it's impossible to forward
2940 // variadic arguments.
2941
2942 // Now emit our call.
2943 auto callee = CGCallee::forDirect(calleePtr, GlobalDecl(callOperator));
2944 RValue RV = EmitCall(*calleeFnInfo, callee, returnSlot, callArgs);
2945
2946 // If necessary, copy the returned value into the slot.
2947 if (!resultType->isVoidType() && returnSlot.isNull()) {
2948 if (getLangOpts().ObjCAutoRefCount && resultType->isObjCRetainableType()) {
2950 }
2951 EmitReturnOfRValue(RV, resultType);
2952 } else
2954}
2955
2957 const BlockDecl *BD = BlockInfo->getBlockDecl();
2958 const VarDecl *variable = BD->capture_begin()->getVariable();
2959 const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl();
2960 const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
2961
2962 if (CallOp->isVariadic()) {
2963 // FIXME: Making this work correctly is nasty because it requires either
2964 // cloning the body of the call operator or making the call operator
2965 // forward.
2966 CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function");
2967 return;
2968 }
2969
2970 // Start building arguments for forwarding call
2971 CallArgList CallArgs;
2972
2974 Address ThisPtr = GetAddrOfBlockDecl(variable);
2975 CallArgs.add(RValue::get(ThisPtr.getPointer()), ThisType);
2976
2977 // Add the rest of the parameters.
2978 for (auto *param : BD->parameters())
2979 EmitDelegateCallArg(CallArgs, param, param->getBeginLoc());
2980
2981 assert(!Lambda->isGenericLambda() &&
2982 "generic lambda interconversion to block not implemented");
2983 EmitForwardingCallToLambda(CallOp, CallArgs);
2984}
2985
2987 if (MD->isVariadic()) {
2988 // FIXME: Making this work correctly is nasty because it requires either
2989 // cloning the body of the call operator or making the call operator
2990 // forward.
2991 CGM.ErrorUnsupported(MD, "lambda conversion to variadic function");
2992 return;
2993 }
2994
2995 const CXXRecordDecl *Lambda = MD->getParent();
2996
2997 // Start building arguments for forwarding call
2998 CallArgList CallArgs;
2999
3000 QualType LambdaType = getContext().getRecordType(Lambda);
3001 QualType ThisType = getContext().getPointerType(LambdaType);
3002 Address ThisPtr = CreateMemTemp(LambdaType, "unused.capture");
3003 CallArgs.add(RValue::get(ThisPtr.getPointer()), ThisType);
3004
3005 EmitLambdaDelegatingInvokeBody(MD, CallArgs);
3006}
3007
3009 CallArgList &CallArgs) {
3010 // Add the rest of the forwarded parameters.
3011 for (auto *Param : MD->parameters())
3012 EmitDelegateCallArg(CallArgs, Param, Param->getBeginLoc());
3013
3014 const CXXRecordDecl *Lambda = MD->getParent();
3015 const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
3016 // For a generic lambda, find the corresponding call operator specialization
3017 // to which the call to the static-invoker shall be forwarded.
3018 if (Lambda->isGenericLambda()) {
3021 FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate();
3022 void *InsertPos = nullptr;
3023 FunctionDecl *CorrespondingCallOpSpecialization =
3024 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
3025 assert(CorrespondingCallOpSpecialization);
3026 CallOp = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization);
3027 }
3028
3029 // Special lambda forwarding when there are inalloca parameters.
3030 if (hasInAllocaArg(MD)) {
3031 const CGFunctionInfo *ImplFnInfo = nullptr;
3032 llvm::Function *ImplFn = nullptr;
3033 EmitLambdaInAllocaImplFn(CallOp, &ImplFnInfo, &ImplFn);
3034
3035 EmitForwardingCallToLambda(CallOp, CallArgs, ImplFnInfo, ImplFn);
3036 return;
3037 }
3038
3039 EmitForwardingCallToLambda(CallOp, CallArgs);
3040}
3041
3043 if (MD->isVariadic()) {
3044 // FIXME: Making this work correctly is nasty because it requires either
3045 // cloning the body of the call operator or making the call operator forward.
3046 CGM.ErrorUnsupported(MD, "lambda conversion to variadic function");
3047 return;
3048 }
3049
3050 // Forward %this argument.
3051 CallArgList CallArgs;
3052 QualType LambdaType = getContext().getRecordType(MD->getParent());
3053 QualType ThisType = getContext().getPointerType(LambdaType);
3054 llvm::Value *ThisArg = CurFn->getArg(0);
3055 CallArgs.add(RValue::get(ThisArg), ThisType);
3056
3057 EmitLambdaDelegatingInvokeBody(MD, CallArgs);
3058}
3059
3061 const CXXMethodDecl *CallOp, const CGFunctionInfo **ImplFnInfo,
3062 llvm::Function **ImplFn) {
3063 const CGFunctionInfo &FnInfo =
3065 llvm::Function *CallOpFn =
3066 cast<llvm::Function>(CGM.GetAddrOfFunction(GlobalDecl(CallOp)));
3067
3068 // Emit function containing the original call op body. __invoke will delegate
3069 // to this function.
3071 for (auto I = FnInfo.arg_begin(); I != FnInfo.arg_end(); ++I)
3072 ArgTypes.push_back(I->type);
3073 *ImplFnInfo = &CGM.getTypes().arrangeLLVMFunctionInfo(
3074 FnInfo.getReturnType(), FnInfoOpts::IsDelegateCall, ArgTypes,
3075 FnInfo.getExtInfo(), {}, FnInfo.getRequiredArgs());
3076
3077 // Create mangled name as if this was a method named __impl. If for some
3078 // reason the name doesn't look as expected then just tack __impl to the
3079 // front.
3080 // TODO: Use the name mangler to produce the right name instead of using
3081 // string replacement.
3082 StringRef CallOpName = CallOpFn->getName();
3083 std::string ImplName;
3084 if (size_t Pos = CallOpName.find_first_of("<lambda"))
3085 ImplName = ("?__impl@" + CallOpName.drop_front(Pos)).str();
3086 else
3087 ImplName = ("__impl" + CallOpName).str();
3088
3089 llvm::Function *Fn = CallOpFn->getParent()->getFunction(ImplName);
3090 if (!Fn) {
3091 Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(**ImplFnInfo),
3092 llvm::GlobalValue::InternalLinkage, ImplName,
3093 CGM.getModule());
3094 CGM.SetInternalFunctionAttributes(CallOp, Fn, **ImplFnInfo);
3095
3096 const GlobalDecl &GD = GlobalDecl(CallOp);
3097 const auto *D = cast<FunctionDecl>(GD.getDecl());
3098 CodeGenFunction(CGM).GenerateCode(GD, Fn, **ImplFnInfo);
3100 }
3101 *ImplFn = Fn;
3102}
#define V(N, I)
Definition: ASTContext.h:3241
StringRef P
static Address ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, Address addr, CharUnits nonVirtualOffset, llvm::Value *virtualOffset, const CXXRecordDecl *derivedClass, const CXXRecordDecl *nearestVBase)
Definition: CGClass.cpp:247
static bool canEmitDelegateCallArgs(CodeGenFunction &CGF, const CXXConstructorDecl *Ctor, CXXCtorType Type, CallArgList &Args)
Definition: CGClass.cpp:2157
static bool CanSkipVTablePointerInitialization(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor)
CanSkipVTablePointerInitialization - Check whether we need to initialize any vtable pointers before c...
Definition: CGClass.cpp:1405
static bool isInitializerOfDynamicClass(const CXXCtorInitializer *BaseInit)
Definition: CGClass.cpp:1253
static const CXXRecordDecl * LeastDerivedClassWithSameLayout(const CXXRecordDecl *RD)
Definition: CGClass.cpp:2689
static void EmitBaseInitializer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, CXXCtorInitializer *BaseInit)
Definition: CGClass.cpp:548
static bool isMemcpyEquivalentSpecialMember(const CXXMethodDecl *D)
Definition: CGClass.cpp:590
static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init)
Definition: CGClass.cpp:542
static bool FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field)
Definition: CGClass.cpp:1385
static bool HasTrivialDestructorBody(ASTContext &Context, const CXXRecordDecl *BaseClassDecl, const CXXRecordDecl *MostDerivedClassDecl)
Definition: CGClass.cpp:1342
static void EmitMemberInitializer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, CXXCtorInitializer *MemberInit, const CXXConstructorDecl *Constructor, FunctionArgList &Args)
Definition: CGClass.cpp:621
static void EmitLValueForAnyFieldInitialization(CodeGenFunction &CGF, CXXCtorInitializer *MemberInit, LValue &LHS)
Definition: CGClass.cpp:607
Defines the C++ template declaration subclasses.
static const RecordType * getRecordType(QualType QT)
Checks that the passed in QualType either is of RecordType or points to RecordType.
Enumerates target-specific builtins in their own namespaces within namespace clang.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
const ConstantArrayType * getAsConstantArrayType(QualType T) const
Definition: ASTContext.h:2728
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl.
QualType getRecordType(const RecordDecl *Decl) const
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2535
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl=nullptr) const
Return the unique reference to the type for the specified type declaration.
Definition: ASTContext.h:1572
const LangOptions & getLangOpts() const
Definition: ASTContext.h:767
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
const NoSanitizeList & getNoSanitizeList() const
Definition: ASTContext.h:777
TypeInfoChars getTypeInfoDataSizeInChars(QualType T) const
TypeInfoChars getTypeInfoInChars(const Type *T) const
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
unsigned getTargetAddressSpace(LangAS AS) const
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Definition: ASTContext.h:2311
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
CharUnits getAlignment() const
getAlignment - Get the record alignment in characters.
Definition: RecordLayout.h:182
CharUnits getSize() const
getSize - Get the record size in characters.
Definition: RecordLayout.h:193
unsigned getFieldCount() const
getFieldCount - Get the number of fields in the layout.
Definition: RecordLayout.h:196
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
Definition: RecordLayout.h:200
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:249
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:259
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
Definition: RecordLayout.h:234
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
Definition: RecordLayout.h:210
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:3145
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3862
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4463
capture_const_iterator capture_begin() const
Definition: Decl.h:4592
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:4549
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:249
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1530
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition: ExprCXX.h:1673
arg_range arguments()
Definition: ExprCXX.h:1654
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition: ExprCXX.h:1593
bool isListInitialization() const
Whether this constructor call was written as list-initialization.
Definition: ExprCXX.h:1612
unsigned getNumArgs() const
Return the number of arguments to the constructor call.
Definition: ExprCXX.h:1670
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2528
init_iterator init_end()
Retrieve an iterator past the last initializer.
Definition: DeclCXX.h:2633
init_iterator init_begin()
Retrieve an iterator to the first initializer.
Definition: DeclCXX.h:2624
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition: DeclCXX.cpp:2700
bool isDelegatingConstructor() const
Determine whether this constructor is a delegating constructor.
Definition: DeclCXX.h:2680
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition: DeclCXX.cpp:2720
InheritedConstructor getInheritedConstructor() const
Get the constructor that this inheriting constructor is based on.
Definition: DeclCXX.h:2765
CXXCtorInitializer *const * init_const_iterator
Iterates through the member/base initializer list.
Definition: DeclCXX.h:2613
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2293
FieldDecl * getMember() const
If this is a member initializer, returns the declaration of the non-static data member being initiali...
Definition: DeclCXX.h:2433
Expr * getInit() const
Get the initializer.
Definition: DeclCXX.h:2495
SourceLocation getSourceLocation() const
Determine the source location of the initializer.
Definition: DeclCXX.cpp:2606
bool isAnyMemberInitializer() const
Definition: DeclCXX.h:2373
bool isBaseInitializer() const
Determine whether this initializer is initializing a base class.
Definition: DeclCXX.h:2365
bool isIndirectMemberInitializer() const
Definition: DeclCXX.h:2377
const Type * getBaseClass() const
If this is a base class initializer, returns the type of the base class.
Definition: DeclCXX.cpp:2599
FieldDecl * getAnyMember() const
Definition: DeclCXX.h:2439
IndirectFieldDecl * getIndirectMember() const
Definition: DeclCXX.h:2447
bool isBaseVirtual() const
Returns whether the base is virtual or not.
Definition: DeclCXX.h:2419
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2792
const FunctionDecl * getOperatorDelete() const
Definition: DeclCXX.h:2825
Expr * getOperatorDeleteThisArg() const
Definition: DeclCXX.h:2829
Represents a call to an inherited base class constructor from an inheriting constructor.
Definition: ExprCXX.h:1721
SourceLocation getLocation() const LLVM_READONLY
Definition: ExprCXX.h:1774
Represents a call to a member function that may be written either with member call syntax (e....
Definition: ExprCXX.h:176
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2053
bool isVirtual() const
Definition: DeclCXX.h:2108
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition: DeclCXX.h:2179
QualType getThisType() const
Return the type of the this pointer.
Definition: DeclCXX.cpp:2512
bool isMoveAssignmentOperator() const
Determine whether this is a move assignment operator.
Definition: DeclCXX.cpp:2446
QualType getFunctionObjectParameterType() const
Definition: DeclCXX.h:2203
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition: DeclCXX.cpp:2424
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
bool isEffectivelyFinal() const
Determine whether it's impossible for a class to be derived from this class.
Definition: DeclCXX.cpp:2074
bool isGenericLambda() const
Determine whether this class describes a generic lambda function object (i.e.
Definition: DeclCXX.cpp:1522
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1366
base_class_range bases()
Definition: DeclCXX.h:618
method_range methods() const
Definition: DeclCXX.h:660
bool isPolymorphic() const
Whether this class is polymorphic (C++ [class.virtual]), which means that the class contains or inher...
Definition: DeclCXX.h:1214
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:612
base_class_iterator bases_begin()
Definition: DeclCXX.h:625
base_class_range vbases()
Definition: DeclCXX.h:635
bool isAbstract() const
Determine whether this class has a pure virtual function.
Definition: DeclCXX.h:1221
bool isDynamicClass() const
Definition: DeclCXX.h:584
bool hasDefinition() const
Definition: DeclCXX.h:571
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition: DeclCXX.h:1189
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1934
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
Definition: DeclCXX.cpp:1553
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:633
Represents the this expression in C++.
Definition: ExprCXX.h:1148
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2847
const CXXBaseSpecifier *const * path_const_iterator
Definition: Expr.h:3584
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset?
Definition: CharUnits.h:207
int64_t QuantityType
Definition: CharUnits.h:40
bool isPositive() const
isPositive - Test whether the quantity is greater than zero.
Definition: CharUnits.h:128
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:122
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition: CharUnits.h:214
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
SanitizerSet SanitizeTrap
Set of sanitizer checks that trap rather than diagnose.
@ Indirect
Indirect - Pass the argument indirectly via a hidden pointer with the specified alignment (0 indicate...
An aligned address.
Definition: Address.h:29
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:78
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:62
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition: Address.h:100
llvm::Value * getPointer() const
Definition: Address.h:51
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:57
An aggregate value slot.
Definition: CGValue.h:512
bool isSanitizerChecked() const
Definition: CGValue.h:670
Address getAddress() const
Definition: CGValue.h:650
static AggValueSlot forLValue(const LValue &LV, CodeGenFunction &CGF, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
Definition: CGValue.h:610
Qualifiers getQualifiers() const
Definition: CGValue.h:625
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:595
Overlap_t mayOverlap() const
Definition: CGValue.h:666
A scoped helper to set the current debug location to the specified location or preferred location of ...
Definition: CGDebugInfo.h:829
A scoped helper to set the current debug location to an inlined location.
Definition: CGDebugInfo.h:892
const BlockDecl * getBlockDecl() const
Definition: CGBlocks.h:304
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:97
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:259
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:71
Address CreateLaunderInvariantGroup(Address Addr)
Definition: CGBuilder.h:356
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:297
Address CreateGEP(Address Addr, llvm::Value *Index, const llvm::Twine &Name="")
Definition: CGBuilder.h:246
virtual llvm::Value * getVTableAddressPointInStructor(CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base, const CXXRecordDecl *NearestVBase)=0
Get the address point of the vtable for the given base subobject while building a constructor or a de...
virtual void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, const CXXRecordDecl *RD)
Emit the code to initialize hidden members required to handle virtual inheritance,...
Definition: CGCXXABI.h:324
virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *, FunctionArgList &Args) const =0
virtual bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr)=0
Checks if ABI requires extra virtual offset for vtable field.
virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF)=0
Emit the ABI-specific prolog for the function.
virtual bool NeedsVTTParameter(GlobalDecl GD)
Return whether the given global decl needs a VTT parameter.
Definition: CGCXXABI.cpp:318
virtual bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const =0
Determine whether it's possible to emit a vtable for RD, even though we do not know that the vtable h...
virtual llvm::Constant * getVTableAddressPoint(BaseSubobject Base, const CXXRecordDecl *VTableClass)=0
Get the address point of the vtable for the given base subobject.
virtual llvm::Value * EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr, const MemberPointerType *MPT)
Calculate an l-value from an object and a data member pointer.
Definition: CGCXXABI.cpp:55
virtual std::pair< llvm::Value *, const CXXRecordDecl * > LoadVTablePtr(CodeGenFunction &CGF, Address This, const CXXRecordDecl *RD)=0
Load a vtable from This, an object of polymorphic type RD, or from one of its virtual bases if it doe...
virtual llvm::BasicBlock * EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, const CXXRecordDecl *RD)
Definition: CGCXXABI.cpp:296
virtual bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass)=0
Checks if ABI requires to initialize vptrs for given dynamic class.
virtual llvm::Value * GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This, const CXXRecordDecl *ClassDecl, const CXXRecordDecl *BaseClassDecl)=0
virtual void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This, QualType ThisTy)=0
Emit the destructor call.
AddedStructorArgCounts addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase, bool Delegating, CallArgList &Args)
Add any ABI-specific implicit arguments needed to call a constructor.
Definition: CGCXXABI.cpp:337
All available information about a concrete callee.
Definition: CGCall.h:61
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:128
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:55
llvm::MDNode * getInlinedAt() const
Definition: CGDebugInfo.h:447
void setInlinedAt(llvm::MDNode *InlinedAt)
Update the current inline scope.
Definition: CGDebugInfo.h:444
CGFunctionInfo - Class to encapsulate the information about a function definition.
bool usesInAlloca() const
Return true if this function uses inalloca arguments.
FunctionType::ExtInfo getExtInfo() const
const_arg_iterator arg_begin() const
CanQualType getReturnType() const
const_arg_iterator arg_end() const
RequiredArgs getRequiredArgs() const
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
const CGBitFieldInfo & getBitFieldInfo(const FieldDecl *FD) const
Return the BitFieldInfo that corresponds to the field FD.
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:257
void add(RValue rvalue, QualType type)
Definition: CGCall.h:281
static ParamValue forDirect(llvm::Value *value)
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, Address This, Address Src, const CXXConstructExpr *E)
void EmitDestructorBody(FunctionArgList &Args)
void EmitNullInitialization(Address DestPtr, QualType Ty)
EmitNullInitialization - Generate code to set a value of the given type to null, If the type contains...
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V, QualType Type, CharUnits Alignment=CharUnits::Zero(), SanitizerSet SkippedChecks=SanitizerSet(), llvm::Value *ArraySize=nullptr)
Emit a check that V is the address of storage of the appropriate size and alignment for an object of ...
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base, llvm::Value *memberPtr, const MemberPointerType *memberPtrType, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
void EmitAsanPrologueOrEpilogue(bool Prologue)
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
void EmitLambdaInAllocaImplFn(const CXXMethodDecl *CallOp, const CGFunctionInfo **ImplFnInfo, llvm::Function **ImplFn)
llvm::CallInst * EmitTrapCall(llvm::Intrinsic::ID IntrID)
Emit a call to trap or debugtrap and attach function attribute "trap-func-name" if specified.
bool sanitizePerformTypeCheck() const
Whether any type-checking sanitizers are enabled.
void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, CXXCtorType CtorType, const FunctionArgList &Args, SourceLocation Loc)
void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK)
void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, const FunctionArgList &Args)
SanitizerSet SanOpts
Sanitizers enabled for this function.
void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator, CallArgList &CallArgs, const CGFunctionInfo *CallOpFnInfo=nullptr, llvm::Constant *CallOpFn=nullptr)
void EmitVTableAssumptionLoad(const VPtr &vptr, Address This)
Emit assumption that vptr load == global vtable.
void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD)
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
static bool hasScalarEvaluationKind(QualType T)
void EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable, CFITypeCheckKind TCK, SourceLocation Loc)
EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
llvm::Value * EmitARCRetainAutoreleasedReturnValue(llvm::Value *value)
llvm::Value * emitArrayLength(const ArrayType *arrayType, QualType &baseType, Address &addr)
emitArrayLength - Compute the length of an array, even if it's a VLA, and drill down to the base elem...
void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, const ArrayType *ArrayTy, Address ArrayPtr, const CXXConstructExpr *E, bool NewPointerIsChecked, bool ZeroInitialization=false)
AggValueSlot::Overlap_t getOverlapForBaseInit(const CXXRecordDecl *RD, const CXXRecordDecl *BaseRD, bool IsVirtual)
Determine whether a base class initialization may overlap some other object.
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This, QualType ThisTy)
LValue EmitLValue(const Expr *E, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitLValue - Emit code to compute a designator that specifies the location of the expression.
void EmitVTablePtrCheckForCast(QualType T, Address Derived, bool MayBeNull, CFITypeCheckKind TCK, SourceLocation Loc)
Derived is the presumed address of an object of type T after a cast.
void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, QualType DeleteTy, llvm::Value *NumElements=nullptr, CharUnits CookieSize=CharUnits())
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
const LangOptions & getLangOpts() const
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler.
void pushEHDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
LValue EmitLValueForFieldInitialization(LValue Base, const FieldDecl *Field)
EmitLValueForFieldInitialization - Like EmitLValueForField, except that if the Field is a reference,...
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
void EmitTrapCheck(llvm::Value *Checked, SanitizerHandler CheckHandlerID)
Create a basic block that will call the trap intrinsic, and emit a conditional branch to it,...
const CodeGen::CGBlockInfo * BlockInfo
void EmitAggregateCopyCtor(LValue Dest, LValue Src, AggValueSlot::Overlap_t MayOverlap)
llvm::Value * EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable, llvm::Type *VTableTy, uint64_t VTableByteOffset)
Emit a type checked load from the given vtable.
void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
EmitExprAsInit - Emits the code necessary to initialize a location in memory with the given initializ...
llvm::Value * GetVTTParameter(GlobalDecl GD, bool ForVirtualBase, bool Delegating)
GetVTTParameter - Return the VTT parameter that should be passed to a base constructor/destructor wit...
void EmitInheritedCXXConstructorCall(const CXXConstructorDecl *D, bool ForVirtualBase, Address This, bool InheritedFromVBase, const CXXInheritedCtorInitExpr *E)
Emit a call to a constructor inherited from a base class, passing the current constructor's arguments...
@ TCK_ConstructorCall
Checking the 'this' pointer for a constructor call.
@ TCK_UpcastToVirtualBase
Checking the operand of a cast to a virtual base object.
@ TCK_Upcast
Checking the operand of a cast to a base object.
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **callOrInvoke, bool IsMustTail, SourceLocation Loc)
EmitCall - Generate a call of the given function, expecting the given result type,...
VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass)
llvm::Type * ConvertTypeForMem(QualType T)
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
JumpDest ReturnBlock
ReturnBlock - Unified return block.
LValue EmitLValueForField(LValue Base, const FieldDecl *Field)
@ ForceLeftToRight
! Language semantics require left-to-right evaluation.
@ Default
! No language constraints on evaluation order.
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
llvm::SmallPtrSet< const CXXRecordDecl *, 4 > VisitedVirtualBasesSetTy
const TargetInfo & getTarget() const
void emitDestroy(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init)
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
EmitComplexExprIntoLValue - Emit the given expression of complex type and place its result into the s...
void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable, CFITypeCheckKind TCK, SourceLocation Loc)
EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for RD using llvm....
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerMask > > Checked, SanitizerHandler Check, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs)
Create a basic block that will either trap or call a handler function in the UBSan runtime with the p...
bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD)
Returns whether we should perform a type checked load when loading a virtual function for virtual cal...
void PushDestructorCleanup(QualType T, Address Addr)
PushDestructorCleanup - Push a cleanup to call the complete-object destructor of an object of the giv...
void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase, bool Delegating, AggValueSlot ThisAVS, const CXXConstructExpr *E)
void EmitDelegateCallArg(CallArgList &args, const VarDecl *param, SourceLocation loc)
EmitDelegateCallArg - We are performing a delegate call; that is, the current function is delegating ...
bool HaveInsertPoint() const
HaveInsertPoint - True if an insertion point is defined.
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
AggValueSlot::Overlap_t getOverlapForFieldInit(const FieldDecl *FD)
Determine whether a field initialization may overlap some other object.
void EmitAggregateCopy(LValue Dest, LValue Src, QualType EltTy, AggValueSlot::Overlap_t MayOverlap, bool isVolatile=false)
EmitAggregateCopy - Emit an aggregate copy.
const TargetCodeGenInfo & getTargetHooks() const
void EmitInlinedInheritingCXXConstructorCall(const CXXConstructorDecl *Ctor, CXXCtorType CtorType, bool ForVirtualBase, bool Delegating, CallArgList &Args)
Emit a call to an inheriting constructor (that is, one that invokes a constructor inherited from a ba...
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type.
Address GetAddressOfDerivedClass(Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue)
void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock=false)
static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor)
void EmitConstructorBody(FunctionArgList &Args)
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
void EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, Address This)
Emit assumption load for all bases.
Address CreateMemTemp(QualType T, const Twine &Name="tmp", Address *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
void EmitLambdaInAllocaCallOpBody(const CXXMethodDecl *MD)
llvm::Value * LoadCXXVTT()
LoadCXXVTT - Load the VTT parameter to base constructors/destructors have virtual bases.
Address GetAddressOfBaseClass(Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue, SourceLocation Loc)
GetAddressOfBaseClass - This function will add the necessary delta to the load of 'this' and returns ...
void EmitBranchThroughCleanup(JumpDest Dest)
EmitBranchThroughCleanup - Emit a branch from the current insert block through the normal cleanup han...
Address GetAddressOfDirectBaseInCompleteClass(Address Value, const CXXRecordDecl *Derived, const CXXRecordDecl *Base, bool BaseIsVirtual)
GetAddressOfBaseOfCompleteClass - Convert the given pointer to a complete class to the given direct b...
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind.
void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD, llvm::Value *VTable, SourceLocation Loc)
If whole-program virtual table optimization is enabled, emit an assumption that VTable is a member of...
CleanupKind getCleanupKind(QualType::DestructionKind kind)
llvm::Type * ConvertType(QualType T)
Address GetAddrOfBlockDecl(const VarDecl *var)
CodeGenTypes & getTypes() const
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T)
QualType BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args)
void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type, FunctionArgList &Args)
Address CreateIRTemp(QualType T, const Twine &Name="tmp")
CreateIRTemp - Create a temporary IR object of the given type, with appropriate alignment.
void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD, CallArgList &CallArgs)
void emitImplicitAssignmentOperatorBody(FunctionArgList &Args)
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
llvm::Value * LoadCXXThis()
LoadCXXThis - Load the value of 'this'.
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo)
EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
void EmitCfiSlowPathCheck(SanitizerMask Kind, llvm::Value *Cond, llvm::ConstantInt *TypeId, llvm::Value *Ptr, ArrayRef< llvm::Constant * > StaticArgs)
Emit a slow path cross-DSO CFI check which calls __cfi_slowpath if Cond if false.
void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type)
EnterDtorCleanups - Enter the cleanups necessary to complete the given phase of destruction for a des...
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
llvm::Value * GetVTablePtr(Address This, llvm::Type *VTableTy, const CXXRecordDecl *VTableClass)
GetVTablePtr - Return the Value of the vtable pointer member pointed to by This.
void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock=false)
void EmitStmt(const Stmt *S, ArrayRef< const Attr * > Attrs=std::nullopt)
EmitStmt - Emit the code for the statement.
LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T)
llvm::LLVMContext & getLLVMContext()
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler's counter for the given statement by StepV.
llvm::SmallVector< VPtr, 4 > VPtrsVector
void InitializeVTablePointers(const CXXRecordDecl *ClassDecl)
void InitializeVTablePointer(const VPtr &vptr)
Initialize the vtable pointer of the given subobject.
void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, llvm::Value *arrayEnd, QualType elementType, CharUnits elementAlignment, Destroyer *destroyer)
void GenerateCode(GlobalDecl GD, llvm::Function *Fn, const CGFunctionInfo &FnInfo)
void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F, const CGFunctionInfo &FI)
Set the attributes on the LLVM function for the given decl and function info.
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
CodeGenVTables & getVTables()
llvm::ConstantInt * CreateCrossDsoCfiTypeId(llvm::Metadata *MD)
Generate a cross-DSO type identifier for MD.
CharUnits getMinimumClassObjectSize(const CXXRecordDecl *CD)
Returns the minimum object size for an object of the given class type (or a class derived from it).
Definition: CGClass.cpp:59
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the given function.
void DecorateInstructionWithInvariantGroup(llvm::Instruction *I, const CXXRecordDecl *RD)
Adds !invariant.barrier !tag to instruction.
llvm::Constant * getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the constructor/destructor of the given type.
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
const LangOptions & getLangOpts() const
CharUnits getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, bool forPointeeType=false)
const TargetInfo & getTarget() const
llvm::Metadata * CreateMetadataIdentifierForType(QualType T)
Create a metadata identifier for the given type.
llvm::Constant * GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd)
Returns the offset from a derived class to a class.
Definition: CGClass.cpp:199
bool HasHiddenLTOVisibility(const CXXRecordDecl *RD)
Returns whether the given record has hidden LTO visibility and therefore may participate in (single-m...
Definition: CGVTables.cpp:1259
const llvm::DataLayout & getDataLayout() const
CharUnits computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start, CastExpr::path_const_iterator End)
Definition: CGClass.cpp:171
TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType)
getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an access to a virtual table poi...
CGCXXABI & getCXXABI() const
CharUnits getVBaseAlignment(CharUnits DerivedAlign, const CXXRecordDecl *Derived, const CXXRecordDecl *VBase)
Returns the assumed alignment of a virtual base of a class.
Definition: CGClass.cpp:76
CharUnits getClassPointerAlignment(const CXXRecordDecl *CD)
Returns the assumed alignment of an opaque pointer to the given class.
Definition: CGClass.cpp:40
bool AlwaysHasLTOVisibilityPublic(const CXXRecordDecl *RD)
Returns whether the given record has public LTO visibility (regardless of -lto-whole-program-visibili...
Definition: CGVTables.cpp:1235
void DecorateInstructionWithTBAA(llvm::Instruction *Inst, TBAAAccessInfo TBAAInfo)
DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign, const CXXRecordDecl *Class, CharUnits ExpectedTargetAlign)
Given a class pointer with an actual known alignment, and the expected alignment of an object at a dy...
Definition: CGClass.cpp:91
ItaniumVTableContext & getItaniumVTableContext()
ASTContext & getContext() const
const CodeGenOptions & getCodeGenOpts() const
llvm::LLVMContext & getLLVMContext()
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=std::nullopt)
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition: CGCall.cpp:297
const CGFunctionInfo & arrangeLLVMFunctionInfo(CanQualType returnType, FnInfoOpts opts, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, ArrayRef< FunctionProtoType::ExtParameterInfo > paramInfos, RequiredArgs args)
"Arrange" the LLVM information for a call or type with the given signature.
Definition: CGCall.cpp:756
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1623
bool inheritingCtorHasParams(const InheritedConstructor &Inherited, CXXCtorType Type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
Definition: CGCall.cpp:314
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
const CGFunctionInfo & arrangeCXXConstructorCall(const CallArgList &Args, const CXXConstructorDecl *D, CXXCtorType CtorKind, unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs, bool PassProtoArgs=true)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:409
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition: CGVTT.cpp:103
uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base)
getSubVTTIndex - Return the index of the sub-VTT for the base class of the given record decl.
Definition: CGVTT.cpp:128
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:141
A stack of scopes which respond to exceptions, including cleanups and catch blocks.
Definition: EHScopeStack.h:94
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition: CGCall.h:351
LValue - This represents an lvalue references.
Definition: CGValue.h:171
bool isBitField() const
Definition: CGValue.h:268
bool isSimple() const
Definition: CGValue.h:266
bool isVolatileQualified() const
Definition: CGValue.h:273
Address getAddress(CodeGenFunction &CGF) const
Definition: CGValue.h:350
Address getBitFieldAddress() const
Definition: CGValue.h:404
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:39
bool isScalar() const
Definition: CGValue.h:54
static RValue get(llvm::Value *V)
Definition: CGValue.h:89
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:61
bool isComplex() const
Definition: CGValue.h:55
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition: CGCall.h:355
virtual llvm::Value * performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, llvm::Value *V, LangAS SrcAddr, LangAS DestAddr, llvm::Type *DestTy, bool IsNonNull=false) const
Perform address space cast of an expression of pointer type.
Definition: TargetInfo.cpp:132
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1602
body_range body()
Definition: Stmt.h:1658
ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:3184
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2065
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
SourceLocation getEndLoc() const LLVM_READONLY
Definition: DeclBase.h:440
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition: DeclBase.h:597
SourceLocation getLocation() const
Definition: DeclBase.h:444
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required.
Definition: DeclBase.cpp:530
bool hasAttr() const
Definition: DeclBase.h:581
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:267
Represents a member of a struct/union/class.
Definition: Decl.h:3015
bool isBitField() const
Determines whether this field is a bitfield.
Definition: Decl.h:3106
unsigned getFieldIndex() const
Returns the index of this field within its record, as appropriate for passing to ASTRecordLayout::get...
Definition: Decl.cpp:4582
bool isZeroSize(const ASTContext &Ctx) const
Determine if this field is a subobject of zero size, that is, either a zero-length bit-field or a fie...
Definition: Decl.cpp:4540
Represents a function declaration or definition.
Definition: Decl.h:1957
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2664
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition: Decl.cpp:3187
bool hasTrivialBody() const
Returns whether the function has a trivial body that does not require any specific codegen.
Definition: Decl.cpp:3118
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition: Decl.cpp:3991
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition: Decl.cpp:3979
bool isDestroyingOperatorDelete() const
Determine whether this is a destroying operator delete.
Definition: Decl.cpp:3424
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3569
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2641
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2312
bool isVariadic() const
Whether this function is variadic.
Definition: Decl.cpp:3075
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition: Decl.cpp:4115
bool isDefaulted() const
Whether this function is defaulted.
Definition: Decl.h:2320
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3629
Represents a prototype with parameter type info, e.g.
Definition: Type.h:4160
param_type_iterator param_type_begin() const
Definition: Type.h:4536
bool isVariadic() const
Whether this function prototype is variadic.
Definition: Type.h:4500
Declaration of a template function.
Definition: DeclTemplate.h:977
FunctionDecl * findSpecialization(ArrayRef< TemplateArgument > Args, void *&InsertPos)
Return the specialization with the provided arguments if it exists, otherwise return the insertion po...
QualType getReturnType() const
Definition: Type.h:4078
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:105
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:110
const Decl * getDecl() const
Definition: GlobalDecl.h:103
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition: Expr.h:3677
Represents a field injected from an anonymous union/struct into the parent scope.
Definition: Decl.h:3293
ArrayRef< NamedDecl * > chain() const
Definition: Decl.h:3314
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:83
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3210
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:3289
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:3087
QualType getPointeeType() const
Definition: Type.h:3103
const Type * getClass() const
Definition: Type.h:3117
This represents a decl that may have a name.
Definition: Decl.h:248
std::string getQualifiedNameAsString() const
Definition: Decl.cpp:1679
bool containsType(SanitizerMask Mask, StringRef MangledTypeName, StringRef Category=StringRef()) const
A (possibly-)qualified type.
Definition: Type.h:736
bool isTriviallyCopyableType(const ASTContext &Context) const
Return true if this is a trivially copyable type (C++0x [basic.types]p9)
Definition: Type.cpp:2607
LangAS getAddressSpace() const
Return the address space of this type.
Definition: Type.h:6906
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6821
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition: Type.h:6981
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
Definition: Type.h:1319
bool isPODType(const ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition: Type.cpp:2497
The collection of all-type qualifiers we support.
Definition: Type.h:146
bool hasVolatile() const
Definition: Type.h:273
bool hasObjCLifetime() const
Definition: Type.h:350
LangAS getAddressSpace() const
Definition: Type.h:377
field_range fields() const
Definition: Decl.h:4323
bool mayInsertExtraPadding(bool EmitRemark=false) const
Whether we are allowed to insert extra padding between fields.
Definition: Decl.cpp:5074
bool isAnonymousStructOrUnion() const
Whether this is an anonymous struct or union.
Definition: Decl.h:4169
bool field_empty() const
Definition: Decl.h:4331
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4971
RecordDecl * getDecl() const
Definition: Type.h:4981
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
Encodes a location in the source.
Stmt - This represents one statement.
Definition: Stmt.h:84
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:325
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition: Decl.h:3636
bool isUnion() const
Definition: Decl.h:3739
bool hasConstructorVariants() const
Does this ABI have different entrypoints for complete-object and base-subobject constructors?
Definition: TargetCXXABI.h:194
bool areArgsDestroyedLeftToRightInCallee() const
Are arguments to a call destroyed left to right in the callee? This is a fundamental language change,...
Definition: TargetCXXABI.h:188
bool isItaniumFamily() const
Does this ABI generally fall into the Itanium family of ABIs?
Definition: TargetCXXABI.h:122
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1289
A template argument list.
Definition: DeclTemplate.h:243
ArrayRef< TemplateArgument > asArray() const
Produce this as an array ref.
Definition: DeclTemplate.h:295
const Type * getTypeForDecl() const
Definition: Decl.h:3365
The base class of the type hierarchy.
Definition: Type.h:1602
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1819
bool isVoidType() const
Definition: Type.h:7352
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7625
bool isReferenceType() const
Definition: Type.h:7045
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7558
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2210
Expr * getSubExpr() const
Definition: Expr.h:2255
Opcode getOpcode() const
Definition: Expr.h:2250
QualType getType() const
Definition: Decl.h:715
QualType getType() const
Definition: Value.cpp:234
Represents a variable declaration or definition.
Definition: Decl.h:916
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:80
@ NotKnownNonNull
Definition: Address.h:26
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ArrayType > arrayType
Matches all kinds of arrays.
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
Definition: StoreRef.h:27
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1711
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
@ Ctor_Base
Base object ctor.
Definition: ABI.h:26
@ Ctor_Complete
Complete object ctor.
Definition: ABI.h:25
@ CPlusPlus
Definition: LangStandard.h:54
CXXDtorType
C++ destructor types.
Definition: ABI.h:33
@ Dtor_Comdat
The COMDAT used for dtors.
Definition: ABI.h:37
@ Dtor_Base
Base object dtor.
Definition: ABI.h:36
@ Dtor_Complete
Complete object dtor.
Definition: ABI.h:35
@ Dtor_Deleting
Deleting dtor.
Definition: ABI.h:34
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
unsigned long uint64_t
Definition: Format.h:5226
#define false
Definition: stdbool.h:22
Structure with information about how a bitfield should be accessed.
CharUnits StorageOffset
The offset of the bitfield storage from the start of the struct.
Similar to AddedStructorArgs, but only notes the number of additional arguments.
Definition: CGCXXABI.h:355
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
void set(SanitizerMask K, bool Value)
Enable or disable a certain (single) sanitizer.
Definition: Sanitizers.h:168
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:159