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.elaboration.utils.ExpressionDecorator;
9 import org.simantics.scl.compiler.internal.parsing.Symbol;
10 import org.simantics.scl.compiler.types.Type;
12 import gnu.trove.map.hash.TObjectIntHashMap;
13 import gnu.trove.set.hash.THashSet;
14 import gnu.trove.set.hash.TIntHashSet;
16 public class Case extends Symbol {
17 public Expression[] patterns;
18 public Expression value;
22 public Case(Expression[] patterns, Expression value) {
23 this.patterns = patterns;
27 public Case(Expression pattern, Expression value) {
28 this(new Expression[] {pattern}, value);
31 public void setLhs(long lhs) {
35 public long getLhs() {
39 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
40 value.collectRefs(allRefs, refs);
43 public void collectVars(TObjectIntHashMap<Variable> allVars,
45 value.collectVars(allVars, vars);
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);
54 public void resolve(TranslationContext context) {
56 for(int i=0;i<patterns.length;++i)
57 patterns[i] = patterns[i].resolveAsPattern(context);
58 value = value.resolve(context);
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);
68 public void setLocationDeep(long loc) {
69 if(location == Locations.NO_LOCATION) {
71 for(Expression pattern : patterns)
72 pattern.setLocationDeep(loc);
73 value.setLocationDeep(loc);
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);
87 public Expression[] getPatterns() {
91 public void checkType(TypingContext context, Type[] parameterTypes,
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+"+).");
98 for(int i=0;i<patterns.length;++i)
99 patterns[i] = patterns[i].checkTypeAsPattern(context, parameterTypes[i]);
100 value = value.checkType(context, requiredType);
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+"+).");
109 for(int i=0;i<patterns.length;++i)
110 patterns[i] = patterns[i].checkTypeAsPattern(context, parameterTypes[i]);
111 value = value.checkIgnoredType(context);
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);
120 public void forVariables(VariableProcedure procedure) {
121 value.forVariables(procedure);