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 "CGRecordLayout.h"
17 #include "CodeGenFunction.h"
19 #include "clang/AST/CXXInheritance.h"
20 #include "clang/AST/EvaluatedExprVisitor.h"
21 #include "clang/AST/RecordLayout.h"
22 #include "clang/AST/StmtCXX.h"
23 #include "clang/Basic/TargetBuiltins.h"
24 #include "clang/Frontend/CodeGenOptions.h"
26 using namespace clang;
27 using namespace CodeGen;
30 ComputeNonVirtualBaseClassOffset(ASTContext &Context,
31 const CXXRecordDecl *DerivedClass,
32 CastExpr::path_const_iterator Start,
33 CastExpr::path_const_iterator End) {
34 CharUnits Offset = CharUnits::Zero();
36 const CXXRecordDecl *RD = DerivedClass;
38 for (CastExpr::path_const_iterator I = Start; I != End; ++I) {
39 const CXXBaseSpecifier *Base = *I;
40 assert(!Base->isVirtual() && "Should not see virtual bases here!");
43 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
45 const CXXRecordDecl *BaseDecl =
46 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
49 Offset += Layout.getBaseClassOffset(BaseDecl);
58 CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
59 CastExpr::path_const_iterator PathBegin,
60 CastExpr::path_const_iterator PathEnd) {
61 assert(PathBegin != PathEnd && "Base path should not be empty!");
64 ComputeNonVirtualBaseClassOffset(getContext(), ClassDecl,
69 llvm::Type *PtrDiffTy =
70 Types.ConvertType(getContext().getPointerDiffType());
72 return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
75 /// Gets the address of a direct base class within a complete object.
76 /// This should only be used for (1) non-virtual bases or (2) virtual bases
77 /// when the type is known to be complete (e.g. in complete destructors).
79 /// The object pointed to by 'This' is assumed to be non-null.
81 CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(llvm::Value *This,
82 const CXXRecordDecl *Derived,
83 const CXXRecordDecl *Base,
85 // 'this' must be a pointer (in some address space) to Derived.
86 assert(This->getType()->isPointerTy() &&
87 cast<llvm::PointerType>(This->getType())->getElementType()
88 == ConvertType(Derived));
90 // Compute the offset of the virtual base.
92 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
94 Offset = Layout.getVBaseClassOffset(Base);
96 Offset = Layout.getBaseClassOffset(Base);
98 // Shift and cast down to the base type.
99 // TODO: for complete types, this should be possible with a GEP.
100 llvm::Value *V = This;
101 if (Offset.isPositive()) {
102 V = Builder.CreateBitCast(V, Int8PtrTy);
103 V = Builder.CreateConstInBoundsGEP1_64(V, Offset.getQuantity());
105 V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo());
111 ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ptr,
112 CharUnits nonVirtualOffset,
113 llvm::Value *virtualOffset) {
114 // Assert that we have something to do.
115 assert(!nonVirtualOffset.isZero() || virtualOffset != 0);
117 // Compute the offset from the static and dynamic components.
118 llvm::Value *baseOffset;
119 if (!nonVirtualOffset.isZero()) {
120 baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy,
121 nonVirtualOffset.getQuantity());
123 baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset);
126 baseOffset = virtualOffset;
129 // Apply the base offset.
130 ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
131 ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr");
136 CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value,
137 const CXXRecordDecl *Derived,
138 CastExpr::path_const_iterator PathBegin,
139 CastExpr::path_const_iterator PathEnd,
140 bool NullCheckValue) {
141 assert(PathBegin != PathEnd && "Base path should not be empty!");
143 CastExpr::path_const_iterator Start = PathBegin;
144 const CXXRecordDecl *VBase = 0;
146 // Sema has done some convenient canonicalization here: if the
147 // access path involved any virtual steps, the conversion path will
148 // *start* with a step down to the correct virtual base subobject,
149 // and hence will not require any further steps.
150 if ((*Start)->isVirtual()) {
152 cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl());
156 // Compute the static offset of the ultimate destination within its
157 // allocating subobject (the virtual base, if there is one, or else
158 // the "complete" object that we see).
159 CharUnits NonVirtualOffset =
160 ComputeNonVirtualBaseClassOffset(getContext(), VBase ? VBase : Derived,
163 // If there's a virtual step, we can sometimes "devirtualize" it.
164 // For now, that's limited to when the derived type is final.
165 // TODO: "devirtualize" this for accesses to known-complete objects.
166 if (VBase && Derived->hasAttr<FinalAttr>()) {
167 const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived);
168 CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase);
169 NonVirtualOffset += vBaseOffset;
170 VBase = 0; // we no longer have a virtual step
173 // Get the base pointer type.
174 llvm::Type *BasePtrTy =
175 ConvertType((PathEnd[-1])->getType())->getPointerTo();
177 // If the static offset is zero and we don't have a virtual step,
178 // just do a bitcast; null checks are unnecessary.
179 if (NonVirtualOffset.isZero() && !VBase) {
180 return Builder.CreateBitCast(Value, BasePtrTy);
183 llvm::BasicBlock *origBB = 0;
184 llvm::BasicBlock *endBB = 0;
186 // Skip over the offset (and the vtable load) if we're supposed to
187 // null-check the pointer.
188 if (NullCheckValue) {
189 origBB = Builder.GetInsertBlock();
190 llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull");
191 endBB = createBasicBlock("cast.end");
193 llvm::Value *isNull = Builder.CreateIsNull(Value);
194 Builder.CreateCondBr(isNull, endBB, notNullBB);
195 EmitBlock(notNullBB);
198 // Compute the virtual offset.
199 llvm::Value *VirtualOffset = 0;
201 VirtualOffset = GetVirtualBaseClassOffset(Value, Derived, VBase);
204 // Apply both offsets.
205 Value = ApplyNonVirtualAndVirtualOffset(*this, Value,
209 // Cast to the destination type.
210 Value = Builder.CreateBitCast(Value, BasePtrTy);
212 // Build a phi if we needed a null check.
213 if (NullCheckValue) {
214 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
215 Builder.CreateBr(endBB);
218 llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result");
219 PHI->addIncoming(Value, notNullBB);
220 PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB);
228 CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value,
229 const CXXRecordDecl *Derived,
230 CastExpr::path_const_iterator PathBegin,
231 CastExpr::path_const_iterator PathEnd,
232 bool NullCheckValue) {
233 assert(PathBegin != PathEnd && "Base path should not be empty!");
236 getContext().getCanonicalType(getContext().getTagDeclType(Derived));
237 llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo();
239 llvm::Value *NonVirtualOffset =
240 CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd);
242 if (!NonVirtualOffset) {
243 // No offset, we can just cast back.
244 return Builder.CreateBitCast(Value, DerivedPtrTy);
247 llvm::BasicBlock *CastNull = 0;
248 llvm::BasicBlock *CastNotNull = 0;
249 llvm::BasicBlock *CastEnd = 0;
251 if (NullCheckValue) {
252 CastNull = createBasicBlock("cast.null");
253 CastNotNull = createBasicBlock("cast.notnull");
254 CastEnd = createBasicBlock("cast.end");
256 llvm::Value *IsNull = Builder.CreateIsNull(Value);
257 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
258 EmitBlock(CastNotNull);
262 Value = Builder.CreateBitCast(Value, Int8PtrTy);
263 Value = Builder.CreateGEP(Value, Builder.CreateNeg(NonVirtualOffset),
267 Value = Builder.CreateBitCast(Value, DerivedPtrTy);
269 if (NullCheckValue) {
270 Builder.CreateBr(CastEnd);
272 Builder.CreateBr(CastEnd);
275 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
276 PHI->addIncoming(Value, CastNotNull);
277 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()),
285 llvm::Value *CodeGenFunction::GetVTTParameter(GlobalDecl GD,
288 if (!CodeGenVTables::needsVTTParameter(GD)) {
289 // This constructor/destructor does not need a VTT parameter.
293 const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent();
294 const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent();
298 uint64_t SubVTTIndex;
301 // If this is a delegating constructor call, just load the VTT.
303 } else if (RD == Base) {
304 // If the record matches the base, this is the complete ctor/dtor
305 // variant calling the base variant in a class with virtual bases.
306 assert(!CodeGenVTables::needsVTTParameter(CurGD) &&
307 "doing no-op VTT offset in base dtor/ctor?");
308 assert(!ForVirtualBase && "Can't have same class as virtual base!");
311 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
312 CharUnits BaseOffset = ForVirtualBase ?
313 Layout.getVBaseClassOffset(Base) :
314 Layout.getBaseClassOffset(Base);
317 CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset));
318 assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
321 if (CodeGenVTables::needsVTTParameter(CurGD)) {
322 // A VTT parameter was passed to the constructor, use it.
324 VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex);
326 // We're the complete constructor, so get the VTT by name.
327 VTT = CGM.getVTables().GetAddrOfVTT(RD);
328 VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex);
335 /// Call the destructor for a direct base class.
336 struct CallBaseDtor : EHScopeStack::Cleanup {
337 const CXXRecordDecl *BaseClass;
339 CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual)
340 : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
342 void Emit(CodeGenFunction &CGF, Flags flags) {
343 const CXXRecordDecl *DerivedClass =
344 cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
346 const CXXDestructorDecl *D = BaseClass->getDestructor();
348 CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThis(),
349 DerivedClass, BaseClass,
351 CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual,
352 /*Delegating=*/false, Addr);
356 /// A visitor which checks whether an initializer uses 'this' in a
357 /// way which requires the vtable to be properly set.
358 struct DynamicThisUseChecker : EvaluatedExprVisitor<DynamicThisUseChecker> {
359 typedef EvaluatedExprVisitor<DynamicThisUseChecker> super;
363 DynamicThisUseChecker(ASTContext &C) : super(C), UsesThis(false) {}
365 // Black-list all explicit and implicit references to 'this'.
367 // Do we need to worry about external references to 'this' derived
368 // from arbitrary code? If so, then anything which runs arbitrary
369 // external code might potentially access the vtable.
370 void VisitCXXThisExpr(CXXThisExpr *E) { UsesThis = true; }
374 static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) {
375 DynamicThisUseChecker Checker(C);
376 Checker.Visit(const_cast<Expr*>(Init));
377 return Checker.UsesThis;
380 static void EmitBaseInitializer(CodeGenFunction &CGF,
381 const CXXRecordDecl *ClassDecl,
382 CXXCtorInitializer *BaseInit,
383 CXXCtorType CtorType) {
384 assert(BaseInit->isBaseInitializer() &&
385 "Must have base initializer!");
387 llvm::Value *ThisPtr = CGF.LoadCXXThis();
389 const Type *BaseType = BaseInit->getBaseClass();
390 CXXRecordDecl *BaseClassDecl =
391 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
393 bool isBaseVirtual = BaseInit->isBaseVirtual();
395 // The base constructor doesn't construct virtual bases.
396 if (CtorType == Ctor_Base && isBaseVirtual)
399 // If the initializer for the base (other than the constructor
400 // itself) accesses 'this' in any way, we need to initialize the
402 if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit()))
403 CGF.InitializeVTablePointers(ClassDecl);
405 // We can pretend to be a complete class because it only matters for
406 // virtual bases, and we only do virtual bases for complete ctors.
408 CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl,
411 CharUnits Alignment = CGF.getContext().getTypeAlignInChars(BaseType);
412 AggValueSlot AggSlot =
413 AggValueSlot::forAddr(V, Alignment, Qualifiers(),
414 AggValueSlot::IsDestructed,
415 AggValueSlot::DoesNotNeedGCBarriers,
416 AggValueSlot::IsNotAliased);
418 CGF.EmitAggExpr(BaseInit->getInit(), AggSlot);
420 if (CGF.CGM.getLangOpts().Exceptions &&
421 !BaseClassDecl->hasTrivialDestructor())
422 CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl,
426 static void EmitAggMemberInitializer(CodeGenFunction &CGF,
429 llvm::Value *ArrayIndexVar,
431 ArrayRef<VarDecl *> ArrayIndexes,
433 if (Index == ArrayIndexes.size()) {
435 { // Scope for Cleanups.
436 CodeGenFunction::RunCleanupsScope Cleanups(CGF);
439 // If we have an array index variable, load it and use it as an offset.
440 // Then, increment the value.
441 llvm::Value *Dest = LHS.getAddress();
442 llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar);
443 Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress");
444 llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1);
445 Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc");
446 CGF.Builder.CreateStore(Next, ArrayIndexVar);
448 // Update the LValue.
450 CharUnits Align = CGF.getContext().getTypeAlignInChars(T);
451 LV.setAlignment(std::min(Align, LV.getAlignment()));
454 switch (CGF.getEvaluationKind(T)) {
456 CGF.EmitScalarInit(Init, /*decl*/ 0, LV, false);
459 CGF.EmitComplexExprIntoLValue(Init, LV, /*isInit*/ true);
461 case TEK_Aggregate: {
463 AggValueSlot::forLValue(LV,
464 AggValueSlot::IsDestructed,
465 AggValueSlot::DoesNotNeedGCBarriers,
466 AggValueSlot::IsNotAliased);
468 CGF.EmitAggExpr(Init, Slot);
474 // Now, outside of the initializer cleanup scope, destroy the backing array
475 // for a std::initializer_list member.
476 CGF.MaybeEmitStdInitializerListCleanup(LV.getAddress(), Init);
481 const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T);
482 assert(Array && "Array initialization without the array type?");
483 llvm::Value *IndexVar
484 = CGF.GetAddrOfLocalVar(ArrayIndexes[Index]);
485 assert(IndexVar && "Array index variable not loaded");
487 // Initialize this index variable to zero.
489 = llvm::Constant::getNullValue(
490 CGF.ConvertType(CGF.getContext().getSizeType()));
491 CGF.Builder.CreateStore(Zero, IndexVar);
493 // Start the loop with a block that tests the condition.
494 llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond");
495 llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end");
497 CGF.EmitBlock(CondBlock);
499 llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body");
500 // Generate: if (loop-index < number-of-elements) fall to the loop body,
501 // otherwise, go to the block after the for-loop.
502 uint64_t NumElements = Array->getSize().getZExtValue();
503 llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar);
504 llvm::Value *NumElementsPtr =
505 llvm::ConstantInt::get(Counter->getType(), NumElements);
506 llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr,
509 // If the condition is true, execute the body.
510 CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor);
512 CGF.EmitBlock(ForBody);
513 llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc");
516 CodeGenFunction::RunCleanupsScope Cleanups(CGF);
518 // Inside the loop body recurse to emit the inner loop or, eventually, the
520 EmitAggMemberInitializer(CGF, LHS, Init, ArrayIndexVar,
521 Array->getElementType(), ArrayIndexes, Index + 1);
524 CGF.EmitBlock(ContinueBlock);
526 // Emit the increment of the loop counter.
527 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
528 Counter = CGF.Builder.CreateLoad(IndexVar);
529 NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc");
530 CGF.Builder.CreateStore(NextVal, IndexVar);
532 // Finally, branch back up to the condition for the next iteration.
533 CGF.EmitBranch(CondBlock);
535 // Emit the fall-through block.
536 CGF.EmitBlock(AfterFor, true);
539 static void EmitMemberInitializer(CodeGenFunction &CGF,
540 const CXXRecordDecl *ClassDecl,
541 CXXCtorInitializer *MemberInit,
542 const CXXConstructorDecl *Constructor,
543 FunctionArgList &Args) {
544 assert(MemberInit->isAnyMemberInitializer() &&
545 "Must have member initializer!");
546 assert(MemberInit->getInit() && "Must have initializer!");
548 // non-static data member initializers.
549 FieldDecl *Field = MemberInit->getAnyMember();
550 QualType FieldType = Field->getType();
552 llvm::Value *ThisPtr = CGF.LoadCXXThis();
553 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
554 LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
556 if (MemberInit->isIndirectMemberInitializer()) {
557 // If we are initializing an anonymous union field, drill down to
559 IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember();
560 IndirectFieldDecl::chain_iterator I = IndirectField->chain_begin(),
561 IEnd = IndirectField->chain_end();
562 for ( ; I != IEnd; ++I)
563 LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(*I));
564 FieldType = MemberInit->getIndirectMember()->getAnonField()->getType();
566 LHS = CGF.EmitLValueForFieldInitialization(LHS, Field);
569 // Special case: if we are in a copy or move constructor, and we are copying
570 // an array of PODs or classes with trivial copy constructors, ignore the
571 // AST and perform the copy we know is equivalent.
572 // FIXME: This is hacky at best... if we had a bit more explicit information
573 // in the AST, we could generalize it more easily.
574 const ConstantArrayType *Array
575 = CGF.getContext().getAsConstantArrayType(FieldType);
576 if (Array && Constructor->isImplicitlyDefined() &&
577 Constructor->isCopyOrMoveConstructor()) {
578 QualType BaseElementTy = CGF.getContext().getBaseElementType(Array);
579 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
580 if (BaseElementTy.isPODType(CGF.getContext()) ||
581 (CE && CE->getConstructor()->isTrivial())) {
582 // Find the source pointer. We know it's the last argument because
583 // we know we're in an implicit copy constructor.
584 unsigned SrcArgIndex = Args.size() - 1;
586 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex]));
587 LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
588 LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field);
590 // Copy the aggregate.
591 CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType,
592 LHS.isVolatileQualified());
597 ArrayRef<VarDecl *> ArrayIndexes;
598 if (MemberInit->getNumArrayIndices())
599 ArrayIndexes = MemberInit->getArrayIndexes();
600 CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit(), ArrayIndexes);
603 void CodeGenFunction::EmitInitializerForField(FieldDecl *Field,
604 LValue LHS, Expr *Init,
605 ArrayRef<VarDecl *> ArrayIndexes) {
606 QualType FieldType = Field->getType();
607 switch (getEvaluationKind(FieldType)) {
609 if (LHS.isSimple()) {
610 EmitExprAsInit(Init, Field, LHS, false);
612 RValue RHS = RValue::get(EmitScalarExpr(Init));
613 EmitStoreThroughLValue(RHS, LHS);
617 EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true);
619 case TEK_Aggregate: {
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,
650 // Ensure that we destroy this object if an exception is thrown
651 // later in the constructor.
652 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
653 if (needsEHCleanup(dtorKind))
654 pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
657 /// Checks whether the given constructor is a valid subject for the
658 /// complete-to-base constructor delegation optimization, i.e.
659 /// emitting the complete constructor as a simple call to the base
661 static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) {
663 // Currently we disable the optimization for classes with virtual
664 // bases because (1) the addresses of parameter variables need to be
665 // consistent across all initializers but (2) the delegate function
666 // call necessarily creates a second copy of the parameter variable.
668 // The limiting example (purely theoretical AFAIK):
669 // struct A { A(int &c) { c++; } };
670 // struct B : virtual A {
671 // B(int count) : A(count) { printf("%d\n", count); }
673 // ...although even this example could in principle be emitted as a
674 // delegation since the address of the parameter doesn't escape.
675 if (Ctor->getParent()->getNumVBases()) {
676 // TODO: white-list trivial vbase initializers. This case wouldn't
677 // be subject to the restrictions below.
679 // TODO: white-list cases where:
680 // - there are no non-reference parameters to the constructor
681 // - the initializers don't access any non-reference parameters
682 // - the initializers don't take the address of non-reference
685 // If we ever add any of the above cases, remember that:
686 // - function-try-blocks will always blacklist this optimization
687 // - we need to perform the constructor prologue and cleanup in
688 // EmitConstructorBody.
693 // We also disable the optimization for variadic functions because
694 // it's impossible to "re-pass" varargs.
695 if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic())
698 // FIXME: Decide if we can do a delegation of a delegating constructor.
699 if (Ctor->isDelegatingConstructor())
705 /// EmitConstructorBody - Emits the body of the current constructor.
706 void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) {
707 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl());
708 CXXCtorType CtorType = CurGD.getCtorType();
710 // Before we go any further, try the complete->base constructor
711 // delegation optimization.
712 if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) &&
713 CGM.getTarget().getCXXABI().hasConstructorVariants()) {
714 if (CGDebugInfo *DI = getDebugInfo())
715 DI->EmitLocation(Builder, Ctor->getLocEnd());
716 EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args);
720 Stmt *Body = Ctor->getBody();
722 // Enter the function-try-block before the constructor prologue if
724 bool IsTryBody = (Body && isa<CXXTryStmt>(Body));
726 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
728 EHScopeStack::stable_iterator CleanupDepth = EHStack.stable_begin();
730 // TODO: in restricted cases, we can emit the vbase initializers of
731 // a complete ctor and then delegate to the base ctor.
733 // Emit the constructor prologue, i.e. the base and member
735 EmitCtorPrologue(Ctor, CtorType, Args);
737 // Emit the body of the statement.
739 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
743 // Emit any cleanup blocks associated with the member or base
744 // initializers, which includes (along the exceptional path) the
745 // destructors for those members and bases that were fully
747 PopCleanupBlocks(CleanupDepth);
750 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
754 class FieldMemcpyizer {
756 FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl,
757 const VarDecl *SrcRec)
758 : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec),
759 RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)),
760 FirstField(0), LastField(0), FirstFieldOffset(0), LastFieldOffset(0),
761 LastAddedFieldIndex(0) { }
763 static bool isMemcpyableField(FieldDecl *F) {
764 Qualifiers Qual = F->getType().getQualifiers();
765 if (Qual.hasVolatile() || Qual.hasObjCLifetime())
770 void addMemcpyableField(FieldDecl *F) {
777 CharUnits getMemcpySize() const {
778 unsigned LastFieldSize =
779 LastField->isBitField() ?
780 LastField->getBitWidthValue(CGF.getContext()) :
781 CGF.getContext().getTypeSize(LastField->getType());
782 uint64_t MemcpySizeBits =
783 LastFieldOffset + LastFieldSize - FirstFieldOffset +
784 CGF.getContext().getCharWidth() - 1;
785 CharUnits MemcpySize =
786 CGF.getContext().toCharUnitsFromBits(MemcpySizeBits);
791 // Give the subclass a chance to bail out if it feels the memcpy isn't
792 // worth it (e.g. Hasn't aggregated enough data).
793 if (FirstField == 0) {
799 if (FirstField->isBitField()) {
800 const CGRecordLayout &RL =
801 CGF.getTypes().getCGRecordLayout(FirstField->getParent());
802 const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField);
803 Alignment = CharUnits::fromQuantity(BFInfo.StorageAlignment);
805 Alignment = CGF.getContext().getDeclAlign(FirstField);
808 assert((CGF.getContext().toCharUnitsFromBits(FirstFieldOffset) %
809 Alignment) == 0 && "Bad field alignment.");
811 CharUnits MemcpySize = getMemcpySize();
812 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
813 llvm::Value *ThisPtr = CGF.LoadCXXThis();
814 LValue DestLV = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
815 LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField);
816 llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec));
817 LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
818 LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField);
820 emitMemcpyIR(Dest.isBitField() ? Dest.getBitFieldAddr() : Dest.getAddress(),
821 Src.isBitField() ? Src.getBitFieldAddr() : Src.getAddress(),
822 MemcpySize, Alignment);
831 CodeGenFunction &CGF;
832 const CXXRecordDecl *ClassDecl;
836 void emitMemcpyIR(llvm::Value *DestPtr, llvm::Value *SrcPtr,
837 CharUnits Size, CharUnits Alignment) {
838 llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
840 llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace());
841 DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP);
843 llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
845 llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace());
846 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP);
848 CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity(),
849 Alignment.getQuantity());
852 void addInitialField(FieldDecl *F) {
855 FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex());
856 LastFieldOffset = FirstFieldOffset;
857 LastAddedFieldIndex = F->getFieldIndex();
861 void addNextField(FieldDecl *F) {
862 assert(F->getFieldIndex() == LastAddedFieldIndex + 1 &&
863 "Cannot aggregate non-contiguous fields.");
864 LastAddedFieldIndex = F->getFieldIndex();
866 // The 'first' and 'last' fields are chosen by offset, rather than field
867 // index. This allows the code to support bitfields, as well as regular
869 uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex());
870 if (FOffset < FirstFieldOffset) {
872 FirstFieldOffset = FOffset;
873 } else if (FOffset > LastFieldOffset) {
875 LastFieldOffset = FOffset;
879 const VarDecl *SrcRec;
880 const ASTRecordLayout &RecLayout;
881 FieldDecl *FirstField;
882 FieldDecl *LastField;
883 uint64_t FirstFieldOffset, LastFieldOffset;
884 unsigned LastAddedFieldIndex;
887 class ConstructorMemcpyizer : public FieldMemcpyizer {
890 /// Get source argument for copy constructor. Returns null if not a copy
892 static const VarDecl* getTrivialCopySource(const CXXConstructorDecl *CD,
893 FunctionArgList &Args) {
894 if (CD->isCopyOrMoveConstructor() && CD->isImplicitlyDefined())
895 return Args[Args.size() - 1];
899 // Returns true if a CXXCtorInitializer represents a member initialization
900 // that can be rolled into a memcpy.
901 bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const {
904 FieldDecl *Field = MemberInit->getMember();
905 assert(Field != 0 && "No field for member init.");
906 QualType FieldType = Field->getType();
907 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
909 // Bail out on non-POD, not-trivially-constructable members.
910 if (!(CE && CE->getConstructor()->isTrivial()) &&
911 !(FieldType.isTriviallyCopyableType(CGF.getContext()) ||
912 FieldType->isReferenceType()))
915 // Bail out on volatile fields.
916 if (!isMemcpyableField(Field))
919 // Otherwise we're good.
924 ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD,
925 FunctionArgList &Args)
926 : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CD, Args)),
928 MemcpyableCtor(CD->isImplicitlyDefined() &&
929 CD->isCopyOrMoveConstructor() &&
930 CGF.getLangOpts().getGC() == LangOptions::NonGC),
933 void addMemberInitializer(CXXCtorInitializer *MemberInit) {
934 if (isMemberInitMemcpyable(MemberInit)) {
935 AggregatedInits.push_back(MemberInit);
936 addMemcpyableField(MemberInit->getMember());
938 emitAggregatedInits();
939 EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit,
940 ConstructorDecl, Args);
944 void emitAggregatedInits() {
945 if (AggregatedInits.size() <= 1) {
946 // This memcpy is too small to be worthwhile. Fall back on default
948 for (unsigned i = 0; i < AggregatedInits.size(); ++i) {
949 EmitMemberInitializer(CGF, ConstructorDecl->getParent(),
950 AggregatedInits[i], ConstructorDecl, Args);
958 AggregatedInits.clear();
961 void pushEHDestructors() {
962 llvm::Value *ThisPtr = CGF.LoadCXXThis();
963 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
964 LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
966 for (unsigned i = 0; i < AggregatedInits.size(); ++i) {
967 QualType FieldType = AggregatedInits[i]->getMember()->getType();
968 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
969 if (CGF.needsEHCleanup(dtorKind))
970 CGF.pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
975 emitAggregatedInits();
979 const CXXConstructorDecl *ConstructorDecl;
981 FunctionArgList &Args;
982 SmallVector<CXXCtorInitializer*, 16> AggregatedInits;
985 class AssignmentMemcpyizer : public FieldMemcpyizer {
988 // Returns the memcpyable field copied by the given statement, if one
989 // exists. Otherwise r
990 FieldDecl* getMemcpyableField(Stmt *S) {
991 if (!AssignmentsMemcpyable)
993 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) {
994 // Recognise trivial assignments.
995 if (BO->getOpcode() != BO_Assign)
997 MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS());
1000 FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1001 if (!Field || !isMemcpyableField(Field))
1003 Stmt *RHS = BO->getRHS();
1004 if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS))
1005 RHS = EC->getSubExpr();
1008 MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS);
1009 if (dyn_cast<FieldDecl>(ME2->getMemberDecl()) != Field)
1012 } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) {
1013 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl());
1014 if (!(MD && (MD->isCopyAssignmentOperator() ||
1015 MD->isMoveAssignmentOperator()) &&
1018 MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument());
1021 FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl());
1022 if (!Field || !isMemcpyableField(Field))
1024 MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0));
1025 if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl()))
1028 } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
1029 FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
1030 if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy)
1032 Expr *DstPtr = CE->getArg(0);
1033 if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr))
1034 DstPtr = DC->getSubExpr();
1035 UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr);
1036 if (!DUO || DUO->getOpcode() != UO_AddrOf)
1038 MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr());
1041 FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1042 if (!Field || !isMemcpyableField(Field))
1044 Expr *SrcPtr = CE->getArg(1);
1045 if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr))
1046 SrcPtr = SC->getSubExpr();
1047 UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr);
1048 if (!SUO || SUO->getOpcode() != UO_AddrOf)
1050 MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr());
1051 if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl()))
1059 bool AssignmentsMemcpyable;
1060 SmallVector<Stmt*, 16> AggregatedStmts;
1064 AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD,
1065 FunctionArgList &Args)
1066 : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]),
1067 AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) {
1068 assert(Args.size() == 2);
1071 void emitAssignment(Stmt *S) {
1072 FieldDecl *F = getMemcpyableField(S);
1074 addMemcpyableField(F);
1075 AggregatedStmts.push_back(S);
1077 emitAggregatedStmts();
1082 void emitAggregatedStmts() {
1083 if (AggregatedStmts.size() <= 1) {
1084 for (unsigned i = 0; i < AggregatedStmts.size(); ++i)
1085 CGF.EmitStmt(AggregatedStmts[i]);
1090 AggregatedStmts.clear();
1094 emitAggregatedStmts();
1100 /// EmitCtorPrologue - This routine generates necessary code to initialize
1101 /// base classes and non-static data members belonging to this constructor.
1102 void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1103 CXXCtorType CtorType,
1104 FunctionArgList &Args) {
1105 if (CD->isDelegatingConstructor())
1106 return EmitDelegatingCXXConstructorCall(CD, Args);
1108 const CXXRecordDecl *ClassDecl = CD->getParent();
1110 CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
1113 llvm::BasicBlock *BaseCtorContinueBB = 0;
1114 if (ClassDecl->getNumVBases() &&
1115 !CGM.getTarget().getCXXABI().hasConstructorVariants()) {
1116 // The ABIs that don't have constructor variants need to put a branch
1117 // before the virtual base initialization code.
1118 BaseCtorContinueBB = CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this);
1119 assert(BaseCtorContinueBB);
1122 // Virtual base initializers first.
1123 for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) {
1124 EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
1127 if (BaseCtorContinueBB) {
1128 // Complete object handler should continue to the remaining initializers.
1129 Builder.CreateBr(BaseCtorContinueBB);
1130 EmitBlock(BaseCtorContinueBB);
1133 // Then, non-virtual base initializers.
1134 for (; B != E && (*B)->isBaseInitializer(); B++) {
1135 assert(!(*B)->isBaseVirtual());
1136 EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
1139 InitializeVTablePointers(ClassDecl);
1141 // And finally, initialize class members.
1142 FieldConstructionScope FCS(*this, CXXThisValue);
1143 ConstructorMemcpyizer CM(*this, CD, Args);
1144 for (; B != E; B++) {
1145 CXXCtorInitializer *Member = (*B);
1146 assert(!Member->isBaseInitializer());
1147 assert(Member->isAnyMemberInitializer() &&
1148 "Delegating initializer on non-delegating constructor");
1149 CM.addMemberInitializer(Member);
1155 FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field);
1158 HasTrivialDestructorBody(ASTContext &Context,
1159 const CXXRecordDecl *BaseClassDecl,
1160 const CXXRecordDecl *MostDerivedClassDecl)
1162 // If the destructor is trivial we don't have to check anything else.
1163 if (BaseClassDecl->hasTrivialDestructor())
1166 if (!BaseClassDecl->getDestructor()->hasTrivialBody())
1170 for (CXXRecordDecl::field_iterator I = BaseClassDecl->field_begin(),
1171 E = BaseClassDecl->field_end(); I != E; ++I) {
1172 const FieldDecl *Field = *I;
1174 if (!FieldHasTrivialDestructorBody(Context, Field))
1178 // Check non-virtual bases.
1179 for (CXXRecordDecl::base_class_const_iterator I =
1180 BaseClassDecl->bases_begin(), E = BaseClassDecl->bases_end();
1185 const CXXRecordDecl *NonVirtualBase =
1186 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
1187 if (!HasTrivialDestructorBody(Context, NonVirtualBase,
1188 MostDerivedClassDecl))
1192 if (BaseClassDecl == MostDerivedClassDecl) {
1193 // Check virtual bases.
1194 for (CXXRecordDecl::base_class_const_iterator I =
1195 BaseClassDecl->vbases_begin(), E = BaseClassDecl->vbases_end();
1197 const CXXRecordDecl *VirtualBase =
1198 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
1199 if (!HasTrivialDestructorBody(Context, VirtualBase,
1200 MostDerivedClassDecl))
1209 FieldHasTrivialDestructorBody(ASTContext &Context,
1210 const FieldDecl *Field)
1212 QualType FieldBaseElementType = Context.getBaseElementType(Field->getType());
1214 const RecordType *RT = FieldBaseElementType->getAs<RecordType>();
1218 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1219 return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
1222 /// CanSkipVTablePointerInitialization - Check whether we need to initialize
1223 /// any vtable pointers before calling this destructor.
1224 static bool CanSkipVTablePointerInitialization(ASTContext &Context,
1225 const CXXDestructorDecl *Dtor) {
1226 if (!Dtor->hasTrivialBody())
1229 // Check the fields.
1230 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1231 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1232 E = ClassDecl->field_end(); I != E; ++I) {
1233 const FieldDecl *Field = *I;
1235 if (!FieldHasTrivialDestructorBody(Context, Field))
1242 /// EmitDestructorBody - Emits the body of the current destructor.
1243 void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) {
1244 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl());
1245 CXXDtorType DtorType = CurGD.getDtorType();
1247 // The call to operator delete in a deleting destructor happens
1248 // outside of the function-try-block, which means it's always
1249 // possible to delegate the destructor body to the complete
1250 // destructor. Do so.
1251 if (DtorType == Dtor_Deleting) {
1252 EnterDtorCleanups(Dtor, Dtor_Deleting);
1253 EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
1254 /*Delegating=*/false, LoadCXXThis());
1259 Stmt *Body = Dtor->getBody();
1261 // If the body is a function-try-block, enter the try before
1263 bool isTryBody = (Body && isa<CXXTryStmt>(Body));
1265 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
1267 // Enter the epilogue cleanups.
1268 RunCleanupsScope DtorEpilogue(*this);
1270 // If this is the complete variant, just invoke the base variant;
1271 // the epilogue will destruct the virtual bases. But we can't do
1272 // this optimization if the body is a function-try-block, because
1273 // we'd introduce *two* handler blocks.
1275 case Dtor_Deleting: llvm_unreachable("already handled deleting case");
1278 // Enter the cleanup scopes for virtual bases.
1279 EnterDtorCleanups(Dtor, Dtor_Complete);
1282 CGM.getTarget().getCXXABI().hasDestructorVariants()) {
1283 EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false,
1284 /*Delegating=*/false, LoadCXXThis());
1287 // Fallthrough: act like we're in the base variant.
1290 // Enter the cleanup scopes for fields and non-virtual bases.
1291 EnterDtorCleanups(Dtor, Dtor_Base);
1293 // Initialize the vtable pointers before entering the body.
1294 if (!CanSkipVTablePointerInitialization(getContext(), Dtor))
1295 InitializeVTablePointers(Dtor->getParent());
1298 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
1302 assert(Dtor->isImplicit() && "bodyless dtor not implicit");
1303 // nothing to do besides what's in the epilogue
1305 // -fapple-kext must inline any call to this dtor into
1306 // the caller's body.
1307 if (getLangOpts().AppleKext)
1308 CurFn->addFnAttr(llvm::Attribute::AlwaysInline);
1312 // Jump out through the epilogue cleanups.
1313 DtorEpilogue.ForceCleanup();
1315 // Exit the try if applicable.
1317 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
1320 void CodeGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &Args) {
1321 const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl());
1322 const Stmt *RootS = AssignOp->getBody();
1323 assert(isa<CompoundStmt>(RootS) &&
1324 "Body of an implicit assignment operator should be compound stmt.");
1325 const CompoundStmt *RootCS = cast<CompoundStmt>(RootS);
1327 LexicalScope Scope(*this, RootCS->getSourceRange());
1329 AssignmentMemcpyizer AM(*this, AssignOp, Args);
1330 for (CompoundStmt::const_body_iterator I = RootCS->body_begin(),
1331 E = RootCS->body_end();
1333 AM.emitAssignment(*I);
1339 /// Call the operator delete associated with the current destructor.
1340 struct CallDtorDelete : EHScopeStack::Cleanup {
1343 void Emit(CodeGenFunction &CGF, Flags flags) {
1344 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1345 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1346 CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
1347 CGF.getContext().getTagDeclType(ClassDecl));
1351 struct CallDtorDeleteConditional : EHScopeStack::Cleanup {
1352 llvm::Value *ShouldDeleteCondition;
1354 CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition)
1355 : ShouldDeleteCondition(ShouldDeleteCondition) {
1356 assert(ShouldDeleteCondition != NULL);
1359 void Emit(CodeGenFunction &CGF, Flags flags) {
1360 llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete");
1361 llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue");
1362 llvm::Value *ShouldCallDelete
1363 = CGF.Builder.CreateIsNull(ShouldDeleteCondition);
1364 CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB);
1366 CGF.EmitBlock(callDeleteBB);
1367 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1368 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1369 CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
1370 CGF.getContext().getTagDeclType(ClassDecl));
1371 CGF.Builder.CreateBr(continueBB);
1373 CGF.EmitBlock(continueBB);
1377 class DestroyField : public EHScopeStack::Cleanup {
1378 const FieldDecl *field;
1379 CodeGenFunction::Destroyer *destroyer;
1380 bool useEHCleanupForArray;
1383 DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer,
1384 bool useEHCleanupForArray)
1385 : field(field), destroyer(destroyer),
1386 useEHCleanupForArray(useEHCleanupForArray) {}
1388 void Emit(CodeGenFunction &CGF, Flags flags) {
1389 // Find the address of the field.
1390 llvm::Value *thisValue = CGF.LoadCXXThis();
1391 QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent());
1392 LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy);
1393 LValue LV = CGF.EmitLValueForField(ThisLV, field);
1394 assert(LV.isSimple());
1396 CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer,
1397 flags.isForNormalCleanup() && useEHCleanupForArray);
1402 /// EmitDtorEpilogue - Emit all code that comes at the end of class's
1403 /// destructor. This is to call destructors on members and base classes
1404 /// in reverse order of their construction.
1405 void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD,
1406 CXXDtorType DtorType) {
1407 assert(!DD->isTrivial() &&
1408 "Should not emit dtor epilogue for trivial dtor!");
1410 // The deleting-destructor phase just needs to call the appropriate
1411 // operator delete that Sema picked up.
1412 if (DtorType == Dtor_Deleting) {
1413 assert(DD->getOperatorDelete() &&
1414 "operator delete missing - EmitDtorEpilogue");
1415 if (CXXStructorImplicitParamValue) {
1416 // If there is an implicit param to the deleting dtor, it's a boolean
1417 // telling whether we should call delete at the end of the dtor.
1418 EHStack.pushCleanup<CallDtorDeleteConditional>(
1419 NormalAndEHCleanup, CXXStructorImplicitParamValue);
1421 EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup);
1426 const CXXRecordDecl *ClassDecl = DD->getParent();
1428 // Unions have no bases and do not call field destructors.
1429 if (ClassDecl->isUnion())
1432 // The complete-destructor phase just destructs all the virtual bases.
1433 if (DtorType == Dtor_Complete) {
1435 // We push them in the forward order so that they'll be popped in
1436 // the reverse order.
1437 for (CXXRecordDecl::base_class_const_iterator I =
1438 ClassDecl->vbases_begin(), E = ClassDecl->vbases_end();
1440 const CXXBaseSpecifier &Base = *I;
1441 CXXRecordDecl *BaseClassDecl
1442 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1444 // Ignore trivial destructors.
1445 if (BaseClassDecl->hasTrivialDestructor())
1448 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1450 /*BaseIsVirtual*/ true);
1456 assert(DtorType == Dtor_Base);
1458 // Destroy non-virtual bases.
1459 for (CXXRecordDecl::base_class_const_iterator I =
1460 ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) {
1461 const CXXBaseSpecifier &Base = *I;
1463 // Ignore virtual bases.
1464 if (Base.isVirtual())
1467 CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl();
1469 // Ignore trivial destructors.
1470 if (BaseClassDecl->hasTrivialDestructor())
1473 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1475 /*BaseIsVirtual*/ false);
1478 // Destroy direct fields.
1479 SmallVector<const FieldDecl *, 16> FieldDecls;
1480 for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1481 E = ClassDecl->field_end(); I != E; ++I) {
1482 const FieldDecl *field = *I;
1483 QualType type = field->getType();
1484 QualType::DestructionKind dtorKind = type.isDestructedType();
1485 if (!dtorKind) continue;
1487 // Anonymous union members do not have their destructors called.
1488 const RecordType *RT = type->getAsUnionType();
1489 if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue;
1491 CleanupKind cleanupKind = getCleanupKind(dtorKind);
1492 EHStack.pushCleanup<DestroyField>(cleanupKind, field,
1493 getDestroyer(dtorKind),
1494 cleanupKind & EHCleanup);
1498 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1499 /// constructor for each of several members of an array.
1501 /// \param ctor the constructor to call for each element
1502 /// \param arrayType the type of the array to initialize
1503 /// \param arrayBegin an arrayType*
1504 /// \param zeroInitialize true if each element should be
1505 /// zero-initialized before it is constructed
1507 CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
1508 const ConstantArrayType *arrayType,
1509 llvm::Value *arrayBegin,
1510 CallExpr::const_arg_iterator argBegin,
1511 CallExpr::const_arg_iterator argEnd,
1512 bool zeroInitialize) {
1513 QualType elementType;
1514 llvm::Value *numElements =
1515 emitArrayLength(arrayType, elementType, arrayBegin);
1517 EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin,
1518 argBegin, argEnd, zeroInitialize);
1521 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1522 /// constructor for each of several members of an array.
1524 /// \param ctor the constructor to call for each element
1525 /// \param numElements the number of elements in the array;
1527 /// \param arrayBegin a T*, where T is the type constructed by ctor
1528 /// \param zeroInitialize true if each element should be
1529 /// zero-initialized before it is constructed
1531 CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
1532 llvm::Value *numElements,
1533 llvm::Value *arrayBegin,
1534 CallExpr::const_arg_iterator argBegin,
1535 CallExpr::const_arg_iterator argEnd,
1536 bool zeroInitialize) {
1538 // It's legal for numElements to be zero. This can happen both
1539 // dynamically, because x can be zero in 'new A[x]', and statically,
1540 // because of GCC extensions that permit zero-length arrays. There
1541 // are probably legitimate places where we could assume that this
1542 // doesn't happen, but it's not clear that it's worth it.
1543 llvm::BranchInst *zeroCheckBranch = 0;
1545 // Optimize for a constant count.
1546 llvm::ConstantInt *constantCount
1547 = dyn_cast<llvm::ConstantInt>(numElements);
1548 if (constantCount) {
1549 // Just skip out if the constant count is zero.
1550 if (constantCount->isZero()) return;
1552 // Otherwise, emit the check.
1554 llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop");
1555 llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty");
1556 zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB);
1560 // Find the end of the array.
1561 llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements,
1564 // Enter the loop, setting up a phi for the current location to initialize.
1565 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1566 llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop");
1568 llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2,
1570 cur->addIncoming(arrayBegin, entryBB);
1572 // Inside the loop body, emit the constructor call on the array element.
1574 QualType type = getContext().getTypeDeclType(ctor->getParent());
1576 // Zero initialize the storage, if requested.
1578 EmitNullInitialization(cur, type);
1580 // C++ [class.temporary]p4:
1581 // There are two contexts in which temporaries are destroyed at a different
1582 // point than the end of the full-expression. The first context is when a
1583 // default constructor is called to initialize an element of an array.
1584 // If the constructor has one or more default arguments, the destruction of
1585 // every temporary created in a default argument expression is sequenced
1586 // before the construction of the next array element, if any.
1589 RunCleanupsScope Scope(*this);
1591 // Evaluate the constructor and its arguments in a regular
1592 // partial-destroy cleanup.
1593 if (getLangOpts().Exceptions &&
1594 !ctor->getParent()->hasTrivialDestructor()) {
1595 Destroyer *destroyer = destroyCXXObject;
1596 pushRegularPartialArrayCleanup(arrayBegin, cur, type, *destroyer);
1599 EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/ false,
1600 /*Delegating=*/false, cur, argBegin, argEnd);
1603 // Go to the next element.
1605 Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1),
1607 cur->addIncoming(next, Builder.GetInsertBlock());
1609 // Check whether that's the end of the loop.
1610 llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done");
1611 llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont");
1612 Builder.CreateCondBr(done, contBB, loopBB);
1614 // Patch the earlier check to skip over the loop.
1615 if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
1620 void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF,
1623 const RecordType *rtype = type->castAs<RecordType>();
1624 const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl());
1625 const CXXDestructorDecl *dtor = record->getDestructor();
1626 assert(!dtor->isTrivial());
1627 CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false,
1628 /*Delegating=*/false, addr);
1632 CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
1633 CXXCtorType Type, bool ForVirtualBase,
1636 CallExpr::const_arg_iterator ArgBeg,
1637 CallExpr::const_arg_iterator ArgEnd) {
1639 CGDebugInfo *DI = getDebugInfo();
1641 CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::LimitedDebugInfo) {
1642 // If debug info for this class has not been emitted then this is the
1643 // right time to do so.
1644 const CXXRecordDecl *Parent = D->getParent();
1645 DI->getOrCreateRecordType(CGM.getContext().getTypeDeclType(Parent),
1646 Parent->getLocation());
1649 // If this is a trivial constructor, just emit what's needed.
1650 if (D->isTrivial()) {
1651 if (ArgBeg == ArgEnd) {
1652 // Trivial default constructor, no codegen required.
1653 assert(D->isDefaultConstructor() &&
1654 "trivial 0-arg ctor not a default ctor");
1658 assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor");
1659 assert(D->isCopyOrMoveConstructor() &&
1660 "trivial 1-arg ctor not a copy/move ctor");
1662 const Expr *E = (*ArgBeg);
1663 QualType Ty = E->getType();
1664 llvm::Value *Src = EmitLValue(E).getAddress();
1665 EmitAggregateCopy(This, Src, Ty);
1669 // Non-trivial constructors are handled in an ABI-specific manner.
1670 llvm::Value *Callee = CGM.getCXXABI().EmitConstructorCall(*this, D, Type,
1671 ForVirtualBase, Delegating, This, ArgBeg, ArgEnd);
1672 if (CGM.getCXXABI().HasThisReturn(CurGD) &&
1673 CGM.getCXXABI().HasThisReturn(GlobalDecl(D, Type)))
1674 CalleeWithThisReturn = Callee;
1678 CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1679 llvm::Value *This, llvm::Value *Src,
1680 CallExpr::const_arg_iterator ArgBeg,
1681 CallExpr::const_arg_iterator ArgEnd) {
1682 if (D->isTrivial()) {
1683 assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor");
1684 assert(D->isCopyOrMoveConstructor() &&
1685 "trivial 1-arg ctor not a copy/move ctor");
1686 EmitAggregateCopy(This, Src, (*ArgBeg)->getType());
1689 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D,
1690 clang::Ctor_Complete);
1691 assert(D->isInstance() &&
1692 "Trying to emit a member call expr on a static method!");
1694 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
1698 // Push the this ptr.
1699 Args.add(RValue::get(This), D->getThisType(getContext()));
1702 // Push the src ptr.
1703 QualType QT = *(FPT->arg_type_begin());
1704 llvm::Type *t = CGM.getTypes().ConvertType(QT);
1705 Src = Builder.CreateBitCast(Src, t);
1706 Args.add(RValue::get(Src), QT);
1708 // Skip over first argument (Src).
1710 CallExpr::const_arg_iterator Arg = ArgBeg;
1711 for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin()+1,
1712 E = FPT->arg_type_end(); I != E; ++I, ++Arg) {
1713 assert(Arg != ArgEnd && "Running over edge of argument list!");
1714 EmitCallArg(Args, *Arg, *I);
1716 // Either we've emitted all the call args, or we have a call to a
1717 // variadic function.
1718 assert((Arg == ArgEnd || FPT->isVariadic()) &&
1719 "Extra arguments in non-variadic function!");
1720 // If we still have any arguments, emit them using the type of the argument.
1721 for (; Arg != ArgEnd; ++Arg) {
1722 QualType ArgType = Arg->getType();
1723 EmitCallArg(Args, *Arg, ArgType);
1726 EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, RequiredArgs::All),
1727 Callee, ReturnValueSlot(), Args, D);
1731 CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1732 CXXCtorType CtorType,
1733 const FunctionArgList &Args) {
1734 CallArgList DelegateArgs;
1736 FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
1737 assert(I != E && "no parameters to constructor");
1740 DelegateArgs.add(RValue::get(LoadCXXThis()), (*I)->getType());
1744 if (llvm::Value *VTT = GetVTTParameter(GlobalDecl(Ctor, CtorType),
1745 /*ForVirtualBase=*/false,
1746 /*Delegating=*/true)) {
1747 QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy);
1748 DelegateArgs.add(RValue::get(VTT), VoidPP);
1750 if (CodeGenVTables::needsVTTParameter(CurGD)) {
1751 assert(I != E && "cannot skip vtt parameter, already done with args");
1752 assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type");
1757 // Explicit arguments.
1758 for (; I != E; ++I) {
1759 const VarDecl *param = *I;
1760 EmitDelegateCallArg(DelegateArgs, param);
1763 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(Ctor, CtorType);
1764 EmitCall(CGM.getTypes().arrangeCXXConstructorDeclaration(Ctor, CtorType),
1765 Callee, ReturnValueSlot(), DelegateArgs, Ctor);
1766 if (CGM.getCXXABI().HasThisReturn(CurGD) &&
1767 CGM.getCXXABI().HasThisReturn(GlobalDecl(Ctor, CtorType)))
1768 CalleeWithThisReturn = Callee;
1772 struct CallDelegatingCtorDtor : EHScopeStack::Cleanup {
1773 const CXXDestructorDecl *Dtor;
1777 CallDelegatingCtorDtor(const CXXDestructorDecl *D, llvm::Value *Addr,
1779 : Dtor(D), Addr(Addr), Type(Type) {}
1781 void Emit(CodeGenFunction &CGF, Flags flags) {
1782 CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false,
1783 /*Delegating=*/true, Addr);
1789 CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1790 const FunctionArgList &Args) {
1791 assert(Ctor->isDelegatingConstructor());
1793 llvm::Value *ThisPtr = LoadCXXThis();
1795 QualType Ty = getContext().getTagDeclType(Ctor->getParent());
1796 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
1797 AggValueSlot AggSlot =
1798 AggValueSlot::forAddr(ThisPtr, Alignment, Qualifiers(),
1799 AggValueSlot::IsDestructed,
1800 AggValueSlot::DoesNotNeedGCBarriers,
1801 AggValueSlot::IsNotAliased);
1803 EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot);
1805 const CXXRecordDecl *ClassDecl = Ctor->getParent();
1806 if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) {
1808 CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base;
1810 EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup,
1811 ClassDecl->getDestructor(),
1816 void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD,
1818 bool ForVirtualBase,
1820 llvm::Value *This) {
1821 llvm::Value *VTT = GetVTTParameter(GlobalDecl(DD, Type),
1822 ForVirtualBase, Delegating);
1823 llvm::Value *Callee = 0;
1824 if (getLangOpts().AppleKext)
1825 Callee = BuildAppleKextVirtualDestructorCall(DD, Type,
1829 Callee = CGM.GetAddrOfCXXDestructor(DD, Type);
1831 // FIXME: Provide a source location here.
1832 EmitCXXMemberCall(DD, SourceLocation(), Callee, ReturnValueSlot(), This,
1833 VTT, getContext().getPointerType(getContext().VoidPtrTy),
1835 if (CGM.getCXXABI().HasThisReturn(CurGD) &&
1836 CGM.getCXXABI().HasThisReturn(GlobalDecl(DD, Type)))
1837 CalleeWithThisReturn = Callee;
1841 struct CallLocalDtor : EHScopeStack::Cleanup {
1842 const CXXDestructorDecl *Dtor;
1845 CallLocalDtor(const CXXDestructorDecl *D, llvm::Value *Addr)
1846 : Dtor(D), Addr(Addr) {}
1848 void Emit(CodeGenFunction &CGF, Flags flags) {
1849 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1850 /*ForVirtualBase=*/false,
1851 /*Delegating=*/false, Addr);
1856 void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D,
1857 llvm::Value *Addr) {
1858 EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr);
1861 void CodeGenFunction::PushDestructorCleanup(QualType T, llvm::Value *Addr) {
1862 CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl();
1863 if (!ClassDecl) return;
1864 if (ClassDecl->hasTrivialDestructor()) return;
1866 const CXXDestructorDecl *D = ClassDecl->getDestructor();
1867 assert(D && D->isUsed() && "destructor not marked as used!");
1868 PushDestructorCleanup(D, Addr);
1872 CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This,
1873 const CXXRecordDecl *ClassDecl,
1874 const CXXRecordDecl *BaseClassDecl) {
1875 llvm::Value *VTablePtr = GetVTablePtr(This, Int8PtrTy);
1876 CharUnits VBaseOffsetOffset =
1877 CGM.getVTableContext().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl);
1879 llvm::Value *VBaseOffsetPtr =
1880 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1881 "vbase.offset.ptr");
1882 llvm::Type *PtrDiffTy =
1883 ConvertType(getContext().getPointerDiffType());
1885 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
1886 PtrDiffTy->getPointerTo());
1888 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1894 CodeGenFunction::InitializeVTablePointer(BaseSubobject Base,
1895 const CXXRecordDecl *NearestVBase,
1896 CharUnits OffsetFromNearestVBase,
1897 llvm::Constant *VTable,
1898 const CXXRecordDecl *VTableClass) {
1899 const CXXRecordDecl *RD = Base.getBase();
1901 // Compute the address point.
1902 llvm::Value *VTableAddressPoint;
1904 // Check if we need to use a vtable from the VTT.
1905 if (CodeGenVTables::needsVTTParameter(CurGD) &&
1906 (RD->getNumVBases() || NearestVBase)) {
1907 // Get the secondary vpointer index.
1908 uint64_t VirtualPointerIndex =
1909 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1912 llvm::Value *VTT = LoadCXXVTT();
1913 if (VirtualPointerIndex)
1914 VTT = Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1916 // And load the address point from the VTT.
1917 VTableAddressPoint = Builder.CreateLoad(VTT);
1919 uint64_t AddressPoint =
1920 CGM.getVTableContext().getVTableLayout(VTableClass).getAddressPoint(Base);
1921 VTableAddressPoint =
1922 Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint);
1925 // Compute where to store the address point.
1926 llvm::Value *VirtualOffset = 0;
1927 CharUnits NonVirtualOffset = CharUnits::Zero();
1929 if (CodeGenVTables::needsVTTParameter(CurGD) && NearestVBase) {
1930 // We need to use the virtual base offset offset because the virtual base
1931 // might have a different offset in the most derived class.
1932 VirtualOffset = GetVirtualBaseClassOffset(LoadCXXThis(), VTableClass,
1934 NonVirtualOffset = OffsetFromNearestVBase;
1936 // We can just use the base offset in the complete class.
1937 NonVirtualOffset = Base.getBaseOffset();
1940 // Apply the offsets.
1941 llvm::Value *VTableField = LoadCXXThis();
1943 if (!NonVirtualOffset.isZero() || VirtualOffset)
1944 VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField,
1948 // Finally, store the address point.
1949 llvm::Type *AddressPointPtrTy =
1950 VTableAddressPoint->getType()->getPointerTo();
1951 VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy);
1952 llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField);
1953 CGM.DecorateInstruction(Store, CGM.getTBAAInfoForVTablePtr());
1957 CodeGenFunction::InitializeVTablePointers(BaseSubobject Base,
1958 const CXXRecordDecl *NearestVBase,
1959 CharUnits OffsetFromNearestVBase,
1960 bool BaseIsNonVirtualPrimaryBase,
1961 llvm::Constant *VTable,
1962 const CXXRecordDecl *VTableClass,
1963 VisitedVirtualBasesSetTy& VBases) {
1964 // If this base is a non-virtual primary base the address point has already
1966 if (!BaseIsNonVirtualPrimaryBase) {
1967 // Initialize the vtable pointer for this base.
1968 InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase,
1969 VTable, VTableClass);
1972 const CXXRecordDecl *RD = Base.getBase();
1975 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1976 E = RD->bases_end(); I != E; ++I) {
1977 CXXRecordDecl *BaseDecl
1978 = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1980 // Ignore classes without a vtable.
1981 if (!BaseDecl->isDynamicClass())
1984 CharUnits BaseOffset;
1985 CharUnits BaseOffsetFromNearestVBase;
1986 bool BaseDeclIsNonVirtualPrimaryBase;
1988 if (I->isVirtual()) {
1989 // Check if we've visited this virtual base before.
1990 if (!VBases.insert(BaseDecl))
1993 const ASTRecordLayout &Layout =
1994 getContext().getASTRecordLayout(VTableClass);
1996 BaseOffset = Layout.getVBaseClassOffset(BaseDecl);
1997 BaseOffsetFromNearestVBase = CharUnits::Zero();
1998 BaseDeclIsNonVirtualPrimaryBase = false;
2000 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2002 BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl);
2003 BaseOffsetFromNearestVBase =
2004 OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl);
2005 BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl;
2008 InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset),
2009 I->isVirtual() ? BaseDecl : NearestVBase,
2010 BaseOffsetFromNearestVBase,
2011 BaseDeclIsNonVirtualPrimaryBase,
2012 VTable, VTableClass, VBases);
2016 void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) {
2017 // Ignore classes without a vtable.
2018 if (!RD->isDynamicClass())
2022 llvm::Constant *VTable = CGM.getVTables().GetAddrOfVTable(RD);
2024 // Initialize the vtable pointers for this class and all of its bases.
2025 VisitedVirtualBasesSetTy VBases;
2026 InitializeVTablePointers(BaseSubobject(RD, CharUnits::Zero()),
2028 /*OffsetFromNearestVBase=*/CharUnits::Zero(),
2029 /*BaseIsNonVirtualPrimaryBase=*/false,
2030 VTable, RD, VBases);
2033 llvm::Value *CodeGenFunction::GetVTablePtr(llvm::Value *This,
2035 llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo());
2036 llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable");
2037 CGM.DecorateInstruction(VTable, CGM.getTBAAInfoForVTablePtr());
2041 static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) {
2042 const Expr *E = Base;
2045 E = E->IgnoreParens();
2046 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
2047 if (CE->getCastKind() == CK_DerivedToBase ||
2048 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2049 CE->getCastKind() == CK_NoOp) {
2050 E = CE->getSubExpr();
2058 QualType DerivedType = E->getType();
2059 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
2060 DerivedType = PTy->getPointeeType();
2062 return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl());
2065 // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
2066 // quite what we want.
2067 static const Expr *skipNoOpCastsAndParens(const Expr *E) {
2069 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
2070 E = PE->getSubExpr();
2074 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
2075 if (CE->getCastKind() == CK_NoOp) {
2076 E = CE->getSubExpr();
2080 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
2081 if (UO->getOpcode() == UO_Extension) {
2082 E = UO->getSubExpr();
2090 /// canDevirtualizeMemberFunctionCall - Checks whether the given virtual member
2091 /// function call on the given expr can be devirtualized.
2092 static bool canDevirtualizeMemberFunctionCall(const Expr *Base,
2093 const CXXMethodDecl *MD) {
2094 // If the most derived class is marked final, we know that no subclass can
2095 // override this member function and so we can devirtualize it. For example:
2097 // struct A { virtual void f(); }
2098 // struct B final : A { };
2104 const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base);
2105 if (MostDerivedClassDecl->hasAttr<FinalAttr>())
2108 // If the member function is marked 'final', we know that it can't be
2109 // overridden and can therefore devirtualize it.
2110 if (MD->hasAttr<FinalAttr>())
2113 // Similarly, if the class itself is marked 'final' it can't be overridden
2114 // and we can therefore devirtualize the member function call.
2115 if (MD->getParent()->hasAttr<FinalAttr>())
2118 Base = skipNoOpCastsAndParens(Base);
2119 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
2120 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
2121 // This is a record decl. We know the type and can devirtualize it.
2122 return VD->getType()->isRecordType();
2128 // We can always devirtualize calls on temporary object expressions.
2129 if (isa<CXXConstructExpr>(Base))
2132 // And calls on bound temporaries.
2133 if (isa<CXXBindTemporaryExpr>(Base))
2136 // Check if this is a call expr that returns a record type.
2137 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
2138 return CE->getCallReturnType()->isRecordType();
2140 // We can't devirtualize the call.
2144 static bool UseVirtualCall(ASTContext &Context,
2145 const CXXOperatorCallExpr *CE,
2146 const CXXMethodDecl *MD) {
2147 if (!MD->isVirtual())
2150 // When building with -fapple-kext, all calls must go through the vtable since
2151 // the kernel linker can do runtime patching of vtables.
2152 if (Context.getLangOpts().AppleKext)
2155 return !canDevirtualizeMemberFunctionCall(CE->getArg(0), MD);
2159 CodeGenFunction::EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E,
2160 const CXXMethodDecl *MD,
2161 llvm::Value *This) {
2162 llvm::FunctionType *fnType =
2163 CGM.getTypes().GetFunctionType(
2164 CGM.getTypes().arrangeCXXMethodDeclaration(MD));
2166 if (UseVirtualCall(getContext(), E, MD))
2167 return BuildVirtualCall(MD, This, fnType);
2169 return CGM.GetAddrOfFunction(MD, fnType);
2172 void CodeGenFunction::EmitForwardingCallToLambda(const CXXRecordDecl *lambda,
2173 CallArgList &callArgs) {
2174 // Lookup the call operator
2175 DeclarationName operatorName
2176 = getContext().DeclarationNames.getCXXOperatorName(OO_Call);
2177 CXXMethodDecl *callOperator =
2178 cast<CXXMethodDecl>(lambda->lookup(operatorName).front());
2180 // Get the address of the call operator.
2181 const CGFunctionInfo &calleeFnInfo =
2182 CGM.getTypes().arrangeCXXMethodDeclaration(callOperator);
2183 llvm::Value *callee =
2184 CGM.GetAddrOfFunction(GlobalDecl(callOperator),
2185 CGM.getTypes().GetFunctionType(calleeFnInfo));
2187 // Prepare the return slot.
2188 const FunctionProtoType *FPT =
2189 callOperator->getType()->castAs<FunctionProtoType>();
2190 QualType resultType = FPT->getResultType();
2191 ReturnValueSlot returnSlot;
2192 if (!resultType->isVoidType() &&
2193 calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
2194 !hasScalarEvaluationKind(calleeFnInfo.getReturnType()))
2195 returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified());
2197 // We don't need to separately arrange the call arguments because
2198 // the call can't be variadic anyway --- it's impossible to forward
2199 // variadic arguments.
2201 // Now emit our call.
2202 RValue RV = EmitCall(calleeFnInfo, callee, returnSlot,
2203 callArgs, callOperator);
2205 // If necessary, copy the returned value into the slot.
2206 if (!resultType->isVoidType() && returnSlot.isNull())
2207 EmitReturnOfRValue(RV, resultType);
2209 EmitBranchThroughCleanup(ReturnBlock);
2212 void CodeGenFunction::EmitLambdaBlockInvokeBody() {
2213 const BlockDecl *BD = BlockInfo->getBlockDecl();
2214 const VarDecl *variable = BD->capture_begin()->getVariable();
2215 const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl();
2217 // Start building arguments for forwarding call
2218 CallArgList CallArgs;
2220 QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
2221 llvm::Value *ThisPtr = GetAddrOfBlockDecl(variable, false);
2222 CallArgs.add(RValue::get(ThisPtr), ThisType);
2224 // Add the rest of the parameters.
2225 for (BlockDecl::param_const_iterator I = BD->param_begin(),
2226 E = BD->param_end(); I != E; ++I) {
2227 ParmVarDecl *param = *I;
2228 EmitDelegateCallArg(CallArgs, param);
2231 EmitForwardingCallToLambda(Lambda, CallArgs);
2234 void CodeGenFunction::EmitLambdaToBlockPointerBody(FunctionArgList &Args) {
2235 if (cast<CXXMethodDecl>(CurCodeDecl)->isVariadic()) {
2236 // FIXME: Making this work correctly is nasty because it requires either
2237 // cloning the body of the call operator or making the call operator forward.
2238 CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function");
2242 EmitFunctionBody(Args);
2245 void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD) {
2246 const CXXRecordDecl *Lambda = MD->getParent();
2248 // Start building arguments for forwarding call
2249 CallArgList CallArgs;
2251 QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
2252 llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType));
2253 CallArgs.add(RValue::get(ThisPtr), ThisType);
2255 // Add the rest of the parameters.
2256 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2257 E = MD->param_end(); I != E; ++I) {
2258 ParmVarDecl *param = *I;
2259 EmitDelegateCallArg(CallArgs, param);
2262 EmitForwardingCallToLambda(Lambda, CallArgs);
2265 void CodeGenFunction::EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD) {
2266 if (MD->isVariadic()) {
2267 // FIXME: Making this work correctly is nasty because it requires either
2268 // cloning the body of the call operator or making the call operator forward.
2269 CGM.ErrorUnsupported(MD, "lambda conversion to variadic function");
2273 EmitLambdaDelegatingInvokeBody(MD);