]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Case.java
(refs #7375) Replaced collectFreeVariables method by a visitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / Case.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
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.errors.Locations;
8 import org.simantics.scl.compiler.internal.parsing.Symbol;
9 import org.simantics.scl.compiler.types.Type;
10
11 import gnu.trove.map.hash.TObjectIntHashMap;
12 import gnu.trove.set.hash.TIntHashSet;
13
14 public class Case extends Symbol {
15     public Expression[] patterns;
16     public Expression value;
17     
18     long lhs;
19     
20     public Case(Expression[] patterns, Expression value) {
21         this.patterns = patterns;
22         this.value = value;
23     }
24     
25     public Case(Expression pattern, Expression value) {
26         this(new Expression[] {pattern}, value);
27     }
28
29     public void setLhs(long lhs) {
30         this.lhs = lhs;
31     }
32     
33     public long getLhs() {
34         return lhs;
35     }
36
37     public void collectVars(TObjectIntHashMap<Variable> allVars,
38             TIntHashSet vars) {
39         value.collectVars(allVars, vars);
40     }
41
42     public void resolve(TranslationContext context) {
43         context.pushFrame();
44         for(int i=0;i<patterns.length;++i)
45             patterns[i] = patterns[i].resolveAsPattern(context);
46         value = value.resolve(context);
47         context.popFrame();
48     }
49
50     public void simplify(SimplificationContext context) {
51         for(int i=0;i<patterns.length;++i)
52             patterns[i] = patterns[i].simplify(context);
53         value = value.simplify(context);
54     }
55
56     public void setLocationDeep(long loc) {
57         if(location == Locations.NO_LOCATION) {
58             location = loc;
59             for(Expression pattern : patterns)
60                 pattern.setLocationDeep(loc);
61             value.setLocationDeep(loc);
62         }
63     }
64
65     public Case replace(ReplaceContext context) {
66         Expression[] newPatterns = new Expression[patterns.length];        
67         for(int i=0;i<patterns.length;++i)
68             newPatterns[i] = patterns[i].replaceInPattern(context);
69         Expression newValue = value.replace(context);
70         Case result = new Case(newPatterns, newValue);
71         result.setLhs(lhs);
72         return result;
73     }
74
75     public Expression[] getPatterns() {
76         return patterns;
77     }
78
79     public void checkType(TypingContext context, Type[] parameterTypes,
80             Type requiredType) {
81         if(patterns.length != parameterTypes.length) {
82             context.getErrorLog().log(location, "This case has different arity ("+patterns.length+
83                     ") than than the first case (+"+parameterTypes.length+"+).");
84             return;
85         }
86         for(int i=0;i<patterns.length;++i)
87             patterns[i] = patterns[i].checkTypeAsPattern(context, parameterTypes[i]);
88         value = value.checkType(context, requiredType);
89     }
90     
91     public void checkIgnoredType(TypingContext context, Type[] parameterTypes) {
92         if(patterns.length != parameterTypes.length) {
93             context.getErrorLog().log(location, "This case has different arity ("+patterns.length+
94                     ") than than the first case (+"+parameterTypes.length+"+).");
95             return;
96         }
97         for(int i=0;i<patterns.length;++i)
98             patterns[i] = patterns[i].checkTypeAsPattern(context, parameterTypes[i]);
99         value = value.checkIgnoredType(context);
100     }
101 }