1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
8 import org.simantics.scl.compiler.compilation.CompilationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
10 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
11 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
12 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
13 import org.simantics.scl.compiler.errors.Locations;
14 import org.simantics.scl.compiler.internal.codegen.references.IVal;
15 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
16 import org.simantics.scl.compiler.internal.interpreted.IExpression;
17 import org.simantics.scl.compiler.internal.interpreted.IVariable;
18 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
19 import org.simantics.scl.compiler.types.Type;
20 import org.simantics.scl.compiler.types.Types;
21 import org.simantics.scl.compiler.types.exceptions.MatchException;
22 import org.simantics.scl.compiler.types.kinds.Kinds;
23 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
25 public class EVariable extends Expression {
26 public static final EVariable[] EMPTY_ARRAY = new EVariable[0];
28 public Variable variable;
30 public EVariable(Variable variable) {
31 this.variable = variable;
34 public EVariable(long loc, Variable variable) {
36 this.variable = variable;
39 public Variable getVariable() {
43 public void setVariable(Variable variable) {
44 this.variable = variable;
48 public Set<Variable> getFreeVariables() {
50 return Collections.emptySet();
52 return Collections.singleton(variable);
55 public void toString(StringBuilder b, TypeUnparsingContext tuc) {
56 b.append(variable == null ? "???" : variable.toString());
60 protected void updateType() throws MatchException {
61 setType(variable.getType());
65 public IVal toVal(CompilationContext context, CodeWriter w) {
66 return variable.getVal();
70 public Expression simplify(SimplificationContext context) {
71 Expression expression = context.getInlinedValue(variable);
72 if(expression != null)
73 return expression.copy();
79 public Expression resolve(TranslationContext context) {
84 public void getParameters(TranslationContext translationContext,
85 ArrayList<Expression> parameters) {
89 public Expression resolveAsPattern(TranslationContext context) {
94 public Expression replace(ReplaceContext context) {
95 if(context.inPattern) {
96 Type type = variable.getType().replace(context.tvarMap);
98 Variable newVariable = new Variable(variable.name);
99 newVariable.setType(type);
100 EVariable result = new EVariable(newVariable);
101 context.varMap.put(variable, result);
105 if(variable == null) {
106 EVariable newVariable = new EVariable(location, null);
107 newVariable.setType(getType().replace(context.tvarMap));
108 if(context.typingContext == null)
109 throw new InternalCompilerError(location, "Encountered unresolved variable but not in type checking phase.");
110 context.typingContext.addConstraintDemand(newVariable);
114 Expression expression = context.varMap.get(variable);
115 if(expression != null)
116 return expression.copy(context.typingContext);
118 return new EVariable(variable);
125 public IExpression toIExpression(ExpressionInterpretationContext target) {
126 return new IVariable(target.getVariableId(variable));
130 public Expression inferType(TypingContext context) {
131 if(context.isInPattern()) {
132 variable.setType(Types.metaVar(Kinds.STAR));
136 return applyPUnit(context);
140 public Expression checkBasicType(TypingContext context, Type requiredType) {
141 if(context.isInPattern()) {
142 variable.setType(requiredType);
146 return context.subsume(this, requiredType);
150 public boolean isEffectful() {
155 public void setLocationDeep(long loc) {
156 if(location == Locations.NO_LOCATION)
161 public void accept(ExpressionVisitor visitor) {
166 public boolean isPattern(int arity) {
171 public Expression accept(ExpressionTransformer transformer) {
172 return transformer.transform(this);
176 public boolean equalsExpression(Expression expression) {
177 if(expression.getClass() != getClass())
179 EVariable other = (EVariable)expression;
180 return variable == other.variable;