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