1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.compilation.CompilationContext;
4 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.internal.codegen.references.IVal;
10 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
11 import org.simantics.scl.compiler.internal.interpreted.IExpression;
12 import org.simantics.scl.compiler.internal.interpreted.ILet;
13 import org.simantics.scl.compiler.internal.interpreted.ISeq;
14 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
22 public class ESimpleLet extends Expression {
23 Variable variable; // may be null
27 public ESimpleLet(Variable variable, Expression value, Expression in) {
29 throw new NullPointerException();
31 throw new NullPointerException();
32 this.variable = variable;
37 public ESimpleLet(long loc, Variable variable, Expression value, Expression in) {
40 throw new NullPointerException();
42 throw new NullPointerException();
43 this.variable = variable;
48 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
49 value.collectRefs(allRefs, refs);
50 in.collectRefs(allRefs, refs);
54 public void collectVars(TObjectIntHashMap<Variable> allVars,
56 value.collectVars(allVars, vars);
57 in.collectVars(allVars, vars);
61 protected void updateType() throws MatchException {
62 setType(in.getType());
66 public IVal toVal(CompilationContext context, CodeWriter w) {
67 IVal valueVal = value.toVal(context, w);
69 variable.setVal(valueVal);
70 return in.toVal(context, w);
74 public void collectFreeVariables(THashSet<Variable> vars) {
75 value.collectFreeVariables(vars);
76 in.collectFreeVariables(vars);
77 vars.remove(variable);
81 public Expression simplify(SimplificationContext context) {
82 value = value.simplify(context);
83 if(value instanceof EConstant || value instanceof ELiteral) {
84 context.addInlinedVariable(variable, value);
85 return in.simplify(context);
87 in = in.simplify(context);
92 public Expression resolve(TranslationContext context) {
93 value = value.resolve(context);
94 in = in.resolve(context);
99 public Expression replace(ReplaceContext context) {
101 return new ESimpleLet(location,
103 value.replace(context),
104 in.replace(context));
106 Variable newVariable = variable.copy();
107 context.varMap.put(variable, new EVariable(newVariable));
108 ESimpleLet result = new ESimpleLet(location, newVariable,
109 value.replace(context),
110 in.replace(context));
111 context.varMap.remove(variable);
117 public void setLocationDeep(long loc) {
118 if(location == Locations.NO_LOCATION) {
120 value.setLocationDeep(loc);
121 in.setLocationDeep(loc);
126 public IExpression toIExpression(ExpressionInterpretationContext context) {
127 if(variable == null) {
128 IExpression valueI = value.toIExpression(context);
129 IExpression inI = in.toIExpression(context);
130 return new ISeq(valueI, inI);
133 IExpression valueI = value.toIExpression(context);
134 int variableId = context.push(variable);
135 IExpression inI = in.toIExpression(context);
136 context.pop(variable);
137 return new ILet(variableId, valueI, inI);
141 private void checkBinding(TypingContext context) {
143 value = value.checkIgnoredType(context);
144 else if(variable.getType() == null) {
145 value = value.inferType(context);
146 variable.setType(value.getType());
149 value = value.checkType(context, variable.type);
151 if(variable.getType() == null)
152 variable.setType(Types.metaVar(Kinds.STAR));
153 value = value.checkType(context, variable.type);
158 public Expression inferType(TypingContext context) {
159 checkBinding(context);
160 in = in.inferType(context);
165 public Expression checkBasicType(TypingContext context, Type requiredType) {
166 checkBinding(context);
167 in = in.checkType(context, requiredType);
172 public Expression checkIgnoredType(TypingContext context) {
173 checkBinding(context);
174 in = in.checkIgnoredType(context);
179 public void collectEffects(THashSet<Type> effects) {
180 value.collectEffects(effects);
181 in.collectEffects(effects);
185 public void accept(ExpressionVisitor visitor) {
189 public Expression getValue() {
193 public Variable getVariable() {
197 public Expression getIn() {
202 public void forVariables(VariableProcedure procedure) {
203 value.forVariables(procedure);
204 in.forVariables(procedure);
208 public Expression accept(ExpressionTransformer transformer) {
209 return transformer.transform(this);
213 public int getSyntacticFunctionArity() {
214 return in.getSyntacticFunctionArity();