Minor: extract high-level interface for ForLoopGenerator
This commit is contained in:
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.codegen.coroutines.CoroutineCodegenUtilKt;
|
|||||||
import org.jetbrains.kotlin.codegen.coroutines.ResolvedCallWithRealDescriptor;
|
import org.jetbrains.kotlin.codegen.coroutines.ResolvedCallWithRealDescriptor;
|
||||||
import org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension;
|
import org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension;
|
||||||
import org.jetbrains.kotlin.codegen.forLoop.AbstractForLoopGenerator;
|
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.inline.*;
|
||||||
import org.jetbrains.kotlin.codegen.intrinsics.*;
|
import org.jetbrains.kotlin.codegen.intrinsics.*;
|
||||||
import org.jetbrains.kotlin.codegen.pseudoInsns.PseudoInsnsKt;
|
import org.jetbrains.kotlin.codegen.pseudoInsns.PseudoInsnsKt;
|
||||||
@@ -627,7 +628,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
return context.getContextKind();
|
return context.getContextKind();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateForLoop(AbstractForLoopGenerator generator) {
|
private void generateForLoop(ForLoopGenerator generator) {
|
||||||
Label loopExit = new Label();
|
Label loopExit = new Label();
|
||||||
Label loopEntry = new Label();
|
Label loopEntry = new Label();
|
||||||
Label continueLabel = new Label();
|
Label continueLabel = new Label();
|
||||||
|
|||||||
+7
-11
@@ -30,8 +30,8 @@ import org.jetbrains.org.objectweb.asm.Type
|
|||||||
|
|
||||||
abstract class AbstractForLoopGenerator(
|
abstract class AbstractForLoopGenerator(
|
||||||
protected val codegen: ExpressionCodegen,
|
protected val codegen: ExpressionCodegen,
|
||||||
val forExpression: KtForExpression
|
override val forExpression: KtForExpression
|
||||||
) {
|
) : ForLoopGenerator {
|
||||||
protected val bindingContext = codegen.bindingContext
|
protected val bindingContext = codegen.bindingContext
|
||||||
protected val v = codegen.v!!
|
protected val v = codegen.v!!
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ abstract class AbstractForLoopGenerator(
|
|||||||
return nextCall.resultingDescriptor.returnType!!
|
return nextCall.resultingDescriptor.returnType!!
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun beforeLoop() {
|
override fun beforeLoop() {
|
||||||
val loopParameter = forExpression.loopParameter ?: return
|
val loopParameter = forExpression.loopParameter ?: return
|
||||||
val multiParameter = loopParameter.destructuringDeclaration
|
val multiParameter = loopParameter.destructuringDeclaration
|
||||||
if (multiParameter != null) {
|
if (multiParameter != null) {
|
||||||
@@ -75,11 +75,7 @@ abstract class AbstractForLoopGenerator(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract fun checkEmptyLoop(loopExit: Label)
|
override fun beforeBody() {
|
||||||
|
|
||||||
abstract fun checkPreCondition(loopExit: Label)
|
|
||||||
|
|
||||||
fun beforeBody() {
|
|
||||||
assignToLoopParameter()
|
assignToLoopParameter()
|
||||||
v.mark(loopParameterStartLabel)
|
v.mark(loopParameterStartLabel)
|
||||||
|
|
||||||
@@ -118,7 +114,7 @@ abstract class AbstractForLoopGenerator(
|
|||||||
|
|
||||||
protected abstract fun checkPostConditionAndIncrement(loopExit: Label)
|
protected abstract fun checkPostConditionAndIncrement(loopExit: Label)
|
||||||
|
|
||||||
fun body() {
|
override fun body() {
|
||||||
codegen.generateLoopBody(forExpression.body)
|
codegen.generateLoopBody(forExpression.body)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +128,7 @@ abstract class AbstractForLoopGenerator(
|
|||||||
return varIndex
|
return varIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
fun afterBody(loopExit: Label) {
|
override fun afterBody(loopExit: Label) {
|
||||||
codegen.markStartLineNumber(forExpression)
|
codegen.markStartLineNumber(forExpression)
|
||||||
|
|
||||||
checkPostConditionAndIncrement(loopExit)
|
checkPostConditionAndIncrement(loopExit)
|
||||||
@@ -140,7 +136,7 @@ abstract class AbstractForLoopGenerator(
|
|||||||
v.mark(bodyEnd)
|
v.mark(bodyEnd)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun afterLoop() {
|
override fun afterLoop() {
|
||||||
for (task in leaveVariableTasks.asReversed()) {
|
for (task in leaveVariableTasks.asReversed()) {
|
||||||
task.run()
|
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
|
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||||
|
|
||||||
interface RangeValue {
|
interface RangeValue {
|
||||||
fun createForLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression): AbstractForLoopGenerator
|
fun createForLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression): ForLoopGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
class ArrayRangeValue : RangeValue {
|
class ArrayRangeValue : RangeValue {
|
||||||
|
|||||||
Reference in New Issue
Block a user