1 //===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with C++ code generation of classes
12 //===----------------------------------------------------------------------===//
15 #include "CGDebugInfo.h"
16 #include "CodeGenFunction.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/EvaluatedExprVisitor.h"
19 #include "clang/AST/RecordLayout.h"
20 #include "clang/AST/StmtCXX.h"
21 #include "clang/Frontend/CodeGenOptions.h"
23 using namespace clang;
24 using namespace CodeGen;
27 ComputeNonVirtualBaseClassOffset(ASTContext &Context,
28 const CXXRecordDecl *DerivedClass,
29 CastExpr::path_const_iterator Start,
30 CastExpr::path_const_iterator End) {
31 CharUnits Offset = CharUnits::Zero();
33 const CXXRecordDecl *RD = DerivedClass;
35 for (CastExpr::path_const_iterator I = Start; I != End; ++I) {
36 const CXXBaseSpecifier *Base = *I;
37 assert(!Base->isVirtual() && "Should not see virtual bases here!");
40 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
42 const CXXRecordDecl *BaseDecl =
43 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
46 Offset += Layout.getBaseClassOffset(BaseDecl);
55 CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
56 CastExpr::path_const_iterator PathBegin,
57 CastExpr::path_const_iterator PathEnd) {
58 assert(PathBegin != PathEnd && "Base path should not be empty!");
61 ComputeNonVirtualBaseClassOffset(getContext(), ClassDecl,
66 llvm::Type *PtrDiffTy =
67 Types.ConvertType(getContext().getPointerDiffType());
69 return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
72 /// Gets the address of a direct base class within a complete object.
73 /// This should only be used for (1) non-virtual bases or (2) virtual bases
74 /// when the type is known to be complete (e.g. in complete destructors).
76 /// The object pointed to by 'This' is assumed to be non-null.
78 CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(llvm::Value *This,
79 const CXXRecordDecl *Derived,
80 const CXXRecordDecl *Base,
82 // 'this' must be a pointer (in some address space) to Derived.
83 assert(This->getType()->isPointerTy() &&
84 cast<llvm::PointerType>(This->getType())->getElementType()
85 == ConvertType(Derived));
87 // Compute the offset of the virtual base.
89 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
91 Offset = Layout.getVBaseClassOffset(Base);
93 Offset = Layout.getBaseClassOffset(Base);
95 // Shift and cast down to the base type.
96 // TODO: for complete types, this should be possible with a GEP.
97 llvm::Value *V = This;
98 if (Offset.isPositive()) {
99 V = Builder.CreateBitCast(V, Int8PtrTy);
100 V = Builder.CreateConstInBoundsGEP1_64(V, Offset.getQuantity());
102 V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo());
108 ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ThisPtr,
109 CharUnits NonVirtual, llvm::Value *Virtual) {
110 llvm::Type *PtrDiffTy =
111 CGF.ConvertType(CGF.getContext().getPointerDiffType());
113 llvm::Value *NonVirtualOffset = 0;
114 if (!NonVirtual.isZero())
115 NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy,
116 NonVirtual.getQuantity());
118 llvm::Value *BaseOffset;
120 if (NonVirtualOffset)
121 BaseOffset = CGF.Builder.CreateAdd(Virtual, NonVirtualOffset);
123 BaseOffset = Virtual;
125 BaseOffset = NonVirtualOffset;
127 // Apply the base offset.
128 ThisPtr = CGF.Builder.CreateBitCast(ThisPtr, CGF.Int8PtrTy);
129 ThisPtr = CGF.Builder.CreateGEP(ThisPtr, BaseOffset, "add.ptr");
135 CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value,
136 const CXXRecordDecl *Derived,
137 CastExpr::path_const_iterator PathBegin,
138 CastExpr::path_const_iterator PathEnd,
139 bool NullCheckValue) {
140 assert(PathBegin != PathEnd && "Base path should not be empty!");
142 CastExpr::path_const_iterator Start = PathBegin;
143 const CXXRecordDecl *VBase = 0;
145 // Get the virtual base.
146 if ((*Start)->isVirtual()) {
148 cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl());
152 CharUnits NonVirtualOffset =
153 ComputeNonVirtualBaseClassOffset(getContext(), VBase ? VBase : Derived,
156 // Get the base pointer type.
157 llvm::Type *BasePtrTy =
158 ConvertType((PathEnd[-1])->getType())->getPointerTo();
160 if (NonVirtualOffset.isZero() && !VBase) {
162 return Builder.CreateBitCast(Value, BasePtrTy);
165 llvm::BasicBlock *CastNull = 0;
166 llvm::BasicBlock *CastNotNull = 0;
167 llvm::BasicBlock *CastEnd = 0;
169 if (NullCheckValue) {
170 CastNull = createBasicBlock("cast.null");
171 CastNotNull = createBasicBlock("cast.notnull");
172 CastEnd = createBasicBlock("cast.end");
174 llvm::Value *IsNull = Builder.CreateIsNull(Value);
175 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
176 EmitBlock(CastNotNull);
179 llvm::Value *VirtualOffset = 0;
182 if (Derived->hasAttr<FinalAttr>()) {
185 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
187 CharUnits VBaseOffset = Layout.getVBaseClassOffset(VBase);
188 NonVirtualOffset += VBaseOffset;
190 VirtualOffset = GetVirtualBaseClassOffset(Value, Derived, VBase);
193 // Apply the offsets.
194 Value = ApplyNonVirtualAndVirtualOffset(*this, Value,
199 Value = Builder.CreateBitCast(Value, BasePtrTy);
201 if (NullCheckValue) {
202 Builder.CreateBr(CastEnd);
204 Builder.CreateBr(CastEnd);
207 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
208 PHI->addIncoming(Value, CastNotNull);
209 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()),
218 CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value,
219 const CXXRecordDecl *Derived,
220 CastExpr::path_const_iterator PathBegin,
221 CastExpr::path_const_iterator PathEnd,
222 bool NullCheckValue) {
223 assert(PathBegin != PathEnd && "Base path should not be empty!");
226 getContext().getCanonicalType(getContext().getTagDeclType(Derived));
227 llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo();
229 llvm::Value *NonVirtualOffset =
230 CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd);
232 if (!NonVirtualOffset) {
233 // No offset, we can just cast back.
234 return Builder.CreateBitCast(Value, DerivedPtrTy);
237 llvm::BasicBlock *CastNull = 0;
238 llvm::BasicBlock *CastNotNull = 0;
239 llvm::BasicBlock *CastEnd = 0;
241 if (NullCheckValue) {
242 CastNull = createBasicBlock("cast.null");
243 CastNotNull = createBasicBlock("cast.notnull");
244 CastEnd = createBasicBlock("cast.end");
246 llvm::Value *IsNull = Builder.CreateIsNull(Value);
247 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
248 EmitBlock(CastNotNull);
252 Value = Builder.CreateBitCast(Value, Int8PtrTy);
253 Value = Builder.CreateGEP(Value, Builder.CreateNeg(NonVirtualOffset),
257 Value = Builder.CreateBitCast(Value, DerivedPtrTy);
259 if (NullCheckValue) {
260 Builder.CreateBr(CastEnd);
262 Builder.CreateBr(CastEnd);
265 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
266 PHI->addIncoming(Value, CastNotNull);
267 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()),
275 /// GetVTTParameter - Return the VTT parameter that should be passed to a
276 /// base constructor/destructor with virtual bases.
277 static llvm::Value *GetVTTParameter(CodeGenFunction &CGF, GlobalDecl GD,
278 bool ForVirtualBase) {
279 if (!CodeGenVTables::needsVTTParameter(GD)) {
280 // This constructor/destructor does not need a VTT parameter.
284 const CXXRecordDecl *RD = cast<CXXMethodDecl>(CGF.CurFuncDecl)->getParent();
285 const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent();
289 uint64_t SubVTTIndex;
291 // If the record matches the base, this is the complete ctor/dtor
292 // variant calling the base variant in a class with virtual bases.
294 assert(!CodeGenVTables::needsVTTParameter(CGF.CurGD) &&
295 "doing no-op VTT offset in base dtor/ctor?");
296 assert(!ForVirtualBase && "Can't have same class as virtual base!");
299 const ASTRecordLayout &Layout =
300 CGF.getContext().getASTRecordLayout(RD);
301 CharUnits BaseOffset = ForVirtualBase ?
302 Layout.getVBaseClassOffset(Base) :
303 Layout.getBaseClassOffset(Base);
306 CGF.CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset));
307 assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
310 if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
311 // A VTT parameter was passed to the constructor, use it.
312 VTT = CGF.LoadCXXVTT();
313 VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex);
315 // We're the complete constructor, so get the VTT by name.
316 VTT = CGF.CGM.getVTables().GetAddrOfVTT(RD);
317 VTT = CGF.Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex);
324 /// Call the destructor for a direct base class.
325 struct CallBaseDtor : EHScopeStack::Cleanup {
326 const CXXRecordDecl *BaseClass;
328 CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual)
329 : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
331 void Emit(CodeGenFunction &CGF, Flags flags) {
332 const CXXRecordDecl *DerivedClass =
333 cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
335 const CXXDestructorDecl *D = BaseClass->getDestructor();
337 CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThis(),
338 DerivedClass, BaseClass,
340 CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual, Addr);
344 /// A visitor which checks whether an initializer uses 'this' in a
345 /// way which requires the vtable to be properly set.
346 struct DynamicThisUseChecker : EvaluatedExprVisitor<DynamicThisUseChecker> {
347 typedef EvaluatedExprVisitor<DynamicThisUseChecker> super;
351 DynamicThisUseChecker(ASTContext &C) : super(C), UsesThis(false) {}
353 // Black-list all explicit and implicit references to 'this'.
355 // Do we need to worry about external references to 'this' derived
356 // from arbitrary code? If so, then anything which runs arbitrary
357 // external code might potentially access the vtable.
358 void VisitCXXThisExpr(CXXThisExpr *E) { UsesThis = true; }
362 static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) {
363 DynamicThisUseChecker Checker(C);
364 Checker.Visit(const_cast<Expr*>(Init));
365 return Checker.UsesThis;
368 static void EmitBaseInitializer(CodeGenFunction &CGF,
369 const CXXRecordDecl *ClassDecl,
370 CXXCtorInitializer *BaseInit,
371 CXXCtorType CtorType) {
372 assert(BaseInit->isBaseInitializer() &&
373 "Must have base initializer!");
375 llvm::Value *ThisPtr = CGF.LoadCXXThis();
377 const Type *BaseType = BaseInit->getBaseClass();
378 CXXRecordDecl *BaseClassDecl =
379 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
381 bool isBaseVirtual = BaseInit->isBaseVirtual();
383 // The base constructor doesn't construct virtual bases.
384 if (CtorType == Ctor_Base && isBaseVirtual)
387 // If the initializer for the base (other than the constructor
388 // itself) accesses 'this' in any way, we need to initialize the
390 if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit()))
391 CGF.InitializeVTablePointers(ClassDecl);
393 // We can pretend to be a complete class because it only matters for
394 // virtual bases, and we only do virtual bases for complete ctors.
396 CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl,
399 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(BaseType);
400 AggValueSlot AggSlot =
401 AggValueSlot::forAddr(V, Alignment, Qualifiers(),
402 AggValueSlot::IsDestructed,
403 AggValueSlot::DoesNotNeedGCBarriers,
404 AggValueSlot::IsNotAliased);
406 CGF.EmitAggExpr(BaseInit->getInit(), AggSlot);
408 if (CGF.CGM.getLangOpts().Exceptions &&
409 !BaseClassDecl->hasTrivialDestructor())
410 CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl,
414 static void EmitAggMemberInitializer(CodeGenFunction &CGF,
417 llvm::Value *ArrayIndexVar,
419 ArrayRef<VarDecl *> ArrayIndexes,
421 if (Index == ArrayIndexes.size()) {
423 { // Scope for Cleanups.
424 CodeGenFunction::RunCleanupsScope Cleanups(CGF);
427 // If we have an array index variable, load it and use it as an offset.
428 // Then, increment the value.
429 llvm::Value *Dest = LHS.getAddress();
430 llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar);
431 Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress");
432 llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1);
433 Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc");
434 CGF.Builder.CreateStore(Next, ArrayIndexVar);
436 // Update the LValue.
438 CharUnits Align = CGF.getContext().getTypeAlignInChars(T);
439 LV.setAlignment(std::min(Align, LV.getAlignment()));
442 if (!CGF.hasAggregateLLVMType(T)) {
443 CGF.EmitScalarInit(Init, /*decl*/ 0, LV, false);
444 } else if (T->isAnyComplexType()) {
445 CGF.EmitComplexExprIntoAddr(Init, LV.getAddress(),
446 LV.isVolatileQualified());
449 AggValueSlot::forLValue(LV,
450 AggValueSlot::IsDestructed,
451 AggValueSlot::DoesNotNeedGCBarriers,
452 AggValueSlot::IsNotAliased);
454 CGF.EmitAggExpr(Init, Slot);
458 // Now, outside of the initializer cleanup scope, destroy the backing array
459 // for a std::initializer_list member.
460 CGF.MaybeEmitStdInitializerListCleanup(LV.getAddress(), Init);
465 const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T);
466 assert(Array && "Array initialization without the array type?");
467 llvm::Value *IndexVar
468 = CGF.GetAddrOfLocalVar(ArrayIndexes[Index]);
469 assert(IndexVar && "Array index variable not loaded");
471 // Initialize this index variable to zero.
473 = llvm::Constant::getNullValue(
474 CGF.ConvertType(CGF.getContext().getSizeType()));
475 CGF.Builder.CreateStore(Zero, IndexVar);
477 // Start the loop with a block that tests the condition.
478 llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond");
479 llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end");
481 CGF.EmitBlock(CondBlock);
483 llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body");
484 // Generate: if (loop-index < number-of-elements) fall to the loop body,
485 // otherwise, go to the block after the for-loop.
486 uint64_t NumElements = Array->getSize().getZExtValue();
487 llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar);
488 llvm::Value *NumElementsPtr =
489 llvm::ConstantInt::get(Counter->getType(), NumElements);
490 llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr,
493 // If the condition is true, execute the body.
494 CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor);
496 CGF.EmitBlock(ForBody);
497 llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc");
500 CodeGenFunction::RunCleanupsScope Cleanups(CGF);
502 // Inside the loop body recurse to emit the inner loop or, eventually, the
504 EmitAggMemberInitializer(CGF, LHS, Init, ArrayIndexVar,
505 Array->getElementType(), ArrayIndexes, Index + 1);
508 CGF.EmitBlock(ContinueBlock);
510 // Emit the increment of the loop counter.
511 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
512 Counter = CGF.Builder.CreateLoad(IndexVar);
513 NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc");
514 CGF.Builder.CreateStore(NextVal, IndexVar);
516 // Finally, branch back up to the condition for the next iteration.
517 CGF.EmitBranch(CondBlock);
519 // Emit the fall-through block.
520 CGF.EmitBlock(AfterFor, true);
524 struct CallMemberDtor : EHScopeStack::Cleanup {
526 CXXDestructorDecl *Dtor;
528 CallMemberDtor(llvm::Value *V, CXXDestructorDecl *Dtor)
529 : V(V), Dtor(Dtor) {}
531 void Emit(CodeGenFunction &CGF, Flags flags) {
532 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
538 static bool hasTrivialCopyOrMoveConstructor(const CXXRecordDecl *Record,
540 return Moving ? Record->hasTrivialMoveConstructor() :
541 Record->hasTrivialCopyConstructor();
544 static void EmitMemberInitializer(CodeGenFunction &CGF,
545 const CXXRecordDecl *ClassDecl,
546 CXXCtorInitializer *MemberInit,
547 const CXXConstructorDecl *Constructor,
548 FunctionArgList &Args) {
549 assert(MemberInit->isAnyMemberInitializer() &&
550 "Must have member initializer!");
551 assert(MemberInit->getInit() && "Must have initializer!");
553 // non-static data member initializers.
554 FieldDecl *Field = MemberInit->getAnyMember();
555 QualType FieldType = Field->getType();
557 llvm::Value *ThisPtr = CGF.LoadCXXThis();
558 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
561 // If we are initializing an anonymous union field, drill down to the field.
562 if (MemberInit->isIndirectMemberInitializer()) {
563 LHS = CGF.EmitLValueForAnonRecordField(ThisPtr,
564 MemberInit->getIndirectMember(), 0);
565 FieldType = MemberInit->getIndirectMember()->getAnonField()->getType();
567 LValue ThisLHSLV = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
568 LHS = CGF.EmitLValueForFieldInitialization(ThisLHSLV, Field);
571 // Special case: if we are in a copy or move constructor, and we are copying
572 // an array of PODs or classes with trivial copy constructors, ignore the
573 // AST and perform the copy we know is equivalent.
574 // FIXME: This is hacky at best... if we had a bit more explicit information
575 // in the AST, we could generalize it more easily.
576 const ConstantArrayType *Array
577 = CGF.getContext().getAsConstantArrayType(FieldType);
578 if (Array && Constructor->isImplicitlyDefined() &&
579 Constructor->isCopyOrMoveConstructor()) {
580 QualType BaseElementTy = CGF.getContext().getBaseElementType(Array);
581 const CXXRecordDecl *Record = BaseElementTy->getAsCXXRecordDecl();
582 if (BaseElementTy.isPODType(CGF.getContext()) ||
583 (Record && hasTrivialCopyOrMoveConstructor(Record,
584 Constructor->isMoveConstructor()))) {
585 // Find the source pointer. We knows it's the last argument because
586 // we know we're in a copy constructor.
587 unsigned SrcArgIndex = Args.size() - 1;
589 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex]));
590 LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
591 LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field);
593 // Copy the aggregate.
594 CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType,
595 LHS.isVolatileQualified());
600 ArrayRef<VarDecl *> ArrayIndexes;
601 if (MemberInit->getNumArrayIndices())
602 ArrayIndexes = MemberInit->getArrayIndexes();
603 CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit(), ArrayIndexes);
606 void CodeGenFunction::EmitInitializerForField(FieldDecl *Field,
607 LValue LHS, Expr *Init,
608 ArrayRef<VarDecl *> ArrayIndexes) {
609 QualType FieldType = Field->getType();
610 if (!hasAggregateLLVMType(FieldType)) {
611 if (LHS.isSimple()) {
612 EmitExprAsInit(Init, Field, LHS, false);
614 RValue RHS = RValue::get(EmitScalarExpr(Init));
615 EmitStoreThroughLValue(RHS, LHS);
617 } else if (FieldType->isAnyComplexType()) {
618 EmitComplexExprIntoAddr(Init, LHS.getAddress(), LHS.isVolatileQualified());
620 llvm::Value *ArrayIndexVar = 0;
621 if (ArrayIndexes.size()) {
622 llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
624 // The LHS is a pointer to the first object we'll be constructing, as
626 QualType BaseElementTy = getContext().getBaseElementType(FieldType);
627 llvm::Type *BasePtr = ConvertType(BaseElementTy);
628 BasePtr = llvm::PointerType::getUnqual(BasePtr);
629 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(LHS.getAddress(),
631 LHS = MakeAddrLValue(BaseAddrPtr, BaseElementTy);
633 // Create an array index that will be used to walk over all of the
634 // objects we're constructing.
635 ArrayIndexVar = CreateTempAlloca(SizeTy, "object.index");
636 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
637 Builder.CreateStore(Zero, ArrayIndexVar);
640 // Emit the block variables for the array indices, if any.
641 for (unsigned I = 0, N = ArrayIndexes.size(); I != N; ++I)
642 EmitAutoVarDecl(*ArrayIndexes[I]);
645 EmitAggMemberInitializer(*this, LHS, Init, ArrayIndexVar, FieldType,
648 if (!CGM.getLangOpts().Exceptions)
651 // FIXME: If we have an array of classes w/ non-trivial destructors,
652 // we need to destroy in reverse order of construction along the exception
654 const RecordType *RT = FieldType->getAs<RecordType>();
658 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
659 if (!RD->hasTrivialDestructor())
660 EHStack.pushCleanup<CallMemberDtor>(EHCleanup, LHS.getAddress(),
661 RD->getDestructor());
665 /// Checks whether the given constructor is a valid subject for the
666 /// complete-to-base constructor delegation optimization, i.e.
667 /// emitting the complete constructor as a simple call to the base
669 static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) {
671 // Currently we disable the optimization for classes with virtual
672 // bases because (1) the addresses of parameter variables need to be
673 // consistent across all initializers but (2) the delegate function
674 // call necessarily creates a second copy of the parameter variable.
676 // The limiting example (purely theoretical AFAIK):
677 // struct A { A(int &c) { c++; } };
678 // struct B : virtual A {
679 // B(int count) : A(count) { printf("%d\n", count); }
681 // ...although even this example could in principle be emitted as a
682 // delegation since the address of the parameter doesn't escape.
683 if (Ctor->getParent()->getNumVBases()) {
684 // TODO: white-list trivial vbase initializers. This case wouldn't
685 // be subject to the restrictions below.
687 // TODO: white-list cases where:
688 // - there are no non-reference parameters to the constructor
689 // - the initializers don't access any non-reference parameters
690 // - the initializers don't take the address of non-reference
693 // If we ever add any of the above cases, remember that:
694 // - function-try-blocks will always blacklist this optimization
695 // - we need to perform the constructor prologue and cleanup in
696 // EmitConstructorBody.
701 // We also disable the optimization for variadic functions because
702 // it's impossible to "re-pass" varargs.
703 if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic())
706 // FIXME: Decide if we can do a delegation of a delegating constructor.
707 if (Ctor->isDelegatingConstructor())
713 /// EmitConstructorBody - Emits the body of the current constructor.
714 void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) {
715 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl());
716 CXXCtorType CtorType = CurGD.getCtorType();
718 // Before we go any further, try the complete->base constructor
719 // delegation optimization.
720 if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor)) {
721 if (CGDebugInfo *DI = getDebugInfo())
722 DI->EmitLocation(Builder, Ctor->getLocEnd());
723 EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args);
727 Stmt *Body = Ctor->getBody();
729 // Enter the function-try-block before the constructor prologue if
731 bool IsTryBody = (Body && isa<CXXTryStmt>(Body));
733 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
735 EHScopeStack::stable_iterator CleanupDepth = EHStack.stable_begin();
737 // TODO: in restricted cases, we can emit the vbase initializers of
738 // a complete ctor and then delegate to the base ctor.
740 // Emit the constructor prologue, i.e. the base and member
742 EmitCtorPrologue(Ctor, CtorType, Args);
744 // Emit the body of the statement.
746 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
750 // Emit any cleanup blocks associated with the member or base
751 // initializers, which includes (along the exceptional path) the
752 // destructors for those members and bases that were fully
754 PopCleanupBlocks(CleanupDepth);
757 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
760 /// EmitCtorPrologue - This routine generates necessary code to initialize
761 /// base classes and non-static data members belonging to this constructor.
762 void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
763 CXXCtorType CtorType,
764 FunctionArgList &Args) {
765 if (CD->isDelegatingConstructor())
766 return EmitDelegatingCXXConstructorCall(CD, Args);
768 const CXXRecordDecl *ClassDecl = CD->getParent();
770 SmallVector<CXXCtorInitializer *, 8> MemberInitializers;
772 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
775 CXXCtorInitializer *Member = (*B);
777 if (Member->isBaseInitializer()) {
778 EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
780 assert(Member->isAnyMemberInitializer() &&
781 "Delegating initializer on non-delegating constructor");
782 MemberInitializers.push_back(Member);
786 InitializeVTablePointers(ClassDecl);
788 for (unsigned I = 0, E = MemberInitializers.size(); I != E; ++I)
789 EmitMemberInitializer(*this, ClassDecl, MemberInitializers[I], CD, Args);
793 FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field);
796 HasTrivialDestructorBody(ASTContext &Context,
797 const CXXRecordDecl *BaseClassDecl,
798 const CXXRecordDecl *MostDerivedClassDecl)
800 // If the destructor is trivial we don't have to check anything else.
801 if (BaseClassDecl->hasTrivialDestructor())
804 if (!BaseClassDecl->getDestructor()->hasTrivialBody())
808 for (CXXRecordDecl::field_iterator I = BaseClassDecl->field_begin(),
809 E = BaseClassDecl->field_end(); I != E; ++I) {
810 const FieldDecl *Field = *I;
812 if (!FieldHasTrivialDestructorBody(Context, Field))
816 // Check non-virtual bases.
817 for (CXXRecordDecl::base_class_const_iterator I =
818 BaseClassDecl->bases_begin(), E = BaseClassDecl->bases_end();
823 const CXXRecordDecl *NonVirtualBase =
824 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
825 if (!HasTrivialDestructorBody(Context, NonVirtualBase,
826 MostDerivedClassDecl))
830 if (BaseClassDecl == MostDerivedClassDecl) {
831 // Check virtual bases.
832 for (CXXRecordDecl::base_class_const_iterator I =
833 BaseClassDecl->vbases_begin(), E = BaseClassDecl->vbases_end();
835 const CXXRecordDecl *VirtualBase =
836 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
837 if (!HasTrivialDestructorBody(Context, VirtualBase,
838 MostDerivedClassDecl))
847 FieldHasTrivialDestructorBody(ASTContext &Context,
848 const FieldDecl *Field)
850 QualType FieldBaseElementType = Context.getBaseElementType(Field->getType());
852 const RecordType *RT = FieldBaseElementType->getAs<RecordType>();
856 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
857 return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
860 /// CanSkipVTablePointerInitialization - Check whether we need to initialize
861 /// any vtable pointers before calling this destructor.
862 static bool CanSkipVTablePointerInitialization(ASTContext &Context,
863 const CXXDestructorDecl *Dtor) {
864 if (!Dtor->hasTrivialBody())
868 const CXXRecordDecl *ClassDecl = Dtor->getParent();
869 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
870 E = ClassDecl->field_end(); I != E; ++I) {
871 const FieldDecl *Field = *I;
873 if (!FieldHasTrivialDestructorBody(Context, Field))
880 /// EmitDestructorBody - Emits the body of the current destructor.
881 void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) {
882 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl());
883 CXXDtorType DtorType = CurGD.getDtorType();
885 // The call to operator delete in a deleting destructor happens
886 // outside of the function-try-block, which means it's always
887 // possible to delegate the destructor body to the complete
888 // destructor. Do so.
889 if (DtorType == Dtor_Deleting) {
890 EnterDtorCleanups(Dtor, Dtor_Deleting);
891 EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
897 Stmt *Body = Dtor->getBody();
899 // If the body is a function-try-block, enter the try before
901 bool isTryBody = (Body && isa<CXXTryStmt>(Body));
903 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
905 // Enter the epilogue cleanups.
906 RunCleanupsScope DtorEpilogue(*this);
908 // If this is the complete variant, just invoke the base variant;
909 // the epilogue will destruct the virtual bases. But we can't do
910 // this optimization if the body is a function-try-block, because
911 // we'd introduce *two* handler blocks.
913 case Dtor_Deleting: llvm_unreachable("already handled deleting case");
916 // Enter the cleanup scopes for virtual bases.
917 EnterDtorCleanups(Dtor, Dtor_Complete);
920 EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false,
924 // Fallthrough: act like we're in the base variant.
927 // Enter the cleanup scopes for fields and non-virtual bases.
928 EnterDtorCleanups(Dtor, Dtor_Base);
930 // Initialize the vtable pointers before entering the body.
931 if (!CanSkipVTablePointerInitialization(getContext(), Dtor))
932 InitializeVTablePointers(Dtor->getParent());
935 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
939 assert(Dtor->isImplicit() && "bodyless dtor not implicit");
940 // nothing to do besides what's in the epilogue
942 // -fapple-kext must inline any call to this dtor into
943 // the caller's body.
944 if (getContext().getLangOpts().AppleKext)
945 CurFn->addFnAttr(llvm::Attribute::AlwaysInline);
949 // Jump out through the epilogue cleanups.
950 DtorEpilogue.ForceCleanup();
952 // Exit the try if applicable.
954 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
958 /// Call the operator delete associated with the current destructor.
959 struct CallDtorDelete : EHScopeStack::Cleanup {
962 void Emit(CodeGenFunction &CGF, Flags flags) {
963 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
964 const CXXRecordDecl *ClassDecl = Dtor->getParent();
965 CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
966 CGF.getContext().getTagDeclType(ClassDecl));
970 class DestroyField : public EHScopeStack::Cleanup {
971 const FieldDecl *field;
972 CodeGenFunction::Destroyer *destroyer;
973 bool useEHCleanupForArray;
976 DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer,
977 bool useEHCleanupForArray)
978 : field(field), destroyer(destroyer),
979 useEHCleanupForArray(useEHCleanupForArray) {}
981 void Emit(CodeGenFunction &CGF, Flags flags) {
982 // Find the address of the field.
983 llvm::Value *thisValue = CGF.LoadCXXThis();
984 QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent());
985 LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy);
986 LValue LV = CGF.EmitLValueForField(ThisLV, field);
987 assert(LV.isSimple());
989 CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer,
990 flags.isForNormalCleanup() && useEHCleanupForArray);
995 /// EmitDtorEpilogue - Emit all code that comes at the end of class's
996 /// destructor. This is to call destructors on members and base classes
997 /// in reverse order of their construction.
998 void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD,
999 CXXDtorType DtorType) {
1000 assert(!DD->isTrivial() &&
1001 "Should not emit dtor epilogue for trivial dtor!");
1003 // The deleting-destructor phase just needs to call the appropriate
1004 // operator delete that Sema picked up.
1005 if (DtorType == Dtor_Deleting) {
1006 assert(DD->getOperatorDelete() &&
1007 "operator delete missing - EmitDtorEpilogue");
1008 EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup);
1012 const CXXRecordDecl *ClassDecl = DD->getParent();
1014 // Unions have no bases and do not call field destructors.
1015 if (ClassDecl->isUnion())
1018 // The complete-destructor phase just destructs all the virtual bases.
1019 if (DtorType == Dtor_Complete) {
1021 // We push them in the forward order so that they'll be popped in
1022 // the reverse order.
1023 for (CXXRecordDecl::base_class_const_iterator I =
1024 ClassDecl->vbases_begin(), E = ClassDecl->vbases_end();
1026 const CXXBaseSpecifier &Base = *I;
1027 CXXRecordDecl *BaseClassDecl
1028 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1030 // Ignore trivial destructors.
1031 if (BaseClassDecl->hasTrivialDestructor())
1034 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1036 /*BaseIsVirtual*/ true);
1042 assert(DtorType == Dtor_Base);
1044 // Destroy non-virtual bases.
1045 for (CXXRecordDecl::base_class_const_iterator I =
1046 ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) {
1047 const CXXBaseSpecifier &Base = *I;
1049 // Ignore virtual bases.
1050 if (Base.isVirtual())
1053 CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl();
1055 // Ignore trivial destructors.
1056 if (BaseClassDecl->hasTrivialDestructor())
1059 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1061 /*BaseIsVirtual*/ false);
1064 // Destroy direct fields.
1065 SmallVector<const FieldDecl *, 16> FieldDecls;
1066 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1067 E = ClassDecl->field_end(); I != E; ++I) {
1068 const FieldDecl *field = *I;
1069 QualType type = field->getType();
1070 QualType::DestructionKind dtorKind = type.isDestructedType();
1071 if (!dtorKind) continue;
1073 // Anonymous union members do not have their destructors called.
1074 const RecordType *RT = type->getAsUnionType();
1075 if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue;
1077 CleanupKind cleanupKind = getCleanupKind(dtorKind);
1078 EHStack.pushCleanup<DestroyField>(cleanupKind, field,
1079 getDestroyer(dtorKind),
1080 cleanupKind & EHCleanup);
1084 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1085 /// constructor for each of several members of an array.
1087 /// \param ctor the constructor to call for each element
1088 /// \param argBegin,argEnd the arguments to evaluate and pass to the
1090 /// \param arrayType the type of the array to initialize
1091 /// \param arrayBegin an arrayType*
1092 /// \param zeroInitialize true if each element should be
1093 /// zero-initialized before it is constructed
1095 CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
1096 const ConstantArrayType *arrayType,
1097 llvm::Value *arrayBegin,
1098 CallExpr::const_arg_iterator argBegin,
1099 CallExpr::const_arg_iterator argEnd,
1100 bool zeroInitialize) {
1101 QualType elementType;
1102 llvm::Value *numElements =
1103 emitArrayLength(arrayType, elementType, arrayBegin);
1105 EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin,
1106 argBegin, argEnd, zeroInitialize);
1109 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1110 /// constructor for each of several members of an array.
1112 /// \param ctor the constructor to call for each element
1113 /// \param numElements the number of elements in the array;
1115 /// \param argBegin,argEnd the arguments to evaluate and pass to the
1117 /// \param arrayBegin a T*, where T is the type constructed by ctor
1118 /// \param zeroInitialize true if each element should be
1119 /// zero-initialized before it is constructed
1121 CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
1122 llvm::Value *numElements,
1123 llvm::Value *arrayBegin,
1124 CallExpr::const_arg_iterator argBegin,
1125 CallExpr::const_arg_iterator argEnd,
1126 bool zeroInitialize) {
1128 // It's legal for numElements to be zero. This can happen both
1129 // dynamically, because x can be zero in 'new A[x]', and statically,
1130 // because of GCC extensions that permit zero-length arrays. There
1131 // are probably legitimate places where we could assume that this
1132 // doesn't happen, but it's not clear that it's worth it.
1133 llvm::BranchInst *zeroCheckBranch = 0;
1135 // Optimize for a constant count.
1136 llvm::ConstantInt *constantCount
1137 = dyn_cast<llvm::ConstantInt>(numElements);
1138 if (constantCount) {
1139 // Just skip out if the constant count is zero.
1140 if (constantCount->isZero()) return;
1142 // Otherwise, emit the check.
1144 llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop");
1145 llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty");
1146 zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB);
1150 // Find the end of the array.
1151 llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements,
1154 // Enter the loop, setting up a phi for the current location to initialize.
1155 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1156 llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop");
1158 llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2,
1160 cur->addIncoming(arrayBegin, entryBB);
1162 // Inside the loop body, emit the constructor call on the array element.
1164 QualType type = getContext().getTypeDeclType(ctor->getParent());
1166 // Zero initialize the storage, if requested.
1168 EmitNullInitialization(cur, type);
1170 // C++ [class.temporary]p4:
1171 // There are two contexts in which temporaries are destroyed at a different
1172 // point than the end of the full-expression. The first context is when a
1173 // default constructor is called to initialize an element of an array.
1174 // If the constructor has one or more default arguments, the destruction of
1175 // every temporary created in a default argument expression is sequenced
1176 // before the construction of the next array element, if any.
1179 RunCleanupsScope Scope(*this);
1181 // Evaluate the constructor and its arguments in a regular
1182 // partial-destroy cleanup.
1183 if (getLangOpts().Exceptions &&
1184 !ctor->getParent()->hasTrivialDestructor()) {
1185 Destroyer *destroyer = destroyCXXObject;
1186 pushRegularPartialArrayCleanup(arrayBegin, cur, type, *destroyer);
1189 EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/ false,
1190 cur, argBegin, argEnd);
1193 // Go to the next element.
1195 Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1),
1197 cur->addIncoming(next, Builder.GetInsertBlock());
1199 // Check whether that's the end of the loop.
1200 llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done");
1201 llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont");
1202 Builder.CreateCondBr(done, contBB, loopBB);
1204 // Patch the earlier check to skip over the loop.
1205 if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
1210 void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF,
1213 const RecordType *rtype = type->castAs<RecordType>();
1214 const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl());
1215 const CXXDestructorDecl *dtor = record->getDestructor();
1216 assert(!dtor->isTrivial());
1217 CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false,
1222 CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
1223 CXXCtorType Type, bool ForVirtualBase,
1225 CallExpr::const_arg_iterator ArgBeg,
1226 CallExpr::const_arg_iterator ArgEnd) {
1228 CGDebugInfo *DI = getDebugInfo();
1229 if (DI && CGM.getCodeGenOpts().LimitDebugInfo) {
1230 // If debug info for this class has not been emitted then this is the
1231 // right time to do so.
1232 const CXXRecordDecl *Parent = D->getParent();
1233 DI->getOrCreateRecordType(CGM.getContext().getTypeDeclType(Parent),
1234 Parent->getLocation());
1237 if (D->isTrivial()) {
1238 if (ArgBeg == ArgEnd) {
1239 // Trivial default constructor, no codegen required.
1240 assert(D->isDefaultConstructor() &&
1241 "trivial 0-arg ctor not a default ctor");
1245 assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor");
1246 assert(D->isCopyOrMoveConstructor() &&
1247 "trivial 1-arg ctor not a copy/move ctor");
1249 const Expr *E = (*ArgBeg);
1250 QualType Ty = E->getType();
1251 llvm::Value *Src = EmitLValue(E).getAddress();
1252 EmitAggregateCopy(This, Src, Ty);
1256 llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(D, Type), ForVirtualBase);
1257 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
1259 EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, VTT, ArgBeg, ArgEnd);
1263 CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1264 llvm::Value *This, llvm::Value *Src,
1265 CallExpr::const_arg_iterator ArgBeg,
1266 CallExpr::const_arg_iterator ArgEnd) {
1267 if (D->isTrivial()) {
1268 assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor");
1269 assert(D->isCopyOrMoveConstructor() &&
1270 "trivial 1-arg ctor not a copy/move ctor");
1271 EmitAggregateCopy(This, Src, (*ArgBeg)->getType());
1274 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D,
1275 clang::Ctor_Complete);
1276 assert(D->isInstance() &&
1277 "Trying to emit a member call expr on a static method!");
1279 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
1283 // Push the this ptr.
1284 Args.add(RValue::get(This), D->getThisType(getContext()));
1287 // Push the src ptr.
1288 QualType QT = *(FPT->arg_type_begin());
1289 llvm::Type *t = CGM.getTypes().ConvertType(QT);
1290 Src = Builder.CreateBitCast(Src, t);
1291 Args.add(RValue::get(Src), QT);
1293 // Skip over first argument (Src).
1295 CallExpr::const_arg_iterator Arg = ArgBeg;
1296 for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin()+1,
1297 E = FPT->arg_type_end(); I != E; ++I, ++Arg) {
1298 assert(Arg != ArgEnd && "Running over edge of argument list!");
1299 EmitCallArg(Args, *Arg, *I);
1301 // Either we've emitted all the call args, or we have a call to a
1302 // variadic function.
1303 assert((Arg == ArgEnd || FPT->isVariadic()) &&
1304 "Extra arguments in non-variadic function!");
1305 // If we still have any arguments, emit them using the type of the argument.
1306 for (; Arg != ArgEnd; ++Arg) {
1307 QualType ArgType = Arg->getType();
1308 EmitCallArg(Args, *Arg, ArgType);
1311 EmitCall(CGM.getTypes().arrangeFunctionCall(Args, FPT), Callee,
1312 ReturnValueSlot(), Args, D);
1316 CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1317 CXXCtorType CtorType,
1318 const FunctionArgList &Args) {
1319 CallArgList DelegateArgs;
1321 FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
1322 assert(I != E && "no parameters to constructor");
1325 DelegateArgs.add(RValue::get(LoadCXXThis()), (*I)->getType());
1329 if (llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(Ctor, CtorType),
1330 /*ForVirtualBase=*/false)) {
1331 QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy);
1332 DelegateArgs.add(RValue::get(VTT), VoidPP);
1334 if (CodeGenVTables::needsVTTParameter(CurGD)) {
1335 assert(I != E && "cannot skip vtt parameter, already done with args");
1336 assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type");
1341 // Explicit arguments.
1342 for (; I != E; ++I) {
1343 const VarDecl *param = *I;
1344 EmitDelegateCallArg(DelegateArgs, param);
1347 EmitCall(CGM.getTypes().arrangeCXXConstructorDeclaration(Ctor, CtorType),
1348 CGM.GetAddrOfCXXConstructor(Ctor, CtorType),
1349 ReturnValueSlot(), DelegateArgs, Ctor);
1353 struct CallDelegatingCtorDtor : EHScopeStack::Cleanup {
1354 const CXXDestructorDecl *Dtor;
1358 CallDelegatingCtorDtor(const CXXDestructorDecl *D, llvm::Value *Addr,
1360 : Dtor(D), Addr(Addr), Type(Type) {}
1362 void Emit(CodeGenFunction &CGF, Flags flags) {
1363 CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false,
1370 CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1371 const FunctionArgList &Args) {
1372 assert(Ctor->isDelegatingConstructor());
1374 llvm::Value *ThisPtr = LoadCXXThis();
1376 QualType Ty = getContext().getTagDeclType(Ctor->getParent());
1377 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
1378 AggValueSlot AggSlot =
1379 AggValueSlot::forAddr(ThisPtr, Alignment, Qualifiers(),
1380 AggValueSlot::IsDestructed,
1381 AggValueSlot::DoesNotNeedGCBarriers,
1382 AggValueSlot::IsNotAliased);
1384 EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot);
1386 const CXXRecordDecl *ClassDecl = Ctor->getParent();
1387 if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) {
1389 CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base;
1391 EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup,
1392 ClassDecl->getDestructor(),
1397 void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD,
1399 bool ForVirtualBase,
1400 llvm::Value *This) {
1401 llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(DD, Type),
1403 llvm::Value *Callee = 0;
1404 if (getContext().getLangOpts().AppleKext)
1405 Callee = BuildAppleKextVirtualDestructorCall(DD, Type,
1409 Callee = CGM.GetAddrOfCXXDestructor(DD, Type);
1411 EmitCXXMemberCall(DD, Callee, ReturnValueSlot(), This, VTT, 0, 0);
1415 struct CallLocalDtor : EHScopeStack::Cleanup {
1416 const CXXDestructorDecl *Dtor;
1419 CallLocalDtor(const CXXDestructorDecl *D, llvm::Value *Addr)
1420 : Dtor(D), Addr(Addr) {}
1422 void Emit(CodeGenFunction &CGF, Flags flags) {
1423 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1424 /*ForVirtualBase=*/false, Addr);
1429 void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D,
1430 llvm::Value *Addr) {
1431 EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr);
1434 void CodeGenFunction::PushDestructorCleanup(QualType T, llvm::Value *Addr) {
1435 CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl();
1436 if (!ClassDecl) return;
1437 if (ClassDecl->hasTrivialDestructor()) return;
1439 const CXXDestructorDecl *D = ClassDecl->getDestructor();
1440 assert(D && D->isUsed() && "destructor not marked as used!");
1441 PushDestructorCleanup(D, Addr);
1445 CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This,
1446 const CXXRecordDecl *ClassDecl,
1447 const CXXRecordDecl *BaseClassDecl) {
1448 llvm::Value *VTablePtr = GetVTablePtr(This, Int8PtrTy);
1449 CharUnits VBaseOffsetOffset =
1450 CGM.getVTableContext().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl);
1452 llvm::Value *VBaseOffsetPtr =
1453 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1454 "vbase.offset.ptr");
1455 llvm::Type *PtrDiffTy =
1456 ConvertType(getContext().getPointerDiffType());
1458 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
1459 PtrDiffTy->getPointerTo());
1461 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1467 CodeGenFunction::InitializeVTablePointer(BaseSubobject Base,
1468 const CXXRecordDecl *NearestVBase,
1469 CharUnits OffsetFromNearestVBase,
1470 llvm::Constant *VTable,
1471 const CXXRecordDecl *VTableClass) {
1472 const CXXRecordDecl *RD = Base.getBase();
1474 // Compute the address point.
1475 llvm::Value *VTableAddressPoint;
1477 // Check if we need to use a vtable from the VTT.
1478 if (CodeGenVTables::needsVTTParameter(CurGD) &&
1479 (RD->getNumVBases() || NearestVBase)) {
1480 // Get the secondary vpointer index.
1481 uint64_t VirtualPointerIndex =
1482 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1485 llvm::Value *VTT = LoadCXXVTT();
1486 if (VirtualPointerIndex)
1487 VTT = Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1489 // And load the address point from the VTT.
1490 VTableAddressPoint = Builder.CreateLoad(VTT);
1492 uint64_t AddressPoint =
1493 CGM.getVTableContext().getVTableLayout(VTableClass).getAddressPoint(Base);
1494 VTableAddressPoint =
1495 Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint);
1498 // Compute where to store the address point.
1499 llvm::Value *VirtualOffset = 0;
1500 CharUnits NonVirtualOffset = CharUnits::Zero();
1502 if (CodeGenVTables::needsVTTParameter(CurGD) && NearestVBase) {
1503 // We need to use the virtual base offset offset because the virtual base
1504 // might have a different offset in the most derived class.
1505 VirtualOffset = GetVirtualBaseClassOffset(LoadCXXThis(), VTableClass,
1507 NonVirtualOffset = OffsetFromNearestVBase;
1509 // We can just use the base offset in the complete class.
1510 NonVirtualOffset = Base.getBaseOffset();
1513 // Apply the offsets.
1514 llvm::Value *VTableField = LoadCXXThis();
1516 if (!NonVirtualOffset.isZero() || VirtualOffset)
1517 VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField,
1521 // Finally, store the address point.
1522 llvm::Type *AddressPointPtrTy =
1523 VTableAddressPoint->getType()->getPointerTo();
1524 VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy);
1525 llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField);
1526 CGM.DecorateInstruction(Store, CGM.getTBAAInfoForVTablePtr());
1530 CodeGenFunction::InitializeVTablePointers(BaseSubobject Base,
1531 const CXXRecordDecl *NearestVBase,
1532 CharUnits OffsetFromNearestVBase,
1533 bool BaseIsNonVirtualPrimaryBase,
1534 llvm::Constant *VTable,
1535 const CXXRecordDecl *VTableClass,
1536 VisitedVirtualBasesSetTy& VBases) {
1537 // If this base is a non-virtual primary base the address point has already
1539 if (!BaseIsNonVirtualPrimaryBase) {
1540 // Initialize the vtable pointer for this base.
1541 InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase,
1542 VTable, VTableClass);
1545 const CXXRecordDecl *RD = Base.getBase();
1548 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1549 E = RD->bases_end(); I != E; ++I) {
1550 CXXRecordDecl *BaseDecl
1551 = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1553 // Ignore classes without a vtable.
1554 if (!BaseDecl->isDynamicClass())
1557 CharUnits BaseOffset;
1558 CharUnits BaseOffsetFromNearestVBase;
1559 bool BaseDeclIsNonVirtualPrimaryBase;
1561 if (I->isVirtual()) {
1562 // Check if we've visited this virtual base before.
1563 if (!VBases.insert(BaseDecl))
1566 const ASTRecordLayout &Layout =
1567 getContext().getASTRecordLayout(VTableClass);
1569 BaseOffset = Layout.getVBaseClassOffset(BaseDecl);
1570 BaseOffsetFromNearestVBase = CharUnits::Zero();
1571 BaseDeclIsNonVirtualPrimaryBase = false;
1573 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1575 BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl);
1576 BaseOffsetFromNearestVBase =
1577 OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl);
1578 BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl;
1581 InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset),
1582 I->isVirtual() ? BaseDecl : NearestVBase,
1583 BaseOffsetFromNearestVBase,
1584 BaseDeclIsNonVirtualPrimaryBase,
1585 VTable, VTableClass, VBases);
1589 void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) {
1590 // Ignore classes without a vtable.
1591 if (!RD->isDynamicClass())
1595 llvm::Constant *VTable = CGM.getVTables().GetAddrOfVTable(RD);
1597 // Initialize the vtable pointers for this class and all of its bases.
1598 VisitedVirtualBasesSetTy VBases;
1599 InitializeVTablePointers(BaseSubobject(RD, CharUnits::Zero()),
1601 /*OffsetFromNearestVBase=*/CharUnits::Zero(),
1602 /*BaseIsNonVirtualPrimaryBase=*/false,
1603 VTable, RD, VBases);
1606 llvm::Value *CodeGenFunction::GetVTablePtr(llvm::Value *This,
1608 llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo());
1609 llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable");
1610 CGM.DecorateInstruction(VTable, CGM.getTBAAInfoForVTablePtr());
1614 static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) {
1615 const Expr *E = Base;
1618 E = E->IgnoreParens();
1619 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
1620 if (CE->getCastKind() == CK_DerivedToBase ||
1621 CE->getCastKind() == CK_UncheckedDerivedToBase ||
1622 CE->getCastKind() == CK_NoOp) {
1623 E = CE->getSubExpr();
1631 QualType DerivedType = E->getType();
1632 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
1633 DerivedType = PTy->getPointeeType();
1635 return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl());
1638 // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
1639 // quite what we want.
1640 static const Expr *skipNoOpCastsAndParens(const Expr *E) {
1642 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
1643 E = PE->getSubExpr();
1647 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
1648 if (CE->getCastKind() == CK_NoOp) {
1649 E = CE->getSubExpr();
1653 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1654 if (UO->getOpcode() == UO_Extension) {
1655 E = UO->getSubExpr();
1663 /// canDevirtualizeMemberFunctionCall - Checks whether the given virtual member
1664 /// function call on the given expr can be devirtualized.
1665 static bool canDevirtualizeMemberFunctionCall(const Expr *Base,
1666 const CXXMethodDecl *MD) {
1667 // If the most derived class is marked final, we know that no subclass can
1668 // override this member function and so we can devirtualize it. For example:
1670 // struct A { virtual void f(); }
1671 // struct B final : A { };
1677 const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base);
1678 if (MostDerivedClassDecl->hasAttr<FinalAttr>())
1681 // If the member function is marked 'final', we know that it can't be
1682 // overridden and can therefore devirtualize it.
1683 if (MD->hasAttr<FinalAttr>())
1686 // Similarly, if the class itself is marked 'final' it can't be overridden
1687 // and we can therefore devirtualize the member function call.
1688 if (MD->getParent()->hasAttr<FinalAttr>())
1691 Base = skipNoOpCastsAndParens(Base);
1692 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
1693 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
1694 // This is a record decl. We know the type and can devirtualize it.
1695 return VD->getType()->isRecordType();
1701 // We can always devirtualize calls on temporary object expressions.
1702 if (isa<CXXConstructExpr>(Base))
1705 // And calls on bound temporaries.
1706 if (isa<CXXBindTemporaryExpr>(Base))
1709 // Check if this is a call expr that returns a record type.
1710 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
1711 return CE->getCallReturnType()->isRecordType();
1713 // We can't devirtualize the call.
1717 static bool UseVirtualCall(ASTContext &Context,
1718 const CXXOperatorCallExpr *CE,
1719 const CXXMethodDecl *MD) {
1720 if (!MD->isVirtual())
1723 // When building with -fapple-kext, all calls must go through the vtable since
1724 // the kernel linker can do runtime patching of vtables.
1725 if (Context.getLangOpts().AppleKext)
1728 return !canDevirtualizeMemberFunctionCall(CE->getArg(0), MD);
1732 CodeGenFunction::EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E,
1733 const CXXMethodDecl *MD,
1734 llvm::Value *This) {
1735 llvm::FunctionType *fnType =
1736 CGM.getTypes().GetFunctionType(
1737 CGM.getTypes().arrangeCXXMethodDeclaration(MD));
1739 if (UseVirtualCall(getContext(), E, MD))
1740 return BuildVirtualCall(MD, This, fnType);
1742 return CGM.GetAddrOfFunction(MD, fnType);
1745 void CodeGenFunction::EmitForwardingCallToLambda(const CXXRecordDecl *Lambda,
1746 CallArgList &CallArgs) {
1747 // Lookup the call operator
1748 DeclarationName Name
1749 = getContext().DeclarationNames.getCXXOperatorName(OO_Call);
1750 DeclContext::lookup_const_result Calls = Lambda->lookup(Name);
1751 CXXMethodDecl *CallOperator = cast<CXXMethodDecl>(*Calls.first++);
1752 const FunctionProtoType *FPT =
1753 CallOperator->getType()->getAs<FunctionProtoType>();
1754 QualType ResultType = FPT->getResultType();
1756 // Get the address of the call operator.
1757 GlobalDecl GD(CallOperator);
1758 const CGFunctionInfo &CalleeFnInfo =
1759 CGM.getTypes().arrangeFunctionCall(ResultType, CallArgs, FPT->getExtInfo(),
1760 RequiredArgs::forPrototypePlus(FPT, 1));
1761 llvm::Type *Ty = CGM.getTypes().GetFunctionType(CalleeFnInfo);
1762 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
1764 // Determine whether we have a return value slot to use.
1765 ReturnValueSlot Slot;
1766 if (!ResultType->isVoidType() &&
1767 CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
1768 hasAggregateLLVMType(CurFnInfo->getReturnType()))
1769 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
1771 // Now emit our call.
1772 RValue RV = EmitCall(CalleeFnInfo, Callee, Slot, CallArgs, CallOperator);
1774 // Forward the returned value
1775 if (!ResultType->isVoidType() && Slot.isNull())
1776 EmitReturnOfRValue(RV, ResultType);
1779 void CodeGenFunction::EmitLambdaBlockInvokeBody() {
1780 const BlockDecl *BD = BlockInfo->getBlockDecl();
1781 const VarDecl *variable = BD->capture_begin()->getVariable();
1782 const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl();
1784 // Start building arguments for forwarding call
1785 CallArgList CallArgs;
1787 QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
1788 llvm::Value *ThisPtr = GetAddrOfBlockDecl(variable, false);
1789 CallArgs.add(RValue::get(ThisPtr), ThisType);
1791 // Add the rest of the parameters.
1792 for (BlockDecl::param_const_iterator I = BD->param_begin(),
1793 E = BD->param_end(); I != E; ++I) {
1794 ParmVarDecl *param = *I;
1795 EmitDelegateCallArg(CallArgs, param);
1798 EmitForwardingCallToLambda(Lambda, CallArgs);
1801 void CodeGenFunction::EmitLambdaToBlockPointerBody(FunctionArgList &Args) {
1802 if (cast<CXXMethodDecl>(CurFuncDecl)->isVariadic()) {
1803 // FIXME: Making this work correctly is nasty because it requires either
1804 // cloning the body of the call operator or making the call operator forward.
1805 CGM.ErrorUnsupported(CurFuncDecl, "lambda conversion to variadic function");
1809 EmitFunctionBody(Args);
1812 void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD) {
1813 const CXXRecordDecl *Lambda = MD->getParent();
1815 // Start building arguments for forwarding call
1816 CallArgList CallArgs;
1818 QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
1819 llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType));
1820 CallArgs.add(RValue::get(ThisPtr), ThisType);
1822 // Add the rest of the parameters.
1823 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
1824 E = MD->param_end(); I != E; ++I) {
1825 ParmVarDecl *param = *I;
1826 EmitDelegateCallArg(CallArgs, param);
1829 EmitForwardingCallToLambda(Lambda, CallArgs);
1832 void CodeGenFunction::EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD) {
1833 if (MD->isVariadic()) {
1834 // FIXME: Making this work correctly is nasty because it requires either
1835 // cloning the body of the call operator or making the call operator forward.
1836 CGM.ErrorUnsupported(MD, "lambda conversion to variadic function");
1840 EmitLambdaDelegatingInvokeBody(MD);