K2: use MutableOrEmptyList for annotations storage in FIR elements

This commit is contained in:
Mikhail Glukhikh
2022-12-14 16:53:40 +01:00
committed by Space Team
parent be5394e211
commit 582b640bec
306 changed files with 1168 additions and 203 deletions
@@ -37,7 +37,11 @@ object FieldSets {
val declarations by lazy { fieldList(declaration.withArgs("E" to "*")) }
val annotations by lazy { fieldList("annotations", annotation).withTransform(needTransformInOtherChildren = true) }
val annotations by lazy {
fieldList(
"annotations", annotation, withReplace = true, useMutableOrEmpty = true
).withTransform(needTransformInOtherChildren = true)
}
fun symbolWithPackage(packageName: String?, symbolClassName: String, argument: String? = null): Field {
return field("symbol", type(packageName, symbolClassName), argument)
@@ -195,7 +195,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
impl(errorLoop) {
default("block", "FirEmptyExpressionBlock()")
default("condition", "FirErrorExpressionImpl(source, mutableListOf(), ConeStubDiagnostic(diagnostic), null, null)")
default("condition", "FirErrorExpressionImpl(source, MutableOrEmptyList(), ConeStubDiagnostic(diagnostic), null, null)")
useTypes(emptyExpressionBlock, coneStubDiagnosticType)
}
@@ -62,8 +62,8 @@ fun field(element: Element, nullable: Boolean = false, withReplace: Boolean = fa
// ----------- Field list -----------
fun fieldList(name: String, type: Importable, withReplace: Boolean = false): Field {
return FieldList(name, type, withReplace)
fun fieldList(name: String, type: Importable, withReplace: Boolean = false, useMutableOrEmpty: Boolean = false): Field {
return FieldList(name, type, withReplace, useMutableOrEmpty)
}
fun fieldList(element: AbstractElement, withReplace: Boolean = false): Field {
@@ -22,6 +22,7 @@ sealed class Field : Importable {
open val defaultValueInImplementation: String? get() = null
abstract var isMutable: Boolean
abstract var isMutableOrEmpty: Boolean
open var isMutableInInterface: Boolean = false
open val withGetter: Boolean get() = false
open val customSetter: String? get() = null
@@ -98,6 +99,7 @@ class FieldWithDefault(val origin: Field) : Field() {
override var defaultValueInImplementation: String? = origin.defaultValueInImplementation
var defaultValueInBuilder: String? = null
override var isMutable: Boolean = origin.isMutable
override var isMutableOrEmpty: Boolean = origin.isMutableOrEmpty
override var isMutableInInterface: Boolean = origin.isMutableInInterface
override var withGetter: Boolean = false
override var customSetter: String? = null
@@ -135,6 +137,7 @@ class SimpleField(
get() = customType?.fullQualifiedName ?: super.fullQualifiedName
override var isMutable: Boolean = withReplace
override var isMutableOrEmpty: Boolean = false
override fun internalCopy(): Field {
return SimpleField(
@@ -182,6 +185,7 @@ class FirField(
override val isFirType: Boolean = true
override var isMutable: Boolean = true
override var isMutableOrEmpty: Boolean = false
override fun internalCopy(): Field {
return FirField(
@@ -200,7 +204,8 @@ class FirField(
class FieldList(
override val name: String,
val baseType: Importable,
override var withReplace: Boolean
override var withReplace: Boolean,
useMutableOrEmpty: Boolean = false
) : Field() {
override var defaultValueInImplementation: String? = null
override val packageName: String? get() = baseType.packageName
@@ -212,12 +217,14 @@ class FieldList(
override var isVolatile: Boolean = false
override var isMutable: Boolean = true
override var isMutableOrEmpty: Boolean = useMutableOrEmpty
override fun internalCopy(): Field {
return FieldList(
name,
baseType,
withReplace
withReplace,
isMutableOrEmpty
)
}
@@ -83,7 +83,9 @@ private fun SmartPrinter.printBuilder(builder: Builder) {
withIndent {
for (field in builder.allFields) {
val name = field.name
println(name, ",")
print(name)
if (field.isMutableOrEmpty) print(".toMutableOrEmpty()")
println(",")
}
}
println(")")
@@ -210,7 +212,7 @@ private fun SmartPrinter.printDeprecationOnUselessFieldIfNeeded(field: Field, bu
private fun SmartPrinter.printFieldListInBuilder(field: FieldList, builder: Builder, fieldIsUseless: Boolean) {
printDeprecationOnUselessFieldIfNeeded(field, builder, fieldIsUseless)
printModifiers(builder, field, fieldIsUseless)
print("val ${field.name}: ${field.mutableType}")
print("val ${field.name}: ${field.getMutableType(forBuilder = true)}")
if (builder is LeafBuilder) {
print(" = mutableListOf()")
}
@@ -22,7 +22,7 @@ fun SmartPrinter.printField(field: Field, isImplementation: Boolean, override: B
} else {
print("var")
}
val type = if (isImplementation) field.mutableType else field.typeWithArguments
val type = if (isImplementation) field.getMutableType() else field.typeWithArguments
println(" ${field.name}: $type$end")
}
@@ -37,7 +37,7 @@ fun SmartPrinter.printFieldWithDefaultInImplementation(field: Field) {
} else {
print("var")
}
print(" ${field.name}: ${field.mutableType} ")
print(" ${field.name}: ${field.getMutableType()} ")
if (field.withGetter) {
if (field.customSetter != null) {
println()
@@ -343,7 +343,7 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
when {
field.withGetter -> {}
field.origin is FieldList -> {
field.origin is FieldList && !field.isMutableOrEmpty -> {
println("${field.name}.clear()")
println("${field.name}.addAll($newValue)")
}
@@ -352,7 +352,11 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
if (field.useNullableForReplace) {
println("require($newValue != null)")
}
println("${field.name} = $newValue")
print("${field.name} = $newValue")
if (field.origin is FieldList && field.isMutableOrEmpty) {
print(".toMutableOrEmpty()")
}
println()
}
}
}
@@ -67,7 +67,19 @@ private fun Element.collectImportsInternal(base: List<String>, kind: ImportKind)
allFields.flatMap { it.overridenTypes.mapNotNull { it.fullQualifiedName } } +
allFields.flatMap { it.arguments.mapNotNull { it.fullQualifiedName } } +
typeArguments.flatMap { it.upperBounds.mapNotNull { it.fullQualifiedName } }
val result = fqns.filterRedundantImports(packageName, kind)
val result = fqns.filterRedundantImports(packageName, kind) +
if (allFields.any { it is FieldList && it.isMutableOrEmpty }) {
when (kind) {
ImportKind.Implementation -> listOf(
"org.jetbrains.kotlin.fir.MutableOrEmptyList",
"org.jetbrains.kotlin.fir.builder.toMutableOrEmpty"
)
ImportKind.Builder -> listOf("org.jetbrains.kotlin.fir.builder.toMutableOrEmpty")
else -> emptyList()
}
} else {
emptyList()
}
if (allFields.any { it.name == "source" && it.withReplace }) {
return (result + "org.jetbrains.kotlin.fir.FirImplementationDetail").distinct()
}
@@ -89,7 +101,8 @@ val KindOwner.needPureAbstractElement: Boolean
get() = (kind != Kind.Interface && kind != Kind.SealedInterface) && !allParents.any { it.kind == Kind.AbstractClass || it.kind == Kind.SealedClass }
val Field.isVal: Boolean get() = this is FieldList || (this is FieldWithDefault && origin is FieldList) || !isMutable
val Field.isVal: Boolean
get() = (this is FieldList && !isMutableOrEmpty) || (this is FieldWithDefault && origin is FieldList && !origin.isMutableOrEmpty) || !isMutable
fun Field.transformFunctionDeclaration(returnType: String): String {
@@ -109,12 +122,15 @@ fun Field.replaceFunctionDeclaration(overridenType: Importable? = null, forceNul
return "fun replace$capName(new$capName: $typeWithNullable)"
}
val Field.mutableType: String
get() = when (this) {
is FieldList -> if (isMutable) "Mutable$typeWithArguments" else typeWithArguments
is FieldWithDefault -> if (isMutable) origin.mutableType else typeWithArguments
fun Field.getMutableType(forBuilder: Boolean = false): String = when (this) {
is FieldList -> when {
isMutableOrEmpty && !forBuilder -> "MutableOrEmpty$typeWithArguments"
isMutable -> "Mutable$typeWithArguments"
else -> typeWithArguments
}
is FieldWithDefault -> if (isMutable) origin.getMutableType() else typeWithArguments
else -> typeWithArguments
}
fun Field.call(): String = if (nullable) "?." else "."