Skip to content

Commit c351f3a

Browse files
committed
[interop][SwiftToCxx] error result param should be handled by lowered function signature
1 parent f4de75a commit c351f3a

File tree

6 files changed

+25
-44
lines changed

6 files changed

+25
-44
lines changed

include/swift/IRGen/IRABIDetailsProvider.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class IRABIDetailsProvider {
6262
public:
6363
enum class ABIParameterRole {
6464
/// The Swift error parameter.
65-
Error
65+
None
6666
};
6767

6868
inline ABIParameterRole getRole() const { return role; }
@@ -182,6 +182,9 @@ class IRABIDetailsProvider {
182182
/// Represents a context parameter passed to the call.
183183
class ContextParameter {};
184184

185+
/// Represents an out error parameter passed indirectly to the call.
186+
class ErrorResultValue {};
187+
185188
/// Returns lowered direct result details, or \c None if direct result is
186189
/// void.
187190
llvm::Optional<DirectResultType> getDirectResultType() const;
@@ -204,7 +207,8 @@ class IRABIDetailsProvider {
204207
genericRequirementVisitor,
205208
llvm::function_ref<void(const MetadataSourceParameter &)>
206209
metadataSourceVisitor,
207-
llvm::function_ref<void(const ContextParameter &)> contextParamVisitor);
210+
llvm::function_ref<void(const ContextParameter &)> contextParamVisitor,
211+
llvm::function_ref<void(const ErrorResultValue &)> errorResultVisitor);
208212

209213
/// FIXME: make private.
210214
SmallVector<ABIAdditionalParam, 1> additionalParams;

lib/IRGen/GenCall.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,8 @@ void SignatureExpansion::expandParameters(
17231723
IGM.getStorageType(getSILFuncConventions().getSILType(
17241724
FnType->getErrorResult(), IGM.getMaximalTypeExpansionContext()));
17251725
ParamIRTypes.push_back(errorType->getPointerTo());
1726+
if (recordedABIDetails)
1727+
recordedABIDetails->hasErrorResult = true;
17261728
}
17271729

17281730
// Witness methods have some extra parameter types.

lib/IRGen/IRABIDetailsProvider.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,6 @@ class IRABIDetailsProviderImpl {
206206

207207
using ABIAdditionalParam = IRABIDetailsProvider::ABIAdditionalParam;
208208
using ParamRole = ABIAdditionalParam::ABIParameterRole;
209-
// FIXME: remove second signature computation.
210-
auto signature = Signature::getUncached(IGM, silFuncType, funcPointerKind);
211-
for (auto attrSet : signature.getAttributes()) {
212-
if (attrSet.hasAttribute(llvm::Attribute::AttrKind::SwiftError))
213-
params.push_back(
214-
ABIAdditionalParam(ParamRole::Error, llvm::None, CanType()));
215-
}
216209
return params;
217210
}
218211

@@ -317,7 +310,8 @@ void IRABIDetailsProvider::LoweredFunctionSignature::visitParameterList(
317310
genericRequirementVisitor,
318311
llvm::function_ref<void(const MetadataSourceParameter &)>
319312
metadataSourceVisitor,
320-
llvm::function_ref<void(const ContextParameter &)> contextParamVisitor) {
313+
llvm::function_ref<void(const ContextParameter &)> contextParamVisitor,
314+
llvm::function_ref<void(const ErrorResultValue &)> errorResultVisitor) {
321315
// Indirect result values come before parameters.
322316
llvm::SmallVector<IndirectResultValue, 1> result;
323317
for (const auto &r : abiDetails.indirectResults)
@@ -387,7 +381,8 @@ void IRABIDetailsProvider::LoweredFunctionSignature::visitParameterList(
387381
contextParamVisitor(ContextParameter());
388382
}
389383

390-
// FIXME: Traverse other additional params.
384+
if (abiDetails.hasErrorResult)
385+
errorResultVisitor(ErrorResultValue());
391386
}
392387

393388
IRABIDetailsProvider::IRABIDetailsProvider(ModuleDecl &mod,

lib/IRGen/Signature.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ class SignatureExpansionABIDetails {
161161
bool hasTrailingSelfParam = false;
162162
/// True if a context parameter passed to the call.
163163
bool hasContextParam = false;
164+
/// True if an error result value indirect parameter is passed to the call.
165+
bool hasErrorResult = false;
164166
};
165167

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

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,16 +1231,7 @@ class DeclAndTypePrinter::Implementation
12311231
llvm::SmallVector<DeclAndTypeClangFunctionPrinter::AdditionalParam, 2>
12321232
&params,
12331233
Optional<NominalTypeDecl *> selfTypeDeclContext = None) {
1234-
for (auto param : ABIparams) {
1235-
using Role = IRABIDetailsProvider::ABIAdditionalParam::ABIParameterRole;
1236-
switch (param.getRole()) {
1237-
case Role::Error:
1238-
params.push_back(
1239-
{DeclAndTypeClangFunctionPrinter::AdditionalParam::Role::Error,
1240-
FD->getASTContext().getOpaquePointerType()});
1241-
break;
1242-
}
1243-
}
1234+
// FIXME: remove.
12441235
}
12451236

12461237
// Print out the extern C Swift ABI function signature.

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,11 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
715715
ContextParameter &) {
716716
emitNewParam();
717717
os << "SWIFT_CONTEXT void * _Nonnull _ctx";
718+
},
719+
[&](const IRABIDetailsProvider::LoweredFunctionSignature::
720+
ErrorResultValue &) {
721+
emitNewParam();
722+
os << "SWIFT_ERROR_RESULT void * _Nullable * _Nullable _error";
718723
});
719724
} else {
720725

@@ -748,18 +753,6 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
748753
return resultingRepresentation;
749754
}
750755
}
751-
if (additionalParams.size()) {
752-
assert(kind == FunctionSignatureKind::CFunctionProto);
753-
if (HasParams)
754-
os << ", ";
755-
HasParams = true;
756-
interleaveComma(additionalParams, os, [&](const AdditionalParam &param) {
757-
if (param.role == AdditionalParam::Role::Error) {
758-
os << "SWIFT_ERROR_RESULT ";
759-
os << "void * _Nullable * _Nullable _error";
760-
}
761-
});
762-
}
763756
if (kind == FunctionSignatureKind::CFunctionProto && !HasParams) {
764757
// Emit 'void' in an empty parameter list for C function declarations.
765758
os << "void";
@@ -922,18 +915,12 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
922915
ContextParameter &) {
923916
emitNewParam();
924917
os << "_ctx";
925-
});
926-
927-
if (additionalParams.size()) {
928-
if (needsComma)
929-
os << ", ";
930-
interleaveComma(additionalParams, os, [&](const AdditionalParam &param) {
931-
if (param.role == AdditionalParam::Role::Error && hasThrows) {
918+
},
919+
[&](const IRABIDetailsProvider::LoweredFunctionSignature::
920+
ErrorResultValue &) {
921+
emitNewParam();
932922
os << "&opaqueError";
933-
}
934-
});
935-
}
936-
923+
});
937924
os << ')';
938925
};
939926

0 commit comments

Comments
 (0)