[FIR] Refactoring: Move set utility functions to setUtils.kt

This commit is contained in:
Marco Pennekamp
2023-10-19 19:41:27 +02:00
committed by Space Team
parent 8d2bf8828c
commit 38ebe60e9f
6 changed files with 16 additions and 18 deletions
@@ -6,8 +6,6 @@
package org.jetbrains.kotlin.analysis.low.level.api.fir.util
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
import org.jetbrains.kotlin.analysis.utils.collections.filterToSetOrEmpty
import org.jetbrains.kotlin.analysis.utils.collections.mapToSetOrEmpty
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolNamesProvider
@@ -15,6 +13,7 @@ import org.jetbrains.kotlin.fir.resolve.providers.FirDelegatingCachedSymbolNames
import org.jetbrains.kotlin.fir.resolve.providers.FirCachedSymbolNamesProvider
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.filterToSetOrEmpty
/**
* A [FirSymbolNamesProvider] that fetches top-level names from a Kotlin [declarationProvider].
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.name.*
import org.jetbrains.kotlin.serialization.SerializerExtensionProtocol
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
import org.jetbrains.kotlin.serialization.deserialization.getName
import org.jetbrains.kotlin.utils.mapToSetOrEmpty
import java.nio.file.Path
class PackagePartsCacheData(
@@ -95,10 +96,7 @@ abstract class AbstractFirDeserializedSymbolProvider(
override val symbolNamesProvider: FirSymbolNamesProvider = object : FirCachedSymbolNamesProvider(session) {
override fun computeTopLevelClassifierNames(packageFqName: FqName): Set<Name>? {
val classesInPackage = knownTopLevelClassesInPackage(packageFqName)
?.mapTo(mutableSetOf()) { Name.identifier(it) }
?.ifEmpty { emptySet() }
?: return null
val classesInPackage = knownTopLevelClassesInPackage(packageFqName)?.mapToSetOrEmpty { Name.identifier(it) } ?: return null
if (packageFqName.asString() !in packageNamesForNonClassDeclarations) return classesInPackage
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.load.java.structure.JavaClass
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.mapToSetOrEmpty
// This symbol provider only loads JVM classes *not* annotated with Kotlin `@Metadata` annotation.
// Use it in application sessions for loading classes from Java files listed on the command line.
@@ -72,9 +73,7 @@ open class JavaSymbolProvider(
override val symbolNamesProvider: FirSymbolNamesProvider = object : FirSymbolNamesProviderWithoutCallables() {
override fun getTopLevelClassifierNamesInPackage(packageFqName: FqName): Set<Name>? =
javaFacade.knownClassNamesInPackage(packageFqName)
?.mapTo(mutableSetOf()) { Name.identifier(it) }
?.ifEmpty { emptySet() }
javaFacade.knownClassNamesInPackage(packageFqName)?.mapToSetOrEmpty { Name.identifier(it) }
}
}
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.fir.resolve.providers.impl.FirSyntheticFunctionInter
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.flatMapToNullableSet
/**
* A [FirSymbolNamesProvider] that caches all name sets.
@@ -102,9 +103,3 @@ open class FirCompositeCachedSymbolNamesProvider(
create(session, providers.map { it.symbolNamesProvider })
}
}
/**
* Works almost as regular flatMap, but returns a set and returns null if any lambda call returned null
*/
inline fun <T, R> Iterable<T>.flatMapToNullableSet(transform: (T) -> Iterable<R>?): Set<R>? =
flatMapTo(mutableSetOf()) { transform(it) ?: return null }
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.flatMapToNullableSet
/**
* [FirSymbolNamesProvider] provides information about which symbols may be provided by a [FirSymbolProvider] given the symbol's name. This
@@ -3,7 +3,13 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.utils.collections
package org.jetbrains.kotlin.utils
/**
* Works almost as regular flatMap, but returns a set and returns null if any lambda call returned null
*/
inline fun <T, R> Iterable<T>.flatMapToNullableSet(transform: (T) -> Iterable<R>?): Set<R>? =
flatMapTo(mutableSetOf()) { transform(it) ?: return null }.ifEmpty { emptySet() }
/**
* Maps all elements of this non-empty collection with the given [transform] function to a new mutable set, or returns [emptySet] if this
@@ -12,8 +18,8 @@ package org.jetbrains.kotlin.analysis.utils.collections
* [mapToSetOrEmpty] should be preferred over `collection.mapTo(mutableSetOf()) { ... }` when `collection` may be empty and the resulting
* set may be cached, because [mapToSetOrEmpty] saves memory by avoiding the creation of an empty mutable set.
*/
public inline fun <T, R> Collection<T>.mapToSetOrEmpty(transform: (T) -> R): Set<R> =
inline fun <T, R> Collection<T>.mapToSetOrEmpty(transform: (T) -> R): Set<R> =
if (isNotEmpty()) mapTo(mutableSetOf(), transform) else emptySet()
public inline fun <T> Collection<T>.filterToSetOrEmpty(predicate: (T) -> Boolean): Set<T> =
inline fun <T> Collection<T>.filterToSetOrEmpty(predicate: (T) -> Boolean): Set<T> =
filterTo(mutableSetOf(), predicate).ifEmpty { emptySet() }