1 package org.simantics.scl.compiler.elaboration.expressions;
\r
3 import java.util.ArrayList;
\r
5 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
\r
6 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
\r
7 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
\r
8 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
\r
9 import org.simantics.scl.compiler.environment.Environment;
\r
10 import org.simantics.scl.compiler.errors.Locations;
\r
11 import org.simantics.scl.compiler.internal.codegen.references.IVal;
\r
12 import org.simantics.scl.compiler.internal.codegen.ssa.exits.Throw;
\r
13 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
\r
14 import org.simantics.scl.compiler.internal.elaboration.matching.PatternMatchingCompiler;
\r
15 import org.simantics.scl.compiler.internal.elaboration.matching.Row;
\r
16 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
\r
17 import org.simantics.scl.compiler.types.Type;
\r
18 import org.simantics.scl.compiler.types.Types;
\r
19 import org.simantics.scl.compiler.types.kinds.Kinds;
\r
21 import gnu.trove.map.hash.TObjectIntHashMap;
\r
22 import gnu.trove.set.hash.THashSet;
\r
23 import gnu.trove.set.hash.TIntHashSet;
\r
25 public class EMatch extends Expression {
\r
27 public Expression[] scrutinee;
\r
28 public Case[] cases;
\r
30 public EMatch(Expression[] scrutinee, Case ... cases) {
\r
31 this.scrutinee = scrutinee;
\r
35 public EMatch(Expression scrutinee, Case ... cases) {
\r
36 this(new Expression[] {scrutinee}, cases);
\r
39 public EMatch(long loc, Expression[] scrutinee, Case ... cases) {
\r
41 this.scrutinee = scrutinee;
\r
45 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
\r
46 for(Expression s : scrutinee)
\r
47 s.collectRefs(allRefs, refs);
\r
48 for(Case case_ : cases)
\r
49 case_.collectRefs(allRefs, refs);
\r
53 public void collectVars(TObjectIntHashMap<Variable> allVars,
\r
55 for(Expression s : scrutinee)
\r
56 s.collectVars(allVars, vars);
\r
57 for(Case case_ : cases)
\r
58 case_.collectVars(allVars, vars);
\r
62 protected void updateType() {
\r
63 setType(cases[0].value.getType());
\r
67 public IVal toVal(Environment env, CodeWriter w) {
\r
68 ArrayList<Row> rows = new ArrayList<Row>(cases.length);
\r
69 for(Case case_ : cases)
\r
70 rows.add(new Row(case_.patterns, case_.value));
\r
72 IVal[] scrutineeVals = new IVal[scrutinee.length];
\r
73 for(int i=0;i<scrutinee.length;++i)
\r
74 scrutineeVals[i] = scrutinee[i].toVal(env, w);
\r
76 CodeWriter joinPoint = w.createBlock(getType());
\r
77 CodeWriter failurePoint = w.createBlock(); // TODO generate only one failurePoint per function
\r
78 PatternMatchingCompiler.split(w, env, scrutineeVals, joinPoint.getContinuation(), failurePoint.getContinuation(), rows);
\r
79 failurePoint.throw_(location, Throw.MatchingException, "Matching failure at: " + toString());
\r
80 w.continueAs(joinPoint);
\r
81 return w.getParameters()[0];
\r
85 public void collectFreeVariables(THashSet<Variable> vars) {
\r
86 for(Expression s : scrutinee)
\r
87 s.collectFreeVariables(vars);
\r
88 for(Case case_ : cases)
\r
89 case_.collectFreeVariables(vars);
\r
93 public Expression simplify(SimplificationContext context) {
\r
94 for(int i=0;i<scrutinee.length;++i)
\r
95 scrutinee[i] = scrutinee[i].simplify(context);
\r
96 for(Case case_ : cases)
\r
97 case_.simplify(context);
\r
98 if(cases.length == 1 && scrutinee.length == 1) {
\r
99 Case case_ = cases[0];
\r
100 Expression pattern = case_.patterns[0];
\r
101 if(case_.patterns[0] instanceof EVariable
\r
102 && !(case_.value instanceof GuardedExpressionGroup)) {
\r
103 Variable var = ((EVariable)pattern).variable;
\r
104 return new ESimpleLet(var, scrutinee[0], case_.value);
\r
111 public Expression resolve(TranslationContext context) {
\r
112 for(int i=0;i<scrutinee.length;++i)
\r
113 scrutinee[i] = scrutinee[i].resolve(context);
\r
114 for(Case case_ : cases)
\r
115 case_.resolve(context);
\r
120 public void setLocationDeep(long loc) {
\r
121 if(location == Locations.NO_LOCATION) {
\r
123 for(Case case_ : cases)
\r
124 case_.setLocationDeep(loc);
\r
125 for(Expression e : scrutinee)
\r
126 e.setLocationDeep(loc);
\r
131 public Expression replace(ReplaceContext context) {
\r
132 Expression[] newScrutinee = new Expression[scrutinee.length];
\r
133 for(int i=0;i<scrutinee.length;++i)
\r
134 newScrutinee[i] = scrutinee[i].replace(context);
\r
135 Case[] newCases = new Case[cases.length];
\r
136 for(int i=0;i<cases.length;++i)
\r
137 newCases[i] = cases[i].replace(context);
\r
138 return new EMatch(getLocation(), newScrutinee, newCases);
\r
142 public Expression checkBasicType(TypingContext context, Type requiredType) {
\r
143 Type[] scrutineeTypes = new Type[scrutinee.length];
\r
144 for(int i=0;i<scrutinee.length;++i) {
\r
145 scrutinee[i] = scrutinee[i].checkType(context, Types.metaVar(Kinds.STAR));
\r
146 scrutineeTypes[i] = scrutinee[i].getType();
\r
148 for(Case case_ : cases)
\r
149 case_.checkType(context, scrutineeTypes, requiredType);
\r
150 setType(requiredType);
\r
155 public Expression checkIgnoredType(TypingContext context) {
\r
156 Type[] scrutineeTypes = new Type[scrutinee.length];
\r
157 for(int i=0;i<scrutinee.length;++i) {
\r
158 scrutinee[i] = scrutinee[i].checkType(context, Types.metaVar(Kinds.STAR));
\r
159 scrutineeTypes[i] = scrutinee[i].getType();
\r
161 for(Case case_ : cases)
\r
162 case_.checkIgnoredType(context, scrutineeTypes);
\r
163 setType(Types.UNIT);
\r
168 public Expression decorate(ExpressionDecorator decorator) {
\r
169 for(int i=0;i<scrutinee.length;++i)
\r
170 scrutinee[i] = scrutinee[i].decorate(decorator);
\r
171 for(Case case_ : cases)
\r
172 case_.decorate(decorator);
\r
173 return decorator.decorate(this);
\r
177 public void collectEffects(THashSet<Type> effects) {
\r
178 for(Expression s : scrutinee)
\r
179 s.collectEffects(effects);
\r
180 for(Case case_ : cases) {
\r
181 for(Expression pattern : case_.patterns)
\r
182 pattern.collectEffects(effects);
\r
183 case_.value.collectEffects(effects);
\r
188 public void accept(ExpressionVisitor visitor) {
\r
189 visitor.visit(this);
\r
192 public Expression[] getScrutinee() {
\r
196 public Case[] getCases() {
\r
201 public void forVariables(VariableProcedure procedure) {
\r
202 for(Expression s : scrutinee)
\r
203 s.forVariables(procedure);
\r
204 for(Case case_ : cases)
\r
205 case_.forVariables(procedure);
\r
209 public Expression accept(ExpressionTransformer transformer) {
\r
210 return transformer.transform(this);
\r