[FIR] move file annotations to the FirFileAnnotationsContainer
This commit is contained in:
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.*
|
||||
import org.jetbrains.kotlin.builtins.StandardNames.DATA_CLASS_COMPONENT_PREFIX
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.fir.*
|
||||
import org.jetbrains.kotlin.fir.builder.buildFileAnnotationsContainer
|
||||
import org.jetbrains.kotlin.fir.builder.buildPackageDirective
|
||||
import org.jetbrains.kotlin.fir.caches.getValue
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
@@ -691,6 +692,11 @@ fun FirSession.createFilesWithGeneratedDeclarations(): List<FirFile> {
|
||||
return buildList {
|
||||
for (packageFqName in (topLevelClasses.keys + topLevelCallables.keys)) {
|
||||
this += buildFile {
|
||||
symbol = FirFileSymbol()
|
||||
annotationsContainer = buildFileAnnotationsContainer {
|
||||
moduleData = this@createFilesWithGeneratedDeclarations.moduleData
|
||||
containingFileSymbol = this@buildFile.symbol
|
||||
}
|
||||
origin = FirDeclarationOrigin.Synthetic
|
||||
moduleData = this@createFilesWithGeneratedDeclarations.moduleData
|
||||
packageDirective = buildPackageDirective {
|
||||
|
||||
+22
-8
@@ -93,7 +93,8 @@ class DeclarationsConverter(
|
||||
throw Exception()
|
||||
}
|
||||
|
||||
val fileAnnotationList = mutableListOf<FirAnnotation>()
|
||||
val fileSymbol = FirFileSymbol()
|
||||
var fileAnnotationContainer: FirFileAnnotationsContainer? = null
|
||||
val importList = mutableListOf<FirImport>()
|
||||
val firDeclarationList = mutableListOf<FirDeclaration>()
|
||||
context.packageFqName = FqName.ROOT
|
||||
@@ -101,7 +102,7 @@ class DeclarationsConverter(
|
||||
file.forEachChildren { child ->
|
||||
@Suppress("RemoveRedundantQualifierName")
|
||||
when (child.tokenType) {
|
||||
FILE_ANNOTATION_LIST -> fileAnnotationList += convertFileAnnotationList(child)
|
||||
FILE_ANNOTATION_LIST -> fileAnnotationContainer = convertFileAnnotationsContainer(child, fileSymbol)
|
||||
PACKAGE_DIRECTIVE -> {
|
||||
packageDirective = convertPackageDirective(child).also { context.packageFqName = it.packageFqName }
|
||||
}
|
||||
@@ -119,6 +120,7 @@ class DeclarationsConverter(
|
||||
}
|
||||
|
||||
return buildFile {
|
||||
symbol = fileSymbol
|
||||
source = file.toFirSourceElement()
|
||||
origin = FirDeclarationOrigin.Source
|
||||
moduleData = baseModuleData
|
||||
@@ -126,7 +128,11 @@ class DeclarationsConverter(
|
||||
this.sourceFile = sourceFile
|
||||
this.sourceFileLinesMapping = linesMapping
|
||||
this.packageDirective = packageDirective ?: buildPackageDirective { packageFqName = context.packageFqName }
|
||||
annotations += fileAnnotationList
|
||||
annotationsContainer = fileAnnotationContainer
|
||||
?: buildFileAnnotationsContainer {
|
||||
moduleData = baseModuleData
|
||||
containingFileSymbol = fileSymbol
|
||||
}
|
||||
imports += importList
|
||||
declarations += firDeclarationList
|
||||
}
|
||||
@@ -317,11 +323,19 @@ class DeclarationsConverter(
|
||||
/**
|
||||
* [org.jetbrains.kotlin.parsing.KotlinParsing.parseFileAnnotationList]
|
||||
*/
|
||||
private fun convertFileAnnotationList(fileAnnotationList: LighterASTNode): List<FirAnnotation> {
|
||||
return fileAnnotationList.forEachChildrenReturnList { node, container ->
|
||||
when (node.tokenType) {
|
||||
ANNOTATION -> container += convertAnnotation(node)
|
||||
ANNOTATION_ENTRY -> container += convertAnnotationEntry(node)
|
||||
private fun convertFileAnnotationsContainer(
|
||||
fileAnnotationList: LighterASTNode,
|
||||
fileSymbol: FirFileSymbol
|
||||
): FirFileAnnotationsContainer {
|
||||
return buildFileAnnotationsContainer {
|
||||
moduleData = baseModuleData
|
||||
source = fileAnnotationList.toFirSourceElement()
|
||||
containingFileSymbol = fileSymbol
|
||||
annotations += fileAnnotationList.forEachChildrenReturnList { node, container ->
|
||||
when (node.tokenType) {
|
||||
ANNOTATION -> container += convertAnnotation(node)
|
||||
ANNOTATION_ENTRY -> container += convertAnnotationEntry(node)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1010,6 +1010,7 @@ open class RawFirBuilder(
|
||||
BodyBuildingMode.LAZY_BODIES -> file.packageFqName
|
||||
}
|
||||
return buildFile {
|
||||
symbol = FirFileSymbol()
|
||||
source = file.toFirSourceElement()
|
||||
moduleData = baseModuleData
|
||||
origin = FirDeclarationOrigin.Source
|
||||
@@ -1020,9 +1021,15 @@ open class RawFirBuilder(
|
||||
packageFqName = context.packageFqName
|
||||
source = file.packageDirective?.toKtPsiSourceElement()
|
||||
}
|
||||
for (annotationEntry in file.annotationEntries) {
|
||||
annotations += annotationEntry.convert<FirAnnotation>()
|
||||
annotationsContainer = buildFileAnnotationsContainer {
|
||||
moduleData = baseModuleData
|
||||
containingFileSymbol = this@buildFile.symbol
|
||||
source = file.fileAnnotationList?.toKtPsiSourceElement()
|
||||
for (annotationEntry in file.annotationEntries) {
|
||||
annotations += annotationEntry.convert<FirAnnotation>()
|
||||
}
|
||||
}
|
||||
|
||||
for (importDirective in file.importDirectives) {
|
||||
imports += buildImport {
|
||||
source = importDirective.toFirSourceElement()
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.KtSourceElement
|
||||
import org.jetbrains.kotlin.KtSourceFile
|
||||
import org.jetbrains.kotlin.KtSourceFileLinesMapping
|
||||
import org.jetbrains.kotlin.fir.FirElement
|
||||
import org.jetbrains.kotlin.fir.FirFileAnnotationsContainer
|
||||
import org.jetbrains.kotlin.fir.FirModuleData
|
||||
import org.jetbrains.kotlin.fir.FirPackageDirective
|
||||
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
|
||||
@@ -27,6 +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 packageDirective: FirPackageDirective
|
||||
abstract val imports: List<FirImport>
|
||||
abstract val declarations: List<FirDeclaration>
|
||||
@@ -45,6 +47,8 @@ abstract class FirFile : FirDeclaration() {
|
||||
|
||||
abstract override fun <D> transformAnnotations(transformer: FirTransformer<D>, data: D): FirFile
|
||||
|
||||
abstract fun <D> transformAnnotationsContainer(transformer: FirTransformer<D>, data: D): FirFile
|
||||
|
||||
abstract fun <D> transformImports(transformer: FirTransformer<D>, data: D): FirFile
|
||||
|
||||
abstract fun <D> transformDeclarations(transformer: FirTransformer<D>, data: D): FirFile
|
||||
|
||||
+8
-2
@@ -11,6 +11,7 @@ import kotlin.contracts.*
|
||||
import org.jetbrains.kotlin.KtSourceElement
|
||||
import org.jetbrains.kotlin.KtSourceFile
|
||||
import org.jetbrains.kotlin.KtSourceFileLinesMapping
|
||||
import org.jetbrains.kotlin.fir.FirFileAnnotationsContainer
|
||||
import org.jetbrains.kotlin.fir.FirModuleData
|
||||
import org.jetbrains.kotlin.fir.FirPackageDirective
|
||||
import org.jetbrains.kotlin.fir.builder.FirAnnotationContainerBuilder
|
||||
@@ -35,34 +36,39 @@ import org.jetbrains.kotlin.fir.visitors.*
|
||||
class FirFileBuilder : FirAnnotationContainerBuilder {
|
||||
override var source: KtSourceElement? = null
|
||||
var resolvePhase: FirResolvePhase = FirResolvePhase.RAW_FIR
|
||||
override val annotations: MutableList<FirAnnotation> = mutableListOf()
|
||||
lateinit var moduleData: FirModuleData
|
||||
lateinit var origin: FirDeclarationOrigin
|
||||
var attributes: FirDeclarationAttributes = FirDeclarationAttributes()
|
||||
lateinit var annotationsContainer: FirFileAnnotationsContainer
|
||||
lateinit var packageDirective: FirPackageDirective
|
||||
val imports: MutableList<FirImport> = mutableListOf()
|
||||
val declarations: MutableList<FirDeclaration> = mutableListOf()
|
||||
lateinit var name: String
|
||||
var sourceFile: KtSourceFile? = null
|
||||
var sourceFileLinesMapping: KtSourceFileLinesMapping? = null
|
||||
lateinit var symbol: FirFileSymbol
|
||||
|
||||
override fun build(): FirFile {
|
||||
return FirFileImpl(
|
||||
source,
|
||||
resolvePhase,
|
||||
annotations,
|
||||
moduleData,
|
||||
origin,
|
||||
attributes,
|
||||
annotationsContainer,
|
||||
packageDirective,
|
||||
imports,
|
||||
declarations,
|
||||
name,
|
||||
sourceFile,
|
||||
sourceFileLinesMapping,
|
||||
symbol,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@Deprecated("Modification of 'annotations' has no impact for FirFileBuilder", level = DeprecationLevel.HIDDEN)
|
||||
override val annotations: MutableList<FirAnnotation> = mutableListOf()
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
|
||||
@@ -10,6 +10,7 @@ package org.jetbrains.kotlin.fir.declarations.impl
|
||||
import org.jetbrains.kotlin.KtSourceElement
|
||||
import org.jetbrains.kotlin.KtSourceFile
|
||||
import org.jetbrains.kotlin.KtSourceFileLinesMapping
|
||||
import org.jetbrains.kotlin.fir.FirFileAnnotationsContainer
|
||||
import org.jetbrains.kotlin.fir.FirModuleData
|
||||
import org.jetbrains.kotlin.fir.FirPackageDirective
|
||||
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
|
||||
@@ -31,32 +32,33 @@ internal class FirFileImpl(
|
||||
override val source: KtSourceElement?,
|
||||
@Volatile
|
||||
override var resolvePhase: FirResolvePhase,
|
||||
override val annotations: MutableList<FirAnnotation>,
|
||||
override val moduleData: FirModuleData,
|
||||
override val origin: FirDeclarationOrigin,
|
||||
override val attributes: FirDeclarationAttributes,
|
||||
override var annotationsContainer: FirFileAnnotationsContainer,
|
||||
override var packageDirective: FirPackageDirective,
|
||||
override val imports: MutableList<FirImport>,
|
||||
override val declarations: MutableList<FirDeclaration>,
|
||||
override val name: String,
|
||||
override val sourceFile: KtSourceFile?,
|
||||
override val sourceFileLinesMapping: KtSourceFileLinesMapping?,
|
||||
override val symbol: FirFileSymbol,
|
||||
) : FirFile() {
|
||||
override val symbol: FirFileSymbol = FirFileSymbol()
|
||||
override val annotations: List<FirAnnotation> get() = annotationsContainer.annotations
|
||||
|
||||
init {
|
||||
symbol.bind(this)
|
||||
}
|
||||
|
||||
override fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D) {
|
||||
annotations.forEach { it.accept(visitor, data) }
|
||||
annotationsContainer.accept(visitor, data)
|
||||
packageDirective.accept(visitor, data)
|
||||
imports.forEach { it.accept(visitor, data) }
|
||||
declarations.forEach { it.accept(visitor, data) }
|
||||
}
|
||||
|
||||
override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirFileImpl {
|
||||
transformAnnotations(transformer, data)
|
||||
transformAnnotationsContainer(transformer, data)
|
||||
packageDirective = packageDirective.transform(transformer, data)
|
||||
transformImports(transformer, data)
|
||||
transformDeclarations(transformer, data)
|
||||
@@ -64,7 +66,11 @@ internal class FirFileImpl(
|
||||
}
|
||||
|
||||
override fun <D> transformAnnotations(transformer: FirTransformer<D>, data: D): FirFileImpl {
|
||||
annotations.transformInplace(transformer, data)
|
||||
return this
|
||||
}
|
||||
|
||||
override fun <D> transformAnnotationsContainer(transformer: FirTransformer<D>, data: D): FirFileImpl {
|
||||
annotationsContainer = annotationsContainer.transform(transformer, data)
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -552,7 +552,10 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
|
||||
noImpl(userTypeRef)
|
||||
|
||||
impl(file) {
|
||||
default("symbol", "FirFileSymbol()")
|
||||
default("annotations") {
|
||||
value = "annotationsContainer.annotations"
|
||||
withGetter = true
|
||||
}
|
||||
}
|
||||
|
||||
noImpl(argumentList)
|
||||
|
||||
+1
@@ -452,6 +452,7 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
|
||||
}
|
||||
|
||||
file.configure {
|
||||
+field("annotationsContainer", fileAnnotationsContainer).withTransform()
|
||||
+field("packageDirective", packageDirective)
|
||||
+fieldList(import).withTransform()
|
||||
+declarations.withTransform()
|
||||
|
||||
Reference in New Issue
Block a user