[AA] Fix conversion of annotation values

This commit is contained in:
Pavel Mikhailovskii
2023-02-22 13:55:50 +00:00
committed by Space Team
parent 40a4845541
commit 7700484a16
10 changed files with 65 additions and 9 deletions
@@ -109,6 +109,12 @@ public class Fe10IdeNormalAnalysisSourceModuleAnalysisApiAnnotationsOnDeclaratio
runTest("analysis/analysis-api/testData/annotations/annotationsOnDeclaration/direct/onTypeAlias.kt");
}
@Test
@TestMetadata("unsignedParameter.kt")
public void testUnsignedParameter() throws Exception {
runTest("analysis/analysis-api/testData/annotations/annotationsOnDeclaration/direct/unsignedParameter.kt");
}
@Test
@TestMetadata("varargComplexParameter.kt")
public void testVarargComplexParameter() throws Exception {
@@ -6,7 +6,7 @@
package org.jetbrains.kotlin.analysis.api.fir.evaluate
import org.jetbrains.kotlin.analysis.api.annotations.*
import org.jetbrains.kotlin.analysis.api.base.KtConstantValueFactory
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
import org.jetbrains.kotlin.analysis.api.components.KtConstantEvaluationMode
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirSession
@@ -23,8 +23,7 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirConstructorSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirEnumEntrySymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirTypeAliasSymbol
import org.jetbrains.kotlin.fir.types.ConeErrorType
import org.jetbrains.kotlin.fir.types.coneType
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtCallElement
import org.jetbrains.kotlin.psi.KtClassOrObject
@@ -44,7 +43,27 @@ internal object FirAnnotationValueConverter {
}
private fun <T> FirConstExpression<T>.convertConstantExpression(): KtConstantAnnotationValue? {
val constantValue = KtConstantValueFactory.createConstantValue(value, psi as? KtElement) ?: return null
val expression = psi as? KtElement
val type = (typeRef as? FirResolvedTypeRef)?.type
val constantValue = when {
value == null -> KtConstantValue.KtNullConstantValue(expression)
type == null -> return null
type.isBoolean -> KtConstantValue.KtBooleanConstantValue(value as Boolean, expression)
type.isChar -> KtConstantValue.KtCharConstantValue((value as? Char) ?: (value as Number).toInt().toChar(), expression)
type.isByte -> KtConstantValue.KtByteConstantValue((value as Number).toByte(), expression)
type.isUByte -> KtConstantValue.KtUnsignedByteConstantValue((value as Number).toByte().toUByte(), expression)
type.isShort -> KtConstantValue.KtShortConstantValue((value as Number).toShort(), expression)
type.isUShort -> KtConstantValue.KtUnsignedShortConstantValue((value as Number).toShort().toUShort(), expression)
type.isInt -> KtConstantValue.KtIntConstantValue((value as Number).toInt(), expression)
type.isUInt -> KtConstantValue.KtUnsignedIntConstantValue((value as Number).toInt().toUInt(), expression)
type.isLong -> KtConstantValue.KtLongConstantValue((value as Number).toLong(), expression)
type.isULong -> KtConstantValue.KtUnsignedLongConstantValue((value as Number).toLong().toULong(), expression)
type.isString -> KtConstantValue.KtStringConstantValue(value.toString(), expression)
type.isFloat -> KtConstantValue.KtFloatConstantValue((value as Number).toFloat(), expression)
type.isDouble -> KtConstantValue.KtDoubleConstantValue((value as Number).toDouble(), expression)
else -> return null
}
return KtConstantAnnotationValue(constantValue)
}
@@ -109,6 +109,12 @@ public class FirIdeDependentAnalysisSourceModuleAnalysisApiAnnotationsOnDeclarat
runTest("analysis/analysis-api/testData/annotations/annotationsOnDeclaration/direct/onTypeAlias.kt");
}
@Test
@TestMetadata("unsignedParameter.kt")
public void testUnsignedParameter() throws Exception {
runTest("analysis/analysis-api/testData/annotations/annotationsOnDeclaration/direct/unsignedParameter.kt");
}
@Test
@TestMetadata("varargComplexParameter.kt")
public void testVarargComplexParameter() throws Exception {
@@ -109,6 +109,12 @@ public class FirIdeNormalAnalysisSourceModuleAnalysisApiAnnotationsOnDeclaration
runTest("analysis/analysis-api/testData/annotations/annotationsOnDeclaration/direct/onTypeAlias.kt");
}
@Test
@TestMetadata("unsignedParameter.kt")
public void testUnsignedParameter() throws Exception {
runTest("analysis/analysis-api/testData/annotations/annotationsOnDeclaration/direct/unsignedParameter.kt");
}
@Test
@TestMetadata("varargComplexParameter.kt")
public void testVarargComplexParameter() throws Exception {
@@ -109,6 +109,12 @@ public class FirStandaloneNormalAnalysisSourceModuleAnalysisApiAnnotationsOnDecl
runTest("analysis/analysis-api/testData/annotations/annotationsOnDeclaration/direct/onTypeAlias.kt");
}
@Test
@TestMetadata("unsignedParameter.kt")
public void testUnsignedParameter() throws Exception {
runTest("analysis/analysis-api/testData/annotations/annotationsOnDeclaration/direct/unsignedParameter.kt");
}
@Test
@TestMetadata("varargComplexParameter.kt")
public void testVarargComplexParameter() throws Exception {
@@ -0,0 +1,6 @@
// WITH_STDLIB
annotation class A(vararg val xs: ULong)
@A(1234u, 18446744073709551615u)
fun fo<caret>o(): Int = 42
@@ -0,0 +1,5 @@
KtDeclaration: KtNamedFunction foo
annotations: [
A(xs = [1234uL, 18446744073709551615uL])
psi: KtAnnotationEntry
]