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.components.base.Fe10KtAnalysisSessionComponent
|
||||
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.components.KtConstantEvaluationMode
|
||||
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.KtNameReferenceExpression
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
|
||||
@@ -22,8 +25,23 @@ internal class KtFe10CompileTimeConstantProvider(
|
||||
override val token: ValidityToken
|
||||
get() = analysisSession.token
|
||||
|
||||
override fun evaluate(expression: KtExpression): KtConstantValue? {
|
||||
override fun evaluate(
|
||||
expression: KtExpression,
|
||||
mode: KtConstantEvaluationMode,
|
||||
): KtConstantValue? {
|
||||
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)
|
||||
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.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.AbstractCompileTimeConstantEvaluatorTest;
|
||||
import org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider.AbstractCompileTimeConstantEvaluatorConstantLikeTest;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
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 */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate")
|
||||
@TestMetadata("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class Fe10CompileTimeConstantEvaluatorTestGenerated extends AbstractCompileTimeConstantEvaluatorTest {
|
||||
public class Fe10CompileTimeConstantEvaluatorConstantLikeTestGenerated extends AbstractCompileTimeConstantEvaluatorConstantLikeTest {
|
||||
@NotNull
|
||||
@Override
|
||||
public FrontendApiTestConfiguratorService getConfigurator() {
|
||||
@@ -30,97 +30,109 @@ public class Fe10CompileTimeConstantEvaluatorTestGenerated extends AbstractCompi
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllFilesPresentInEvaluate() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
public void testAllFilesPresentInEvaluate_constantLike() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate_constantLike"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("binaryExpressionWithString.kt")
|
||||
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
|
||||
@TestMetadata("namedReference_const.kt")
|
||||
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
|
||||
@TestMetadata("namedReference_val.kt")
|
||||
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
|
||||
@TestMetadata("namedReference_var.kt")
|
||||
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
|
||||
@TestMetadata("propertyInCompanionObject.kt")
|
||||
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
|
||||
@TestMetadata("propertyInCompanionObject_indirect.kt")
|
||||
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
|
||||
@TestMetadata("propertyInCompanionObject_indirect_twice.kt")
|
||||
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
|
||||
@TestMetadata("propertyInit_Byte.kt")
|
||||
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
|
||||
@TestMetadata("propertyInit_DivByZero.kt")
|
||||
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
|
||||
@TestMetadata("propertyInit_Double.kt")
|
||||
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
|
||||
@TestMetadata("propertyInit_Float.kt")
|
||||
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
|
||||
@TestMetadata("propertyInit_Int.kt")
|
||||
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
|
||||
@TestMetadata("propertyInit_Long.kt")
|
||||
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
|
||||
@TestMetadata("propertyInit_UInt.kt")
|
||||
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
|
||||
@TestMetadata("stringLiteral.kt")
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user