forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSILGenDestructor.cpp
204 lines (167 loc) · 7.87 KB
/
SILGenDestructor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//===--- SILGenDestructor.cpp - SILGen for destructors --------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 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->getResult().getSILType(),
subs, superSelf);
// Return.
B.createReturn(returnLoc, emitEmptyTuple(cleanupLoc));
}