Skip to content

Commit c821a7f

Browse files
committed
Refactor and rely on environments chain instead of #parent field in Translators to create flip-flop's state
1 parent 34ffb5b commit c821a7f

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

src/main/java/org/truffleruby/parser/YARPBlockNodeTranslator.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.truffleruby.language.control.InvalidReturnNode;
3434
import org.truffleruby.language.control.NotNodeGen;
3535
import org.truffleruby.language.control.ReturnID;
36-
import org.truffleruby.language.locals.FindDeclarationVariableNodes;
3736
import org.truffleruby.language.locals.LocalVariableType;
3837
import org.truffleruby.language.locals.ReadLocalVariableNode;
3938
import org.truffleruby.language.locals.WriteLocalVariableNode;
@@ -47,8 +46,6 @@ public final class YARPBlockNodeTranslator extends YARPTranslator {
4746
private final Nodes.ParametersNode parameters;
4847
private final Arity arity;
4948
private final String currentCallMethodName;
50-
// used to find the nearest outer non-block lexical scope
51-
private final YARPTranslator parent;
5249

5350
public YARPBlockNodeTranslator(
5451
RubyLanguage language,
@@ -57,13 +54,11 @@ public YARPBlockNodeTranslator(
5754
Source source,
5855
Nodes.ParametersNode parameters,
5956
Arity arity,
60-
String currentCallMethodName,
61-
YARPTranslator parent) {
57+
String currentCallMethodName) {
6258
super(language, environment, sourceBytes, source, null, null, null);
6359
this.parameters = parameters;
6460
this.arity = arity;
6561
this.currentCallMethodName = currentCallMethodName;
66-
this.parent = parent;
6762
}
6863

6964
public RubyNode compileBlockNode(Nodes.Node body, String[] locals, boolean isStabbyLambda,
@@ -344,10 +339,4 @@ private boolean shouldConsiderDestructuringArrayArg(Arity arity) {
344339
}
345340
}
346341

347-
@Override
348-
// local variable will be declared in the nearest outer non-block lexical scope
349-
protected FindDeclarationVariableNodes.FrameSlotAndDepth createFlipFlopState(int depth) {
350-
return parent.createFlipFlopState(depth + 1);
351-
}
352-
353342
}

src/main/java/org/truffleruby/parser/YARPTranslator.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
import org.truffleruby.language.literal.ObjectLiteralNode;
101101
import org.truffleruby.language.literal.StringLiteralNode;
102102
import org.truffleruby.language.literal.TruffleInternalModuleLiteralNode;
103-
import org.truffleruby.language.locals.FindDeclarationVariableNodes;
103+
import org.truffleruby.language.locals.FindDeclarationVariableNodes.FrameSlotAndDepth;
104104
import org.truffleruby.language.locals.FlipFlopNodeGen;
105105
import org.truffleruby.language.locals.InitFlipFlopSlotNode;
106106
import org.truffleruby.language.locals.ReadLocalNode;
@@ -499,8 +499,7 @@ private RubyNode translateBlockAndLambda(Nodes.Node node, Nodes.BlockParametersN
499499
source,
500500
parameters,
501501
arity,
502-
currentCallMethodName,
503-
this);
502+
currentCallMethodName);
504503

505504
methodCompiler.frameOnStackMarkerSlotStack = frameOnStackMarkerSlotStack;
506505

@@ -1201,7 +1200,7 @@ public RubyNode visitFlipFlopNode(Nodes.FlipFlopNode node) {
12011200
final RubyNode begin = node.left.accept(this);
12021201
final RubyNode end = node.right.accept(this);
12031202

1204-
final FindDeclarationVariableNodes.FrameSlotAndDepth slotAndDepth = createFlipFlopState(0);
1203+
final var slotAndDepth = createFlipFlopState();
12051204
final RubyNode rubyNode = FlipFlopNodeGen.create(begin, end, node.isExcludeEnd(), slotAndDepth.depth,
12061205
slotAndDepth.slot);
12071206

@@ -2014,10 +2013,13 @@ protected RubyNode defaultVisit(Nodes.Node node) {
20142013
throw new Error("Unknown node: " + node);
20152014
}
20162015

2017-
protected FindDeclarationVariableNodes.FrameSlotAndDepth createFlipFlopState(int depth) {
2018-
final int frameSlot = environment.declareLocalTemp("flipflop");
2019-
environment.getFlipFlopStates().add(frameSlot);
2020-
return new FindDeclarationVariableNodes.FrameSlotAndDepth(frameSlot, depth);
2016+
/** Declare variable in the nearest non-block outer lexical scope - either method, class or top-level */
2017+
protected FrameSlotAndDepth createFlipFlopState() {
2018+
final var target = environment.getSurroundingMethodEnvironment();
2019+
final int frameSlot = target.declareLocalTemp("flipflop");
2020+
target.getFlipFlopStates().add(frameSlot);
2021+
2022+
return new FrameSlotAndDepth(frameSlot, environment.getBlockDepth());
20212023
}
20222024

20232025
/** Translate a list of nodes, e.g. break/return operands, into an array producing node. It returns ArrayLiteralNode

0 commit comments

Comments
 (0)