From 40dbf8a3fd49e1ce28eba8f8ab32e06053cd01f0 Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Tue, 19 Mar 2019 16:25:07 -0700 Subject: [PATCH] Move elementType() and getProgressionType() extension methods into ProgressionType. --- .../backend/common/lower/loops/HeaderInfo.kt | 27 +++++++++++++------ .../common/lower/loops/HeaderProcessor.kt | 10 ++----- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt index 29c913658d8..012fc6a2eba 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderInfo.kt @@ -6,10 +6,12 @@ package org.jetbrains.kotlin.backend.common.lower.loops import org.jetbrains.kotlin.backend.common.CommonBackendContext +import org.jetbrains.kotlin.backend.common.ir.Symbols import org.jetbrains.kotlin.backend.common.lower.matchers.IrCallMatcher import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.declarations.IrValueDeclaration import org.jetbrains.kotlin.ir.declarations.IrVariable +import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.types.IrType @@ -27,6 +29,22 @@ enum class ProgressionType(val numberCastFunctionName: Name) { INT_PROGRESSION(Name.identifier("toInt")), LONG_PROGRESSION(Name.identifier("toLong")), CHAR_PROGRESSION(Name.identifier("toChar")); + + /** Returns the [IrType] of the `first`/`last` properties and elements in the progression. */ + fun elementType(builtIns: IrBuiltIns): IrType = when (this) { + ProgressionType.INT_PROGRESSION -> builtIns.intType + ProgressionType.LONG_PROGRESSION -> builtIns.longType + ProgressionType.CHAR_PROGRESSION -> builtIns.charType + } + + companion object { + fun fromIrType(irType: IrType, symbols: Symbols): ProgressionType? = when { + irType.isSubtypeOfClass(symbols.charProgression) -> ProgressionType.CHAR_PROGRESSION + irType.isSubtypeOfClass(symbols.intProgression) -> ProgressionType.INT_PROGRESSION + irType.isSubtypeOfClass(symbols.longProgression) -> ProgressionType.LONG_PROGRESSION + else -> null + } + } } // Information about loop that is required by HeaderProcessor to build ForLoopHeader @@ -89,17 +107,10 @@ private class ProgressionHeaderInfoBuilder(val context: CommonBackendContext) : RangeToHandler(progressionElementTypes) ) - private fun IrType.getProgressionType(): ProgressionType? = when { - isSubtypeOfClass(symbols.charProgression) -> ProgressionType.CHAR_PROGRESSION - isSubtypeOfClass(symbols.intProgression) -> ProgressionType.INT_PROGRESSION - isSubtypeOfClass(symbols.longProgression) -> ProgressionType.LONG_PROGRESSION - else -> null - } - override fun visitElement(element: IrElement, data: Nothing?): HeaderInfo? = null override fun visitCall(expression: IrCall, data: Nothing?): HeaderInfo? { - val progressionType = expression.type.getProgressionType() + val progressionType = ProgressionType.fromIrType(expression.type, symbols) ?: return null return progressionHandlers.firstNotNullResult { it.handle(expression, progressionType) } } diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt index 84a8b1a231a..daca346dc8e 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/HeaderProcessor.kt @@ -122,12 +122,6 @@ internal class ArrayLoopHeader( } } -private fun ProgressionType.elementType(context: CommonBackendContext): IrType = when (this) { - ProgressionType.INT_PROGRESSION -> context.irBuiltIns.intType - ProgressionType.LONG_PROGRESSION -> context.irBuiltIns.longType - ProgressionType.CHAR_PROGRESSION -> context.irBuiltIns.charType -} - // Given the for loop iterator variable, extract information about iterable subject // and create ForLoopHeader from it. internal class HeaderProcessor( @@ -228,7 +222,7 @@ internal class HeaderProcessor( private fun IrExpression.castIfNecessary(progressionType: ProgressionType): IrExpression { - return if (type.toKotlinType() == progressionType.elementType(context).toKotlinType()) { + return if (type.toKotlinType() == progressionType.elementType(context.irBuiltIns).toKotlinType()) { this } else { val function = type.getClass()!!.functions.first { it.name == progressionType.numberCastFunctionName } @@ -252,7 +246,7 @@ internal class HeaderProcessor( } private fun HeaderInfo.defaultStep(startOffset: Int, endOffset: Int): IrExpression { - val type = progressionType.elementType(context) + val type = progressionType.elementType(context.irBuiltIns) val step = if (increasing) 1 else -1 return when { type.isInt() || type.isChar() ->