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