kotlin.reflect.jvm.internal.impl.descriptors.ScopesHolderForClass.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-reflect Show documentation
Show all versions of kotlin-reflect Show documentation
Kotlin Full Reflection Library
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.reflect.jvm.internal.impl.descriptors
import kotlin.reflect.jvm.internal.impl.resolve.descriptorUtil.module
import kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope
import kotlin.reflect.jvm.internal.impl.storage.StorageManager
import kotlin.reflect.jvm.internal.impl.storage.getValue
import kotlin.reflect.jvm.internal.impl.types.checker.KotlinTypeRefiner
import kotlin.reflect.jvm.internal.impl.types.refinement.TypeRefinement
class ScopesHolderForClass private constructor(
private val classDescriptor: ClassDescriptor,
storageManager: StorageManager,
private val scopeFactory: (KotlinTypeRefiner) -> T,
private val kotlinTypeRefinerForOwnerModule: KotlinTypeRefiner
) {
private val scopeForOwnerModule by storageManager.createLazyValue {
scopeFactory(kotlinTypeRefinerForOwnerModule)
}
@OptIn(TypeRefinement::class)
fun getScope(kotlinTypeRefiner: KotlinTypeRefiner): T {
/*
* That check doesn't break anything, because scopeForOwnerModule _will_ anyway refine supertypes from module of
* class descriptor.
*
* Without that fastpass there is problem wit recursion types such that:
*
* interface A>
*
* interface B : B
*
* In this case (without check) we start compute default type of class descriptor B, go to isRefinementNeededForTypeConstructor,
* ask for supertypes of B, see A, ask default type of class B and fail with recursion problem
*/
if (!kotlinTypeRefiner.isRefinementNeededForModule(classDescriptor.module)) return scopeForOwnerModule
if (!kotlinTypeRefiner.isRefinementNeededForTypeConstructor(classDescriptor.typeConstructor)) return scopeForOwnerModule
return kotlinTypeRefiner.getOrPutScopeForClass(classDescriptor) { scopeFactory(kotlinTypeRefiner) }
}
companion object {
fun create(
classDescriptor: ClassDescriptor,
storageManager: StorageManager,
kotlinTypeRefinerForOwnerModule: KotlinTypeRefiner,
scopeFactory: (KotlinTypeRefiner) -> T
): ScopesHolderForClass {
return ScopesHolderForClass(classDescriptor, storageManager, scopeFactory, kotlinTypeRefinerForOwnerModule)
}
}
}