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;
45 public void collectVars(TObjectIntHashMap<Variable> allVars,
47 for(Expression s : scrutinee)
48 s.collectVars(allVars, vars);
49 for(Case case_ : cases)
50 case_.collectVars(allVars, vars);
54 protected void updateType() {
55 setType(cases[0].value.getType());
59 public IVal toVal(CompilationContext context, CodeWriter w) {
60 ArrayList<Row> rows = new ArrayList<Row>(cases.length);
61 for(Case case_ : cases)
62 rows.add(new Row(case_.patterns, case_.value));
64 IVal[] scrutineeVals = new IVal[scrutinee.length];
65 for(int i=0;i<scrutinee.length;++i)
66 scrutineeVals[i] = scrutinee[i].toVal(context, w);
68 CodeWriter joinPoint = w.createBlock(getType());
69 CodeWriter failurePoint = w.createBlock(); // TODO generate only one failurePoint per function
70 PatternMatchingCompiler.split(w, context, scrutineeVals, joinPoint.getContinuation(), failurePoint.getContinuation(), rows);
71 failurePoint.throw_(location, Throw.MatchingException, "Matching failure at: " + toString());
72 w.continueAs(joinPoint);
73 return w.getParameters()[0];
77 public void collectFreeVariables(THashSet<Variable> vars) {
78 for(Expression s : scrutinee)
79 s.collectFreeVariables(vars);
80 for(Case case_ : cases)
81 case_.collectFreeVariables(vars);
85 public Expression simplify(SimplificationContext context) {
86 for(int i=0;i<scrutinee.length;++i)
87 scrutinee[i] = scrutinee[i].simplify(context);
88 for(Case case_ : cases)
89 case_.simplify(context);
90 if(cases.length == 1 && scrutinee.length == 1) {
91 Case case_ = cases[0];
92 Expression pattern = case_.patterns[0];
93 if(case_.patterns[0] instanceof EVariable
94 && !(case_.value instanceof GuardedExpressionGroup)) {
95 Variable var = ((EVariable)pattern).variable;
96 return new ESimpleLet(var, scrutinee[0], case_.value);
103 public Expression resolve(TranslationContext context) {
104 for(int i=0;i<scrutinee.length;++i)
105 scrutinee[i] = scrutinee[i].resolve(context);
106 for(Case case_ : cases)
107 case_.resolve(context);
112 public void setLocationDeep(long loc) {
113 if(location == Locations.NO_LOCATION) {
115 for(Case case_ : cases)
116 case_.setLocationDeep(loc);
117 for(Expression e : scrutinee)
118 e.setLocationDeep(loc);
123 public Expression replace(ReplaceContext context) {
124 Expression[] newScrutinee = new Expression[scrutinee.length];
125 for(int i=0;i<scrutinee.length;++i)
126 newScrutinee[i] = scrutinee[i].replace(context);
127 Case[] newCases = new Case[cases.length];
128 for(int i=0;i<cases.length;++i)
129 newCases[i] = cases[i].replace(context);
130 return new EMatch(getLocation(), newScrutinee, newCases);
134 public Expression checkBasicType(TypingContext context, Type requiredType) {
135 Type[] scrutineeTypes = new Type[scrutinee.length];
136 for(int i=0;i<scrutinee.length;++i) {
137 scrutinee[i] = scrutinee[i].checkType(context, Types.metaVar(Kinds.STAR));
138 scrutineeTypes[i] = scrutinee[i].getType();
140 for(Case case_ : cases)
141 case_.checkType(context, scrutineeTypes, requiredType);
142 setType(requiredType);
147 public Expression checkIgnoredType(TypingContext context) {
148 Type[] scrutineeTypes = new Type[scrutinee.length];
149 for(int i=0;i<scrutinee.length;++i) {
150 scrutinee[i] = scrutinee[i].checkType(context, Types.metaVar(Kinds.STAR));
151 scrutineeTypes[i] = scrutinee[i].getType();
153 for(Case case_ : cases)
154 case_.checkIgnoredType(context, scrutineeTypes);
160 public void collectEffects(THashSet<Type> effects) {
161 for(Expression s : scrutinee)
162 s.collectEffects(effects);
163 for(Case case_ : cases) {
164 for(Expression pattern : case_.patterns)
165 pattern.collectEffects(effects);
166 case_.value.collectEffects(effects);
171 public void accept(ExpressionVisitor visitor) {
175 public Expression[] getScrutinee() {
179 public Case[] getCases() {
184 public Expression accept(ExpressionTransformer transformer) {
185 return transformer.transform(this);
189 public int getSyntacticFunctionArity() {
191 for(Case case_ : cases)
192 result = Math.max(result, case_.value.getSyntacticFunctionArity());