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.TIntHashSet;
21 public class ELambdaType extends Expression {
22 public TVar[] parameters;
23 public Expression value;
25 public ELambdaType(TVar[] parameters, Expression value) {
26 super(value.getLocation());
27 this.parameters = parameters;
32 public void collectVars(TObjectIntHashMap<Variable> allVars,
34 value.collectVars(allVars, vars);
38 protected void updateType() throws MatchException {
39 setType(Types.forAll(parameters, value.getType()));
43 public IVal toVal(CompilationContext context, CodeWriter w) {
44 return lambdaToVal(context, w);
48 public Expression simplify(SimplificationContext context) {
49 value = value.simplify(context);
54 public Expression resolve(TranslationContext context) {
55 value = value.resolve(context);
60 public Expression replace(ReplaceContext context) {
61 TVar[] newParameters = new TVar[parameters.length];
62 for(int i=0;i<parameters.length;++i) {
63 TVar var = Types.var(parameters[i].getKind());
64 newParameters[i] = var;
65 context.tvarMap.put(parameters[i], var);
68 ELambdaType result = new ELambdaType(newParameters, value.replace(context));
69 for(int i=0;i<parameters.length;++i)
70 context.tvarMap.remove(parameters[i]);
75 public IExpression toIExpression(ExpressionInterpretationContext target) {
76 return value.toIExpression(target);
80 public Expression inferType(TypingContext context) {
81 throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
85 public void setLocationDeep(long loc) {
86 if(location == Locations.NO_LOCATION) {
88 value.setLocationDeep(loc);
93 public void accept(ExpressionVisitor visitor) {
98 public Expression accept(ExpressionTransformer transformer) {
99 return transformer.transform(this);
103 public int getSyntacticFunctionArity() {
104 return value.getSyntacticFunctionArity();