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.TIntHashSet;
21 public class ESimpleLet extends Expression {
22 public Variable variable; // may be null
23 public Expression value;
26 public ESimpleLet(Variable variable, Expression value, Expression in) {
28 throw new NullPointerException();
30 throw new NullPointerException();
31 this.variable = variable;
36 public ESimpleLet(long loc, Variable variable, Expression value, Expression in) {
39 throw new NullPointerException();
41 throw new NullPointerException();
42 this.variable = variable;
48 public void collectVars(TObjectIntHashMap<Variable> allVars,
50 value.collectVars(allVars, vars);
51 in.collectVars(allVars, vars);
55 protected void updateType() throws MatchException {
56 setType(in.getType());
60 public IVal toVal(CompilationContext context, CodeWriter w) {
61 IVal valueVal = value.toVal(context, w);
63 variable.setVal(valueVal);
64 return in.toVal(context, w);
68 public Expression simplify(SimplificationContext context) {
69 value = value.simplify(context);
70 if(value instanceof EConstant || value instanceof ELiteral) {
71 context.addInlinedVariable(variable, value);
72 return in.simplify(context);
74 in = in.simplify(context);
79 public Expression resolve(TranslationContext context) {
80 value = value.resolve(context);
81 in = in.resolve(context);
86 public Expression replace(ReplaceContext context) {
88 return new ESimpleLet(location,
90 value.replace(context),
93 Variable newVariable = variable.copy();
94 context.varMap.put(variable, new EVariable(newVariable));
95 ESimpleLet result = new ESimpleLet(location, newVariable,
96 value.replace(context),
98 context.varMap.remove(variable);
104 public void setLocationDeep(long loc) {
105 if(location == Locations.NO_LOCATION) {
107 value.setLocationDeep(loc);
108 in.setLocationDeep(loc);
113 public IExpression toIExpression(ExpressionInterpretationContext context) {
114 if(variable == null) {
115 IExpression valueI = value.toIExpression(context);
116 IExpression inI = in.toIExpression(context);
117 return new ISeq(valueI, inI);
120 IExpression valueI = value.toIExpression(context);
121 int variableId = context.push(variable);
122 IExpression inI = in.toIExpression(context);
123 context.pop(variable);
124 return new ILet(variableId, valueI, inI);
128 private void checkBinding(TypingContext context) {
130 value = value.checkIgnoredType(context);
131 else if(variable.getType() == null) {
132 value = value.inferType(context);
133 variable.setType(value.getType());
136 value = value.checkType(context, variable.type);
138 if(variable.getType() == null)
139 variable.setType(Types.metaVar(Kinds.STAR));
140 value = value.checkType(context, variable.type);
145 public Expression inferType(TypingContext context) {
146 checkBinding(context);
147 in = in.inferType(context);
152 public Expression checkBasicType(TypingContext context, Type requiredType) {
153 checkBinding(context);
154 in = in.checkType(context, requiredType);
159 public Expression checkIgnoredType(TypingContext context) {
160 checkBinding(context);
161 in = in.checkIgnoredType(context);
166 public void accept(ExpressionVisitor visitor) {
170 public Expression getValue() {
174 public Variable getVariable() {
178 public Expression getIn() {
183 public Expression accept(ExpressionTransformer transformer) {
184 return transformer.transform(this);
188 public int getSyntacticFunctionArity() {
189 return in.getSyntacticFunctionArity();