import kotlin.reflect.KProperty import kotlin.properties.ReadOnlyProperty interface KtLifetimeToken { fun assertIsValid() } interface KtLifetimeTokenOwner { val token: KtLifetimeToken } inline fun KtLifetimeTokenOwner.assertIsValid() { token.assertIsValid() } inline fun KtLifetimeTokenOwner.withValidityAssertion(action: () -> R): R { assertIsValid() return action() } class ValidityAwareCachedValue( private val token: KtLifetimeToken, init: () -> T ) : ReadOnlyProperty { private val lazyValue = lazy(LazyThreadSafetyMode.PUBLICATION, init) @Suppress("UNCHECKED_CAST") override fun getValue(thisRef: Any, property: KProperty<*>): T { token.assertIsValid() return lazyValue.value } } internal fun KtLifetimeTokenOwner.cached(init: () -> T) = ValidityAwareCachedValue(token, init) public typealias KtScopeNameFilter = (String) -> Boolean abstract class KtFirNonStarImportingScope( private val firScope: FirScope, private val builder: KtSymbolByFirBuilder, override val token: KtLifetimeToken, ) : KtLifetimeTokenOwner { private val imports: List by cached { buildList { getCallableNames().forEach { add(it) } } } fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence = withValidityAssertion { firScope.getCallableSymbols(getCallableNames().filter(nameFilter), builder) } abstract fun getCallableNames(): Set } interface FirScope { fun getCallableSymbols(callableNames: Collection, builder: KtSymbolByFirBuilder): Sequence } interface KtCallableSymbol interface KtSymbolByFirBuilder