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