Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stu-elastic committed Jan 23, 2024
1 parent fda18eb commit 24b9c74
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.elasticsearch.painless.phase.DefaultIRTreeToASMBytesPhase;
import org.elasticsearch.painless.phase.DefaultStaticConstantExtractionPhase;
import org.elasticsearch.painless.phase.DefaultStringConcatenationOptimizationPhase;
import org.elasticsearch.painless.phase.DefaultTypeAnalysisPhase;
import org.elasticsearch.painless.phase.IRTreeVisitor;
import org.elasticsearch.painless.phase.PainlessSemanticAnalysisPhase;
import org.elasticsearch.painless.phase.PainlessSemanticHeaderPhase;
Expand Down Expand Up @@ -249,6 +250,7 @@ byte[] compile(String name, String source, CompilerSettings settings, Printer de
SClass root = Walker.buildPainlessTree(scriptName, source, settings);
ScriptScope scriptScope = new ScriptScope(painlessLookup, settings, scriptClassInfo, scriptName, source, root.getIdentifier() + 1);
new PainlessSemanticHeaderPhase().visitClass(root, scriptScope);
new DefaultTypeAnalysisPhase().visitClass(root, scriptScope);
new PainlessSemanticAnalysisPhase().visitClass(root, scriptScope);
new PainlessUserTreeToIRTreePhase().visitClass(root, scriptScope);
ClassNode classNode = (ClassNode) scriptScope.getDecoration(root, IRNodeDecoration.class).irNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,18 +729,21 @@ public void visitDeclaration(SDeclaration userDeclarationNode, SemanticScope sem
}

AExpression userValueNode = userDeclarationNode.getValueNode();

Class<?> assignment = null;
if (userValueNode != null) {
semanticScope.setCondition(userValueNode, Read.class);
semanticScope.putDecoration(userValueNode, new TargetType(type));
checkedVisit(userValueNode, semanticScope);
decorateWithCast(userValueNode, semanticScope);
assignment = semanticScope.getDecoration(userValueNode, ValueType.class).valueType();
}

Location location = userDeclarationNode.getLocation();
// TODO(stu): value type of userValueNode maybe
// Class<?> valueType = semanticScope.getDecoration(userExpressionNode, ValueType.class).valueType();
Variable variable = semanticScope.defineVariable(location, type, symbol, false);
if (assignment != null) {
variable.assigned(assignment, userValueNode);
}
semanticScope.putDecoration(userDeclarationNode, new SemanticVariable(variable));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ public void visitDeclaration(SDeclaration userDeclarationNode, SemanticScope sem
// TODO(stu): value type of userValueNode maybe
// Class<?> valueType = semanticScope.getDecoration(userExpressionNode, ValueType.class).valueType();
Variable variable = semanticScope.defineVariable(location, type, symbol, false);
// variable.assigned(semanticScope.getDecoration(userValueNode, ValueType.class).valueType(), false);
semanticScope.putDecoration(userDeclarationNode, new SemanticVariable(variable));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

import org.elasticsearch.painless.Location;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.node.AExpression;
import org.elasticsearch.painless.node.ANode;
import org.elasticsearch.painless.symbol.Decorator.Condition;
import org.elasticsearch.painless.symbol.Decorator.Decoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand All @@ -38,11 +41,42 @@ public abstract class SemanticScope {
* variables. Each {@link SemanticScope} tracks its own set of defined
* variables available for use.
*/
public record Variable(Class<?> type, String name, boolean isFinal) {
public static class Variable {
public final Class<?> type;
public final String name;
public final boolean isFinal;
protected List<Assignment> assignments;

public Variable(Class<?> type, String name, boolean isFinal) {
this.type = Objects.requireNonNull(type);
this.name = Objects.requireNonNull(name);
this.isFinal = isFinal;
}

public Variable {
Objects.requireNonNull(type);
Objects.requireNonNull(name);
public Class<?> type() {
return type;
}

public String name() {
return name;
}

public boolean isFinal() {
return isFinal;
}

public void assigned(Class<?> assigned, AExpression node) {
if (assignments == null) {
assignments = new ArrayList<>();
}
assignments.add(new Assignment(assigned, node));
}

private record Assignment(Class<?> assigned, AExpression node) {
Assignment {
Objects.requireNonNull(assigned);
Objects.requireNonNull(node);
}
}

/**
Expand Down Expand Up @@ -297,10 +331,6 @@ public <T extends Decoration> T putDecoration(ANode node, T decoration) {
return scriptScope.put(node.getIdentifier(), decoration);
}

public <T extends Decoration> T removeDecoration(ANode node, Class<T> type) {
return scriptScope.remove(node.getIdentifier(), type);
}

public <T extends Decoration> T getDecoration(ANode node, Class<T> type) {
return scriptScope.get(node.getIdentifier(), type);
}
Expand All @@ -317,10 +347,6 @@ public boolean setCondition(ANode node, Class<? extends Condition> type) {
return scriptScope.set(node.getIdentifier(), type);
}

public boolean deleteCondition(ANode node, Class<? extends Condition> type) {
return scriptScope.delete(node.getIdentifier(), type);
}

public boolean getCondition(ANode node, Class<? extends Condition> type) {
return scriptScope.exists(node.getIdentifier(), type);
}
Expand Down

0 comments on commit 24b9c74

Please sign in to comment.