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
@@ -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)
}
}