JVM IR: do not use KotlinTypeMapper indirectly via InlineCodegen

Move JVM signature and method owner computation out of InlineCodegen's
constructor to the call sites.
This commit is contained in:
Alexander Udalov
2019-08-28 15:53:46 +02:00
parent ad8b7ff645
commit 4953f67563
9 changed files with 63 additions and 61 deletions
@@ -12,8 +12,11 @@ import org.jetbrains.kotlin.backend.jvm.intrinsics.JavaClassProperty
import org.jetbrains.kotlin.backend.jvm.lower.MultifileFacadeFileEntry
import org.jetbrains.kotlin.backend.jvm.lower.constantValue
import org.jetbrains.kotlin.backend.jvm.lower.inlineclasses.unboxInlineClass
import org.jetbrains.kotlin.codegen.*
import org.jetbrains.kotlin.codegen.AsmUtil.*
import org.jetbrains.kotlin.codegen.BaseExpressionCodegen
import org.jetbrains.kotlin.codegen.CallGenerator
import org.jetbrains.kotlin.codegen.OwnerKind
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.coroutines.INVOKE_SUSPEND_METHOD_NAME
import org.jetbrains.kotlin.codegen.inline.*
import org.jetbrains.kotlin.codegen.inline.ReifiedTypeInliner.Companion.putNeedClassReificationMarker
@@ -40,6 +43,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.resolve.jvm.AsmTypes.OBJECT_TYPE
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.utils.keysToMap
@@ -323,7 +327,7 @@ class ExpressionCodegen(
val callable = methodSignatureMapper.mapToCallableMethod(expression)
val callee = expression.symbol.owner
val callGenerator = getOrCreateCallGenerator(expression, data)
val callGenerator = getOrCreateCallGenerator(expression, data, callable.signature)
val asmType = if (expression is IrConstructorCall) typeMapper.mapTypeAsDeclaration(expression.type) else expression.asmType
when {
@@ -957,7 +961,9 @@ class ExpressionCodegen(
return classReference.onStack
}
private fun getOrCreateCallGenerator(element: IrFunctionAccessExpression, data: BlockInfo): IrCallGenerator {
private fun getOrCreateCallGenerator(
element: IrFunctionAccessExpression, data: BlockInfo, signature: JvmMethodSignature
): IrCallGenerator {
if (!element.symbol.owner.isInlineFunctionCall(context) ||
classCodegen.irClass.fileParent.fileEntry is MultifileFacadeFileEntry
) {
@@ -993,7 +999,9 @@ class ExpressionCodegen(
}
val original = (callee as? IrSimpleFunction)?.resolveFakeOverride() ?: irFunction
return IrInlineCodegen(this, state, original.descriptor, mappings, IrSourceCompilerForInline(state, element, this, data))
val methodOwner = callee.parent.safeAs<IrClass>()?.let(typeMapper::mapClass) ?: MethodSignatureMapper.FAKE_OWNER_TYPE
val sourceCompiler = IrSourceCompilerForInline(state, element, this, data)
return IrInlineCodegen(this, state, original.descriptor, methodOwner, signature, mappings, sourceCompiler)
}
private fun consumeReifiedOperationMarker(typeParameter: IrTypeParameter) {
@@ -5,10 +5,13 @@
package org.jetbrains.kotlin.backend.jvm.codegen
import org.jetbrains.kotlin.backend.jvm.ir.isInlineParameter
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.codegen.*
import org.jetbrains.kotlin.backend.jvm.ir.isInlineParameter
import org.jetbrains.kotlin.codegen.AsmUtil.BOUND_REFERENCE_RECEIVER
import org.jetbrains.kotlin.codegen.IrExpressionLambda
import org.jetbrains.kotlin.codegen.JvmKotlinType
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.ValueKind
import org.jetbrains.kotlin.codegen.inline.*
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
@@ -19,6 +22,7 @@ import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.ir.util.getArguments
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.kotlin.utils.keysToMap
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.commons.Method
@@ -27,10 +31,13 @@ class IrInlineCodegen(
codegen: ExpressionCodegen,
state: GenerationState,
function: FunctionDescriptor,
methodOwner: Type,
signature: JvmMethodSignature,
typeParameterMappings: IrTypeParameterMappings,
sourceCompiler: SourceCompilerForInline
) : InlineCodegen<ExpressionCodegen>(codegen, state, function, typeParameterMappings.toTypeParameterMappings(), sourceCompiler),
IrCallGenerator {
) : InlineCodegen<ExpressionCodegen>(
codegen, state, function, methodOwner, signature, typeParameterMappings.toTypeParameterMappings(), sourceCompiler
), IrCallGenerator {
override fun generateAssertFieldIfNeeded(info: RootInliningContext) {
// TODO: JVM assertions are not implemented yet in IR backend
}
@@ -156,10 +156,6 @@ class IrSourceCompilerForInline(
return setOf(codegen.irFunction.name.asString())
}
override fun initializeInlineFunctionContext(functionDescriptor: FunctionDescriptor) {
//TODO
}
private class FakeClassCodegen(irFunction: IrFunction, codegen: ClassCodegen) :
ClassCodegen(irFunction.parent as IrClass, codegen.context) {
@@ -241,4 +237,4 @@ class IrSourceCompilerForInline(
}
}
}
}
}
@@ -61,8 +61,7 @@ class MethodSignatureMapper(context: JvmBackendContext) {
// we still need to return some IrCallableMethod with some owner instance, but that owner will be ignored at the call site.
// Here we return a fake type, but this needs to be refactored so that we never call mapToCallableMethod on intrinsics.
// TODO: get rid of fake owner here
val fakeOwner = Type.getObjectType("kotlin/internal/ir/Intrinsic")
return IrCallableMethod(fakeOwner, Opcodes.INVOKESTATIC, mapSignatureSkipGeneric(callee), false)
return IrCallableMethod(FAKE_OWNER_TYPE, Opcodes.INVOKESTATIC, mapSignatureSkipGeneric(callee), false)
}
val owner = typeMapper.mapClass(calleeParent)
@@ -119,4 +118,8 @@ class MethodSignatureMapper(context: JvmBackendContext) {
}
return current
}
companion object {
val FAKE_OWNER_TYPE = Type.getObjectType("kotlin/internal/ir/Intrinsic")
}
}