FIR: introduce separate companion object resolve context

Before this commit, during the resolve of companion objects we used
the same context than for any nested class. However, during companion
object resolve we should not have companion object receiver itself
accessible in any case (in particular, it should not be accessible
in constructor). So in this commit we introduced separate context
for this purpose.
This commit is contained in:
Mikhail Glukhikh
2021-01-14 18:23:05 +03:00
parent f282c3e547
commit f85fc47383
10 changed files with 72 additions and 33 deletions
@@ -143,6 +143,7 @@ typealias TowerDataContextForAnonymousFunctions = Map<FirAnonymousFunctionSymbol
class FirTowerDataContextsForClassParts(
val forNestedClasses: FirTowerDataContext,
val forCompanionObject: FirTowerDataContext,
val forConstructorHeaders: FirTowerDataContext,
val primaryConstructorPureParametersScope: FirLocalScope?,
val primaryConstructorAllParametersScope: FirLocalScope?,
@@ -82,6 +82,9 @@ class BodyResolveContext(
fun getTowerDataContextForStaticNestedClassesUnsafe(): FirTowerDataContext =
firTowerDataContextsForClassParts().forNestedClasses
fun getTowerDataContextForCompanionUnsafe(): FirTowerDataContext =
firTowerDataContextsForClassParts().forCompanionObject
fun getTowerDataContextForConstructorResolution(): FirTowerDataContext =
firTowerDataContextsForClassParts().forConstructorHeaders
@@ -348,7 +348,11 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
return context.withTowerDataCleanup {
if (!regularClass.isInner && context.containerIfAny is FirRegularClass) {
context.replaceTowerDataContext(
context.getTowerDataContextForStaticNestedClassesUnsafe()
if (regularClass.isCompanion) {
context.getTowerDataContextForCompanionUnsafe()
} else {
context.getTowerDataContextForStaticNestedClassesUnsafe()
}
)
}
@@ -816,17 +820,16 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
): T = context.withTowerDataCleanup {
val towerElementsForClass = components.collectTowerDataElementsForClass(owner, type)
val staticsAndCompanion =
context.towerDataContext
.addNonLocalTowerDataElements(towerElementsForClass.superClassesStaticsAndCompanionReceivers)
.run {
if (towerElementsForClass.companionReceiver != null)
addReceiver(null, towerElementsForClass.companionReceiver)
else
this
}
.addNonLocalScopeIfNotNull(towerElementsForClass.companionStaticScope)
.addNonLocalScopeIfNotNull(towerElementsForClass.staticScope)
val base = context.towerDataContext.addNonLocalTowerDataElements(towerElementsForClass.superClassesStaticsAndCompanionReceivers)
val statics = base
.addNonLocalScopeIfNotNull(towerElementsForClass.companionStaticScope)
.addNonLocalScopeIfNotNull(towerElementsForClass.staticScope)
val companionReceiver = towerElementsForClass.companionReceiver
val staticsAndCompanion = if (companionReceiver == null) statics else base
.addReceiver(null, companionReceiver)
.addNonLocalScopeIfNotNull(towerElementsForClass.companionStaticScope)
.addNonLocalScopeIfNotNull(towerElementsForClass.staticScope)
val typeParameterScope = (owner as? FirRegularClass)?.let(this::createTypeParameterScope)
@@ -856,6 +859,7 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
val newContexts = FirTowerDataContextsForClassParts(
newTowerDataContextForStaticNestedClasses,
statics,
scopeForConstructorHeader,
primaryConstructorPureParametersScope,
primaryConstructorAllParametersScope