1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.compilation.CompilationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
7 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
8 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
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.types.Type;
17 import org.simantics.scl.compiler.types.Types;
18 import org.simantics.scl.compiler.types.kinds.Kinds;
20 import gnu.trove.map.hash.TObjectIntHashMap;
21 import gnu.trove.set.hash.THashSet;
22 import gnu.trove.set.hash.TIntHashSet;
24 public class EMatch extends Expression {
26 public Expression[] scrutinee;
29 public EMatch(Expression[] scrutinee, Case ... cases) {
30 this.scrutinee = scrutinee;
34 public EMatch(Expression scrutinee, Case ... cases) {
35 this(new Expression[] {scrutinee}, cases);
38 public EMatch(long loc, Expression[] scrutinee, Case ... cases) {
40 this.scrutinee = scrutinee;
44 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
45 for(Expression s : scrutinee)
46 s.collectRefs(allRefs, refs);
47 for(Case case_ : cases)
48 case_.collectRefs(allRefs, refs);
52 public void collectVars(TObjectIntHashMap<Variable> allVars,
54 for(Expression s : scrutinee)
55 s.collectVars(allVars, vars);
56 for(Case case_ : cases)
57 case_.collectVars(allVars, vars);
61 protected void updateType() {
62 setType(cases[0].value.getType());
66 public IVal toVal(CompilationContext context, CodeWriter w) {
67 ArrayList<Row> rows = new ArrayList<Row>(cases.length);
68 for(Case case_ : cases)
69 rows.add(new Row(case_.patterns, case_.value));
71 IVal[] scrutineeVals = new IVal[scrutinee.length];
72 for(int i=0;i<scrutinee.length;++i)
73 scrutineeVals[i] = scrutinee[i].toVal(context, w);
75 CodeWriter joinPoint = w.createBlock(getType());
76 CodeWriter failurePoint = w.createBlock(); // TODO generate only one failurePoint per function
77 PatternMatchingCompiler.split(w, context, scrutineeVals, joinPoint.getContinuation(), failurePoint.getContinuation(), rows);
78 failurePoint.throw_(location, Throw.MatchingException, "Matching failure at: " + toString());
79 w.continueAs(joinPoint);
80 return w.getParameters()[0];
84 public void collectFreeVariables(THashSet<Variable> vars) {
85 for(Expression s : scrutinee)
86 s.collectFreeVariables(vars);
87 for(Case case_ : cases)
88 case_.collectFreeVariables(vars);
92 public Expression simplify(SimplificationContext context) {
93 for(int i=0;i<scrutinee.length;++i)
94 scrutinee[i] = scrutinee[i].simplify(context);
95 for(Case case_ : cases)
96 case_.simplify(context);
97 if(cases.length == 1 && scrutinee.length == 1) {
98 Case case_ = cases[0];
99 Expression pattern = case_.patterns[0];
100 if(case_.patterns[0] instanceof EVariable
101 && !(case_.value instanceof GuardedExpressionGroup)) {
102 Variable var = ((EVariable)pattern).variable;
103 return new ESimpleLet(var, scrutinee[0], case_.value);
110 public Expression resolve(TranslationContext context) {
111 for(int i=0;i<scrutinee.length;++i)
112 scrutinee[i] = scrutinee[i].resolve(context);
113 for(Case case_ : cases)
114 case_.resolve(context);
119 public void setLocationDeep(long loc) {
120 if(location == Locations.NO_LOCATION) {
122 for(Case case_ : cases)
123 case_.setLocationDeep(loc);
124 for(Expression e : scrutinee)
125 e.setLocationDeep(loc);
130 public Expression replace(ReplaceContext context) {
131 Expression[] newScrutinee = new Expression[scrutinee.length];
132 for(int i=0;i<scrutinee.length;++i)
133 newScrutinee[i] = scrutinee[i].replace(context);
134 Case[] newCases = new Case[cases.length];
135 for(int i=0;i<cases.length;++i)
136 newCases[i] = cases[i].replace(context);
137 return new EMatch(getLocation(), newScrutinee, newCases);
141 public Expression checkBasicType(TypingContext context, Type requiredType) {
142 Type[] scrutineeTypes = new Type[scrutinee.length];
143 for(int i=0;i<scrutinee.length;++i) {
144 scrutinee[i] = scrutinee[i].checkType(context, Types.metaVar(Kinds.STAR));
145 scrutineeTypes[i] = scrutinee[i].getType();
147 for(Case case_ : cases)
148 case_.checkType(context, scrutineeTypes, requiredType);
149 setType(requiredType);
154 public Expression checkIgnoredType(TypingContext context) {
155 Type[] scrutineeTypes = new Type[scrutinee.length];
156 for(int i=0;i<scrutinee.length;++i) {
157 scrutinee[i] = scrutinee[i].checkType(context, Types.metaVar(Kinds.STAR));
158 scrutineeTypes[i] = scrutinee[i].getType();
160 for(Case case_ : cases)
161 case_.checkIgnoredType(context, scrutineeTypes);
167 public void collectEffects(THashSet<Type> effects) {
168 for(Expression s : scrutinee)
169 s.collectEffects(effects);
170 for(Case case_ : cases) {
171 for(Expression pattern : case_.patterns)
172 pattern.collectEffects(effects);
173 case_.value.collectEffects(effects);
178 public void accept(ExpressionVisitor visitor) {
182 public Expression[] getScrutinee() {
186 public Case[] getCases() {
191 public void forVariables(VariableProcedure procedure) {
192 for(Expression s : scrutinee)
193 s.forVariables(procedure);
194 for(Case case_ : cases)
195 case_.forVariables(procedure);
199 public Expression accept(ExpressionTransformer transformer) {
200 return transformer.transform(this);
204 public int getSyntacticFunctionArity() {
206 for(Case case_ : cases)
207 result = Math.max(result, case_.value.getSyntacticFunctionArity());