Minor: extract high-level interface for ForLoopGenerator

This commit is contained in:
Dmitry Petrov
2017-06-29 10:20:43 +03:00
parent c870eeac2e
commit e261b8edf8
4 changed files with 41 additions and 13 deletions
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.codegen.coroutines.CoroutineCodegenUtilKt;
import org.jetbrains.kotlin.codegen.coroutines.ResolvedCallWithRealDescriptor;
import org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension;
import org.jetbrains.kotlin.codegen.forLoop.AbstractForLoopGenerator;
import org.jetbrains.kotlin.codegen.forLoop.ForLoopGenerator;
import org.jetbrains.kotlin.codegen.inline.*;
import org.jetbrains.kotlin.codegen.intrinsics.*;
import org.jetbrains.kotlin.codegen.pseudoInsns.PseudoInsnsKt;
@@ -627,7 +628,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
return context.getContextKind();
}
private void generateForLoop(AbstractForLoopGenerator generator) {
private void generateForLoop(ForLoopGenerator generator) {
Label loopExit = new Label();
Label loopEntry = new Label();
Label continueLabel = new Label();
@@ -30,8 +30,8 @@ import org.jetbrains.org.objectweb.asm.Type
abstract class AbstractForLoopGenerator(
protected val codegen: ExpressionCodegen,
val forExpression: KtForExpression
) {
override val forExpression: KtForExpression
) : ForLoopGenerator {
protected val bindingContext = codegen.bindingContext
protected val v = codegen.v!!
@@ -52,7 +52,7 @@ abstract class AbstractForLoopGenerator(
return nextCall.resultingDescriptor.returnType!!
}
open fun beforeLoop() {
override fun beforeLoop() {
val loopParameter = forExpression.loopParameter ?: return
val multiParameter = loopParameter.destructuringDeclaration
if (multiParameter != null) {
@@ -75,11 +75,7 @@ abstract class AbstractForLoopGenerator(
}
}
abstract fun checkEmptyLoop(loopExit: Label)
abstract fun checkPreCondition(loopExit: Label)
fun beforeBody() {
override fun beforeBody() {
assignToLoopParameter()
v.mark(loopParameterStartLabel)
@@ -118,7 +114,7 @@ abstract class AbstractForLoopGenerator(
protected abstract fun checkPostConditionAndIncrement(loopExit: Label)
fun body() {
override fun body() {
codegen.generateLoopBody(forExpression.body)
}
@@ -132,7 +128,7 @@ abstract class AbstractForLoopGenerator(
return varIndex
}
fun afterBody(loopExit: Label) {
override fun afterBody(loopExit: Label) {
codegen.markStartLineNumber(forExpression)
checkPostConditionAndIncrement(loopExit)
@@ -140,7 +136,7 @@ abstract class AbstractForLoopGenerator(
v.mark(bodyEnd)
}
fun afterLoop() {
override fun afterLoop() {
for (task in leaveVariableTasks.asReversed()) {
task.run()
}
@@ -0,0 +1,31 @@
/*
* 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.psi.KtForExpression
import org.jetbrains.org.objectweb.asm.Label
interface ForLoopGenerator {
val forExpression: KtForExpression
fun beforeLoop()
fun checkEmptyLoop(loopExit: Label)
fun checkPreCondition(loopExit: Label)
fun beforeBody()
fun body()
fun afterBody(loopExit: Label)
fun afterLoop()
}
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
interface RangeValue {
fun createForLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression): AbstractForLoopGenerator
fun createForLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression): ForLoopGenerator
}
class ArrayRangeValue : RangeValue {