[FIR2IR] Move delegating constructor call to body start

This commit is contained in:
Mikhail Glukhikh
2020-03-16 16:26:15 +03:00
parent 10c2aa1657
commit 51c83e5f62
9 changed files with 32 additions and 33 deletions
@@ -94,31 +94,28 @@ internal class ClassMemberGenerator(
valueParameter.setDefaultValue(firValueParameter)
}
}
var body = firFunction?.body?.let { visitor.convertToIrBlockBody(it) }
if (firFunction is FirConstructor && irFunction is IrConstructor && !parentAsClass.isAnnotationClass) {
if (body == null) {
body = IrBlockBodyImpl(startOffset, endOffset)
}
val body = IrBlockBodyImpl(startOffset, endOffset)
val delegatedConstructor = firFunction.delegatedConstructor
if (delegatedConstructor != null) {
val irDelegatingConstructorCall = delegatedConstructor.toIrDelegatingConstructorCall()
body.statements += irDelegatingConstructorCall ?: delegatedConstructor.convertWithOffsets { startOffset, endOffset ->
IrErrorCallExpressionImpl(
startOffset, endOffset, returnType, "Cannot find delegated constructor call"
)
}
body.statements += irDelegatingConstructorCall
}
if (delegatedConstructor?.isThis == false) {
val irClass = parent as IrClass
body.statements += IrInstanceInitializerCallImpl(
startOffset, endOffset, irClass.symbol, irFunction.constructedClassType
val instanceInitializerCall = IrInstanceInitializerCallImpl(
startOffset, endOffset, (parent as IrClass).symbol, irFunction.constructedClassType
)
body.statements += instanceInitializerCall
}
val regularBody = firFunction.body?.let { visitor.convertToIrBlockBody(it) }
if (regularBody != null) {
body.statements += regularBody.statements
}
if (body.statements.isNotEmpty()) {
irFunction.body = body
}
} else if (irFunction !is IrConstructor) {
irFunction.body = body
irFunction.body = firFunction?.body?.let { visitor.convertToIrBlockBody(it) }
}
if (irFunction !is IrConstructor || !irFunction.isPrimary) {
// Scope for primary constructor should be left after class declaration
@@ -247,10 +244,14 @@ internal class ClassMemberGenerator(
return this
}
private fun FirDelegatedConstructorCall.toIrDelegatingConstructorCall(): IrCallWithIndexedArgumentsBase? {
private fun FirDelegatedConstructorCall.toIrDelegatingConstructorCall(): IrExpressionBase {
val constructedIrType = constructedTypeRef.toIrType()
val constructorSymbol = (this.calleeReference as? FirResolvedNamedReference)?.resolvedSymbol as? FirConstructorSymbol
?: return null
?: return convertWithOffsets { startOffset, endOffset ->
IrErrorCallExpressionImpl(
startOffset, endOffset, constructedIrType, "Cannot find delegated constructor call"
)
}
return convertWithOffsets { startOffset, endOffset ->
val irConstructorSymbol = declarationStorage.getIrFunctionSymbol(constructorSymbol) as IrConstructorSymbol
if (constructorSymbol.fir.isFromEnumClass || constructorSymbol.fir.returnTypeRef.isEnum) {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class A {
val prop: Int
constructor(arg: Boolean) {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class A {
val prop: Int
constructor(arg: Boolean) {
@@ -83,7 +83,7 @@ FILE fqName:<root> fileName:/argumentReorderingInDelegatingConstructorCall.kt
VALUE_PARAMETER name:xx index:0 type:kotlin.Int
VALUE_PARAMETER name:yy index:1 type:kotlin.Int
BLOCK_BODY
ERROR_CALL 'Cannot find delegated constructor call' type=<root>.Test2
ERROR_CALL 'Cannot find delegated constructor call' type=kotlin.Any
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test2 modality:FINAL visibility:public superTypes:[<root>.Base]'
CONSTRUCTOR visibility:public <> (xxx:kotlin.Int, yyy:kotlin.Int, a:kotlin.Any) returnType:<root>.Test2
VALUE_PARAMETER name:xxx index:0 type:kotlin.Int
@@ -80,32 +80,32 @@ FILE fqName:<root> fileName:/enumWithMultipleCtors.kt
CONSTRUCTOR visibility:private <> (arg:kotlin.String) returnType:<root>.A
VALUE_PARAMETER name:arg index:0 type:kotlin.String
BLOCK_BODY
SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:prop1 type:kotlin.String visibility:private [final]' type=kotlin.Unit origin=null
receiver: GET_VAR '<this>: <root>.A declared in <root>.A' type=<root>.A origin=null
value: GET_VAR 'arg: kotlin.String declared in <root>.A.<init>' type=kotlin.String origin=null
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.A
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:A modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.A>]'
SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:prop1 type:kotlin.String visibility:private [final]' type=kotlin.Unit origin=null
receiver: GET_VAR '<this>: <root>.A declared in <root>.A' type=<root>.A origin=null
value: GET_VAR 'arg: kotlin.String declared in <root>.A.<init>' type=kotlin.String origin=null
CONSTRUCTOR visibility:private <> () returnType:<root>.A
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.A
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:A modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.A>]'
SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:prop1 type:kotlin.String visibility:private [final]' type=kotlin.Unit origin=null
receiver: GET_VAR '<this>: <root>.A declared in <root>.A' type=<root>.A origin=null
value: CONST String type=kotlin.String value="default"
CALL 'public final fun <set-prop3> (<set-?>: kotlin.String): kotlin.Unit declared in <root>.A' type=kotlin.Unit origin=EQ
$this: GET_VAR '<this>: <root>.A declared in <root>.A' type=<root>.A origin=null
<set-?>: CONST String type=kotlin.String value="empty"
ENUM_CONSTRUCTOR_CALL 'public constructor <init> (name: kotlin.String, ordinal: kotlin.Int) [primary] declared in kotlin.Enum'
<E>: <root>.A
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:A modality:OPEN visibility:public superTypes:[kotlin.Enum<<root>.A>]'
CONSTRUCTOR visibility:private <> (x:kotlin.Int) returnType:<root>.A
VALUE_PARAMETER name:x index:0 type:kotlin.Int
BLOCK_BODY
CALL 'public final fun <set-prop3> (<set-?>: kotlin.String): kotlin.Unit declared in <root>.A' type=kotlin.Unit origin=EQ
$this: GET_VAR '<this>: <root>.A declared in <root>.A' type=<root>.A origin=null
<set-?>: CONST String type=kotlin.String value="int"
ENUM_CONSTRUCTOR_CALL 'private constructor <init> (arg: kotlin.String) declared in <root>.A'
arg: CALL 'public open fun toString (): kotlin.String declared in kotlin.Any' type=kotlin.String origin=null
$this: GET_VAR 'x: kotlin.Int declared in <root>.A.<init>' type=kotlin.Int origin=null
CALL 'public final fun <set-prop3> (<set-?>: kotlin.String): kotlin.Unit declared in <root>.A' type=kotlin.Unit origin=EQ
$this: GET_VAR '<this>: <root>.A declared in <root>.A' type=<root>.A origin=null
<set-?>: CONST String type=kotlin.String value="int"
FUN name:f visibility:public modality:OPEN <> ($this:<root>.A) returnType:kotlin.String
$this: VALUE_PARAMETER name:<this> type:<root>.A
BLOCK_BODY
+2 -2
View File
@@ -214,11 +214,11 @@ FILE fqName:<root> fileName:/initVar.kt
value: GET_VAR 'value: kotlin.Int declared in <root>.TestInitVarWithCustomSetterInCtor.<set-x>' type=kotlin.Int origin=null
CONSTRUCTOR visibility:public <> () returnType:<root>.TestInitVarWithCustomSetterInCtor
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:TestInitVarWithCustomSetterInCtor modality:FINAL visibility:public superTypes:[kotlin.Any]'
CALL 'public final fun <set-x> (value: kotlin.Int): kotlin.Unit declared in <root>.TestInitVarWithCustomSetterInCtor' type=kotlin.Unit origin=EQ
$this: GET_VAR '<this>: <root>.TestInitVarWithCustomSetterInCtor declared in <root>.TestInitVarWithCustomSetterInCtor' type=<root>.TestInitVarWithCustomSetterInCtor origin=null
value: CONST Int type=kotlin.Int value=42
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:TestInitVarWithCustomSetterInCtor modality:FINAL visibility:public superTypes:[kotlin.Any]'
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
@@ -8,6 +8,7 @@ FILE fqName:<root> fileName:/implicitCastInReturnFromConstructor.kt
CONSTRUCTOR visibility:public <> (x:kotlin.Any?) returnType:<root>.C
VALUE_PARAMETER name:x index:0 type:kotlin.Any?
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.C'
WHEN type=kotlin.Unit origin=IF
BRANCH
if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Unit
@@ -15,7 +16,6 @@ FILE fqName:<root> fileName:/implicitCastInReturnFromConstructor.kt
then: RETURN type=kotlin.Nothing from='public constructor <init> (x: kotlin.Any?) declared in <root>.C'
TYPE_OP type=kotlin.Unit origin=IMPLICIT_CAST typeOperand=kotlin.Unit
GET_VAR 'x: kotlin.Any? declared in <root>.C.<init>' type=kotlin.Any? origin=null
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.C'
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
+2 -2
View File
@@ -75,6 +75,8 @@ FILE fqName:<root> fileName:/kt16904.kt
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Test1
CONSTRUCTOR visibility:public <> () returnType:<root>.Test1
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test1 modality:FINAL visibility:public superTypes:[<root>.A]'
CALL 'public final fun plusAssign (x: kotlin.Int): kotlin.Unit [operator] declared in <root>.B' type=kotlin.Unit origin=null
$this: CALL 'public final fun <get-x> (): <root>.B declared in <root>.A' type=<root>.B origin=GET_PROPERTY
$this: GET_VAR '<this>: <root>.Test1 declared in <root>.Test1' type=<root>.Test1 origin=null
@@ -84,8 +86,6 @@ FILE fqName:<root> fileName:/kt16904.kt
$this: CALL 'public final fun <get-y> (): kotlin.Int declared in <root>.A' type=kotlin.Int origin=GET_PROPERTY
$this: GET_VAR '<this>: <root>.Test1 declared in <root>.Test1' type=<root>.Test1 origin=null
other: CONST Int type=kotlin.Int value=42
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Test1 modality:FINAL visibility:public superTypes:[<root>.A]'
PROPERTY FAKE_OVERRIDE name:x visibility:public modality:FINAL [fake_override,val]
FUN FAKE_OVERRIDE name:<get-x> visibility:public modality:FINAL <> ($this:<root>.Test1) returnType:<root>.B [fake_override]
correspondingProperty: PROPERTY FAKE_OVERRIDE name:x visibility:public modality:FINAL [fake_override,val]
@@ -8,7 +8,7 @@ FILE fqName:<root> fileName:/thisReferenceBeforeClassDeclared.kt
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.test.<no name provided>
CONSTRUCTOR visibility:private <> () returnType:<root>.test.<no name provided> [primary]
BLOCK_BODY
ERROR_CALL 'Cannot find delegated constructor call' type=<root>.test.<no name provided>
ERROR_CALL 'Cannot find delegated constructor call' type=<root>.WithCompanion
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:<no name provided> modality:FINAL visibility:local superTypes:[<root>.WithCompanion]'
CONSTRUCTOR_CALL 'private constructor <init> () [primary] declared in <root>.test.<no name provided>' type=<root>.test.<no name provided> origin=OBJECT_LITERAL
VAR name:test2 type:<root>.test.<no name provided> [val]