Move out jvm-specific part from ir-common

#KT-27005 Fixed
This commit is contained in:
Mikhael Bogdanov
2018-10-01 11:23:23 +02:00
parent 023e4e3a0e
commit 4fb434c0de
11 changed files with 136 additions and 89 deletions
@@ -23,12 +23,18 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.util.PatchDeclarationParentsVisitor
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.name.NameUtils
class JvmLower(val context: JvmBackendContext) {
fun lower(irFile: IrFile) {
// TODO run lowering passes as callbacks in bottom-up visitor
JvmCoercionToUnitPatcher(
context.builtIns,
context.irBuiltIns,
TypeTranslator(context.ir.symbols.externalSymbolTable, context.state.languageVersionSettings)
).lower(irFile)
FileClassLowering(context).lower(irFile)
KCallableNamePropertyLowering(context).lower(irFile)
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2018 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.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.psi2ir.transformations.InsertImplicitCasts
class JvmCoercionToUnitPatcher(builtIns: KotlinBuiltIns, irBuiltIns: IrBuiltIns, typeTranslator: TypeTranslator) :
InsertImplicitCasts(builtIns, irBuiltIns, typeTranslator), FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(this)
}
override fun IrExpression.coerceToUnit(): IrExpression {
if (isUnitSubtype(getKotlinType(this)) && this is IrCall) {
return coerceToUnitIfNeeded(this.symbol.descriptor.original.returnType!!)
}
return this
}
}
@@ -23,11 +23,13 @@ import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.psi2ir.containsNull
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
@@ -40,15 +42,15 @@ import org.jetbrains.kotlin.types.typeUtil.makeNullable
import org.jetbrains.kotlin.types.upperIfFlexible
fun insertImplicitCasts(element: IrElement, context: GeneratorContext) {
element.transformChildren(InsertImplicitCasts(context), null)
element.transformChildren(InsertImplicitCasts(context.builtIns, context.irBuiltIns, context.typeTranslator), null)
}
class InsertImplicitCasts(context: GeneratorContext) : IrElementTransformerVoid() {
open class InsertImplicitCasts(
private val builtIns: KotlinBuiltIns,
private val irBuiltIns: IrBuiltIns,
private val typeTranslator: TypeTranslator
) : IrElementTransformerVoid() {
private val builtIns = context.builtIns
private val irBuiltIns = context.irBuiltIns
private val typeTranslator = context.typeTranslator
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
override fun visitCallableReference(expression: IrCallableReference): IrExpression =
@@ -232,10 +234,16 @@ class InsertImplicitCasts(context: GeneratorContext) : IrElementTransformerVoid(
)
}
private fun IrExpression.coerceToUnit(): IrExpression {
protected open fun IrExpression.coerceToUnit(): IrExpression {
val valueType = getKotlinType(this)
return coerceToUnitIfNeeded(valueType)
}
return if (KotlinTypeChecker.DEFAULT.isSubtypeOf(valueType, builtIns.unitType))
protected fun getKotlinType(irExpression: IrExpression) =
irExpression.type.originalKotlinType!!
protected fun IrExpression.coerceToUnitIfNeeded(valueType: KotlinType): IrExpression {
return if (isUnitSubtype(valueType))
this
else
IrTypeOperatorCallImpl(
@@ -247,10 +255,8 @@ class InsertImplicitCasts(context: GeneratorContext) : IrElementTransformerVoid(
)
}
private fun getKotlinType(irExpression: IrExpression) =
if (irExpression is IrCall)
irExpression.symbol.descriptor.original.returnType!!
else irExpression.type.originalKotlinType!!
protected fun isUnitSubtype(valueType: KotlinType) =
KotlinTypeChecker.DEFAULT.isSubtypeOf(valueType, builtIns.unitType)
private fun KotlinType.isBuiltInIntegerType(): Boolean =
KotlinBuiltIns.isByte(this) ||
+5
View File
@@ -0,0 +1,5 @@
suspend fun foo() = baz<Unit>()
suspend fun bar() = baz<Any>()
suspend fun <T> baz(): T {
TODO()
}
+15
View File
@@ -0,0 +1,15 @@
FILE fqName:<root> fileName:/kt27005.kt
FUN name:foo visibility:public modality:FINAL <> () returnType:kotlin.Unit flags:suspend
BLOCK_BODY
RETURN type=kotlin.Nothing from='foo(): Unit'
CALL 'baz(): Unit' type=kotlin.Unit origin=null
<T>: kotlin.Unit
FUN name:bar visibility:public modality:FINAL <> () returnType:kotlin.Any flags:suspend
BLOCK_BODY
RETURN type=kotlin.Nothing from='bar(): Any'
CALL 'baz(): Any' type=kotlin.Any origin=null
<T>: kotlin.Any
FUN name:baz visibility:public modality:FINAL <T> () returnType:T flags:suspend
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?]
BLOCK_BODY
CALL 'TODO(): Nothing' type=kotlin.Nothing origin=null
@@ -5,10 +5,8 @@ FILE fqName:<root> fileName:/catchParameterAccess.kt
RETURN type=kotlin.Nothing from='test(() -> Unit): Unit'
TRY type=kotlin.Unit
try: BLOCK type=kotlin.Unit origin=null
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'invoke(): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter f: () -> Unit' type=() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
CALL 'invoke(): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter f: () -> Unit' type=() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
CATCH parameter=e: Exception /* = Exception */
VAR CATCH_PARAMETER name:e type:kotlin.Exception /* = java.lang.Exception */ flags:val
BLOCK type=kotlin.Nothing origin=null
@@ -4,19 +4,15 @@ FILE fqName:<root> fileName:/extFunInvokeAsFun.kt
VALUE_PARAMETER name:block index:1 type:kotlin.Any?.() -> kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='with1(Any?, Any?.() -> Unit): Unit'
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'invoke(Any?): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter block: Any?.() -> Unit' type=kotlin.Any?.() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
p1: GET_VAR 'value-parameter receiver: Any?' type=kotlin.Any? origin=null
CALL 'invoke(Any?): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter block: Any?.() -> Unit' type=kotlin.Any?.() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
p1: GET_VAR 'value-parameter receiver: Any?' type=kotlin.Any? origin=null
FUN name:with2 visibility:public modality:FINAL <> (receiver:kotlin.Any?, block:kotlin.Any?.() -> kotlin.Unit) returnType:kotlin.Unit flags:
VALUE_PARAMETER name:receiver index:0 type:kotlin.Any? flags:
VALUE_PARAMETER name:block index:1 type:kotlin.Any?.() -> kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='with2(Any?, Any?.() -> Unit): Unit'
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'invoke(Any?): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter block: Any?.() -> Unit' type=kotlin.Any?.() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
p1: GET_VAR 'value-parameter receiver: Any?' type=kotlin.Any? origin=null
CALL 'invoke(Any?): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter block: Any?.() -> Unit' type=kotlin.Any?.() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
p1: GET_VAR 'value-parameter receiver: Any?' type=kotlin.Any? origin=null
@@ -74,9 +74,7 @@ FILE fqName:<root> fileName:/implicitCastToNonNull.kt
arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ
arg0: GET_VAR 'value-parameter x: T' type=T origin=null
arg1: CONST Null type=kotlin.Nothing? value=null
then: TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'invoke(S): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter fn: (S) -> Unit' type=(S) -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
p1: GET_VAR 'value-parameter x: T' type=T origin=null
then: CALL 'invoke(S): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter fn: (S) -> Unit' type=(S) -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
p1: GET_VAR 'value-parameter x: T' type=T origin=null
@@ -13,19 +13,15 @@ FILE fqName:<root> fileName:/variableAsFunctionCall.kt
VALUE_PARAMETER name:f index:0 type:() -> kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='test1(() -> Unit): Unit'
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'invoke(): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter f: () -> Unit' type=() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
CALL 'invoke(): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter f: () -> Unit' type=() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
FUN name:test2 visibility:public modality:FINAL <> (f:kotlin.String.() -> kotlin.Unit) returnType:kotlin.Unit flags:
VALUE_PARAMETER name:f index:0 type:kotlin.String.() -> kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='test2(String.() -> Unit): Unit'
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'invoke(String): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter f: String.() -> Unit' type=kotlin.String.() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
p1: CONST String type=kotlin.String value=hello
CALL 'invoke(String): Unit' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'value-parameter f: String.() -> Unit' type=kotlin.String.() -> kotlin.Unit origin=VARIABLE_AS_FUNCTION
p1: CONST String type=kotlin.String value=hello
FUN name:test3 visibility:public modality:FINAL <> () returnType:kotlin.String flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='test3(): String'
+39 -49
View File
@@ -1,61 +1,51 @@
FILE fqName:<root> fileName:/nonLocalReturn.kt
FUN name:test0 visibility:public modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'run(() -> Nothing): Nothing' type=kotlin.Nothing origin=null
<R>: kotlin.Nothing
block: BLOCK type=() -> kotlin.Nothing origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Nothing flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='test0(): Unit'
GET_OBJECT 'Unit' type=kotlin.Unit
FUNCTION_REFERENCE '<anonymous>(): Nothing' type=() -> kotlin.Nothing origin=LAMBDA
CALL 'run(() -> Nothing): Nothing' type=kotlin.Nothing origin=null
<R>: kotlin.Nothing
block: BLOCK type=() -> kotlin.Nothing origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Nothing flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='test0(): Unit'
GET_OBJECT 'Unit' type=kotlin.Unit
FUNCTION_REFERENCE '<anonymous>(): Nothing' type=() -> kotlin.Nothing origin=LAMBDA
FUN name:test1 visibility:public modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'run(() -> Unit): Unit' type=kotlin.Unit origin=null
<R>: kotlin.Unit
block: BLOCK type=() -> kotlin.Unit origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<anonymous>(): Unit'
GET_OBJECT 'Unit' type=kotlin.Unit
FUNCTION_REFERENCE '<anonymous>(): Unit' type=() -> kotlin.Unit origin=LAMBDA
CALL 'run(() -> Unit): Unit' type=kotlin.Unit origin=null
<R>: kotlin.Unit
block: BLOCK type=() -> kotlin.Unit origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<anonymous>(): Unit'
GET_OBJECT 'Unit' type=kotlin.Unit
FUNCTION_REFERENCE '<anonymous>(): Unit' type=() -> kotlin.Unit origin=LAMBDA
FUN name:test2 visibility:public modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'run(() -> Unit): Unit' type=kotlin.Unit origin=null
<R>: kotlin.Unit
block: BLOCK type=() -> kotlin.Unit origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<anonymous>(): Unit'
GET_OBJECT 'Unit' type=kotlin.Unit
FUNCTION_REFERENCE '<anonymous>(): Unit' type=() -> kotlin.Unit origin=LAMBDA
CALL 'run(() -> Unit): Unit' type=kotlin.Unit origin=null
<R>: kotlin.Unit
block: BLOCK type=() -> kotlin.Unit origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<anonymous>(): Unit'
GET_OBJECT 'Unit' type=kotlin.Unit
FUNCTION_REFERENCE '<anonymous>(): Unit' type=() -> kotlin.Unit origin=LAMBDA
FUN name:test3 visibility:public modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'run(() -> Unit): Unit' type=kotlin.Unit origin=null
<R>: kotlin.Unit
block: BLOCK type=() -> kotlin.Unit origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<anonymous>(): Unit'
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
CALL 'run(() -> Nothing): Nothing' type=kotlin.Nothing origin=null
<R>: kotlin.Nothing
block: BLOCK type=() -> kotlin.Nothing origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Nothing flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<anonymous>(): Unit'
GET_OBJECT 'Unit' type=kotlin.Unit
FUNCTION_REFERENCE '<anonymous>(): Nothing' type=() -> kotlin.Nothing origin=LAMBDA
FUNCTION_REFERENCE '<anonymous>(): Unit' type=() -> kotlin.Unit origin=LAMBDA
CALL 'run(() -> Unit): Unit' type=kotlin.Unit origin=null
<R>: kotlin.Unit
block: BLOCK type=() -> kotlin.Unit origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Unit flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<anonymous>(): Unit'
CALL 'run(() -> Nothing): Nothing' type=kotlin.Nothing origin=null
<R>: kotlin.Nothing
block: BLOCK type=() -> kotlin.Nothing origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Nothing flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<anonymous>(): Unit'
GET_OBJECT 'Unit' type=kotlin.Unit
FUNCTION_REFERENCE '<anonymous>(): Nothing' type=() -> kotlin.Nothing origin=LAMBDA
FUNCTION_REFERENCE '<anonymous>(): Unit' type=() -> kotlin.Unit origin=LAMBDA
FUN name:testLrmFoo1 visibility:public modality:FINAL <> (ints:kotlin.collections.List<kotlin.Int>) returnType:kotlin.Unit flags:
VALUE_PARAMETER name:ints index:0 type:kotlin.collections.List<kotlin.Int> flags:
BLOCK_BODY
@@ -259,6 +259,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
runTest("compiler/testData/ir/irText/declarations/interfaceProperties.kt");
}
@TestMetadata("kt27005.kt")
public void testKt27005() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt27005.kt");
}
@TestMetadata("localClassWithOverrides.kt")
public void testLocalClassWithOverrides() throws Exception {
runTest("compiler/testData/ir/irText/declarations/localClassWithOverrides.kt");