]> gerrit.simantics Code Review - simantics/platform.git/blob
761ed0f2f23da45b7cdac5b39bffdb6d22d43c2b
[simantics/platform.git] /
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     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         public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
50     }
51         
52         @Override
53         public void collectVars(TObjectIntHashMap<Variable> allVars,
54                 TIntHashSet vars) {
55             int id = allVars.get(variable);
56             if(id >= 0)
57                 vars.add(id);
58         }
59
60         public void toString(StringBuilder b, TypeUnparsingContext tuc) {
61         b.append(variable == null ? "???" : variable.toString());
62     }
63
64         @Override
65         protected void updateType() throws MatchException {
66             setType(variable.getType());
67         }
68         
69         @Override
70         public IVal toVal(CompilationContext context, CodeWriter w) {
71         return variable.getVal();
72     }
73
74     @Override
75     public void collectFreeVariables(THashSet<Variable> vars) {
76         vars.add(variable);
77     }
78
79     @Override
80     public Expression simplify(SimplificationContext context) {
81         Expression expression = context.getInlinedValue(variable);
82         if(expression != null)
83             return expression.copy();
84         else
85             return this;
86     }
87
88     @Override
89     public Expression resolve(TranslationContext context) {
90         return this;
91     }
92     
93     @Override
94     public void getParameters(TranslationContext translationContext,
95             ArrayList<Expression> parameters) {
96     }
97     
98     @Override
99     public void removeFreeVariables(THashSet<Variable> vars) {
100         vars.remove(variable);
101     }
102     
103     @Override
104     public Expression resolveAsPattern(TranslationContext context) {
105         return this;
106     }
107
108     @Override
109     public Expression replace(ReplaceContext context) {
110         if(context.inPattern) {
111             Type type = variable.getType().replace(context.tvarMap);
112             
113             Variable newVariable = new Variable(variable.name);
114             newVariable.setType(type);
115             EVariable result = new EVariable(newVariable);
116             context.varMap.put(variable, result);
117             return result;
118         }
119         else {
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);
126                 return newVariable;
127             }
128             else {
129                 Expression expression = context.varMap.get(variable);
130                 if(expression != null)
131                     return expression.copy(context.typingContext);
132                 else {
133                     return new EVariable(variable);
134                 }
135             }
136         }
137     }
138     
139     @Override
140     public IExpression toIExpression(ExpressionInterpretationContext target) {
141         return new IVariable(target.getVariableId(variable));
142     }
143     
144     @Override
145     public Expression inferType(TypingContext context) {
146         if(context.isInPattern()) {
147             variable.setType(Types.metaVar(Kinds.STAR));
148             return this;
149         }
150         else
151             return applyPUnit(context);
152     }
153     
154     @Override
155     public Expression checkBasicType(TypingContext context, Type requiredType) {
156         if(context.isInPattern()) {
157             variable.setType(requiredType);
158             return this;
159         }
160         else
161             return context.subsume(this, requiredType);
162     }
163     
164     @Override
165     public boolean isEffectful() {
166         return false;
167     }
168     
169     @Override
170     public void collectEffects(THashSet<Type> effects) {
171     }
172     
173     @Override
174     public void setLocationDeep(long loc) {
175         if(location == Locations.NO_LOCATION)
176             location = loc;
177     }
178     
179     @Override
180     public void accept(ExpressionVisitor visitor) {
181         visitor.visit(this);
182     }
183
184     @Override
185     public void forVariables(VariableProcedure procedure) {
186         if(variable != null)
187             procedure.execute(location, variable);
188     }
189     
190     @Override
191     public boolean isPattern(int arity) {
192         return arity == 0;
193     }
194     
195     @Override
196     public Expression accept(ExpressionTransformer transformer) {
197         return transformer.transform(this);
198     }
199     
200     @Override
201     public boolean equalsExpression(Expression expression) {
202         if(expression.getClass() != getClass())
203             return false;
204         EVariable other = (EVariable)expression;
205         return variable == other.variable;
206     }
207
208 }