FIR IDE: introduce builtin types container

This commit is contained in:
Ilya Kirillov
2020-12-13 12:18:05 +01:00
parent 835577383b
commit c2d83353e8
3 changed files with 74 additions and 0 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.idea.frontend.api.components
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.frontend.api.ValidityTokenOwner
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtExpression
@@ -21,4 +22,28 @@ abstract class KtTypeProvider : KtAnalysisSessionComponent() {
abstract fun isBuiltinFunctionalType(type: KtType): Boolean
abstract fun getExpectedType(expression: PsiElement): KtType?
abstract val builtinTypes: KtBuiltinTypes
}
@Suppress("PropertyName")
abstract class KtBuiltinTypes : ValidityTokenOwner {
abstract val INT: KtType
abstract val LONG: KtType
abstract val SHORT: KtType
abstract val BYTE: KtType
abstract val FLOAT: KtType
abstract val DOUBLE: KtType
abstract val BOOLEAN: KtType
abstract val CHAR: KtType
abstract val STRING: KtType
abstract val UNIT: KtType
abstract val NOTHING: KtType
abstract val ANY: KtType
abstract val NULLABLE_ANY: KtType
abstract val NULLABLE_NOTHING: KtType
}
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.idea.frontend.api.fir.components
import org.jetbrains.kotlin.fir.BuiltinTypes
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.fir.types.impl.FirImplicitBuiltinTypeRef
import org.jetbrains.kotlin.idea.frontend.api.ValidityToken
import org.jetbrains.kotlin.idea.frontend.api.components.KtBuiltinTypes
import org.jetbrains.kotlin.idea.frontend.api.fir.KtSymbolByFirBuilder
import org.jetbrains.kotlin.idea.frontend.api.fir.types.KtFirClassType
import org.jetbrains.kotlin.idea.frontend.api.fir.utils.cached
import org.jetbrains.kotlin.idea.frontend.api.fir.utils.weakRef
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
internal class KtFirBuiltInTypes(builtinTypes: BuiltinTypes, builder: KtSymbolByFirBuilder, override val token: ValidityToken) : KtBuiltinTypes() {
private val builder by weakRef(builder)
override val INT: KtType by cachedBuiltin(builtinTypes.intType)
override val LONG: KtType by cachedBuiltin(builtinTypes.longType)
override val SHORT: KtType by cachedBuiltin(builtinTypes.shortType)
override val BYTE: KtType by cachedBuiltin(builtinTypes.byteType)
override val FLOAT: KtType by cachedBuiltin(builtinTypes.floatType)
override val DOUBLE: KtType by cachedBuiltin(builtinTypes.doubleType)
override val CHAR: KtType by cachedBuiltin(builtinTypes.charType)
override val BOOLEAN: KtType by cachedBuiltin(builtinTypes.booleanType)
override val STRING: KtType by cachedBuiltin(builtinTypes.stringType)
override val UNIT: KtType by cachedBuiltin(builtinTypes.unitType)
override val NOTHING: KtType by cachedBuiltin(builtinTypes.nothingType)
override val ANY: KtType by cachedBuiltin(builtinTypes.anyType)
override val NULLABLE_ANY: KtType by cachedBuiltin(builtinTypes.nullableAnyType)
override val NULLABLE_NOTHING: KtType by cachedBuiltin(builtinTypes.nullableNothingType)
private fun cachedBuiltin(builtinTypeRef: FirImplicitBuiltinTypeRef) = cached {
KtFirClassType(builtinTypeRef.type as ConeClassLikeTypeImpl, token, builder) // TODO builder leaking
}
}
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.fir.types.coneType
import org.jetbrains.kotlin.idea.fir.low.level.api.api.getOrBuildFirOfType
import org.jetbrains.kotlin.idea.frontend.api.ValidityToken
import org.jetbrains.kotlin.idea.frontend.api.assertIsValid
import org.jetbrains.kotlin.idea.frontend.api.components.KtBuiltinTypes
import org.jetbrains.kotlin.idea.frontend.api.components.KtTypeProvider
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession
import org.jetbrains.kotlin.idea.frontend.api.fir.types.KtFirType
@@ -85,6 +86,9 @@ internal class KtFirTypeProvider(
type.coneType.isBuiltinFunctionalType(analysisSession.firResolveState.rootModuleSession) //TODO use correct session here
}
override val builtinTypes: KtBuiltinTypes =
KtFirBuiltInTypes(analysisSession.firResolveState.rootModuleSession.builtinTypes, analysisSession.firSymbolBuilder, token)
private fun createTypeCheckerContext() = ConeTypeCheckerContext(
isErrorTypeEqualsToAnything = true,
isStubTypeEqualsToAnything = true,