1 package org.simantics.scl.compiler.elaboration.expressions;
\r
3 import gnu.trove.map.hash.TObjectIntHashMap;
\r
4 import gnu.trove.set.hash.THashSet;
\r
5 import gnu.trove.set.hash.TIntHashSet;
\r
7 import java.util.ArrayList;
\r
9 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
\r
10 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
\r
11 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
\r
12 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
\r
13 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
\r
14 import org.simantics.scl.compiler.environment.Environment;
\r
15 import org.simantics.scl.compiler.errors.Locations;
\r
16 import org.simantics.scl.compiler.internal.codegen.references.IVal;
\r
17 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
\r
18 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
\r
19 import org.simantics.scl.compiler.internal.interpreted.IExpression;
\r
20 import org.simantics.scl.compiler.internal.interpreted.IVariable;
\r
21 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
\r
22 import org.simantics.scl.compiler.types.Type;
\r
23 import org.simantics.scl.compiler.types.Types;
\r
24 import org.simantics.scl.compiler.types.exceptions.MatchException;
\r
25 import org.simantics.scl.compiler.types.kinds.Kinds;
\r
26 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
\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
153 public Expression checkBasicType(TypingContext context, Type requiredType) {
\r
154 if(context.isInPattern()) {
\r
155 variable.setType(requiredType);
\r
159 return context.subsume(this, requiredType);
\r
163 public Expression decorate(ExpressionDecorator decorator) {
\r
164 return decorator.decorate(this);
\r
168 public boolean isEffectful() {
\r
173 public void collectEffects(THashSet<Type> effects) {
\r
177 public void setLocationDeep(long loc) {
\r
178 if(location == Locations.NO_LOCATION)
\r
183 public void accept(ExpressionVisitor visitor) {
\r
184 visitor.visit(this);
\r
188 public void forVariables(VariableProcedure procedure) {
\r
189 if(variable != null)
\r
190 procedure.execute(location, variable);
\r
194 public boolean isPattern(int arity) {
\r
199 public Expression accept(ExpressionTransformer transformer) {
\r
200 return transformer.transform(this);
\r