IR: create stubs for constructor type parameters

Java constructors can have type parameters of their own:
  public class J<X extends Number> {
    public <Y extends CharSequence> J() {}
  }

When such constructors are called from Kotlin, type parameters for
constructor follow type parameters for class:
  fun test() = J<Int, String>() // <X=Int, Y=String>

Descriptor-based representation uses the same type parameters ordering.

Also, use 'withScope' in IrLazyFunction type parameters creation.
This commit is contained in:
Dmitry Petrov
2019-03-25 17:45:31 +03:00
parent 8be7f7f1f7
commit 2c9ed73ba8
6 changed files with 58 additions and 11 deletions
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.util.withScope
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
@@ -55,7 +56,17 @@ class IrLazyConstructor(
TypeTranslator
)
override val typeParameters: MutableList<IrTypeParameter> = arrayListOf()
override val typeParameters: MutableList<IrTypeParameter> by lazy {
typeTranslator.buildWithScope(this) {
stubGenerator.symbolTable.withScope(descriptor) {
val classTypeParametersCount = descriptor.constructedClass.original.declaredTypeParameters.size
val allConstructorTypeParameters = descriptor.typeParameters
allConstructorTypeParameters.subList(classTypeParametersCount, allConstructorTypeParameters.size).mapTo(ArrayList()) {
stubGenerator.generateOrGetTypeParameterStub(it)
}
}
}
}
init {
symbol.bind(this)
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.util.withScope
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.descriptorUtil.propertyIfAccessor
@@ -62,18 +63,17 @@ class IrLazyFunction(
override val typeParameters: MutableList<IrTypeParameter> by lazy {
typeTranslator.buildWithScope(this) {
stubGenerator.symbolTable.enterScope(descriptor)
val propertyIfAccessor = descriptor.propertyIfAccessor
propertyIfAccessor.typeParameters.mapTo(arrayListOf()) {
if (descriptor != propertyIfAccessor) {
stubGenerator.generateOrGetScopedTypeParameterStub(it).also {
it.parent = this@IrLazyFunction
stubGenerator.symbolTable.withScope(descriptor) {
val propertyIfAccessor = descriptor.propertyIfAccessor
propertyIfAccessor.typeParameters.mapTo(arrayListOf()) {
if (descriptor != propertyIfAccessor) {
stubGenerator.generateOrGetScopedTypeParameterStub(it).also {
it.parent = this@IrLazyFunction
}
} else {
stubGenerator.generateOrGetTypeParameterStub(it)
}
} else {
stubGenerator.generateOrGetTypeParameterStub(it)
}
}.also {
stubGenerator.symbolTable.leaveScope(descriptor)
}
}
}