IR: use buildTypeParameter, minor cleanup

This commit is contained in:
Alexander Udalov
2020-07-08 13:07:39 +02:00
parent 4b5464c6cc
commit c3560e5854
3 changed files with 18 additions and 42 deletions
@@ -16,13 +16,11 @@ import org.jetbrains.kotlin.ir.IrElement
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.IrTypeParameterBuilder
import org.jetbrains.kotlin.ir.builders.declarations.build
import org.jetbrains.kotlin.ir.builders.declarations.buildReceiverParameter
import org.jetbrains.kotlin.ir.builders.declarations.buildTypeParameter
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeParameterImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.descriptors.*
import org.jetbrains.kotlin.ir.expressions.*
@@ -31,7 +29,6 @@ import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
@@ -175,18 +172,15 @@ fun IrValueParameter.copyTo(
}
}
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun IrTypeParameter.copyToWithoutSuperTypes(
target: IrTypeParametersContainer,
index: Int = this.index,
origin: IrDeclarationOrigin = this.origin
): IrTypeParameter {
val descriptor = WrappedTypeParameterDescriptor(symbol.descriptor.annotations, symbol.descriptor.source)
val symbol = IrTypeParameterSymbolImpl(descriptor)
return IrTypeParameterImpl(startOffset, endOffset, origin, symbol, name, index, isReified, variance).also { copied ->
descriptor.bind(copied)
copied.parent = target
}
): IrTypeParameter = buildTypeParameter(target) {
updateFrom(this@copyToWithoutSuperTypes)
this.name = this@copyToWithoutSuperTypes.name
this.origin = origin
this.index = index
}
@OptIn(ObsoleteDescriptorBasedAPI::class)
@@ -637,15 +631,10 @@ private fun IrSimpleFunction.copyAndRenameConflictingTypeParametersFrom(
contextType.name.asString()
}
val newTypeParameter = IrTypeParameterBuilder().run {
newParameters.add(buildTypeParameter(this) {
updateFrom(contextType)
name = Name.identifier(newName)
build()
}.also {
it.parent = this
}
newParameters.add(newTypeParameter)
})
}
typeParameters = typeParameters + newParameters
@@ -300,7 +300,8 @@ fun IrSimpleFunction.addExtensionReceiver(type: IrType, origin: IrDeclarationOri
this.origin = origin
}
fun IrTypeParameterBuilder.build(): IrTypeParameter {
@PublishedApi
internal fun IrTypeParameterBuilder.buildTypeParameter(parent: IrDeclarationParent): IrTypeParameter {
val wrappedDescriptor = WrappedTypeParameterDescriptor()
return IrTypeParameterImpl(
startOffset, endOffset, origin,
@@ -309,18 +310,24 @@ fun IrTypeParameterBuilder.build(): IrTypeParameter {
).also {
wrappedDescriptor.bind(it)
it.superTypes.addAll(superTypes)
it.parent = parent
}
}
inline fun buildTypeParameter(parent: IrDeclarationParent, builder: IrTypeParameterBuilder.() -> Unit): IrTypeParameter =
IrTypeParameterBuilder().run {
builder()
buildTypeParameter(parent)
}
inline fun IrTypeParametersContainer.addTypeParameter(builder: IrTypeParameterBuilder.() -> Unit): IrTypeParameter =
IrTypeParameterBuilder().run {
builder()
if (index == UNDEFINED_PARAMETER_INDEX) {
index = typeParameters.size
}
build().also { typeParameter ->
buildTypeParameter(this@addTypeParameter).also { typeParameter ->
typeParameters += typeParameter
typeParameter.parent = this@addTypeParameter
}
}
@@ -14,22 +14,18 @@ import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
import org.jetbrains.kotlin.ir.declarations.*
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.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
object JsIrBuilder {
@@ -67,22 +63,6 @@ object JsIrBuilder {
this.type = type
}
fun buildTypeParameter(name: Name, index: Int, isReified: Boolean, variance: Variance = Variance.INVARIANT): IrTypeParameter {
val descriptor = WrappedTypeParameterDescriptor()
return IrTypeParameterImpl(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
SYNTHESIZED_DECLARATION,
IrTypeParameterSymbolImpl(descriptor),
name,
index,
isReified,
variance
).also {
descriptor.bind(it)
}
}
fun buildFunction(
name: String,
returnType: IrType,