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