Refactor AnnotationImplementationLowering

- Replaced UNDEFINED_OFFSET with SYNTHETIC_OFFSET, it's required by
  Native backend codegen
- Fixed missing overridden symbols
- Enforce adding fakeoverrides for members not overridden by backend
- Support more points for platform customisation
This commit is contained in:
Pavel Kunyavskiy
2021-09-13 10:32:37 +03:00
committed by Space
parent d9424fa092
commit 773c82ae48
5 changed files with 222 additions and 158 deletions
@@ -8,25 +8,19 @@ package org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.BackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.deepCopyWithVariables
import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.backend.common.ir.addFakeOverrides
import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.types.isKClass
import org.jetbrains.kotlin.ir.types.isArray
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name
@@ -46,7 +40,7 @@ class AnnotationImplementationLowering(
}
}
open class AnnotationImplementationTransformer(val context: BackendContext, val irFile: IrFile?) : IrElementTransformerVoidWithContext() {
abstract class AnnotationImplementationTransformer(val context: BackendContext, val irFile: IrFile?) : IrElementTransformerVoidWithContext() {
internal val implementations: MutableMap<IrClass, IrClass> = mutableMapOf()
override fun visitConstructorCall(expression: IrConstructorCall): IrExpression {
@@ -67,11 +61,15 @@ open class AnnotationImplementationTransformer(val context: BackendContext, val
return newCall
}
open fun IrClass.platformSetup() {}
private fun createAnnotationImplementation(annotationClass: IrClass): IrClass {
val localDeclarationParent = currentClass?.scope?.getLocalDeclarationParent() as? IrClass
val parentFqName = annotationClass.fqNameWhenAvailable!!.asString().replace('.', '_')
val wrapperName = Name.identifier("annotationImpl\$$parentFqName$0")
val subclass = context.irFactory.buildClass {
startOffset = SYNTHETIC_OFFSET
endOffset = SYNTHETIC_OFFSET
name = wrapperName
origin = ANNOTATION_IMPLEMENTATION
// It can be seen from inline functions and multiple classes within one file
@@ -79,90 +77,29 @@ open class AnnotationImplementationTransformer(val context: BackendContext, val
// since declaration is synthetic anyway
visibility = DescriptorVisibilities.INTERNAL
}.apply {
parent = localDeclarationParent ?: irFile ?: error("irFile in transformer should be specified when creating synthetic implementation")
parent = localDeclarationParent ?: irFile
?: error("irFile in transformer should be specified when creating synthetic implementation")
createImplicitParameterDeclarationWithWrappedDescriptor()
superTypes = listOf(annotationClass.defaultType)
platformSetup()
}
val ctor = subclass.addConstructor {
startOffset = SYNTHETIC_OFFSET
endOffset = SYNTHETIC_OFFSET
visibility = DescriptorVisibilities.PUBLIC
}
val (originalProps, implementationProps) = implementAnnotationProperties(subclass, annotationClass, ctor)
implementEqualsAndHashCode(annotationClass, subclass, originalProps, implementationProps)
implementAnnotationPropertiesAndConstructor(subclass, annotationClass, ctor)
implementGeneratedFunctions(annotationClass, subclass)
implementPlatformSpecificParts(annotationClass, subclass)
return subclass
}
fun implementAnnotationProperties(implClass: IrClass, annotationClass: IrClass, generatedConstructor: IrConstructor): Pair<List<IrProperty>, List<IrProperty>> {
val ctorBody = context.irFactory.createBlockBody(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, listOf(
IrDelegatingConstructorCallImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, context.irBuiltIns.unitType, context.irBuiltIns.anyClass.constructors.single(),
typeArgumentsCount = 0, valueArgumentsCount = 0
)
)
)
generatedConstructor.body = ctorBody
val properties = annotationClass.getAnnotationProperties()
return properties to properties.map { property ->
val propType = property.getter!!.returnType
val propName = property.name
val field = context.irFactory.buildField {
name = propName
type = propType
origin = ANNOTATION_IMPLEMENTATION
isFinal = true
visibility = DescriptorVisibilities.PRIVATE
}.also { it.parent = implClass }
val parameter = generatedConstructor.addValueParameter(propName.asString(), propType)
// VALUE_FROM_PARAMETER
val originalParameter = ((property.backingField?.initializer?.expression as? IrGetValue)?.symbol?.owner as? IrValueParameter)
if (originalParameter?.defaultValue != null) {
parameter.defaultValue = originalParameter.defaultValue!!.deepCopyWithVariables().also { it.transformChildrenVoid() }
}
ctorBody.statements += IrSetFieldImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, field.symbol,
IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, implClass.thisReceiver!!.symbol),
IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, parameter.symbol),
context.irBuiltIns.unitType,
)
val prop = implClass.addProperty {
name = propName
isVar = false
origin = ANNOTATION_IMPLEMENTATION
}.apply {
field.correspondingPropertySymbol = this.symbol
backingField = field
parent = implClass
}
prop.addGetter {
name = propName // Annotation value getter should be named 'x', not 'getX'
returnType = propType.kClassToJClassIfNeeded() // On JVM, annotation store j.l.Class even if declared with KClass
origin = ANNOTATION_IMPLEMENTATION
visibility = DescriptorVisibilities.PUBLIC
modality = Modality.FINAL
}.apply {
correspondingPropertySymbol = prop.symbol
dispatchReceiverParameter = implClass.thisReceiver!!.copyTo(this)
body = context.createIrBuilder(symbol).irBlockBody {
var value: IrExpression = irGetField(irGet(dispatchReceiverParameter!!), field)
if (propType.isKClass()) value = this.kClassExprToJClassIfNeeded(value)
+irReturn(value)
}
}
prop
}
}
abstract fun implementAnnotationPropertiesAndConstructor(
implClass: IrClass,
annotationClass: IrClass,
generatedConstructor: IrConstructor
)
fun IrClass.getAnnotationProperties(): List<IrProperty> {
// For some weird reason, annotations defined in other IrFiles, do not have IrProperties in declarations.
@@ -173,31 +110,61 @@ open class AnnotationImplementationTransformer(val context: BackendContext, val
.mapNotNull { it.correspondingPropertySymbol?.owner }
}
open fun IrType.kClassToJClassIfNeeded(): IrType = this
open fun IrBuilderWithScope.kClassExprToJClassIfNeeded(irExpression: IrExpression): IrExpression = irExpression
open fun generatedEquals(irBuilder: IrBlockBodyBuilder, type: IrType, arg1: IrExpression, arg2: IrExpression): IrExpression =
irBuilder.irEquals(arg1, arg2)
abstract fun getArrayContentEqualsSymbol(type: IrType): IrFunctionSymbol
@Suppress("UNUSED_VARIABLE")
fun implementEqualsAndHashCode(annotationClass: IrClass, implClass: IrClass, originalProps: List<IrProperty>, childProps: List<IrProperty>) {
fun generatedEquals(irBuilder: IrBlockBodyBuilder, type: IrType, arg1: IrExpression, arg2: IrExpression): IrExpression =
if (type.isArray() || type.isPrimitiveArray()) {
val requiredSymbol = getArrayContentEqualsSymbol(type)
irBuilder.irCall(
requiredSymbol
).apply {
if (requiredSymbol.owner.extensionReceiverParameter != null) {
extensionReceiver = arg1
putValueArgument(0, arg2)
} else {
putValueArgument(0, arg1)
putValueArgument(1, arg2)
}
}
} else
irBuilder.irEquals(arg1, arg2)
open val forbidDirectFieldAccessInMethods = false
open fun generateFunctionBodies(
annotationClass: IrClass,
implClass: IrClass,
eqFun: IrSimpleFunction,
hcFun: IrSimpleFunction,
toStringFun: IrSimpleFunction,
generator: AnnotationImplementationMemberGenerator
) {
val properties = annotationClass.getAnnotationProperties()
generator.generateEqualsUsingGetters(eqFun, annotationClass.defaultType, properties)
generator.generateHashCodeMethod(hcFun, properties)
generator.generateToStringMethod(toStringFun, properties)
}
fun implementGeneratedFunctions(annotationClass: IrClass, implClass: IrClass) {
val creator = MethodsFromAnyGeneratorForLowerings(context, implClass, ANNOTATION_IMPLEMENTATION)
val eqFun = creator.createEqualsMethodDeclaration()
val hcFun = creator.createHashCodeMethodDeclaration()
val toStringFun = creator.createToStringMethodDeclaration()
if (annotationClass != implClass) {
implClass.addFakeOverrides(context.typeSystem)
}
val generator = AnnotationImplementationMemberGenerator(
context, implClass,
nameForToString = "@" + annotationClass.fqNameWhenAvailable!!.asString(),
forbidDirectFieldAccess = forbidDirectFieldAccessInMethods
) { type, a, b ->
generatedEquals(this, type, a, b)
}
val eqFun = creator.createEqualsMethodDeclaration()
generator.generateEqualsUsingGetters(eqFun, annotationClass.defaultType, originalProps)
val hcFun = creator.createHashCodeMethodDeclaration()
generator.generateHashCodeMethod(hcFun, childProps)
val toStringFun = creator.createToStringMethodDeclaration()
generator.generateToStringMethod(toStringFun, childProps)
generateFunctionBodies(annotationClass, implClass, eqFun, hcFun, toStringFun, generator)
}
open fun implementPlatformSpecificParts(annotationClass: IrClass, implClass: IrClass) {}
@@ -207,8 +174,9 @@ class AnnotationImplementationMemberGenerator(
backendContext: BackendContext,
irClass: IrClass,
val nameForToString: String,
forbidDirectFieldAccess: Boolean,
val selectEquals: IrBlockBodyBuilder.(IrType, IrExpression, IrExpression) -> IrExpression,
) : LoweringDataClassMemberGenerator(backendContext, irClass, ANNOTATION_IMPLEMENTATION) {
) : LoweringDataClassMemberGenerator(backendContext, irClass, ANNOTATION_IMPLEMENTATION, forbidDirectFieldAccess) {
override fun IrClass.classNameForToString(): String = nameForToString
@@ -230,7 +198,7 @@ class AnnotationImplementationMemberGenerator(
// 2. Properties should be retrieved using getters without accessing backing fields
// (DataClassMembersGenerator typically tries to access fields)
fun generateEqualsUsingGetters(equalsFun: IrSimpleFunction, typeForEquals: IrType, properties: List<IrProperty>) = equalsFun.apply {
body = backendContext.createIrBuilder(symbol).irBlockBody {
body = backendContext.createIrBuilder(symbol, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET).irBlockBody {
val irType = typeForEquals
fun irOther() = irGet(valueParameters[0])
fun irThis() = irGet(dispatchReceiverParameter!!)
@@ -22,22 +22,29 @@ import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.types.isArray
import org.jetbrains.kotlin.ir.util.DataClassMembersGenerator
import org.jetbrains.kotlin.ir.util.SYNTHETIC_OFFSET
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.isPrimitiveArray
class MethodsFromAnyGeneratorForLowerings(val context: BackendContext, val irClass: IrClass, val origin: IrDeclarationOrigin) {
fun createToStringMethodDeclaration(): IrSimpleFunction = irClass.addFunction("toString", context.irBuiltIns.stringType).apply {
overriddenSymbols = irClass.collectOverridenSymbols { it.isToString() }
}
private fun IrClass.addSyntheticFunction(name: String, returnType: IrType) =
addFunction(name, returnType, startOffset = SYNTHETIC_OFFSET, endOffset = SYNTHETIC_OFFSET)
fun createHashCodeMethodDeclaration(): IrSimpleFunction = irClass.addFunction("hashCode", context.irBuiltIns.intType).apply {
overriddenSymbols = irClass.collectOverridenSymbols { it.isHashCode() }
}
fun createToStringMethodDeclaration(): IrSimpleFunction =
irClass.addSyntheticFunction("toString", context.irBuiltIns.stringType).apply {
overriddenSymbols = irClass.collectOverridenSymbols { it.isToString() }
}
fun createEqualsMethodDeclaration(): IrSimpleFunction = irClass.addFunction("equals", context.irBuiltIns.booleanType).apply {
overriddenSymbols = irClass.collectOverridenSymbols { it.isEquals(context) }
addValueParameter("other", context.irBuiltIns.anyNType)
}
fun createHashCodeMethodDeclaration(): IrSimpleFunction =
irClass.addSyntheticFunction("hashCode", context.irBuiltIns.intType).apply {
overriddenSymbols = irClass.collectOverridenSymbols { it.isHashCode() }
}
fun createEqualsMethodDeclaration(): IrSimpleFunction =
irClass.addSyntheticFunction("equals", context.irBuiltIns.booleanType).apply {
overriddenSymbols = irClass.collectOverridenSymbols { it.isEquals(context) }
addValueParameter("other", context.irBuiltIns.anyNType)
}
companion object {
fun IrFunction.isToString(): Boolean =
@@ -60,13 +67,15 @@ class MethodsFromAnyGeneratorForLowerings(val context: BackendContext, val irCla
open class LoweringDataClassMemberGenerator(
val backendContext: BackendContext,
irClass: IrClass,
origin: IrDeclarationOrigin
origin: IrDeclarationOrigin,
forbidDirectFieldAccess: Boolean = false
) :
DataClassMembersGenerator(
IrGeneratorContextBase(backendContext.irBuiltIns),
backendContext.ir.symbols.externalSymbolTable,
irClass,
origin
origin,
forbidDirectFieldAccess
) {
override fun declareSimpleFunction(startOffset: Int, endOffset: Int, functionDescriptor: FunctionDescriptor): IrFunction {