Skip to content

Commit 55caff0

Browse files
committed
Clean up, reword comments and fix typos
1 parent c95ad71 commit 55caff0

12 files changed

+83
-100
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fails(GR-30031):Numbered parameters warns when numbered parameter is overwritten with local variable
1+
fails(GR-30031):Numbered parameters warns when numbered parameter is overwritten with local variable

src/main/java/org/truffleruby/core/inlined/CoreMethodAssumptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public RubyContextSourceNode createCallNode(RubyCallNodeParameters callParameter
158158
final RubyNode[] args = callParameters.getArguments();
159159
int n = 1 /* self */ + args.length;
160160

161-
if (callParameters.getBlock() != null) { // TODO: could we also handle a `proc` call?
161+
if (callParameters.getBlock() != null) {
162162
if (callParameters.getMethodName().equals("lambda") &&
163163
(callParameters.getBlock() instanceof BlockDefinitionNode)) {
164164

src/main/java/org/truffleruby/debug/TruffleASTPrinter.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
package org.truffleruby.debug;
1111

1212
import java.util.ArrayList;
13+
import java.util.Collections;
1314
import java.util.Comparator;
1415
import java.util.HashMap;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.Set;
18-
import java.util.TreeMap;
1919
import java.util.TreeSet;
2020
import java.util.regex.Matcher;
2121

@@ -152,7 +152,6 @@ private static List<Pair<String, Object>> getNodeAttributes(Node node) {
152152

153153
// map frame slots to local variable names
154154
private static Map<String, String> getNodeAttributeAnnotations(Node node) {
155-
final TreeMap<String, String> annotations = new TreeMap<>();
156155
final int frameSlot;
157156

158157
// ignore WriteDeclarationVariableNode and ReadDeclarationVariableNode
@@ -167,25 +166,15 @@ private static Map<String, String> getNodeAttributeAnnotations(Node node) {
167166
} else if (node instanceof ReadFrameSlotNode readFrameSlotNode) {
168167
frameSlot = readFrameSlotNode.getFrameSlot();
169168
} else {
170-
return annotations;
169+
return Collections.emptyMap();
171170
}
172171

173-
Node parent = node.getParent();
174-
175-
while (parent != null && !(parent instanceof RootNode)) {
176-
parent = parent.getParent();
177-
}
178-
179-
assert parent != null;
180-
181-
final var rootNode = (RootNode) parent;
172+
final var rootNode = node.getRootNode();
182173
final var frameDescriptor = rootNode.getFrameDescriptor();
183174
final String variableName = frameDescriptor.getSlotName(frameSlot).toString();
184175

185176
// all the mentioned above classes use the same field name - "frameSlot", so just hardcode it
186-
annotations.put("frameSlot", variableName);
187-
188-
return annotations;
177+
return Collections.singletonMap("frameSlot", variableName);
189178
}
190179

191180
private static List<Pair<String, Object>> getNodeChildren(Node node) {

src/main/java/org/truffleruby/language/arguments/ReadUserKeywordsHashNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public RubyHash execute(VirtualFrame frame) {
2626
if (keywordArgumentsProfile.profile(descriptor instanceof KeywordArgumentsDescriptor)) {
2727
final RubyHash keywords = (RubyHash) RubyArguments.getLastArgument(frame);
2828
assert !keywords.empty();
29-
assert assertHashMatchesDescriptor(keywords, (KeywordArgumentsDescriptor) descriptor); // TODO: seems incorrect as far as descriptor contains keyword names from the arguments, not declared in a called method
29+
assert assertHashMatchesDescriptor(keywords, (KeywordArgumentsDescriptor) descriptor);
3030
return keywords;
3131
} else {
3232
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private static RubyNode composeBody(TranslatorEnvironment environment, SourceInd
371371

372372
body = sequence(sourceSection, Arrays.asList(prelude, body));
373373

374-
if (environment.getFlipFlopStates().size() > 0) { // TODO: it could be called twice in case of conversion block->lambda or vise versa
374+
if (environment.getFlipFlopStates().size() > 0) {
375375
body = sequence(sourceSection, Arrays.asList(initFlipFlopStates(environment, sourceSection), body));
376376
}
377377

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,30 @@
3636
import com.oracle.truffle.api.frame.FrameDescriptor;
3737
import org.truffleruby.language.objects.SelfNode;
3838
import org.truffleruby.language.threadlocal.SpecialVariableStorage;
39+
import org.truffleruby.parser.parser.ParserSupport;
3940

4041
public final class TranslatorEnvironment {
4142

43+
/* Names of hidden local variables.
44+
*
45+
* For each parameter in methods and blocks a local variable is declared to keep actual argument value. Use the
46+
* following names for parameters that don't have explicit names - anonymous rest, keyword rest and block.
47+
*
48+
* Store values of anonymous parameters to forward them either implicitly to a super method call or explicitly to a
49+
* method call with *, **, & or "...". */
50+
51+
/** local variable to access a block argument */
4252
public static final String METHOD_BLOCK_NAME = Layouts.TEMP_PREFIX + "method_block_arg";
53+
/** local variable name for * parameter */
54+
static final String DEFAULT_REST_NAME = ParserSupport.REST_VAR;
55+
/** local variable name for ** parameter */
56+
static final String DEFAULT_KEYWORD_REST_NAME = ParserSupport.KWREST_VAR;
57+
/** local variable name for * parameter caused by desugaring ... parameter (forward-everything) */
58+
static final String FORWARDED_REST_NAME = ParserSupport.FORWARD_ARGS_REST_VAR;
59+
/** local variable name for ** parameter caused by desugaring ... parameter (forward-everything) */
60+
static final String FORWARDED_KEYWORD_REST_NAME = ParserSupport.FORWARD_ARGS_KWREST_VAR;
61+
/** local variable name for & parameter caused by desugaring ... parameter (forward-everything) */
62+
static final String FORWARDED_BLOCK_NAME = ParserSupport.FORWARD_ARGS_BLOCK_VAR;
4363

4464
private final ParseEnvironment parseEnvironment;
4565

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@
4040
import org.truffleruby.language.methods.Arity;
4141
import org.truffleruby.language.methods.BlockDefinitionNodeGen;
4242

43-
import java.util.ArrayDeque;
4443
import java.util.Arrays;
45-
import java.util.Deque;
4644
import java.util.function.Supplier;
4745

4846
public final class YARPBlockNodeTranslator extends YARPTranslator {
@@ -139,7 +137,7 @@ public RubyNode compileBlockNode(Nodes.Node body, String[] locals, boolean isSta
139137
} else {
140138
// 100% proc
141139

142-
// it could be converted lately to lambda, e.g. if passed as argument to Module#define_method
140+
// it could be converted later to lambda, e.g. if passed as argument to Module#define_method
143141
callTargets = new ProcCallTargets(procCompiler.get(), null, lambdaCompiler);
144142
}
145143

@@ -225,8 +223,6 @@ private static Supplier<RootCallTarget> procCompiler(
225223
? body.cloneUninitialized() // previously compiled as lambda, must copy
226224
: body;
227225

228-
// TODO: shouldn't we clone preludeProc as well?
229-
// It contains loadArguments node that could be used for lambda
230226
final RubyNode bodyProc = composeBody(environment, preludeProc, bodyForProc);
231227

232228
final RubyProcRootNode newRootNodeForProcs = new RubyProcRootNode(
@@ -249,8 +245,8 @@ private static Supplier<RootCallTarget> procCompiler(
249245
// Note that the compilation to lambda does not alter the original returnID (instead it's "hijacked"
250246
// and used in RubyLambdaRootNode).
251247
//
252-
// This needs to run after nodes are adopted for replace() (during callTarget initialisation) to work
253-
// and nodes to know their parent.
248+
// replace() should be called **after** nodes are adopted (in RootNode#getCallTarget() using adoptChildren())
249+
// so nodes know their parent.
254250
for (DynamicReturnNode returnNode : NodeUtil
255251
.findAllNodeInstances(bodyForProc, DynamicReturnNode.class)) {
256252
if (returnNode.returnID == ReturnID.MODULE_BODY) {

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,8 @@
2222
import com.oracle.truffle.api.nodes.Node;
2323
import com.oracle.truffle.api.source.Source;
2424
import org.prism.Nodes;
25-
import org.truffleruby.parser.parser.ParserSupport;
2625

2726
public final class YARPDefNodeTranslator extends YARPTranslator {
28-
// TODO: not the best place for these constants. Is YARPLoadArgumentsTranslator better? Or YARPTranslator?
29-
static final String DEFAULT_KEYWORD_REST_NAME = ParserSupport.KWREST_VAR; // local variable name for ** parameter
30-
static final String DEFAULT_REST_NAME = ParserSupport.REST_VAR; // local variable name for * parameter
31-
static final String FORWARDED_REST_NAME = ParserSupport.FORWARD_ARGS_REST_VAR; // local variable name for * parameter caused by desugaring ...
32-
static final String FORWARDED_KEYWORD_REST_NAME = ParserSupport.FORWARD_ARGS_KWREST_VAR; // local variable name for ** parameter caused by desugaring ...
33-
static final String FORWARDED_BLOCK_NAME = ParserSupport.FORWARD_ARGS_BLOCK_VAR; // local variable name for & parameter caused by desugaring ...
34-
3527
private final boolean shouldLazyTranslate;
3628

3729
public YARPDefNodeTranslator(
@@ -105,15 +97,15 @@ private void declareLocalVariables(Nodes.DefNode node) {
10597

10698
for (String name : node.locals) {
10799
switch (name) {
108-
case "*" -> environment.declareVar(DEFAULT_REST_NAME);
109-
case "**" -> environment.declareVar(DEFAULT_KEYWORD_REST_NAME);
100+
case "*" -> environment.declareVar(TranslatorEnvironment.DEFAULT_REST_NAME);
101+
case "**" -> environment.declareVar(TranslatorEnvironment.DEFAULT_KEYWORD_REST_NAME);
110102
case "&" ->
111103
// we don't support yet Ruby 3.1's anonymous block parameter
112104
throw CompilerDirectives.shouldNotReachHere("Anonymous block parameters aren't supported yet");
113105
case "..." -> {
114-
environment.declareVar(YARPDefNodeTranslator.FORWARDED_REST_NAME);
115-
environment.declareVar(YARPDefNodeTranslator.FORWARDED_KEYWORD_REST_NAME);
116-
environment.declareVar(YARPDefNodeTranslator.FORWARDED_BLOCK_NAME);
106+
environment.declareVar(TranslatorEnvironment.FORWARDED_REST_NAME);
107+
environment.declareVar(TranslatorEnvironment.FORWARDED_KEYWORD_REST_NAME);
108+
environment.declareVar(TranslatorEnvironment.FORWARDED_BLOCK_NAME);
117109
}
118110
default -> environment.declareVar(name);
119111
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ public RubyNode visitRestParameterNode(Nodes.RestParameterNode node) {
239239
int to = -parametersNode.posts.length;
240240
readNode = new ReadRestArgumentNode(from, -to, hasKeywordArguments());
241241

242-
final String name = (node.name != null) ? node.name : YARPDefNodeTranslator.DEFAULT_REST_NAME;
242+
final String name = (node.name != null) ? node.name : TranslatorEnvironment.DEFAULT_REST_NAME;
243243

244-
// When a rest parameter in a block is nameless then YARP doesn't add '*' to block's locals,
245-
// and we don't declare this hidden variable beforehand.
246-
// So declare it here right before usage.
244+
// When a rest parameter in a block is nameless then YARP doesn't add '*' to block's locals
245+
// (what is expected as far as arguments forwarding doesn't work in blocks), and we don't
246+
// declare this hidden variable beforehand. So declare it here right before usage.
247247
final int slot = environment.declareVar(name);
248248

249249
return new WriteLocalVariableNode(slot, readNode);
@@ -252,11 +252,11 @@ public RubyNode visitRestParameterNode(Nodes.RestParameterNode node) {
252252
@Override
253253
public RubyNode visitKeywordRestParameterNode(Nodes.KeywordRestParameterNode node) {
254254
final RubyNode readNode = new ReadKeywordRestArgumentNode(language, arity);
255-
final String name = (node.name != null) ? node.name : YARPDefNodeTranslator.DEFAULT_KEYWORD_REST_NAME;
255+
final String name = (node.name != null) ? node.name : TranslatorEnvironment.DEFAULT_KEYWORD_REST_NAME;
256256

257-
// When a keyword rest parameter in a block is nameless then YARP doesn't add '**' to block's locals,
258-
// and we don't declare this hidden variable beforehand.
259-
// So declare it here right before usage.
257+
// When a keyword rest parameter in a block is nameless then YARP doesn't add '**' to block's locals
258+
// (what is expected as far as arguments forwarding doesn't work in blocks), and we don't declare this
259+
// hidden variable beforehand. So declare it here right before usage.
260260
final int slot = environment.declareVar(name);
261261

262262
return new WriteLocalVariableNode(slot, readNode);
@@ -281,9 +281,9 @@ public RubyNode visitForwardingParameterNode(Nodes.ForwardingParameterNode node)
281281
ArrayList<RubyNode> sequence = new ArrayList<>();
282282

283283
// desugar ... to *, **, and & parameters
284-
final var rest = new Nodes.RestParameterNode(YARPDefNodeTranslator.FORWARDED_REST_NAME, 0, 0);
285-
final var keyrest = new Nodes.KeywordRestParameterNode(YARPDefNodeTranslator.FORWARDED_KEYWORD_REST_NAME, 0, 0);
286-
final var block = new Nodes.BlockParameterNode(YARPDefNodeTranslator.FORWARDED_BLOCK_NAME, 0, 0);
284+
final var rest = new Nodes.RestParameterNode(TranslatorEnvironment.FORWARDED_REST_NAME, 0, 0);
285+
final var keyrest = new Nodes.KeywordRestParameterNode(TranslatorEnvironment.FORWARDED_KEYWORD_REST_NAME, 0, 0);
286+
final var block = new Nodes.BlockParameterNode(TranslatorEnvironment.FORWARDED_BLOCK_NAME, 0, 0);
287287

288288
sequence.add(rest.accept(this));
289289
sequence.add(keyrest.accept(this));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public AssignableNode visitSplatNode(Nodes.SplatNode node) {
144144
@Override
145145
public AssignableNode visitRequiredParameterNode(Nodes.RequiredParameterNode node) {
146146
// TODO: this could be done more directly but the logic of visitLocalVariableWriteNode() needs to be simpler first
147-
// TODO: depth is not suppose to be used anyway so pass 0 value.
147+
// NOTE: depth is not supposed to be used anyway so pass 0 value.
148148
final RubyNode rubyNode = yarpTranslator.visitLocalVariableWriteNode(
149149
new Nodes.LocalVariableWriteNode(node.name, 0, null, node.startOffset, node.length));
150150
return ((AssignableNode) rubyNode).toAssignableNode();

0 commit comments

Comments
 (0)