FIR IDE: add APIs about FunctionClassKind
This commit is contained in:
committed by
Ilya Kirillov
parent
d029e84b96
commit
6d150fb7a5
+8
-1
@@ -11,6 +11,8 @@ 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.KtType
|
||||
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionClassKind
|
||||
import org.jetbrains.kotlin.builtins.getFunctionalClassKind
|
||||
import org.jetbrains.kotlin.load.java.sam.JavaSingleAbstractMethodUtils
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
|
||||
@@ -23,8 +25,13 @@ internal class KtFe10TypeInfoProvider(override val analysisSession: KtFe10Analys
|
||||
return JavaSingleAbstractMethodUtils.isSamType(type.type)
|
||||
}
|
||||
|
||||
override fun getFunctionClassKind(type: KtType): FunctionClassKind? {
|
||||
require(type is KtFe10Type)
|
||||
return type.type.constructor.declarationDescriptor?.getFunctionalClassKind()
|
||||
}
|
||||
|
||||
override fun canBeNull(type: KtType): Boolean = withValidityAssertion {
|
||||
require(type is KtFe10Type)
|
||||
return TypeUtils.isNullableType(type.type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+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
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.KtFe10FrontendApiTestConfiguratorService
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.AbstractFunctionClassKindTest
|
||||
|
||||
abstract class AbstractKtFe10FunctionClassKindTest : AbstractFunctionClassKindTest(KtFe10FrontendApiTestConfiguratorService)
|
||||
+7
@@ -13,6 +13,8 @@ import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.fir.types.KtFirType
|
||||
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtType
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionClassKind
|
||||
import org.jetbrains.kotlin.fir.types.functionClassKind
|
||||
|
||||
internal class KtFirTypeInfoProvider(
|
||||
override val analysisSession: KtFirAnalysisSession,
|
||||
@@ -25,5 +27,10 @@ internal class KtFirTypeInfoProvider(
|
||||
return samResolver.getFunctionTypeForPossibleSamType(coneType) != null
|
||||
}
|
||||
|
||||
override fun getFunctionClassKind(type: KtType): FunctionClassKind? {
|
||||
val coneType = (type as KtFirType).coneType
|
||||
return coneType.functionClassKind(analysisSession.rootModuleSession)
|
||||
}
|
||||
|
||||
override fun canBeNull(type: KtType): Boolean = (type as KtFirType).coneType.canBeNull
|
||||
}
|
||||
|
||||
+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
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.fir.FirFrontendApiTestConfiguratorService
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.AbstractFunctionClassKindTest
|
||||
|
||||
abstract class AbstractFirFunctionClassKindTest : AbstractFunctionClassKindTest(FirFrontendApiTestConfiguratorService)
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
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/functionClassKind")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class FirFunctionClassKindTestGenerated extends AbstractFirFunctionClassKindTest {
|
||||
@Test
|
||||
public void testAllFilesPresentInFunctionClassKind() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/functionClassKind"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("function.kt")
|
||||
public void testFunction() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/functionClassKind/function.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kFunction.kt")
|
||||
public void testKFunction() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/functionClassKind/kFunction.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("kSuspendFunction.kt")
|
||||
public void testKSuspendFunction() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/functionClassKind/kSuspendFunction.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("suspendFunction.kt")
|
||||
public void testSuspendFunction() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/functionClassKind/suspendFunction.kt");
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
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.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.test.model.TestModule
|
||||
import org.jetbrains.kotlin.test.services.TestServices
|
||||
import org.jetbrains.kotlin.test.services.assertions
|
||||
|
||||
abstract class AbstractFunctionClassKindTest (
|
||||
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 KtExpression
|
||||
|
||||
val (type, functionClassKind) = executeOnPooledThreadInReadAction {
|
||||
analyseForTest(expressionAtCaret) {
|
||||
val functionType = expressionAtCaret.getExpectedType()
|
||||
functionType?.render() to functionType?.functionClassKind
|
||||
}
|
||||
}
|
||||
val actual = buildString {
|
||||
appendLine("expression: ${expressionAtCaret.text}")
|
||||
appendLine("expected type: $type")
|
||||
appendLine("functionClassKind: $functionClassKind")
|
||||
}
|
||||
testServices.assertions.assertEqualsToFile(testDataFileSibling(".txt"), actual)
|
||||
}
|
||||
}
|
||||
+20
@@ -12,10 +12,12 @@ import org.jetbrains.kotlin.analysis.api.types.KtNonErrorClassType
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtType
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtTypeNullability
|
||||
import org.jetbrains.kotlin.builtins.StandardNames
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionClassKind
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
public abstract class KtTypeInfoProvider : KtAnalysisSessionComponent() {
|
||||
public abstract fun isFunctionalInterfaceType(type: KtType): Boolean
|
||||
public abstract fun getFunctionClassKind(type: KtType): FunctionClassKind?
|
||||
public abstract fun canBeNull(type: KtType): Boolean
|
||||
}
|
||||
|
||||
@@ -26,6 +28,24 @@ public interface KtTypeInfoProviderMixIn : KtAnalysisSessionMixIn {
|
||||
public val KtType.isFunctionalInterfaceType: Boolean
|
||||
get() = analysisSession.typeInfoProvider.isFunctionalInterfaceType(this)
|
||||
|
||||
/**
|
||||
* Returns [FunctionClassKind] of the given [KtType]
|
||||
*/
|
||||
public val KtType.functionClassKind: FunctionClassKind?
|
||||
get() = analysisSession.typeInfoProvider.getFunctionClassKind(this)
|
||||
|
||||
public val KtType.isFunctionType: Boolean
|
||||
get() = functionClassKind == FunctionClassKind.Function
|
||||
|
||||
public val KtType.isKFunctionType: Boolean
|
||||
get() = functionClassKind == FunctionClassKind.KFunction
|
||||
|
||||
public val KtType.isSuspendFunctionType: Boolean
|
||||
get() = functionClassKind == FunctionClassKind.SuspendFunction
|
||||
|
||||
public val KtType.isKSuspendFunctionType: Boolean
|
||||
get() = functionClassKind == FunctionClassKind.KSuspendFunction
|
||||
|
||||
/**
|
||||
* Returns true if a public value of this type can potentially be null. This means this type is not a subtype of [Any]. However, it does not
|
||||
* mean one can assign `null` to a variable of this type because it may be unknown if this type can accept `null`. For example, a public value
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
fun <T, R> foo(p: T, mapper : (T) -> R): R {
|
||||
mapper(p)
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
foo(1, x<caret>y)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
expression: xy
|
||||
expected type: (T) -> R
|
||||
functionClassKind: Function
|
||||
@@ -0,0 +1,11 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.KFunction1
|
||||
|
||||
fun <T, R> foo(p: T, mapper : (T) -> R): R {
|
||||
mapper(p)
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
foo(1, x<caret>y as KFunction1<Int, String>)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
expression: xy
|
||||
expected type: (kotlin.Int) -> kotlin.String
|
||||
functionClassKind: KFunction
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.KSuspendFunction0
|
||||
|
||||
fun foo(pause : suspend () -> Unit) {
|
||||
pause()
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
foo(x<caret>y as KSuspendFunction0<Unit>)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
expression: xy
|
||||
expected type: suspend () -> kotlin.Unit
|
||||
functionClassKind: KSuspendFunction
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fun foo(pause : suspend () -> Unit) {
|
||||
pause()
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
foo(x<caret>y)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
expression: xy
|
||||
expected type: suspend () -> kotlin.Unit
|
||||
functionClassKind: SuspendFunction
|
||||
+8
@@ -73,6 +73,10 @@ fun main(args: Array<String>) {
|
||||
model("components/expectedExpressionType")
|
||||
}
|
||||
|
||||
testClass<AbstractFirFunctionClassKindTest> {
|
||||
model("components/functionClassKind")
|
||||
}
|
||||
|
||||
testClass<AbstractFirOverriddenDeclarationProviderTest> {
|
||||
model("components/overriddenDeclarations")
|
||||
}
|
||||
@@ -147,6 +151,10 @@ fun main(args: Array<String>) {
|
||||
model("components/expectedExpressionType")
|
||||
}
|
||||
|
||||
// testClass<AbstractKtFe10FunctionClassKindTest> {
|
||||
// model("components/functionClassKind")
|
||||
// }
|
||||
|
||||
testClass<AbstractKtFe10OverriddenDeclarationProviderTest> {
|
||||
model("components/overriddenDeclarations")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user