|
| 1 | +package com.oracle.graal.python.util; |
| 2 | + |
| 3 | +import com.oracle.graal.python.nodes.PRootNode; |
| 4 | +import com.oracle.graal.python.nodes.argument.ReadArgumentNode; |
| 5 | +import com.oracle.graal.python.nodes.frame.WriteIdentifierNode; |
| 6 | +import com.oracle.graal.python.runtime.interop.InteropArray; |
| 7 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 8 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 9 | +import com.oracle.truffle.api.interop.TruffleObject; |
| 10 | +import com.oracle.truffle.api.library.ExportLibrary; |
| 11 | +import com.oracle.truffle.api.library.ExportMessage; |
| 12 | +import com.oracle.truffle.api.nodes.Node; |
| 13 | +import com.oracle.truffle.api.nodes.NodeUtil; |
| 14 | +import com.oracle.truffle.api.nodes.RootNode; |
| 15 | + |
| 16 | +import java.util.LinkedList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class PFunctionArgsFinder { |
| 20 | + private final RootNode rootNode; |
| 21 | + |
| 22 | + public PFunctionArgsFinder(Node node) { |
| 23 | + this.rootNode = node.getRootNode(); |
| 24 | + assert rootNode instanceof PRootNode; |
| 25 | + } |
| 26 | + |
| 27 | + public ArgumentListObject collectArgs() { |
| 28 | + List<String> arguments = new LinkedList<>(); |
| 29 | + |
| 30 | + NodeUtil.findAllNodeInstances(rootNode, ReadArgumentNode.class).forEach(readArgumentNode -> { |
| 31 | + WriteIdentifierNode identifierNode = NodeUtil.findParent(readArgumentNode, WriteIdentifierNode.class); |
| 32 | + if (identifierNode != null) { |
| 33 | + arguments.add(identifierNode.getIdentifier().toString()); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + return new ArgumentListObject(arguments.toArray(new String[0])); |
| 38 | + } |
| 39 | + |
| 40 | + @ExportLibrary(InteropLibrary.class) |
| 41 | + static final class ArgumentListObject implements TruffleObject { |
| 42 | + final String[] args; |
| 43 | + |
| 44 | + private ArgumentListObject(String[] args) { |
| 45 | + this.args = args; |
| 46 | + } |
| 47 | + |
| 48 | + @SuppressWarnings("unused") |
| 49 | + @ExportMessage |
| 50 | + boolean hasMembers() { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + @SuppressWarnings("unused") |
| 55 | + @ExportMessage |
| 56 | + @TruffleBoundary |
| 57 | + Object getMembers(boolean includeInternal) { |
| 58 | + return new InteropArray(args); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments