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