1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.compilation.CompilationContext;
4 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.internal.codegen.continuations.ICont;
10 import org.simantics.scl.compiler.internal.codegen.references.IVal;
11 import org.simantics.scl.compiler.internal.codegen.ssa.exits.Throw;
12 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
13 import org.simantics.scl.compiler.types.Type;
14 import org.simantics.scl.compiler.types.Types;
15 import org.simantics.scl.compiler.types.exceptions.MatchException;
17 import gnu.trove.map.hash.TObjectIntHashMap;
18 import gnu.trove.set.hash.TIntHashSet;
20 public class GuardedExpressionGroup extends Expression {
21 public GuardedExpression[] expressions;
23 public GuardedExpressionGroup(GuardedExpression[] expressions) {
24 this.expressions = expressions;
28 public void collectVars(TObjectIntHashMap<Variable> allVars,
30 for(GuardedExpression expression : expressions) {
31 for(Expression guard : expression.guards)
32 guard.collectVars(allVars, vars);
33 expression.value.collectVars(allVars, vars);
38 protected void updateType() throws MatchException {
39 setType(expressions[0].value.getType());
43 public IVal toVal(CompilationContext context, CodeWriter w) {
44 CodeWriter success = w.createBlock(getType());
45 IVal result = success.getParameters()[0];
46 CodeWriter failure = w.createBlock();
47 compile(context, w, success.getContinuation(), failure.getContinuation());
48 w.continueAs(success);
49 failure.throw_(location, Throw.MatchingException, "Matching failure at: " + toString());
51 //throw new InternalCompilerError("GuardedExpressionGroup should be handled in match compilation.");
55 public Expression simplify(SimplificationContext context) {
56 for(GuardedExpression expression : expressions) {
57 for(int i=0;i<expression.guards.length;++i)
58 expression.guards[i] = expression.guards[i].simplify(context);
59 expression.value = expression.value.simplify(context);
65 public Expression resolve(TranslationContext context) {
66 for(GuardedExpression expression : expressions) {
67 for(int i=0;i<expression.guards.length;++i)
68 expression.guards[i] = expression.guards[i].resolve(context);
69 expression.value = expression.value.resolve(context);
75 public Expression checkBasicType(TypingContext context, Type requiredType) {
76 for(GuardedExpression expression : expressions) {
77 for(int i=0;i<expression.guards.length;++i)
78 expression.guards[i] = expression.guards[i].checkType(context, Types.BOOLEAN);
79 expression.value = expression.value.checkType(context, requiredType);
84 public void compile(CompilationContext context, CodeWriter firstWriter, ICont success,
86 // Create all code blocks
87 CodeWriter[] writers = new CodeWriter[expressions.length];
88 ICont[] failures = new ICont[expressions.length];
89 writers[0] = firstWriter;
90 failures[expressions.length-1] = lastFailure;
91 for(int i=1;i<expressions.length;++i) {
92 CodeWriter writer = firstWriter.createBlock();
94 failures[i-1] = writer.getContinuation();
98 for(int i=0;i<expressions.length;++i) {
99 CodeWriter w = writers[i];
100 ICont failure = failures[i];
102 for(Expression guard : expressions[i].guards) {
103 CodeWriter nextW = w.createBlock();
104 w.if_(guard.toVal(context, w), nextW.getContinuation(), failure);
108 w.jump(success, expressions[i].value.toVal(context, w));
113 public Expression replace(ReplaceContext context) {
114 GuardedExpression[] newExpressions = new GuardedExpression[expressions.length];
115 for(int i=0;i<expressions.length;++i)
116 newExpressions[i] = expressions[i].replace(context);
117 return new GuardedExpressionGroup(newExpressions);
121 public void setLocationDeep(long loc) {
122 if(location == Locations.NO_LOCATION) {
124 for(GuardedExpression expression : expressions)
125 expression.setLocationDeep(loc);
130 public void accept(ExpressionVisitor visitor) {
135 public Expression accept(ExpressionTransformer transformer) {
136 return transformer.transform(this);
140 public int getSyntacticFunctionArity() {
141 return expressions[0].value.getSyntacticFunctionArity();