[IR] Improved tuning of SAM wrapper visibility

This commit is contained in:
Igor Chevdar
2020-06-04 18:44:46 +05:00
parent 1b1e579cbf
commit 66bbd3e102
3 changed files with 21 additions and 9 deletions
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.ScopeWithIr
import org.jetbrains.kotlin.backend.common.ir.addFakeOverridesViaIncorrectHeuristic
import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor
@@ -61,11 +62,12 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) :
private val inlineCachedImplementations = mutableMapOf<IrType, IrClass>()
private var enclosingContainer: IrDeclarationContainer? = null
open val privateGeneratedWrapperVisibility: Visibility
get() = Visibilities.PRIVATE
abstract fun getWrapperVisibility(expression: IrTypeOperatorCall, scopes: List<ScopeWithIr>): Visibility
abstract fun getSuperTypeForWrapper(typeOperand: IrType): IrType
open val inInlineFunctionScope get() = allScopes.any { scope -> (scope.irElement as? IrFunction)?.isInline ?: false }
override fun lower(irFile: IrFile) {
cachedImplementations.clear()
inlineCachedImplementations.clear()
@@ -102,10 +104,9 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) :
if (invokable.isNullConst())
return invokable
val inInlineFunctionScope = allScopes.any { scope -> (scope.irElement as? IrFunction)?.isInline ?: false }
val cache = if (inInlineFunctionScope) inlineCachedImplementations else cachedImplementations
val implementation = cache.getOrPut(erasedSuperType) {
createObjectProxy(erasedSuperType, inInlineFunctionScope, expression)
createObjectProxy(erasedSuperType, getWrapperVisibility(expression, allScopes), expression)
}
return if (superType.isNullable() && invokable.type.isNullable()) {
@@ -136,14 +137,14 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) :
// Construct a class that wraps an invokable object into an implementation of an interface:
// class sam$n(private val invokable: F) : Interface { override fun method(...) = invokable(...) }
private fun createObjectProxy(superType: IrType, generatePublicWrapper: Boolean, createFor: IrElement): IrClass {
private fun createObjectProxy(superType: IrType, wrapperVisibility: Visibility, createFor: IrElement): IrClass {
val superClass = superType.classifierOrFail.owner as IrClass
// The language documentation prohibits casting lambdas to classes, but if it was allowed,
// the `irDelegatingConstructorCall` in the constructor below would need to be modified.
assert(superClass.kind == ClassKind.INTERFACE) { "SAM conversion to an abstract class not allowed" }
val superFqName = superClass.fqNameWhenAvailable!!.asString().replace('.', '_')
val inlinePrefix = if (generatePublicWrapper) "\$i" else ""
val inlinePrefix = if (wrapperVisibility == Visibilities.PUBLIC) "\$i" else ""
val wrapperName = Name.identifier("sam$inlinePrefix\$$superFqName$SAM_WRAPPER_SUFFIX")
val superMethod = superClass.functions.single { it.modality == Modality.ABSTRACT }
// TODO: have psi2ir cast the argument to the correct function type. Also see the TODO
@@ -155,7 +156,6 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) :
context.ir.symbols.functionN(superMethod.valueParameters.size).owner
val wrappedFunctionType = wrappedFunctionClass.defaultType
val wrapperVisibility = if (generatePublicWrapper) Visibilities.PUBLIC else privateGeneratedWrapperVisibility
val subclass = buildClass {
name = wrapperName
origin = IrDeclarationOrigin.GENERATED_SAM_IMPLEMENTATION