IR fixes to help symbolization of K/N

This commit is contained in:
Igor Chevdar
2018-11-21 17:08:21 +03:00
parent d363763420
commit a78b80f8bf
5 changed files with 49 additions and 72 deletions
@@ -490,9 +490,8 @@ open class WrappedClassDescriptor(
override fun getThisAsReceiverParameter() = owner.thisReceiver?.descriptor as ReceiverParameterDescriptor override fun getThisAsReceiverParameter() = owner.thisReceiver?.descriptor as ReceiverParameterDescriptor
override fun getUnsubstitutedPrimaryConstructor(): ClassConstructorDescriptor? { override fun getUnsubstitutedPrimaryConstructor() =
TODO("not implemented") owner.declarations.filterIsInstance<IrConstructor>().singleOrNull { it.isPrimary }?.descriptor
}
override fun getDeclaredTypeParameters() = owner.typeParameters.map { it.descriptor } override fun getDeclaredTypeParameters() = owner.typeParameters.map { it.descriptor }
@@ -18,11 +18,10 @@ package org.jetbrains.kotlin.backend.common.ir
import org.jetbrains.kotlin.backend.common.DumpIrTreeWithDescriptorsVisitor import org.jetbrains.kotlin.backend.common.DumpIrTreeWithDescriptorsVisitor
import org.jetbrains.kotlin.backend.common.deepCopyWithVariables import org.jetbrains.kotlin.backend.common.deepCopyWithVariables
import org.jetbrains.kotlin.backend.common.descriptors.WrappedClassConstructorDescriptor
import org.jetbrains.kotlin.backend.common.descriptors.WrappedTypeParameterDescriptor import org.jetbrains.kotlin.backend.common.descriptors.WrappedTypeParameterDescriptor
import org.jetbrains.kotlin.backend.common.descriptors.WrappedValueParameterDescriptor import org.jetbrains.kotlin.backend.common.descriptors.WrappedValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.ClassConstructorDescriptorImpl
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.declarations.impl.IrConstructorImpl import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl
@@ -35,6 +34,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.types.IrSimpleType
@@ -92,61 +92,45 @@ fun FunctionDescriptor.createOverriddenDescriptor(owner: ClassDescriptor, final:
} }
fun IrClass.addSimpleDelegatingConstructor( fun IrClass.addSimpleDelegatingConstructor(
superConstructor: IrConstructor, superConstructor: IrConstructor,
irBuiltIns: IrBuiltIns, irBuiltIns: IrBuiltIns,
origin: IrDeclarationOrigin, isPrimary: Boolean = false,
isPrimary: Boolean = false origin: IrDeclarationOrigin? = null
): IrConstructor { ) = WrappedClassConstructorDescriptor().let { descriptor ->
val superConstructorDescriptor = superConstructor.descriptor IrConstructorImpl(
val constructorDescriptor = ClassConstructorDescriptorImpl.createSynthesized( startOffset, endOffset,
/* containingDeclaration = */ this.descriptor, origin ?: this.origin,
/* annotations = */ Annotations.EMPTY, IrConstructorSymbolImpl(descriptor),
/* isPrimary = */ isPrimary, superConstructor.name,
/* source = */ SourceElement.NO_SOURCE superConstructor.visibility,
) defaultType,
val valueParameters = superConstructor.valueParameters.map { false,
val descriptor = it.descriptor as ValueParameterDescriptor false,
val newDescriptor = descriptor.copy(constructorDescriptor, descriptor.name, descriptor.index) isPrimary
IrValueParameterImpl( ).also { constructor ->
startOffset, descriptor.bind(constructor)
endOffset, constructor.parent = this
IrDeclarationOrigin.DEFINED, declarations += constructor
newDescriptor,
it.type,
it.varargElementType
)
}
superConstructor.valueParameters.mapIndexedTo(constructor.valueParameters) { index, parameter ->
constructorDescriptor.initialize( parameter.copyTo(constructor, index = index)
valueParameters.map { it.descriptor as ValueParameterDescriptor }, }
superConstructorDescriptor.visibility
)
constructorDescriptor.returnType = superConstructorDescriptor.returnType
return IrConstructorImpl(startOffset, endOffset, origin, constructorDescriptor, this.defaultType).also { constructor ->
assert(superConstructor.dispatchReceiverParameter == null) // Inner classes aren't supported.
constructor.valueParameters += valueParameters
constructor.body = IrBlockBodyImpl( constructor.body = IrBlockBodyImpl(
startOffset, endOffset, startOffset, endOffset,
listOf( listOf(
IrDelegatingConstructorCallImpl( IrDelegatingConstructorCallImpl(
startOffset, endOffset, irBuiltIns.unitType, startOffset, endOffset, irBuiltIns.unitType,
superConstructor.symbol, superConstructor.descriptor superConstructor.symbol, superConstructor.descriptor,
).apply { 0, superConstructor.valueParameters.size
constructor.valueParameters.forEachIndexed { idx, parameter -> ).apply {
putValueArgument(idx, IrGetValueImpl(startOffset, endOffset, parameter.type, parameter.symbol)) constructor.valueParameters.forEachIndexed { idx, parameter ->
} putValueArgument(idx, IrGetValueImpl(startOffset, endOffset, parameter.type, parameter.symbol))
}, }
IrInstanceInitializerCallImpl(startOffset, endOffset, this.symbol, irBuiltIns.unitType) },
) IrInstanceInitializerCallImpl(startOffset, endOffset, this.symbol, irBuiltIns.unitType)
)
) )
constructor.parent = this
this.declarations.add(constructor)
} }
} }
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrFail import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.addToStdlib.assertedCast import org.jetbrains.kotlin.utils.addToStdlib.assertedCast
@@ -218,7 +219,8 @@ fun IrBuilderWithScope.irCall(callee: IrFunction, origin: IrStatementOrigin): Ir
IrCallImpl(startOffset, endOffset, callee.returnType, callee.symbol, callee.descriptor, origin) IrCallImpl(startOffset, endOffset, callee.returnType, callee.symbol, callee.descriptor, origin)
fun IrBuilderWithScope.irDelegatingConstructorCall(callee: IrConstructor): IrDelegatingConstructorCall = fun IrBuilderWithScope.irDelegatingConstructorCall(callee: IrConstructor): IrDelegatingConstructorCall =
IrDelegatingConstructorCallImpl(startOffset, endOffset, callee.returnType, callee.symbol, callee.descriptor, callee.typeParameters.size) IrDelegatingConstructorCallImpl(startOffset, endOffset, callee.returnType, callee.symbol, callee.descriptor,
callee.parentAsClass.typeParameters.size, callee.valueParameters.size)
fun IrBuilderWithScope.irCallOp( fun IrBuilderWithScope.irCallOp(
callee: IrFunctionSymbol, callee: IrFunctionSymbol,
@@ -50,6 +50,13 @@ class IrDelegatingConstructorCallImpl(
descriptor: ClassConstructorDescriptor descriptor: ClassConstructorDescriptor
) : this(startOffset, endOffset, type, symbol, descriptor, descriptor.typeParametersCount, descriptor.valueParameters.size) ) : this(startOffset, endOffset, type, symbol, descriptor, descriptor.typeParametersCount, descriptor.valueParameters.size)
constructor(
startOffset: Int,
endOffset: Int,
type: IrType,
symbol: IrConstructorSymbol
) : this(startOffset, endOffset, type, symbol, symbol.descriptor)
constructor( constructor(
startOffset: Int, startOffset: Int,
endOffset: Int, endOffset: Int,
@@ -442,21 +442,6 @@ fun createField(
return IrFieldImpl(startOffset, endOffset, origin, descriptor, type) return IrFieldImpl(startOffset, endOffset, origin, descriptor, type)
} }
fun IrFunction.createDispatchReceiverParameter() {
assert(this.dispatchReceiverParameter == null)
val descriptor = this.descriptor.dispatchReceiverParameter ?: return
this.dispatchReceiverParameter = IrValueParameterImpl(
startOffset,
endOffset,
IrDeclarationOrigin.DEFINED,
descriptor,
this.parentAsClass.defaultType,
null
).also { it.parent = this }
}
// In presence of `IrBlock`s, return the expression that actually serves as the value (the last one). // In presence of `IrBlock`s, return the expression that actually serves as the value (the last one).
tailrec fun IrExpression.removeBlocks(): IrExpression? = when (this) { tailrec fun IrExpression.removeBlocks(): IrExpression? = when (this) {
is IrBlock -> (statements.last() as? IrExpression)?.removeBlocks() is IrBlock -> (statements.last() as? IrExpression)?.removeBlocks()