[FIR IDE] Refactoring, introduce Fe10AnalysisContext.builtIns

This commit is contained in:
Yan Zhulanow
2021-10-28 22:54:17 +09:00
parent a0e28972d5
commit d5e26bcf9e
7 changed files with 29 additions and 34 deletions
@@ -10,6 +10,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver
@@ -43,4 +44,7 @@ class Fe10AnalysisContext(
) : Fe10AnalysisFacade by facade {
val resolveSession: ResolveSession = getResolveSession(contextElement)
val deprecationResolver: DeprecationResolver = getDeprecationResolver(contextElement)
val builtIns: KotlinBuiltIns
get() = resolveSession.moduleDescriptor.builtIns
}
@@ -14,7 +14,6 @@ import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.bas
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.lexer.KtTokens
@@ -51,9 +50,6 @@ class KtFe10ExpressionTypeProvider(
override val token: ValidityToken
get() = analysisSession.token
private val builtIns: KotlinBuiltIns
get() = analysisContext.resolveSession.moduleDescriptor.builtIns
override fun getKtExpressionType(expression: KtExpression): KtType? = withValidityAssertion {
// Not sure if it's safe enough. In theory, some annotations on expressions might change its type
val unwrapped = expression.unwrapParenthesesLabelsAndAnnotations() as? KtExpression ?: return null
@@ -62,7 +58,7 @@ class KtFe10ExpressionTypeProvider(
}
val bindingContext = analysisContext.analyze(unwrapped, AnalysisMode.PARTIAL)
val kotlinType = expression.getType(bindingContext) ?: builtIns.unitType
val kotlinType = expression.getType(bindingContext) ?: analysisContext.builtIns.unitType
return kotlinType.toKtType(analysisContext)
}
@@ -104,7 +100,7 @@ class KtFe10ExpressionTypeProvider(
return kotlinType.toKtType(analysisContext)
}
return builtIns.unitType.toKtType(analysisContext)
return analysisContext.builtIns.unitType.toKtType(analysisContext)
}
override fun getFunctionalTypeForKtFunction(declaration: KtFunction): KtType = withValidityAssertion {
@@ -119,8 +115,8 @@ class KtFe10ExpressionTypeProvider(
val parameterCount = declaration.valueParameters.size + (if (declaration.isExtensionDeclaration()) 1 else 0)
val function = when {
declaration.hasModifier(KtTokens.SUSPEND_KEYWORD) -> builtIns.getSuspendFunction(parameterCount)
else -> builtIns.getFunction(parameterCount)
declaration.hasModifier(KtTokens.SUSPEND_KEYWORD) -> analysisContext.builtIns.getSuspendFunction(parameterCount)
else -> analysisContext.builtIns.getFunction(parameterCount)
}
val errorMessage = "Descriptor not found for function \"${declaration.name}\""
@@ -48,7 +48,7 @@ internal class KtFe10TypeProvider(
@Suppress("SpellCheckingInspection")
private val typeApproximator by lazy {
TypeApproximator(
analysisContext.resolveSession.moduleDescriptor.builtIns,
analysisContext.builtIns,
analysisContext.resolveSession.languageVersionSettings
)
}
@@ -405,51 +405,49 @@ private class KtFe10BuiltinTypes(private val analysisContext: Fe10AnalysisContex
override val token: ValidityToken
get() = analysisContext.token
private val builtIns = analysisContext.resolveSession.moduleDescriptor.builtIns
override val INT: KtType
get() = withValidityAssertion { builtIns.intType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.intType.toKtType(analysisContext) }
override val LONG: KtType
get() = withValidityAssertion { builtIns.longType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.longType.toKtType(analysisContext) }
override val SHORT: KtType
get() = withValidityAssertion { builtIns.shortType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.shortType.toKtType(analysisContext) }
override val BYTE: KtType
get() = withValidityAssertion { builtIns.byteType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.byteType.toKtType(analysisContext) }
override val FLOAT: KtType
get() = withValidityAssertion { builtIns.floatType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.floatType.toKtType(analysisContext) }
override val DOUBLE: KtType
get() = withValidityAssertion { builtIns.doubleType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.doubleType.toKtType(analysisContext) }
override val BOOLEAN: KtType
get() = withValidityAssertion { builtIns.booleanType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.booleanType.toKtType(analysisContext) }
override val CHAR: KtType
get() = withValidityAssertion { builtIns.charType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.charType.toKtType(analysisContext) }
override val STRING: KtType
get() = withValidityAssertion { builtIns.stringType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.stringType.toKtType(analysisContext) }
override val UNIT: KtType
get() = withValidityAssertion { builtIns.unitType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.unitType.toKtType(analysisContext) }
override val NOTHING: KtType
get() = withValidityAssertion { builtIns.nothingType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.nothingType.toKtType(analysisContext) }
override val ANY: KtType
get() = withValidityAssertion { builtIns.anyType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.anyType.toKtType(analysisContext) }
override val THROWABLE: KtType
get() = withValidityAssertion { builtIns.throwable.defaultType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.throwable.defaultType.toKtType(analysisContext) }
override val NULLABLE_ANY: KtType
get() = withValidityAssertion { builtIns.nullableAnyType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.nullableAnyType.toKtType(analysisContext) }
override val NULLABLE_NOTHING: KtType
get() = withValidityAssertion { builtIns.nullableNothingType.toKtType(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.nullableNothingType.toKtType(analysisContext) }
}
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
class KtFe10DescDefaultPropertySetterSymbol(
private val propertyDescriptor: PropertyDescriptor,
@@ -57,7 +56,7 @@ class KtFe10DescDefaultPropertySetterSymbol(
get() = withValidityAssertion { null }
override val annotatedType: KtTypeAndAnnotations
get() = withValidityAssertion { propertyDescriptor.builtIns.unitType.toKtTypeAndAnnotations(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.unitType.toKtTypeAndAnnotations(analysisContext) }
override val origin: KtSymbolOrigin
get() = withValidityAssertion { propertyDescriptor.getSymbolOrigin(analysisContext) }
@@ -19,14 +19,13 @@ import org.jetbrains.kotlin.descriptors.PropertySetterDescriptor
import org.jetbrains.kotlin.descriptors.SyntheticPropertyDescriptor
import org.jetbrains.kotlin.descriptors.hasBody
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
internal class KtFe10DescPropertySetterSymbol(
override val descriptor: PropertySetterDescriptor,
override val analysisContext: Fe10AnalysisContext
) : KtPropertySetterSymbol(), KtFe10DescMemberSymbol<PropertySetterDescriptor> {
override val annotatedType: KtTypeAndAnnotations
get() = withValidityAssertion { descriptor.builtIns.unitType.toKtTypeAndAnnotations(analysisContext) }
get() = withValidityAssertion { analysisContext.builtIns.unitType.toKtTypeAndAnnotations(analysisContext) }
override val isDefault: Boolean
get() = withValidityAssertion { descriptor.isDefault }
@@ -81,8 +81,7 @@ class KtFe10PsiDefaultPropertySetterSymbol(
override val annotatedType: KtTypeAndAnnotations
get() = withValidityAssertion {
val builtIns = analysisContext.resolveSession.moduleDescriptor.builtIns
return builtIns.unitType.toKtTypeAndAnnotations(analysisContext)
return analysisContext.builtIns.unitType.toKtTypeAndAnnotations(analysisContext)
}
override val receiverType: KtTypeAndAnnotations?
@@ -68,7 +68,7 @@ internal class KtFe10Renderer(
private val typeRenderer = KtFe10TypeRenderer(options.typeRendererOptions, isDebugText)
private val typeApproximator = TypeApproximator(
analysisContext.resolveSession.moduleDescriptor.builtIns,
analysisContext.builtIns,
analysisContext.resolveSession.languageVersionSettings
)
@@ -96,7 +96,7 @@ internal class KtFe10Renderer(
if (shouldApproximate) {
val approximatedType = typeApproximator.approximateToSuperType(type.unwrap(), PublicApproximatorConfiguration)
?: type.takeIf { it.constructor.declarationDescriptor?.name != SpecialNames.NO_NAME_PROVIDED }
?: analysisContext.resolveSession.moduleDescriptor.builtIns.anyType
?: analysisContext.builtIns.anyType
renderType(approximatedType, shouldApproximate = false)
return