IR: use buildValueParameter where possible

This commit is contained in:
Alexander Udalov
2020-07-08 12:26:41 +02:00
parent 378d0a757a
commit b36a6114aa
13 changed files with 85 additions and 127 deletions
@@ -12,20 +12,19 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.Scope import org.jetbrains.kotlin.ir.builders.Scope
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
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
import org.jetbrains.kotlin.ir.declarations.impl.IrFieldImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFieldImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.descriptors.WrappedClassConstructorDescriptor import org.jetbrains.kotlin.ir.descriptors.WrappedClassConstructorDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedFieldDescriptor import org.jetbrains.kotlin.ir.descriptors.WrappedFieldDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedSimpleFunctionDescriptor import org.jetbrains.kotlin.ir.descriptors.WrappedSimpleFunctionDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedValueParameterDescriptor
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
@@ -34,7 +33,6 @@ import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.types.impl.IrTypeAbbreviationImpl import org.jetbrains.kotlin.ir.types.impl.IrTypeAbbreviationImpl
@@ -662,23 +660,19 @@ class LocalDeclarationsLowering(
) = ArrayList<IrValueParameter>(capturedValues.size + oldDeclaration.valueParameters.size).apply { ) = ArrayList<IrValueParameter>(capturedValues.size + oldDeclaration.valueParameters.size).apply {
val generatedNames = mutableSetOf<Name>() val generatedNames = mutableSetOf<Name>()
capturedValues.mapIndexedTo(this) { i, capturedValue -> capturedValues.mapIndexedTo(this) { i, capturedValue ->
val parameterDescriptor = WrappedValueParameterDescriptor()
val p = capturedValue.owner val p = capturedValue.owner
IrValueParameterImpl( buildValueParameter(newDeclaration) {
p.startOffset, startOffset = p.startOffset
p.endOffset, endOffset = p.endOffset
if (p.descriptor is ReceiverParameterDescriptor && newDeclaration is IrConstructor) origin =
BOUND_RECEIVER_PARAMETER else BOUND_VALUE_PARAMETER, if (p.descriptor is ReceiverParameterDescriptor && newDeclaration is IrConstructor) BOUND_RECEIVER_PARAMETER
IrValueParameterSymbolImpl(parameterDescriptor), else BOUND_VALUE_PARAMETER
suggestNameForCapturedValue(p, generatedNames), name = suggestNameForCapturedValue(p, generatedNames)
i, index = i
localFunctionContext.remapType(p.type), type = localFunctionContext.remapType(p.type)
null, isCrossInline = (capturedValue as? IrValueParameterSymbol)?.owner?.isCrossinline == true
isCrossinline = (capturedValue as? IrValueParameterSymbol)?.owner?.isCrossinline == true,
isNoinline = (capturedValue as? IrValueParameterSymbol)?.owner?.isNoinline == true isNoinline = (capturedValue as? IrValueParameterSymbol)?.owner?.isNoinline == true
).also { }.also {
parameterDescriptor.bind(it)
it.parent = newDeclaration
newParameterToCaptured[it] = capturedValue newParameterToCaptured[it] = capturedValue
} }
} }
@@ -218,21 +218,23 @@ inline fun IrClass.addConstructor(builder: IrFunctionBuilder.() -> Unit = {}): I
constructor.parent = this@addConstructor constructor.parent = this@addConstructor
} }
fun IrValueParameterBuilder.build(): IrValueParameter { @PublishedApi
val wrappedDescriptor = WrappedValueParameterDescriptor() internal fun IrValueParameterBuilder.buildValueParameter(parent: IrDeclarationParent): IrValueParameter {
val wrappedDescriptor = WrappedValueParameterDescriptor(wrappedDescriptorAnnotations)
return IrValueParameterImpl( return IrValueParameterImpl(
startOffset, endOffset, origin, startOffset, endOffset, origin,
IrValueParameterSymbolImpl(wrappedDescriptor), IrValueParameterSymbolImpl(wrappedDescriptor),
name, index, type, varargElementType, isCrossInline, isNoinline name, index, type, varargElementType, isCrossInline, isNoinline
).also { ).also {
wrappedDescriptor.bind(it) wrappedDescriptor.bind(it)
it.parent = parent
} }
} }
inline fun buildValueParameter(builder: IrValueParameterBuilder.() -> Unit): IrValueParameter = inline fun buildValueParameter(parent: IrDeclarationParent, builder: IrValueParameterBuilder.() -> Unit): IrValueParameter =
IrValueParameterBuilder().run { IrValueParameterBuilder().run {
builder() builder()
build() buildValueParameter(parent)
} }
inline fun IrFunction.addValueParameter(builder: IrValueParameterBuilder.() -> Unit): IrValueParameter = inline fun IrFunction.addValueParameter(builder: IrValueParameterBuilder.() -> Unit): IrValueParameter =
@@ -241,9 +243,8 @@ inline fun IrFunction.addValueParameter(builder: IrValueParameterBuilder.() -> U
if (index == UNDEFINED_PARAMETER_INDEX) { if (index == UNDEFINED_PARAMETER_INDEX) {
index = valueParameters.size index = valueParameters.size
} }
build().also { valueParameter -> buildValueParameter(this@addValueParameter).also { valueParameter ->
valueParameters += valueParameter valueParameters += valueParameter
valueParameter.parent = this@addValueParameter
} }
} }
@@ -259,9 +260,8 @@ inline fun IrSimpleFunction.addDispatchReceiver(builder: IrValueParameterBuilder
builder() builder()
index = -1 index = -1
name = "this".synthesizedName name = "this".synthesizedName
build().also { receiver -> buildValueParameter(this@addDispatchReceiver).also { receiver ->
dispatchReceiverParameter = receiver dispatchReceiverParameter = receiver
receiver.parent = this@addDispatchReceiver
} }
} }
@@ -270,9 +270,8 @@ inline fun IrSimpleFunction.addExtensionReceiver(builder: IrValueParameterBuilde
builder() builder()
index = -1 index = -1
name = "receiver".synthesizedName name = "receiver".synthesizedName
build().also { receiver -> buildValueParameter(this@addExtensionReceiver).also { receiver ->
extensionReceiverParameter = receiver extensionReceiverParameter = receiver
receiver.parent = this@addExtensionReceiver
} }
} }
@@ -121,8 +121,7 @@ class JsDeclarationFactory(mapping: JsMapping) : DeclarationFactory {
newConstructor.copyTypeParametersFrom(oldConstructor) newConstructor.copyTypeParametersFrom(oldConstructor)
val outerThisValueParameter = val outerThisValueParameter = JsIrBuilder.buildValueParameter(newConstructor, Namer.OUTER_NAME, 0, outerThisType)
JsIrBuilder.buildValueParameter(Namer.OUTER_NAME, 0, outerThisType).also { it.parent = newConstructor }
val newValueParameters = mutableListOf(outerThisValueParameter) val newValueParameters = mutableListOf(outerThisValueParameter)
@@ -10,13 +10,23 @@ import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.* import org.jetbrains.kotlin.ir.declarations.impl.IrAnonymousInitializerImpl
import org.jetbrains.kotlin.ir.descriptors.* import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeParameterImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl
import org.jetbrains.kotlin.ir.descriptors.WrappedClassDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedSimpleFunctionDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedTypeParameterDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedVariableDescriptor
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.impl.* import org.jetbrains.kotlin.ir.symbols.impl.IrAnonymousInitializerSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
@@ -49,25 +59,13 @@ object JsIrBuilder {
fun buildThrow(type: IrType, value: IrExpression) = IrThrowImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, type, value) fun buildThrow(type: IrType, value: IrExpression) = IrThrowImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, type, value)
fun buildValueParameter(name: String = "tmp", index: Int, type: IrType) = buildValueParameter(Name.identifier(name), index, type) fun buildValueParameter(parent: IrDeclarationParent, name: String, index: Int, type: IrType): IrValueParameter =
buildValueParameter(parent) {
fun buildValueParameter(name: Name, index: Int, type: IrType, origin: IrDeclarationOrigin = SYNTHESIZED_DECLARATION): IrValueParameter { this.origin = SYNTHESIZED_DECLARATION
val descriptor = WrappedValueParameterDescriptor() this.name = Name.identifier(name)
return IrValueParameterImpl( this.index = index
UNDEFINED_OFFSET, this.type = type
UNDEFINED_OFFSET,
origin,
IrValueParameterSymbolImpl(descriptor),
name,
index,
type,
null,
isCrossinline = false,
isNoinline = false
).also {
descriptor.bind(it)
} }
}
fun buildTypeParameter(name: Name, index: Int, isReified: Boolean, variance: Variance = Variance.INVARIANT): IrTypeParameter { fun buildTypeParameter(name: Name, index: Int, isReified: Boolean, variance: Variance = Variance.INVARIANT): IrTypeParameter {
val descriptor = WrappedTypeParameterDescriptor() val descriptor = WrappedTypeParameterDescriptor()
@@ -25,7 +25,10 @@ import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrTypeProjection import org.jetbrains.kotlin.ir.types.IrTypeProjection
import org.jetbrains.kotlin.ir.types.classOrNull import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.explicitParameters
import org.jetbrains.kotlin.ir.util.isSuspend
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -293,11 +296,11 @@ class CallableReferenceLowering(private val context: CommonBackendContext) : Bod
val argumentTypes = parameterTypes.dropLast(1) val argumentTypes = parameterTypes.dropLast(1)
valueParameters = argumentTypes.mapIndexed { i, t -> valueParameters = argumentTypes.mapIndexed { i, t ->
buildValueParameter { buildValueParameter(this) {
name = Name.identifier("p$i") name = Name.identifier("p$i")
type = t type = t
index = i index = i
}.also { it.parent = this } }
} }
body = IrBlockBodyImpl(reference.startOffset, reference.endOffset, listOf(reference.run { body = IrBlockBodyImpl(reference.startOffset, reference.endOffset, listOf(reference.run {
@@ -326,10 +329,10 @@ class CallableReferenceLowering(private val context: CommonBackendContext) : Bod
returnType = stringType returnType = stringType
} }
getter.overriddenSymbols += supperGetter.symbol getter.overriddenSymbols += supperGetter.symbol
getter.dispatchReceiverParameter = buildValueParameter { getter.dispatchReceiverParameter = buildValueParameter(getter) {
name = THIS_NAME name = THIS_NAME
type = clazz.defaultType type = clazz.defaultType
}.also { it.parent = getter } }
getter.body = IrBlockBodyImpl( getter.body = IrBlockBodyImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, listOf( UNDEFINED_OFFSET, UNDEFINED_OFFSET, listOf(
@@ -137,8 +137,8 @@ class EnumClassConstructorLowering(val context: JsCommonBackendContext) : Declar
).apply { ).apply {
loweredConstructorDescriptor.bind(this) loweredConstructorDescriptor.bind(this)
parent = enumClass parent = enumClass
valueParameters += JsIrBuilder.buildValueParameter("name", 0, context.irBuiltIns.stringType).also { it.parent = this } valueParameters += JsIrBuilder.buildValueParameter(this, "name", 0, context.irBuiltIns.stringType)
valueParameters += JsIrBuilder.buildValueParameter("ordinal", 1, context.irBuiltIns.intType).also { it.parent = this } valueParameters += JsIrBuilder.buildValueParameter(this, "ordinal", 1, context.irBuiltIns.intType)
copyParameterDeclarationsFrom(enumConstructor) copyParameterDeclarationsFrom(enumConstructor)
val newConstructor = this val newConstructor = this
@@ -9,17 +9,14 @@ import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.DeclarationTransformer import org.jetbrains.kotlin.backend.common.DeclarationTransformer
import org.jetbrains.kotlin.backend.common.ir.copyTo import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.descriptors.WrappedSimpleFunctionDescriptor import org.jetbrains.kotlin.ir.descriptors.WrappedSimpleFunctionDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedValueParameterDescriptor
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -71,22 +68,11 @@ class PrivateMembersLowering(val context: JsIrBackendContext) : DeclarationTrans
staticFunction.typeParameters += function.typeParameters.map { it.deepCopyWithSymbols(staticFunction) } staticFunction.typeParameters += function.typeParameters.map { it.deepCopyWithSymbols(staticFunction) }
staticFunction.extensionReceiverParameter = function.extensionReceiverParameter?.copyTo(staticFunction) staticFunction.extensionReceiverParameter = function.extensionReceiverParameter?.copyTo(staticFunction)
val thisDesc = WrappedValueParameterDescriptor() staticFunction.valueParameters += buildValueParameter(staticFunction) {
val thisSymbol = IrValueParameterSymbolImpl(thisDesc) origin = STATIC_THIS_PARAMETER
staticFunction.valueParameters += IrValueParameterImpl( name = Name.identifier("\$this")
UNDEFINED_OFFSET, index = 0
UNDEFINED_OFFSET, type = function.dispatchReceiverParameter!!.type
STATIC_THIS_PARAMETER,
thisSymbol,
Name.identifier("\$this"),
0,
function.dispatchReceiverParameter!!.type,
null,
isCrossinline = false,
isNoinline = false
).also {
thisDesc.bind(it)
it.parent = staticFunction
} }
function.correspondingStatic = staticFunction function.correspondingStatic = staticFunction
@@ -61,18 +61,14 @@ class PropertyReferenceLowering(private val context: JsIrBackendContext) : BodyL
val boundArguments = listOfNotNull(reference.dispatchReceiver, reference.extensionReceiver) val boundArguments = listOfNotNull(reference.dispatchReceiver, reference.extensionReceiver)
val valueParameters = ArrayList<IrValueParameter>(boundArguments.size) val valueParameters = boundArguments.mapIndexed { i, arg ->
factoryDeclaration.valueParameters = valueParameters buildValueParameter(factoryDeclaration) {
for ((i, arg) in boundArguments.withIndex()) {
val vp = buildValueParameter {
type = arg.type type = arg.type
index = i index = i
name = Name.identifier("\$b$i") name = Name.identifier("\$b$i")
} }
vp.parent = factoryDeclaration
valueParameters.add(vp)
} }
factoryDeclaration.valueParameters = valueParameters
// TODO: type parameters // TODO: type parameters
@@ -176,7 +176,7 @@ private fun buildInitDeclaration(constructor: IrConstructor, irClass: IrClass):
it.copyTypeParametersFrom(constructor.parentAsClass) it.copyTypeParametersFrom(constructor.parentAsClass)
it.valueParameters = constructor.valueParameters.map { p -> p.copyTo(it) } it.valueParameters = constructor.valueParameters.map { p -> p.copyTo(it) }
it.valueParameters += JsIrBuilder.buildValueParameter("\$this", constructor.valueParameters.size, type).apply { parent = it } it.valueParameters += JsIrBuilder.buildValueParameter(it, "\$this", constructor.valueParameters.size, type)
} }
} }
@@ -23,21 +23,19 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.declarations.buildField import org.jetbrains.kotlin.ir.builders.declarations.buildField
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
import org.jetbrains.kotlin.ir.builders.irCall import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.setSourceRange import org.jetbrains.kotlin.ir.builders.setSourceRange
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.descriptors.WrappedClassConstructorDescriptor import org.jetbrains.kotlin.ir.descriptors.WrappedClassConstructorDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedClassDescriptor import org.jetbrains.kotlin.ir.descriptors.WrappedClassDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedValueParameterDescriptor
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.load.java.JavaVisibilities import org.jetbrains.kotlin.load.java.JavaVisibilities
import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.load.java.JvmAbi
@@ -129,20 +127,11 @@ class JvmDeclarationFactory(
returnType = oldConstructor.returnType returnType = oldConstructor.returnType
copyTypeParametersFrom(oldConstructor) copyTypeParametersFrom(oldConstructor)
val outerThisType = oldConstructor.parentAsClass.parentAsClass.defaultType val outerThisValueParameter = buildValueParameter(this) {
val outerThisDescriptor = WrappedValueParameterDescriptor() origin = JvmLoweredDeclarationOrigin.FIELD_FOR_OUTER_THIS
val outerThisValueParameter = IrValueParameterImpl( name = Name.identifier(AsmUtil.CAPTURED_THIS_FIELD)
UNDEFINED_OFFSET, UNDEFINED_OFFSET, JvmLoweredDeclarationOrigin.FIELD_FOR_OUTER_THIS, index = 0
IrValueParameterSymbolImpl(outerThisDescriptor), type = oldConstructor.parentAsClass.parentAsClass.defaultType
Name.identifier(AsmUtil.CAPTURED_THIS_FIELD),
0,
type = outerThisType,
varargElementType = null,
isCrossinline = false,
isNoinline = false
).also {
outerThisDescriptor.bind(it)
it.parent = this
} }
valueParameters = listOf(outerThisValueParameter) + oldConstructor.valueParameters.map { it.copyTo(this, index = it.index + 1) } valueParameters = listOf(outerThisValueParameter) + oldConstructor.valueParameters.map { it.copyTo(this, index = it.index + 1) }
metadata = oldConstructor.metadata metadata = oldConstructor.metadata
@@ -68,7 +68,7 @@ private class AdditionalClassAnnotationLowering(private val context: JvmBackendC
val irClass = this val irClass = this
parent = annotationPackage parent = annotationPackage
annotationPackage.addChild(this) annotationPackage.addChild(this)
thisReceiver = buildValueParameter { thisReceiver = buildValueParameter(this) {
name = Name.identifier("\$this") name = Name.identifier("\$this")
type = IrSimpleTypeImpl(irClass.symbol, false, emptyList(), emptyList()) type = IrSimpleTypeImpl(irClass.symbol, false, emptyList(), emptyList())
} }
@@ -12,8 +12,8 @@ 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.backend.jvm.codegen.isJvmInterface import org.jetbrains.kotlin.backend.jvm.codegen.isJvmInterface
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.declarations.buildFun import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
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.irString import org.jetbrains.kotlin.ir.builders.irString
@@ -21,11 +21,8 @@ import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.descriptors.WrappedValueParameterDescriptor
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
@@ -98,22 +95,19 @@ internal class CollectionStubMethodLowering(val context: JvmBackendContext) : Cl
} }
// Copy value parameter with type substitution // Copy value parameter with type substitution
private fun IrValueParameter.copyWithSubstitution(target: IrSimpleFunction, substitutionMap: Map<IrTypeParameterSymbol, IrType>) private fun IrValueParameter.copyWithSubstitution(
: IrValueParameter { target: IrSimpleFunction, substitutionMap: Map<IrTypeParameterSymbol, IrType>
val descriptor = WrappedValueParameterDescriptor(this.descriptor.annotations) ): IrValueParameter {
return IrValueParameterImpl( val parameter = this
UNDEFINED_OFFSET, UNDEFINED_OFFSET, return buildValueParameter(target) {
IrDeclarationOrigin.IR_BUILTINS_STUB, wrappedDescriptorAnnotations = descriptor.annotations
IrValueParameterSymbolImpl(descriptor), origin = IrDeclarationOrigin.IR_BUILTINS_STUB
name, name = parameter.name
index, index = parameter.index
type.substitute(substitutionMap), type = parameter.type.substitute(substitutionMap)
varargElementType?.substitute(substitutionMap), varargElementType = parameter.varargElementType?.substitute(substitutionMap)
isCrossinline, isCrossInline = parameter.isCrossinline
isNoinline isNoinline = parameter.isNoinline
).apply {
descriptor.bind(this)
parent = target
} }
} }
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.ir.builders.declarations
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.ir.builders.IrElementBuilder import org.jetbrains.kotlin.ir.builders.IrElementBuilder
import org.jetbrains.kotlin.ir.declarations.IrDeclaration import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
@@ -14,10 +15,10 @@ import org.jetbrains.kotlin.ir.declarations.IrDeclarationWithVisibility
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
abstract class IrDeclarationBuilder : IrElementBuilder() { abstract class IrDeclarationBuilder : IrElementBuilder() {
var origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED var origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED
var visibility: Visibility = Visibilities.PUBLIC var visibility: Visibility = Visibilities.PUBLIC
var wrappedDescriptorAnnotations: Annotations = Annotations.EMPTY
lateinit var name: Name lateinit var name: Name
fun updateFrom(from: IrDeclaration) { fun updateFrom(from: IrDeclaration) {
@@ -26,5 +27,4 @@ abstract class IrDeclarationBuilder : IrElementBuilder() {
origin = from.origin origin = from.origin
visibility = if (from is IrDeclarationWithVisibility) from.visibility else Visibilities.PUBLIC visibility = if (from is IrDeclarationWithVisibility) from.visibility else Visibilities.PUBLIC
} }
} }