mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Similarly to how we've always handled parameter types, we now recursively expand tuples in result types and separately determine a result convention for each result. The most important code-generation change here is that indirect results are now returned separately from each other and from any direct results. It is generally far better, when receiving an indirect result, to receive it as an independent result; the caller is much more likely to be able to directly receive the result in the address they want to initialize, rather than having to receive it in temporary memory and then copy parts of it into the target. The most important conceptual change here that clients and producers of SIL must be aware of is the new distinction between a SILFunctionType's *parameters* and its *argument list*. The former is just the formal parameters, derived purely from the parameter types of the original function; indirect results are no longer in this list. The latter includes the indirect result arguments; as always, all the indirect results strictly precede the parameters. Apply instructions and entry block arguments follow the argument list, not the parameter list. A relatively minor change is that there can now be multiple direct results, each with its own result convention. This is a minor change because I've chosen to leave return instructions as taking a single operand and apply instructions as producing a single result; when the type describes multiple results, they are implicitly bound up in a tuple. It might make sense to split these up and allow e.g. return instructions to take a list of operands; however, it's not clear what to do on the caller side, and this would be a major change that can be separated out from this already over-large patch. Unsurprisingly, the most invasive changes here are in SILGen; this requires substantial reworking of both call emission and reabstraction. It also proved important to switch several SILGen operations over to work with RValue instead of ManagedValue, since otherwise they would be forced to spuriously "implode" buffers.
205 lines
7.9 KiB
C++
205 lines
7.9 KiB
C++
//===--- SILGenDestructor.cpp - SILGen for destructors --------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "SILGenFunction.h"
|
|
#include "RValue.h"
|
|
#include "swift/AST/AST.h"
|
|
#include "swift/SIL/TypeLowering.h"
|
|
|
|
using namespace swift;
|
|
using namespace Lowering;
|
|
|
|
void SILGenFunction::emitDestroyingDestructor(DestructorDecl *dd) {
|
|
MagicFunctionName = DeclName(SGM.M.getASTContext().getIdentifier("deinit"));
|
|
|
|
RegularLocation Loc(dd);
|
|
if (dd->isImplicit())
|
|
Loc.markAutoGenerated();
|
|
|
|
auto cd = cast<ClassDecl>(dd->getDeclContext());
|
|
SILValue selfValue = emitSelfDecl(dd->getImplicitSelfDecl());
|
|
|
|
// Create a basic block to jump to for the implicit destruction behavior
|
|
// of releasing the elements and calling the superclass destructor.
|
|
// We won't actually emit the block until we finish with the destructor body.
|
|
prepareEpilog(Type(), false, CleanupLocation::get(Loc));
|
|
|
|
emitProfilerIncrement(dd->getBody());
|
|
// Emit the destructor body.
|
|
emitStmt(dd->getBody());
|
|
|
|
Optional<SILValue> maybeReturnValue;
|
|
SILLocation returnLoc(Loc);
|
|
std::tie(maybeReturnValue, returnLoc) = emitEpilogBB(Loc);
|
|
|
|
if (!maybeReturnValue)
|
|
return;
|
|
|
|
auto cleanupLoc = CleanupLocation::get(Loc);
|
|
|
|
// If we have a superclass, invoke its destructor.
|
|
SILValue resultSelfValue;
|
|
SILType objectPtrTy = SILType::getNativeObjectType(F.getASTContext());
|
|
if (cd->hasSuperclass()) {
|
|
Type superclassTy
|
|
= ArchetypeBuilder::mapTypeIntoContext(dd, cd->getSuperclass());
|
|
ClassDecl *superclass = superclassTy->getClassOrBoundGenericClass();
|
|
auto superclassDtorDecl = superclass->getDestructor();
|
|
SILDeclRef dtorConstant =
|
|
SILDeclRef(superclassDtorDecl, SILDeclRef::Kind::Destroyer);
|
|
SILType baseSILTy = getLoweredLoadableType(superclassTy);
|
|
SILValue baseSelf = B.createUpcast(cleanupLoc, selfValue, baseSILTy);
|
|
ManagedValue dtorValue;
|
|
SILType dtorTy;
|
|
ArrayRef<Substitution> subs
|
|
= superclassTy->gatherAllSubstitutions(SGM.M.getSwiftModule(), nullptr);
|
|
std::tie(dtorValue, dtorTy, subs)
|
|
= emitSiblingMethodRef(cleanupLoc, baseSelf, dtorConstant, subs);
|
|
resultSelfValue = B.createApply(cleanupLoc, dtorValue.forward(*this),
|
|
dtorTy, objectPtrTy, subs, baseSelf);
|
|
} else {
|
|
resultSelfValue = B.createUncheckedRefCast(cleanupLoc, selfValue,
|
|
objectPtrTy);
|
|
}
|
|
|
|
// Release our members.
|
|
emitClassMemberDestruction(selfValue, cd, cleanupLoc);
|
|
|
|
B.createReturn(returnLoc, resultSelfValue);
|
|
}
|
|
|
|
void SILGenFunction::emitDeallocatingDestructor(DestructorDecl *dd) {
|
|
MagicFunctionName = DeclName(SGM.M.getASTContext().getIdentifier("deinit"));
|
|
|
|
// The deallocating destructor is always auto-generated.
|
|
RegularLocation loc(dd);
|
|
loc.markAutoGenerated();
|
|
|
|
// Emit the prolog.
|
|
auto cd = cast<ClassDecl>(dd->getDeclContext());
|
|
SILValue selfValue = emitSelfDecl(dd->getImplicitSelfDecl());
|
|
|
|
// Form a reference to the destroying destructor.
|
|
SILDeclRef dtorConstant(dd, SILDeclRef::Kind::Destroyer);
|
|
auto classTy = cd->getDeclaredTypeInContext();
|
|
ManagedValue dtorValue;
|
|
SILType dtorTy;
|
|
ArrayRef<Substitution> subs
|
|
= classTy->gatherAllSubstitutions(SGM.M.getSwiftModule(), nullptr);
|
|
std::tie(dtorValue, dtorTy, subs)
|
|
= emitSiblingMethodRef(loc, selfValue, dtorConstant, subs);
|
|
|
|
// Call the destroying destructor.
|
|
SILType objectPtrTy = SILType::getNativeObjectType(F.getASTContext());
|
|
selfValue = B.createApply(loc, dtorValue.forward(*this),
|
|
dtorTy, objectPtrTy, subs, selfValue);
|
|
|
|
// Deallocate the object.
|
|
selfValue = B.createUncheckedRefCast(loc, selfValue,
|
|
getLoweredType(classTy));
|
|
B.createDeallocRef(loc, selfValue, false);
|
|
|
|
// Return.
|
|
B.createReturn(loc, emitEmptyTuple(loc));
|
|
}
|
|
|
|
void SILGenFunction::emitIVarDestroyer(SILDeclRef ivarDestroyer) {
|
|
auto cd = cast<ClassDecl>(ivarDestroyer.getDecl());
|
|
RegularLocation loc(cd);
|
|
loc.markAutoGenerated();
|
|
|
|
SILValue selfValue = emitSelfDecl(cd->getDestructor()->getImplicitSelfDecl());
|
|
|
|
auto cleanupLoc = CleanupLocation::get(loc);
|
|
prepareEpilog(TupleType::getEmpty(getASTContext()), false, cleanupLoc);
|
|
emitClassMemberDestruction(selfValue, cd, cleanupLoc);
|
|
B.createReturn(loc, emitEmptyTuple(loc));
|
|
emitEpilog(loc);
|
|
}
|
|
|
|
void SILGenFunction::emitClassMemberDestruction(SILValue selfValue,
|
|
ClassDecl *cd,
|
|
CleanupLocation cleanupLoc) {
|
|
for (VarDecl *vd : cd->getStoredProperties()) {
|
|
const TypeLowering &ti = getTypeLowering(vd->getType());
|
|
if (!ti.isTrivial()) {
|
|
SILValue addr = B.createRefElementAddr(cleanupLoc, selfValue, vd,
|
|
ti.getLoweredType().getAddressType());
|
|
B.emitDestroyAddrAndFold(cleanupLoc, addr);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void SILGenFunction::emitObjCDestructor(SILDeclRef dtor) {
|
|
auto dd = cast<DestructorDecl>(dtor.getDecl());
|
|
auto cd = cast<ClassDecl>(dd->getDeclContext());
|
|
MagicFunctionName = DeclName(SGM.M.getASTContext().getIdentifier("deinit"));
|
|
|
|
RegularLocation loc(dd);
|
|
if (dd->isImplicit())
|
|
loc.markAutoGenerated();
|
|
|
|
SILValue selfValue = emitSelfDecl(dd->getImplicitSelfDecl());
|
|
|
|
// Create a basic block to jump to for the implicit destruction behavior
|
|
// of releasing the elements and calling the superclass destructor.
|
|
// We won't actually emit the block until we finish with the destructor body.
|
|
prepareEpilog(Type(), false, CleanupLocation::get(loc));
|
|
|
|
// Emit the destructor body.
|
|
emitStmt(dd->getBody());
|
|
|
|
Optional<SILValue> maybeReturnValue;
|
|
SILLocation returnLoc(loc);
|
|
std::tie(maybeReturnValue, returnLoc) = emitEpilogBB(loc);
|
|
|
|
if (!maybeReturnValue)
|
|
return;
|
|
|
|
auto cleanupLoc = CleanupLocation::get(loc);
|
|
|
|
// Note: the ivar destroyer is responsible for destroying the
|
|
// instance variables before the object is actually deallocated.
|
|
|
|
// Form a reference to the superclass -dealloc.
|
|
Type superclassTy = ArchetypeBuilder::mapTypeIntoContext(dd,
|
|
cd->getSuperclass());
|
|
assert(superclassTy && "Emitting Objective-C -dealloc without superclass?");
|
|
ClassDecl *superclass = superclassTy->getClassOrBoundGenericClass();
|
|
auto superclassDtorDecl = superclass->getDestructor();
|
|
SILDeclRef superclassDtor(superclassDtorDecl,
|
|
SILDeclRef::Kind::Deallocator,
|
|
SILDeclRef::ConstructAtBestResilienceExpansion,
|
|
SILDeclRef::ConstructAtNaturalUncurryLevel,
|
|
/*isForeign=*/true);
|
|
auto superclassDtorType = SGM.getConstantType(superclassDtor);
|
|
SILValue superclassDtorValue = B.createSuperMethod(
|
|
cleanupLoc, selfValue, superclassDtor,
|
|
superclassDtorType);
|
|
|
|
// Call the superclass's -dealloc.
|
|
SILType superclassSILTy = getLoweredLoadableType(superclassTy);
|
|
SILValue superSelf = B.createUpcast(cleanupLoc, selfValue, superclassSILTy);
|
|
ArrayRef<Substitution> subs
|
|
= superclassTy->gatherAllSubstitutions(SGM.M.getSwiftModule(), nullptr);
|
|
auto substDtorType = superclassDtorType.castTo<SILFunctionType>()
|
|
->substGenericArgs(SGM.M, SGM.M.getSwiftModule(), subs);
|
|
B.createApply(cleanupLoc, superclassDtorValue,
|
|
SILType::getPrimitiveObjectType(substDtorType),
|
|
substDtorType->getSILResult(),
|
|
subs, superSelf);
|
|
|
|
// Return.
|
|
B.createReturn(returnLoc, emitEmptyTuple(cleanupLoc));
|
|
}
|