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.elaboration.utils.ExpressionDecorator;
13 import org.simantics.scl.compiler.internal.interpreted.IExpression;
14 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
15 import org.simantics.scl.compiler.types.TVar;
16 import org.simantics.scl.compiler.types.Type;
17 import org.simantics.scl.compiler.types.Types;
18 import org.simantics.scl.compiler.types.exceptions.MatchException;
20 import gnu.trove.map.hash.TObjectIntHashMap;
21 import gnu.trove.set.hash.THashSet;
22 import gnu.trove.set.hash.TIntHashSet;
24 public class ELambdaType extends Expression {
25 public TVar[] parameters;
26 public Expression value;
28 public ELambdaType(TVar[] parameters, Expression value) {
29 super(value.getLocation());
30 this.parameters = parameters;
34 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
35 value.collectRefs(allRefs, refs);
39 public void collectVars(TObjectIntHashMap<Variable> allVars,
41 value.collectVars(allVars, vars);
45 protected void updateType() throws MatchException {
46 setType(Types.forAll(parameters, value.getType()));
50 public IVal toVal(CompilationContext context, CodeWriter w) {
51 return lambdaToVal(context, w);
55 public void collectFreeVariables(THashSet<Variable> vars) {
56 value.collectFreeVariables(vars);
60 public Expression simplify(SimplificationContext context) {
61 value = value.simplify(context);
66 public Expression resolve(TranslationContext context) {
67 value = value.resolve(context);
72 public Expression replace(ReplaceContext context) {
73 TVar[] newParameters = new TVar[parameters.length];
74 for(int i=0;i<parameters.length;++i) {
75 TVar var = Types.var(parameters[i].getKind());
76 newParameters[i] = var;
77 context.tvarMap.put(parameters[i], var);
80 ELambdaType result = new ELambdaType(newParameters, value.replace(context));
81 for(int i=0;i<parameters.length;++i)
82 context.tvarMap.remove(parameters[i]);
87 public IExpression toIExpression(ExpressionInterpretationContext target) {
88 return value.toIExpression(target);
92 public Expression inferType(TypingContext context) {
93 throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
97 public Expression decorate(ExpressionDecorator decorator) {
98 value = value.decorate(decorator);
99 return decorator.decorate(this);
103 public void collectEffects(THashSet<Type> effects) {
107 public void setLocationDeep(long loc) {
108 if(location == Locations.NO_LOCATION) {
110 value.setLocationDeep(loc);
115 public void accept(ExpressionVisitor visitor) {
120 public void forVariables(VariableProcedure procedure) {
121 value.forVariables(procedure);
125 public Expression accept(ExpressionTransformer transformer) {
126 return transformer.transform(this);
130 public int getSyntacticFunctionArity() {
131 return value.getSyntacticFunctionArity();