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.TIntHashSet;
23 public class EMatch extends Expression {
25 public Expression[] scrutinee;
28 public EMatch(Expression[] scrutinee, Case ... cases) {
29 this.scrutinee = scrutinee;
33 public EMatch(Expression scrutinee, Case ... cases) {
34 this(new Expression[] {scrutinee}, cases);
37 public EMatch(long loc, Expression[] scrutinee, Case ... cases) {
39 this.scrutinee = scrutinee;
44 public void collectVars(TObjectIntHashMap<Variable> allVars,
46 for(Expression s : scrutinee)
47 s.collectVars(allVars, vars);
48 for(Case case_ : cases)
49 case_.collectVars(allVars, vars);
53 protected void updateType() {
54 setType(cases[0].value.getType());
58 public IVal toVal(CompilationContext context, CodeWriter w) {
59 ArrayList<Row> rows = new ArrayList<Row>(cases.length);
60 for(Case case_ : cases)
61 rows.add(new Row(case_.patterns, case_.value));
63 IVal[] scrutineeVals = new IVal[scrutinee.length];
64 for(int i=0;i<scrutinee.length;++i)
65 scrutineeVals[i] = scrutinee[i].toVal(context, w);
67 CodeWriter joinPoint = w.createBlock(getType());
68 CodeWriter failurePoint = w.createBlock(); // TODO generate only one failurePoint per function
69 PatternMatchingCompiler.split(w, context, scrutineeVals, joinPoint.getContinuation(), failurePoint.getContinuation(), rows);
70 failurePoint.throw_(location, Throw.MatchingException, "Matching failure at: " + toString());
71 w.continueAs(joinPoint);
72 return w.getParameters()[0];
76 public Expression simplify(SimplificationContext context) {
77 for(int i=0;i<scrutinee.length;++i)
78 scrutinee[i] = scrutinee[i].simplify(context);
79 for(Case case_ : cases)
80 case_.simplify(context);
81 if(cases.length == 1 && scrutinee.length == 1) {
82 Case case_ = cases[0];
83 Expression pattern = case_.patterns[0];
84 if(case_.patterns[0] instanceof EVariable
85 && !(case_.value instanceof GuardedExpressionGroup)) {
86 Variable var = ((EVariable)pattern).variable;
87 return new ESimpleLet(var, scrutinee[0], case_.value);
94 public Expression resolve(TranslationContext context) {
95 for(int i=0;i<scrutinee.length;++i)
96 scrutinee[i] = scrutinee[i].resolve(context);
97 for(Case case_ : cases)
98 case_.resolve(context);
103 public void setLocationDeep(long loc) {
104 if(location == Locations.NO_LOCATION) {
106 for(Case case_ : cases)
107 case_.setLocationDeep(loc);
108 for(Expression e : scrutinee)
109 e.setLocationDeep(loc);
114 public Expression replace(ReplaceContext context) {
115 Expression[] newScrutinee = new Expression[scrutinee.length];
116 for(int i=0;i<scrutinee.length;++i)
117 newScrutinee[i] = scrutinee[i].replace(context);
118 Case[] newCases = new Case[cases.length];
119 for(int i=0;i<cases.length;++i)
120 newCases[i] = cases[i].replace(context);
121 return new EMatch(getLocation(), newScrutinee, newCases);
125 public Expression checkBasicType(TypingContext context, Type requiredType) {
126 Type[] scrutineeTypes = new Type[scrutinee.length];
127 for(int i=0;i<scrutinee.length;++i) {
128 scrutinee[i] = scrutinee[i].checkType(context, Types.metaVar(Kinds.STAR));
129 scrutineeTypes[i] = scrutinee[i].getType();
131 for(Case case_ : cases)
132 case_.checkType(context, scrutineeTypes, requiredType);
133 setType(requiredType);
138 public Expression checkIgnoredType(TypingContext context) {
139 Type[] scrutineeTypes = new Type[scrutinee.length];
140 for(int i=0;i<scrutinee.length;++i) {
141 scrutinee[i] = scrutinee[i].checkType(context, Types.metaVar(Kinds.STAR));
142 scrutineeTypes[i] = scrutinee[i].getType();
144 for(Case case_ : cases)
145 case_.checkIgnoredType(context, scrutineeTypes);
151 public void accept(ExpressionVisitor visitor) {
155 public Expression[] getScrutinee() {
159 public Case[] getCases() {
164 public Expression accept(ExpressionTransformer transformer) {
165 return transformer.transform(this);
169 public int getSyntacticFunctionArity() {
171 for(Case case_ : cases)
172 result = Math.max(result, case_.value.getSyntacticFunctionArity());