1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
6 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
7 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
8 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
10 import org.simantics.scl.compiler.environment.Environment;
11 import org.simantics.scl.compiler.errors.Locations;
12 import org.simantics.scl.compiler.internal.codegen.references.IVal;
13 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
14 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
15 import org.simantics.scl.compiler.internal.interpreted.IExpression;
16 import org.simantics.scl.compiler.internal.interpreted.IVariable;
17 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
18 import org.simantics.scl.compiler.types.Type;
19 import org.simantics.scl.compiler.types.Types;
20 import org.simantics.scl.compiler.types.exceptions.MatchException;
21 import org.simantics.scl.compiler.types.kinds.Kinds;
22 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
24 import gnu.trove.map.hash.TObjectIntHashMap;
25 import gnu.trove.set.hash.THashSet;
26 import gnu.trove.set.hash.TIntHashSet;
28 public class EVariable extends Expression {
29 public static final EVariable[] EMPTY_ARRAY = new EVariable[0];
33 public EVariable(Variable variable) {
34 this.variable = variable;
37 public EVariable(long loc, Variable variable) {
39 this.variable = variable;
42 public Variable getVariable() {
46 public void setVariable(Variable variable) {
47 this.variable = variable;
50 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
54 public void collectVars(TObjectIntHashMap<Variable> allVars,
56 int id = allVars.get(variable);
61 public void toString(StringBuilder b, TypeUnparsingContext tuc) {
62 b.append(variable == null ? "???" : variable.toString());
66 protected void updateType() throws MatchException {
67 setType(variable.getType());
71 public IVal toVal(Environment env, CodeWriter w) {
72 return variable.getVal();
76 public void collectFreeVariables(THashSet<Variable> vars) {
81 public Expression simplify(SimplificationContext context) {
82 Expression expression = context.getInlinedValue(variable);
83 if(expression != null)
84 return expression.copy();
90 public Expression resolve(TranslationContext context) {
95 public void getParameters(TranslationContext translationContext,
96 ArrayList<Expression> parameters) {
100 public void removeFreeVariables(THashSet<Variable> vars) {
101 vars.remove(variable);
105 public Expression resolveAsPattern(TranslationContext context) {
110 public Expression replace(ReplaceContext context) {
111 if(context.inPattern) {
112 Type type = variable.getType().replace(context.tvarMap);
114 Variable newVariable = new Variable(variable.name);
115 newVariable.setType(type);
116 EVariable result = new EVariable(newVariable);
117 context.varMap.put(variable, result);
121 if(variable == null) {
122 EVariable newVariable = new EVariable(location, null);
123 newVariable.setType(getType().replace(context.tvarMap));
124 if(context.typingContext == null)
125 throw new InternalCompilerError(location, "Encountered unresolved variable but not in type checking phase.");
126 context.typingContext.addConstraintDemand(newVariable);
130 Expression expression = context.varMap.get(variable);
131 if(expression != null)
132 return expression.copy(context.typingContext);
134 return new EVariable(variable);
141 public IExpression toIExpression(ExpressionInterpretationContext target) {
142 return new IVariable(target.getVariableId(variable));
146 public Expression inferType(TypingContext context) {
147 if(context.isInPattern()) {
148 variable.setType(Types.metaVar(Kinds.STAR));
152 return applyPUnit(context.getCompilationContext());
156 public Expression checkBasicType(TypingContext context, Type requiredType) {
157 if(context.isInPattern()) {
158 variable.setType(requiredType);
162 return context.subsume(this, requiredType);
166 public Expression decorate(ExpressionDecorator decorator) {
167 return decorator.decorate(this);
171 public boolean isEffectful() {
176 public void collectEffects(THashSet<Type> effects) {
180 public void setLocationDeep(long loc) {
181 if(location == Locations.NO_LOCATION)
186 public void accept(ExpressionVisitor visitor) {
191 public void forVariables(VariableProcedure procedure) {
193 procedure.execute(location, variable);
197 public boolean isPattern(int arity) {
202 public Expression accept(ExpressionTransformer transformer) {
203 return transformer.transform(this);
207 public boolean equalsExpression(Expression expression) {
208 if(expression.getClass() != getClass())
210 EVariable other = (EVariable)expression;
211 return variable == other.variable;