Minor: reformat

This commit is contained in:
Dmitry Petrov
2018-06-09 14:22:23 +03:00
parent b4dc3dc91b
commit 3528405666
@@ -31,35 +31,36 @@ import org.jetbrains.org.objectweb.asm.Type
import java.util.ArrayList
class SwitchCodegenProvider
private constructor (
private val bindingContext: BindingContext,
private val shouldInlineConstVals: Boolean,
private val codegen: ExpressionCodegen?
private constructor(
private val bindingContext: BindingContext,
private val shouldInlineConstVals: Boolean,
private val codegen: ExpressionCodegen?
) {
constructor(state: GenerationState) : this(state.bindingContext, state.shouldInlineConstVals, null)
constructor(codegen: ExpressionCodegen) : this(codegen.bindingContext, codegen.state.shouldInlineConstVals, codegen)
fun checkAllItemsAreConstantsSatisfying(expression: KtWhenExpression, predicate: Function1<ConstantValue<*>, Boolean>): Boolean =
expression.entries.all { entry ->
entry.conditions.all { condition ->
if (condition !is KtWhenConditionWithExpression) return false
val patternExpression = condition.expression ?: return false
val constant = ExpressionCodegen.getCompileTimeConstant(patternExpression, bindingContext, shouldInlineConstVals) ?: return false
predicate.invoke(constant)
}
expression.entries.all { entry ->
entry.conditions.all { condition ->
if (condition !is KtWhenConditionWithExpression) return false
val patternExpression = condition.expression ?: return false
val constant =
ExpressionCodegen.getCompileTimeConstant(patternExpression, bindingContext, shouldInlineConstVals) ?: return false
predicate.invoke(constant)
}
}
fun getAllConstants(expression: KtWhenExpression): Iterable<ConstantValue<*>?> =
ArrayList<ConstantValue<*>?>().apply {
for (entry in expression.entries) {
addConstantsFromConditions(entry)
}
}
fun getConstantsFromEntry(entry: KtWhenEntry): Iterable<ConstantValue<*>?> =
ArrayList<ConstantValue<*>?>().apply {
ArrayList<ConstantValue<*>?>().apply {
for (entry in expression.entries) {
addConstantsFromConditions(entry)
}
}
fun getConstantsFromEntry(entry: KtWhenEntry): Iterable<ConstantValue<*>?> =
ArrayList<ConstantValue<*>?>().apply {
addConstantsFromConditions(entry)
}
private fun ArrayList<ConstantValue<*>?>.addConstantsFromConditions(entry: KtWhenEntry) {
for (condition in entry.conditions) {
@@ -70,9 +71,9 @@ private constructor (
}
fun buildAppropriateSwitchCodegenIfPossible(
expression: KtWhenExpression,
isStatement: Boolean,
isExhaustive: Boolean
expression: KtWhenExpression,
isStatement: Boolean,
isExhaustive: Boolean
): SwitchCodegen? {
val codegen = codegen ?: throw AssertionError("Can't create SwitchCodegen in this context")
@@ -97,13 +98,13 @@ private constructor (
}
private fun isThereConstantEntriesButNulls(expression: KtWhenExpression): Boolean =
getAllConstants(expression).any { it != null && it !is NullValue }
getAllConstants(expression).any { it != null && it !is NullValue }
private fun isIntegralConstantsSwitch(expression: KtWhenExpression, subjectType: Type): Boolean =
AsmUtil.isIntPrimitive(subjectType) &&
checkAllItemsAreConstantsSatisfying(expression) { it is IntegerValueConstant<*> }
AsmUtil.isIntPrimitive(subjectType) &&
checkAllItemsAreConstantsSatisfying(expression) { it is IntegerValueConstant<*> }
private fun isStringConstantsSwitch(expression: KtWhenExpression, subjectType: Type): Boolean =
subjectType.className == String::class.java.name &&
checkAllItemsAreConstantsSatisfying(expression) { it is StringValue || it is NullValue }
subjectType.className == String::class.java.name &&
checkAllItemsAreConstantsSatisfying(expression) { it is StringValue || it is NullValue }
}