From 529efff8cb816b6a3d217d7819dbea0fcf5006a0 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 18 Jun 2019 16:28:40 +0200 Subject: [PATCH] IR: introduce IrAttributeContainer This is useful to store data that must be kept when the element is transformed, such as names of local/anonymous classes and EnclosingMethod values on JVM which are computed before any lowerings. For now, this is implemented very conservatively: the only field `attributeOwnerId` which can be used in BackendContext implementation as a key in the map --- .../common/lower/LocalDeclarationsLowering.kt | 1 + .../backend/jvm/lower/InterfaceLowering.kt | 1 + .../jvm/lower/JvmInlineClassLowering.kt | 3 +-- .../ir/declarations/IrAttributeContainer.kt | 25 +++++++++++++++++++ .../kotlin/ir/declarations/IrClass.kt | 3 +-- .../ir/declarations/impl/IrClassImpl.kt | 2 ++ .../ir/declarations/lazy/IrLazyClass.kt | 3 ++- .../kotlin/ir/expressions/IrExpression.kt | 3 ++- .../ir/expressions/impl/IrExpressionBase.kt | 5 +++- .../ir/util/DeepCopyIrTreeWithSymbols.kt | 1 + 10 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrAttributeContainer.kt diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt index 2db5e999fae..0eabafd454f 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt @@ -330,6 +330,7 @@ class LocalDeclarationsLowering( ).also { it.fillArguments2(expression, newCallee) it.copyTypeArgumentsFrom(expression) + it.copyAttributes(expression) } } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt index 5850c0144a7..2e790c280f9 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt @@ -140,6 +140,7 @@ private class InterfaceLowering(val context: JvmBackendContext) : IrElementTrans origin ).apply { copyTypeAndValueArgumentsFrom(expression, receiversAsArguments = true) + copyAttributes(expression) } } } else { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmInlineClassLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmInlineClassLowering.kt index 3f09cda950b..925dce21fdf 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmInlineClassLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmInlineClassLowering.kt @@ -13,7 +13,6 @@ import org.jetbrains.kotlin.backend.common.lower.irBlockBody import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin -import org.jetbrains.kotlin.backend.jvm.ir.erasedUpperBound import org.jetbrains.kotlin.backend.jvm.lower.inlineclasses.* import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.ir.IrStatement @@ -234,7 +233,7 @@ private class JvmInlineClassLowering(private val context: JvmBackendContext) : F expression.origin ).apply { buildReplacement(expression.symbol.owner, expression, replacement) - } + }.copyAttributes(expression) } override fun visitFunctionAccess(expression: IrFunctionAccessExpression): IrExpression { diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrAttributeContainer.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrAttributeContainer.kt new file mode 100644 index 00000000000..74bbb8cd2d5 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrAttributeContainer.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2010-2019 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. + */ + +package org.jetbrains.kotlin.ir.declarations + +import org.jetbrains.kotlin.ir.IrElement + +interface IrAttributeContainer : IrElement { + /** + * An object which can be used as a key in the map in the backend-specific storage. If a brand new IR element is created, this property + * should be equal to that element itself. If an element is copied from some other element, its id should be copied as well. + * + * Note that an [attributeOwnerId] of any element must be an element, whose [attributeOwnerId] is itself. In other words, + * it shouldn't be needed to call this more than once to find the original attribute owner. + */ + var attributeOwnerId: IrAttributeContainer +} + +fun D.copyAttributes(other: IrAttributeContainer?): D = apply { + if (other != null) { + attributeOwnerId = other.attributeOwnerId + } +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrClass.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrClass.kt index 1ab4d86d4d7..a23a8cc7452 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrClass.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrClass.kt @@ -24,7 +24,7 @@ import org.jetbrains.kotlin.ir.types.IrType interface IrClass : IrSymbolDeclaration, IrDeclarationWithName, IrDeclarationWithVisibility, - IrDeclarationContainer, IrTypeParametersContainer { + IrDeclarationContainer, IrTypeParametersContainer, IrAttributeContainer { override val descriptor: ClassDescriptor @@ -61,4 +61,3 @@ fun IrClass.getInstanceInitializerMembers() = else -> false } } - diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/impl/IrClassImpl.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/impl/IrClassImpl.kt index bddcf3e26cc..2a81be5fd84 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/impl/IrClassImpl.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/impl/IrClassImpl.kt @@ -83,6 +83,8 @@ class IrClassImpl( override var metadata: MetadataSource? = null + override var attributeOwnerId: IrAttributeContainer = this + override fun accept(visitor: IrElementVisitor, data: D): R = visitor.visitClass(this, data) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/IrLazyClass.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/IrLazyClass.kt index bca1257b384..0b49a6aeb89 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/IrLazyClass.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/lazy/IrLazyClass.kt @@ -10,7 +10,6 @@ import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.ir.declarations.* -import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator @@ -104,6 +103,8 @@ class IrLazyClass( } } + override var attributeOwnerId: IrAttributeContainer = this + override fun accept(visitor: IrElementVisitor, data: D): R = visitor.visitClass(this, data) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrExpression.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrExpression.kt index 939be128235..b64df400fbc 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrExpression.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrExpression.kt @@ -17,10 +17,11 @@ package org.jetbrains.kotlin.ir.expressions import org.jetbrains.kotlin.ir.IrStatement +import org.jetbrains.kotlin.ir.declarations.IrAttributeContainer import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.visitors.IrElementTransformer -interface IrExpression : IrStatement, IrVarargElement { +interface IrExpression : IrStatement, IrVarargElement, IrAttributeContainer { val type: IrType override fun transform(transformer: IrElementTransformer, data: D): IrExpression = diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrExpressionBase.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrExpressionBase.kt index 0320af0e738..5ccb8715a96 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrExpressionBase.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrExpressionBase.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.ir.expressions.impl import org.jetbrains.kotlin.ir.IrElementBase +import org.jetbrains.kotlin.ir.declarations.IrAttributeContainer import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.types.IrType @@ -24,4 +25,6 @@ abstract class IrExpressionBase( startOffset: Int, endOffset: Int, override val type: IrType -) : IrElementBase(startOffset, endOffset), IrExpression \ No newline at end of file +) : IrElementBase(startOffset, endOffset), IrExpression { + override var attributeOwnerId: IrAttributeContainer = this +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt index 11b7c9e61c2..c8f22ffa680 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt @@ -138,6 +138,7 @@ open class DeepCopyIrTreeWithSymbols( } thisReceiver = declaration.thisReceiver?.transform() declaration.transformDeclarationsTo(this) + copyAttributes(declaration) } override fun visitSimpleFunction(declaration: IrSimpleFunction): IrSimpleFunction =