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;
34 public void collectVars(TObjectIntHashMap<Variable> allVars,
36 value.collectVars(allVars, vars);
40 protected void updateType() throws MatchException {
41 setType(Types.forAll(parameters, value.getType()));
45 public IVal toVal(CompilationContext context, CodeWriter w) {
46 return lambdaToVal(context, w);
50 public void collectFreeVariables(THashSet<Variable> vars) {
51 value.collectFreeVariables(vars);
55 public Expression simplify(SimplificationContext context) {
56 value = value.simplify(context);
61 public Expression resolve(TranslationContext context) {
62 value = value.resolve(context);
67 public Expression replace(ReplaceContext context) {
68 TVar[] newParameters = new TVar[parameters.length];
69 for(int i=0;i<parameters.length;++i) {
70 TVar var = Types.var(parameters[i].getKind());
71 newParameters[i] = var;
72 context.tvarMap.put(parameters[i], var);
75 ELambdaType result = new ELambdaType(newParameters, value.replace(context));
76 for(int i=0;i<parameters.length;++i)
77 context.tvarMap.remove(parameters[i]);
82 public IExpression toIExpression(ExpressionInterpretationContext target) {
83 return value.toIExpression(target);
87 public Expression inferType(TypingContext context) {
88 throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
92 public void collectEffects(THashSet<Type> effects) {
96 public void setLocationDeep(long loc) {
97 if(location == Locations.NO_LOCATION) {
99 value.setLocationDeep(loc);
104 public void accept(ExpressionVisitor visitor) {
109 public Expression accept(ExpressionTransformer transformer) {
110 return transformer.transform(this);
114 public int getSyntacticFunctionArity() {
115 return value.getSyntacticFunctionArity();