[IR] ForLoopsLowering: fixed problems with type parameters

This commit is contained in:
Igor Chevdar
2019-09-02 16:44:15 +03:00
parent 21c7c89dbc
commit 5bd88f3191
3 changed files with 22 additions and 11 deletions
@@ -142,7 +142,8 @@ internal class IndexedGetHeaderInfo(
first: IrExpression, first: IrExpression,
last: IrExpression, last: IrExpression,
step: IrExpression, step: IrExpression,
val objectVariable: IrVariable val objectVariable: IrVariable,
val expressionHandler: IndexedGetIterationHandler
) : HeaderInfo( ) : HeaderInfo(
ProgressionType.INT_PROGRESSION, ProgressionType.INT_PROGRESSION,
first, first,
@@ -209,7 +209,7 @@ internal class IndexedGetLoopHeader(
override fun initializeLoopVariable(symbols: Symbols<CommonBackendContext>, builder: DeclarationIrBuilder) = with(builder) { override fun initializeLoopVariable(symbols: Symbols<CommonBackendContext>, builder: DeclarationIrBuilder) = with(builder) {
// inductionVar = loopVar[inductionVariable] // inductionVar = loopVar[inductionVariable]
val indexedGetFun = headerInfo.objectVariable.type.getClass()!!.functions.first { it.name.asString() == "get" } val indexedGetFun = with(headerInfo.expressionHandler) { headerInfo.objectVariable.type.getFunction }
irCall(indexedGetFun).apply { irCall(indexedGetFun).apply {
dispatchReceiver = irGet(headerInfo.objectVariable) dispatchReceiver = irGet(headerInfo.objectVariable)
putValueArgument(0, irGet(inductionVariable)) putValueArgument(0, irGet(inductionVariable))
@@ -13,13 +13,14 @@ import org.jetbrains.kotlin.backend.common.lower.matchers.createIrCallMatcher
import org.jetbrains.kotlin.backend.common.lower.matchers.singleArgumentExtension import org.jetbrains.kotlin.backend.common.lower.matchers.singleArgumentExtension
import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.isPrimitiveArray import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.properties
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -348,9 +349,7 @@ internal abstract class IndexedGetIterationHandler(protected val context: Common
origin = IrDeclarationOrigin.FOR_LOOP_IMPLICIT_VARIABLE origin = IrDeclarationOrigin.FOR_LOOP_IMPLICIT_VARIABLE
) )
// `last = array.size` (last is exclusive) for the loop `for (i in array.indices)`. val last = irCall(expression.type.sizePropertyGetter).apply {
val arraySizeProperty = arrayReference.type.getClass()!!.properties.first { it.name.asString() == sizePropertyName() }
val last = irCall(arraySizeProperty.getter!!).apply {
dispatchReceiver = irGet(arrayReference) dispatchReceiver = irGet(arrayReference)
} }
@@ -358,23 +357,34 @@ internal abstract class IndexedGetIterationHandler(protected val context: Common
first = irInt(0), first = irInt(0),
last = last, last = last,
step = irInt(1), step = irInt(1),
objectVariable = arrayReference objectVariable = arrayReference,
expressionHandler = this@IndexedGetIterationHandler
) )
} }
abstract fun sizePropertyName() : String abstract val IrType.sizePropertyGetter: IrSimpleFunction
abstract val IrType.getFunction: IrSimpleFunction
} }
/** Builds a [HeaderInfo] for arrays. */ /** Builds a [HeaderInfo] for arrays. */
internal class ArrayIterationHandler(context: CommonBackendContext) : IndexedGetIterationHandler(context) { internal class ArrayIterationHandler(context: CommonBackendContext) : IndexedGetIterationHandler(context) {
override fun match(expression: IrExpression) = expression.type.run { isArray() || isPrimitiveArray() } override fun match(expression: IrExpression) = expression.type.run { isArray() || isPrimitiveArray() }
override fun sizePropertyName(): String = "size" override val IrType.sizePropertyGetter
get() = getClass()!!.properties.first { it.name == Name.identifier("size") }.getter!!
override val IrType.getFunction
get() = getClass()!!.functions.first { it.name.asString() == "get" }
} }
/** Builds a [HeaderInfo] for iteration over characters in a `CharacterSequence`. */ /** Builds a [HeaderInfo] for iteration over characters in a `CharacterSequence`. */
internal class CharSequenceIterationHandler(context: CommonBackendContext) : IndexedGetIterationHandler(context) { internal class CharSequenceIterationHandler(context: CommonBackendContext) : IndexedGetIterationHandler(context) {
override fun match(expression: IrExpression) = expression.type.isSubtypeOfClass(context.ir.symbols.charSequence) override fun match(expression: IrExpression) = expression.type.isSubtypeOfClass(context.ir.symbols.charSequence)
override fun sizePropertyName(): String = "length" override val IrType.sizePropertyGetter
get() = context.ir.symbols.charSequence.getPropertyGetter("length")!!.owner
override val IrType.getFunction
get() = context.ir.symbols.charSequence.getSimpleFunction("get")!!.owner
} }