Remove declaration that would be inlined

This commit is contained in:
Mikhael Bogdanov
2019-04-04 14:19:38 +02:00
parent dd2c7aff6e
commit 3f9154a4a1
2 changed files with 62 additions and 0 deletions
@@ -110,6 +110,8 @@ val jvmPhases = namedIrFilePhase(
jvmBuiltinOptimizationLoweringPhase then
additionalClassAnnotationPhase then
// should be last transformation
removeDeclarationsThatWouldBeInlined then
makePatchParentsPhase(3)
)
@@ -0,0 +1,60 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
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.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.codegen.isInlineCall
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.util.isFunction
import org.jetbrains.kotlin.ir.util.isNullable
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
internal val removeDeclarationsThatWouldBeInlined = makeIrFilePhase(
::RemoveDeclarationsThatWouldBeInlinedLowering,
name = "RemoveInlinedDeclarations",
description = "Rename declaration that should be inlined"
)
private class RemoveDeclarationsThatWouldBeInlinedLowering(val context: JvmBackendContext) : FileLoweringPass {
override fun lower(irFile: IrFile) {
val loweredLambdasToDelete = hashSetOf<IrDeclaration>()
irFile.transformChildrenVoid(object : IrElementTransformerVoid() {
override fun visitCall(expression: IrCall): IrExpression {
val owner = expression.symbol.owner
if (expression.descriptor.isInlineCall(context.state)) {
owner.valueParameters.filter {
!it.isNoinline && it.type.isFunction() && !it.type.isNullable()
}.forEach {
val valueArgument = expression.getValueArgument(it.index) as? IrContainerExpression ?: return@forEach
if (valueArgument.origin == IrStatementOrigin.LAMBDA) {
val reference =
valueArgument.statements.firstIsInstanceOrNull<IrFunctionReference>() ?: return@forEach
loweredLambdasToDelete.add(reference.symbol.owner)
}
}
}
return super.visitCall(expression)
}
})
irFile.transformChildrenVoid(object : IrElementTransformerVoid() {
override fun visitClass(declaration: IrClass): IrStatement {
return super.visitClass(declaration).also {
declaration.declarations.removeAll(loweredLambdasToDelete)
}
}
})
}
}