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