Make isDefaultValueForType shorter

This commit is contained in:
pyos
2019-03-27 15:16:54 +01:00
committed by Mikhael Bogdanov
parent fb55e34b8f
commit c3a74ded0e
@@ -488,7 +488,7 @@ class ExpressionCodegen(
// i.e., not in an initializer block or constructor body. // i.e., not in an initializer block or constructor body.
val skip = irFunction is IrConstructor && irFunction.isPrimary && val skip = irFunction is IrConstructor && irFunction.isPrimary &&
expression.origin == null && expressionValue is IrConst<*> && expression.origin == null && expressionValue is IrConst<*> &&
isDefaultValueForType(expression.symbol.owner.type, expressionValue) isDefaultValueForType(expression.symbol.owner.type.asmType, expressionValue.value)
if (!skip) { if (!skip) {
expression.markLineNumber(startOffset = true) expression.markLineNumber(startOffset = true)
val fieldValue = generateFieldValue(expression, data) val fieldValue = generateFieldValue(expression, data)
@@ -505,32 +505,16 @@ class ExpressionCodegen(
* Returns true if the given constant value is the JVM's default value for the given type. * Returns true if the given constant value is the JVM's default value for the given type.
* See: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.3 * See: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.3
*/ */
private fun isDefaultValueForType(fieldType: IrType, constExpression: IrConst<*>): Boolean { private fun isDefaultValueForType(type: Type, value: Any?): Boolean =
val value = constExpression.value when (type) {
val type = constExpression.asmType Type.BOOLEAN_TYPE -> value is Boolean && !value
if (isPrimitive(type)) { Type.CHAR_TYPE -> value is Char && value.toInt() == 0
if (!fieldType.isMarkedNullable() && value is Number) { Type.BYTE_TYPE, Type.SHORT_TYPE, Type.INT_TYPE, Type.LONG_TYPE -> value is Number && value.toLong() == 0L
if (type in setOf(Type.INT_TYPE, Type.BYTE_TYPE, Type.LONG_TYPE, Type.SHORT_TYPE) && value.toLong() == 0L) { // Must use `equals` for these two to differentiate between +0.0 and -0.0:
return true Type.FLOAT_TYPE -> value is Number && value.toFloat().equals(0.0f)
} Type.DOUBLE_TYPE -> value is Number && value.toDouble().equals(0.0)
if (type === Type.DOUBLE_TYPE && value.toDouble().equals(0.0)) { else -> !isPrimitive(type) && value == null
return true
}
if (type === Type.FLOAT_TYPE && value.toFloat().equals(0.0f)) {
return true
}
}
if (type === Type.BOOLEAN_TYPE && value is Boolean && !value) {
return true
}
if (type === Type.CHAR_TYPE && value is Char && value.toInt() == 0) {
return true
}
} else if (value == null) {
return true
} }
return false
}
private fun generateLocal(symbol: IrSymbol, type: Type): StackValue { private fun generateLocal(symbol: IrSymbol, type: Type): StackValue {
val index = findLocalIndex(symbol) val index = findLocalIndex(symbol)