Skip to content

Commit f4de75a

Browse files
committed
[interop][SwiftToCxx] 'self' param should be handled by type param visitor
1 parent 0592894 commit f4de75a

File tree

8 files changed

+55
-49
lines changed

8 files changed

+55
-49
lines changed

include/swift/IRGen/IRABIDetailsProvider.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ class IRABIDetailsProvider {
6161
class ABIAdditionalParam {
6262
public:
6363
enum class ABIParameterRole {
64-
/// A parameter that corresponds to the 'self' parameter.
65-
Self,
6664
/// The Swift error parameter.
6765
Error
6866
};
@@ -181,6 +179,9 @@ class IRABIDetailsProvider {
181179
friend class LoweredFunctionSignature;
182180
};
183181

182+
/// Represents a context parameter passed to the call.
183+
class ContextParameter {};
184+
184185
/// Returns lowered direct result details, or \c None if direct result is
185186
/// void.
186187
llvm::Optional<DirectResultType> getDirectResultType() const;
@@ -202,7 +203,8 @@ class IRABIDetailsProvider {
202203
llvm::function_ref<void(const GenericRequirementParameter &)>
203204
genericRequirementVisitor,
204205
llvm::function_ref<void(const MetadataSourceParameter &)>
205-
metadataSourceVisitor);
206+
metadataSourceVisitor,
207+
llvm::function_ref<void(const ContextParameter &)> contextParamVisitor);
206208

207209
/// FIXME: make private.
208210
SmallVector<ABIAdditionalParam, 1> additionalParams;

lib/IRGen/GenCall.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,8 @@ void SignatureExpansion::expandParameters(
16791679
if (claimSelf())
16801680
IGM.addSwiftSelfAttributes(Attrs, curLength);
16811681
expand(FnType->getSelfParameter());
1682+
if (recordedABIDetails)
1683+
recordedABIDetails->hasTrailingSelfParam = true;
16821684
assert(ParamIRTypes.size() == curLength + 1 &&
16831685
"adding 'self' added unexpected number of parameters");
16841686
} else {
@@ -1706,6 +1708,8 @@ void SignatureExpansion::expandParameters(
17061708
if (claimSelf())
17071709
IGM.addSwiftSelfAttributes(Attrs, ParamIRTypes.size());
17081710
ParamIRTypes.push_back(IGM.RefCountedPtrTy);
1711+
if (recordedABIDetails)
1712+
recordedABIDetails->hasContextParam = true;
17091713
}
17101714
}
17111715

lib/IRGen/IRABIDetailsProvider.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ class IRABIDetailsProviderImpl {
209209
// FIXME: remove second signature computation.
210210
auto signature = Signature::getUncached(IGM, silFuncType, funcPointerKind);
211211
for (auto attrSet : signature.getAttributes()) {
212-
if (attrSet.hasAttribute(llvm::Attribute::AttrKind::SwiftSelf))
213-
params.push_back(
214-
ABIAdditionalParam(ParamRole::Self, llvm::None, CanType()));
215212
if (attrSet.hasAttribute(llvm::Attribute::AttrKind::SwiftError))
216213
params.push_back(
217214
ABIAdditionalParam(ParamRole::Error, llvm::None, CanType()));
@@ -319,7 +316,8 @@ void IRABIDetailsProvider::LoweredFunctionSignature::visitParameterList(
319316
llvm::function_ref<void(const GenericRequirementParameter &)>
320317
genericRequirementVisitor,
321318
llvm::function_ref<void(const MetadataSourceParameter &)>
322-
metadataSourceVisitor) {
319+
metadataSourceVisitor,
320+
llvm::function_ref<void(const ContextParameter &)> contextParamVisitor) {
323321
// Indirect result values come before parameters.
324322
llvm::SmallVector<IndirectResultValue, 1> result;
325323
for (const auto &r : abiDetails.indirectResults)
@@ -381,6 +379,14 @@ void IRABIDetailsProvider::LoweredFunctionSignature::visitParameterList(
381379
});
382380
}
383381

382+
if (abiDetails.hasTrailingSelfParam) {
383+
assert(!abiDetails.hasContextParam);
384+
assert(FD->hasImplicitSelfDecl());
385+
indirectParamVisitor(IndirectParameter(*FD->getImplicitSelfDecl()));
386+
} else if (abiDetails.hasContextParam) {
387+
contextParamVisitor(ContextParameter());
388+
}
389+
384390
// FIXME: Traverse other additional params.
385391
}
386392

lib/IRGen/Signature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ class SignatureExpansionABIDetails {
157157
/// Type sources added to the signature during expansion.
158158
llvm::SmallVector<PolymorphicSignatureExpandedTypeSource, 2>
159159
polymorphicSignatureExpandedTypeSources;
160+
/// True if a trailing self parameter is passed to the call.
161+
bool hasTrailingSelfParam = false;
162+
/// True if a context parameter passed to the call.
163+
bool hasContextParam = false;
160164
};
161165

162166
/// A signature represents something which can actually be called.

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,15 +1234,6 @@ class DeclAndTypePrinter::Implementation
12341234
for (auto param : ABIparams) {
12351235
using Role = IRABIDetailsProvider::ABIAdditionalParam::ABIParameterRole;
12361236
switch (param.getRole()) {
1237-
case Role::Self:
1238-
params.push_back(
1239-
{DeclAndTypeClangFunctionPrinter::AdditionalParam::Role::Self,
1240-
selfTypeDeclContext
1241-
? (*selfTypeDeclContext)->getDeclaredInterfaceType()
1242-
: FD->getASTContext().getOpaquePointerType(),
1243-
/*isIndirect=*/
1244-
isa<FuncDecl>(FD) ? cast<FuncDecl>(FD)->isMutating() : false});
1245-
break;
12461237
case Role::Error:
12471238
params.push_back(
12481239
{DeclAndTypeClangFunctionPrinter::AdditionalParam::Role::Error,

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,14 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
679679
emitNewParam();
680680
if (param.getParamDecl().isSelfParameter())
681681
os << "SWIFT_CONTEXT ";
682-
if (!param.getParamDecl().isInOut())
682+
bool isConst =
683+
!param.getParamDecl().isInOut() &&
684+
!(param.getParamDecl().isSelfParameter() &&
685+
!param.getParamDecl().getInterfaceType()->hasTypeParameter() &&
686+
param.getParamDecl()
687+
.getInterfaceType()
688+
->isAnyClassReferenceType());
689+
if (isConst)
683690
os << "const ";
684691
if (isKnownCType(param.getParamDecl().getInterfaceType(),
685692
typeMapping) ||
@@ -703,6 +710,11 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
703710
MetadataSourceParameter &metadataSrcParam) {
704711
emitNewParam();
705712
os << "void * _Nonnull ";
713+
},
714+
[&](const IRABIDetailsProvider::LoweredFunctionSignature::
715+
ContextParameter &) {
716+
emitNewParam();
717+
os << "SWIFT_CONTEXT void * _Nonnull _ctx";
706718
});
707719
} else {
708720

@@ -742,19 +754,7 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
742754
os << ", ";
743755
HasParams = true;
744756
interleaveComma(additionalParams, os, [&](const AdditionalParam &param) {
745-
if (param.role == AdditionalParam::Role::Self) {
746-
os << "SWIFT_CONTEXT ";
747-
// FIXME: this should be a proper param.
748-
if (!param.isIndirect && !(FD->getImplicitSelfDecl() &&
749-
!FD->getImplicitSelfDecl()
750-
->getInterfaceType()
751-
->hasTypeParameter() &&
752-
FD->getImplicitSelfDecl()
753-
->getInterfaceType()
754-
->isAnyClassReferenceType()))
755-
os << "const ";
756-
os << "void * _Nonnull _self";
757-
} else if (param.role == AdditionalParam::Role::Error) {
757+
if (param.role == AdditionalParam::Role::Error) {
758758
os << "SWIFT_ERROR_RESULT ";
759759
os << "void * _Nullable * _Nullable _error";
760760
}
@@ -834,7 +834,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
834834
const AnyFunctionType *funcType) {
835835
if (hasThrows) {
836836
os << " void* opaqueError = nullptr;\n";
837-
os << " void* self = nullptr;\n";
837+
os << " void* _ctx = nullptr;\n";
838838
}
839839
auto signature = interopContext.getIrABIDetails().getFunctionLoweredSignature(
840840
const_cast<AbstractFunctionDecl *>(FD));
@@ -917,19 +917,18 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
917917
/*isInOut=*/false);
918918
assert(!result.isUnsupported());
919919
os << ">::getTypeMetadata()";
920+
},
921+
[&](const IRABIDetailsProvider::LoweredFunctionSignature::
922+
ContextParameter &) {
923+
emitNewParam();
924+
os << "_ctx";
920925
});
921926

922927
if (additionalParams.size()) {
923928
if (needsComma)
924929
os << ", ";
925930
interleaveComma(additionalParams, os, [&](const AdditionalParam &param) {
926-
if (param.role == AdditionalParam::Role::Self && !hasThrows) {
927-
// FIXME: this is wonky.
928-
needsComma = false;
929-
printParamUse(*FD->getImplicitSelfDecl(), /*isIndirect=*/true, "");
930-
} else if (param.role == AdditionalParam::Role::Self && hasThrows) {
931-
os << "self";
932-
} else if (param.role == AdditionalParam::Role::Error && hasThrows) {
931+
if (param.role == AdditionalParam::Role::Error && hasThrows) {
933932
os << "&opaqueError";
934933
}
935934
});

lib/PrintAsClang/PrintClangFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class DeclAndTypeClangFunctionPrinter {
7979

8080
/// Information about any additional parameters.
8181
struct AdditionalParam {
82-
enum class Role { Self, Error };
82+
enum class Role { Error };
8383

8484
Role role;
8585
Type type;

test/Interop/SwiftToCxx/functions/swift-functions-errors.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
// CHECK-LABEL: namespace _impl {
1010

11-
// CHECK: SWIFT_EXTERN void $s9Functions18emptyThrowFunctionyyKF(SWIFT_CONTEXT const void * _Nonnull _self, SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error) SWIFT_CALL; // emptyThrowFunction()
12-
// CHECK: SWIFT_EXTERN void $s9Functions13throwFunctionyyKF(SWIFT_CONTEXT const void * _Nonnull _self, SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error) SWIFT_CALL; // throwFunction()
11+
// CHECK: SWIFT_EXTERN void $s9Functions18emptyThrowFunctionyyKF(SWIFT_CONTEXT void * _Nonnull _ctx, SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error) SWIFT_CALL; // emptyThrowFunction()
12+
// CHECK: SWIFT_EXTERN void $s9Functions13throwFunctionyyKF(SWIFT_CONTEXT void * _Nonnull _ctx, SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error) SWIFT_CALL; // throwFunction()
1313

1414
// CHECK: }
1515

@@ -22,8 +22,8 @@ public func emptyThrowFunction() throws { print("passEmptyThrowFunction") }
2222

2323
// CHECK: inline void emptyThrowFunction() {
2424
// CHECK: void* opaqueError = nullptr;
25-
// CHECK: void* self = nullptr;
26-
// CHECK: _impl::$s9Functions18emptyThrowFunctionyyKF(self, &opaqueError);
25+
// CHECK: void* _ctx = nullptr;
26+
// CHECK: _impl::$s9Functions18emptyThrowFunctionyyKF(_ctx, &opaqueError);
2727
// CHECK: if (opaqueError != nullptr)
2828
// CHECK: throw (swift::Error(opaqueError))
2929
// CHECK: }
@@ -42,8 +42,8 @@ public func testDestroyedError() throws { throw DestroyedError() }
4242

4343
// CHECK: inline void testDestroyedError() {
4444
// CHECK: void* opaqueError = nullptr;
45-
// CHECK: void* self = nullptr;
46-
// CHECK: _impl::$s9Functions18testDestroyedErroryyKF(self, &opaqueError);
45+
// CHECK: void* _ctx = nullptr;
46+
// CHECK: _impl::$s9Functions18testDestroyedErroryyKF(_ctx, &opaqueError);
4747
// CHECK: if (opaqueError != nullptr)
4848
// CHECK: throw (swift::Error(opaqueError))
4949
// CHECK: }
@@ -55,8 +55,8 @@ public func throwFunction() throws {
5555

5656
// CHECK: inline void throwFunction() {
5757
// CHECK: void* opaqueError = nullptr;
58-
// CHECK: void* self = nullptr;
59-
// CHECK: _impl::$s9Functions13throwFunctionyyKF(self, &opaqueError);
58+
// CHECK: void* _ctx = nullptr;
59+
// CHECK: _impl::$s9Functions13throwFunctionyyKF(_ctx, &opaqueError);
6060
// CHECK: if (opaqueError != nullptr)
6161
// CHECK: throw (swift::Error(opaqueError))
6262
// CHECK: }
@@ -69,8 +69,8 @@ public func throwFunctionWithReturn() throws -> Int {
6969

7070
// CHECK: inline swift::Int throwFunctionWithReturn() SWIFT_WARN_UNUSED_RESULT {
7171
// CHECK: void* opaqueError = nullptr;
72-
// CHECK: void* self = nullptr;
73-
// CHECK: auto returnValue = _impl::$s9Functions23throwFunctionWithReturnSiyKF(self, &opaqueError);
72+
// CHECK: void* _ctx = nullptr;
73+
// CHECK: auto returnValue = _impl::$s9Functions23throwFunctionWithReturnSiyKF(_ctx, &opaqueError);
7474
// CHECK: if (opaqueError != nullptr)
7575
// CHECK: throw (swift::Error(opaqueError))
7676
// CHECK: return returnValue;

0 commit comments

Comments
 (0)