Introduce AbstractForInExclusiveRangeLoopGenerator
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.codegen.forLoop
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.codegen.ExpressionCodegen
|
||||||
|
import org.jetbrains.kotlin.codegen.StackValue
|
||||||
|
import org.jetbrains.kotlin.psi.KtForExpression
|
||||||
|
import org.jetbrains.org.objectweb.asm.Label
|
||||||
|
import org.jetbrains.org.objectweb.asm.Type
|
||||||
|
|
||||||
|
abstract class AbstractForInExclusiveRangeLoopGenerator(
|
||||||
|
codegen: ExpressionCodegen,
|
||||||
|
forExpression: KtForExpression
|
||||||
|
) : AbstractForInRangeLoopGenerator(codegen, forExpression) {
|
||||||
|
protected abstract fun generateFrom(): StackValue
|
||||||
|
protected abstract fun generateTo(): StackValue
|
||||||
|
|
||||||
|
override fun storeRangeStartAndEnd() {
|
||||||
|
loopParameter().store(generateFrom(), v)
|
||||||
|
StackValue.local(endVar, asmElementType).store(generateTo(), v)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun checkEmptyLoop(loopExit: Label) {}
|
||||||
|
|
||||||
|
override fun checkPreCondition(loopExit: Label) {
|
||||||
|
loopParameter().put(asmElementType, v)
|
||||||
|
v.load(endVar, asmElementType)
|
||||||
|
if (asmElementType.sort == Type.LONG) {
|
||||||
|
v.lcmp()
|
||||||
|
v.ifge(loopExit)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
v.ificmpge(loopExit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun checkPostConditionAndIncrement(loopExit: Label) {
|
||||||
|
incrementLoopVariable()
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
-24
@@ -22,37 +22,18 @@ import org.jetbrains.kotlin.psi.KtExpression
|
|||||||
import org.jetbrains.kotlin.psi.KtForExpression
|
import org.jetbrains.kotlin.psi.KtForExpression
|
||||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
|
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
|
||||||
import org.jetbrains.org.objectweb.asm.Label
|
|
||||||
import org.jetbrains.org.objectweb.asm.Type
|
|
||||||
|
|
||||||
class ForInUntilRangeLoopGenerator(
|
class ForInUntilRangeLoopGenerator(
|
||||||
codegen: ExpressionCodegen,
|
codegen: ExpressionCodegen,
|
||||||
forExpression: KtForExpression,
|
forExpression: KtForExpression,
|
||||||
loopRangeCall: ResolvedCall<*>
|
loopRangeCall: ResolvedCall<*>
|
||||||
) : AbstractForInRangeLoopGenerator(codegen, forExpression) {
|
) : AbstractForInExclusiveRangeLoopGenerator(codegen, forExpression) {
|
||||||
private val from: ReceiverValue = loopRangeCall.extensionReceiver!!
|
private val from: ReceiverValue = loopRangeCall.extensionReceiver!!
|
||||||
private val to: KtExpression = ExpressionCodegen.getSingleArgumentExpression(loopRangeCall)!!
|
private val to: KtExpression = ExpressionCodegen.getSingleArgumentExpression(loopRangeCall)!!
|
||||||
|
|
||||||
override fun storeRangeStartAndEnd() {
|
override fun generateFrom(): StackValue =
|
||||||
loopParameter().store(codegen.generateReceiverValue(from, false), v)
|
codegen.generateReceiverValue(from, false)
|
||||||
StackValue.local(endVar, asmElementType).store(codegen.gen(to), v)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun checkEmptyLoop(loopExit: Label) {}
|
override fun generateTo(): StackValue =
|
||||||
|
codegen.gen(to)
|
||||||
override fun checkPreCondition(loopExit: Label) {
|
|
||||||
loopParameter().put(asmElementType, v)
|
|
||||||
v.load(endVar, asmElementType)
|
|
||||||
if (asmElementType.sort == Type.LONG) {
|
|
||||||
v.lcmp()
|
|
||||||
v.ifge(loopExit)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
v.ificmpge(loopExit)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun checkPostConditionAndIncrement(loopExit: Label) {
|
|
||||||
incrementLoopVariable()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user