FIR IDE: add API to get KtType from KtTypeParameter
This commit is contained in:
committed by
Ilya Kirillov
parent
d91170ed2c
commit
205866a516
+18
-2
@@ -9,16 +9,21 @@ import org.jetbrains.kotlin.analysis.api.KtStarProjectionTypeArgument
|
||||
import org.jetbrains.kotlin.analysis.api.KtTypeArgumentWithVariance
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtClassTypeBuilder
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtTypeCreator
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtTypeParameterTypeBuilder
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.components.base.Fe10KtAnalysisSessionComponent
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.getSymbolDescriptor
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtType
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.types.KtFe10ClassErrorType
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.types.KtFe10UsualClassType
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.types.base.KtFe10Type
|
||||
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtClassType
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtTypeNullability
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtTypeParameterType
|
||||
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
@@ -29,7 +34,7 @@ internal class KtFe10TypeCreator(
|
||||
override val token: ValidityToken
|
||||
get() = analysisSession.token
|
||||
|
||||
override fun buildClassType(builder: KtClassTypeBuilder): KtClassType {
|
||||
override fun buildClassType(builder: KtClassTypeBuilder): KtClassType = withValidityAssertion {
|
||||
val descriptor: ClassDescriptor? = when (builder) {
|
||||
is KtClassTypeBuilder.ByClassId -> {
|
||||
val fqName = builder.classId.asSingleFqName()
|
||||
@@ -64,4 +69,15 @@ internal class KtFe10TypeCreator(
|
||||
val typeWithNullability = TypeUtils.makeNullableAsSpecified(type, builder.nullability == KtTypeNullability.NULLABLE)
|
||||
return KtFe10UsualClassType(typeWithNullability as SimpleType, descriptor, analysisContext)
|
||||
}
|
||||
}
|
||||
|
||||
override fun buildTypeParameterType(builder: KtTypeParameterTypeBuilder): KtTypeParameterType = withValidityAssertion {
|
||||
val descriptor = when (builder) {
|
||||
is KtTypeParameterTypeBuilder.BySymbol -> {
|
||||
getSymbolDescriptor(builder.symbol) as? TypeParameterDescriptor
|
||||
}
|
||||
}
|
||||
val kotlinType = descriptor?.defaultType
|
||||
?: ErrorUtils.createErrorType("Cannot build type parameter type, descriptor not found for builder $builder")
|
||||
return kotlinType.toKtType(analysisContext) as KtTypeParameterType
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -467,4 +467,4 @@ private class KtFe10BuiltinTypes(private val analysisContext: Fe10AnalysisContex
|
||||
override val NULLABLE_NOTHING: KtType
|
||||
get() = withValidityAssertion { analysisContext.builtIns.nullableNothingType.toKtType(analysisContext) }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.analysis.api.descriptors.test.components.typeCreator
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.KtFe10FrontendApiTestConfiguratorService
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.typeCreator.AbstractTypeParameterTypeTest
|
||||
|
||||
abstract class AbstractKtFe10TypeParameterTypeTest : AbstractTypeParameterTypeTest(KtFe10FrontendApiTestConfiguratorService)
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.analysis.api.descriptors.test.components.typeCreator;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("analysis/analysis-api/testData/components/typeCreator/typeParameter")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class KtFe10TypeParameterTypeTestGenerated extends AbstractKtFe10TypeParameterTypeTest {
|
||||
@Test
|
||||
public void testAllFilesPresentInTypeParameter() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/typeCreator/typeParameter"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleBounds.kt")
|
||||
public void testMultipleBounds() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/typeCreator/typeParameter/multipleBounds.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("regular.kt")
|
||||
public void testRegular() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/typeCreator/typeParameter/regular.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("reified.kt")
|
||||
public void testReified() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/typeCreator/typeParameter/reified.kt");
|
||||
}
|
||||
}
|
||||
+15
@@ -7,14 +7,18 @@ package org.jetbrains.kotlin.analysis.api.fir.components
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtClassTypeBuilder
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtTypeCreator
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtTypeParameterTypeBuilder
|
||||
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.fir.symbols.KtFirSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtClassType
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtTypeParameterType
|
||||
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
|
||||
import org.jetbrains.kotlin.fir.declarations.FirClassLikeDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirTypeParameter
|
||||
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnresolvedSymbolError
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.toConeType
|
||||
import org.jetbrains.kotlin.fir.types.ConeClassErrorType
|
||||
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
|
||||
import org.jetbrains.kotlin.fir.types.typeContext
|
||||
@@ -47,4 +51,15 @@ internal class KtFirTypeCreator(
|
||||
|
||||
return coneType.asKtType() as KtClassType
|
||||
}
|
||||
|
||||
override fun buildTypeParameterType(builder: KtTypeParameterTypeBuilder): KtTypeParameterType = withValidityAssertion {
|
||||
val coneType = when (builder) {
|
||||
is KtTypeParameterTypeBuilder.BySymbol -> {
|
||||
val symbol = builder.symbol
|
||||
check(symbol is KtFirSymbol<*>)
|
||||
symbol.firRef.withFir { (it as FirTypeParameter).toConeType() }
|
||||
}
|
||||
}
|
||||
return coneType.asKtType() as KtTypeParameterType
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.analysis.api.fir.components.typeCreator
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.fir.FirFrontendApiTestConfiguratorService
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.typeCreator.AbstractTypeParameterTypeTest
|
||||
|
||||
abstract class AbstractFirTypeParameterTypeTest : AbstractTypeParameterTypeTest(FirFrontendApiTestConfiguratorService)
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.analysis.api.fir.components.typeCreator;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("analysis/analysis-api/testData/components/typeCreator/typeParameter")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class FirTypeParameterTypeTestGenerated extends AbstractFirTypeParameterTypeTest {
|
||||
@Test
|
||||
public void testAllFilesPresentInTypeParameter() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/typeCreator/typeParameter"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("multipleBounds.kt")
|
||||
public void testMultipleBounds() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/typeCreator/typeParameter/multipleBounds.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("regular.kt")
|
||||
public void testRegular() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/typeCreator/typeParameter/regular.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("reified.kt")
|
||||
public void testReified() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/typeCreator/typeParameter/reified.kt");
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.analysis.api.impl.base.test.components.typeCreator
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.components.buildTypeParameterType
|
||||
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService
|
||||
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.expressionMarkerProvider
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.test.framework.AbstractHLApiSingleFileTest
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtTypeParameter
|
||||
import org.jetbrains.kotlin.test.model.TestModule
|
||||
import org.jetbrains.kotlin.test.services.TestServices
|
||||
import org.jetbrains.kotlin.test.services.assertions
|
||||
|
||||
abstract class AbstractTypeParameterTypeTest(
|
||||
configurator: FrontendApiTestConfiguratorService
|
||||
) : AbstractHLApiSingleFileTest(configurator) {
|
||||
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
|
||||
super.doTestByFileStructure(ktFile, module, testServices)
|
||||
|
||||
val expressionAtCaret = testServices.expressionMarkerProvider.getElementOfTypAtCaret(ktFile) as KtTypeParameter
|
||||
|
||||
val actual = analyseForTest(expressionAtCaret) {
|
||||
val symbol = expressionAtCaret.getTypeParameterSymbol()
|
||||
val ktType = buildTypeParameterType(symbol)
|
||||
buildString {
|
||||
appendLine("expression: ${expressionAtCaret.text}")
|
||||
appendLine("ktType: ${ktType.render()}")
|
||||
}
|
||||
}
|
||||
|
||||
testServices.assertions.assertEqualsToTestDataFileSibling(actual)
|
||||
}
|
||||
}
|
||||
+15
-1
@@ -8,19 +8,22 @@ package org.jetbrains.kotlin.analysis.api.components
|
||||
import org.jetbrains.kotlin.analysis.api.KtTypeArgument
|
||||
import org.jetbrains.kotlin.analysis.api.KtTypeArgumentWithVariance
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtTypeParameterSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtClassType
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtType
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtTypeNullability
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtTypeParameterType
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
|
||||
public abstract class KtTypeCreator : KtAnalysisSessionComponent() {
|
||||
public abstract fun buildClassType(builder: KtClassTypeBuilder): KtClassType
|
||||
|
||||
public abstract fun buildTypeParameterType(builder: KtTypeParameterTypeBuilder): KtTypeParameterType
|
||||
}
|
||||
|
||||
public interface KtTypeCreatorMixIn : KtAnalysisSessionMixIn
|
||||
|
||||
|
||||
public inline fun KtTypeCreatorMixIn.buildClassType(
|
||||
classId: ClassId,
|
||||
build: KtClassTypeBuilder.() -> Unit = {}
|
||||
@@ -33,6 +36,11 @@ public inline fun KtTypeCreatorMixIn.buildClassType(
|
||||
): KtClassType =
|
||||
analysisSession.typesCreator.buildClassType(KtClassTypeBuilder.BySymbol(symbol).apply(build))
|
||||
|
||||
public inline fun KtTypeCreatorMixIn.buildTypeParameterType(
|
||||
symbol: KtTypeParameterSymbol,
|
||||
build: KtTypeParameterTypeBuilder.() -> Unit = {}
|
||||
): KtTypeParameterType =
|
||||
analysisSession.typesCreator.buildTypeParameterType(KtTypeParameterTypeBuilder.BySymbol(symbol).apply(build))
|
||||
|
||||
public sealed class KtTypeBuilder
|
||||
|
||||
@@ -55,3 +63,9 @@ public sealed class KtClassTypeBuilder : KtTypeBuilder() {
|
||||
public class BySymbol(public val symbol: KtClassOrObjectSymbol) : KtClassTypeBuilder()
|
||||
}
|
||||
|
||||
public sealed class KtTypeParameterTypeBuilder : KtTypeBuilder() {
|
||||
public var nullability: KtTypeNullability = KtTypeNullability.NULLABLE
|
||||
|
||||
public class BySymbol(public val symbol: KtTypeParameterSymbol) : KtTypeParameterTypeBuilder()
|
||||
}
|
||||
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
fun <<caret>T> function(t: T) where T : Int, T : Long = TODO()
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
expression: T
|
||||
ktType: T
|
||||
+1
@@ -0,0 +1 @@
|
||||
fun <<caret>T> function(t: T) = TODO()
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
expression: T
|
||||
ktType: T
|
||||
+1
@@ -0,0 +1 @@
|
||||
inline fun <reified <caret>T> function(t: T) = TODO()
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
expression: reified T
|
||||
ktType: T
|
||||
+8
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.analysis.api.descriptors.test.components.expressionT
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.smartCastProvider.AbstractKtFe10HLSmartCastInfoTest
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.symbolDeclarationOverridesProvider.AbstractKtFe10OverriddenDeclarationProviderTest
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.symbolDeclarationRenderer.AbstractKtFe10RendererTest
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.typeCreator.AbstractKtFe10TypeParameterTypeTest
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.typeProvider.AbstractKtFe10HasCommonSubtypeTest
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.components.compileTimeConstantProvider.AbstractKtFe10CompileTimeConstantEvaluatorTest
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.symbols.AbstractKtFe10SymbolByFqNameTest
|
||||
@@ -29,6 +30,7 @@ import org.jetbrains.kotlin.analysis.api.fir.components.psiTypeProvider.Abstract
|
||||
import org.jetbrains.kotlin.analysis.api.fir.components.smartCastProvider.AbstractFirHLSmartCastInfoTest
|
||||
import org.jetbrains.kotlin.analysis.api.fir.components.symbolDeclarationOverridesProvider.AbstractFirOverriddenDeclarationProviderTest
|
||||
import org.jetbrains.kotlin.analysis.api.fir.components.symbolDeclarationRenderer.AbstractFirRendererTest
|
||||
import org.jetbrains.kotlin.analysis.api.fir.components.typeCreator.AbstractFirTypeParameterTypeTest
|
||||
import org.jetbrains.kotlin.analysis.api.fir.components.typeInfoProvider.AbstractFirFunctionClassKindTest
|
||||
import org.jetbrains.kotlin.analysis.api.fir.components.typeProvider.AbstractFirGetSuperTypesTest
|
||||
import org.jetbrains.kotlin.analysis.api.fir.components.typeProvider.AbstractFirHasCommonSubtypeTest
|
||||
@@ -183,6 +185,12 @@ private fun TestGroupSuite.generateAnalysisApiComponentsTests() {
|
||||
}
|
||||
}
|
||||
|
||||
component("typeCreator") {
|
||||
test(fir = AbstractFirTypeParameterTypeTest::class, fe10 = AbstractKtFe10TypeParameterTypeTest::class) {
|
||||
model("typeParameter")
|
||||
}
|
||||
}
|
||||
|
||||
component("typeInfoProvider") {
|
||||
test(fir = AbstractFirFunctionClassKindTest::class, fe10 = null) {
|
||||
model("functionClassKind")
|
||||
|
||||
Reference in New Issue
Block a user