Property initializers from primary constructor parameters

are generated as property initializer expressions
and should not be repeated in primary constructor.
This commit is contained in:
Dmitry Petrov
2016-09-06 15:48:36 +03:00
committed by Dmitry Petrov
parent a9dbaca473
commit 8fc16345e6
17 changed files with 0 additions and 110 deletions
@@ -165,7 +165,6 @@ class BodyGenerator(val scopeOwner: DeclarationDescriptor, override val context:
val irBlockBody = IrBlockBodyImpl(ktClassOrObject.startOffset, ktClassOrObject.endOffset)
generateSuperConstructorCall(irBlockBody, ktClassOrObject)
generateInitializersForPropertiesDefinedInPrimaryConstructor(irBlockBody, ktClassOrObject)
val classDescriptor = (scopeOwner as ConstructorDescriptor).containingDeclaration
irBlockBody.addStatement(IrInstanceInitializerCallImpl(ktClassOrObject.startOffset, ktClassOrObject.endOffset, classDescriptor))
@@ -280,33 +279,5 @@ class BodyGenerator(val scopeOwner: DeclarationDescriptor, override val context:
enumClassConstructor, enumEntryOrNull)
}
private fun generateInitializersForPropertiesDefinedInPrimaryConstructor(irBlockBody: IrBlockBodyImpl, ktClassOrObject: KtClassOrObject) {
ktClassOrObject.getPrimaryConstructor()?.let { ktPrimaryConstructor ->
for (ktParameter in ktPrimaryConstructor.valueParameters) {
if (ktParameter.hasValOrVar()) {
val propertyDescriptor = getOrFail(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, ktParameter)
val valueParameterDescriptor = getOrFail(BindingContext.VALUE_PARAMETER, ktParameter)
irBlockBody.addStatement(
initializePropertyInPrimaryConstructor(
ktParameter, propertyDescriptor,
IrGetVariableImpl(ktParameter.startOffset, ktParameter.endOffset,
valueParameterDescriptor, IrOperator.INITIALIZE_PROPERTY_FROM_PARAMETER)
))
}
}
}
}
private fun initializePropertyInPrimaryConstructor(
ktElement: KtElement,
propertyDescriptor: PropertyDescriptor,
value: IrExpression
): IrExpression {
val thisClass = propertyDescriptor.containingDeclaration as ClassDescriptor
return IrSetBackingFieldImpl(ktElement.startOffset, ktElement.endOffset, propertyDescriptor,
IrThisReferenceImpl(ktElement.startOffset, ktElement.endOffset, thisClass.defaultType, thisClass),
value)
}
}
@@ -3,12 +3,6 @@ FILE /argumentReorderingInDelegatingConstructorCall.kt
CONSTRUCTOR public constructor Base(x: kotlin.Int, y: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Base' type=Base
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
SET_BACKING_FIELD 'y: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Base' type=Base
value: GET_VAR 'value-parameter y: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Base'
PROPERTY public final val x: kotlin.Int
EXPRESSION_BODY
-6
View File
@@ -5,12 +5,6 @@ FILE /classMembers.kt
CONST Int type=kotlin.Int value='1'
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'y: Int' type=kotlin.Unit operator=null
receiver: THIS of 'C' type=C
value: GET_VAR 'value-parameter y: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
SET_BACKING_FIELD 'z: Int' type=kotlin.Unit operator=null
receiver: THIS of 'C' type=C
value: GET_VAR 'value-parameter z: Int = ...' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='C'
PROPERTY public final val y: kotlin.Int
EXPRESSION_BODY
-9
View File
@@ -3,15 +3,6 @@ FILE /dataClasses.kt
CONSTRUCTOR public constructor Test1(x: kotlin.Int, y: kotlin.String, z: kotlin.Any)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Test1' type=Test1
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
SET_BACKING_FIELD 'y: String' type=kotlin.Unit operator=null
receiver: THIS of 'Test1' type=Test1
value: GET_VAR 'value-parameter y: String' type=kotlin.String operator=INITIALIZE_PROPERTY_FROM_PARAMETER
SET_BACKING_FIELD 'z: Any' type=kotlin.Unit operator=null
receiver: THIS of 'Test1' type=Test1
value: GET_VAR 'value-parameter z: Any' type=kotlin.Any operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Test1'
PROPERTY public final val x: kotlin.Int
EXPRESSION_BODY
-6
View File
@@ -16,9 +16,6 @@ FILE /enum.kt
CONSTRUCTOR private constructor TestEnum2(x: kotlin.Int)
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'constructor Enum(String, Int)' super
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'TestEnum2' type=TestEnum2
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='TestEnum2'
PROPERTY public final val x: kotlin.Int
EXPRESSION_BODY
@@ -61,9 +58,6 @@ FILE /enum.kt
CONSTRUCTOR private constructor TestEnum4(x: kotlin.Int)
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'constructor Enum(String, Int)' super
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'TestEnum4' type=TestEnum4
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='TestEnum4'
PROPERTY public final val x: kotlin.Int
EXPRESSION_BODY
-3
View File
@@ -11,9 +11,6 @@ FILE /initBlock.kt
CONSTRUCTOR public constructor Test2(x: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Test2' type=Test2
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Test2'
PROPERTY public final val x: kotlin.Int
EXPRESSION_BODY
-3
View File
@@ -3,9 +3,6 @@ FILE /initVal.kt
CONSTRUCTOR public constructor TestInitValFromParameter(x: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'TestInitValFromParameter' type=TestInitValFromParameter
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='TestInitValFromParameter'
PROPERTY public final val x: kotlin.Int
EXPRESSION_BODY
-3
View File
@@ -3,9 +3,6 @@ FILE /initVar.kt
CONSTRUCTOR public constructor TestInitVarFromParameter(x: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'TestInitVarFromParameter' type=TestInitVarFromParameter
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='TestInitVarFromParameter'
PROPERTY public final var x: kotlin.Int
EXPRESSION_BODY
@@ -3,12 +3,6 @@ FILE /primaryConstructor.kt
CONSTRUCTOR public constructor Test1(x: kotlin.Int, y: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Test1' type=Test1
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
SET_BACKING_FIELD 'y: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Test1' type=Test1
value: GET_VAR 'value-parameter y: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Test1'
PROPERTY public final val x: kotlin.Int
EXPRESSION_BODY
@@ -20,9 +14,6 @@ FILE /primaryConstructor.kt
CONSTRUCTOR public constructor Test2(x: kotlin.Int, y: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'y: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Test2' type=Test2
value: GET_VAR 'value-parameter y: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Test2'
PROPERTY public final val y: kotlin.Int
EXPRESSION_BODY
@@ -34,9 +25,6 @@ FILE /primaryConstructor.kt
CONSTRUCTOR public constructor Test3(x: kotlin.Int, y: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'y: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Test3' type=Test3
value: GET_VAR 'value-parameter y: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Test3'
PROPERTY public final val y: kotlin.Int
EXPRESSION_BODY
@@ -18,12 +18,6 @@ FILE /primaryConstructorWithSuperConstructorCall.kt
CONSTRUCTOR public constructor TestWithDelegatingConstructor(x: kotlin.Int, y: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Base()'
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'TestWithDelegatingConstructor' type=TestWithDelegatingConstructor
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
SET_BACKING_FIELD 'y: Int' type=kotlin.Unit operator=null
receiver: THIS of 'TestWithDelegatingConstructor' type=TestWithDelegatingConstructor
value: GET_VAR 'value-parameter y: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='TestWithDelegatingConstructor'
PROPERTY public final val x: kotlin.Int
EXPRESSION_BODY
-9
View File
@@ -8,9 +8,6 @@ FILE /sealedClasses.kt
CONSTRUCTOR public constructor Const(number: kotlin.Double)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Expr()'
SET_BACKING_FIELD 'number: Double' type=kotlin.Unit operator=null
receiver: THIS of 'Const' type=Expr.Const
value: GET_VAR 'value-parameter number: Double' type=kotlin.Double operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Const'
PROPERTY public final val number: kotlin.Double
EXPRESSION_BODY
@@ -19,12 +16,6 @@ FILE /sealedClasses.kt
CONSTRUCTOR public constructor Sum(e1: Expr, e2: Expr)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Expr()'
SET_BACKING_FIELD 'e1: Expr' type=kotlin.Unit operator=null
receiver: THIS of 'Sum' type=Expr.Sum
value: GET_VAR 'value-parameter e1: Expr' type=Expr operator=INITIALIZE_PROPERTY_FROM_PARAMETER
SET_BACKING_FIELD 'e2: Expr' type=kotlin.Unit operator=null
receiver: THIS of 'Sum' type=Expr.Sum
value: GET_VAR 'value-parameter e2: Expr' type=Expr operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Sum'
PROPERTY public final val e1: Expr
EXPRESSION_BODY
@@ -20,9 +20,6 @@ FILE /delegatedProperties.kt
CONSTRUCTOR public constructor C(map: kotlin.collections.MutableMap<kotlin.String, kotlin.Any>)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'map: MutableMap<String, Any>' type=kotlin.Unit operator=null
receiver: THIS of 'C' type=C
value: GET_VAR 'value-parameter map: MutableMap<String, Any>' type=kotlin.collections.MutableMap<kotlin.String, kotlin.Any> operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='C'
PROPERTY public final val map: kotlin.collections.MutableMap<kotlin.String, kotlin.Any>
EXPRESSION_BODY
@@ -15,9 +15,6 @@ FILE /arrayAugmentedAssignment1.kt
CONSTRUCTOR public constructor C(x: kotlin.IntArray)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'x: IntArray' type=kotlin.Unit operator=null
receiver: THIS of 'C' type=C
value: GET_VAR 'value-parameter x: IntArray' type=kotlin.IntArray operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='C'
PROPERTY public final val x: kotlin.IntArray
EXPRESSION_BODY
@@ -3,9 +3,6 @@ FILE /assignments.kt
CONSTRUCTOR public constructor Ref(x: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'x: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Ref' type=Ref
value: GET_VAR 'value-parameter x: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Ref'
PROPERTY public final var x: kotlin.Int
EXPRESSION_BODY
@@ -92,9 +92,6 @@ FILE /complexAugmentedAssignment.kt
CONST Int type=kotlin.Int value='0'
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 's: Int' type=kotlin.Unit operator=null
receiver: THIS of 'B' type=B
value: GET_VAR 'value-parameter s: Int = ...' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='B'
PROPERTY public final var s: kotlin.Int
EXPRESSION_BODY
@@ -8,9 +8,6 @@ FILE /forWithImplicitReceivers.kt
CONSTRUCTOR public constructor IntCell(value: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'value: Int' type=kotlin.Unit operator=null
receiver: THIS of 'IntCell' type=IntCell
value: GET_VAR 'value-parameter value: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='IntCell'
PROPERTY public final var value: kotlin.Int
EXPRESSION_BODY
-3
View File
@@ -3,9 +3,6 @@ FILE /safeCalls.kt
CONSTRUCTOR public constructor Ref(value: kotlin.Int)
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
SET_BACKING_FIELD 'value: Int' type=kotlin.Unit operator=null
receiver: THIS of 'Ref' type=Ref
value: GET_VAR 'value-parameter value: Int' type=kotlin.Int operator=INITIALIZE_PROPERTY_FROM_PARAMETER
INSTANCE_INITIALIZER_CALL classDescriptor='Ref'
PROPERTY public final var value: kotlin.Int
EXPRESSION_BODY