FIR IDE: type-aware constant conversion
This commit is contained in:
committed by
Ilya Kirillov
parent
9e4f234941
commit
068e81570d
+31
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.executeOnPooledThreadInReadAction
|
||||
import org.jetbrains.kotlin.analysis.api.fir.test.framework.AbstractHLApiSingleFileTest
|
||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.base.expressionMarkerProvider
|
||||
import org.jetbrains.kotlin.analysis.api.analyse
|
||||
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 AbstractCompileTimeConstantEvaluatorTest : AbstractHLApiSingleFileTest() {
|
||||
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
|
||||
val expression = testServices.expressionMarkerProvider.getSelectedElement(ktFile) as KtExpression
|
||||
val constantValue = executeOnPooledThreadInReadAction {
|
||||
analyse(expression) { expression.evaluate() }
|
||||
}
|
||||
val actual = buildString {
|
||||
appendLine("expression: ${expression.text}")
|
||||
appendLine("constant_value: $constantValue")
|
||||
appendLine("constant: ${constantValue?.toConst()}")
|
||||
}
|
||||
testServices.assertions.assertEqualsToFile(testDataFileSibling(".txt"), actual)
|
||||
}
|
||||
}
|
||||
-2
@@ -7,13 +7,11 @@ package org.jetbrains.kotlin.analysis.api.fir.components
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.fir.executeOnPooledThreadInReadAction
|
||||
import org.jetbrains.kotlin.analysis.api.fir.test.framework.AbstractHLApiSingleFileTest
|
||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.base.AbstractLowLevelApiSingleFileTest
|
||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.base.expressionMarkerProvider
|
||||
import org.jetbrains.kotlin.analysis.api.analyse
|
||||
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.TestModuleStructure
|
||||
import org.jetbrains.kotlin.test.services.TestServices
|
||||
import org.jetbrains.kotlin.test.services.assertions
|
||||
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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/compileTimeConstantEvaluator")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
public class CompileTimeConstantEvaluatorTestGenerated extends AbstractCompileTimeConstantEvaluatorTest {
|
||||
@Test
|
||||
public void testAllFilesPresentInCompileTimeConstantEvaluator() throws Exception {
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantEvaluator"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyInit_Byte.kt")
|
||||
public void testPropertyInit_Byte() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantEvaluator/propertyInit_Byte.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyInit_Double.kt")
|
||||
public void testPropertyInit_Double() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantEvaluator/propertyInit_Double.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyInit_Float.kt")
|
||||
public void testPropertyInit_Float() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantEvaluator/propertyInit_Float.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyInit_Int.kt")
|
||||
public void testPropertyInit_Int() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantEvaluator/propertyInit_Int.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyInit_Long.kt")
|
||||
public void testPropertyInit_Long() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantEvaluator/propertyInit_Long.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("propertyInit_UInt.kt")
|
||||
public void testPropertyInit_UInt() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantEvaluator/propertyInit_UInt.kt");
|
||||
}
|
||||
}
|
||||
+19
-1
@@ -10,4 +10,22 @@ import org.jetbrains.kotlin.types.ConstantValueKind
|
||||
public sealed class KtConstantValue
|
||||
public object KtUnsupportedConstantValue : KtConstantValue()
|
||||
|
||||
public data class KtSimpleConstantValue<T>(val constantValueKind: ConstantValueKind<T>, val value: T) : KtConstantValue()
|
||||
public data class KtSimpleConstantValue<T>(val constantValueKind: ConstantValueKind<T>, val value: T) : KtConstantValue() {
|
||||
public fun toConst(): Any? {
|
||||
return (value as? Long)?.let {
|
||||
when (constantValueKind) {
|
||||
ConstantValueKind.Byte -> it.toByte()
|
||||
ConstantValueKind.Short -> it.toShort()
|
||||
ConstantValueKind.Int -> it.toInt()
|
||||
ConstantValueKind.Float -> it.toFloat()
|
||||
ConstantValueKind.Double -> it.toDouble()
|
||||
|
||||
ConstantValueKind.UnsignedByte -> it.toUByte()
|
||||
ConstantValueKind.UnsignedShort -> it.toUShort()
|
||||
ConstantValueKind.UnsignedInt -> it.toUInt()
|
||||
ConstantValueKind.UnsignedLong -> it.toULong()
|
||||
else -> it
|
||||
}
|
||||
} ?: value
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p : Byte = <expr>42</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 42
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Byte, value=42)
|
||||
constant: 42
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p : Double = <expr>3.14</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 3.14
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Double, value=3.14)
|
||||
constant: 3.14
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p = <expr>3.14f</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 3.14f
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Float, value=3.14)
|
||||
constant: 3.14
|
||||
+1
@@ -0,0 +1 @@
|
||||
val p : Int = <expr>42</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 42
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Int, value=42)
|
||||
constant: 42
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p : Long = <expr>42</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 42
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Long, value=42)
|
||||
constant: 42
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p : UInt = <expr>42u</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 42u
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=UInt, value=42)
|
||||
constant: 42
|
||||
+5
-1
@@ -57,6 +57,10 @@ fun main(args: Array<String>) {
|
||||
model("symbols/symbolByReference")
|
||||
}
|
||||
|
||||
testClass<AbstractCompileTimeConstantEvaluatorTest> {
|
||||
model("components/compileTimeConstantEvaluator")
|
||||
}
|
||||
|
||||
testClass<AbstractExpectedExpressionTypeTest> {
|
||||
model("components/expectedExpressionType")
|
||||
}
|
||||
@@ -169,4 +173,4 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user