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