FIR: consider Java default constructors as primary ones

This commit is contained in:
Mikhail Glukhikh
2019-07-10 18:50:54 +03:00
parent 2ceffa241b
commit c6a09dba4e
9 changed files with 33 additions and 15 deletions
@@ -210,11 +210,14 @@ class JavaSymbolProvider(
val javaClassDeclaredConstructors = javaClass.constructors
val constructorId = CallableId(classId.packageFqName, classId.relativeClassName, classId.shortClassName)
fun addJavaConstructor(visibility: Visibility = this.visibility): FirJavaConstructor {
fun addJavaConstructor(
visibility: Visibility = this.visibility,
isPrimary: Boolean = false
): FirJavaConstructor {
val constructorSymbol = FirConstructorSymbol(constructorId)
val classTypeParameters = javaClass.typeParameters.convertTypeParameters(javaTypeParameterStack)
val firJavaConstructor = FirJavaConstructor(
this@JavaSymbolProvider.session, constructorSymbol, visibility,
this@JavaSymbolProvider.session, constructorSymbol, visibility, isPrimary,
FirResolvedTypeRefImpl(
this@JavaSymbolProvider.session, null,
firSymbol.constructType(
@@ -230,7 +233,7 @@ class JavaSymbolProvider(
}
if (javaClassDeclaredConstructors.isEmpty() && javaClass.classKind == ClassKind.CLASS) {
addJavaConstructor()
addJavaConstructor(isPrimary = true)
}
for (javaConstructor in javaClassDeclaredConstructors) {
addJavaConstructor(javaConstructor.visibility).apply {
@@ -21,6 +21,7 @@ class FirJavaConstructor(
session: FirSession,
override val symbol: FirConstructorSymbol,
visibility: Visibility,
override val isPrimary: Boolean,
delegatedSelfTypeRef: FirTypeRef
) : FirAbstractCallableMember<FirConstructor>(
session,
@@ -180,12 +180,26 @@ class JavaClassEnhancementScope(
}
}
val function: FirMemberFunction<*> = when (firMethod) {
is FirJavaConstructor -> FirConstructorImpl(
this@JavaClassEnhancementScope.session, null, FirConstructorSymbol(methodId),
newReceiverTypeRef, newReturnTypeRef
).apply {
this.valueParameters += newValueParameters
this.typeParameters += firMethod.typeParameters
is FirJavaConstructor -> {
val symbol = FirConstructorSymbol(methodId)
if (firMethod.isPrimary) {
FirPrimaryConstructorImpl(
this@JavaClassEnhancementScope.session, null, symbol,
firMethod.visibility,
isExpect = false,
isActual = false,
delegatedSelfTypeRef = newReturnTypeRef,
delegatedConstructor = null
)
} else {
FirConstructorImpl(
this@JavaClassEnhancementScope.session, null, symbol,
newReceiverTypeRef, newReturnTypeRef
)
}.apply {
this.valueParameters += newValueParameters
this.typeParameters += firMethod.typeParameters
}
}
else -> FirMemberFunctionImpl(
this@JavaClassEnhancementScope.session, null,
@@ -3,7 +3,7 @@ FILE fqName:<root> fileName:/Derived.kt
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Derived
CONSTRUCTOR visibility:public <> () returnType:<root>.Derived [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.Base'
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.Base'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]'
ANONYMOUS_INITIALIZER isStatic=false
BLOCK_BODY
+1 -1
View File
@@ -98,7 +98,7 @@ FILE fqName:<root> fileName:/kt16904.kt
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Test2
CONSTRUCTOR visibility:public <> () returnType:<root>.Test2 [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.J'
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.J'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test2 modality:FINAL visibility:public superTypes:[<root>.J]'
ANONYMOUS_INITIALIZER isStatic=false
BLOCK_BODY
@@ -82,5 +82,5 @@ FILE fqName:<root> fileName:/samConversionsWithSmartCasts.kt
FUN name:test9 visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
CALL 'public open fun run1 (r: java.lang.Runnable?): kotlin.Unit declared in <root>.J' type=kotlin.Unit origin=null
$this: CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.J' type=<root>.J origin=null
$this: CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.J' type=<root>.J origin=null
r: ERROR_CALL 'Unresolved reference: <Unresolved name: test9>#' type=IrErrorType
@@ -3,7 +3,7 @@ FILE fqName:<root> fileName:/Derived.kt
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Derived
CONSTRUCTOR visibility:public <> () returnType:<root>.Derived [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.Base'
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.Base'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[<root>.Base]'
FUN name:setValue visibility:public modality:FINAL <> ($this:<root>.Derived, v:kotlin.Any) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.Derived
+1 -1
View File
@@ -3,7 +3,7 @@ FILE fqName:<root> fileName:/javaInnerClass.kt
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Test1
CONSTRUCTOR visibility:public <> () returnType:<root>.Test1 [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.J'
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.J'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test1 modality:FINAL visibility:public superTypes:[<root>.J]'
PROPERTY name:test visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:test type:IrErrorType visibility:public [final]
@@ -3,7 +3,7 @@ FILE fqName:<root> fileName:/javaSyntheticProperty.kt
FIELD PROPERTY_BACKING_FIELD name:test type:kotlin.String? visibility:public [final,static]
EXPRESSION_BODY
CALL 'public open fun getFoo (): kotlin.String? declared in <root>.J' type=kotlin.String? origin=null
$this: CONSTRUCTOR_CALL 'public/*package*/ constructor <init> () declared in <root>.J' type=<root>.J origin=null
$this: CONSTRUCTOR_CALL 'public/*package*/ constructor <init> () [primary] declared in <root>.J' type=<root>.J origin=null
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test> visibility:public modality:FINAL <> () returnType:kotlin.String?
correspondingProperty: PROPERTY name:test visibility:public modality:FINAL [val]
BLOCK_BODY