]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EViewPattern.java
c0dfb5a75158d3f5c03181aabf27a11192ae8175
[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.compilation.CompilationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
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.types.Types;
12 import org.simantics.scl.compiler.types.exceptions.MatchException;
13 import org.simantics.scl.compiler.types.util.MultiFunction;
14
15 import gnu.trove.map.hash.TObjectIntHashMap;
16 import gnu.trove.set.hash.THashSet;
17 import gnu.trove.set.hash.TIntHashSet;
18
19 public class EViewPattern extends Expression {
20     public Expression expression;
21     public Expression pattern;
22     
23     public EViewPattern(Expression expression, Expression pattern) {
24         this.expression = expression;
25         this.pattern = pattern;
26     }
27
28     @Override
29     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
30         expression.collectVars(allVars, vars);
31         pattern.collectVars(allVars, vars);
32     }
33     
34     @Override
35     public Expression inferType(TypingContext context) {
36         context.setInPattern(false);
37         expression = expression.inferType(context);
38         context.setInPattern(true);
39         MultiFunction mfun;
40         try {
41             mfun = Types.matchFunction(expression.getType(), 1);
42         } catch (MatchException e) {
43             context.getErrorLog().log(expression.location, "Expected a function as a transformation expression.");
44             return new EError(location);
45         }
46         setType(mfun.parameterTypes[0]);
47         pattern.checkType(context, mfun.returnType);
48         return this;
49     }
50
51     @Override
52     protected void updateType() throws MatchException {
53         MultiFunction mfun = Types.matchFunction(expression.getType(), 1);
54         setType(mfun.parameterTypes[0]);
55     }
56
57     @Override
58     public IVal toVal(CompilationContext context, CodeWriter w) {
59         throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked.");
60     }
61
62     @Override
63     public void collectFreeVariables(THashSet<Variable> vars) {
64         throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
65     }
66     
67     @Override
68     public void removeFreeVariables(THashSet<Variable> vars) {
69         expression.collectFreeVariables(vars);
70         pattern.removeFreeVariables(vars);
71     }
72
73     @Override
74     public Expression resolve(TranslationContext context) {
75         context.getErrorLog().log("View pattern cannot occur only in patterns. Maybe you are missing '\\' in front of a lambda experssion?");
76         return new EError(location);
77     }
78
79     @Override
80     public Expression resolveAsPattern(TranslationContext context) {
81         expression = expression.resolve(context);
82         pattern = pattern.resolveAsPattern(context);
83         return this;
84     }
85     
86     @Override
87     public void setLocationDeep(long loc) {
88         if(location == Locations.NO_LOCATION) {
89             location = loc; 
90             expression.setLocationDeep(loc);
91             pattern.setLocationDeep(loc);
92         }
93     }
94
95     @Override
96     public void accept(ExpressionVisitor visitor) {
97         visitor.visit(this);
98     }
99
100     @Override
101     public Expression accept(ExpressionTransformer transformer) {
102         return transformer.transform(this);
103     }
104     
105     @Override
106     public Expression simplify(SimplificationContext context) {
107         expression = expression.simplify(context);
108         pattern = pattern.simplify(context);
109         return this;
110     }
111
112 }