[FIR] Make FirRegularClass.companionObject companionObjectSymbol field

This is needed for two reasons:
1. Before this change companion object appeared in FirRegularClass
  twice: in declarations list and in companionObject field. This may
  trigger twice transform of it
2. It's very hard to implement generation of companion object by plugins
  because if it is part of the tree then generated declaration must be
  registered in FirProvider, which is inconsistent with other generated
  declarations. Replacing FIR with symbol and removing custom logic of
  visiting/transforming companion FIR allows us to just replace companionSymbol
  in FirClass if plugin wants to generate it without any additional work
This commit is contained in:
Dmitriy Novozhilov
2021-10-18 18:37:14 +03:00
committed by teamcityserver
parent 01deac5e5c
commit 75b6f7ca00
29 changed files with 50 additions and 67 deletions
@@ -40,7 +40,7 @@ object BuilderConfigurator : AbstractBuilderConfigurator<FirTreeBuilder>(FirTree
builder(regularClass) {
parents += classBuilder
parents += typeParameterRefsOwnerBuilder
defaultNull("companionObject")
defaultNull("companionObjectSymbol")
openBuilder()
withCopy()
}
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir.tree.generator
import org.jetbrains.kotlin.fir.tree.generator.context.AbstractFirTreeImplementationConfigurator
import org.jetbrains.kotlin.fir.tree.generator.model.Implementation.Kind.Object
import org.jetbrains.kotlin.fir.tree.generator.model.Implementation.Kind.OpenClass
import org.jetbrains.kotlin.fir.tree.generator.model.Type
object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator() {
fun configureImplementations() {
@@ -496,7 +497,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
//
// If this `FirResolvedQualifier` is a receiver expression of some other qualified access, the value is updated in
// `FirCallResolver` according to the resolution result.
default("resolvedToCompanionObject", "(symbol?.fir as? FirRegularClass)?.companionObject != null")
default("resolvedToCompanionObject", "(symbol?.fir as? FirRegularClass)?.companionObjectSymbol != null")
useTypes(regularClass)
}
@@ -251,8 +251,8 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
regularClass.configure {
+name
+symbol("FirRegularClassSymbol")
+field("companionObject", regularClass, nullable = true).withTransform()
+booleanField("hasLazyNestedClassifiers")
+field("companionObjectSymbol", regularClassSymbolType, nullable = true, withReplace = true)
+superTypeRefs(withReplace = true)
}
@@ -70,6 +70,7 @@ val backingFieldSymbolType = type("fir.symbols.impl", "FirBackingFieldSymbol")
val delegateFieldSymbolType = type("fir.symbols.impl", "FirDelegateFieldSymbol")
val classSymbolType = type("fir.symbols.impl", "FirClassSymbol")
val classLikeSymbolType = type("fir.symbols.impl", "FirClassLikeSymbol<*>")
val regularClassSymbolType = type("fir.symbols.impl", "FirRegularClassSymbol")
val typeParameterSymbolType = type("fir.symbols.impl", "FirTypeParameterSymbol")
val emptyArgumentListType = type("fir.expressions", "FirEmptyArgumentList")
val firScopeProviderType = type("fir.scopes", "FirScopeProvider")
@@ -105,7 +105,7 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
element.allFields.filter {
it.withBindThis && it.type.contains("Symbol") && it !is FieldList
it.withBindThis && it.type.contains("Symbol") && it !is FieldList && it.name != "companionObjectSymbol"
}.takeIf {
it.isNotEmpty() && !isInterface && !isAbstract &&
!element.type.contains("Reference")
@@ -225,10 +225,6 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
field.name in setOf("dispatchReceiver", "extensionReceiver") -> {}
field.name == "companionObject" -> {
println("companionObject = declarations.asSequence().filterIsInstance<FirRegularClass>().firstOrNull { it.status.isCompanion }")
}
field.needsSeparateTransform -> {
if (!(element.needTransformOtherChildren && field.needTransformInOtherChildren)) {
println("transform${field.name.replaceFirstChar(Char::uppercaseChar)}(transformer, data)")