@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeTypeParameterSupertype
24
24
import org.jetbrains.kotlin.fir.resolve.providers.firProvider
25
25
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
26
26
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.LocalClassesNavigationInfo
27
- import org.jetbrains.kotlin.fir.scopes.FirCompositeScope
28
27
import org.jetbrains.kotlin.fir.scopes.FirScope
29
28
import org.jetbrains.kotlin.fir.scopes.createImportingScopes
30
29
import org.jetbrains.kotlin.fir.scopes.getNestedClassifierScope
@@ -43,6 +42,7 @@ import org.jetbrains.kotlin.fir.visitors.FirTransformer
43
42
import org.jetbrains.kotlin.name.ClassId
44
43
import org.jetbrains.kotlin.types.model.TypeArgumentMarker
45
44
import org.jetbrains.kotlin.utils.addIfNotNull
45
+ import org.jetbrains.kotlin.utils.addToStdlib.safeAs
46
46
47
47
class FirSupertypeResolverProcessor (session : FirSession , scopeSession : ScopeSession ) :
48
48
FirTransformerBasedResolveProcessor (session, scopeSession) {
@@ -169,19 +169,22 @@ private fun FirClassLikeDeclaration.typeParametersScope(): FirScope? {
169
169
return FirMemberTypeParameterScope (this )
170
170
}
171
171
172
- private fun createOtherScopesForNestedClasses (
172
+ private fun createOtherScopesForNestedClassesOrCompanion (
173
173
klass : FirClass ,
174
174
session : FirSession ,
175
175
scopeSession : ScopeSession ,
176
- supertypeComputationSession : SupertypeComputationSession
176
+ supertypeComputationSession : SupertypeComputationSession ,
177
+ withCompanionScopes : Boolean ,
177
178
): Collection <FirScope > =
178
179
mutableListOf<FirScope >().apply {
179
180
// Note: from higher priority to lower priority
180
181
// See also: BodyResolveContext.withScopesForClass
181
182
addIfNotNull(session.nestedClassifierScope(klass))
182
- val companionObjects = klass.declarations.filterIsInstance<FirRegularClass >().filter { it.isCompanion }
183
- for (companionObject in companionObjects) {
184
- addIfNotNull(session.nestedClassifierScope(companionObject))
183
+ if (withCompanionScopes) {
184
+ val companionObjects = klass.declarations.filterIsInstance<FirRegularClass >().filter { it.isCompanion }
185
+ for (companionObject in companionObjects) {
186
+ addIfNotNull(session.nestedClassifierScope(companionObject))
187
+ }
185
188
}
186
189
lookupSuperTypes(
187
190
klass,
@@ -240,13 +243,26 @@ open class FirSupertypeResolverVisitor(
240
243
241
244
private fun prepareScopeForNestedClasses (klass : FirClass ): ScopePersistentList {
242
245
return supertypeComputationSession.getOrPutScopeForNestedClasses(klass) {
243
- val scopes = prepareScopes(klass)
246
+ calculateScopes(klass, true )
247
+ }
248
+ }
244
249
245
- resolveAllSupertypes(klass, klass.superTypeRefs)
246
- scopes.pushAll(createOtherScopesForNestedClasses(klass, session, scopeSession, supertypeComputationSession))
250
+ private fun prepareScopeForCompanion (klass : FirClass ): ScopePersistentList {
251
+ return supertypeComputationSession.getOrPutScopeForCompanion(klass) {
252
+ calculateScopes(klass, false )
247
253
}
248
254
}
249
255
256
+ private fun calculateScopes (
257
+ klass : FirClass ,
258
+ withCompanionScopes : Boolean ,
259
+ ): PersistentList <FirScope > {
260
+ resolveAllSupertypes(klass, klass.superTypeRefs)
261
+ return prepareScopes(klass).pushAll(
262
+ createOtherScopesForNestedClassesOrCompanion(klass, session, scopeSession, supertypeComputationSession, withCompanionScopes)
263
+ )
264
+ }
265
+
250
266
private fun resolveAllSupertypes (
251
267
classLikeDeclaration : FirClassLikeDeclaration ,
252
268
supertypeRefs : List <FirTypeRef >,
@@ -285,6 +301,10 @@ open class FirSupertypeResolverVisitor(
285
301
else -> scopeForLocalClass ? : return persistentListOf()
286
302
}
287
303
}
304
+ classLikeDeclaration.safeAs<FirRegularClass >()?.isCompanion == true -> {
305
+ val outerClassFir = classId.outerClassId?.let (::getFirClassifierByFqName) as ? FirRegularClass
306
+ prepareScopeForCompanion(outerClassFir ? : return persistentListOf())
307
+ }
288
308
classId.isNestedClass -> {
289
309
val outerClassFir = classId.outerClassId?.let (::getFirClassifierByFqName) as ? FirRegularClass
290
310
prepareScopeForNestedClasses(outerClassFir ? : return persistentListOf())
@@ -446,6 +466,7 @@ private fun createErrorTypeRef(fir: FirElement, message: String, kind: Diagnosti
446
466
class SupertypeComputationSession {
447
467
private val fileScopesMap = hashMapOf<FirFile , ScopePersistentList >()
448
468
private val scopesForNestedClassesMap = hashMapOf<FirClass , ScopePersistentList >()
469
+ private val scopesForCompanionMap = hashMapOf<FirClass , ScopePersistentList >()
449
470
val supertypeStatusMap = linkedMapOf<FirClassLikeDeclaration , SupertypeComputationStatus >()
450
471
451
472
val supertypesSupplier: SupertypeSupplier = object : SupertypeSupplier () {
@@ -473,6 +494,9 @@ class SupertypeComputationSession {
473
494
fun getOrPutScopeForNestedClasses (klass : FirClass , scope : () -> ScopePersistentList ): ScopePersistentList =
474
495
scopesForNestedClassesMap.getOrPut(klass) { scope() }
475
496
497
+ fun getOrPutScopeForCompanion (klass : FirClass , scope : () -> ScopePersistentList ): ScopePersistentList =
498
+ scopesForCompanionMap.getOrPut(klass) { scope() }
499
+
476
500
fun startComputingSupertypes (classLikeDeclaration : FirClassLikeDeclaration ) {
477
501
require(supertypeStatusMap[classLikeDeclaration] == null ) {
478
502
" Unexpected in startComputingSupertypes supertype status for $classLikeDeclaration : ${supertypeStatusMap[classLikeDeclaration]} "
0 commit comments