JVM_IR. Combine all inline function resolve logic in ResolveInlineCalls lower

This commit is contained in:
Mikhael Bogdanov
2019-11-20 13:27:00 +01:00
parent 939a9ff53e
commit c3af2d5a10
4 changed files with 16 additions and 40 deletions
@@ -286,7 +286,6 @@ private val jvmFilePhases =
additionalClassAnnotationPhase then
typeOperatorLowering then
replaceKFunctionInvokeWithFunctionInvokePhase then
resolveInlineCallsPhase then
checkLocalNamesWithOldBackendPhase then
@@ -302,6 +301,7 @@ val jvmPhases = namedIrModulePhase(
fileClassPhase then
performByIrFile(lower = jvmFilePhases) then
generateMultifileFacadesPhase then
resolveInlineCallsPhase then
// should be last transformation
removeDeclarationsThatWouldBeInlined then
validateIrAfterLowering
@@ -948,14 +948,6 @@ class ExpressionCodegen(
}
}
private fun IrFunction.resolveMultiFileFacades(): IrFunction {
//TODO: move to ResolveInlineCalls lower (now it's goes before generateMultifileFacadesPhase)
return if (origin == JvmLoweredDeclarationOrigin.MULTIFILE_BRIDGE) {
context.multifileFacadeMemberToPartMember[this]?.let { return it }
error("Function from a multi-file facade without the link to the function in the part: ${this.render()}")
} else this
}
private fun getOrCreateCallGenerator(
element: IrFunctionAccessExpression, data: BlockInfo, signature: JvmMethodSignature
): IrCallGenerator {
@@ -965,7 +957,7 @@ class ExpressionCodegen(
return IrCallGenerator.DefaultCallGenerator
}
val callee = element.symbol.owner.resolveMultiFileFacades()
val callee = element.symbol.owner
val typeArgumentContainer = if (callee is IrConstructor) callee.parentAsClass else callee
val typeArguments =
if (element.typeArgumentsCount == 0) {
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.backend.jvm.codegen
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.backend.common.ir.ir2string
import org.jetbrains.kotlin.backend.jvm.lower.MultifileFacadeFileEntry
import org.jetbrains.kotlin.codegen.BaseExpressionCodegen
import org.jetbrains.kotlin.codegen.ClassBuilder
import org.jetbrains.kotlin.codegen.OwnerKind
@@ -23,9 +22,6 @@ import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
import org.jetbrains.kotlin.ir.util.isSuspend
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature
@@ -95,28 +91,7 @@ class IrSourceCompilerForInline(
asmMethod: Method
): SMAPAndMethodNode {
assert(callableDescriptor == callee.symbol.descriptor.original) { "Expected $callableDescriptor got ${callee.descriptor.original}" }
val irFunction = getFunctionToInline()
return makeInlineNode(irFunction, FakeClassCodegen(irFunction, codegen.classCodegen), false)
}
private fun getFunctionToInline(): IrFunction {
val parent = callee.parentAsClass
if (parent.fileParent.fileEntry is MultifileFacadeFileEntry) {
codegen.context.multifileFacadeMemberToPartMember[callee]?.let { return it }
if (callee.isSuspend) {
codegen.context.suspendFunctionViewToOriginal[callee]?.let { facadeMemberOriginal ->
codegen.context.multifileFacadeMemberToPartMember[facadeMemberOriginal]?.let { partMemberOriginal ->
return partMemberOriginal.getOrCreateSuspendFunctionViewIfNeeded(codegen.context)
}
}
}
error("Function from a multi-file facade without the link to the function in the part: ${callee.render()}")
}
return callee
return makeInlineNode(callee, FakeClassCodegen(callee, codegen.classCodegen), false)
}
override fun hasFinallyBlocks() = data.hasFinallyBlocks()
@@ -6,10 +6,12 @@
package org.jetbrains.kotlin.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.common.phaser.makeIrModulePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.codegen.isInlineFunctionCall
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
@@ -19,7 +21,7 @@ import org.jetbrains.kotlin.ir.util.resolveFakeOverride
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
@Suppress("RemoveExplicitTypeArguments")
internal val resolveInlineCallsPhase = makeIrFilePhase<JvmBackendContext>(
internal val resolveInlineCallsPhase = makeIrModulePhase(
::ResolveInlineCalls,
name = "ResolveInlineCalls",
description = "Statically resolve calls to inline methods to particular implementations"
@@ -31,9 +33,10 @@ class ResolveInlineCalls(val context: JvmBackendContext) : IrElementTransformerV
override fun visitCall(expression: IrCall): IrExpression {
if (!expression.symbol.owner.isInlineFunctionCall(context))
return super.visitCall(expression)
val maybeFakeOverride = expression.symbol.owner as? IrSimpleFunction
val maybeFakeOverrideOfMultiFileBridge = expression.symbol.owner as? IrSimpleFunction
?: return super.visitCall(expression)
val resolved = maybeFakeOverride.resolveFakeOverride()
val resolved =
maybeFakeOverrideOfMultiFileBridge?.resolveMultiFileFacades() ?: maybeFakeOverrideOfMultiFileBridge.resolveFakeOverride()
?: return super.visitCall(expression)
return super.visitCall(with(expression) {
IrCallImpl(startOffset, endOffset, type, resolved.symbol, expression.typeArgumentsCount, null, superQualifierSymbol).apply {
@@ -41,4 +44,10 @@ class ResolveInlineCalls(val context: JvmBackendContext) : IrElementTransformerV
}
})
}
private fun IrFunction.resolveMultiFileFacades(): IrFunction? {
return if (origin == JvmLoweredDeclarationOrigin.MULTIFILE_BRIDGE) {
context.multifileFacadeMemberToPartMember[this]
} else null
}
}