Skip to content

Remove toString from Painless user tree nodes #54117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 62 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
0b79ef6
remove isNull from AExpression
jdconrad Jan 12, 2020
7325642
remove explicit cast optimization
jdconrad Jan 13, 2020
6e41497
remove modification of semantic tree for casting
jdconrad Jan 13, 2020
247080b
remove ECast node
jdconrad Jan 13, 2020
63e904f
Merge branch 'master' into trees11
jdconrad Feb 26, 2020
0e12611
start of input/output in expressions
jdconrad Jan 13, 2020
f7ae0e7
partial change for input and output in expression nodes
jdconrad Jan 14, 2020
3a2b443
add input/output objects for expressions
jdconrad Jan 16, 2020
8dfb933
fix shift bug in EBinary
jdconrad Jan 16, 2020
6a811de
add input/output to statements
jdconrad Jan 17, 2020
07180d9
Merge branch 'master' into trees11
jdconrad Mar 2, 2020
02f7bb9
Merge branch 'trees11' into trees12
jdconrad Mar 2, 2020
ab9c2c7
Merge branch 'trees12' into trees13
jdconrad Mar 2, 2020
115c8e0
response to pr comment
jdconrad Mar 2, 2020
9a96289
Merge branch 'master' into trees11
jdconrad Mar 3, 2020
d23d394
Merge branch 'trees11' into trees12
jdconrad Mar 3, 2020
3d6018c
Merge branch 'trees12' into trees13
jdconrad Mar 3, 2020
39a8806
Merge branch 'master' into trees12
jdconrad Mar 3, 2020
5536429
Merge branch 'trees12' into trees13
jdconrad Mar 3, 2020
ceb8967
t Merge branch 'master' into trees12
jdconrad Mar 9, 2020
f65d4a7
Merge branch 'trees12' into trees13
jdconrad Mar 9, 2020
d906147
Merge branch 'master' into trees12
jdconrad Mar 9, 2020
08cf476
Merge branch 'trees12' into trees13
jdconrad Mar 9, 2020
94891dc
Merge branch 'trees12' into trees15
jdconrad Mar 9, 2020
5ef8186
Merge branch 'master' into trees13
rjernst Mar 9, 2020
4e13cb3
Merge branch 'trees13' into trees15
rjernst Mar 9, 2020
a92f642
Merge branch 'master' into trees13
rjernst Mar 10, 2020
f140b25
Merge branch 'trees13' into trees15
rjernst Mar 10, 2020
dd40775
updated expression nodes to remove member state
jdconrad Jan 17, 2020
7f6e5d6
update statements to remove most mutable state
jdconrad Jan 17, 2020
79a801b
fix def optimization
jdconrad Jan 17, 2020
b3f52b3
remove SField
jdconrad Jan 17, 2020
074b7e5
fix def optimization in assignment
rjernst Mar 10, 2020
54c43a5
move debug stream
rjernst Mar 11, 2020
d02bc59
add methods to generate class scope
rjernst Mar 11, 2020
6ec1740
move info around
rjernst Mar 11, 2020
a2504e4
remove toString on user nodes
rjernst Mar 11, 2020
09623ed
fix imports
rjernst Mar 11, 2020
b79c54c
response to pr comments
rjernst Mar 13, 2020
e7cbaaa
Merge branch 'master' into trees13
rjernst Mar 13, 2020
5e16f35
Merge branch 'master' into trees13
rjernst Mar 16, 2020
a6a0b3b
Merge branch 'trees13' into trees15
rjernst Mar 16, 2020
f84157b
Merge branch 'trees15' into trees16
rjernst Mar 16, 2020
73f9d12
Merge branch 'master' into trees15
rjernst Mar 17, 2020
3c20af7
Merge branch 'trees15' into trees16
rjernst Mar 17, 2020
2086dcc
Merge branch 'master' into trees13
rjernst Mar 23, 2020
5b13acf
Merge branch 'trees13' into trees15
rjernst Mar 23, 2020
a6ed5d1
response to PR comments
rjernst Mar 23, 2020
21faba3
Merge branch 'master' into trees16
rjernst Mar 23, 2020
ed49704
Merge branch 'trees15' into trees16
rjernst Mar 23, 2020
f4fde39
Merge branch 'master' into trees15
Mar 23, 2020
31de473
fix more todos
jdconrad Mar 23, 2020
73f92bb
Merge branch 'trees15' into trees16
jdconrad Mar 23, 2020
e084de9
fix issue number
jdconrad Mar 23, 2020
4c37dd9
Merge branch 'trees15' into trees16
jdconrad Mar 23, 2020
7529876
Merge branch 'master' into trees15
jdconrad Mar 23, 2020
de45693
Merge branch 'trees15' into trees16
jdconrad Mar 23, 2020
0d4a47e
Merge branch 'master' into trees16
jdconrad Mar 24, 2020
26186e4
Merge branch 'master' into trees16
jdconrad Mar 25, 2020
adcec73
Merge branch 'master' into trees16
jdconrad Mar 26, 2020
f4408d7
Merge branch 'master' into trees16
jdconrad Mar 27, 2020
f054956
fix space
jdconrad Mar 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ private static void addFactoryMethod(Map<String, Class<?>> additionalClasses, Cl
* @return The ScriptRoot used to compile
*/
ScriptRoot compile(Loader loader, String name, String source, CompilerSettings settings) {
String scriptName = Location.computeSourceName(name);
ScriptClassInfo scriptClassInfo = new ScriptClassInfo(painlessLookup, scriptClass);
SClass root = Walker.buildPainlessTree(scriptClassInfo, name, source, settings, painlessLookup, null);
ScriptRoot scriptRoot = new ScriptRoot(painlessLookup, settings, scriptClassInfo, root);
SClass root = Walker.buildPainlessTree(scriptClassInfo, scriptName, source, settings, painlessLookup);
ScriptRoot scriptRoot = new ScriptRoot(painlessLookup, settings, scriptClassInfo, scriptName, source);
ClassNode classNode = root.writeClass(scriptRoot);
DefBootstrapInjectionPhase.phase(classNode);
ScriptInjectionPhase.phase(scriptRoot, classNode);
Expand All @@ -236,10 +237,12 @@ ScriptRoot compile(Loader loader, String name, String source, CompilerSettings s
* @return The bytes for compilation.
*/
byte[] compile(String name, String source, CompilerSettings settings, Printer debugStream) {
String scriptName = Location.computeSourceName(name);
ScriptClassInfo scriptClassInfo = new ScriptClassInfo(painlessLookup, scriptClass);
SClass root = Walker.buildPainlessTree(scriptClassInfo, name, source, settings, painlessLookup, debugStream);
ScriptRoot scriptRoot = new ScriptRoot(painlessLookup, settings, scriptClassInfo, root);
SClass root = Walker.buildPainlessTree(scriptClassInfo, scriptName, source, settings, painlessLookup);
ScriptRoot scriptRoot = new ScriptRoot(painlessLookup, settings, scriptClassInfo, scriptName, source);
ClassNode classNode = root.writeClass(scriptRoot);
classNode.setDebugStream(debugStream);
DefBootstrapInjectionPhase.phase(classNode);
ScriptInjectionPhase.phase(scriptRoot, classNode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
import org.elasticsearch.painless.node.SThrow;
import org.elasticsearch.painless.node.STry;
import org.elasticsearch.painless.node.SWhile;
import org.objectweb.asm.util.Printer;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -170,26 +169,21 @@
public final class Walker extends PainlessParserBaseVisitor<ANode> {

public static SClass buildPainlessTree(ScriptClassInfo mainMethod, String sourceName,
String sourceText, CompilerSettings settings, PainlessLookup painlessLookup,
Printer debugStream) {
return new Walker(mainMethod, sourceName, sourceText, settings, painlessLookup, debugStream).source;
String sourceText, CompilerSettings settings, PainlessLookup painlessLookup) {
return new Walker(mainMethod, sourceName, sourceText, settings, painlessLookup).source;
}

private final ScriptClassInfo scriptClassInfo;
private final SClass source;
private final CompilerSettings settings;
private final Printer debugStream;
private final String sourceName;
private final String sourceText;
private final PainlessLookup painlessLookup;

private Walker(ScriptClassInfo scriptClassInfo, String sourceName, String sourceText,
CompilerSettings settings, PainlessLookup painlessLookup, Printer debugStream) {
CompilerSettings settings, PainlessLookup painlessLookup) {
this.scriptClassInfo = scriptClassInfo;
this.debugStream = debugStream;
this.settings = settings;
this.sourceName = Location.computeSourceName(sourceName);
this.sourceText = sourceText;
this.sourceName = sourceName;
this.painlessLookup = painlessLookup;
this.source = (SClass)visit(buildAntlrTree(sourceText));
}
Expand Down Expand Up @@ -279,7 +273,7 @@ public ANode visitSource(SourceContext ctx) {
location(ctx), statements), true, false, false, true);
functions.add(execute);

return new SClass(scriptClassInfo, sourceName, sourceText, debugStream, location(ctx), functions);
return new SClass(location(ctx), functions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,9 @@ public BlockNode getClinitBlockNode() {

/* ---- end tree structure, begin node data ---- */

private ScriptClassInfo scriptClassInfo;
private String name;
private String sourceText;
private Printer debugStream;
private ScriptRoot scriptRoot;

public void setScriptClassInfo(ScriptClassInfo scriptClassInfo) {
this.scriptClassInfo = scriptClassInfo;
}

public ScriptClassInfo getScriptClassInfo() {
return scriptClassInfo;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setSourceText(String sourceText) {
this.sourceText = sourceText;
}

public String getSourceText() {
return sourceText;
}

public void setDebugStream(Printer debugStream) {
this.debugStream = debugStream;
}
Expand All @@ -125,7 +98,8 @@ public ClassNode() {
}

public byte[] write() {
BitSet statements = new BitSet(sourceText.length());
ScriptClassInfo scriptClassInfo = scriptRoot.getScriptClassInfo();
BitSet statements = new BitSet(scriptRoot.getScriptSource().length());
scriptRoot.addStaticConstant("$STATEMENTS", statements);

// Create the ClassWriter.
Expand All @@ -139,7 +113,7 @@ public byte[] write() {
ClassWriter classWriter = new ClassWriter(scriptRoot.getCompilerSettings(), statements, debugStream,
scriptClassInfo.getBaseClass(), classFrames, classAccess, className, classInterfaces);
ClassVisitor classVisitor = classWriter.getClassVisitor();
classVisitor.visitSource(Location.computeSourceName(name), null);
classVisitor.visitSource(Location.computeSourceName(scriptRoot.getScriptName()), null);

org.objectweb.asm.commons.Method init;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,8 @@

import org.elasticsearch.painless.Location;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import static java.lang.Math.max;
import static java.util.Collections.emptyList;

/**
* The superclass for all nodes.
*/
Expand All @@ -40,7 +31,7 @@ public abstract class ANode {
/**
* The identifier of the script and character offset used for debugging and errors.
*/
final Location location;
protected final Location location;

/**
* Standard constructor with location used for error tracking.
Expand All @@ -55,81 +46,4 @@ public abstract class ANode {
RuntimeException createError(RuntimeException exception) {
return location.createError(exception);
}

/**
* Subclasses should implement this with a method like {@link #singleLineToString(Object...)} or
* {@link #multilineToString(Collection, Collection)}.
*/
public abstract String toString();

// Below are utilities for building the toString

/**
* Build {@link #toString()} for a node without inserting line breaks between the sub-nodes.
*/
protected String singleLineToString(Object... subs) {
return singleLineToString(Arrays.asList(subs));
}

/**
* Build {@link #toString()} for a node without inserting line breaks between the sub-nodes.
*/
protected String singleLineToString(Collection<? extends Object> subs) {
return joinWithName(getClass().getSimpleName(), subs, emptyList());
}

/**
* Build {@link #toString()} for a node that optionally ends in {@code (Args some arguments here)}. Usually function calls.
*/
protected String singleLineToStringWithOptionalArgs(Collection<? extends ANode> arguments, Object... restOfSubs) {
List<Object> subs = new ArrayList<>();
Collections.addAll(subs, restOfSubs);
if (false == arguments.isEmpty()) {
subs.add(joinWithName("Args", arguments, emptyList()));
}
return singleLineToString(subs);
}

/**
* Build {@link #toString()} for a node that should have new lines after some of its sub-nodes.
*/
protected String multilineToString(Collection<? extends Object> sameLine, Collection<? extends Object> ownLine) {
return joinWithName(getClass().getSimpleName(), sameLine, ownLine);
}

/**
* Zip two (potentially uneven) lists together into for {@link #toString()}.
*/
protected List<String> pairwiseToString(Collection<? extends Object> lefts, Collection<? extends Object> rights) {
List<String> pairs = new ArrayList<>(max(lefts.size(), rights.size()));
Iterator<? extends Object> left = lefts.iterator();
Iterator<? extends Object> right = rights.iterator();
while (left.hasNext() || right.hasNext()) {
pairs.add(joinWithName("Pair",
Arrays.asList(left.hasNext() ? left.next() : "<uneven>", right.hasNext() ? right.next() : "<uneven>"),
emptyList()));
}
return pairs;
}

/**
* Build a {@link #toString()} for some expressions. Usually best to use {@link #singleLineToString(Object...)} or
* {@link #multilineToString(Collection, Collection)} instead because they include the name of the node by default.
*/
protected String joinWithName(String name, Collection<? extends Object> sameLine,
Collection<? extends Object> ownLine) {
StringBuilder b = new StringBuilder();
b.append('(').append(name);
for (Object sub : sameLine) {
b.append(' ').append(sub);
}
if (ownLine.size() == 1 && sameLine.isEmpty()) {
b.append(' ').append(ownLine.iterator().next());
} else {
for (Object sub : ownLine) {
b.append("\n ").append(Objects.toString(sub).replace("\n", "\n "));
}
}
return b.append(')').toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,4 @@ public DResolvedType resolveType(PainlessLookup painlessLookup) {
public Class<?> getType() {
return type;
}

@Override
public String toString() {
return "(DResolvedType [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "])";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,5 @@ public DResolvedType resolveType(PainlessLookup painlessLookup) {

return new DResolvedType(location, type);
}

@Override
public String toString() {
return "(DUnresolvedType [" + typeName + "])";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import org.elasticsearch.painless.lookup.def;
import org.elasticsearch.painless.symbol.ScriptRoot;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -251,24 +249,4 @@ Output analyze(ClassNode classNode, ScriptRoot scriptRoot, Scope scope, Input in

return output;
}

@Override
public String toString() {
List<Object> subs = new ArrayList<>();
subs.add(lhs);
if (rhs != null) {
// Make sure "=" is in the symbol so this is easy to read at a glance
subs.add(operation == null ? "=" : operation.symbol + "=");
subs.add(rhs);
return singleLineToString(subs);
}
subs.add(operation.symbol);
if (pre) {
subs.add("pre");
}
if (post) {
subs.add("post");
}
return singleLineToString(subs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,4 @@ Output analyze(ClassNode classNode, ScriptRoot scriptRoot, Scope scope, Input in

return output;
}

@Override
public String toString() {
return singleLineToString(left, operation.symbol, right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,4 @@ Output analyze(ClassNode classNode, ScriptRoot scriptRoot, Scope scope, Input in

return output;
}

@Override
public String toString() {
return singleLineToString(left, operation.symbol, right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,4 @@ Output analyze(ClassNode classNode, ScriptRoot scriptRoot, Scope scope, Input in

return output;
}

@Override
public String toString() {
return singleLineToString(constant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.elasticsearch.painless.Location;
import org.elasticsearch.painless.Scope;
import org.elasticsearch.painless.ir.ClassNode;
import org.elasticsearch.painless.ir.MemberCallNode;
import org.elasticsearch.painless.ir.FieldNode;
import org.elasticsearch.painless.ir.MemberCallNode;
import org.elasticsearch.painless.lookup.PainlessClassBinding;
import org.elasticsearch.painless.lookup.PainlessInstanceBinding;
import org.elasticsearch.painless.lookup.PainlessMethod;
Expand Down Expand Up @@ -188,9 +188,4 @@ Output analyze(ClassNode classNode, ScriptRoot scriptRoot, Scope scope, Input in

return output;
}

@Override
public String toString() {
return singleLineToStringWithOptionalArgs(arguments, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,4 @@ public String getPointer() {
public List<Class<?>> getCaptures() {
return Collections.singletonList(captured.getType());
}

@Override
public String toString() {
return singleLineToString(variable, call);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,4 @@ Output analyze(ClassNode classNode, ScriptRoot scriptRoot, Scope scope, Input in

return output;
}

@Override
public String toString() {
return singleLineToString(left, operation.symbol, right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,4 @@ Output analyze(ClassNode classNode, ScriptRoot scriptRoot, Scope scope, Input in

return output;
}

@Override
public String toString() {
return singleLineToString(condition, left, right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,4 @@ Output analyze(ClassNode classNode, ScriptRoot scriptRoot, Scope scope, Input in

return output;
}

@Override
public String toString() {
String c = constant.toString();
if (constant instanceof String) {
c = "'" + c + "'";
}
return singleLineToString(constant.getClass().getSimpleName(), c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,4 @@ Output analyze(ClassNode classNode, ScriptRoot scriptRoot, Scope scope, Input in

return output;
}

@Override
public String toString() {
return singleLineToString(value);
}
}
Loading