aa278928239ea4efd2ee0d6cb9e336c129541110
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EVariable.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.ArrayList;
4
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;
22
23 import gnu.trove.map.hash.TObjectIntHashMap;
24 import gnu.trove.set.hash.THashSet;
25 import gnu.trove.set.hash.TIntHashSet;
26
27 public class EVariable extends Expression {
28     public static final EVariable[] EMPTY_ARRAY = new EVariable[0];
29     
30     public Variable variable;
31     
32     public EVariable(Variable variable) {
33         this.variable = variable;
34     }
35
36     public EVariable(long loc, Variable variable) {
37         super(loc);
38         this.variable = variable;
39     }
40
41     public Variable getVariable() {
42         return variable;
43     }
44     
45     public void setVariable(Variable variable) {
46         this.variable = variable;
47     }
48
49     @Override
50     public void collectVars(TObjectIntHashMap<Variable> allVars,
51             TIntHashSet vars) {
52         int id = allVars.get(variable);
53         if(id >= 0)
54             vars.add(id);
55     }
56
57     public void toString(StringBuilder b, TypeUnparsingContext tuc) {
58         b.append(variable == null ? "???" : variable.toString());
59     }
60
61         @Override
62         protected void updateType() throws MatchException {
63             setType(variable.getType());
64         }
65         
66         @Override
67         public IVal toVal(CompilationContext context, CodeWriter w) {
68         return variable.getVal();
69     }
70
71     @Override
72     public void collectFreeVariables(THashSet<Variable> vars) {
73         vars.add(variable);
74     }
75
76     @Override
77     public Expression simplify(SimplificationContext context) {
78         Expression expression = context.getInlinedValue(variable);
79         if(expression != null)
80             return expression.copy();
81         else
82             return this;
83     }
84
85     @Override
86     public Expression resolve(TranslationContext context) {
87         return this;
88     }
89     
90     @Override
91     public void getParameters(TranslationContext translationContext,
92             ArrayList<Expression> parameters) {
93     }
94     
95     @Override
96     public void removeFreeVariables(THashSet<Variable> vars) {
97         vars.remove(variable);
98     }
99     
100     @Override
101     public Expression resolveAsPattern(TranslationContext context) {
102         return this;
103     }
104
105     @Override
106     public Expression replace(ReplaceContext context) {
107         if(context.inPattern) {
108             Type type = variable.getType().replace(context.tvarMap);
109             
110             Variable newVariable = new Variable(variable.name);
111             newVariable.setType(type);
112             EVariable result = new EVariable(newVariable);
113             context.varMap.put(variable, result);
114             return result;
115         }
116         else {
117             if(variable == null) {
118                 EVariable newVariable = new EVariable(location, null);
119                 newVariable.setType(getType().replace(context.tvarMap));
120                 if(context.typingContext == null)
121                     throw new InternalCompilerError(location, "Encountered unresolved variable but not in type checking phase.");
122                 context.typingContext.addConstraintDemand(newVariable);
123                 return newVariable;
124             }
125             else {
126                 Expression expression = context.varMap.get(variable);
127                 if(expression != null)
128                     return expression.copy(context.typingContext);
129                 else {
130                     return new EVariable(variable);
131                 }
132             }
133         }
134     }
135     
136     @Override
137     public IExpression toIExpression(ExpressionInterpretationContext target) {
138         return new IVariable(target.getVariableId(variable));
139     }
140     
141     @Override
142     public Expression inferType(TypingContext context) {
143         if(context.isInPattern()) {
144             variable.setType(Types.metaVar(Kinds.STAR));
145             return this;
146         }
147         else
148             return applyPUnit(context);
149     }
150     
151     @Override
152     public Expression checkBasicType(TypingContext context, Type requiredType) {
153         if(context.isInPattern()) {
154             variable.setType(requiredType);
155             return this;
156         }
157         else
158             return context.subsume(this, requiredType);
159     }
160     
161     @Override
162     public boolean isEffectful() {
163         return false;
164     }
165     
166     @Override
167     public void setLocationDeep(long loc) {
168         if(location == Locations.NO_LOCATION)
169             location = loc;
170     }
171     
172     @Override
173     public void accept(ExpressionVisitor visitor) {
174         visitor.visit(this);
175     }
176     
177     @Override
178     public boolean isPattern(int arity) {
179         return arity == 0;
180     }
181     
182     @Override
183     public Expression accept(ExpressionTransformer transformer) {
184         return transformer.transform(this);
185     }
186     
187     @Override
188     public boolean equalsExpression(Expression expression) {
189         if(expression.getClass() != getClass())
190             return false;
191         EVariable other = (EVariable)expression;
192         return variable == other.variable;
193     }
194
195 }