Skip to content

Commit 4247d96

Browse files
committed
[wip][cxx-interop] Re-factor ClassTypeInfo specific logic into that class.
1 parent 4021082 commit 4247d96

File tree

2 files changed

+58
-54
lines changed

2 files changed

+58
-54
lines changed

lib/IRGen/ClassTypeInfo.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,58 @@ class ClassTypeInfo : public HeapTypeInfo<ClassTypeInfo> {
5858

5959
StructLayout *createLayoutWithTailElems(IRGenModule &IGM, SILType classType,
6060
ArrayRef<SILType> tailTypes) const;
61+
62+
void emitScalarRelease(IRGenFunction &IGF, llvm::Value *value,
63+
Atomicity atomicity) const override {
64+
if (getReferenceCounting() == ReferenceCounting::CxxCustom) {
65+
auto releaseFn = findForeignReferenceTypeRefCountingOperation(getClass(),
66+
ForeignReferenceTypeRefCountingOperation::release);
67+
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value);
68+
return;
69+
}
70+
71+
HeapTypeInfo::emitScalarRelease(IGF, value, atomicity);
72+
}
73+
74+
void emitScalarRetain(IRGenFunction &IGF, llvm::Value *value,
75+
Atomicity atomicity) const override {
76+
if (getReferenceCounting() == ReferenceCounting::CxxCustom) {
77+
auto releaseFn = findForeignReferenceTypeRefCountingOperation(getClass(),
78+
ForeignReferenceTypeRefCountingOperation::retain);
79+
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value);
80+
return;
81+
}
82+
83+
HeapTypeInfo::emitScalarRetain(IGF, value, atomicity);
84+
}
85+
86+
// Implement the primary retain/release operations of ReferenceTypeInfo
87+
// using basic reference counting.
88+
void strongRetain(IRGenFunction &IGF, Explosion &e,
89+
Atomicity atomicity) const override {
90+
if (getReferenceCounting() == ReferenceCounting::CxxCustom) {
91+
llvm::Value *value = e.claimNext();
92+
auto releaseFn = findForeignReferenceTypeRefCountingOperation(getClass(),
93+
ForeignReferenceTypeRefCountingOperation::retain);
94+
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value);
95+
return;
96+
}
97+
98+
HeapTypeInfo::strongRetain(IGF, e, atomicity);
99+
}
100+
101+
void strongRelease(IRGenFunction &IGF, Explosion &e,
102+
Atomicity atomicity) const override {
103+
if (getReferenceCounting() == ReferenceCounting::CxxCustom) {
104+
llvm::Value *value = e.claimNext();
105+
auto releaseFn = findForeignReferenceTypeRefCountingOperation(getClass(),
106+
ForeignReferenceTypeRefCountingOperation::release);
107+
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value);
108+
return;
109+
}
110+
111+
HeapTypeInfo::strongRelease(IGF, e, atomicity);
112+
}
61113
};
62114

63115
} // namespace irgen

lib/IRGen/HeapTypeInfo.h

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ enum class IsaEncoding : uint8_t {
7474
Unknown = ObjC,
7575
};
7676

77-
class ClassTypeInfo;
78-
7977
/// HeapTypeInfo - A type designed for use implementing a type
8078
/// which consists solely of something reference-counted.
8179
///
@@ -134,81 +132,35 @@ class HeapTypeInfo
134132

135133
// Emit the copy/destroy operations required by SingleScalarTypeInfo
136134
// using strong reference counting.
137-
void emitScalarRelease(IRGenFunction &IGF, llvm::Value *value,
135+
virtual void emitScalarRelease(IRGenFunction &IGF, llvm::Value *value,
138136
Atomicity atomicity) const {
139-
if (asDerived().getReferenceCounting() == ReferenceCounting::CxxCustom) {
140-
if constexpr (__is_same(Impl, ClassTypeInfo)) {
141-
auto releaseFn = findForeignReferenceTypeRefCountingOperation(
142-
asDerived().getClass(),
143-
ForeignReferenceTypeRefCountingOperation::release);
144-
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value);
145-
return;
146-
} else {
147-
llvm_unreachable("");
148-
}
149-
}
150-
137+
assert(asDerived().getReferenceCounting() != ReferenceCounting::CxxCustom);
151138
IGF.emitStrongRelease(value, asDerived().getReferenceCounting(), atomicity);
152139
}
153140

154141
void emitScalarFixLifetime(IRGenFunction &IGF, llvm::Value *value) const {
155142
return IGF.emitFixLifetime(value);
156143
}
157144

158-
void emitScalarRetain(IRGenFunction &IGF, llvm::Value *value,
145+
virtual void emitScalarRetain(IRGenFunction &IGF, llvm::Value *value,
159146
Atomicity atomicity) const {
160-
if (asDerived().getReferenceCounting() == ReferenceCounting::CxxCustom) {
161-
if constexpr (__is_same(Impl, ClassTypeInfo)) {
162-
auto releaseFn = findForeignReferenceTypeRefCountingOperation(
163-
asDerived().getClass(),
164-
ForeignReferenceTypeRefCountingOperation::retain);
165-
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value);
166-
return;
167-
} else {
168-
llvm_unreachable("");
169-
}
170-
}
171-
147+
assert(asDerived().getReferenceCounting() != ReferenceCounting::CxxCustom);
172148
IGF.emitStrongRetain(value, asDerived().getReferenceCounting(), atomicity);
173149
}
174150

175151
// Implement the primary retain/release operations of ReferenceTypeInfo
176152
// using basic reference counting.
177153
void strongRetain(IRGenFunction &IGF, Explosion &e,
178154
Atomicity atomicity) const override {
155+
assert(asDerived().getReferenceCounting() != ReferenceCounting::CxxCustom);
179156
llvm::Value *value = e.claimNext();
180-
181-
if (asDerived().getReferenceCounting() == ReferenceCounting::CxxCustom) {
182-
if constexpr (__is_same(Impl, ClassTypeInfo)) {
183-
auto releaseFn = findForeignReferenceTypeRefCountingOperation(
184-
asDerived().getClass(),
185-
ForeignReferenceTypeRefCountingOperation::retain);
186-
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value);
187-
return;
188-
} else {
189-
llvm_unreachable("");
190-
}
191-
}
192-
193157
asDerived().emitScalarRetain(IGF, value, atomicity);
194158
}
195159

196160
void strongRelease(IRGenFunction &IGF, Explosion &e,
197161
Atomicity atomicity) const override {
162+
assert(asDerived().getReferenceCounting() != ReferenceCounting::CxxCustom);
198163
llvm::Value *value = e.claimNext();
199-
200-
if (asDerived().getReferenceCounting() == ReferenceCounting::CxxCustom) {
201-
if constexpr (__is_same(Impl, ClassTypeInfo)) {
202-
auto releaseFn = findForeignReferenceTypeRefCountingOperation(
203-
asDerived().getClass(),
204-
ForeignReferenceTypeRefCountingOperation::release);
205-
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value);
206-
return;
207-
} else {
208-
llvm_unreachable("");
209-
}
210-
}
211-
212164
asDerived().emitScalarRelease(IGF, value, atomicity);
213165
}
214166

0 commit comments

Comments
 (0)