|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, 2023 Oracle and/or its affiliates. All rights reserved. This |
| 3 | + * code is released under a tri EPL/GPL/LGPL license. You can use it, |
| 4 | + * redistribute it and/or modify it under the terms of the: |
| 5 | + * |
| 6 | + * Eclipse Public License version 2.0, or |
| 7 | + * GNU General Public License version 2, or |
| 8 | + * GNU Lesser General Public License version 2.1. |
| 9 | + */ |
| 10 | +package org.truffleruby.parser; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import org.prism.AbstractNodeVisitor; |
| 16 | +import org.prism.Nodes; |
| 17 | +import org.truffleruby.RubyLanguage; |
| 18 | +import org.truffleruby.core.hash.ConcatHashLiteralNode; |
| 19 | +import org.truffleruby.core.hash.HashLiteralNode; |
| 20 | +import org.truffleruby.language.RubyNode; |
| 21 | +import org.truffleruby.language.arguments.MissingArgumentBehavior; |
| 22 | +import org.truffleruby.language.arguments.ReadKeywordArgumentNode; |
| 23 | +import org.truffleruby.language.arguments.ReadPreArgumentNode; |
| 24 | +import org.truffleruby.language.literal.NilLiteralNode; |
| 25 | +import org.truffleruby.language.literal.ObjectLiteralNode; |
| 26 | + |
| 27 | + |
| 28 | +/** Produces code to reload arguments from local variables back into the arguments array. Only works for simple cases. |
| 29 | + * Used for zsuper calls which pass the same arguments, but will pick up modifications made to them in the method so |
| 30 | + * far. */ |
| 31 | +public final class YARPReloadArgumentsTranslator extends AbstractNodeVisitor<RubyNode> { |
| 32 | + |
| 33 | + private final RubyLanguage language; |
| 34 | + private final YARPTranslator yarpTranslator; |
| 35 | + private final boolean hasKeywordArguments; |
| 36 | + |
| 37 | + private int index = 0; |
| 38 | + private int restParameterIndex = -1; |
| 39 | + |
| 40 | + public YARPReloadArgumentsTranslator( |
| 41 | + RubyLanguage language, |
| 42 | + YARPTranslator yarpTranslator, |
| 43 | + Nodes.ParametersNode parametersNode) { |
| 44 | + this.language = language; |
| 45 | + this.yarpTranslator = yarpTranslator; |
| 46 | + this.hasKeywordArguments = parametersNode.keywords.length > 0 || parametersNode.keyword_rest != null; |
| 47 | + } |
| 48 | + |
| 49 | + public int getRestParameterIndex() { |
| 50 | + return restParameterIndex; |
| 51 | + } |
| 52 | + |
| 53 | + public RubyNode[] reload(Nodes.ParametersNode parametersNode) { |
| 54 | + final List<RubyNode> sequence = new ArrayList<>(); |
| 55 | + |
| 56 | + for (var node : parametersNode.requireds) { |
| 57 | + sequence.add(node.accept(this)); // Nodes.RequiredParameterNode is expected here |
| 58 | + index++; |
| 59 | + } |
| 60 | + |
| 61 | + for (var node : parametersNode.optionals) { |
| 62 | + sequence.add(node.accept(this)); // Nodes.OptionalParameterNode is expected here |
| 63 | + index++; |
| 64 | + } |
| 65 | + |
| 66 | + if (parametersNode.rest != null) { |
| 67 | + restParameterIndex = index; |
| 68 | + sequence.add(parametersNode.rest.accept(this)); // Nodes.RestParameterNode is expected here |
| 69 | + } |
| 70 | + |
| 71 | + int postCount = parametersNode.posts.length; |
| 72 | + if (postCount > 0) { |
| 73 | + index = -postCount; |
| 74 | + for (var node : parametersNode.posts) { |
| 75 | + sequence.add(node.accept(this)); // Nodes.RequiredParameterNode is expected here |
| 76 | + index++; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + RubyNode kwArgsNode = null; |
| 81 | + |
| 82 | + if (parametersNode.keywords.length > 0) { |
| 83 | + final int keywordCount = parametersNode.keywords.length; |
| 84 | + RubyNode[] keyValues = new RubyNode[keywordCount * 2]; |
| 85 | + |
| 86 | + for (int i = 0; i < keywordCount; i++) { |
| 87 | + // Nodes.RequiredKeywordParameterNode/Nodes.OptionalKeywordParameterNode are expected here |
| 88 | + final Nodes.Node kwArg = parametersNode.keywords[i]; |
| 89 | + final RubyNode value = kwArg.accept(this); |
| 90 | + var name = ((ReadKeywordArgumentNode) value).getName(); |
| 91 | + RubyNode key = new ObjectLiteralNode(name); |
| 92 | + keyValues[2 * i] = key; |
| 93 | + keyValues[2 * i + 1] = value; |
| 94 | + } |
| 95 | + kwArgsNode = HashLiteralNode.create(keyValues); |
| 96 | + } |
| 97 | + |
| 98 | + if (parametersNode.keyword_rest != null) { |
| 99 | + // Nodes.KeywordRestParameterNode/Nodes.NoKeywordsParameterNode are expected here |
| 100 | + final RubyNode keyRest = parametersNode.keyword_rest.accept(this); |
| 101 | + if (kwArgsNode == null) { |
| 102 | + kwArgsNode = keyRest; |
| 103 | + } else { |
| 104 | + kwArgsNode = new ConcatHashLiteralNode(new RubyNode[]{ kwArgsNode, keyRest }); |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + if (kwArgsNode != null) { |
| 110 | + sequence.add(kwArgsNode); |
| 111 | + } |
| 112 | + |
| 113 | + return sequence.toArray(RubyNode.EMPTY_ARRAY); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public RubyNode visitRequiredParameterNode(Nodes.RequiredParameterNode node) { |
| 118 | + return yarpTranslator.getEnvironment().findLocalVarNode(node.name, null); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public RubyNode visitOptionalParameterNode(Nodes.OptionalParameterNode node) { |
| 123 | + return yarpTranslator.getEnvironment().findLocalVarNode(node.name, null); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public RubyNode visitMultiTargetNode(Nodes.MultiTargetNode node) { |
| 128 | + return Translator.profileArgument(language, |
| 129 | + new ReadPreArgumentNode(index, hasKeywordArguments, MissingArgumentBehavior.NIL)); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public RubyNode visitRestParameterNode(Nodes.RestParameterNode node) { |
| 134 | + return yarpTranslator.getEnvironment().findLocalVarNode(node.name, null); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public RubyNode visitRequiredKeywordParameterNode(Nodes.RequiredKeywordParameterNode node) { |
| 139 | + return yarpTranslator.getEnvironment().findLocalVarNode(node.name, null); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public RubyNode visitOptionalKeywordParameterNode(Nodes.OptionalKeywordParameterNode node) { |
| 144 | + return yarpTranslator.getEnvironment().findLocalVarNode(node.name, null); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public RubyNode visitKeywordRestParameterNode(Nodes.KeywordRestParameterNode node) { |
| 149 | + return yarpTranslator.getEnvironment().findLocalVarNode(node.name, null); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public RubyNode visitNoKeywordsParameterNode(Nodes.NoKeywordsParameterNode node) { |
| 154 | + return defaultVisit(node); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + protected RubyNode defaultVisit(Nodes.Node node) { |
| 159 | + var nilNode = new NilLiteralNode(); |
| 160 | + return YARPTranslator.assignPositionAndFlags(node, nilNode); |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments