1 package org.simantics.scl.compiler.elaboration.expressions;
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;
11 import gnu.trove.map.hash.TObjectIntHashMap;
12 import gnu.trove.set.hash.THashSet;
13 import gnu.trove.set.hash.TIntHashSet;
15 public class Case extends Symbol {
16 public Expression[] patterns;
17 public Expression value;
21 public Case(Expression[] patterns, Expression value) {
22 this.patterns = patterns;
26 public Case(Expression pattern, Expression value) {
27 this(new Expression[] {pattern}, value);
30 public void setLhs(long lhs) {
34 public long getLhs() {
38 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
39 value.collectRefs(allRefs, refs);
42 public void collectVars(TObjectIntHashMap<Variable> allVars,
44 value.collectVars(allVars, vars);
47 public void collectFreeVariables(THashSet<Variable> vars) {
48 value.collectFreeVariables(vars);
49 for(int i=patterns.length-1;i>=0;--i)
50 patterns[i].removeFreeVariables(vars);
53 public void resolve(TranslationContext context) {
55 for(int i=0;i<patterns.length;++i)
56 patterns[i] = patterns[i].resolveAsPattern(context);
57 value = value.resolve(context);
61 public void simplify(SimplificationContext context) {
62 for(int i=0;i<patterns.length;++i)
63 patterns[i] = patterns[i].simplify(context);
64 value = value.simplify(context);
67 public void setLocationDeep(long loc) {
68 if(location == Locations.NO_LOCATION) {
70 for(Expression pattern : patterns)
71 pattern.setLocationDeep(loc);
72 value.setLocationDeep(loc);
76 public Case replace(ReplaceContext context) {
77 Expression[] newPatterns = new Expression[patterns.length];
78 for(int i=0;i<patterns.length;++i)
79 newPatterns[i] = patterns[i].replaceInPattern(context);
80 Expression newValue = value.replace(context);
81 Case result = new Case(newPatterns, newValue);
86 public Expression[] getPatterns() {
90 public void checkType(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+"+).");
97 for(int i=0;i<patterns.length;++i)
98 patterns[i] = patterns[i].checkTypeAsPattern(context, parameterTypes[i]);
99 value = value.checkType(context, requiredType);
102 public void checkIgnoredType(TypingContext context, Type[] parameterTypes) {
103 if(patterns.length != parameterTypes.length) {
104 context.getErrorLog().log(location, "This case has different arity ("+patterns.length+
105 ") than than the first case (+"+parameterTypes.length+"+).");
108 for(int i=0;i<patterns.length;++i)
109 patterns[i] = patterns[i].checkTypeAsPattern(context, parameterTypes[i]);
110 value = value.checkIgnoredType(context);
113 public void forVariables(VariableProcedure procedure) {
114 value.forVariables(procedure);