IR: IrCall.fromSymbolDescriptor

This commit is contained in:
Georgy Bronnikov
2020-10-14 12:31:00 +03:00
parent ab3ef3a7e9
commit b28c85f5ea
21 changed files with 69 additions and 60 deletions
@@ -127,7 +127,7 @@ class JvmSharedVariablesManager(
} }
private fun unsafeCoerce(value: IrExpression, from: IrType, to: IrType): IrExpression = private fun unsafeCoerce(value: IrExpression, from: IrType, to: IrType): IrExpression =
IrCallImpl(value.startOffset, value.endOffset, to, symbols.unsafeCoerceIntrinsic).apply { IrCallImpl.fromSymbolOwner(value.startOffset, value.endOffset, to, symbols.unsafeCoerceIntrinsic).apply {
putTypeArgument(0, from) putTypeArgument(0, from)
putTypeArgument(1, to) putTypeArgument(1, to)
putValueArgument(0, value) putValueArgument(0, value)
@@ -124,7 +124,7 @@ fun IrType.defaultValue(startOffset: Int, endOffset: Int, context: JvmBackendCon
val underlyingType = unboxInlineClass() val underlyingType = unboxInlineClass()
val defaultValueForUnderlyingType = IrConstImpl.defaultValueForType(startOffset, endOffset, underlyingType) val defaultValueForUnderlyingType = IrConstImpl.defaultValueForType(startOffset, endOffset, underlyingType)
return IrCallImpl(startOffset, endOffset, this, context.ir.symbols.unsafeCoerceIntrinsic).also { return IrCallImpl.fromSymbolOwner(startOffset, endOffset, this, context.ir.symbols.unsafeCoerceIntrinsic).also {
it.putTypeArgument(0, underlyingType) // from it.putTypeArgument(0, underlyingType) // from
it.putTypeArgument(1, this) // to it.putTypeArgument(1, this) // to
it.putValueArgument(0, defaultValueForUnderlyingType) it.putValueArgument(0, defaultValueForUnderlyingType)
@@ -76,7 +76,10 @@ private class AddContinuationLowering(context: JvmBackendContext) : SuspendLower
override fun visitCall(expression: IrCall): IrExpression { override fun visitCall(expression: IrCall): IrExpression {
val transformed = super.visitCall(expression) as IrCall val transformed = super.visitCall(expression) as IrCall
return transformed.retargetToSuspendView(context, functionStack.peek() ?: return transformed) { return transformed.retargetToSuspendView(context, functionStack.peek() ?: return transformed) {
IrCallImpl(startOffset, endOffset, type, it, origin, superQualifierSymbol) IrCallImpl.fromSymbolOwner(
startOffset, endOffset, type, it,
origin = origin, superQualifierSymbol = superQualifierSymbol
)
} }
} }
}) })
@@ -269,7 +269,7 @@ private class UpdateFunctionCallSites(
return expression.run { return expression.run {
// TODO: deduplicate this with ReplaceKFunctionInvokeWithFunctionInvoke // TODO: deduplicate this with ReplaceKFunctionInvokeWithFunctionInvoke
IrCallImpl(startOffset, endOffset, type, newFunction.symbol).apply { IrCallImpl.fromSymbolOwner(startOffset, endOffset, type, newFunction.symbol).apply {
copyTypeArgumentsFrom(expression) copyTypeArgumentsFrom(expression)
extensionReceiver = expression.extensionReceiver?.transform(this@UpdateFunctionCallSites, null) extensionReceiver = expression.extensionReceiver?.transform(this@UpdateFunctionCallSites, null)
for (i in 0 until valueArgumentsCount) { for (i in 0 until valueArgumentsCount) {
@@ -193,7 +193,7 @@ internal class InterfaceLowering(val context: JvmBackendContext) : IrElementTran
// Bridge from static to static method - simply fill the function arguments to the parameters. // Bridge from static to static method - simply fill the function arguments to the parameters.
// By nature of the generation of both source and target of bridge, they line up. // By nature of the generation of both source and target of bridge, they line up.
private fun IrFunction.bridgeToStatic(callTarget: IrSimpleFunction) { private fun IrFunction.bridgeToStatic(callTarget: IrSimpleFunction) {
body = IrExpressionBodyImpl(IrCallImpl(startOffset, endOffset, returnType, callTarget.symbol).also { call -> body = IrExpressionBodyImpl(IrCallImpl.fromSymbolOwner(startOffset, endOffset, returnType, callTarget.symbol).also { call ->
callTarget.typeParameters.forEachIndexed { i, _ -> callTarget.typeParameters.forEachIndexed { i, _ ->
call.putTypeArgument(i, createPlaceholderAnyNType(context.irBuiltIns)) call.putTypeArgument(i, createPlaceholderAnyNType(context.irBuiltIns))
@@ -209,7 +209,7 @@ internal class InterfaceLowering(val context: JvmBackendContext) : IrElementTran
// be shifted in presence of dispatch and extension receiver. // be shifted in presence of dispatch and extension receiver.
private fun IrFunction.bridgeViaAccessorTo(callTarget: IrSimpleFunction) { private fun IrFunction.bridgeViaAccessorTo(callTarget: IrSimpleFunction) {
body = IrExpressionBodyImpl( body = IrExpressionBodyImpl(
IrCallImpl( IrCallImpl.fromSymbolOwner(
startOffset, startOffset,
endOffset, endOffset,
returnType, returnType,
@@ -302,7 +302,7 @@ private class JvmInlineClassLowering(private val context: JvmBackendContext) : F
} }
private fun coerceInlineClasses(argument: IrExpression, from: IrType, to: IrType) = private fun coerceInlineClasses(argument: IrExpression, from: IrType, to: IrType) =
IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, to, context.ir.symbols.unsafeCoerceIntrinsic).apply { IrCallImpl.fromSymbolOwner(UNDEFINED_OFFSET, UNDEFINED_OFFSET, to, context.ir.symbols.unsafeCoerceIntrinsic).apply {
putTypeArgument(0, from) putTypeArgument(0, from)
putTypeArgument(1, to) putTypeArgument(1, to)
putValueArgument(0, argument) putValueArgument(0, argument)
@@ -204,7 +204,7 @@ class JvmOptimizationLowering(val context: JvmBackendContext) : FileLoweringPass
"Failing expression: ${expression.dump()}" "Failing expression: ${expression.dump()}"
} }
// Replace conjunction condition with intrinsic "and" function call // Replace conjunction condition with intrinsic "and" function call
return IrCallImpl( return IrCallImpl.fromSymbolOwner(
expression.startOffset, expression.startOffset,
expression.endOffset, expression.endOffset,
context.irBuiltIns.booleanType, context.irBuiltIns.booleanType,
@@ -225,7 +225,7 @@ class JvmOptimizationLowering(val context: JvmBackendContext) : FileLoweringPass
"and an 'if true then b' body on its second branch. " + "and an 'if true then b' body on its second branch. " +
"Failing expression: ${expression.dump()}" "Failing expression: ${expression.dump()}"
} }
return IrCallImpl( return IrCallImpl.fromSymbolOwner(
expression.startOffset, expression.startOffset,
expression.endOffset, expression.endOffset,
context.irBuiltIns.booleanType, context.irBuiltIns.booleanType,
@@ -60,7 +60,7 @@ private class JvmOverloadsAnnotationLowering(val context: JvmBackendContext) : C
is IrConstructor -> is IrConstructor ->
IrDelegatingConstructorCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, context.irBuiltIns.unitType, target.symbol) IrDelegatingConstructorCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, context.irBuiltIns.unitType, target.symbol)
is IrSimpleFunction -> is IrSimpleFunction ->
IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, target.returnType, target.symbol) IrCallImpl.fromSymbolOwner(UNDEFINED_OFFSET, UNDEFINED_OFFSET, target.returnType, target.symbol)
else -> else ->
error("unknown function kind: ${target.render()}") error("unknown function kind: ${target.render()}")
} }
@@ -60,7 +60,7 @@ class JvmStandardLibraryBuiltInsLowering(val context: JvmBackendContext) : FileL
// Originals are so far only instance methods, and the replacements are // Originals are so far only instance methods, and the replacements are
// statics, so we copy dispatch receivers to a value argument if needed. // statics, so we copy dispatch receivers to a value argument if needed.
private fun IrCall.replaceWithCallTo(replacement: IrSimpleFunctionSymbol) = private fun IrCall.replaceWithCallTo(replacement: IrSimpleFunctionSymbol) =
IrCallImpl( IrCallImpl.fromSymbolOwner(
startOffset, startOffset,
endOffset, endOffset,
type, type,
@@ -77,7 +77,7 @@ class JvmStandardLibraryBuiltInsLowering(val context: JvmBackendContext) : FileL
} }
private fun IrExpression.coerceTo(target: IrType): IrExpression = private fun IrExpression.coerceTo(target: IrType): IrExpression =
IrCallImpl( IrCallImpl.fromSymbolOwner(
startOffset, startOffset,
endOffset, endOffset,
target, target,
@@ -77,7 +77,10 @@ class PolymorphicSignatureLowering(val context: JvmBackendContext) : IrElementTr
addValueParameter("\$$i", value.type, JvmLoweredDeclarationOrigin.POLYMORPHIC_SIGNATURE_INSTANTIATION) addValueParameter("\$$i", value.type, JvmLoweredDeclarationOrigin.POLYMORPHIC_SIGNATURE_INSTANTIATION)
} }
} }
return IrCallImpl(startOffset, endOffset, fakeFunction.returnType, fakeFunction.symbol, origin, superQualifierSymbol).apply { return IrCallImpl.fromSymbolOwner(
startOffset, endOffset, fakeFunction.returnType, fakeFunction.symbol,
origin = origin, superQualifierSymbol = superQualifierSymbol
).apply {
copyTypeArgumentsFrom(this@transform) copyTypeArgumentsFrom(this@transform)
dispatchReceiver = this@transform.dispatchReceiver dispatchReceiver = this@transform.dispatchReceiver
extensionReceiver = this@transform.extensionReceiver extensionReceiver = this@transform.extensionReceiver
@@ -50,7 +50,7 @@ private class ReplaceKFunctionInvokeWithFunctionInvoke : FileLoweringPass, IrEle
// The single overridden function of KFunction{n}.invoke must be Function{n}.invoke. // The single overridden function of KFunction{n}.invoke must be Function{n}.invoke.
val newCallee = callee.overriddenSymbols.single() val newCallee = callee.overriddenSymbols.single()
return expression.run { return expression.run {
IrCallImpl(startOffset, endOffset, type, newCallee).apply { IrCallImpl.fromSymbolOwner(startOffset, endOffset, type, newCallee).apply {
copyTypeArgumentsFrom(expression) copyTypeArgumentsFrom(expression)
dispatchReceiver = expression.dispatchReceiver?.transform(this@ReplaceKFunctionInvokeWithFunctionInvoke, null)?.let { dispatchReceiver = expression.dispatchReceiver?.transform(this@ReplaceKFunctionInvokeWithFunctionInvoke, null)?.let {
val newType = newCallee.owner.parentAsClass.defaultType val newType = newCallee.owner.parentAsClass.defaultType
@@ -49,6 +49,7 @@ class ResolveInlineCalls(val context: JvmBackendContext) : IrElementTransformerV
type, type,
resolved.symbol, resolved.symbol,
expression.typeArgumentsCount, expression.typeArgumentsCount,
expression.valueArgumentsCount,
expression.origin, expression.origin,
superQualifierSymbol superQualifierSymbol
).apply { ).apply {
@@ -314,7 +314,7 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
} }
private fun createSimpleFunctionCall(accessor: IrFunction, targetSymbol: IrSimpleFunctionSymbol, superQualifierSymbol: IrClassSymbol?) = private fun createSimpleFunctionCall(accessor: IrFunction, targetSymbol: IrSimpleFunctionSymbol, superQualifierSymbol: IrClassSymbol?) =
IrCallImpl( IrCallImpl.fromSymbolOwner(
accessor.startOffset, accessor.startOffset,
accessor.endOffset, accessor.endOffset,
accessor.returnType, accessor.returnType,
@@ -412,11 +412,11 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
accessorSymbol: IrFunctionSymbol accessorSymbol: IrFunctionSymbol
): IrFunctionAccessExpression { ): IrFunctionAccessExpression {
val newExpression = when (oldExpression) { val newExpression = when (oldExpression) {
is IrCall -> IrCallImpl( is IrCall -> IrCallImpl.fromSymbolOwner(
oldExpression.startOffset, oldExpression.endOffset, oldExpression.startOffset, oldExpression.endOffset,
oldExpression.type, oldExpression.type,
accessorSymbol as IrSimpleFunctionSymbol, oldExpression.typeArgumentsCount, accessorSymbol as IrSimpleFunctionSymbol, oldExpression.typeArgumentsCount,
oldExpression.origin origin = oldExpression.origin
) )
is IrDelegatingConstructorCall -> IrDelegatingConstructorCallImpl( is IrDelegatingConstructorCall -> IrDelegatingConstructorCallImpl(
oldExpression.startOffset, oldExpression.endOffset, oldExpression.startOffset, oldExpression.endOffset,
@@ -457,7 +457,7 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
val call = IrCallImpl( val call = IrCallImpl(
oldExpression.startOffset, oldExpression.endOffset, oldExpression.startOffset, oldExpression.endOffset,
oldExpression.type, oldExpression.type,
accessorSymbol, 0, accessorSymbol, 0, accessorSymbol.owner.valueParameters.size,
oldExpression.origin oldExpression.origin
) )
oldExpression.receiver?.let { oldExpression.receiver?.let {
@@ -473,7 +473,7 @@ internal class SyntheticAccessorLowering(val context: JvmBackendContext) : IrEle
val call = IrCallImpl( val call = IrCallImpl(
oldExpression.startOffset, oldExpression.endOffset, oldExpression.startOffset, oldExpression.endOffset,
oldExpression.type, oldExpression.type,
accessorSymbol, 0, accessorSymbol, 0, accessorSymbol.owner.valueParameters.size,
oldExpression.origin oldExpression.origin
) )
oldExpression.receiver?.let { oldExpression.receiver?.let {
@@ -143,7 +143,7 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
val isExhaustive = whenExpression.isExhaustiveWhen() val isExhaustive = whenExpression.isExhaustiveWhen()
if (isExhaustive) { if (isExhaustive) {
val call = IrCallImpl( val call = IrCallImpl.fromSymbolDescriptor(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, UNDEFINED_OFFSET, UNDEFINED_OFFSET,
context.irBuiltIns.nothingType, context.irBuiltIns.nothingType,
context.irBuiltIns.noWhenBranchMatchedExceptionSymbol context.irBuiltIns.noWhenBranchMatchedExceptionSymbol
@@ -122,8 +122,8 @@ class CallGenerator(statementGenerator: StatementGenerator) : StatementGenerator
if (descriptor is LocalVariableDescriptor && descriptor.isDelegated) { if (descriptor is LocalVariableDescriptor && descriptor.isDelegated) {
val getterDescriptor = descriptor.getter!! val getterDescriptor = descriptor.getter!!
val getterSymbol = context.symbolTable.referenceSimpleFunction(getterDescriptor.original) val getterSymbol = context.symbolTable.referenceSimpleFunction(getterDescriptor.original)
IrCallImpl( IrCallImpl.fromSymbolDescriptor(
startOffset, endOffset, descriptor.type.toIrType(), getterSymbol, origin ?: IrStatementOrigin.GET_LOCAL_PROPERTY startOffset, endOffset, descriptor.type.toIrType(), getterSymbol, origin = origin ?: IrStatementOrigin.GET_LOCAL_PROPERTY
).apply { ).apply {
context.callToSubstitutedDescriptorMap[this] = getterDescriptor context.callToSubstitutedDescriptorMap[this] = getterDescriptor
putTypeArguments(typeArguments) { it.toIrType() } putTypeArguments(typeArguments) { it.toIrType() }
@@ -333,12 +333,12 @@ class CallGenerator(statementGenerator: StatementGenerator) : StatementGenerator
} }
} else { } else {
val originalSymbol = context.symbolTable.referenceSimpleFunction(functionDescriptor.original) val originalSymbol = context.symbolTable.referenceSimpleFunction(functionDescriptor.original)
IrCallImpl( IrCallImpl.fromSymbolDescriptor(
startOffset, endOffset, startOffset, endOffset,
irType, irType,
originalSymbol, originalSymbol,
origin, origin = origin,
call.superQualifier?.let { context.symbolTable.referenceClass(it) } superQualifierSymbol = call.superQualifier?.let { context.symbolTable.referenceClass(it) }
).run { ).run {
context.callToSubstitutedDescriptorMap[this] = functionDescriptor context.callToSubstitutedDescriptorMap[this] = functionDescriptor
putTypeArguments(call.typeArguments) { it.toIrType() } putTypeArguments(call.typeArguments) { it.toIrType() }
@@ -334,7 +334,7 @@ class ClassGenerator(
val delegateToSymbol = context.symbolTable.referenceSimpleFunction(delegateToDescriptor.original) val delegateToSymbol = context.symbolTable.referenceSimpleFunction(delegateToDescriptor.original)
val irCall = IrCallImpl( val irCall = IrCallImpl.fromSymbolDescriptor(
startOffset, endOffset, startOffset, endOffset,
returnType.toIrType(), returnType.toIrType(),
delegateToSymbol, delegateToSymbol,
@@ -414,7 +414,7 @@ class OperatorExpressionGenerator(statementGenerator: StatementGenerator) : Stat
receiver: IrExpression receiver: IrExpression
): IrExpression { ): IrExpression {
val originalSymbol = context.symbolTable.referenceSimpleFunction(functionDescriptor.original) val originalSymbol = context.symbolTable.referenceSimpleFunction(functionDescriptor.original)
return IrCallImpl( return IrCallImpl.fromSymbolDescriptor(
startOffset, startOffset,
endOffset, endOffset,
functionDescriptor.returnType!!.toIrType(), functionDescriptor.returnType!!.toIrType(),
@@ -508,11 +508,11 @@ class OperatorExpressionGenerator(statementGenerator: StatementGenerator) : Stat
) ?: throw AssertionError("Substitution failed for $checkNotNull: T=$argumentType") ) ?: throw AssertionError("Substitution failed for $checkNotNull: T=$argumentType")
val checkNotNullSymbol = context.irBuiltIns.checkNotNullSymbol val checkNotNullSymbol = context.irBuiltIns.checkNotNullSymbol
return IrCallImpl( return IrCallImpl.fromSymbolDescriptor(
ktOperator.startOffsetSkippingComments, ktOperator.endOffset, ktOperator.startOffsetSkippingComments, ktOperator.endOffset,
expressionType.toIrType(), expressionType.toIrType(),
checkNotNullSymbol, checkNotNullSymbol,
origin origin = origin
).apply { ).apply {
context.callToSubstitutedDescriptorMap[this] = checkNotNullSubstituted context.callToSubstitutedDescriptorMap[this] = checkNotNullSubstituted
putTypeArgument(0, argumentType.toIrType().makeNotNull()) putTypeArgument(0, argumentType.toIrType().makeNotNull())
@@ -202,7 +202,7 @@ class ReflectionReferencesGenerator(statementGenerator: StatementGenerator) : St
is IrConstructorSymbol -> is IrConstructorSymbol ->
IrConstructorCallImpl.fromSymbolDescriptor(startOffset, endOffset, irType, adapteeSymbol) IrConstructorCallImpl.fromSymbolDescriptor(startOffset, endOffset, irType, adapteeSymbol)
is IrSimpleFunctionSymbol -> is IrSimpleFunctionSymbol ->
IrCallImpl(startOffset, endOffset, irType, adapteeSymbol, origin = null, superQualifierSymbol = null) IrCallImpl.fromSymbolDescriptor(startOffset, endOffset, irType, adapteeSymbol)
else -> error("Unknown symbol kind $adapteeSymbol") else -> error("Unknown symbol kind $adapteeSymbol")
} }
@@ -36,10 +36,10 @@ class DelegatedLocalPropertyLValue(
AssignmentReceiver { AssignmentReceiver {
override fun load(): IrExpression = override fun load(): IrExpression =
IrCallImpl(startOffset, endOffset, type, getterSymbol!!, origin) IrCallImpl.fromSymbolDescriptor(startOffset, endOffset, type, getterSymbol!!, origin = origin)
override fun store(irExpression: IrExpression): IrExpression = override fun store(irExpression: IrExpression): IrExpression =
IrCallImpl(startOffset, endOffset, context.irBuiltIns.unitType, setterSymbol!!, origin).apply { IrCallImpl.fromSymbolDescriptor(startOffset, endOffset, context.irBuiltIns.unitType, setterSymbol!!, origin = origin).apply {
putValueArgument(0, irExpression) putValueArgument(0, irExpression)
} }
@@ -43,33 +43,35 @@ class IrCallImpl(
} }
} }
@ObsoleteDescriptorBasedAPI
constructor(
startOffset: Int,
endOffset: Int,
type: IrType,
symbol: IrSimpleFunctionSymbol,
origin: IrStatementOrigin? = null,
superQualifierSymbol: IrClassSymbol? = null
) : this(
startOffset, endOffset, type, symbol, symbol.descriptor.typeParametersCount, symbol.descriptor.valueParameters.size,
origin, superQualifierSymbol
)
@ObsoleteDescriptorBasedAPI
constructor(
startOffset: Int,
endOffset: Int,
type: IrType,
symbol: IrSimpleFunctionSymbol,
typeArgumentsCount: Int,
origin: IrStatementOrigin? = null,
superQualifierSymbol: IrClassSymbol? = null
) : this(
startOffset, endOffset, type, symbol, typeArgumentsCount, symbol.descriptor.valueParameters.size,
origin, superQualifierSymbol
)
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R = override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitCall(this, data) visitor.visitCall(this, data)
companion object {
@ObsoleteDescriptorBasedAPI
fun fromSymbolDescriptor(
startOffset: Int,
endOffset: Int,
type: IrType,
symbol: IrSimpleFunctionSymbol,
typeArgumentsCount: Int = symbol.descriptor.typeParametersCount,
valueArgumentsCount: Int = symbol.descriptor.valueParameters.size,
origin: IrStatementOrigin? = null,
superQualifierSymbol: IrClassSymbol? = null,
) = IrCallImpl(
startOffset, endOffset, type, symbol, typeArgumentsCount, valueArgumentsCount, origin, superQualifierSymbol
)
fun fromSymbolOwner(
startOffset: Int,
endOffset: Int,
type: IrType,
symbol: IrSimpleFunctionSymbol,
typeArgumentsCount: Int = symbol.owner.typeParameters.size,
valueArgumentsCount: Int = symbol.owner.valueParameters.size,
origin: IrStatementOrigin? = null,
superQualifierSymbol: IrClassSymbol? = null,
) = IrCallImpl(
startOffset, endOffset, type, symbol, typeArgumentsCount, valueArgumentsCount, origin, superQualifierSymbol
)
}
} }
@@ -220,7 +220,7 @@ private fun FqName.child(name: String) = child(Name.identifier(name))
@ObsoleteDescriptorBasedAPI @ObsoleteDescriptorBasedAPI
private fun IrSimpleFunction.callWithRanges(source: IrExpression) = private fun IrSimpleFunction.callWithRanges(source: IrExpression) =
IrCallImpl(source.startOffset, source.endOffset, returnType, symbol) IrCallImpl.fromSymbolDescriptor(source.startOffset, source.endOffset, returnType, symbol)
private val AndroidContainerType.fqName: FqName private val AndroidContainerType.fqName: FqName
get() = FqName(internalClassName.replace("/", ".")) get() = FqName(internalClassName.replace("/", "."))