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