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.compilation.CompilationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
8 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
10 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
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.interpreted.IExpression;
15 import org.simantics.scl.compiler.internal.interpreted.IVariable;
16 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
17 import org.simantics.scl.compiler.types.Type;
18 import org.simantics.scl.compiler.types.Types;
19 import org.simantics.scl.compiler.types.exceptions.MatchException;
20 import org.simantics.scl.compiler.types.kinds.Kinds;
21 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
23 import gnu.trove.map.hash.TObjectIntHashMap;
24 import gnu.trove.set.hash.THashSet;
25 import gnu.trove.set.hash.TIntHashSet;
27 public class EVariable extends Expression {
28 public static final EVariable[] EMPTY_ARRAY = new EVariable[0];
32 public EVariable(Variable variable) {
33 this.variable = variable;
36 public EVariable(long loc, Variable variable) {
38 this.variable = variable;
41 public Variable getVariable() {
45 public void setVariable(Variable variable) {
46 this.variable = variable;
49 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
53 public void collectVars(TObjectIntHashMap<Variable> allVars,
55 int id = allVars.get(variable);
60 public void toString(StringBuilder b, TypeUnparsingContext tuc) {
61 b.append(variable == null ? "???" : variable.toString());
65 protected void updateType() throws MatchException {
66 setType(variable.getType());
70 public IVal toVal(CompilationContext context, CodeWriter w) {
71 return variable.getVal();
75 public void collectFreeVariables(THashSet<Variable> vars) {
80 public Expression simplify(SimplificationContext context) {
81 Expression expression = context.getInlinedValue(variable);
82 if(expression != null)
83 return expression.copy();
89 public Expression resolve(TranslationContext context) {
94 public void getParameters(TranslationContext translationContext,
95 ArrayList<Expression> parameters) {
99 public void removeFreeVariables(THashSet<Variable> vars) {
100 vars.remove(variable);
104 public Expression resolveAsPattern(TranslationContext context) {
109 public Expression replace(ReplaceContext context) {
110 if(context.inPattern) {
111 Type type = variable.getType().replace(context.tvarMap);
113 Variable newVariable = new Variable(variable.name);
114 newVariable.setType(type);
115 EVariable result = new EVariable(newVariable);
116 context.varMap.put(variable, result);
120 if(variable == null) {
121 EVariable newVariable = new EVariable(location, null);
122 newVariable.setType(getType().replace(context.tvarMap));
123 if(context.typingContext == null)
124 throw new InternalCompilerError(location, "Encountered unresolved variable but not in type checking phase.");
125 context.typingContext.addConstraintDemand(newVariable);
129 Expression expression = context.varMap.get(variable);
130 if(expression != null)
131 return expression.copy(context.typingContext);
133 return new EVariable(variable);
140 public IExpression toIExpression(ExpressionInterpretationContext target) {
141 return new IVariable(target.getVariableId(variable));
145 public Expression inferType(TypingContext context) {
146 if(context.isInPattern()) {
147 variable.setType(Types.metaVar(Kinds.STAR));
151 return applyPUnit(context);
155 public Expression checkBasicType(TypingContext context, Type requiredType) {
156 if(context.isInPattern()) {
157 variable.setType(requiredType);
161 return context.subsume(this, requiredType);
165 public boolean isEffectful() {
170 public void collectEffects(THashSet<Type> effects) {
174 public void setLocationDeep(long loc) {
175 if(location == Locations.NO_LOCATION)
180 public void accept(ExpressionVisitor visitor) {
185 public void forVariables(VariableProcedure procedure) {
187 procedure.execute(location, variable);
191 public boolean isPattern(int arity) {
196 public Expression accept(ExpressionTransformer transformer) {
197 return transformer.transform(this);
201 public boolean equalsExpression(Expression expression) {
202 if(expression.getClass() != getClass())
204 EVariable other = (EVariable)expression;
205 return variable == other.variable;