K2: introduce & use (for annotation creation) smartPlus for lists

This commit is contained in:
Mikhail Glukhikh
2022-12-19 17:21:58 +01:00
committed by Space Team
parent f33f87e3d4
commit 761a0f8248
5 changed files with 16 additions and 24 deletions
@@ -2021,12 +2021,7 @@ class DeclarationsConverter(
} }
for (modifierList in allTypeModifiers) { for (modifierList in allTypeModifiers) {
if (modifierList.annotations.isNotEmpty()) { calculatedFirType.replaceAnnotations(calculatedFirType.annotations.smartPlus(modifierList.annotations))
val replacementAnnotations =
if (calculatedFirType.annotations.isNotEmpty()) calculatedFirType.annotations + modifierList.annotations
else modifierList.annotations
calculatedFirType.replaceAnnotations(replacementAnnotations)
}
} }
return calculatedFirType return calculatedFirType
} }
@@ -451,10 +451,7 @@ class ExpressionsConverter(
val result = firExpression ?: buildErrorExpression(null, ConeNotAnnotationContainer("???")) val result = firExpression ?: buildErrorExpression(null, ConeNotAnnotationContainer("???"))
require(result is FirAnnotationContainer) require(result is FirAnnotationContainer)
if (firAnnotationList.isNotEmpty()) { result.replaceAnnotations(result.annotations.smartPlus(firAnnotationList))
val replacementAnnotations = if (result.annotations.isNotEmpty()) result.annotations + firAnnotationList else firAnnotationList
result.replaceAnnotations(replacementAnnotations)
}
return result return result
} }
@@ -499,12 +499,7 @@ open class RawFirBuilder(
if (this != null) { if (this != null) {
it.extractAnnotationsFrom(this) it.extractAnnotationsFrom(this)
} }
val resultAnnotations = when { it.replaceAnnotations(it.annotations.smartPlus(accessorAnnotationsFromProperty))
accessorAnnotationsFromProperty.isEmpty() -> it.annotations
it.annotations.isEmpty() -> accessorAnnotationsFromProperty
else -> it.annotations + accessorAnnotationsFromProperty
}
it.replaceAnnotations(resultAnnotations)
it.status = status it.status = status
it.initContainingClassAttr() it.initContainingClassAttr()
} }
@@ -704,7 +699,7 @@ open class RawFirBuilder(
val annotations = buildList { val annotations = buildList {
addAll(container.annotations) addAll(container.annotations)
for (annotationEntry in annotationEntries) { for (annotationEntry in annotationEntries) {
add(annotationEntry.convert<FirAnnotation>()) add(annotationEntry.convert())
} }
} }
container.replaceAnnotations(annotations) container.replaceAnnotations(annotations)
@@ -958,13 +958,7 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
} }
return if (operation == FirOperation.ASSIGN) { return if (operation == FirOperation.ASSIGN) {
val result = unwrappedLhs.convert() val result = unwrappedLhs.convert()
if (annotations.isNotEmpty()) { result.replaceAnnotations(result.annotations.smartPlus(annotations))
val annotationsToReplace = if (result.annotations.isEmpty()) annotations else mutableListOf<FirAnnotation>().apply {
addAll(result.annotations)
addAll(annotations)
}
result.replaceAnnotations(annotationsToReplace)
}
result.pullUpSafeCallIfNecessary() result.pullUpSafeCallIfNecessary()
} else { } else {
val receiver = unwrappedLhs.convert() val receiver = unwrappedLhs.convert()
@@ -197,3 +197,14 @@ value class MutableOrEmptyList<T>(val list: MutableList<T>?) : List<T> {
fun <T> empty(): MutableOrEmptyList<T> = EMPTY as MutableOrEmptyList<T> fun <T> empty(): MutableOrEmptyList<T> = EMPTY as MutableOrEmptyList<T>
} }
} }
fun <T> List<T>.smartPlus(other: List<T>): List<T> = when {
other.isEmpty() -> this
this.isEmpty() -> other
else -> {
val result = ArrayList<T>(this.size + other.size)
result.addAll(this)
result.addAll(other)
result
}
}