JVM IR: Generate synthetic methods for type aliases with annotations

This commit is contained in:
Kristoffer Andersen
2019-09-23 14:59:05 +02:00
committed by max-kammerer
parent acd1cc5a57
commit 52dc469657
8 changed files with 62 additions and 5 deletions
@@ -157,7 +157,7 @@ inline fun buildFun(builder: IrFunctionBuilder.() -> Unit): IrFunctionImpl =
buildFun()
}
inline fun IrDeclarationContainer.addFunction(builder: IrFunctionBuilder.() -> Unit): IrSimpleFunction =
inline fun IrDeclarationContainer.addFunction(builder: IrFunctionBuilder.() -> Unit): IrFunctionImpl =
buildFun(builder).also { function ->
declarations.add(function)
function.parent = this@addFunction
@@ -35,6 +35,8 @@ interface JvmLoweredDeclarationOrigin : IrDeclarationOrigin {
object JVM_OVERLOADS_WRAPPER : IrDeclarationOriginImpl("JVM_OVERLOADS_WRAPPER")
object SYNTHETIC_METHOD_FOR_PROPERTY_ANNOTATIONS :
IrDeclarationOriginImpl("SYNTHETIC_METHOD_FOR_PROPERTY_ANNOTATIONS", isSynthetic = true)
object SYNTHETIC_METHOD_FOR_TYPEALIAS_ANNOTATIONS :
IrDeclarationOriginImpl("SYNTHETIC_METHOD_FOR_TYPEALIAS_ANNOTATIONS", isSynthetic = true)
object GENERATED_PROPERTY_REFERENCE : IrDeclarationOriginImpl("GENERATED_PROPERTY_REFERENCE", isSynthetic = true)
object GENERATED_SAM_IMPLEMENTATION : IrDeclarationOriginImpl("GENERATED_SAM_IMPLEMENTATION", isSynthetic = true)
object ENUM_MAPPINGS_FOR_WHEN : IrDeclarationOriginImpl("ENUM_MAPPINGS_FOR_WHEN", isSynthetic = true)
@@ -148,6 +148,7 @@ private val returnableBlocksPhase = makeIrFilePhase(
@Suppress("Reformat")
private val jvmFilePhases =
typeAliasAnnotationMethodsPhase then
stripTypeAliasDeclarationsPhase then
provisionalFunctionExpressionPhase then
inventNamesForLocalClassesPhase then
@@ -0,0 +1,58 @@
/*
* 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.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.ClassLoweringPass
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationContainer
import org.jetbrains.kotlin.ir.declarations.IrTypeAlias
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.name.Name
internal val typeAliasAnnotationMethodsPhase = makeIrFilePhase(
::TypeAliasAnnotationMethodsLowering,
name = "TypeAliasAnnotationMethodsLowering",
description = "Generate method stubs for type alias annotations"
)
class TypeAliasAnnotationMethodsLowering(val context: CommonBackendContext) :
ClassLoweringPass {
override fun lower(irClass: IrClass) {
irClass.visitTypeAliases()
}
private val IrTypeAlias.syntheticAnnotationMethodName
get() = Name.identifier(JvmAbi.getSyntheticMethodNameForAnnotatedTypeAlias(name))
private fun IrDeclarationContainer.visitTypeAliases() {
val annotatedAliases = declarations
.filterIsInstance<IrTypeAlias>()
.filter { !it.descriptor.annotations.isEmpty() }
for (alias in annotatedAliases) {
addFunction {
name = alias.syntheticAnnotationMethodName
visibility = alias.visibility
returnType = context.irBuiltIns.unitType
modality = Modality.OPEN
origin = JvmLoweredDeclarationOrigin.SYNTHETIC_METHOD_FOR_TYPEALIAS_ANNOTATIONS
}.apply {
body = IrBlockBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
annotations.addAll(alias.annotations)
metadata = alias.metadata
}
}
}
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// FILE: A.kt
package a
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
@Target(AnnotationTarget.TYPEALIAS)
annotation class Anno
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
@Target(AnnotationTarget.TYPEALIAS)
annotation class Anno