Replace FirSymbolProvider::getCallableSymbols with two methods

Requesting content of classes seems to be reasonable and at the same time
more easy to implement in Composite providers: they should find
just the first class' result instead of flatmapping all of them
This commit is contained in:
Denis Zharkov
2019-03-26 19:44:13 +03:00
parent 1426341e9f
commit 825218b479
20 changed files with 137 additions and 118 deletions
@@ -8,8 +8,6 @@ package org.jetbrains.kotlin.fir.java
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirCallableMemberDeclaration
import org.jetbrains.kotlin.fir.declarations.FirConstructor
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.java.declarations.FirJavaClass
import org.jetbrains.kotlin.fir.java.declarations.FirJavaConstructor
@@ -17,6 +15,8 @@ import org.jetbrains.kotlin.fir.java.declarations.FirJavaField
import org.jetbrains.kotlin.fir.java.declarations.FirJavaMethod
import org.jetbrains.kotlin.fir.resolve.AbstractFirSymbolProvider
import org.jetbrains.kotlin.fir.resolve.constructType
import org.jetbrains.kotlin.fir.scopes.FirScope
import org.jetbrains.kotlin.fir.scopes.impl.FirClassDeclaredMemberScope
import org.jetbrains.kotlin.fir.symbols.CallableId
import org.jetbrains.kotlin.fir.symbols.ConeCallableSymbol
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeSymbol
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.load.java.JvmAnnotationNames
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.resolve.jvm.KotlinJavaPsiFacade
class JavaSymbolProvider(
@@ -41,30 +42,12 @@ class JavaSymbolProvider(
private fun findClass(classId: ClassId): JavaClass? = facade.findClass(JavaClassFinder.Request(classId), searchScope)
override fun getCallableSymbols(callableId: CallableId): List<ConeCallableSymbol> {
return callableCache.lookupCacheOrCalculate(callableId) {
val classId = callableId.classId ?: return@lookupCacheOrCalculate emptyList()
val classSymbol = getClassLikeSymbolByFqName(classId) as? FirClassSymbol
?: return@lookupCacheOrCalculate emptyList()
val firClass = classSymbol.fir
val callableSymbols = mutableListOf<ConeCallableSymbol>()
for (declaration in firClass.declarations) {
val declarationId = when (declaration) {
is FirConstructor -> {
CallableId(callableId.packageName, callableId.className, firClass.name)
}
is FirCallableMemberDeclaration -> {
CallableId(callableId.packageName, callableId.className, declaration.name)
}
else -> null
}
if (declarationId == callableId) {
val symbol = (declaration as FirCallableMemberDeclaration).symbol as ConeCallableSymbol
callableSymbols += symbol
}
}
callableSymbols
}.orEmpty()
override fun getTopLevelCallableSymbols(packageFqName: FqName, name: Name): List<ConeCallableSymbol> =
emptyList()
override fun getClassDeclaredMemberScope(classId: ClassId): FirScope? {
val classSymbol = getClassLikeSymbolByFqName(classId) as? FirClassSymbol ?: return null
return FirClassDeclaredMemberScope(classSymbol.fir)
}
override fun getClassLikeSymbolByFqName(classId: ClassId): ConeClassLikeSymbol? {
@@ -21,8 +21,8 @@ import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
import org.jetbrains.kotlin.fir.references.FirResolvedCallableReferenceImpl
import org.jetbrains.kotlin.fir.resolve.FirSymbolProvider
import org.jetbrains.kotlin.fir.resolve.constructType
import org.jetbrains.kotlin.fir.resolve.getClassDeclaredCallableSymbols
import org.jetbrains.kotlin.fir.service
import org.jetbrains.kotlin.fir.symbols.CallableId
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTagImpl
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol
@@ -237,8 +237,8 @@ private fun JavaAnnotationArgument.toFirExpression(session: FirSession): FirExpr
val classId = this@toFirExpression.enumClassId
val entryName = this@toFirExpression.entryName
val calleeReference = if (classId != null && entryName != null) {
val callableSymbol = session.service<FirSymbolProvider>().getCallableSymbols(
CallableId(classId.packageFqName, classId.relativeClassName, entryName)
val callableSymbol = session.service<FirSymbolProvider>().getClassDeclaredCallableSymbols(
classId, entryName
).firstOrNull()
callableSymbol?.let {
FirResolvedCallableReferenceImpl(session, null, entryName, it)
@@ -57,7 +57,7 @@ class FirJavaClass(
private fun buildJavaUseSiteScope(regularClass: FirRegularClass, useSiteSession: FirSession): JavaClassUseSiteScope {
val superTypeEnhancementScope = FirCompositeScope(mutableListOf())
val declaredScope = FirClassDeclaredMemberScope(regularClass, useSiteSession)
val declaredScope = FirClassDeclaredMemberScope(regularClass)
lookupSuperTypes(regularClass, lookupInterfaces = true, deep = false, useSiteSession = useSiteSession)
.mapNotNullTo(superTypeEnhancementScope.scopes) { useSiteSuperType ->
if (useSiteSuperType is ConeClassErrorType) return@mapNotNullTo null
@@ -77,4 +77,4 @@ class FirJavaClass(
superTypeRefs.addAll(newSupertypes)
return this
}
}
}
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.fir.java.deserialization
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirCallableMemberDeclaration
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.declarations.FirNamedDeclaration
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
@@ -15,7 +14,7 @@ import org.jetbrains.kotlin.fir.deserialization.FirDeserializationContext
import org.jetbrains.kotlin.fir.deserialization.deserializeClassToSymbol
import org.jetbrains.kotlin.fir.resolve.AbstractFirSymbolProvider
import org.jetbrains.kotlin.fir.resolve.getOrPut
import org.jetbrains.kotlin.fir.symbols.CallableId
import org.jetbrains.kotlin.fir.scopes.impl.FirClassDeclaredMemberScope
import org.jetbrains.kotlin.fir.symbols.ConeCallableSymbol
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeSymbol
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
@@ -81,22 +80,17 @@ class KotlinDeserializedJvmSymbolsProvider(
}
}
override fun getCallableSymbols(callableId: CallableId): List<ConeCallableSymbol> {
if (callableId.classId != null) {
return getClassDeclarations(callableId.classId!!).filterIsInstance<FirCallableMemberDeclaration>()
.filter { it.name == callableId.callableName }
.map { it.symbol }
}
val packageFqName = callableId.packageName
override fun getTopLevelCallableSymbols(packageFqName: FqName, name: Name): List<ConeCallableSymbol> {
return getPackageParts(packageFqName).flatMap { (packageProto, context) ->
packageProto.functionList.map {
context.memberDeserializer.loadFunction(it).symbol
}.filter { callableSymbol -> callableSymbol.callableId.callableName == callableId.callableName }
}.filter { callableSymbol -> callableSymbol.callableId.callableName == name }
}
}
override fun getClassDeclaredMemberScope(classId: ClassId) =
findRegularClass(classId)?.let(::FirClassDeclaredMemberScope)
private fun getPackageParts(packageFqName: FqName): Collection<Pair<ProtoBuf.Package, FirDeserializationContext>> {
return packagePartsCache.getOrPut(packageFqName) {
computePackagePartsInfos(packageFqName)
@@ -115,11 +109,14 @@ class KotlinDeserializedJvmSymbolsProvider(
.mapTo(sortedSetOf(), JavaClass::name)
private fun getClassDeclarations(classId: ClassId): List<FirDeclaration> {
@Suppress("UNCHECKED_CAST")
val classSymbol = getClassLikeSymbolByFqName(classId) as? FirBasedSymbol<FirRegularClass> ?: return emptyList()
return classSymbol.fir.declarations
return findRegularClass(classId)?.declarations ?: emptyList()
}
private fun findRegularClass(classId: ClassId): FirRegularClass? =
@Suppress("UNCHECKED_CAST")
(getClassLikeSymbolByFqName(classId) as? FirBasedSymbol<FirRegularClass>)?.fir
override fun getAllCallableNamesInClass(classId: ClassId): Set<Name> =
getClassDeclarations(classId)
.filterIsInstance<FirNamedDeclaration>()
@@ -15,7 +15,6 @@ import org.jetbrains.kotlin.fir.java.toNotNullConeKotlinType
import org.jetbrains.kotlin.fir.scopes.FirScope
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
import org.jetbrains.kotlin.fir.scopes.impl.FirAbstractProviderBasedScope
import org.jetbrains.kotlin.fir.scopes.impl.FirClassDeclaredMemberScope
import org.jetbrains.kotlin.fir.symbols.ConeCallableSymbol
import org.jetbrains.kotlin.fir.symbols.ConeFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.ConeVariableSymbol
@@ -28,7 +27,7 @@ class JavaClassUseSiteScope(
klass: FirRegularClass,
session: FirSession,
internal val superTypesScope: FirScope,
private val declaredMemberScope: FirClassDeclaredMemberScope
private val declaredMemberScope: FirScope
) : FirAbstractProviderBasedScope(session, lookupInFir = true) {
internal val symbol = klass.symbol