Skip to content

Commit

Permalink
Avoid hard-coded operator names in CompileTimeConstantEvaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
jsjeon committed Jun 20, 2021
1 parent a969924 commit bb297b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,6 @@ class CompileTimeConstantEvaluator {
private fun <T : Number> FirConstKind<T>.toConstExpression(source: FirSourceElement?, value: Number): FirConstExpression<T> =
@Suppress("UNCHECKED_CAST")
buildConstExpression(source, this, value as T)

// TODO: extension functions, e.g., Byte.and
// TODO: toChar?, toString?
companion object {
private val unaryOperatorNames: Set<String> = setOf(
"hashCode",
"dec", "inc", "unaryMinus", "unaryPlus",
"toByte", "toDouble", "toFloat", "toInt", "toLong", "toShort"
)

// TODO: rangeTo?
private val binaryOperatorNames: Set<String> = setOf(
// TODO: "compareTo", "equals",
"div", "minus", "mod", "plus", "rem", "times"
)
}
}

val <T> FirConstExpression<T>.typedValue: T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,6 @@ internal val binaryOperations: HashMap<BinaryOperationKey<*, *>, Function2<Numbe
binaryOperation(FirConstKind.Short, FirConstKind.Long, "times", { a, b -> a.times(b) }),
binaryOperation(FirConstKind.Short, FirConstKind.Short, "times", { a, b -> a.times(b) }),
)

internal val unaryOperatorNames = unaryOperations.map { it.key.opName }.toHashSet()
internal val binaryOperatorNames = binaryOperations.map { it.key.opName }.toHashSet()
8 changes: 8 additions & 0 deletions generators/evaluate/GenerateFirOperationsMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ fun generateFirMap(): String {
p.renderBinaryOperations(binaryOperationsMap)
p.popIndent()
p.println(")")
p.println()

p.println(
"""
|internal val unaryOperatorNames = unaryOperations.map { it.key.opName }.toHashSet()
|internal val binaryOperatorNames = binaryOperations.map { it.key.opName }.toHashSet()
""".trimMargin()
)

return sb.toString()
}
Expand Down

0 comments on commit bb297b8

Please sign in to comment.