1 package org.simantics.scl.compiler.elaboration.expressions;
\r
3 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
\r
4 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
\r
5 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
\r
6 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
\r
7 import org.simantics.scl.compiler.environment.Environment;
\r
8 import org.simantics.scl.compiler.errors.Locations;
\r
9 import org.simantics.scl.compiler.internal.codegen.references.IVal;
\r
10 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
\r
11 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
\r
12 import org.simantics.scl.compiler.internal.interpreted.IExpression;
\r
13 import org.simantics.scl.compiler.internal.interpreted.ILet;
\r
14 import org.simantics.scl.compiler.internal.interpreted.ISeq;
\r
15 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
\r
16 import org.simantics.scl.compiler.types.Type;
\r
17 import org.simantics.scl.compiler.types.exceptions.MatchException;
\r
19 import gnu.trove.map.hash.TObjectIntHashMap;
\r
20 import gnu.trove.set.hash.THashSet;
\r
21 import gnu.trove.set.hash.TIntHashSet;
\r
23 public class ESimpleLet extends Expression {
\r
24 Variable variable; // may be null
\r
28 public ESimpleLet(Variable variable, Expression value, Expression in) {
\r
30 throw new NullPointerException();
\r
32 throw new NullPointerException();
\r
33 this.variable = variable;
\r
38 public ESimpleLet(long loc, Variable variable, Expression value, Expression in) {
\r
41 throw new NullPointerException();
\r
43 throw new NullPointerException();
\r
44 this.variable = variable;
\r
49 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
\r
50 value.collectRefs(allRefs, refs);
\r
51 in.collectRefs(allRefs, refs);
\r
55 public void collectVars(TObjectIntHashMap<Variable> allVars,
\r
57 value.collectVars(allVars, vars);
\r
58 in.collectVars(allVars, vars);
\r
62 protected void updateType() throws MatchException {
\r
63 setType(in.getType());
\r
67 public IVal toVal(Environment env, CodeWriter w) {
\r
68 IVal valueVal = value.toVal(env, w);
\r
69 if(variable != null)
\r
70 variable.setVal(valueVal);
\r
71 return in.toVal(env, w);
\r
75 public void collectFreeVariables(THashSet<Variable> vars) {
\r
76 value.collectFreeVariables(vars);
\r
77 in.collectFreeVariables(vars);
\r
78 vars.remove(variable);
\r
82 public Expression simplify(SimplificationContext context) {
\r
83 value = value.simplify(context);
\r
84 if(value instanceof EConstant || value instanceof ELiteral) {
\r
85 context.addInlinedVariable(variable, value);
\r
86 return in.simplify(context);
\r
88 in = in.simplify(context);
\r
93 public Expression resolve(TranslationContext context) {
\r
94 value = value.resolve(context);
\r
95 in = in.resolve(context);
\r
100 public Expression replace(ReplaceContext context) {
\r
101 if(variable == null)
\r
102 return new ESimpleLet(location,
\r
104 value.replace(context),
\r
105 in.replace(context));
\r
107 Variable newVariable = variable.copy();
\r
108 context.varMap.put(variable, new EVariable(newVariable));
\r
109 ESimpleLet result = new ESimpleLet(location, newVariable,
\r
110 value.replace(context),
\r
111 in.replace(context));
\r
112 context.varMap.remove(variable);
\r
118 public void setLocationDeep(long loc) {
\r
119 if(location == Locations.NO_LOCATION) {
\r
121 value.setLocationDeep(loc);
\r
122 in.setLocationDeep(loc);
\r
127 public IExpression toIExpression(ExpressionInterpretationContext context) {
\r
128 if(variable == null) {
\r
129 IExpression valueI = value.toIExpression(context);
\r
130 IExpression inI = in.toIExpression(context);
\r
131 return new ISeq(valueI, inI);
\r
134 IExpression valueI = value.toIExpression(context);
\r
135 int variableId = context.push(variable);
\r
136 IExpression inI = in.toIExpression(context);
\r
137 context.pop(variable);
\r
138 return new ILet(variableId, valueI, inI);
\r
142 private void checkBinding(TypingContext context) {
\r
143 if(variable == null)
\r
144 value = value.inferType(context);
\r
145 else if(variable.getType() == null) {
\r
146 value = value.inferType(context);
\r
147 variable.setType(value.getType());
\r
150 value = value.checkType(context, variable.type);
\r
152 if(variable.getType() == null)
\r
153 variable.setType(Types.metaVar(Kinds.STAR));
\r
154 value = value.checkType(context, variable.type);
\r
159 public Expression inferType(TypingContext context) {
\r
160 checkBinding(context);
\r
161 in = in.inferType(context);
\r
166 public Expression checkBasicType(TypingContext context, Type requiredType) {
\r
167 checkBinding(context);
\r
168 in = in.checkType(context, requiredType);
\r
173 public Expression decorate(ExpressionDecorator decorator) {
\r
174 value = value.decorate(decorator);
\r
175 in = in.decorate(decorator);
\r
176 return decorator.decorate(this);
\r
180 public void collectEffects(THashSet<Type> effects) {
\r
181 value.collectEffects(effects);
\r
182 in.collectEffects(effects);
\r
186 public void accept(ExpressionVisitor visitor) {
\r
187 visitor.visit(this);
\r
190 public Expression getValue() {
\r
194 public Variable getVariable() {
\r
198 public Expression getIn() {
\r
203 public void forVariables(VariableProcedure procedure) {
\r
204 value.forVariables(procedure);
\r
205 in.forVariables(procedure);
\r
209 public Expression accept(ExpressionTransformer transformer) {
\r
210 return transformer.transform(this);
\r