Skip to content

Commit

Permalink
Handle IrFunctionExpression in SAM conversion.
Browse files Browse the repository at this point in the history
We will see that case when we will switch to Kotlin 2.0.21.

PiperOrigin-RevId: 688578869
  • Loading branch information
jDramaix authored and copybara-github committed Oct 22, 2024
1 parent 2545a4c commit 42c1564
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ import com.google.j2cl.transpiler.frontend.common.AbstractCompilationUnitBuilder
import com.google.j2cl.transpiler.frontend.kotlin.ir.IntrinsicMethods
import com.google.j2cl.transpiler.frontend.kotlin.ir.findFunctionByName
import com.google.j2cl.transpiler.frontend.kotlin.ir.getArguments
import com.google.j2cl.transpiler.frontend.kotlin.ir.getFunctionExpression
import com.google.j2cl.transpiler.frontend.kotlin.ir.getNameSourcePosition
import com.google.j2cl.transpiler.frontend.kotlin.ir.getParameters
import com.google.j2cl.transpiler.frontend.kotlin.ir.getSourcePosition
Expand All @@ -98,6 +97,7 @@ import com.google.j2cl.transpiler.frontend.kotlin.ir.isUnitInstanceReference
import com.google.j2cl.transpiler.frontend.kotlin.ir.javaName
import com.google.j2cl.transpiler.frontend.kotlin.ir.resolveLabel
import com.google.j2cl.transpiler.frontend.kotlin.ir.typeSubstitutionMap
import com.google.j2cl.transpiler.frontend.kotlin.ir.unfoldExpression
import com.google.j2cl.transpiler.frontend.kotlin.lower.IrForInLoop
import com.google.j2cl.transpiler.frontend.kotlin.lower.IrForLoop
import com.google.j2cl.transpiler.frontend.kotlin.lower.IrSwitch
Expand Down Expand Up @@ -1289,7 +1289,7 @@ class CompilationUnitBuilder(
}

private fun convertSamConversion(irTypeOperatorCall: IrTypeOperatorCall): Expression {
val expression = irTypeOperatorCall.argument
val expression = irTypeOperatorCall.unfoldExpression()
val functionalTypeDescriptor = environment.getDeclaredTypeDescriptor(irTypeOperatorCall.type)
return when (expression) {
is IrFunctionReference -> createFunctionExpression(functionalTypeDescriptor, expression)
Expand All @@ -1300,13 +1300,10 @@ class CompilationUnitBuilder(
convertQualifier(expression),
expression.getter,
)
is IrFunctionExpression -> createFunctionExpression(functionalTypeDescriptor, expression)
else ->
// TODO(b/225955286): Implement conversion functionality from things that are not
// lambdas.
createFunctionExpression(
functionalTypeDescriptor,
irTypeOperatorCall.getFunctionExpression(),
)
// TODO(b/225955286): Implement conversion functionality from things that are not lambdas.
throw IllegalStateException("Unsupported SAM conversion ${irTypeOperatorCall.dump()}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import org.jetbrains.kotlin.ir.expressions.IrFunctionReference
import org.jetbrains.kotlin.ir.expressions.IrGetField
import org.jetbrains.kotlin.ir.expressions.IrLoop
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
import org.jetbrains.kotlin.ir.expressions.IrPropertyReference
import org.jetbrains.kotlin.ir.expressions.IrSetField
import org.jetbrains.kotlin.ir.expressions.IrSetValue
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
Expand Down Expand Up @@ -130,12 +131,14 @@ import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.utils.addIfNotNull

/** Returns the actual function expression from inside a nested type operator. */
fun IrTypeOperatorCall.getFunctionExpression(): IrFunctionExpression =
fun IrTypeOperatorCall.unfoldExpression(): IrExpression =
// TODO(b/225955286): Fix to handle the general case.
argument.let {
when (it) {
is IrTypeOperatorCall -> it.getFunctionExpression()
is IrFunctionExpression -> it
is IrTypeOperatorCall -> it.unfoldExpression()
is IrFunctionExpression,
is IrFunctionReference,
is IrPropertyReference -> it
else -> throw IllegalStateException("Unsupported arguments type")
}
}
Expand Down

0 comments on commit 42c1564

Please sign in to comment.