1 package org.simantics.scl.compiler.elaboration.expressions;
\r
3 import gnu.trove.map.hash.TObjectIntHashMap;
\r
4 import gnu.trove.set.hash.THashSet;
\r
5 import gnu.trove.set.hash.TIntHashSet;
\r
7 import java.util.ArrayList;
\r
9 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
\r
10 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
\r
11 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
\r
12 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
\r
13 import org.simantics.scl.compiler.environment.Environment;
\r
14 import org.simantics.scl.compiler.errors.Locations;
\r
15 import org.simantics.scl.compiler.internal.codegen.references.IVal;
\r
16 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
\r
17 import org.simantics.scl.compiler.internal.elaboration.matching.PatternMatchingCompiler;
\r
18 import org.simantics.scl.compiler.internal.elaboration.matching.Row;
\r
19 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
\r
20 import org.simantics.scl.compiler.types.Type;
\r
21 import org.simantics.scl.compiler.types.Types;
\r
22 import org.simantics.scl.compiler.types.kinds.Kinds;
\r
24 public class EMatch extends Expression {
\r
26 public Expression[] scrutinee;
\r
27 public Case[] cases;
\r
29 public EMatch(Expression[] scrutinee, Case ... cases) {
\r
30 this.scrutinee = scrutinee;
\r
34 public EMatch(Expression scrutinee, Case ... cases) {
\r
35 this(new Expression[] {scrutinee}, cases);
\r
38 public EMatch(long loc, Expression[] scrutinee, Case ... cases) {
\r
40 this.scrutinee = scrutinee;
\r
44 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
\r
45 for(Expression s : scrutinee)
\r
46 s.collectRefs(allRefs, refs);
\r
47 for(Case case_ : cases)
\r
48 case_.collectRefs(allRefs, refs);
\r
52 public void collectVars(TObjectIntHashMap<Variable> allVars,
\r
54 for(Expression s : scrutinee)
\r
55 s.collectVars(allVars, vars);
\r
56 for(Case case_ : cases)
\r
57 case_.collectVars(allVars, vars);
\r
61 protected void updateType() {
\r
62 setType(cases[0].value.getType());
\r
66 public IVal toVal(Environment env, CodeWriter w) {
\r
67 ArrayList<Row> rows = new ArrayList<Row>(cases.length);
\r
68 for(Case case_ : cases)
\r
69 rows.add(new Row(case_.patterns, case_.value));
\r
71 IVal[] scrutineeVals = new IVal[scrutinee.length];
\r
72 for(int i=0;i<scrutinee.length;++i)
\r
73 scrutineeVals[i] = scrutinee[i].toVal(env, w);
\r
75 CodeWriter joinPoint = w.createBlock(getType());
\r
76 CodeWriter failurePoint = w.createBlock(); // TODO generate only one failurePoint per function
\r
77 PatternMatchingCompiler.split(w, env, scrutineeVals, joinPoint.getContinuation(), failurePoint.getContinuation(), rows);
\r
78 failurePoint.throw_(location, "Matching failure at: " + toString());
\r
79 w.continueAs(joinPoint);
\r
80 return w.getParameters()[0];
\r
84 public void collectFreeVariables(THashSet<Variable> vars) {
\r
85 for(Expression s : scrutinee)
\r
86 s.collectFreeVariables(vars);
\r
87 for(Case case_ : cases)
\r
88 case_.collectFreeVariables(vars);
\r
92 public Expression simplify(SimplificationContext context) {
\r
93 for(int i=0;i<scrutinee.length;++i)
\r
94 scrutinee[i] = scrutinee[i].simplify(context);
\r
95 for(Case case_ : cases)
\r
96 case_.simplify(context);
\r
97 if(cases.length == 1 && scrutinee.length == 1) {
\r
98 Case case_ = cases[0];
\r
99 Expression pattern = case_.patterns[0];
\r
100 if(case_.patterns[0] instanceof EVariable
\r
101 && !(case_.value instanceof GuardedExpressionGroup)) {
\r
102 Variable var = ((EVariable)pattern).variable;
\r
103 return new ESimpleLet(var, scrutinee[0], case_.value);
\r
110 public Expression resolve(TranslationContext context) {
\r
111 for(int i=0;i<scrutinee.length;++i)
\r
112 scrutinee[i] = scrutinee[i].resolve(context);
\r
113 for(Case case_ : cases)
\r
114 case_.resolve(context);
\r
119 public void setLocationDeep(long loc) {
\r
120 if(location == Locations.NO_LOCATION) {
\r
122 for(Case case_ : cases)
\r
123 case_.setLocationDeep(loc);
\r
124 for(Expression e : scrutinee)
\r
125 e.setLocationDeep(loc);
\r
130 public Expression replace(ReplaceContext context) {
\r
131 Expression[] newScrutinee = new Expression[scrutinee.length];
\r
132 for(int i=0;i<scrutinee.length;++i)
\r
133 newScrutinee[i] = scrutinee[i].replace(context);
\r
134 Case[] newCases = new Case[cases.length];
\r
135 for(int i=0;i<cases.length;++i)
\r
136 newCases[i] = cases[i].replace(context);
\r
137 return new EMatch(getLocation(), newScrutinee, newCases);
\r
141 public Expression checkBasicType(TypingContext context, Type requiredType) {
\r
142 Type[] scrutineeTypes = new Type[scrutinee.length];
\r
143 for(int i=0;i<scrutinee.length;++i) {
\r
144 scrutinee[i] = scrutinee[i].checkType(context, Types.metaVar(Kinds.STAR));
\r
145 scrutineeTypes[i] = scrutinee[i].getType();
\r
147 for(Case case_ : cases)
\r
148 case_.checkType(context, scrutineeTypes, requiredType);
\r
149 setType(requiredType);
\r
154 public Expression decorate(ExpressionDecorator decorator) {
\r
155 for(int i=0;i<scrutinee.length;++i)
\r
156 scrutinee[i] = scrutinee[i].decorate(decorator);
\r
157 for(Case case_ : cases)
\r
158 case_.decorate(decorator);
\r
159 return decorator.decorate(this);
\r
163 public void collectEffects(THashSet<Type> effects) {
\r
164 for(Expression s : scrutinee)
\r
165 s.collectEffects(effects);
\r
166 for(Case case_ : cases) {
\r
167 for(Expression pattern : case_.patterns)
\r
168 pattern.collectEffects(effects);
\r
169 case_.value.collectEffects(effects);
\r
174 public void accept(ExpressionVisitor visitor) {
\r
175 visitor.visit(this);
\r
178 public Expression[] getScrutinee() {
\r
182 public Case[] getCases() {
\r
187 public void forVariables(VariableProcedure procedure) {
\r
188 for(Expression s : scrutinee)
\r
189 s.forVariables(procedure);
\r
190 for(Case case_ : cases)
\r
191 case_.forVariables(procedure);
\r
195 public Expression accept(ExpressionTransformer transformer) {
\r
196 return transformer.transform(this);
\r