Minor. FIR: Simplify createFunctionalType

^KT-32725 In Progress
This commit is contained in:
Denis Zharkov
2019-10-10 16:18:16 +03:00
parent bffcf8478e
commit e359791ab4
2 changed files with 14 additions and 12 deletions
@@ -6,7 +6,8 @@
package org.jetbrains.kotlin.fir.resolve package org.jetbrains.kotlin.fir.resolve
import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.* import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.componentArrayAccessor
import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccess import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccess
import org.jetbrains.kotlin.fir.expressions.FirResolvable import org.jetbrains.kotlin.fir.expressions.FirResolvable
@@ -14,9 +15,11 @@ import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
import org.jetbrains.kotlin.fir.references.FirResolvedCallableReference import org.jetbrains.kotlin.fir.references.FirResolvedCallableReference
import org.jetbrains.kotlin.fir.references.FirThisReference import org.jetbrains.kotlin.fir.references.FirThisReference
import org.jetbrains.kotlin.fir.render
import org.jetbrains.kotlin.fir.resolve.calls.FirNamedReferenceWithCandidate import org.jetbrains.kotlin.fir.resolve.calls.FirNamedReferenceWithCandidate
import org.jetbrains.kotlin.fir.resolve.substitution.AbstractConeSubstitutor import org.jetbrains.kotlin.fir.resolve.substitution.AbstractConeSubstitutor
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.resultType import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.resultType
import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype
import org.jetbrains.kotlin.fir.scopes.impl.FirMemberScopeProvider import org.jetbrains.kotlin.fir.scopes.impl.FirMemberScopeProvider
import org.jetbrains.kotlin.fir.scopes.impl.withReplacedConeType import org.jetbrains.kotlin.fir.scopes.impl.withReplacedConeType
import org.jetbrains.kotlin.fir.symbols.* import org.jetbrains.kotlin.fir.symbols.*
@@ -205,13 +208,12 @@ fun FirFunction<*>.constructFunctionalTypeRef(session: FirSession): FirResolvedT
} }
val rawReturnType = (this as FirTypedDeclaration).returnTypeRef.coneTypeUnsafe<ConeKotlinType>() val rawReturnType = (this as FirTypedDeclaration).returnTypeRef.coneTypeUnsafe<ConeKotlinType>()
val functionalType = createFunctionalType(session, parameters, receiverTypeRef?.coneTypeUnsafe(), rawReturnType) val functionalType = createFunctionalType(parameters, receiverTypeRef?.coneTypeUnsafe(), rawReturnType)
return FirResolvedTypeRefImpl(psi, functionalType) return FirResolvedTypeRefImpl(psi, functionalType)
} }
fun createFunctionalType( fun createFunctionalType(
session: FirSession,
parameters: List<ConeKotlinType>, parameters: List<ConeKotlinType>,
receiverType: ConeKotlinType?, receiverType: ConeKotlinType?,
rawReturnType: ConeKotlinType rawReturnType: ConeKotlinType
@@ -219,15 +221,14 @@ fun createFunctionalType(
val receiverAndParameterTypes = listOfNotNull(receiverType) + parameters + listOf(rawReturnType) val receiverAndParameterTypes = listOfNotNull(receiverType) + parameters + listOf(rawReturnType)
val functionalTypeId = StandardClassIds.byName("Function${receiverAndParameterTypes.size - 1}") val functionalTypeId = StandardClassIds.byName("Function${receiverAndParameterTypes.size - 1}")
val functionalType = functionalTypeId(session.firSymbolProvider).constructType(receiverAndParameterTypes.toTypedArray(), isNullable = false) return ConeClassTypeImpl(ConeClassLikeLookupTagImpl(functionalTypeId), receiverAndParameterTypes.toTypedArray(), isNullable = false)
return functionalType
} }
fun BodyResolveComponents.typeForQualifier(resolvedQualifier: FirResolvedQualifier): FirTypeRef { fun BodyResolveComponents.typeForQualifier(resolvedQualifier: FirResolvedQualifier): FirTypeRef {
val classId = resolvedQualifier.classId val classId = resolvedQualifier.classId
val resultType = resolvedQualifier.resultType val resultType = resolvedQualifier.resultType
if (classId != null) { if (classId != null) {
val classSymbol: FirClassLikeSymbol<*> = symbolProvider.getClassLikeSymbolByFqName(classId)!! val classSymbol = symbolProvider.getClassLikeSymbolByFqName(classId)!!
val declaration = classSymbol.phasedFir val declaration = classSymbol.phasedFir
if (declaration is FirClass) { if (declaration is FirClass) {
if (declaration.classKind == ClassKind.OBJECT) { if (declaration.classKind == ClassKind.OBJECT) {
@@ -311,8 +312,10 @@ private fun BodyResolveComponents.typeFromSymbol(symbol: AbstractFirBasedSymbol<
"no enum item supertype" "no enum item supertype"
) )
} else } else
FirResolvedTypeRefImpl(null, symbol.constructType(emptyArray(), isNullable = false)) FirResolvedTypeRefImpl(
null, symbol.constructType(emptyArray(), isNullable = false)
)
} }
else -> error("WTF ! $symbol") else -> error("WTF ! $symbol")
} }
} }
@@ -191,7 +191,7 @@ class FirSamResolverImpl(
val abstractMethod = firRegularClass.getSingleAbstractMethodOrNull(firSession, scopeSession) ?: return@getOrPut NULL_STUB val abstractMethod = firRegularClass.getSingleAbstractMethodOrNull(firSession, scopeSession) ?: return@getOrPut NULL_STUB
// TODO: val shouldConvertFirstParameterToDescriptor = samWithReceiverResolvers.any { it.shouldConvertFirstSamParameterToReceiver(abstractMethod) } // TODO: val shouldConvertFirstParameterToDescriptor = samWithReceiverResolvers.any { it.shouldConvertFirstSamParameterToReceiver(abstractMethod) }
abstractMethod.getFunctionTypeForAbstractMethod(firSession) abstractMethod.getFunctionTypeForAbstractMethod()
} as? ConeKotlinType } as? ConeKotlinType
} }
@@ -292,14 +292,13 @@ private fun FirRegularClass.hasMoreThenOneAbstractFunctionOrHasAbstractProperty(
return false return false
} }
private fun FirSimpleFunction.getFunctionTypeForAbstractMethod(session: FirSession): ConeLookupTagBasedType { private fun FirSimpleFunction.getFunctionTypeForAbstractMethod(): ConeLookupTagBasedType {
val parameterTypes = valueParameters.map { val parameterTypes = valueParameters.map {
it.returnTypeRef.coneTypeSafe<ConeKotlinType>() ?: ConeKotlinErrorType("No type for parameter $it") it.returnTypeRef.coneTypeSafe<ConeKotlinType>() ?: ConeKotlinErrorType("No type for parameter $it")
} }
return createFunctionalType( return createFunctionalType(
session, parameterTypes, parameterTypes, receiverType = null,
receiverType = null,
rawReturnType = returnTypeRef.coneTypeSafe() ?: ConeKotlinErrorType("No type for return type of $this") rawReturnType = returnTypeRef.coneTypeSafe() ?: ConeKotlinErrorType("No type for return type of $this")
) )
} }