]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EViewPattern.java
ac987ed8102fe0ff14c87f49fbc5d27263b9925d
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EViewPattern.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
7 import org.simantics.scl.compiler.environment.Environment;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.internal.codegen.references.IVal;
10 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
11 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
12 import org.simantics.scl.compiler.types.Type;
13 import org.simantics.scl.compiler.types.Types;
14 import org.simantics.scl.compiler.types.exceptions.MatchException;
15 import org.simantics.scl.compiler.types.util.MultiFunction;
16
17 import gnu.trove.map.hash.TObjectIntHashMap;
18 import gnu.trove.set.hash.THashSet;
19 import gnu.trove.set.hash.TIntHashSet;
20
21 public class EViewPattern extends Expression {
22     public Expression expression;
23     public Expression pattern;
24     
25     public EViewPattern(Expression expression, Expression pattern) {
26         this.expression = expression;
27         this.pattern = pattern;
28     }
29
30     @Override
31     public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
32         expression.collectRefs(allRefs, refs);
33         pattern.collectRefs(allRefs, refs);
34     }
35
36     @Override
37     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
38         expression.collectVars(allVars, vars);
39         pattern.collectVars(allVars, vars);
40     }
41
42     @Override
43     public void forVariables(VariableProcedure procedure) {
44         expression.forVariables(procedure);
45         pattern.forVariables(procedure);
46     }
47     
48     @Override
49     public Expression inferType(TypingContext context) {
50         context.setInPattern(false);
51         expression = expression.inferType(context);
52         context.setInPattern(true);
53         MultiFunction mfun;
54         try {
55             mfun = Types.matchFunction(expression.getType(), 1);
56         } catch (MatchException e) {
57             context.getErrorLog().log(expression.location, "Expected a function as a transformation expression.");
58             return new EError(location);
59         }
60         setType(mfun.parameterTypes[0]);
61         pattern.checkType(context, mfun.returnType);
62         return this;
63     }
64
65     @Override
66     protected void updateType() throws MatchException {
67         MultiFunction mfun = Types.matchFunction(expression.getType(), 1);
68         setType(mfun.parameterTypes[0]);
69     }
70
71     @Override
72     public IVal toVal(Environment env, CodeWriter w) {
73         throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked.");
74     }
75
76     @Override
77     public void collectFreeVariables(THashSet<Variable> vars) {
78         throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
79     }
80     
81     @Override
82     public void removeFreeVariables(THashSet<Variable> vars) {
83         expression.collectFreeVariables(vars);
84         pattern.removeFreeVariables(vars);
85     }
86
87     @Override
88     public Expression resolve(TranslationContext context) {
89         context.getErrorLog().log("View pattern cannot occur only in patterns. Maybe you are missing '\\' in front of a lambda experssion?");
90         return new EError(location);
91     }
92
93     @Override
94     public Expression resolveAsPattern(TranslationContext context) {
95         expression = expression.resolve(context);
96         pattern = pattern.resolveAsPattern(context);
97         return this;
98     }
99     
100     @Override
101     public void setLocationDeep(long loc) {
102         if(location == Locations.NO_LOCATION) {
103             location = loc; 
104             expression.setLocationDeep(loc);
105             pattern.setLocationDeep(loc);
106         }
107     }
108
109     @Override
110     public Expression decorate(ExpressionDecorator decorator) {
111         expression = expression.decorate(decorator);
112         return this;
113     }
114
115     @Override
116     public void collectEffects(THashSet<Type> effects) {
117         expression.collectEffects(effects);
118     }
119
120     @Override
121     public void accept(ExpressionVisitor visitor) {
122         visitor.visit(this);
123     }
124
125     @Override
126     public Expression accept(ExpressionTransformer transformer) {
127         return transformer.transform(this);
128     }
129     
130     @Override
131     public Expression simplify(SimplificationContext context) {
132         expression = expression.simplify(context);
133         pattern = pattern.simplify(context);
134         return this;
135     }
136
137 }