[FIR] Make FirFile.annotationsContainer nullable

Previously, when no file annotations were present, the FIR element
didn't have a source.
By making it nullable, it will only be created when appropriate and the
source will never be null.

#KT-55835
This commit is contained in:
Kirill Rakhman
2023-06-26 15:06:13 +03:00
committed by Space Team
parent 311975f1ed
commit bed6cb7154
334 changed files with 47 additions and 1937 deletions
@@ -28,7 +28,7 @@ abstract class FirFile : FirDeclaration() {
abstract override val moduleData: FirModuleData
abstract override val origin: FirDeclarationOrigin
abstract override val attributes: FirDeclarationAttributes
abstract val annotationsContainer: FirFileAnnotationsContainer
abstract val annotationsContainer: FirFileAnnotationsContainer?
abstract val packageDirective: FirPackageDirective
abstract val imports: List<FirImport>
abstract val declarations: List<FirDeclaration>
@@ -43,7 +43,7 @@ class FirFileBuilder : FirAnnotationContainerBuilder {
lateinit var moduleData: FirModuleData
lateinit var origin: FirDeclarationOrigin
var attributes: FirDeclarationAttributes = FirDeclarationAttributes()
lateinit var annotationsContainer: FirFileAnnotationsContainer
var annotationsContainer: FirFileAnnotationsContainer? = null
lateinit var packageDirective: FirPackageDirective
val imports: MutableList<FirImport> = mutableListOf()
val declarations: MutableList<FirDeclaration> = mutableListOf()
@@ -39,7 +39,7 @@ internal class FirFileImpl(
override val moduleData: FirModuleData,
override val origin: FirDeclarationOrigin,
override val attributes: FirDeclarationAttributes,
override var annotationsContainer: FirFileAnnotationsContainer,
override var annotationsContainer: FirFileAnnotationsContainer?,
override var packageDirective: FirPackageDirective,
override val imports: MutableList<FirImport>,
override val declarations: MutableList<FirDeclaration>,
@@ -48,7 +48,7 @@ internal class FirFileImpl(
override val sourceFileLinesMapping: KtSourceFileLinesMapping?,
override val symbol: FirFileSymbol,
) : FirFile() {
override val annotations: List<FirAnnotation> get() = annotationsContainer.annotations
override val annotations: List<FirAnnotation> get() = annotationsContainer?.annotations ?: emptyList()
init {
symbol.bind(this)
@@ -57,7 +57,7 @@ internal class FirFileImpl(
}
override fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D) {
annotationsContainer.accept(visitor, data)
annotationsContainer?.accept(visitor, data)
packageDirective.accept(visitor, data)
imports.forEach { it.accept(visitor, data) }
declarations.forEach { it.accept(visitor, data) }
@@ -76,7 +76,7 @@ internal class FirFileImpl(
}
override fun <D> transformAnnotationsContainer(transformer: FirTransformer<D>, data: D): FirFileImpl {
annotationsContainer = annotationsContainer.transform(transformer, data)
annotationsContainer = annotationsContainer?.transform(transformer, data)
return this
}
@@ -172,7 +172,7 @@ class FirRenderer(
printer.println(file.name)
printer.pushIndent()
visitFileAnnotationsContainer(file.annotationsContainer)
file.annotationsContainer?.let { visitFileAnnotationsContainer(it) }
visitPackageDirective(file.packageDirective)
file.imports.forEach { it.accept(this) }
file.declarations.forEach { it.accept(this) }
@@ -41,6 +41,10 @@ object BuilderConfigurator : AbstractBuilderConfigurator<FirTreeBuilder>(FirTree
fields from klass without listOf("symbol", "resolvePhase", "resolveState", "controlFlowGraphReference")
}
builder(file) {
defaultNull("annotationsContainer")
}
builder(regularClass) {
parents += classBuilder
parents += typeParameterRefsOwnerBuilder
@@ -609,7 +609,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
impl(file) {
default("annotations") {
value = "annotationsContainer.annotations"
value = "annotationsContainer?.annotations ?: emptyList()"
withGetter = true
}
}
@@ -477,7 +477,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
}
file.configure {
+field("annotationsContainer", fileAnnotationsContainer).withTransform()
+field("annotationsContainer", fileAnnotationsContainer, nullable = true).withTransform()
+field("packageDirective", packageDirective)
+fieldList(import).withTransform()
+declarations.withTransform()