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, startOffset,
endOffset, endOffset,
IrDeclarationOrigin.DEFINED, IrDeclarationOrigin.DEFINED,
typeParameterDescriptor, typeParameterDescriptor
typeParameterDescriptor.upperBounds.map { it.toIrType() }
) )
} }
} }
@@ -106,8 +105,7 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
startOffset, startOffset,
endOffset, endOffset,
IrDeclarationOrigin.DEFINED, IrDeclarationOrigin.DEFINED,
typeParameterDescriptor, typeParameterDescriptor
typeParameterDescriptor.upperBounds.map { it.toIrType() }
) )
} }
} }
@@ -117,7 +115,7 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
from: List<TypeParameterDescriptor>, from: List<TypeParameterDescriptor>,
declareTypeParameter: (Int, Int, TypeParameterDescriptor) -> IrTypeParameter declareTypeParameter: (Int, Int, TypeParameterDescriptor) -> IrTypeParameter
) { ) {
val irTypeParameters = from.map { typeParameterDescriptor -> from.mapTo(irTypeParametersOwner.typeParameters) { typeParameterDescriptor ->
val ktTypeParameterDeclaration = DescriptorToSourceUtils.getSourceFromDescriptor(typeParameterDescriptor) val ktTypeParameterDeclaration = DescriptorToSourceUtils.getSourceFromDescriptor(typeParameterDescriptor)
val startOffset = ktTypeParameterDeclaration.startOffsetOrUndefined val startOffset = ktTypeParameterDeclaration.startOffsetOrUndefined
val endOffset = ktTypeParameterDeclaration.endOffsetOrUndefined 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 = fun generateInitializerBody(scopeOwnerSymbol: IrSymbol, ktBody: KtExpression): IrExpressionBody =
@@ -30,7 +30,7 @@ interface IrTypeParameter : IrSymbolDeclaration<IrTypeParameterSymbol> {
val name: Name val name: Name
val variance: Variance val variance: Variance
val index: Int val index: Int
val superTypes: List<IrType> val superTypes: MutableList<IrType>
override fun <D> transform(transformer: IrElementTransformer<D>, data: D): IrTypeParameter 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.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter 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.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
@@ -36,8 +35,7 @@ class IrTypeParameterImpl(
override val symbol: IrTypeParameterSymbol, override val symbol: IrTypeParameterSymbol,
override val name: Name, override val name: Name,
override val index: Int, override val index: Int,
override val variance: Variance, override val variance: Variance
override val superTypes: List<IrType>
) : ) :
IrDeclarationBase(startOffset, endOffset, origin), IrDeclarationBase(startOffset, endOffset, origin),
IrTypeParameter { IrTypeParameter {
@@ -46,25 +44,22 @@ class IrTypeParameterImpl(
startOffset: Int, startOffset: Int,
endOffset: Int, endOffset: Int,
origin: IrDeclarationOrigin, origin: IrDeclarationOrigin,
symbol: IrTypeParameterSymbol, symbol: IrTypeParameterSymbol
upperBounds: List<IrType>
) : ) :
this( this(
startOffset, endOffset, origin, symbol, startOffset, endOffset, origin, symbol,
symbol.descriptor.name, symbol.descriptor.name,
symbol.descriptor.index, symbol.descriptor.index,
symbol.descriptor.variance, symbol.descriptor.variance
upperBounds
) )
constructor( constructor(
startOffset: Int, startOffset: Int,
endOffset: Int, endOffset: Int,
origin: IrDeclarationOrigin, origin: IrDeclarationOrigin,
descriptor: TypeParameterDescriptor, descriptor: TypeParameterDescriptor
upperBounds: List<IrType>
) : ) :
this(startOffset, endOffset, origin, IrTypeParameterSymbolImpl(descriptor), upperBounds) this(startOffset, endOffset, origin, IrTypeParameterSymbolImpl(descriptor))
init { init {
symbol.bind(this) symbol.bind(this)
@@ -72,6 +67,8 @@ class IrTypeParameterImpl(
override val descriptor: TypeParameterDescriptor get() = symbol.descriptor 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 = override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitTypeParameter(this, data) visitor.visitTypeParameter(this, data)
@@ -156,12 +156,19 @@ class DeclarationStubGenerator(
private fun generateTypeParameterStubs(typeParameters: List<TypeParameterDescriptor>, container: IrTypeParametersContainer) { private fun generateTypeParameterStubs(typeParameters: List<TypeParameterDescriptor>, container: IrTypeParametersContainer) {
typeParameters.mapTo(container.typeParameters) { generateTypeParameterStub(it) } 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 = private fun generateTypeParameterStub(descriptor: TypeParameterDescriptor): IrTypeParameter =
IrTypeParameterImpl( IrTypeParameterImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin,
descriptor, descriptor.upperBounds.map { it.toIrType() } descriptor
) )
private fun generateMemberStubs(memberScope: MemberScope, container: IrDeclarationContainer) { private fun generateMemberStubs(memberScope: MemberScope, container: IrDeclarationContainer) {
@@ -190,10 +190,10 @@ open class DeepCopyIrTree : IrElementTransformerVoid() {
IrTypeParameterImpl( IrTypeParameterImpl(
originalTypeParameter.startOffset, originalTypeParameter.endOffset, originalTypeParameter.startOffset, originalTypeParameter.endOffset,
mapDeclarationOrigin(originalTypeParameter.origin), mapDeclarationOrigin(originalTypeParameter.origin),
newTypeParameterDescriptor, newTypeParameterDescriptor
originalTypeParameter.superTypes // TODO
).apply { ).apply {
transformAnnotations(originalTypeParameter) transformAnnotations(originalTypeParameter)
superTypes.addAll(originalTypeParameter.superTypes) // TODO
} }
protected fun createUnboundClassifierSymbol(classifier: ClassifierDescriptor): IrClassifierSymbol = protected fun createUnboundClassifierSymbol(classifier: ClassifierDescriptor): IrClassifierSymbol =
@@ -99,7 +99,7 @@ open class DeepCopyIrTreeWithSymbols(
symbolRemapper.getDeclaredClass(declaration.symbol) symbolRemapper.getDeclaredClass(declaration.symbol)
).apply { ).apply {
transformAnnotations(declaration) transformAnnotations(declaration)
declaration.typeParameters.transformTo(typeParameters) copyTypeParametersFrom(declaration)
declaration.superTypes.mapTo(superTypes) { declaration.superTypes.mapTo(superTypes) {
it.remapType() it.remapType()
} }
@@ -140,7 +140,7 @@ open class DeepCopyIrTreeWithSymbols(
private fun <T : IrFunction> T.transformFunctionChildren(declaration: T): T = private fun <T : IrFunction> T.transformFunctionChildren(declaration: T): T =
apply { apply {
transformAnnotations(declaration) transformAnnotations(declaration)
declaration.typeParameters.transformTo(typeParameters) copyTypeParametersFrom(declaration)
typeRemapper.withinScope(this) { typeRemapper.withinScope(this) {
dispatchReceiverParameter = declaration.dispatchReceiverParameter?.transform() dispatchReceiverParameter = declaration.dispatchReceiverParameter?.transform()
extensionReceiverParameter = declaration.extensionReceiverParameter?.transform() extensionReceiverParameter = declaration.extensionReceiverParameter?.transform()
@@ -224,15 +224,34 @@ open class DeepCopyIrTreeWithSymbols(
} }
override fun visitTypeParameter(declaration: IrTypeParameter): IrTypeParameter = 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( IrTypeParameterImpl(
declaration.startOffset, declaration.endOffset, declaration.startOffset, declaration.endOffset,
mapDeclarationOrigin(declaration.origin), mapDeclarationOrigin(declaration.origin),
symbolRemapper.getDeclaredTypeParameter(declaration.symbol), symbolRemapper.getDeclaredTypeParameter(declaration.symbol)
declaration.superTypes.map { it.remapType() }
).apply { ).apply {
transformAnnotations(declaration) 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 = override fun visitValueParameter(declaration: IrValueParameter): IrValueParameter =
IrValueParameterImpl( IrValueParameterImpl(
declaration.startOffset, declaration.endOffset, declaration.startOffset, declaration.endOffset,
@@ -269,26 +269,24 @@ class SymbolTable {
startOffset: Int, startOffset: Int,
endOffset: Int, endOffset: Int,
origin: IrDeclarationOrigin, origin: IrDeclarationOrigin,
descriptor: TypeParameterDescriptor, descriptor: TypeParameterDescriptor
upperBounds: List<IrType>
): IrTypeParameter = ): IrTypeParameter =
globalTypeParameterSymbolTable.declare( globalTypeParameterSymbolTable.declare(
descriptor, descriptor,
{ IrTypeParameterSymbolImpl(descriptor) }, { IrTypeParameterSymbolImpl(descriptor) },
{ IrTypeParameterImpl(startOffset, endOffset, origin, it, upperBounds) } { IrTypeParameterImpl(startOffset, endOffset, origin, it) }
) )
fun declareScopedTypeParameter( fun declareScopedTypeParameter(
startOffset: Int, startOffset: Int,
endOffset: Int, endOffset: Int,
origin: IrDeclarationOrigin, origin: IrDeclarationOrigin,
descriptor: TypeParameterDescriptor, descriptor: TypeParameterDescriptor
upperBounds: List<IrType>
): IrTypeParameter = ): IrTypeParameter =
scopedTypeParameterSymbolTable.declare( scopedTypeParameterSymbolTable.declare(
descriptor, descriptor,
{ IrTypeParameterSymbolImpl(descriptor) }, { IrTypeParameterSymbolImpl(descriptor) },
{ IrTypeParameterImpl(startOffset, endOffset, origin, it, upperBounds) } { IrTypeParameterImpl(startOffset, endOffset, origin, it) }
) )
val unboundTypeParameters: Set<IrTypeParameterSymbol> get() = globalTypeParameterSymbolTable.unboundSymbols val unboundTypeParameters: Set<IrTypeParameterSymbol> get() = globalTypeParameterSymbolTable.unboundSymbols