FIR LC: use optimal type mapping mode for value parameter
This commit is contained in:
committed by
Ilya Kirillov
parent
6d150fb7a5
commit
d3c34fa200
+8
-2
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.analysis.api.types.KtType
|
||||
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
|
||||
import org.jetbrains.kotlin.codegen.signature.BothSignatureWriter
|
||||
import org.jetbrains.kotlin.load.kotlin.TypeMappingMode
|
||||
import org.jetbrains.kotlin.load.kotlin.getOptimalModeForValueParameter
|
||||
import org.jetbrains.kotlin.name.SpecialNames
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import java.text.StringCharacterIterator
|
||||
@@ -40,6 +41,11 @@ internal class KtFe10PsiTypeProvider(override val analysisSession: KtFe10Analysi
|
||||
return asPsiType(simplifyType(kotlinType), useSitePosition, mode)
|
||||
}
|
||||
|
||||
override fun getOptimalModeForValueParameter(type: KtType): TypeMappingMode = withValidityAssertion {
|
||||
require(type is KtFe10Type)
|
||||
typeMapper.typeContext.getOptimalModeForValueParameter(type.type)
|
||||
}
|
||||
|
||||
private fun simplifyType(type: UnwrappedType): KotlinType {
|
||||
var result = type
|
||||
do {
|
||||
@@ -53,7 +59,7 @@ internal class KtFe10PsiTypeProvider(override val analysisSession: KtFe10Analysi
|
||||
return result
|
||||
}
|
||||
|
||||
private fun asPsiType(type: KotlinType, context: PsiElement, mode: TypeMappingMode): PsiType? {
|
||||
private fun asPsiType(type: KotlinType, useSitePosition: PsiElement, mode: TypeMappingMode): PsiType? {
|
||||
val signatureWriter = BothSignatureWriter(BothSignatureWriter.Mode.SKIP_CHECKS)
|
||||
typeMapper.mapType(type, mode, signatureWriter)
|
||||
|
||||
@@ -69,7 +75,7 @@ internal class KtFe10PsiTypeProvider(override val analysisSession: KtFe10Analysi
|
||||
val typeInfo = TypeInfo.fromString(javaType, false)
|
||||
val typeText = TypeInfo.createTypeText(typeInfo) ?: return null
|
||||
|
||||
val typeElement = ClsTypeElementImpl(context, typeText, '\u0000')
|
||||
val typeElement = ClsTypeElementImpl(useSitePosition, typeText, '\u0000')
|
||||
return typeElement.type
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -13,6 +13,7 @@ import com.intellij.psi.impl.compiled.SignatureParsing
|
||||
import com.intellij.psi.impl.compiled.StubBuildingVisitor
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtPsiTypeProvider
|
||||
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.fir.types.KtFirType
|
||||
import org.jetbrains.kotlin.analysis.api.fir.types.PublicTypeApproximator
|
||||
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtType
|
||||
@@ -38,6 +39,7 @@ import org.jetbrains.kotlin.fir.resolve.toSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.load.kotlin.TypeMappingMode
|
||||
import org.jetbrains.kotlin.load.kotlin.getOptimalModeForValueParameter
|
||||
import org.jetbrains.kotlin.name.SpecialNames
|
||||
import org.jetbrains.kotlin.psi
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parents
|
||||
@@ -56,6 +58,11 @@ internal class KtFirPsiTypeProvider(
|
||||
): PsiType? = withValidityAssertion {
|
||||
type.coneType.asPsiType(rootModuleSession, analysisSession.firResolveState, mode, useSitePosition)
|
||||
}
|
||||
|
||||
override fun getOptimalModeForValueParameter(type: KtType): TypeMappingMode = withValidityAssertion {
|
||||
require(type is KtFirType)
|
||||
rootModuleSession.jvmTypeMapper.typeContext.getOptimalModeForValueParameter(type.coneType)
|
||||
}
|
||||
}
|
||||
|
||||
private fun ConeKotlinType.simplifyType(
|
||||
|
||||
+4
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.load.kotlin.TypeMappingMode
|
||||
|
||||
public abstract class KtPsiTypeProvider : KtAnalysisSessionComponent() {
|
||||
public abstract fun asPsiType(type: KtType, useSitePosition: PsiElement, mode: TypeMappingMode): PsiType?
|
||||
public abstract fun getOptimalModeForValueParameter(type: KtType): TypeMappingMode
|
||||
}
|
||||
|
||||
public interface KtPsiTypeProviderMixIn : KtAnalysisSessionMixIn {
|
||||
@@ -36,4 +37,7 @@ public interface KtPsiTypeProviderMixIn : KtAnalysisSessionMixIn {
|
||||
mode: TypeMappingMode = TypeMappingMode.DEFAULT,
|
||||
): PsiType? =
|
||||
analysisSession.psiTypeProvider.asPsiType(this, useSitePosition, mode)
|
||||
|
||||
public fun KtType.getOptimalModeForValueParameter(): TypeMappingMode =
|
||||
analysisSession.psiTypeProvider.getOptimalModeForValueParameter(this)
|
||||
}
|
||||
|
||||
+11
-5
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.light.classes.symbol
|
||||
import com.intellij.psi.*
|
||||
import org.jetbrains.kotlin.asJava.classes.lazyPub
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtValueParameterSymbol
|
||||
import org.jetbrains.kotlin.load.kotlin.TypeMappingMode
|
||||
import org.jetbrains.kotlin.psi.KtParameter
|
||||
|
||||
internal abstract class FirLightParameterBaseForSymbol(
|
||||
@@ -39,17 +40,22 @@ internal abstract class FirLightParameterBaseForSymbol(
|
||||
parameterSymbol.annotatedType.type
|
||||
)
|
||||
}
|
||||
}
|
||||
else NullabilityType.Unknown
|
||||
} else NullabilityType.Unknown
|
||||
}
|
||||
|
||||
override fun getNameIdentifier(): PsiIdentifier = _identifier
|
||||
|
||||
private val _type by lazyPub {
|
||||
val convertedType = analyzeWithSymbolAsContext(parameterSymbol) {
|
||||
parameterSymbol.annotatedType.type.asPsiType(this@FirLightParameterBaseForSymbol)
|
||||
?: this@FirLightParameterBaseForSymbol.nonExistentType()
|
||||
}
|
||||
val ktType = parameterSymbol.annotatedType.type
|
||||
val typeMappingMode = when {
|
||||
ktType.isSuspendFunctionType -> TypeMappingMode.DEFAULT
|
||||
// TODO: extract type mapping mode from annotation?
|
||||
// TODO: methods with declaration site wildcards?
|
||||
else -> ktType.getOptimalModeForValueParameter()
|
||||
}
|
||||
ktType.asPsiType(this@FirLightParameterBaseForSymbol, typeMappingMode)
|
||||
} ?: nonExistentType()
|
||||
if (parameterSymbol.isVararg) {
|
||||
PsiEllipsisType(convertedType, convertedType.annotationProvider)
|
||||
} else convertedType
|
||||
|
||||
+1
-2
@@ -72,8 +72,7 @@ internal class FirLightParameterForReceiver private constructor(
|
||||
private val _type: PsiType by lazyPub {
|
||||
analyzeWithSymbolAsContext(context) {
|
||||
receiverTypeAndAnnotations.type.asPsiType(this@FirLightParameterForReceiver)
|
||||
?: this@FirLightParameterForReceiver.nonExistentType()
|
||||
}
|
||||
} ?: nonExistentType()
|
||||
}
|
||||
|
||||
override fun getType(): PsiType = _type
|
||||
|
||||
@@ -22,3 +22,5 @@ class C {
|
||||
@JvmName("fooInt")
|
||||
fun foo(x: List<Int>) {}
|
||||
}
|
||||
|
||||
// FIR_COMPARISON
|
||||
|
||||
@@ -20,3 +20,5 @@ fun setRwProp(v: Int) {}
|
||||
fun foo(x: List<String>) {}
|
||||
@JvmName("fooInt")
|
||||
fun foo(x: List<Int>) {}
|
||||
|
||||
// FIR_COMPARISON
|
||||
|
||||
Reference in New Issue
Block a user