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