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
This commit is contained in:
Alexander Udalov
2019-06-18 16:28:40 +02:00
parent d399144b80
commit 529efff8cb
10 changed files with 40 additions and 7 deletions
@@ -330,6 +330,7 @@ class LocalDeclarationsLowering(
).also {
it.fillArguments2(expression, newCallee)
it.copyTypeArgumentsFrom(expression)
it.copyAttributes(expression)
}
}
@@ -140,6 +140,7 @@ private class InterfaceLowering(val context: JvmBackendContext) : IrElementTrans
origin
).apply {
copyTypeAndValueArgumentsFrom(expression, receiversAsArguments = true)
copyAttributes(expression)
}
}
} else {
@@ -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 {
@@ -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 : IrAttributeContainer> D.copyAttributes(other: IrAttributeContainer?): D = apply {
if (other != null) {
attributeOwnerId = other.attributeOwnerId
}
}
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.ir.types.IrType
interface IrClass :
IrSymbolDeclaration<IrClassSymbol>, IrDeclarationWithName, IrDeclarationWithVisibility,
IrDeclarationContainer, IrTypeParametersContainer {
IrDeclarationContainer, IrTypeParametersContainer, IrAttributeContainer {
override val descriptor: ClassDescriptor
@@ -61,4 +61,3 @@ fun IrClass.getInstanceInitializerMembers() =
else -> false
}
}
@@ -83,6 +83,8 @@ class IrClassImpl(
override var metadata: MetadataSource? = null
override var attributeOwnerId: IrAttributeContainer = this
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitClass(this, data)
@@ -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 <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitClass(this, data)
@@ -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 <D> transform(transformer: IrElementTransformer<D>, data: D): IrExpression =
@@ -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
) : IrElementBase(startOffset, endOffset), IrExpression {
override var attributeOwnerId: IrAttributeContainer = this
}
@@ -138,6 +138,7 @@ open class DeepCopyIrTreeWithSymbols(
}
thisReceiver = declaration.thisReceiver?.transform()
declaration.transformDeclarationsTo(this)
copyAttributes(declaration)
}
override fun visitSimpleFunction(declaration: IrSimpleFunction): IrSimpleFunction =