FIR2IR: refactor annotation generations.
This commit is contained in:
committed by
Mikhail Glukhikh
parent
931ba4892a
commit
76e679a6ca
+77
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2010-2020 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.fir.backend.generators
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
|
||||
import org.jetbrains.kotlin.fir.FirAnnotationContainer
|
||||
import org.jetbrains.kotlin.fir.backend.Fir2IrVisitor
|
||||
import org.jetbrains.kotlin.fir.declarations.FirProperty
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
|
||||
import org.jetbrains.kotlin.ir.util.isPropertyField
|
||||
import org.jetbrains.kotlin.ir.util.isSetter
|
||||
|
||||
internal class AnnotationGenerator(private val visitor: Fir2IrVisitor) {
|
||||
|
||||
fun generate(irContainer: IrMutableAnnotationContainer, firContainer: FirAnnotationContainer) {
|
||||
irContainer.annotations = firContainer.annotations.mapNotNull {
|
||||
it.accept(visitor, null) as? IrConstructorCall
|
||||
}
|
||||
}
|
||||
|
||||
fun generate(irProperty: IrProperty, property: FirProperty) {
|
||||
irProperty.annotations +=
|
||||
property.annotations
|
||||
.filter {
|
||||
it.useSiteTarget == null || it.useSiteTarget == AnnotationUseSiteTarget.PROPERTY
|
||||
}
|
||||
.mapNotNull {
|
||||
it.accept(visitor, null) as? IrConstructorCall
|
||||
}
|
||||
}
|
||||
|
||||
fun generate(irField: IrField, property: FirProperty) {
|
||||
assert(irField.isPropertyField) {
|
||||
"$irField is not a property field."
|
||||
}
|
||||
irField.annotations +=
|
||||
property.annotations
|
||||
.filter {
|
||||
it.useSiteTarget == AnnotationUseSiteTarget.FIELD ||
|
||||
it.useSiteTarget == AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD
|
||||
}
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
}
|
||||
|
||||
fun generate(propertyAccessor: IrFunction, property: FirProperty) {
|
||||
assert(propertyAccessor.isPropertyAccessor) {
|
||||
"$propertyAccessor is not a property accessor."
|
||||
}
|
||||
if (propertyAccessor.isSetter) {
|
||||
propertyAccessor.annotations +=
|
||||
property.annotations
|
||||
.filter { it.useSiteTarget == AnnotationUseSiteTarget.PROPERTY_SETTER }
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
propertyAccessor.valueParameters.singleOrNull()?.annotations =
|
||||
propertyAccessor.valueParameters.singleOrNull()?.annotations?.plus(
|
||||
property.annotations
|
||||
.filter { it.useSiteTarget == AnnotationUseSiteTarget.SETTER_PARAMETER }
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
)!!
|
||||
} else {
|
||||
propertyAccessor.annotations +=
|
||||
property.annotations
|
||||
.filter { it.useSiteTarget == AnnotationUseSiteTarget.PROPERTY_GETTER }
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
}
|
||||
propertyAccessor.extensionReceiverParameter?.annotations =
|
||||
propertyAccessor.extensionReceiverParameter?.annotations?.plus(
|
||||
property.annotations
|
||||
.filter { it.useSiteTarget == AnnotationUseSiteTarget.RECEIVER }
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
)!!
|
||||
}
|
||||
}
|
||||
+8
-49
@@ -6,7 +6,6 @@
|
||||
package org.jetbrains.kotlin.fir.backend.generators
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
|
||||
import org.jetbrains.kotlin.fir.backend.*
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter
|
||||
@@ -18,7 +17,6 @@ import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirConstructorSymbol
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
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.IrFieldAccessExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.*
|
||||
@@ -34,6 +32,8 @@ internal class ClassMemberGenerator(
|
||||
fakeOverrideMode: FakeOverrideMode
|
||||
) : Fir2IrComponents by components {
|
||||
|
||||
private val annotationGenerator = AnnotationGenerator(visitor)
|
||||
|
||||
private val fakeOverrideGenerator = FakeOverrideGenerator(
|
||||
session, components.scopeSession, classifierStorage, declarationStorage, conversionScope, fakeOverrideMode
|
||||
)
|
||||
@@ -78,9 +78,7 @@ internal class ClassMemberGenerator(
|
||||
it.accept(visitor, null)
|
||||
}
|
||||
}
|
||||
annotations = klass.annotations.mapNotNull {
|
||||
it.accept(visitor, null) as? IrConstructorCall
|
||||
}
|
||||
annotationGenerator.generate(irClass, klass)
|
||||
if (irPrimaryConstructor != null) {
|
||||
declarationStorage.leaveScope(irPrimaryConstructor.descriptor)
|
||||
}
|
||||
@@ -102,13 +100,9 @@ internal class ClassMemberGenerator(
|
||||
}
|
||||
for ((valueParameter, firValueParameter) in valueParameters.zip(firFunction.valueParameters)) {
|
||||
valueParameter.setDefaultValue(firValueParameter)
|
||||
valueParameter.annotations = firValueParameter.annotations.mapNotNull {
|
||||
it.accept(visitor, null) as? IrConstructorCall
|
||||
}
|
||||
}
|
||||
annotations = firFunction.annotations.mapNotNull {
|
||||
it.accept(visitor, null) as? IrConstructorCall
|
||||
annotationGenerator.generate(valueParameter, firValueParameter)
|
||||
}
|
||||
annotationGenerator.generate(irFunction, firFunction)
|
||||
}
|
||||
if (firFunction is FirConstructor && irFunction is IrConstructor && !parentAsClass.isAnnotationClass) {
|
||||
val body = IrBlockBodyImpl(startOffset, endOffset)
|
||||
@@ -174,14 +168,7 @@ internal class ClassMemberGenerator(
|
||||
property, property.setter, irProperty, propertyType, property.setter is FirDefaultPropertySetter
|
||||
)
|
||||
}
|
||||
irProperty.annotations +=
|
||||
property.annotations
|
||||
.filter {
|
||||
it.useSiteTarget == null || it.useSiteTarget == AnnotationUseSiteTarget.PROPERTY
|
||||
}
|
||||
.mapNotNull {
|
||||
it.accept(visitor, null) as? IrConstructorCall
|
||||
}
|
||||
annotationGenerator.generate(irProperty, property)
|
||||
return irProperty
|
||||
}
|
||||
|
||||
@@ -199,13 +186,7 @@ internal class ClassMemberGenerator(
|
||||
}
|
||||
declarationStorage.leaveScope(descriptor)
|
||||
}
|
||||
irField.annotations +=
|
||||
property.annotations
|
||||
.filter {
|
||||
it.useSiteTarget == AnnotationUseSiteTarget.FIELD ||
|
||||
it.useSiteTarget == AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD
|
||||
}
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
annotationGenerator.generate(irField, property)
|
||||
}
|
||||
|
||||
private fun IrFunction.setPropertyAccessorContent(
|
||||
@@ -246,29 +227,7 @@ internal class ClassMemberGenerator(
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isSetter) {
|
||||
annotations +=
|
||||
property.annotations
|
||||
.filter { it.useSiteTarget == AnnotationUseSiteTarget.PROPERTY_SETTER }
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
valueParameters.singleOrNull()?.annotations =
|
||||
valueParameters.singleOrNull()?.annotations?.plus(
|
||||
property.annotations
|
||||
.filter { it.useSiteTarget == AnnotationUseSiteTarget.SETTER_PARAMETER }
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
)!!
|
||||
} else {
|
||||
annotations +=
|
||||
property.annotations
|
||||
.filter { it.useSiteTarget == AnnotationUseSiteTarget.PROPERTY_GETTER }
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
}
|
||||
extensionReceiverParameter?.annotations =
|
||||
extensionReceiverParameter?.annotations?.plus(
|
||||
property.annotations
|
||||
.filter { it.useSiteTarget == AnnotationUseSiteTarget.RECEIVER }
|
||||
.mapNotNull { it.accept(visitor, null) as? IrConstructorCall }
|
||||
)!!
|
||||
annotationGenerator.generate(this, property)
|
||||
}
|
||||
|
||||
private fun IrFieldAccessExpression.setReceiver(declaration: IrDeclaration): IrFieldAccessExpression {
|
||||
|
||||
Reference in New Issue
Block a user