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.Types;
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 ELambdaType extends Expression {
23 public TVar[] parameters;
24 public Expression value;
26 public ELambdaType(TVar[] parameters, Expression value) {
27 super(value.getLocation());
28 this.parameters = parameters;
33 public void collectVars(TObjectIntHashMap<Variable> allVars,
35 value.collectVars(allVars, vars);
39 protected void updateType() throws MatchException {
40 setType(Types.forAll(parameters, value.getType()));
44 public IVal toVal(CompilationContext context, CodeWriter w) {
45 return lambdaToVal(context, w);
49 public void collectFreeVariables(THashSet<Variable> vars) {
50 value.collectFreeVariables(vars);
54 public Expression simplify(SimplificationContext context) {
55 value = value.simplify(context);
60 public Expression resolve(TranslationContext context) {
61 value = value.resolve(context);
66 public Expression replace(ReplaceContext context) {
67 TVar[] newParameters = new TVar[parameters.length];
68 for(int i=0;i<parameters.length;++i) {
69 TVar var = Types.var(parameters[i].getKind());
70 newParameters[i] = var;
71 context.tvarMap.put(parameters[i], var);
74 ELambdaType result = new ELambdaType(newParameters, value.replace(context));
75 for(int i=0;i<parameters.length;++i)
76 context.tvarMap.remove(parameters[i]);
81 public IExpression toIExpression(ExpressionInterpretationContext target) {
82 return value.toIExpression(target);
86 public Expression inferType(TypingContext context) {
87 throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
91 public void setLocationDeep(long loc) {
92 if(location == Locations.NO_LOCATION) {
94 value.setLocationDeep(loc);
99 public void accept(ExpressionVisitor visitor) {
104 public Expression accept(ExpressionTransformer transformer) {
105 return transformer.transform(this);
109 public int getSyntacticFunctionArity() {
110 return value.getSyntacticFunctionArity();