1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.compilation.CompilationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
6 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
8 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
9 import org.simantics.scl.compiler.errors.Locations;
10 import org.simantics.scl.compiler.internal.codegen.references.IVal;
11 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
12 import org.simantics.scl.compiler.internal.interpreted.IExpression;
13 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
14 import org.simantics.scl.compiler.types.TVar;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.compiler.types.Types;
17 import org.simantics.scl.compiler.types.exceptions.MatchException;
19 import gnu.trove.map.hash.TObjectIntHashMap;
20 import gnu.trove.set.hash.THashSet;
21 import gnu.trove.set.hash.TIntHashSet;
23 public class ELambdaType extends Expression {
24 public TVar[] parameters;
25 public Expression value;
27 public ELambdaType(TVar[] parameters, Expression value) {
28 super(value.getLocation());
29 this.parameters = parameters;
33 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
34 value.collectRefs(allRefs, refs);
38 public void collectVars(TObjectIntHashMap<Variable> allVars,
40 value.collectVars(allVars, vars);
44 protected void updateType() throws MatchException {
45 setType(Types.forAll(parameters, value.getType()));
49 public IVal toVal(CompilationContext context, CodeWriter w) {
50 return lambdaToVal(context, w);
54 public void collectFreeVariables(THashSet<Variable> vars) {
55 value.collectFreeVariables(vars);
59 public Expression simplify(SimplificationContext context) {
60 value = value.simplify(context);
65 public Expression resolve(TranslationContext context) {
66 value = value.resolve(context);
71 public Expression replace(ReplaceContext context) {
72 TVar[] newParameters = new TVar[parameters.length];
73 for(int i=0;i<parameters.length;++i) {
74 TVar var = Types.var(parameters[i].getKind());
75 newParameters[i] = var;
76 context.tvarMap.put(parameters[i], var);
79 ELambdaType result = new ELambdaType(newParameters, value.replace(context));
80 for(int i=0;i<parameters.length;++i)
81 context.tvarMap.remove(parameters[i]);
86 public IExpression toIExpression(ExpressionInterpretationContext target) {
87 return value.toIExpression(target);
91 public Expression inferType(TypingContext context) {
92 throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
96 public void collectEffects(THashSet<Type> effects) {
100 public void setLocationDeep(long loc) {
101 if(location == Locations.NO_LOCATION) {
103 value.setLocationDeep(loc);
108 public void accept(ExpressionVisitor visitor) {
113 public Expression accept(ExpressionTransformer transformer) {
114 return transformer.transform(this);
118 public int getSyntacticFunctionArity() {
119 return value.getSyntacticFunctionArity();