IR: remove unneeded casts after making IrCall's symbol a simple function

This commit is contained in:
Alexander Udalov
2020-08-14 12:28:49 +02:00
parent 3bdbfc1e73
commit 9961bd1fe1
11 changed files with 18 additions and 39 deletions
@@ -220,12 +220,11 @@ class InlineClassLowering(val context: CommonBackendContext) {
override fun visitCall(expression: IrCall): IrExpression { override fun visitCall(expression: IrCall): IrExpression {
expression.transformChildrenVoid(this) expression.transformChildrenVoid(this)
val function = expression.symbol.owner val function: IrSimpleFunction = expression.symbol.owner
if (function.parent !is IrClass || if (function.parent !is IrClass ||
function.isStaticMethodOfClass || function.isStaticMethodOfClass ||
!function.parentAsClass.isInline || !function.parentAsClass.isInline ||
(function is IrSimpleFunction && !function.isReal) || !function.isReal
(function is IrConstructor && function.isPrimary)
) { ) {
return expression return expression
} }
@@ -233,7 +232,7 @@ class InlineClassLowering(val context: CommonBackendContext) {
return irCall( return irCall(
expression, expression,
getOrCreateStaticMethod(function), getOrCreateStaticMethod(function),
receiversAsArguments = (function is IrSimpleFunction) receiversAsArguments = true
) )
} }
@@ -31,7 +31,7 @@ class PropertyAccessorInlineLowering(private val context: CommonBackendContext)
override fun visitCall(expression: IrCall): IrExpression { override fun visitCall(expression: IrCall): IrExpression {
expression.transformChildrenVoid(this) expression.transformChildrenVoid(this)
val callee = expression.symbol.owner as IrSimpleFunction val callee = expression.symbol.owner
val property = callee.correspondingPropertySymbol?.owner ?: return expression val property = callee.correspondingPropertySymbol?.owner ?: return expression
// Some devirtualization required here // Some devirtualization required here
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.ir.backend.js.lower package org.jetbrains.kotlin.ir.backend.js.lower
import org.jetbrains.kotlin.backend.common.BodyLoweringPass import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclaration import org.jetbrains.kotlin.ir.declarations.IrDeclaration
@@ -73,9 +72,7 @@ class PrimitiveCompanionLowering(val context: JsIrBackendContext) : BodyLowering
override fun visitCall(expression: IrCall): IrExpression { override fun visitCall(expression: IrCall): IrExpression {
val newCall = super.visitCall(expression) as IrCall val newCall = super.visitCall(expression) as IrCall
val function = expression.symbol.owner as IrSimpleFunction val actualFunction = getActualPrimitiveCompanionPropertyAccessor(expression.symbol.owner)
val actualFunction = getActualPrimitiveCompanionPropertyAccessor(function)
?: return newCall ?: return newCall
return irCall(newCall, actualFunction) return irCall(newCall, actualFunction)
@@ -59,13 +59,12 @@ class PrimitiveContainerMemberCallTransformer(private val context: JsIrBackendCo
} }
} }
override fun transformFunctionAccess(expression: IrFunctionAccessExpression): IrExpression { override fun transformFunctionAccess(call: IrFunctionAccessExpression): IrExpression {
val symbol = expression.symbol symbolToTransformer[call.symbol]?.let {
symbolToTransformer[symbol]?.let { return it(call)
return it(expression)
} }
return expression return call
} }
} }
@@ -78,11 +78,8 @@ fun translateCall(
transformer: IrElementToJsExpressionTransformer transformer: IrElementToJsExpressionTransformer
): JsExpression { ): JsExpression {
val function = expression.symbol.owner.realOverrideTarget val function = expression.symbol.owner.realOverrideTarget
require(function is IrSimpleFunction) { "Only IrSimpleFunction could be called via IrCall" } // TODO: fix it in IrCall
val symbol = function.symbol context.staticContext.intrinsics[function.symbol]?.let {
context.staticContext.intrinsics[symbol]?.let {
return it(expression, context) return it(expression, context)
} }
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.deserialization.PLATFORM_DEPENDENT_ANNOTATION_FQ_NAME import org.jetbrains.kotlin.descriptors.deserialization.PLATFORM_DEPENDENT_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.irBlockBody import org.jetbrains.kotlin.ir.builders.irBlockBody
import org.jetbrains.kotlin.ir.builders.irCall import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irGet import org.jetbrains.kotlin.ir.builders.irGet
@@ -128,7 +127,7 @@ private class InterfaceSuperCallsLowering(val context: JvmBackendContext) : IrEl
return super.visitCall(expression) return super.visitCall(expression)
} }
val superCallee = expression.symbol.owner as IrSimpleFunction val superCallee = expression.symbol.owner
if (superCallee.isDefinitelyNotDefaultImplsMethod(context.state.jvmDefaultMode)) return super.visitCall(expression) if (superCallee.isDefinitelyNotDefaultImplsMethod(context.state.jvmDefaultMode)) return super.visitCall(expression)
val redirectTarget = context.cachedDeclarations.getDefaultImplsFunction(superCallee) val redirectTarget = context.cachedDeclarations.getDefaultImplsFunction(superCallee)
@@ -159,7 +158,7 @@ private class InterfaceDefaultCallsLowering(val context: JvmBackendContext) : Ir
return super.visitCall(expression) return super.visitCall(expression)
} }
val redirectTarget = context.cachedDeclarations.getDefaultImplsFunction(callee as IrSimpleFunction) val redirectTarget = context.cachedDeclarations.getDefaultImplsFunction(callee)
// InterfaceLowering bridges from DefaultImpls in compatibility mode -- if that's the case, // InterfaceLowering bridges from DefaultImpls in compatibility mode -- if that's the case,
// this phase will inadvertently cause a recursive loop as the bridge on the DefaultImpls // this phase will inadvertently cause a recursive loop as the bridge on the DefaultImpls
@@ -201,7 +200,7 @@ private class InterfaceObjectCallsLowering(val context: JvmBackendContext) : IrE
if (expression.superQualifierSymbol != null && !expression.isSuperToAny()) if (expression.superQualifierSymbol != null && !expression.isSuperToAny())
return super.visitCall(expression) return super.visitCall(expression)
val callee = expression.symbol.owner val callee = expression.symbol.owner
if (callee !is IrSimpleFunction || !callee.hasInterfaceParent()) if (!callee.hasInterfaceParent())
return super.visitCall(expression) return super.visitCall(expression)
val resolved = callee.resolveFakeOverride() val resolved = callee.resolveFakeOverride()
if (resolved?.isMethodOfAny() != true) if (resolved?.isMethodOfAny() != true)
@@ -165,7 +165,7 @@ private class MakeCallsStatic(
// Imported functions do not have their receiver parameter nulled by SingletonObjectJvmStaticLowering, // Imported functions do not have their receiver parameter nulled by SingletonObjectJvmStaticLowering,
// so we have to do it here. // so we have to do it here.
// TODO: would be better handled by lowering imported declarations. // TODO: would be better handled by lowering imported declarations.
val callee = expression.symbol.owner as IrSimpleFunction val callee = expression.symbol.owner
val newCallee = if (!callee.isInCurrentModule()) { val newCallee = if (!callee.isInCurrentModule()) {
callee.copyRemovingDispatchReceiver() // TODO: cache these callee.copyRemovingDispatchReceiver() // TODO: cache these
} else callee } else callee
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
@@ -43,7 +42,7 @@ private class ReplaceKFunctionInvokeWithFunctionInvoke : FileLoweringPass, IrEle
override fun visitCall(expression: IrCall): IrExpression { override fun visitCall(expression: IrCall): IrExpression {
val callee = expression.symbol.owner val callee = expression.symbol.owner
if (callee !is IrSimpleFunction || callee.name != OperatorNameConventions.INVOKE) return super.visitCall(expression) if (callee.name != OperatorNameConventions.INVOKE) return super.visitCall(expression)
val parentClass = callee.parent as? IrClass ?: return super.visitCall(expression) val parentClass = callee.parent as? IrClass ?: return super.visitCall(expression)
if (!parentClass.defaultType.isKFunction() && !parentClass.defaultType.isKSuspendFunction()) return super.visitCall(expression) if (!parentClass.defaultType.isKFunction() && !parentClass.defaultType.isKSuspendFunction()) return super.visitCall(expression)
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.backend.common.ir.isElseBranch
import org.jetbrains.kotlin.backend.wasm.ast.* import org.jetbrains.kotlin.backend.wasm.ast.*
import org.jetbrains.kotlin.backend.wasm.utils.getWasmInstructionAnnotation import org.jetbrains.kotlin.backend.wasm.utils.getWasmInstructionAnnotation
import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.isUnit import org.jetbrains.kotlin.ir.types.isUnit
@@ -89,7 +88,6 @@ class ExpressionTransformer : BaseTransformer<WasmInstruction, WasmCodegenContex
override fun visitCall(expression: IrCall, data: WasmCodegenContext): WasmInstruction { override fun visitCall(expression: IrCall, data: WasmCodegenContext): WasmInstruction {
val function = expression.symbol.owner.realOverrideTarget val function = expression.symbol.owner.realOverrideTarget
require(function is IrSimpleFunction) { "Only IrSimpleFunction could be called via IrCall" }
val valueArgs = (0 until expression.valueArgumentsCount).mapNotNull { expression.getValueArgument(it) } val valueArgs = (0 until expression.valueArgumentsCount).mapNotNull { expression.getValueArgument(it) }
val irArguments = listOfNotNull(expression.dispatchReceiver, expression.extensionReceiver) + valueArgs val irArguments = listOfNotNull(expression.dispatchReceiver, expression.extensionReceiver) + valueArgs
val wasmArguments = irArguments.map { expressionToWasmInstruction(it, data) } val wasmArguments = irArguments.map { expressionToWasmInstruction(it, data) }
@@ -5,13 +5,13 @@
package org.jetbrains.kotlin.ir.interpreter.state package org.jetbrains.kotlin.ir.interpreter.state
import org.jetbrains.kotlin.ir.interpreter.getLastOverridden
import org.jetbrains.kotlin.ir.interpreter.stack.Variable
import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrProperty import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.interpreter.getLastOverridden
import org.jetbrains.kotlin.ir.interpreter.stack.Variable
import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classOrNull import org.jetbrains.kotlin.ir.types.classOrNull
@@ -33,7 +33,7 @@ internal class Primitive<T>(var value: T, val type: IrType) : State {
// must add property's getter to declaration's list because they are not present in ir class for primitives // must add property's getter to declaration's list because they are not present in ir class for primitives
val declarations = irClass.declarations.map { if (it is IrProperty) it.getter else it } val declarations = irClass.declarations.map { if (it is IrProperty) it.getter else it }
return declarations.filterIsInstance<IrFunction>() return declarations.filterIsInstance<IrFunction>()
.firstOrNull { it.symbol == owner.symbol || (it is IrSimpleFunction && owner is IrSimpleFunction && it.overrides(owner)) } .firstOrNull { it.symbol == owner.symbol || (it is IrSimpleFunction && it.overrides(owner)) }
?.let { if (it.isFakeOverride) it.getLastOverridden() else it } ?.let { if (it.isFakeOverride) it.getLastOverridden() else it }
} }
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.ir.builders package org.jetbrains.kotlin.ir.builders
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
@@ -278,14 +277,6 @@ fun IrBuilderWithScope.irCall(callee: IrConstructorSymbol): IrConstructorCall =
fun IrBuilderWithScope.irCall(callee: IrFunctionSymbol): IrFunctionAccessExpression = fun IrBuilderWithScope.irCall(callee: IrFunctionSymbol): IrFunctionAccessExpression =
irCall(callee, callee.owner.returnType) irCall(callee, callee.owner.returnType)
fun IrBuilderWithScope.irCall(callee: IrFunctionSymbol, descriptor: FunctionDescriptor, type: IrType): IrCall =
IrCallImpl(
startOffset, endOffset, type,
callee as IrSimpleFunctionSymbol,
valueArgumentsCount = callee.owner.valueParameters.size,
typeArgumentsCount = callee.owner.typeParameters.size
)
fun IrBuilderWithScope.irCall(callee: IrFunction): IrFunctionAccessExpression = fun IrBuilderWithScope.irCall(callee: IrFunction): IrFunctionAccessExpression =
irCall(callee.symbol) irCall(callee.symbol)