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)
}
}
}
@@ -0,0 +1,8 @@
// FILE: javaConstructorWithTypeParameters.kt
// DUMP_EXTERNAL_CLASS J
fun test() = J<String>()
// FILE: J.java
public class J {
public <T> J() {}
}
@@ -0,0 +1,6 @@
FILE fqName:<root> fileName:/javaConstructorWithTypeParameters.kt
FUN name:test visibility:public modality:FINAL <> () returnType:<root>.J
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test (): <root>.J declared in <root>'
CALL 'public constructor <init> <T> () declared in <root>.J' type=<root>.J origin=null
<T>: kotlin.String
@@ -0,0 +1,17 @@
CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:J modality:OPEN visibility:public superTypes:[<unbound IrClassSymbolImpl>]
$this: VALUE_PARAMETER IR_EXTERNAL_DECLARATION_STUB name:<this> type:<root>.J
CONSTRUCTOR IR_EXTERNAL_DECLARATION_STUB visibility:public <T> () returnType:<root>.J
TYPE_PARAMETER IR_EXTERNAL_DECLARATION_STUB name:T index:0 variance: superTypes:[<unbound IrClassSymbolImpl>?]
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:<unbound IrClassSymbolImpl>) returnType:<unbound IrClassSymbolImpl>
overridden:
public open fun hashCode (): <unbound IrClassSymbolImpl> declared in kotlin.Any
$this: VALUE_PARAMETER FAKE_OVERRIDE name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:<unbound IrClassSymbolImpl>
overridden:
public open fun equals (other: kotlin.Any?): <unbound IrClassSymbolImpl> declared in kotlin.Any
$this: VALUE_PARAMETER FAKE_OVERRIDE name:<this> type:kotlin.Any
VALUE_PARAMETER IR_EXTERNAL_DECLARATION_STUB name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:<unbound IrClassSymbolImpl>
overridden:
public open fun toString (): <unbound IrClassSymbolImpl> declared in kotlin.Any
$this: VALUE_PARAMETER FAKE_OVERRIDE name:<this> type:kotlin.Any
@@ -1518,6 +1518,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
runTest("compiler/testData/ir/irText/stubs/genericClassInDifferentModule.kt");
}
@TestMetadata("javaConstructorWithTypeParameters.kt")
public void testJavaConstructorWithTypeParameters() throws Exception {
runTest("compiler/testData/ir/irText/stubs/javaConstructorWithTypeParameters.kt");
}
@TestMetadata("javaEnum.kt")
public void testJavaEnum() throws Exception {
runTest("compiler/testData/ir/irText/stubs/javaEnum.kt");