KJS: Add JsLoop AST interface and corresponding visitors
This commit is contained in:
@@ -18,7 +18,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
* Note that any of the parts of the <code>for</code> loop header can be
|
||||
* <code>null</code>, although the body will never be null.
|
||||
*/
|
||||
public class JsFor extends SourceInfoAwareJsNode implements JsStatement {
|
||||
public class JsFor extends SourceInfoAwareJsNode implements JsLoop {
|
||||
private JsStatement body;
|
||||
private JsExpression condition;
|
||||
private JsExpression incrementExpression;
|
||||
|
||||
@@ -7,7 +7,7 @@ package org.jetbrains.kotlin.js.backend.ast;
|
||||
import org.jetbrains.kotlin.js.util.AstUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class JsForIn extends SourceInfoAwareJsNode implements JsStatement {
|
||||
public class JsForIn extends SourceInfoAwareJsNode implements JsLoop {
|
||||
private JsStatement body;
|
||||
private JsExpression iterExpression;
|
||||
private JsExpression objectExpression;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.js.backend.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface JsLoop extends JsStatement {
|
||||
@NotNull
|
||||
@Override
|
||||
JsStatement deepCopy();
|
||||
}
|
||||
@@ -64,7 +64,7 @@ abstract class JsVisitor {
|
||||
visitElement(x)
|
||||
|
||||
open fun visitDoWhile(x: JsDoWhile): Unit =
|
||||
visitElement(x)
|
||||
visitLoop(x)
|
||||
|
||||
open fun visitEmpty(x: JsEmpty): Unit =
|
||||
visitElement(x)
|
||||
@@ -73,10 +73,10 @@ abstract class JsVisitor {
|
||||
visitElement(x)
|
||||
|
||||
open fun visitFor(x: JsFor): Unit =
|
||||
visitElement(x)
|
||||
visitLoop(x)
|
||||
|
||||
open fun visitForIn(x: JsForIn): Unit =
|
||||
visitElement(x)
|
||||
visitLoop(x)
|
||||
|
||||
open fun visitFunction(x: JsFunction): Unit =
|
||||
visitElement(x)
|
||||
@@ -90,6 +90,9 @@ abstract class JsVisitor {
|
||||
open fun visitLabel(x: JsLabel): Unit =
|
||||
visitElement(x)
|
||||
|
||||
open fun visitLoop(x: JsLoop): Unit =
|
||||
visitElement(x)
|
||||
|
||||
open fun visitNameRef(nameRef: JsNameRef): Unit =
|
||||
visitElement(nameRef)
|
||||
|
||||
@@ -151,7 +154,7 @@ abstract class JsVisitor {
|
||||
visitElement(x)
|
||||
|
||||
open fun visitWhile(x: JsWhile): Unit =
|
||||
visitElement(x)
|
||||
visitLoop(x)
|
||||
|
||||
open fun visitDocComment(comment: JsDocComment): Unit =
|
||||
visitElement(comment)
|
||||
|
||||
@@ -104,6 +104,7 @@ public abstract class JsVisitorWithContext {
|
||||
}
|
||||
|
||||
public void endVisit(@NotNull JsDoWhile x, @NotNull JsContext ctx) {
|
||||
endVisit((JsLoop) x, ctx);
|
||||
}
|
||||
|
||||
public void endVisit(@NotNull JsEmpty x, @NotNull JsContext ctx) {
|
||||
@@ -113,9 +114,11 @@ public abstract class JsVisitorWithContext {
|
||||
}
|
||||
|
||||
public void endVisit(@NotNull JsFor x, @NotNull JsContext ctx) {
|
||||
endVisit((JsLoop) x, ctx);
|
||||
}
|
||||
|
||||
public void endVisit(@NotNull JsForIn x, @NotNull JsContext ctx) {
|
||||
endVisit((JsLoop) x, ctx);
|
||||
}
|
||||
|
||||
public void endVisit(@NotNull JsFunction x, @NotNull JsContext ctx) {
|
||||
@@ -132,6 +135,9 @@ public abstract class JsVisitorWithContext {
|
||||
public void endVisit(@NotNull JsLabel x, @NotNull JsContext ctx) {
|
||||
}
|
||||
|
||||
public void endVisit(@NotNull JsLoop x, @NotNull JsContext ctx) {
|
||||
}
|
||||
|
||||
public void endVisit(@NotNull JsName x, @NotNull JsContext ctx) {
|
||||
}
|
||||
|
||||
@@ -202,6 +208,7 @@ public abstract class JsVisitorWithContext {
|
||||
}
|
||||
|
||||
public void endVisit(@NotNull JsWhile x, @NotNull JsContext ctx) {
|
||||
endVisit((JsLoop) x, ctx);
|
||||
}
|
||||
|
||||
public boolean visit(@NotNull JsArrayAccess x, @NotNull JsContext ctx) {
|
||||
@@ -253,7 +260,7 @@ public abstract class JsVisitorWithContext {
|
||||
}
|
||||
|
||||
public boolean visit(@NotNull JsDoWhile x, @NotNull JsContext ctx) {
|
||||
return true;
|
||||
return visit((JsLoop) x, ctx);
|
||||
}
|
||||
|
||||
public boolean visit(@NotNull JsEmpty x, @NotNull JsContext ctx) {
|
||||
@@ -265,11 +272,11 @@ public abstract class JsVisitorWithContext {
|
||||
}
|
||||
|
||||
public boolean visit(@NotNull JsFor x, @NotNull JsContext ctx) {
|
||||
return true;
|
||||
return visit((JsLoop) x, ctx);
|
||||
}
|
||||
|
||||
public boolean visit(@NotNull JsForIn x, @NotNull JsContext ctx) {
|
||||
return true;
|
||||
return visit((JsLoop) x, ctx);
|
||||
}
|
||||
|
||||
public boolean visit(@NotNull JsFunction x, @NotNull JsContext ctx) {
|
||||
@@ -288,6 +295,10 @@ public abstract class JsVisitorWithContext {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(@NotNull JsLoop x, @NotNull JsContext ctx) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(@NotNull JsName x, @NotNull JsContext ctx) {
|
||||
return true;
|
||||
}
|
||||
@@ -369,7 +380,7 @@ public abstract class JsVisitorWithContext {
|
||||
}
|
||||
|
||||
public boolean visit(@NotNull JsWhile x, @NotNull JsContext ctx) {
|
||||
return true;
|
||||
return visit((JsLoop) x, ctx);
|
||||
}
|
||||
|
||||
protected abstract <T extends JsNode> T doAccept(T node);
|
||||
|
||||
@@ -10,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
/**
|
||||
* A JavaScript <code>while</code> statement.
|
||||
*/
|
||||
public class JsWhile extends SourceInfoAwareJsNode implements JsStatement {
|
||||
public class JsWhile extends SourceInfoAwareJsNode implements JsLoop {
|
||||
protected JsStatement body;
|
||||
protected JsExpression condition;
|
||||
|
||||
|
||||
@@ -123,17 +123,9 @@ internal class DoWhileGuardElimination(private val root: JsStatement) {
|
||||
|
||||
private fun isInLoop() = loopLevel > 0
|
||||
|
||||
override fun visitDoWhile(x: JsDoWhile) = enterLoop { super.visitDoWhile(x) }
|
||||
|
||||
override fun visitWhile(x: JsWhile) = enterLoop { super.visitWhile(x) }
|
||||
|
||||
override fun visitFor(x: JsFor) = enterLoop { super.visitFor(x) }
|
||||
|
||||
override fun visitForIn(x: JsForIn) = enterLoop { super.visitForIn(x) }
|
||||
|
||||
private inline fun enterLoop(action: () -> Unit) {
|
||||
override fun visitLoop(x: JsLoop) {
|
||||
loopLevel++
|
||||
action()
|
||||
super.visitLoop(x)
|
||||
loopLevel--
|
||||
}
|
||||
|
||||
|
||||
+5
-26
@@ -28,14 +28,6 @@ object LabeledBlockToDoWhileTransformation {
|
||||
}
|
||||
}
|
||||
|
||||
fun JsStatement.isLoop(): Boolean = when (this) {
|
||||
is JsWhile,
|
||||
is JsDoWhile,
|
||||
is JsFor,
|
||||
is JsForIn -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
fun apply(root: JsNode) {
|
||||
object : JsVisitorWithContextImpl() {
|
||||
val loopStack = Stack<JsStatement>()
|
||||
@@ -58,28 +50,18 @@ object LabeledBlockToDoWhileTransformation {
|
||||
}
|
||||
|
||||
override fun visit(x: JsLabel, ctx: JsContext<JsNode>): Boolean {
|
||||
if (x.statement.isLoop()) {
|
||||
if (x.statement is JsLoop) {
|
||||
loopLabels[x.statement] = x
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun visit(x: JsWhile, ctx: JsContext<JsNode>) = visitLoop(x)
|
||||
override fun visit(x: JsDoWhile, ctx: JsContext<JsNode>) = visitLoop(x)
|
||||
override fun visit(x: JsFor, ctx: JsContext<JsNode>) = visitLoop(x)
|
||||
override fun visit(x: JsForIn, ctx: JsContext<JsNode>) = visitLoop(x)
|
||||
|
||||
override fun endVisit(x: JsWhile, ctx: JsContext<JsNode>) = endVisitLoop(x, ctx)
|
||||
override fun endVisit(x: JsDoWhile, ctx: JsContext<JsNode>) = endVisitLoop(x, ctx)
|
||||
override fun endVisit(x: JsFor, ctx: JsContext<JsNode>) = endVisitLoop(x, ctx)
|
||||
override fun endVisit(x: JsForIn, ctx: JsContext<JsNode>) = endVisitLoop(x, ctx)
|
||||
|
||||
private fun visitLoop(x: JsStatement): Boolean {
|
||||
override fun visit(x: JsLoop, ctx: JsContext<JsNode>): Boolean {
|
||||
loopStack.push(x)
|
||||
return true
|
||||
}
|
||||
|
||||
private fun endVisitLoop(x: JsStatement, ctx: JsContext<JsNode>) {
|
||||
override fun endVisit(x: JsLoop, ctx: JsContext<JsNode>) {
|
||||
loopStack.pop()
|
||||
if (loopsToLabel.contains(x)) {
|
||||
// Reuse loop label if present. Otherwise create new label.
|
||||
@@ -102,11 +84,8 @@ object LabeledBlockToDoWhileTransformation {
|
||||
*/
|
||||
private fun labelLoopBreaksAndContinues(loop: JsStatement, fakeLoops: Set<JsDoWhile>, label: JsNameRef) {
|
||||
object : JsVisitorWithContextImpl() {
|
||||
override fun visit(x: JsWhile, ctx: JsContext<JsNode>): Boolean = visitLoop(x)
|
||||
override fun visit(x: JsDoWhile, ctx: JsContext<JsNode>): Boolean = visitLoop(x)
|
||||
override fun visit(x: JsFor, ctx: JsContext<JsNode>): Boolean = visitLoop(x)
|
||||
override fun visit(x: JsForIn, ctx: JsContext<JsNode>): Boolean = visitLoop(x)
|
||||
private fun visitLoop(x: JsStatement): Boolean = fakeLoops.contains(x) || x === loop
|
||||
override fun visit(x: JsLoop, ctx: JsContext<JsNode>): Boolean =
|
||||
fakeLoops.contains(x) || x === loop
|
||||
|
||||
override fun endVisit(x: JsBreak, ctx: JsContext<JsNode>) {
|
||||
if (x.label == null)
|
||||
|
||||
+1
-7
@@ -183,13 +183,7 @@ internal class TemporaryVariableElimination(private val function: JsFunction) {
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitFor(x: JsFor) = withNewScope { super.visitFor(x) }
|
||||
|
||||
override fun visitForIn(x: JsForIn) = withNewScope { super.visitForIn(x) }
|
||||
|
||||
override fun visitWhile(x: JsWhile) = withNewScope { super.visitWhile(x) }
|
||||
|
||||
override fun visitDoWhile(x: JsDoWhile) = withNewScope { super.visitDoWhile(x) }
|
||||
override fun visitLoop(x: JsLoop) = withNewScope { super.visitLoop(x) }
|
||||
|
||||
override fun visitIf(x: JsIf) {
|
||||
accept(x.ifExpression)
|
||||
|
||||
+2
-41
@@ -34,53 +34,14 @@ class ContinueReplacingVisitor(val loopLabelName: JsName?, val guardLabelName: J
|
||||
return false
|
||||
}
|
||||
|
||||
override fun visit(x: JsWhile, ctx: JsContext<JsNode>): Boolean {
|
||||
override fun visit(x: JsLoop, ctx: JsContext<JsNode>): Boolean {
|
||||
if (loopLabelName == null) return false
|
||||
|
||||
loopNestingLevel++
|
||||
return super.visit(x, ctx)
|
||||
}
|
||||
|
||||
override fun endVisit(x: JsWhile, ctx: JsContext<JsNode>) {
|
||||
super.endVisit(x, ctx)
|
||||
if (loopLabelName == null) return
|
||||
loopNestingLevel--
|
||||
}
|
||||
|
||||
override fun visit(x: JsDoWhile, ctx: JsContext<JsNode>): Boolean {
|
||||
if (loopLabelName == null) return false
|
||||
|
||||
loopNestingLevel++
|
||||
return super.visit(x, ctx)
|
||||
}
|
||||
|
||||
override fun endVisit(x: JsDoWhile, ctx: JsContext<JsNode>) {
|
||||
super.endVisit(x, ctx)
|
||||
if (loopLabelName == null) return
|
||||
loopNestingLevel--
|
||||
}
|
||||
|
||||
override fun visit(x: JsFor, ctx: JsContext<JsNode>): Boolean {
|
||||
if (loopLabelName == null) return false
|
||||
|
||||
loopNestingLevel++
|
||||
return super.visit(x, ctx)
|
||||
}
|
||||
|
||||
override fun endVisit(x: JsFor, ctx: JsContext<JsNode>) {
|
||||
super.endVisit(x, ctx)
|
||||
if (loopLabelName == null) return
|
||||
loopNestingLevel--
|
||||
}
|
||||
|
||||
override fun visit(x: JsForIn, ctx: JsContext<JsNode>): Boolean {
|
||||
if (loopLabelName == null) return false
|
||||
|
||||
loopNestingLevel++
|
||||
return super.visit(x, ctx)
|
||||
}
|
||||
|
||||
override fun endVisit(x: JsForIn, ctx: JsContext<JsNode>) {
|
||||
override fun endVisit(x: JsLoop, ctx: JsContext<JsNode>) {
|
||||
super.endVisit(x, ctx)
|
||||
if (loopLabelName == null) return
|
||||
loopNestingLevel--
|
||||
|
||||
Reference in New Issue
Block a user