Generate nested initializers for class.
This commit is contained in:
committed by
Dmitry Petrov
parent
415265276a
commit
ba85e714e3
+2
-1
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.psi2ir.generators
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor
|
||||
@@ -158,7 +159,7 @@ class AssignmentGenerator(statementGenerator: StatementGenerator) : StatementGen
|
||||
|
||||
private fun isValInitializationInConstructor(descriptor: PropertyDescriptor, resolvedCall: ResolvedCall<*>): Boolean =
|
||||
!descriptor.isVar &&
|
||||
statementGenerator.scopeOwner is ConstructorDescriptor &&
|
||||
statementGenerator.scopeOwner.let { it is ConstructorDescriptor || it is ClassDescriptor } &&
|
||||
resolvedCall.dispatchReceiver is ThisClassReceiver
|
||||
|
||||
private fun generateArrayAccessAssignmentReceiver(ktLeft: KtArrayAccessExpression, irOperator: IrOperator): ArrayAccessAssignmentReceiver {
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.psi2ir.generators
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
@@ -27,7 +28,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import java.util.*
|
||||
|
||||
class BodyGenerator(val scopeOwner: CallableDescriptor, override val context: GeneratorContext): GeneratorWithScope {
|
||||
class BodyGenerator(val scopeOwner: DeclarationDescriptor, override val context: GeneratorContext): GeneratorWithScope {
|
||||
override val scope = Scope(scopeOwner)
|
||||
private val loopTable = HashMap<KtLoopExpression, IrLoop>()
|
||||
|
||||
@@ -79,8 +80,12 @@ class BodyGenerator(val scopeOwner: CallableDescriptor, override val context: Ge
|
||||
private fun IrExpression.wrapWithReturn() =
|
||||
if (KotlinBuiltIns.isNothing(type))
|
||||
this
|
||||
else
|
||||
IrReturnImpl(startOffset, endOffset, context.builtIns.nothingType, scopeOwner, this)
|
||||
else {
|
||||
val returnTarget = (scopeOwner as? CallableDescriptor) ?:
|
||||
throw AssertionError("'return' in a non-callable: $scopeOwner")
|
||||
IrReturnImpl(startOffset, endOffset, context.builtIns.nothingType,
|
||||
returnTarget, this)
|
||||
}
|
||||
|
||||
|
||||
fun generateSecondaryConstructorBody(ktConstructor: KtSecondaryConstructor): IrBody {
|
||||
@@ -124,11 +129,13 @@ class BodyGenerator(val scopeOwner: CallableDescriptor, override val context: Ge
|
||||
return irBlockBody
|
||||
}
|
||||
|
||||
fun generateSecondaryConstructorBodyWithClassInitializers(ktConstructor: KtSecondaryConstructor, ktClassOrObject: KtClassOrObject): IrBody {
|
||||
fun generateSecondaryConstructorBodyWithNestedInitializers(ktConstructor: KtSecondaryConstructor, ktClassOrObject: KtClassOrObject): IrBody {
|
||||
val irBlockBody = IrBlockBodyImpl(ktClassOrObject.startOffset, ktClassOrObject.endOffset)
|
||||
|
||||
generateDelegatingConstructorCall(irBlockBody, ktConstructor)
|
||||
generateInitializersForClassBody(irBlockBody, ktClassOrObject)
|
||||
|
||||
irBlockBody.addStatement(IrNestedInitializersCallImpl(ktConstructor.startOffset, ktConstructor.endOffset,
|
||||
getOrFail(BindingContext.CLASS, ktClassOrObject)))
|
||||
|
||||
ktConstructor.bodyExpression?.let { ktBody ->
|
||||
createStatementGenerator().generateBlockBodyStatements(irBlockBody, ktBody)
|
||||
@@ -150,6 +157,12 @@ class BodyGenerator(val scopeOwner: CallableDescriptor, override val context: Ge
|
||||
}
|
||||
}
|
||||
|
||||
fun generateNestedInitializersBody(ktClassOrObject: KtClassOrObject): IrBody {
|
||||
val irBody = IrBlockBodyImpl(ktClassOrObject.startOffset, ktClassOrObject.endOffset)
|
||||
generateInitializersForClassBody(irBody, ktClassOrObject)
|
||||
return irBody
|
||||
}
|
||||
|
||||
private fun generateInitializersForClassBody(irBlockBody: IrBlockBodyImpl, ktClassOrObject: KtClassOrObject) {
|
||||
ktClassOrObject.getBody()?.let { ktClassBody ->
|
||||
for (ktDeclaration in ktClassBody.declarations) {
|
||||
|
||||
@@ -37,8 +37,14 @@ class ClassGenerator(val declarationGenerator: DeclarationGenerator) : Generator
|
||||
val irClass = IrClassImpl(ktClassOrObject.startOffset, ktClassOrObject.endOffset, IrDeclarationOrigin.DEFINED, descriptor)
|
||||
|
||||
generatePrimaryConstructor(irClass, ktClassOrObject)
|
||||
|
||||
generatePropertiesDeclaredInPrimaryConstructor(irClass, ktClassOrObject)
|
||||
generateMembersDeclaredInClassBody(irClass, ktClassOrObject)
|
||||
|
||||
val shouldGenerateNestedInitializers = generateMembersDeclaredInClassBody(irClass, ktClassOrObject)
|
||||
|
||||
if (shouldGenerateNestedInitializers) {
|
||||
irClass.nestedInitializers = BodyGenerator(descriptor, context).generateNestedInitializersBody(ktClassOrObject)
|
||||
}
|
||||
|
||||
return irClass
|
||||
}
|
||||
@@ -64,14 +70,18 @@ class ClassGenerator(val declarationGenerator: DeclarationGenerator) : Generator
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateMembersDeclaredInClassBody(irClass: IrClassImpl, ktClassOrObject: KtClassOrObject) {
|
||||
private fun generateMembersDeclaredInClassBody(irClass: IrClassImpl, ktClassOrObject: KtClassOrObject): Boolean {
|
||||
var hasConstructorsWithNestedInitializersCall = false
|
||||
|
||||
ktClassOrObject.getBody()?.let { ktClassBody ->
|
||||
for (ktDeclaration in ktClassBody.declarations) {
|
||||
if (ktDeclaration is KtAnonymousInitializer) continue
|
||||
|
||||
val irMember =
|
||||
if (ktDeclaration is KtSecondaryConstructor && isConstructorDelegatingToSuper(ktDeclaration, irClass.descriptor))
|
||||
declarationGenerator.generateSecondaryConstructorWithClassInitializers(ktDeclaration, ktClassOrObject)
|
||||
if (ktDeclaration is KtSecondaryConstructor && isConstructorDelegatingToSuper(ktDeclaration, irClass.descriptor)) {
|
||||
hasConstructorsWithNestedInitializersCall = true
|
||||
declarationGenerator.generateSecondaryConstructorWithNestedInitializers(ktDeclaration, ktClassOrObject)
|
||||
}
|
||||
else
|
||||
declarationGenerator.generateMemberDeclaration(ktDeclaration)
|
||||
|
||||
@@ -82,6 +92,8 @@ class ClassGenerator(val declarationGenerator: DeclarationGenerator) : Generator
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return hasConstructorsWithNestedInitializersCall
|
||||
}
|
||||
|
||||
private fun isConstructorDelegatingToSuper(ktConstructor: KtSecondaryConstructor, classOwner: ClassDescriptor): Boolean {
|
||||
@@ -99,7 +111,4 @@ class ClassGenerator(val declarationGenerator: DeclarationGenerator) : Generator
|
||||
irProperty.valueInitializer = IrExpressionBodyImpl(ktParameter.startOffset, ktParameter.endOffset, irGetParameter)
|
||||
return irProperty
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -66,9 +66,9 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
|
||||
}
|
||||
|
||||
|
||||
fun generateSecondaryConstructorWithClassInitializers(ktConstructor: KtSecondaryConstructor, ktClassOrObject: KtClassOrObject): IrDeclaration {
|
||||
fun generateSecondaryConstructorWithNestedInitializers(ktConstructor: KtSecondaryConstructor, ktClassOrObject: KtClassOrObject): IrDeclaration {
|
||||
val constructorDescriptor = getOrFail(BindingContext.CONSTRUCTOR, ktConstructor)
|
||||
val body = createBodyGenerator(constructorDescriptor).generateSecondaryConstructorBodyWithClassInitializers(ktConstructor, ktClassOrObject)
|
||||
val body = createBodyGenerator(constructorDescriptor).generateSecondaryConstructorBodyWithNestedInitializers(ktConstructor, ktClassOrObject)
|
||||
return IrFunctionImpl(ktConstructor.startOffset, ktConstructor.endOffset, IrDeclarationOrigin.DEFINED,
|
||||
constructorDescriptor, body)
|
||||
}
|
||||
|
||||
+6
-3
@@ -46,7 +46,7 @@ import java.lang.AssertionError
|
||||
|
||||
class StatementGenerator(
|
||||
override val context: GeneratorContext,
|
||||
val scopeOwner: CallableDescriptor,
|
||||
val scopeOwner: DeclarationDescriptor,
|
||||
val bodyGenerator: BodyGenerator,
|
||||
override val scope: Scope
|
||||
) : KtVisitor<IrStatement, Nothing?>(), GeneratorWithScope {
|
||||
@@ -125,9 +125,12 @@ class StatementGenerator(
|
||||
return IrReturnImpl(expression.startOffset, expression.endOffset, context.builtIns.nothingType, returnTarget, irReturnedExpression)
|
||||
}
|
||||
|
||||
private fun scopeOwnerAsCallable() =
|
||||
(scopeOwner as? CallableDescriptor) ?: throw AssertionError("'return' in a non-callable: $scopeOwner")
|
||||
|
||||
private fun getReturnExpressionTarget(expression: KtReturnExpression): CallableDescriptor =
|
||||
if (!ExpressionTypingUtils.isFunctionLiteral(scopeOwner) && !ExpressionTypingUtils.isFunctionExpression(scopeOwner)) {
|
||||
scopeOwner ?: throw AssertionError("Return not in a callable: $scopeOwner")
|
||||
scopeOwnerAsCallable()
|
||||
}
|
||||
else {
|
||||
val label = expression.getTargetLabel()
|
||||
@@ -140,7 +143,7 @@ class StatementGenerator(
|
||||
BindingContextUtils.getContainingFunctionSkipFunctionLiterals(scopeOwner, true).first
|
||||
}
|
||||
else {
|
||||
scopeOwner ?: throw AssertionError("Return not in a callable: $scopeOwner")
|
||||
scopeOwnerAsCallable()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -36,7 +36,7 @@ class SimplePropertyLValue(
|
||||
override val type: KotlinType get() = descriptor.type
|
||||
|
||||
override fun load(): IrExpression {
|
||||
val getter = descriptor.getter!!
|
||||
val getter = descriptor.getter ?: throw AssertionError("No getter for $descriptor")
|
||||
return callReceiver.call { dispatchReceiverValue, extensionReceiverValue ->
|
||||
IrGetterCallImpl(startOffset, endOffset, getter,
|
||||
dispatchReceiverValue?.load(),
|
||||
@@ -47,7 +47,7 @@ class SimplePropertyLValue(
|
||||
}
|
||||
|
||||
override fun store(irExpression: IrExpression): IrExpression {
|
||||
val setter = descriptor.setter!!
|
||||
val setter = descriptor.setter ?: throw AssertionError("No setter for $descriptor")
|
||||
return callReceiver.call { dispatchReceiverValue, extensionReceiverValue ->
|
||||
IrSetterCallImpl(startOffset, endOffset, setter,
|
||||
dispatchReceiverValue?.load(),
|
||||
|
||||
+7
-4
@@ -16,16 +16,19 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.expressions
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
|
||||
interface IrNestedInitializersCall : IrExpression
|
||||
interface IrNestedInitializersCall : IrExpression {
|
||||
val classDescriptor: ClassDescriptor
|
||||
}
|
||||
|
||||
class IrNestedInitializersCallImpl(
|
||||
startOffset: Int,
|
||||
endOffset: Int,
|
||||
type: KotlinType
|
||||
) : IrTerminalExpressionBase(startOffset, endOffset, type), IrNestedInitializersCall {
|
||||
override val classDescriptor: ClassDescriptor
|
||||
) : IrTerminalExpressionBase(startOffset, endOffset, classDescriptor.builtIns.unitType), IrNestedInitializersCall {
|
||||
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
|
||||
return visitor.visitNestedInitializersCall(this, data)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.ir.util
|
||||
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.SourceLocationManager
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
|
||||
@@ -43,6 +44,15 @@ class DumpIrTreeVisitor(out: Appendable): IrElementVisitor<Unit, String> {
|
||||
element.dumpLabeledSubTree(data)
|
||||
}
|
||||
|
||||
override fun visitClass(declaration: IrClass, data: String) {
|
||||
declaration.dumpLabeledElementWith(data) {
|
||||
declaration.nestedInitializers?.accept(this, "nestedInitializers")
|
||||
declaration.members.forEach {
|
||||
it.accept(this, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitCall(expression: IrCall, data: String) {
|
||||
expression.dumpLabeledElementWith(data) {
|
||||
expression.dispatchReceiver?.accept(this, "\$this")
|
||||
|
||||
@@ -98,7 +98,7 @@ class RenderIrElementVisitor : IrElementVisitor<String, Nothing?> {
|
||||
superQualifier?.let { "superQualifier=${it.name} " } ?: ""
|
||||
|
||||
override fun visitNestedInitializersCall(expression: IrNestedInitializersCall, data: Nothing?): String =
|
||||
"NESTED_INITIALIZERS_CALL"
|
||||
"NESTED_INITIALIZERS_CALL classDescriptor=${expression.classDescriptor.name}"
|
||||
|
||||
override fun visitGetVariable(expression: IrGetVariable, data: Nothing?): String =
|
||||
"GET_VAR ${expression.descriptor.name} type=${expression.type.render()} operator=${expression.operator}"
|
||||
|
||||
+3
@@ -3,12 +3,15 @@ FILE /delegatingConstructorCallsInSecondaryConstructors.kt
|
||||
FUN public constructor Base()
|
||||
BLOCK_BODY
|
||||
CLASS CLASS Test
|
||||
nestedInitializers: BLOCK_BODY
|
||||
FUN public constructor Test()
|
||||
BLOCK_BODY
|
||||
CALL .<init> type=Base operator=DELEGATING_CONSTRUCTOR_CALL
|
||||
NESTED_INITIALIZERS_CALL classDescriptor=Test
|
||||
FUN public constructor Test(/*0*/ xx: kotlin.Int)
|
||||
BLOCK_BODY
|
||||
CALL .<init> type=Base operator=DELEGATING_CONSTRUCTOR_CALL
|
||||
NESTED_INITIALIZERS_CALL classDescriptor=Test
|
||||
FUN public constructor Test(/*0*/ xx: kotlin.Short)
|
||||
BLOCK_BODY
|
||||
CALL .<init> type=Test operator=DELEGATING_CONSTRUCTOR_CALL
|
||||
|
||||
+8
-4
@@ -36,6 +36,11 @@ FILE /initVar.kt
|
||||
SET_BACKING_FIELD x type=kotlin.Unit operator=EQ
|
||||
GET_VAR value type=kotlin.Int operator=null
|
||||
CLASS CLASS TestInitVarWithCustomSetterWithExplicitCtor
|
||||
nestedInitializers: BLOCK_BODY
|
||||
BLOCK type=kotlin.Unit operator=null
|
||||
CALL .<set-x> type=kotlin.Unit operator=EQ
|
||||
$this: THIS public final class TestInitVarWithCustomSetterWithExplicitCtor type=TestInitVarWithCustomSetterWithExplicitCtor
|
||||
value: CONST Int type=kotlin.Int value='0'
|
||||
PROPERTY public final var x: kotlin.Int getter=null setter=<set-x>
|
||||
PROPERTY_SETTER public final fun <set-x>(/*0*/ value: kotlin.Int): kotlin.Unit property=x
|
||||
BLOCK_BODY
|
||||
@@ -44,11 +49,9 @@ FILE /initVar.kt
|
||||
FUN public constructor TestInitVarWithCustomSetterWithExplicitCtor()
|
||||
BLOCK_BODY
|
||||
CALL .<init> type=kotlin.Any operator=DELEGATING_CONSTRUCTOR_CALL
|
||||
BLOCK type=kotlin.Unit operator=null
|
||||
CALL .<set-x> type=kotlin.Unit operator=EQ
|
||||
$this: THIS public final class TestInitVarWithCustomSetterWithExplicitCtor type=TestInitVarWithCustomSetterWithExplicitCtor
|
||||
value: CONST Int type=kotlin.Int value='0'
|
||||
NESTED_INITIALIZERS_CALL classDescriptor=TestInitVarWithCustomSetterWithExplicitCtor
|
||||
CLASS CLASS TestInitVarWithCustomSetterInCtor
|
||||
nestedInitializers: BLOCK_BODY
|
||||
PROPERTY public final var x: kotlin.Int getter=null setter=<set-x>
|
||||
PROPERTY_SETTER public final fun <set-x>(/*0*/ value: kotlin.Int): kotlin.Unit property=x
|
||||
BLOCK_BODY
|
||||
@@ -57,6 +60,7 @@ FILE /initVar.kt
|
||||
FUN public constructor TestInitVarWithCustomSetterInCtor()
|
||||
BLOCK_BODY
|
||||
CALL .<init> type=kotlin.Any operator=DELEGATING_CONSTRUCTOR_CALL
|
||||
NESTED_INITIALIZERS_CALL classDescriptor=TestInitVarWithCustomSetterInCtor
|
||||
CALL .<set-x> type=kotlin.Unit operator=EQ
|
||||
$this: THIS public final class TestInitVarWithCustomSetterInCtor type=TestInitVarWithCustomSetterInCtor
|
||||
value: CONST Int type=kotlin.Int value='42'
|
||||
|
||||
+8
-4
@@ -3,19 +3,23 @@ FILE /secondaryConstructorWithInitializersFromClassBody.kt
|
||||
FUN public constructor Base()
|
||||
BLOCK_BODY
|
||||
CLASS CLASS TestProperty
|
||||
nestedInitializers: BLOCK_BODY
|
||||
SET_BACKING_FIELD x type=kotlin.Unit operator=null
|
||||
CONST Int type=kotlin.Int value='0'
|
||||
PROPERTY public final val x: kotlin.Int = 0 getter=null setter=null
|
||||
EXPRESSION_BODY
|
||||
CONST Int type=kotlin.Int value='0'
|
||||
FUN public constructor TestProperty()
|
||||
BLOCK_BODY
|
||||
CALL .<init> type=Base operator=DELEGATING_CONSTRUCTOR_CALL
|
||||
NESTED_INITIALIZERS_CALL classDescriptor=TestProperty
|
||||
CLASS CLASS TestInitBlock
|
||||
nestedInitializers: BLOCK_BODY
|
||||
BLOCK type=kotlin.Unit operator=null
|
||||
SET_BACKING_FIELD x type=kotlin.Unit operator=null
|
||||
CONST Int type=kotlin.Int value='0'
|
||||
CLASS CLASS TestInitBlock
|
||||
PROPERTY public final val x: kotlin.Int getter=null setter=null
|
||||
FUN public constructor TestInitBlock()
|
||||
BLOCK_BODY
|
||||
CALL .<init> type=Base operator=DELEGATING_CONSTRUCTOR_CALL
|
||||
BLOCK type=kotlin.Unit operator=null
|
||||
SET_BACKING_FIELD x type=kotlin.Unit operator=null
|
||||
CONST Int type=kotlin.Int value='0'
|
||||
NESTED_INITIALIZERS_CALL classDescriptor=TestInitBlock
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
FILE /secondaryConstructors.kt
|
||||
CLASS CLASS C
|
||||
nestedInitializers: BLOCK_BODY
|
||||
FUN public constructor C()
|
||||
BLOCK_BODY
|
||||
CALL .<init> type=C operator=DELEGATING_CONSTRUCTOR_CALL
|
||||
@@ -7,3 +8,4 @@ FILE /secondaryConstructors.kt
|
||||
FUN public constructor C(/*0*/ x: kotlin.Int)
|
||||
BLOCK_BODY
|
||||
CALL .<init> type=kotlin.Any operator=DELEGATING_CONSTRUCTOR_CALL
|
||||
NESTED_INITIALIZERS_CALL classDescriptor=C
|
||||
|
||||
Reference in New Issue
Block a user