From ad65fa8c459bb465582b44aaac51e34bdacb7461 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 25 May 2018 15:25:07 +0300 Subject: [PATCH] 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. --- .../psi2ir/generators/DeclarationGenerator.kt | 14 +++++----- .../kotlin/ir/declarations/IrTypeParameter.kt | 2 +- .../declarations/impl/IrTypeParameterImpl.kt | 17 +++++------- .../ir/util/DeclarationStubGenerator.kt | 9 ++++++- .../kotlin/ir/util/DeepCopyIrTree.kt | 4 +-- .../ir/util/DeepCopyIrTreeWithSymbols.kt | 27 ++++++++++++++++--- .../jetbrains/kotlin/ir/util/SymbolTable.kt | 10 +++---- 7 files changed, 53 insertions(+), 30 deletions(-) diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/DeclarationGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/DeclarationGenerator.kt index 40bcc8b8087..5b4285def2e 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/DeclarationGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/DeclarationGenerator.kt @@ -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, 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 = diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrTypeParameter.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrTypeParameter.kt index b875b05cf39..a90e37e8ce9 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrTypeParameter.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrTypeParameter.kt @@ -30,7 +30,7 @@ interface IrTypeParameter : IrSymbolDeclaration { val name: Name val variance: Variance val index: Int - val superTypes: List + val superTypes: MutableList override fun transform(transformer: IrElementTransformer, data: D): IrTypeParameter } \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/impl/IrTypeParameterImpl.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/impl/IrTypeParameterImpl.kt index 0aa70bc957f..595540ff470 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/impl/IrTypeParameterImpl.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/impl/IrTypeParameterImpl.kt @@ -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 + 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 + 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 + 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 = SmartList() + override fun accept(visitor: IrElementVisitor, data: D): R = visitor.visitTypeParameter(this, data) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt index 146fff21b3f..fc67753d645 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt @@ -156,12 +156,19 @@ class DeclarationStubGenerator( private fun generateTypeParameterStubs(typeParameters: List, 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) { diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTree.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTree.kt index 5fcbc659438..0380b862c50 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTree.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTree.kt @@ -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 = diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt index dd12ad344e6..5f0e926921f 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt @@ -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.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, diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/SymbolTable.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/SymbolTable.kt index f5bebeac4df..98ec20672d4 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/SymbolTable.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/SymbolTable.kt @@ -269,26 +269,24 @@ class SymbolTable { startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, - descriptor: TypeParameterDescriptor, - upperBounds: List + 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 + descriptor: TypeParameterDescriptor ): IrTypeParameter = scopedTypeParameterSymbolTable.declare( descriptor, { IrTypeParameterSymbolImpl(descriptor) }, - { IrTypeParameterImpl(startOffset, endOffset, origin, it, upperBounds) } + { IrTypeParameterImpl(startOffset, endOffset, origin, it) } ) val unboundTypeParameters: Set get() = globalTypeParameterSymbolTable.unboundSymbols