AA: introduce KtConstantEvaluationMode
This commit is contained in:
committed by
Ilya Kirillov
parent
ee23a52e54
commit
606033e1e6
+20
-2
@@ -9,10 +9,13 @@ import org.jetbrains.kotlin.analysis.api.components.KtCompileTimeConstantProvide
|
|||||||
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
|
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.components.base.Fe10KtAnalysisSessionComponent
|
||||||
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtConstantValue
|
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtConstantValue
|
||||||
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationValue
|
|
||||||
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
|
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
|
||||||
|
import org.jetbrains.kotlin.analysis.api.components.KtConstantEvaluationMode
|
||||||
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
||||||
|
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||||
import org.jetbrains.kotlin.psi.KtExpression
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
|
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
|
||||||
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||||
import org.jetbrains.kotlin.types.TypeUtils
|
import org.jetbrains.kotlin.types.TypeUtils
|
||||||
|
|
||||||
@@ -22,8 +25,23 @@ internal class KtFe10CompileTimeConstantProvider(
|
|||||||
override val token: ValidityToken
|
override val token: ValidityToken
|
||||||
get() = analysisSession.token
|
get() = analysisSession.token
|
||||||
|
|
||||||
override fun evaluate(expression: KtExpression): KtConstantValue? {
|
override fun evaluate(
|
||||||
|
expression: KtExpression,
|
||||||
|
mode: KtConstantEvaluationMode,
|
||||||
|
): KtConstantValue? {
|
||||||
val bindingContext = analysisContext.analyze(expression)
|
val bindingContext = analysisContext.analyze(expression)
|
||||||
|
|
||||||
|
if (mode == KtConstantEvaluationMode.CONSTANT_EXPRESSION_EVALUATION) {
|
||||||
|
// TODO: how to _not_ evaluate composite expression with a non-const property?
|
||||||
|
// TODO: how to _not_ evaluate expressions with a compilation error, e.g., uninitialized property access
|
||||||
|
when (expression) {
|
||||||
|
is KtNameReferenceExpression -> {
|
||||||
|
val reference = bindingContext[BindingContext.REFERENCE_TARGET, expression]
|
||||||
|
if (reference is PropertyDescriptor && !reference.isConst) return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val constant = ConstantExpressionEvaluator.getPossiblyErrorConstant(expression, bindingContext)
|
val constant = ConstantExpressionEvaluator.getPossiblyErrorConstant(expression, bindingContext)
|
||||||
return constant?.toConstantValue(TypeUtils.NO_EXPECTED_TYPE)?.toKtConstantValue()
|
return constant?.toConstantValue(TypeUtils.NO_EXPECTED_TYPE)?.toKtConstantValue()
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-20
@@ -10,7 +10,7 @@ import org.jetbrains.kotlin.test.util.KtTestUtil;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService;
|
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService;
|
||||||
import org.jetbrains.kotlin.analysis.api.descriptors.test.KtFe10FrontendApiTestConfiguratorService;
|
import org.jetbrains.kotlin.analysis.api.descriptors.test.KtFe10FrontendApiTestConfiguratorService;
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorTest;
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorConstantLikeTest;
|
||||||
import org.jetbrains.kotlin.test.TestMetadata;
|
import org.jetbrains.kotlin.test.TestMetadata;
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -20,9 +20,9 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */
|
/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@TestMetadata("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate")
|
@TestMetadata("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
public class Fe10CompileTimeConstantEvaluatorTestGenerated extends AbstractCompileTimeConstantEvaluatorTest {
|
public class Fe10CompileTimeConstantEvaluatorConstantLikeTestGenerated extends AbstractCompileTimeConstantEvaluatorConstantLikeTest {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public FrontendApiTestConfiguratorService getConfigurator() {
|
public FrontendApiTestConfiguratorService getConfigurator() {
|
||||||
@@ -30,97 +30,109 @@ public class Fe10CompileTimeConstantEvaluatorTestGenerated extends AbstractCompi
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAllFilesPresentInEvaluate() throws Exception {
|
public void testAllFilesPresentInEvaluate_constantLike() throws Exception {
|
||||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("binaryExpressionWithString.kt")
|
@TestMetadata("binaryExpressionWithString.kt")
|
||||||
public void testBinaryExpressionWithString() throws Exception {
|
public void testBinaryExpressionWithString() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/binaryExpressionWithString.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/binaryExpressionWithString.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("namedReference_const.kt")
|
@TestMetadata("namedReference_const.kt")
|
||||||
public void testNamedReference_const() throws Exception {
|
public void testNamedReference_const() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/namedReference_const.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/namedReference_const.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("namedReference_val.kt")
|
@TestMetadata("namedReference_val.kt")
|
||||||
public void testNamedReference_val() throws Exception {
|
public void testNamedReference_val() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/namedReference_val.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/namedReference_val.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("namedReference_var.kt")
|
@TestMetadata("namedReference_var.kt")
|
||||||
public void testNamedReference_var() throws Exception {
|
public void testNamedReference_var() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/namedReference_var.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/namedReference_var.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInCompanionObject.kt")
|
@TestMetadata("propertyInCompanionObject.kt")
|
||||||
public void testPropertyInCompanionObject() throws Exception {
|
public void testPropertyInCompanionObject() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInCompanionObject.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInCompanionObject.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInCompanionObject_indirect.kt")
|
@TestMetadata("propertyInCompanionObject_indirect.kt")
|
||||||
public void testPropertyInCompanionObject_indirect() throws Exception {
|
public void testPropertyInCompanionObject_indirect() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInCompanionObject_indirect.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInCompanionObject_indirect.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInCompanionObject_indirect_twice.kt")
|
@TestMetadata("propertyInCompanionObject_indirect_twice.kt")
|
||||||
public void testPropertyInCompanionObject_indirect_twice() throws Exception {
|
public void testPropertyInCompanionObject_indirect_twice() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInCompanionObject_indirect_twice.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInCompanionObject_indirect_twice.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Byte.kt")
|
@TestMetadata("propertyInit_Byte.kt")
|
||||||
public void testPropertyInit_Byte() throws Exception {
|
public void testPropertyInit_Byte() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Byte.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Byte.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("propertyInit_DivByOtherProperty_const.kt")
|
||||||
|
public void testPropertyInit_DivByOtherProperty_const() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_DivByOtherProperty_const.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("propertyInit_DivByOtherProperty_val.kt")
|
||||||
|
public void testPropertyInit_DivByOtherProperty_val() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_DivByOtherProperty_val.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_DivByZero.kt")
|
@TestMetadata("propertyInit_DivByZero.kt")
|
||||||
public void testPropertyInit_DivByZero() throws Exception {
|
public void testPropertyInit_DivByZero() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_DivByZero.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_DivByZero.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Double.kt")
|
@TestMetadata("propertyInit_Double.kt")
|
||||||
public void testPropertyInit_Double() throws Exception {
|
public void testPropertyInit_Double() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Double.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Double.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Float.kt")
|
@TestMetadata("propertyInit_Float.kt")
|
||||||
public void testPropertyInit_Float() throws Exception {
|
public void testPropertyInit_Float() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Float.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Float.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Int.kt")
|
@TestMetadata("propertyInit_Int.kt")
|
||||||
public void testPropertyInit_Int() throws Exception {
|
public void testPropertyInit_Int() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Int.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Int.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Long.kt")
|
@TestMetadata("propertyInit_Long.kt")
|
||||||
public void testPropertyInit_Long() throws Exception {
|
public void testPropertyInit_Long() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Long.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Long.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_UInt.kt")
|
@TestMetadata("propertyInit_UInt.kt")
|
||||||
public void testPropertyInit_UInt() throws Exception {
|
public void testPropertyInit_UInt() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_UInt.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_UInt.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("stringLiteral.kt")
|
@TestMetadata("stringLiteral.kt")
|
||||||
public void testStringLiteral() throws Exception {
|
public void testStringLiteral() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/stringLiteral.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/stringLiteral.kt");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+60
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.fe10.components.compileTimeConstantProvider;
|
||||||
|
|
||||||
|
import com.intellij.testFramework.TestDataPath;
|
||||||
|
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService;
|
||||||
|
import org.jetbrains.kotlin.analysis.api.descriptors.test.KtFe10FrontendApiTestConfiguratorService;
|
||||||
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorConstantTest;
|
||||||
|
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/compileTimeConstantProvider/evaluate_constant")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
public class Fe10CompileTimeConstantEvaluatorConstantTestGenerated extends AbstractCompileTimeConstantEvaluatorConstantTest {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public FrontendApiTestConfiguratorService getConfigurator() {
|
||||||
|
return KtFe10FrontendApiTestConfiguratorService.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAllFilesPresentInEvaluate_constant() throws Exception {
|
||||||
|
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("namedReference_const.kt")
|
||||||
|
public void testNamedReference_const() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant/namedReference_const.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("namedReference_val.kt")
|
||||||
|
public void testNamedReference_val() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant/namedReference_val.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("propertyInit_DivByOtherProperty_const.kt")
|
||||||
|
public void testPropertyInit_DivByOtherProperty_const() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant/propertyInit_DivByOtherProperty_const.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("propertyInit_DivByOtherProperty_val.kt")
|
||||||
|
public void testPropertyInit_DivByOtherProperty_val() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant/propertyInit_DivByOtherProperty_val.kt");
|
||||||
|
}
|
||||||
|
}
|
||||||
+25
-9
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.analysis.api.fir.components
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
|
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
|
||||||
import org.jetbrains.kotlin.analysis.api.components.KtCompileTimeConstantProvider
|
import org.jetbrains.kotlin.analysis.api.components.KtCompileTimeConstantProvider
|
||||||
|
import org.jetbrains.kotlin.analysis.api.components.KtConstantEvaluationMode
|
||||||
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
|
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
|
||||||
import org.jetbrains.kotlin.analysis.api.fir.evaluate.FirCompileTimeConstantEvaluator
|
import org.jetbrains.kotlin.analysis.api.fir.evaluate.FirCompileTimeConstantEvaluator
|
||||||
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
||||||
@@ -15,6 +16,7 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getOrBuildFir
|
|||||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.throwUnexpectedFirElementError
|
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.throwUnexpectedFirElementError
|
||||||
import org.jetbrains.kotlin.fir.FirElement
|
import org.jetbrains.kotlin.fir.FirElement
|
||||||
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
|
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
|
||||||
|
import org.jetbrains.kotlin.fir.declarations.utils.isConst
|
||||||
import org.jetbrains.kotlin.fir.declarations.utils.referredPropertySymbol
|
import org.jetbrains.kotlin.fir.declarations.utils.referredPropertySymbol
|
||||||
import org.jetbrains.kotlin.fir.expressions.FirExpression
|
import org.jetbrains.kotlin.fir.expressions.FirExpression
|
||||||
import org.jetbrains.kotlin.fir.expressions.FirPropertyAccessExpression
|
import org.jetbrains.kotlin.fir.expressions.FirPropertyAccessExpression
|
||||||
@@ -30,14 +32,21 @@ internal class KtFirCompileTimeConstantProvider(
|
|||||||
override val token: ValidityToken,
|
override val token: ValidityToken,
|
||||||
) : KtCompileTimeConstantProvider(), KtFirAnalysisSessionComponent {
|
) : KtCompileTimeConstantProvider(), KtFirAnalysisSessionComponent {
|
||||||
|
|
||||||
override fun evaluate(expression: KtExpression): KtConstantValue? = withValidityAssertion {
|
override fun evaluate(
|
||||||
evaluateFir(expression.getOrBuildFir(firResolveState), expression)
|
expression: KtExpression,
|
||||||
|
mode: KtConstantEvaluationMode,
|
||||||
|
): KtConstantValue? = withValidityAssertion {
|
||||||
|
evaluateFir(expression.getOrBuildFir(firResolveState), expression, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun evaluateFir(fir: FirElement?, context: KtExpression): KtConstantValue? = withValidityAssertion {
|
private fun evaluateFir(
|
||||||
|
fir: FirElement?,
|
||||||
|
context: KtExpression,
|
||||||
|
mode: KtConstantEvaluationMode,
|
||||||
|
): KtConstantValue? = withValidityAssertion {
|
||||||
when (fir) {
|
when (fir) {
|
||||||
is FirPropertyAccessExpression -> {
|
is FirPropertyAccessExpression -> {
|
||||||
fir.referredPropertySymbol?.toKtConstantValue(context)
|
fir.referredPropertySymbol?.toKtConstantValue(context, mode)
|
||||||
}
|
}
|
||||||
is FirExpression -> {
|
is FirExpression -> {
|
||||||
try {
|
try {
|
||||||
@@ -48,7 +57,7 @@ internal class KtFirCompileTimeConstantProvider(
|
|||||||
}
|
}
|
||||||
is FirNamedReference -> {
|
is FirNamedReference -> {
|
||||||
when (val resolvedSymbol = fir.resolvedSymbol) {
|
when (val resolvedSymbol = fir.resolvedSymbol) {
|
||||||
is FirPropertySymbol -> resolvedSymbol.toKtConstantValue(context)
|
is FirPropertySymbol -> resolvedSymbol.toKtConstantValue(context, mode)
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,11 +74,18 @@ internal class KtFirCompileTimeConstantProvider(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun FirPropertySymbol.toKtConstantValue(context: KtExpression): KtConstantValue? = withValidityAssertion {
|
private fun FirPropertySymbol.toKtConstantValue(
|
||||||
|
context: KtExpression,
|
||||||
|
mode: KtConstantEvaluationMode,
|
||||||
|
): KtConstantValue? = withValidityAssertion {
|
||||||
if (isVal && hasInitializer) {
|
if (isVal && hasInitializer) {
|
||||||
// NB: the initializer could be [FirLazyExpression] in [BodyBuildingMode.LAZY_BODIES].
|
if (mode == KtConstantEvaluationMode.CONSTANT_EXPRESSION_EVALUATION && !isConst) {
|
||||||
this.ensureResolved(FirResolvePhase.BODY_RESOLVE) // to unwrap lazy body
|
null
|
||||||
evaluateFir(fir.initializer, context)
|
} else {
|
||||||
|
// NB: the initializer could be [FirLazyExpression] in [BodyBuildingMode.LAZY_BODIES].
|
||||||
|
this.ensureResolved(FirResolvePhase.BODY_RESOLVE) // to unwrap lazy body
|
||||||
|
evaluateFir(fir.initializer, context, mode)
|
||||||
|
}
|
||||||
} else null
|
} else null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-20
@@ -10,7 +10,7 @@ import org.jetbrains.kotlin.test.util.KtTestUtil;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService;
|
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService;
|
||||||
import org.jetbrains.kotlin.analysis.api.fir.FirFrontendApiTestConfiguratorService;
|
import org.jetbrains.kotlin.analysis.api.fir.FirFrontendApiTestConfiguratorService;
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorTest;
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorConstantLikeTest;
|
||||||
import org.jetbrains.kotlin.test.TestMetadata;
|
import org.jetbrains.kotlin.test.TestMetadata;
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -20,9 +20,9 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */
|
/** This class is generated by {@link GenerateNewCompilerTests.kt}. DO NOT MODIFY MANUALLY */
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@TestMetadata("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate")
|
@TestMetadata("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
public class FirCompileTimeConstantEvaluatorTestGenerated extends AbstractCompileTimeConstantEvaluatorTest {
|
public class FirCompileTimeConstantEvaluatorConstantLikeTestGenerated extends AbstractCompileTimeConstantEvaluatorConstantLikeTest {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public FrontendApiTestConfiguratorService getConfigurator() {
|
public FrontendApiTestConfiguratorService getConfigurator() {
|
||||||
@@ -30,97 +30,109 @@ public class FirCompileTimeConstantEvaluatorTestGenerated extends AbstractCompil
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAllFilesPresentInEvaluate() throws Exception {
|
public void testAllFilesPresentInEvaluate_constantLike() throws Exception {
|
||||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("binaryExpressionWithString.kt")
|
@TestMetadata("binaryExpressionWithString.kt")
|
||||||
public void testBinaryExpressionWithString() throws Exception {
|
public void testBinaryExpressionWithString() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/binaryExpressionWithString.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/binaryExpressionWithString.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("namedReference_const.kt")
|
@TestMetadata("namedReference_const.kt")
|
||||||
public void testNamedReference_const() throws Exception {
|
public void testNamedReference_const() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/namedReference_const.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/namedReference_const.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("namedReference_val.kt")
|
@TestMetadata("namedReference_val.kt")
|
||||||
public void testNamedReference_val() throws Exception {
|
public void testNamedReference_val() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/namedReference_val.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/namedReference_val.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("namedReference_var.kt")
|
@TestMetadata("namedReference_var.kt")
|
||||||
public void testNamedReference_var() throws Exception {
|
public void testNamedReference_var() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/namedReference_var.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/namedReference_var.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInCompanionObject.kt")
|
@TestMetadata("propertyInCompanionObject.kt")
|
||||||
public void testPropertyInCompanionObject() throws Exception {
|
public void testPropertyInCompanionObject() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInCompanionObject.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInCompanionObject.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInCompanionObject_indirect.kt")
|
@TestMetadata("propertyInCompanionObject_indirect.kt")
|
||||||
public void testPropertyInCompanionObject_indirect() throws Exception {
|
public void testPropertyInCompanionObject_indirect() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInCompanionObject_indirect.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInCompanionObject_indirect.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInCompanionObject_indirect_twice.kt")
|
@TestMetadata("propertyInCompanionObject_indirect_twice.kt")
|
||||||
public void testPropertyInCompanionObject_indirect_twice() throws Exception {
|
public void testPropertyInCompanionObject_indirect_twice() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInCompanionObject_indirect_twice.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInCompanionObject_indirect_twice.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Byte.kt")
|
@TestMetadata("propertyInit_Byte.kt")
|
||||||
public void testPropertyInit_Byte() throws Exception {
|
public void testPropertyInit_Byte() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Byte.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Byte.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("propertyInit_DivByOtherProperty_const.kt")
|
||||||
|
public void testPropertyInit_DivByOtherProperty_const() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_DivByOtherProperty_const.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("propertyInit_DivByOtherProperty_val.kt")
|
||||||
|
public void testPropertyInit_DivByOtherProperty_val() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_DivByOtherProperty_val.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_DivByZero.kt")
|
@TestMetadata("propertyInit_DivByZero.kt")
|
||||||
public void testPropertyInit_DivByZero() throws Exception {
|
public void testPropertyInit_DivByZero() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_DivByZero.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_DivByZero.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Double.kt")
|
@TestMetadata("propertyInit_Double.kt")
|
||||||
public void testPropertyInit_Double() throws Exception {
|
public void testPropertyInit_Double() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Double.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Double.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Float.kt")
|
@TestMetadata("propertyInit_Float.kt")
|
||||||
public void testPropertyInit_Float() throws Exception {
|
public void testPropertyInit_Float() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Float.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Float.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Int.kt")
|
@TestMetadata("propertyInit_Int.kt")
|
||||||
public void testPropertyInit_Int() throws Exception {
|
public void testPropertyInit_Int() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Int.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Int.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_Long.kt")
|
@TestMetadata("propertyInit_Long.kt")
|
||||||
public void testPropertyInit_Long() throws Exception {
|
public void testPropertyInit_Long() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_Long.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_Long.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("propertyInit_UInt.kt")
|
@TestMetadata("propertyInit_UInt.kt")
|
||||||
public void testPropertyInit_UInt() throws Exception {
|
public void testPropertyInit_UInt() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/propertyInit_UInt.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/propertyInit_UInt.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestMetadata("stringLiteral.kt")
|
@TestMetadata("stringLiteral.kt")
|
||||||
public void testStringLiteral() throws Exception {
|
public void testStringLiteral() throws Exception {
|
||||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/stringLiteral.kt");
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike/stringLiteral.kt");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+60
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.compileTimeConstantProvider;
|
||||||
|
|
||||||
|
import com.intellij.testFramework.TestDataPath;
|
||||||
|
import org.jetbrains.kotlin.test.util.KtTestUtil;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService;
|
||||||
|
import org.jetbrains.kotlin.analysis.api.fir.FirFrontendApiTestConfiguratorService;
|
||||||
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorConstantTest;
|
||||||
|
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/compileTimeConstantProvider/evaluate_constant")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
public class FirCompileTimeConstantEvaluatorConstantTestGenerated extends AbstractCompileTimeConstantEvaluatorConstantTest {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public FrontendApiTestConfiguratorService getConfigurator() {
|
||||||
|
return FirFrontendApiTestConfiguratorService.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAllFilesPresentInEvaluate_constant() throws Exception {
|
||||||
|
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("namedReference_const.kt")
|
||||||
|
public void testNamedReference_const() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant/namedReference_const.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("namedReference_val.kt")
|
||||||
|
public void testNamedReference_val() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant/namedReference_val.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("propertyInit_DivByOtherProperty_const.kt")
|
||||||
|
public void testPropertyInit_DivByOtherProperty_const() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant/propertyInit_DivByOtherProperty_const.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestMetadata("propertyInit_DivByOtherProperty_val.kt")
|
||||||
|
public void testPropertyInit_DivByOtherProperty_val() throws Exception {
|
||||||
|
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constant/propertyInit_DivByOtherProperty_val.kt");
|
||||||
|
}
|
||||||
|
}
|
||||||
+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.impl.base.test.components.compileTimeConstantProvider
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.analysis.api.components.KtConstantEvaluationMode
|
||||||
|
|
||||||
|
abstract class AbstractCompileTimeConstantEvaluatorConstantLikeTest
|
||||||
|
: AbstractCompileTimeConstantEvaluatorTest(KtConstantEvaluationMode.CONSTANT_LIKE_EXPRESSION_EVALUATION)
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.compileTimeConstantProvider
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.analysis.api.components.KtConstantEvaluationMode
|
||||||
|
|
||||||
|
abstract class AbstractCompileTimeConstantEvaluatorConstantTest
|
||||||
|
: AbstractCompileTimeConstantEvaluatorTest(KtConstantEvaluationMode.CONSTANT_EXPRESSION_EVALUATION)
|
||||||
+9
-6
@@ -1,11 +1,11 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
* Copyright 2010-2022 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.
|
* 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.compileTimeConstantProvider
|
package org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider
|
||||||
|
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService
|
import org.jetbrains.kotlin.analysis.api.components.KtConstantEvaluationMode
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.expressionMarkerProvider
|
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.analysis.api.impl.base.test.test.framework.AbstractHLApiSingleFileTest
|
||||||
import org.jetbrains.kotlin.psi.KtExpression
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
@@ -15,7 +15,9 @@ import org.jetbrains.kotlin.test.model.TestModule
|
|||||||
import org.jetbrains.kotlin.test.services.TestServices
|
import org.jetbrains.kotlin.test.services.TestServices
|
||||||
import org.jetbrains.kotlin.test.services.assertions
|
import org.jetbrains.kotlin.test.services.assertions
|
||||||
|
|
||||||
abstract class AbstractCompileTimeConstantEvaluatorTest : AbstractHLApiSingleFileTest() {
|
abstract class AbstractCompileTimeConstantEvaluatorTest(
|
||||||
|
private val mode: KtConstantEvaluationMode
|
||||||
|
) : AbstractHLApiSingleFileTest() {
|
||||||
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
|
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
|
||||||
val element = testServices.expressionMarkerProvider.getSelectedElement(ktFile)
|
val element = testServices.expressionMarkerProvider.getSelectedElement(ktFile)
|
||||||
val expression = when (element) {
|
val expression = when (element) {
|
||||||
@@ -24,7 +26,9 @@ abstract class AbstractCompileTimeConstantEvaluatorTest : AbstractHLApiSingleFil
|
|||||||
else -> null
|
else -> null
|
||||||
} ?: testServices.assertions.fail { "Unsupported expression: $element" }
|
} ?: testServices.assertions.fail { "Unsupported expression: $element" }
|
||||||
val constantValue = executeOnPooledThreadInReadAction {
|
val constantValue = executeOnPooledThreadInReadAction {
|
||||||
analyseForTest(expression) { expression.evaluate() }
|
analyseForTest(expression) {
|
||||||
|
expression.evaluate(mode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val actual = buildString {
|
val actual = buildString {
|
||||||
appendLine("expression: ${expression.text}")
|
appendLine("expression: ${expression.text}")
|
||||||
@@ -33,5 +37,4 @@ abstract class AbstractCompileTimeConstantEvaluatorTest : AbstractHLApiSingleFil
|
|||||||
}
|
}
|
||||||
testServices.assertions.assertEqualsToTestDataFileSibling(actual)
|
testServices.assertions.assertEqualsToTestDataFileSibling(actual)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|||||||
+24
-3
@@ -8,11 +8,32 @@ package org.jetbrains.kotlin.analysis.api.components
|
|||||||
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
|
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
|
||||||
import org.jetbrains.kotlin.psi.KtExpression
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
|
|
||||||
|
public enum class KtConstantEvaluationMode {
|
||||||
|
/**
|
||||||
|
* In this mode, what a compiler views as constants will be evaluated. In other words,
|
||||||
|
* expressions and properties that are free from runtime behaviors/changes will be evaluated,
|
||||||
|
* such as `const val` properties or binary expressions whose operands are constants.
|
||||||
|
*/
|
||||||
|
CONSTANT_EXPRESSION_EVALUATION,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In this mode, what a checker can approximate as constants will be evaluated. In other words,
|
||||||
|
* more expressions and properties that could be composites of other constants will be evaluated,
|
||||||
|
* such as `val` properties with constant initializers or binary expressions whose operands could be constants.
|
||||||
|
*
|
||||||
|
* Note that, as an approximation, the result can be sometimes incorrect or present even though there is an error.
|
||||||
|
*/
|
||||||
|
CONSTANT_LIKE_EXPRESSION_EVALUATION;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract class KtCompileTimeConstantProvider : KtAnalysisSessionComponent() {
|
public abstract class KtCompileTimeConstantProvider : KtAnalysisSessionComponent() {
|
||||||
public abstract fun evaluate(expression: KtExpression): KtConstantValue?
|
public abstract fun evaluate(
|
||||||
|
expression: KtExpression,
|
||||||
|
mode: KtConstantEvaluationMode,
|
||||||
|
): KtConstantValue?
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface KtCompileTimeConstantProviderMixIn : KtAnalysisSessionMixIn {
|
public interface KtCompileTimeConstantProviderMixIn : KtAnalysisSessionMixIn {
|
||||||
public fun KtExpression.evaluate(): KtConstantValue? =
|
public fun KtExpression.evaluate(mode: KtConstantEvaluationMode): KtConstantValue? =
|
||||||
analysisSession.compileTimeConstantProvider.evaluate(this)
|
analysisSession.compileTimeConstantProvider.evaluate(this, mode)
|
||||||
}
|
}
|
||||||
|
|||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: 42 / d
|
||||||
|
constant: 42
|
||||||
|
constantValueKind: Int
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
class Test {
|
||||||
|
val p = <expr>42 / d</expr> // uninitialized d
|
||||||
|
const val d = 1
|
||||||
|
}
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: 42 / d
|
||||||
|
constant: NOT_EVALUATED
|
||||||
|
constantValueKind: NOT_EVALUATED
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: 42 / d
|
||||||
|
constant: error("Division by zero")
|
||||||
|
constantValueKind: Error
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
class Test {
|
||||||
|
val p = <expr>42 / d</expr> // uninitialized d
|
||||||
|
val d = 0 // should not raise div-by-zero error
|
||||||
|
}
|
||||||
|
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: 42 / d
|
||||||
|
constant: NOT_EVALUATED
|
||||||
|
constantValueKind: NOT_EVALUATED
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
class Test {
|
||||||
|
const val bar : Int = 42
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box() {
|
||||||
|
val t = Test()
|
||||||
|
t.<expr>bar</expr>
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
class Test {
|
||||||
|
val bar : Int = 42
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box() {
|
||||||
|
val t = Test()
|
||||||
|
t.<expr>bar</expr>
|
||||||
|
}
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: bar
|
||||||
|
constant: 42
|
||||||
|
constantValueKind: Int
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: bar
|
||||||
|
constant: NOT_EVALUATED
|
||||||
|
constantValueKind: NOT_EVALUATED
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: 42 / d
|
||||||
|
constant: 42
|
||||||
|
constantValueKind: Int
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
const val d = 1
|
||||||
|
val p = <expr>42 / d</expr>
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: 42 / d
|
||||||
|
constant: NOT_EVALUATED
|
||||||
|
constantValueKind: NOT_EVALUATED
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: 42 / d
|
||||||
|
constant: 42
|
||||||
|
constantValueKind: Int
|
||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
val d = 1
|
||||||
|
val p = <expr>42 / d</expr>
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
expression: 42 / d
|
||||||
|
constant: NOT_EVALUATED
|
||||||
|
constantValueKind: NOT_EVALUATED
|
||||||
+7
-3
@@ -15,7 +15,8 @@ import org.jetbrains.kotlin.analysis.api.impl.base.test.annotations.AbstractAnal
|
|||||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.annotations.AbstractAnalysisApiAnnotationsOnTypesTest
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.annotations.AbstractAnalysisApiAnnotationsOnTypesTest
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.AbstractResolveCallTest
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.AbstractResolveCallTest
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.AbstractResolveCandidatesTest
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.AbstractResolveCandidatesTest
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorTest
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorConstantLikeTest
|
||||||
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorConstantTest
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.diagnosticProvider.AbstractCollectDiagnosticsTest
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.diagnosticProvider.AbstractCollectDiagnosticsTest
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.expressionInfoProvider.AbstractReturnTargetSymbolTest
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.expressionInfoProvider.AbstractReturnTargetSymbolTest
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.expressionInfoProvider.AbstractWhenMissingCasesTest
|
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.expressionInfoProvider.AbstractWhenMissingCasesTest
|
||||||
@@ -143,8 +144,11 @@ private fun TestGroupSuite.generateAnalysisApiComponentsTests() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
component("compileTimeConstantProvider") {
|
component("compileTimeConstantProvider") {
|
||||||
test(AbstractCompileTimeConstantEvaluatorTest::class) {
|
test(AbstractCompileTimeConstantEvaluatorConstantTest::class) {
|
||||||
model("evaluate")
|
model("evaluate_constant")
|
||||||
|
}
|
||||||
|
test(AbstractCompileTimeConstantEvaluatorConstantLikeTest::class) {
|
||||||
|
model("evaluate_constantLike")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user