IrTypes: IrTypeParameter.superTypes can depend on type parameters

IrTypeParameter.superTypes can depend on type parameters and thus should
be initialized with type parameters in scope.
This commit is contained in:
Dmitry Petrov
2018-05-25 15:25:07 +03:00
parent eb6f652763
commit ad65fa8c45
7 changed files with 53 additions and 30 deletions
@@ -91,8 +91,7 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
startOffset,
endOffset,
IrDeclarationOrigin.DEFINED,
typeParameterDescriptor,
typeParameterDescriptor.upperBounds.map { it.toIrType() }
typeParameterDescriptor
)
}
}
@@ -106,8 +105,7 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
startOffset,
endOffset,
IrDeclarationOrigin.DEFINED,
typeParameterDescriptor,
typeParameterDescriptor.upperBounds.map { it.toIrType() }
typeParameterDescriptor
)
}
}
@@ -117,7 +115,7 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
from: List<TypeParameterDescriptor>,
declareTypeParameter: (Int, Int, TypeParameterDescriptor) -> IrTypeParameter
) {
val irTypeParameters = from.map { typeParameterDescriptor ->
from.mapTo(irTypeParametersOwner.typeParameters) { typeParameterDescriptor ->
val ktTypeParameterDeclaration = DescriptorToSourceUtils.getSourceFromDescriptor(typeParameterDescriptor)
val startOffset = ktTypeParameterDeclaration.startOffsetOrUndefined
val endOffset = ktTypeParameterDeclaration.endOffsetOrUndefined
@@ -128,7 +126,11 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
)
}
irTypeParametersOwner.typeParameters.addAll(irTypeParameters)
for (irTypeParameter in irTypeParametersOwner.typeParameters) {
irTypeParameter.descriptor.upperBounds.mapTo(irTypeParameter.superTypes) {
it.toIrType()
}
}
}
fun generateInitializerBody(scopeOwnerSymbol: IrSymbol, ktBody: KtExpression): IrExpressionBody =
@@ -30,7 +30,7 @@ interface IrTypeParameter : IrSymbolDeclaration<IrTypeParameterSymbol> {
val name: Name
val variance: Variance
val index: Int
val superTypes: List<IrType>
val superTypes: MutableList<IrType>
override fun <D> transform(transformer: IrElementTransformer<D>, data: D): IrTypeParameter
}
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.ir.declarations.impl
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.IrType
@@ -36,8 +35,7 @@ class IrTypeParameterImpl(
override val symbol: IrTypeParameterSymbol,
override val name: Name,
override val index: Int,
override val variance: Variance,
override val superTypes: List<IrType>
override val variance: Variance
) :
IrDeclarationBase(startOffset, endOffset, origin),
IrTypeParameter {
@@ -46,25 +44,22 @@ class IrTypeParameterImpl(
startOffset: Int,
endOffset: Int,
origin: IrDeclarationOrigin,
symbol: IrTypeParameterSymbol,
upperBounds: List<IrType>
symbol: IrTypeParameterSymbol
) :
this(
startOffset, endOffset, origin, symbol,
symbol.descriptor.name,
symbol.descriptor.index,
symbol.descriptor.variance,
upperBounds
symbol.descriptor.variance
)
constructor(
startOffset: Int,
endOffset: Int,
origin: IrDeclarationOrigin,
descriptor: TypeParameterDescriptor,
upperBounds: List<IrType>
descriptor: TypeParameterDescriptor
) :
this(startOffset, endOffset, origin, IrTypeParameterSymbolImpl(descriptor), upperBounds)
this(startOffset, endOffset, origin, IrTypeParameterSymbolImpl(descriptor))
init {
symbol.bind(this)
@@ -72,6 +67,8 @@ class IrTypeParameterImpl(
override val descriptor: TypeParameterDescriptor get() = symbol.descriptor
override val superTypes: MutableList<IrType> = SmartList()
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitTypeParameter(this, data)
@@ -156,12 +156,19 @@ class DeclarationStubGenerator(
private fun generateTypeParameterStubs(typeParameters: List<TypeParameterDescriptor>, container: IrTypeParametersContainer) {
typeParameters.mapTo(container.typeParameters) { generateTypeParameterStub(it) }
typeTranslator.enterScope(container)
for (typeParameter in container.typeParameters) {
val descriptor = typeParameter.descriptor
descriptor.upperBounds.mapTo(typeParameter.superTypes) { it.toIrType() }
}
typeTranslator.leaveScope()
}
private fun generateTypeParameterStub(descriptor: TypeParameterDescriptor): IrTypeParameter =
IrTypeParameterImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin,
descriptor, descriptor.upperBounds.map { it.toIrType() }
descriptor
)
private fun generateMemberStubs(memberScope: MemberScope, container: IrDeclarationContainer) {
@@ -190,10 +190,10 @@ open class DeepCopyIrTree : IrElementTransformerVoid() {
IrTypeParameterImpl(
originalTypeParameter.startOffset, originalTypeParameter.endOffset,
mapDeclarationOrigin(originalTypeParameter.origin),
newTypeParameterDescriptor,
originalTypeParameter.superTypes // TODO
newTypeParameterDescriptor
).apply {
transformAnnotations(originalTypeParameter)
superTypes.addAll(originalTypeParameter.superTypes) // TODO
}
protected fun createUnboundClassifierSymbol(classifier: ClassifierDescriptor): IrClassifierSymbol =
@@ -99,7 +99,7 @@ open class DeepCopyIrTreeWithSymbols(
symbolRemapper.getDeclaredClass(declaration.symbol)
).apply {
transformAnnotations(declaration)
declaration.typeParameters.transformTo(typeParameters)
copyTypeParametersFrom(declaration)
declaration.superTypes.mapTo(superTypes) {
it.remapType()
}
@@ -140,7 +140,7 @@ open class DeepCopyIrTreeWithSymbols(
private fun <T : IrFunction> T.transformFunctionChildren(declaration: T): T =
apply {
transformAnnotations(declaration)
declaration.typeParameters.transformTo(typeParameters)
copyTypeParametersFrom(declaration)
typeRemapper.withinScope(this) {
dispatchReceiverParameter = declaration.dispatchReceiverParameter?.transform()
extensionReceiverParameter = declaration.extensionReceiverParameter?.transform()
@@ -224,15 +224,34 @@ open class DeepCopyIrTreeWithSymbols(
}
override fun visitTypeParameter(declaration: IrTypeParameter): IrTypeParameter =
copyTypeParameter(declaration).apply {
// TODO type parameter scopes?
declaration.superTypes.mapTo(superTypes) { it.remapType() }
}
private fun copyTypeParameter(declaration: IrTypeParameter) =
IrTypeParameterImpl(
declaration.startOffset, declaration.endOffset,
mapDeclarationOrigin(declaration.origin),
symbolRemapper.getDeclaredTypeParameter(declaration.symbol),
declaration.superTypes.map { it.remapType() }
symbolRemapper.getDeclaredTypeParameter(declaration.symbol)
).apply {
transformAnnotations(declaration)
}
private fun IrTypeParametersContainer.copyTypeParametersFrom(other: IrTypeParametersContainer) {
other.typeParameters.mapTo(this.typeParameters) {
copyTypeParameter(it)
}
typeRemapper.withinScope(this) {
for ((thisTypeParameter, otherTypeParameter) in this.typeParameters.zip(other.typeParameters)) {
otherTypeParameter.superTypes.mapTo(thisTypeParameter.superTypes) {
typeRemapper.remapType(it)
}
}
}
}
override fun visitValueParameter(declaration: IrValueParameter): IrValueParameter =
IrValueParameterImpl(
declaration.startOffset, declaration.endOffset,
@@ -269,26 +269,24 @@ class SymbolTable {
startOffset: Int,
endOffset: Int,
origin: IrDeclarationOrigin,
descriptor: TypeParameterDescriptor,
upperBounds: List<IrType>
descriptor: TypeParameterDescriptor
): IrTypeParameter =
globalTypeParameterSymbolTable.declare(
descriptor,
{ IrTypeParameterSymbolImpl(descriptor) },
{ IrTypeParameterImpl(startOffset, endOffset, origin, it, upperBounds) }
{ IrTypeParameterImpl(startOffset, endOffset, origin, it) }
)
fun declareScopedTypeParameter(
startOffset: Int,
endOffset: Int,
origin: IrDeclarationOrigin,
descriptor: TypeParameterDescriptor,
upperBounds: List<IrType>
descriptor: TypeParameterDescriptor
): IrTypeParameter =
scopedTypeParameterSymbolTable.declare(
descriptor,
{ IrTypeParameterSymbolImpl(descriptor) },
{ IrTypeParameterImpl(startOffset, endOffset, origin, it, upperBounds) }
{ IrTypeParameterImpl(startOffset, endOffset, origin, it) }
)
val unboundTypeParameters: Set<IrTypeParameterSymbol> get() = globalTypeParameterSymbolTable.unboundSymbols