[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
@@ -77,7 +77,7 @@ internal object FirReferenceResolveHelper {
val callee = calleeReference.resolvedSymbol.fir as? FirCallableDeclaration
// TODO: check callee owner directly?
if (callee !is FirConstructor && callee?.isStatic != true) {
classLikeDeclaration.companionObject?.let { return it.buildSymbol(symbolBuilder) }
classLikeDeclaration.companionObjectSymbol?.let { return it.fir.buildSymbol(symbolBuilder) }
}
}
}
@@ -398,7 +398,7 @@ internal object FirReferenceResolveHelper {
symbolBuilder: KtSymbolByFirBuilder
): Collection<KtSymbol> {
val referencedSymbol = if (fir.resolvedToCompanionObject) {
(fir.symbol?.fir as? FirRegularClass)?.companionObject?.symbol
(fir.symbol?.fir as? FirRegularClass)?.companionObjectSymbol
} else {
fir.symbol
}
@@ -73,7 +73,7 @@ internal class KtFirNamedClassOrObjectSymbol(
override val isExternal: Boolean get() = firRef.withFir { it.isExternal }
override val companionObject: KtFirNamedClassOrObjectSymbol? by firRef.withFirAndCache { fir ->
fir.companionObject?.let { builder.classifierBuilder.buildNamedClassOrObjectSymbol(it) }
fir.companionObjectSymbol?.let { builder.classifierBuilder.buildNamedClassOrObjectSymbol(it.fir) }
}
override val superTypes: List<KtType> by cached {
@@ -18,14 +18,13 @@ import org.jetbrains.kotlin.fir.declarations.primaryConstructorIfAny
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid
import org.jetbrains.kotlin.fir.visitors.acceptAllElements
import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
object FirEnumCompanionInEnumConstructorCallChecker : FirEnumEntryChecker() {
override fun check(declaration: FirEnumEntry, context: CheckerContext, reporter: DiagnosticReporter) {
val enumClass = context.containingDeclarations.lastIsInstanceOrNull<FirRegularClass>() ?: return
if (enumClass.classKind != ClassKind.ENUM_CLASS) return
val companionOfEnumSymbol = enumClass.companionObject?.symbol ?: return
val companionOfEnumSymbol = enumClass.companionObjectSymbol ?: return
val initializerObject = (declaration.initializer as? FirAnonymousObjectExpression)?.anonymousObject ?: return
val delegatingConstructorCall = initializerObject.primaryConstructorIfAny(context.session)?.resolvedDelegatedConstructorCall ?: return
val visitor = Visitor(context, reporter, companionOfEnumSymbol)
@@ -15,9 +15,7 @@ import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.builder.*
import org.jetbrains.kotlin.fir.declarations.comparators.FirMemberDeclarationComparator
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl
import org.jetbrains.kotlin.fir.declarations.utils.addDeclarations
import org.jetbrains.kotlin.fir.declarations.utils.moduleName
import org.jetbrains.kotlin.fir.declarations.utils.sourceElement
import org.jetbrains.kotlin.fir.declarations.utils.*
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
import org.jetbrains.kotlin.fir.scopes.FirScopeProvider
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag
@@ -199,6 +197,7 @@ fun deserializeClassToSymbol(
return 0
}
})
companionObjectSymbol = (declarations.firstOrNull { it is FirRegularClass && it.isCompanion } as FirRegularClass?)?.symbol
}.also {
if (isSealed) {
val inheritors = classProto.sealedSubclassFqNameList.map { nameIndex ->
@@ -174,7 +174,7 @@ class FirElementSerializer private constructor(
}
}
val companionObject = regularClass?.companionObject
val companionObject = regularClass?.companionObjectSymbol?.fir
if (companionObject != null) {
builder.companionObjectName = getSimpleNameIndex(companionObject.name)
}
@@ -61,9 +61,6 @@ class FirJavaClass @FirImplementationDetail internal constructor(
override val attributes: FirDeclarationAttributes = FirDeclarationAttributes()
override val companionObject: FirRegularClass?
get() = null
override fun replaceSuperTypeRefs(newSuperTypeRefs: List<FirTypeRef>) {
superTypeRefs.clear()
superTypeRefs.addAll(newSuperTypeRefs)
@@ -79,6 +76,11 @@ class FirJavaClass @FirImplementationDetail internal constructor(
override fun replaceControlFlowGraphReference(newControlFlowGraphReference: FirControlFlowGraphReference?) {}
override val companionObjectSymbol: FirRegularClassSymbol?
get() = null
override fun replaceCompanionObjectSymbol(newCompanionObjectSymbol: FirRegularClassSymbol?) {}
override fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D) {
declarations.forEach { it.accept(visitor, data) }
annotations.forEach { it.accept(visitor, data) }
@@ -116,10 +118,6 @@ class FirJavaClass @FirImplementationDetail internal constructor(
return this
}
override fun <D> transformCompanionObject(transformer: FirTransformer<D>, data: D): FirJavaClass {
return this
}
override fun <D> transformTypeParameters(transformer: FirTransformer<D>, data: D): FirRegularClass {
typeParameters.transformInplace(transformer, data)
return this
@@ -166,13 +164,6 @@ internal class FirJavaClassBuilder : FirRegularClassBuilder(), FirAnnotationCont
)
}
@Deprecated("Modification of 'hasLazyNestedClassifiers' has no impact for FirRegularClassImplBuilder", level = DeprecationLevel.HIDDEN)
override var companionObject: FirRegularClass?
get() = throw IllegalStateException()
set(@Suppress("UNUSED_PARAMETER") value) {
throw IllegalStateException()
}
@Deprecated("Modification of 'origin' has no impact for FirJavaClassBuilder", level = DeprecationLevel.HIDDEN)
override var origin: FirDeclarationOrigin
get() = throw IllegalStateException()
@@ -560,6 +560,7 @@ class DeclarationsConverter(
modifiers.hasExpect()
)
}
initCompanionObjectSymbolAttr()
}
}
}.also {
@@ -1133,6 +1133,8 @@ open class RawFirBuilder(
)
}
initCompanionObjectSymbolAttr()
context.popFirTypeParameters()
}
}
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.builder.*
import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl
import org.jetbrains.kotlin.fir.declarations.utils.addDeclaration
import org.jetbrains.kotlin.fir.declarations.utils.isCompanion
import org.jetbrains.kotlin.fir.declarations.utils.isLocal
import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic
import org.jetbrains.kotlin.fir.diagnostics.ConeUnderscoreIsReserved
@@ -1202,6 +1203,10 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
}
}
protected fun FirRegularClassBuilder.initCompanionObjectSymbolAttr() {
companionObjectSymbol = (declarations.firstOrNull { it is FirRegularClass && it.isCompanion } as FirRegularClass?)?.symbol
}
protected fun FirCallableDeclaration.initContainingClassAttr() {
initContainingClassAttr(context)
}
@@ -178,10 +178,10 @@ internal fun typeForQualifierByDeclaration(declaration: FirDeclaration, resultTy
declaration.symbol.constructType(emptyArray(), false),
)
} else {
val companionObject = declaration.companionObject
if (companionObject != null) {
val companionObjectSymbol = declaration.companionObjectSymbol
if (companionObjectSymbol != null) {
return resultType.resolvedTypeFromPrototype(
companionObject.symbol.constructType(emptyArray(), false),
companionObjectSymbol.constructType(emptyArray(), false),
)
}
}
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.declarations.builder.buildErrorFunction
import org.jetbrains.kotlin.fir.declarations.builder.buildErrorProperty
import org.jetbrains.kotlin.fir.declarations.utils.classId
import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.moduleData
@@ -92,7 +91,7 @@ class CandidateFactory private constructor(
val expressionReceiverValue = this as? ExpressionReceiverValue ?: return false
val resolvedQualifier = (expressionReceiverValue.explicitReceiver as? FirResolvedQualifier) ?: return false
val originClassOfCandidate = expressionReceiverValue.type.classId ?: return false
return (resolvedQualifier.symbol?.fir as? FirRegularClass)?.companionObject?.classId == originClassOfCandidate
return (resolvedQualifier.symbol?.fir as? FirRegularClass)?.companionObjectSymbol?.classId == originClassOfCandidate
}
fun createErrorCandidate(callInfo: CallInfo, diagnostic: ConeDiagnostic): Candidate {
@@ -250,7 +250,7 @@ open class FirTypeResolveTransformer(
}
session.nestedClassifierScope(firClass)?.let(scopes::add)
if (firClass is FirRegularClass) {
val companionObject = firClass.companionObject
val companionObject = firClass.companionObjectSymbol?.fir
if (companionObject != null) {
session.nestedClassifierScope(companionObject)?.let(scopes::add)
}
@@ -10,10 +10,7 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.diagnostics.WhenMissingCase
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirEnumEntry
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.declarations.collectEnumEntries
import org.jetbrains.kotlin.fir.declarations.getSealedClassInheritors
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.modality
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.LogicOperationKind.OR
@@ -342,7 +339,7 @@ private object WhenOnSealedClassExhaustivenessChecker : WhenExhaustivenessChecke
if (firClass?.classKind == ClassKind.OBJECT) {
firClass.symbol
} else {
firClass?.companionObject?.symbol
firClass?.companionObjectSymbol
}
}
else -> {
@@ -223,7 +223,6 @@ open class FirContractResolveTransformer(
}
override fun transformRegularClass(regularClass: FirRegularClass, data: ResolutionMode): FirStatement {
regularClass.transformCompanionObject(this, data)
context.withRegularClass(regularClass, components, forContracts = true) {
transformDeclarationContent(regularClass, data)
}
@@ -44,7 +44,6 @@ internal abstract class FirAbstractAnnotationResolveTransformer<D, S>(
return transformDeclaration(regularClass, data).also {
val state = beforeTransformingChildren(regularClass)
regularClass.transformDeclarations(this, data)
regularClass.transformCompanionObject(this, data)
regularClass.transformSuperTypeRefs(this, data)
afterTransformingChildren(state)
} as FirStatement
@@ -43,7 +43,6 @@ private class FirDeclarationsResolveTransformerForArgumentAnnotations(
.transformTypeParameters(transformer, data)
.transformSuperTypeRefs(transformer, data)
.transformDeclarations(transformer, data)
.transformCompanionObject(transformer, data)
}
}
return regularClass
@@ -54,7 +54,7 @@ data class ImplicitReceivers(
fun SessionHolder.collectTowerDataElementsForClass(owner: FirClass, defaultType: ConeKotlinType): TowerElementsForClass {
val allImplicitCompanionValues = mutableListOf<ImplicitReceiverValue<*>>()
val companionObject = (owner as? FirRegularClass)?.companionObject
val companionObject = (owner as? FirRegularClass)?.companionObjectSymbol?.fir
val companionReceiver = companionObject?.let { companion ->
ImplicitDispatchReceiverValue(
companion.symbol, session, scopeSession
@@ -72,9 +72,9 @@ fun SessionHolder.collectTowerDataElementsForClass(owner: FirClass, defaultType:
?.asTowerDataElement(isLocal = false)
?.let(superClassesStaticsAndCompanionReceivers::add)
(superClass as? FirRegularClass)?.companionObject?.let { companion ->
(superClass as? FirRegularClass)?.companionObjectSymbol?.let {
val superCompanionReceiver = ImplicitDispatchReceiverValue(
companion.symbol, session, scopeSession
it, session, scopeSession
)
superClassesStaticsAndCompanionReceivers += superCompanionReceiver.asTowerDataElement()
@@ -38,8 +38,8 @@ abstract class FirRegularClass : FirClass(), FirControlFlowGraphOwner {
abstract override val controlFlowGraphReference: FirControlFlowGraphReference?
abstract val name: Name
abstract override val symbol: FirRegularClassSymbol
abstract val companionObject: FirRegularClass?
abstract val hasLazyNestedClassifiers: Boolean
abstract val companionObjectSymbol: FirRegularClassSymbol?
abstract override val superTypeRefs: List<FirTypeRef>
override fun <R, D> accept(visitor: FirVisitor<R, D>, data: D): R = visitor.visitRegularClass(this, data)
@@ -54,6 +54,8 @@ abstract class FirRegularClass : FirClass(), FirControlFlowGraphOwner {
abstract override fun replaceControlFlowGraphReference(newControlFlowGraphReference: FirControlFlowGraphReference?)
abstract fun replaceCompanionObjectSymbol(newCompanionObjectSymbol: FirRegularClassSymbol?)
abstract override fun replaceSuperTypeRefs(newSuperTypeRefs: List<FirTypeRef>)
abstract override fun <D> transformTypeParameters(transformer: FirTransformer<D>, data: D): FirRegularClass
@@ -64,7 +66,5 @@ abstract class FirRegularClass : FirClass(), FirControlFlowGraphOwner {
abstract override fun <D> transformAnnotations(transformer: FirTransformer<D>, data: D): FirRegularClass
abstract fun <D> transformCompanionObject(transformer: FirTransformer<D>, data: D): FirRegularClass
abstract override fun <D> transformSuperTypeRefs(transformer: FirTransformer<D>, data: D): FirRegularClass
}
@@ -51,7 +51,7 @@ open class FirRegularClassBuilder : FirClassBuilder, FirTypeParameterRefsOwnerBu
override lateinit var scopeProvider: FirScopeProvider
open lateinit var name: Name
open lateinit var symbol: FirRegularClassSymbol
open var companionObject: FirRegularClass? = null
open var companionObjectSymbol: FirRegularClassSymbol? = null
override val superTypeRefs: MutableList<FirTypeRef> = mutableListOf()
override fun build(): FirRegularClass {
@@ -70,7 +70,7 @@ open class FirRegularClassBuilder : FirClassBuilder, FirTypeParameterRefsOwnerBu
scopeProvider,
name,
symbol,
companionObject,
companionObjectSymbol,
superTypeRefs,
)
}
@@ -105,7 +105,7 @@ inline fun buildRegularClassCopy(original: FirRegularClass, init: FirRegularClas
copyBuilder.scopeProvider = original.scopeProvider
copyBuilder.name = original.name
copyBuilder.symbol = original.symbol
copyBuilder.companionObject = original.companionObject
copyBuilder.companionObjectSymbol = original.companionObjectSymbol
copyBuilder.superTypeRefs.addAll(original.superTypeRefs)
return copyBuilder.apply(init).build()
}
@@ -45,7 +45,7 @@ internal class FirRegularClassImpl(
override val scopeProvider: FirScopeProvider,
override val name: Name,
override val symbol: FirRegularClassSymbol,
override var companionObject: FirRegularClass?,
override var companionObjectSymbol: FirRegularClassSymbol?,
override val superTypeRefs: MutableList<FirTypeRef>,
) : FirRegularClass() {
override var controlFlowGraphReference: FirControlFlowGraphReference? = null
@@ -70,7 +70,6 @@ internal class FirRegularClassImpl(
transformDeclarations(transformer, data)
transformAnnotations(transformer, data)
controlFlowGraphReference = controlFlowGraphReference?.transform(transformer, data)
companionObject = declarations.asSequence().filterIsInstance<FirRegularClass>().firstOrNull { it.status.isCompanion }
transformSuperTypeRefs(transformer, data)
return this
}
@@ -95,11 +94,6 @@ internal class FirRegularClassImpl(
return this
}
override fun <D> transformCompanionObject(transformer: FirTransformer<D>, data: D): FirRegularClassImpl {
companionObject = companionObject?.transform(transformer, data)
return this
}
override fun <D> transformSuperTypeRefs(transformer: FirTransformer<D>, data: D): FirRegularClassImpl {
superTypeRefs.transformInplace(transformer, data)
return this
@@ -117,6 +111,10 @@ internal class FirRegularClassImpl(
controlFlowGraphReference = newControlFlowGraphReference
}
override fun replaceCompanionObjectSymbol(newCompanionObjectSymbol: FirRegularClassSymbol?) {
companionObjectSymbol = newCompanionObjectSymbol
}
override fun replaceSuperTypeRefs(newSuperTypeRefs: List<FirTypeRef>) {
superTypeRefs.clear()
superTypeRefs.addAll(newSuperTypeRefs)
@@ -36,7 +36,7 @@ internal class FirResolvedQualifierImpl(
override val classId: ClassId? get() = relativeClassFqName?.let {
ClassId(packageFqName, it, false)
}
override var resolvedToCompanionObject: Boolean = (symbol?.fir as? FirRegularClass)?.companionObject != null
override var resolvedToCompanionObject: Boolean = (symbol?.fir as? FirRegularClass)?.companionObjectSymbol != null
override fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D) {
typeRef.accept(visitor, data)
@@ -21,9 +21,6 @@ fun FirTypeParameterBuilder.addDefaultBoundIfNecessary() {
fun FirRegularClassBuilder.addDeclaration(declaration: FirDeclaration) {
declarations += declaration
if (companionObject == null && declaration is FirRegularClass && declaration.isCompanion) {
companionObject = declaration
}
}
fun FirRegularClassBuilder.addDeclarations(declarations: Collection<FirDeclaration>) {
@@ -66,7 +66,7 @@ class FirRegularClassSymbol(classId: ClassId) : FirClassSymbol<FirRegularClass>(
}
val companionObjectSymbol: FirRegularClassSymbol?
get() = fir.companionObject?.symbol
get() = fir.companionObjectSymbol
}
val ANONYMOUS_CLASS_ID = ClassId(FqName.ROOT, FqName.topLevel(SpecialNames.ANONYMOUS), true)
@@ -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)")
@@ -780,7 +780,7 @@ class FirVisualizer(private val firFile: FirFile) : BaseRenderer() {
override fun visitResolvedQualifier(resolvedQualifier: FirResolvedQualifier, data: StringBuilder) {
val fir = resolvedQualifier.symbol?.fir
when {
fir is FirRegularClass && fir.classKind != ClassKind.ENUM_CLASS && fir.companionObject?.defaultType() == resolvedQualifier.typeRef.coneTypeSafe() -> {
fir is FirRegularClass && fir.classKind != ClassKind.ENUM_CLASS && fir.companionObjectSymbol?.defaultType() == resolvedQualifier.typeRef.coneTypeSafe() -> {
data.append("companion object ")
data.append(resolvedQualifier.typeRef.render()).append(": ")
data.append(fir.symbol.classId.asString().removeCurrentFilePackage())