Support mod and floorDiv extensions in constant evaluator

Ignore evaluation tests for floorDiv and mod with FIR for now.
This commit is contained in:
Ilya Gorbunov
2021-02-21 06:05:03 +03:00
parent 58e6f775bb
commit 9cc8f44390
14 changed files with 206 additions and 18 deletions
+24 -3
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.
*/
@@ -26,7 +26,7 @@ fun main() {
fun generate(): String {
val sb = StringBuilder()
val p = Printer(sb)
p.println(File("license/COPYRIGHT.txt").readText())
p.println(File("license/COPYRIGHT_HEADER.txt").readText())
p.println("@file:Suppress(\"DEPRECATION\", \"DEPRECATION_ERROR\", \"NON_EXHAUSTIVE_WHEN\")")
p.println()
@@ -47,6 +47,9 @@ fun generate(): String {
val allPrimitiveTypes = builtIns.builtInsPackageScope.getContributedDescriptors()
.filter { it is ClassDescriptor && KotlinBuiltIns.isPrimitiveType(it.defaultType) } as List<ClassDescriptor>
val integerTypes = allPrimitiveTypes.map { it.defaultType }.filter { it.isIntegerType() }
val fpTypes = allPrimitiveTypes.map { it.defaultType }.filter { it.isFpType() }
for (descriptor in allPrimitiveTypes + builtIns.string) {
@Suppress("UNCHECKED_CAST")
val functions = descriptor.getMemberScope(listOf()).getContributedDescriptors()
@@ -65,6 +68,21 @@ fun generate(): String {
}
}
for (type in integerTypes) {
for (otherType in integerTypes) {
val parameters = listOf(type, otherType)
binaryOperationsMap.add("mod" to parameters)
binaryOperationsMap.add("floorDiv" to parameters)
}
}
for (type in fpTypes) {
for (otherType in fpTypes) {
val parameters = listOf(type, otherType)
binaryOperationsMap.add("mod" to parameters)
}
}
p.println("internal fun evalUnaryOp(name: String, type: CompileTimeType, value: Any): Any? {")
p.pushIndent()
p.println("when (type) {")
@@ -155,7 +173,7 @@ private fun getBinaryCheckerName(name: String, leftType: KotlinType, rightType:
"minus" -> "subtract"
"div" -> "divide"
"times" -> "multiply"
"mod", "rem", "xor", "or", "and" -> name
"rem", "xor", "or", "and" -> name
else -> null
}
}
@@ -163,6 +181,9 @@ private fun getBinaryCheckerName(name: String, leftType: KotlinType, rightType:
private fun KotlinType.isIntegerType(): Boolean =
KotlinBuiltIns.isInt(this) || KotlinBuiltIns.isShort(this) || KotlinBuiltIns.isByte(this) || KotlinBuiltIns.isLong(this)
private fun KotlinType.isFpType(): Boolean =
KotlinBuiltIns.isDouble(this) || KotlinBuiltIns.isFloat(this)
private fun CallableDescriptor.getParametersTypes(): List<KotlinType> =
listOf((containingDeclaration as ClassDescriptor).defaultType) +
valueParameters.map { it.type.makeNotNullable() }