Kotlin: extract suspend modifier and handle suspend SAM conversions

This commit is contained in:
Tamas Vajk
2022-09-19 13:11:49 +02:00
parent 3e58605e8e
commit a6e44ed1cf
7 changed files with 103 additions and 9 deletions

View File

@@ -910,6 +910,9 @@ open class KotlinFileExtractor(
if (f is IrSimpleFunction && f.overriddenSymbols.isNotEmpty()) {
addModifiers(id, "override")
}
if (f.isSuspend) {
addModifiers(id, "suspend")
}
return id
}
@@ -1461,7 +1464,7 @@ open class KotlinFileExtractor(
val (isFunctionInvoke, isBigArityFunctionInvoke) =
if (drType is IrSimpleType &&
drType.isFunctionOrKFunction() &&
(drType.isFunctionOrKFunction() || drType.isSuspendFunctionOrKFunction()) &&
callTarget.name.asString() == OperatorNameConventions.INVOKE.asString()) {
Pair(true, drType.arguments.size > BuiltInFunctionArity.BIG_ARITY)
} else {
@@ -4516,17 +4519,17 @@ open class KotlinFileExtractor(
```
*/
if (!e.argument.type.isFunctionOrKFunction()) {
logger.errorElement("Expected to find expression with function type in SAM conversion.", e)
return
}
val st = e.argument.type as? IrSimpleType
if (st == null) {
logger.errorElement("Expected to find a simple type in SAM conversion.", e)
return
}
if (!st.isFunctionOrKFunction() && !st.isSuspendFunctionOrKFunction()) {
logger.errorElement("Expected to find expression with function type in SAM conversion.", e)
return
}
// Either Function1, ... Function22 or FunctionN type, but not Function23 or above.
val functionType = getFunctionalInterfaceTypeWithTypeArgs(st.arguments)
if (functionType == null) {
@@ -4589,6 +4592,10 @@ open class KotlinFileExtractor(
// the real underlying R Function<T, R>.apply(T t).
forceExtractFunction(samMember, classId, extractBody = false, extractMethodAndParameterTypeAccesses = true, typeSub, classTypeArgs, ids.function, tw.getLocation(e))
if (st.isSuspendFunctionOrKFunction()) {
addModifiers(ids.function, "suspend")
}
//body
val blockId = tw.getFreshIdLabel<DbBlock>()
tw.writeStmts_block(blockId, ids.function, 0, ids.function)