From 5e9c3025bb4e9cf8402d899bdd48309742025ab7 Mon Sep 17 00:00:00 2001 From: Wojciech Litewka Date: Tue, 12 Mar 2024 13:31:20 +0100 Subject: [PATCH] [IR] Move IrValueDeclaration.isAssignable to extension property to remove custom logic from generated classes, in particular to simplify source generator itself. --- .../kotlin/backend/common/ir/ValueRemapper.kt | 3 ++- .../kotlin/ir/declarations/IrValueDeclaration.kt | 2 -- .../kotlin/ir/declarations/IrValueParameter.kt | 2 ++ .../jetbrains/kotlin/ir/declarations/IrVariable.kt | 9 --------- .../kotlin/ir/expressions/impl/IrSetValueImpl.kt | 1 + .../src/org/jetbrains/kotlin/ir/util/IrUtils.kt | 14 ++++++++++++-- .../org/jetbrains/kotlin/ir/generator/IrTree.kt | 12 +----------- 7 files changed, 18 insertions(+), 25 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/ValueRemapper.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/ValueRemapper.kt index bac2b150f44..707609b88c7 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/ValueRemapper.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/ValueRemapper.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.ir.expressions.IrSetValue import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl import org.jetbrains.kotlin.ir.expressions.impl.IrSetValueImpl import org.jetbrains.kotlin.ir.symbols.IrValueSymbol +import org.jetbrains.kotlin.ir.util.isAssignable import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid abstract class AbstractValueRemapper : IrElementTransformerVoid() { diff --git a/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrValueDeclaration.kt b/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrValueDeclaration.kt index f6c1bdacb19..977318aa689 100644 --- a/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrValueDeclaration.kt +++ b/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrValueDeclaration.kt @@ -23,6 +23,4 @@ interface IrValueDeclaration : IrDeclarationWithName, IrSymbolOwner { override val symbol: IrValueSymbol var type: IrType - - val isAssignable: Boolean } diff --git a/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrValueParameter.kt b/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrValueParameter.kt index f29cb4c2573..8cc2969af04 100644 --- a/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrValueParameter.kt +++ b/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrValueParameter.kt @@ -24,6 +24,8 @@ abstract class IrValueParameter : IrDeclarationBase(), IrValueDeclaration { @ObsoleteDescriptorBasedAPI abstract override val descriptor: ParameterDescriptor + abstract val isAssignable: Boolean + abstract override val symbol: IrValueParameterSymbol abstract var index: Int diff --git a/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrVariable.kt b/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrVariable.kt index 0c861723234..97acf813f7b 100644 --- a/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrVariable.kt +++ b/compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/declarations/IrVariable.kt @@ -11,7 +11,6 @@ package org.jetbrains.kotlin.ir.declarations import org.jetbrains.kotlin.descriptors.VariableDescriptor import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI import org.jetbrains.kotlin.ir.expressions.IrExpression -import org.jetbrains.kotlin.ir.expressions.IrSetValue import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol import org.jetbrains.kotlin.ir.visitors.IrElementTransformer import org.jetbrains.kotlin.ir.visitors.IrElementVisitor @@ -33,14 +32,6 @@ abstract class IrVariable : IrDeclarationBase(), IrValueDeclaration { abstract var initializer: IrExpression? - /** - * Variables are assignable by default. This means that they can be used in [IrSetValue]. - * Variables are assigned in the IR even though they are not 'var' in the input. Hence - * the separate assignability flag. - */ - override val isAssignable: Boolean - get() = true - override fun accept(visitor: IrElementVisitor, data: D): R = visitor.visitVariable(this, data) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrSetValueImpl.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrSetValueImpl.kt index fd3b85e275f..59784773372 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrSetValueImpl.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrSetValueImpl.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin import org.jetbrains.kotlin.ir.symbols.IrValueSymbol import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.util.IrElementConstructorIndicator +import org.jetbrains.kotlin.ir.util.isAssignable class IrSetValueImpl internal constructor( @Suppress("UNUSED_PARAMETER") diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt index cf45e0bd4a3..224de3b899a 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -1590,4 +1590,14 @@ fun IrElement.sourceElement(): AbstractKtSourceElement? = else null fun IrFunction.isTopLevelInPackage(name: String, packageFqName: FqName) = - this.name.asString() == name && parent.kotlinFqName == packageFqName \ No newline at end of file + this.name.asString() == name && parent.kotlinFqName == packageFqName + +val IrValueDeclaration.isAssignable: Boolean + get() = when (this) { + is IrValueParameter -> isAssignable + // Variables are assignable by default. This means that they can be used in [IrSetValue]. + // Variables are assigned in the IR even though they are not 'var' in the input. Hence + // the separate assignability flag. + is IrVariable -> true + else -> error("Unexpected IrValueDeclaration class $this") + } \ No newline at end of file diff --git a/compiler/ir/ir.tree/tree-generator/src/org/jetbrains/kotlin/ir/generator/IrTree.kt b/compiler/ir/ir.tree/tree-generator/src/org/jetbrains/kotlin/ir/generator/IrTree.kt index 159c56222ac..ec4182a9647 100644 --- a/compiler/ir/ir.tree/tree-generator/src/org/jetbrains/kotlin/ir/generator/IrTree.kt +++ b/compiler/ir/ir.tree/tree-generator/src/org/jetbrains/kotlin/ir/generator/IrTree.kt @@ -194,7 +194,6 @@ object IrTree : AbstractTreeBuilder() { +descriptor("ValueDescriptor") +symbol(valueSymbolType) +field("type", irTypeType) - +field("isAssignable", boolean, mutable = false) } val valueParameter: Element by element(Declaration) { needTransformMethod() @@ -203,6 +202,7 @@ object IrTree : AbstractTreeBuilder() { parent(valueDeclaration) +descriptor("ParameterDescriptor") + +field("isAssignable", boolean, mutable = false) +symbol(valueParameterSymbolType) +field("index", int) +field("varargElementType", irTypeType, nullable = true) @@ -597,16 +597,6 @@ object IrTree : AbstractTreeBuilder() { +field("isConst", boolean) +field("isLateinit", boolean) +field("initializer", expression, nullable = true) - +field("isAssignable", boolean, mutable = false) { - defaultValueInBase = "true" - withGetter = true - additionalImports.add(setValue) - kDoc = """ - Variables are assignable by default. This means that they can be used in [${setValue.typeName}]. - Variables are assigned in the IR even though they are not 'var' in the input. Hence - the separate assignability flag. - """.trimIndent() - } } val packageFragment: Element by element(Declaration) { ownsChildren = false