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.internal.elaboration.utils.ExpressionDecorator;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.compiler.types.Types;
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
22 public class GuardedExpressionGroup extends Expression {
23 public GuardedExpression[] expressions;
25 public GuardedExpressionGroup(GuardedExpression[] expressions) {
26 this.expressions = expressions;
30 public void collectRefs(TObjectIntHashMap<Object> allRefs,
32 for(GuardedExpression expression : expressions) {
33 for(Expression guard : expression.guards)
34 guard.collectRefs(allRefs, refs);
35 expression.value.collectRefs(allRefs, refs);
40 public void collectVars(TObjectIntHashMap<Variable> allVars,
42 for(GuardedExpression expression : expressions) {
43 for(Expression guard : expression.guards)
44 guard.collectVars(allVars, vars);
45 expression.value.collectVars(allVars, vars);
50 protected void updateType() throws MatchException {
51 setType(expressions[0].value.getType());
55 public IVal toVal(CompilationContext context, CodeWriter w) {
56 CodeWriter success = w.createBlock(getType());
57 IVal result = success.getParameters()[0];
58 CodeWriter failure = w.createBlock();
59 compile(context, w, success.getContinuation(), failure.getContinuation());
60 w.continueAs(success);
61 failure.throw_(location, Throw.MatchingException, "Matching failure at: " + toString());
63 //throw new InternalCompilerError("GuardedExpressionGroup should be handled in match compilation.");
67 public void collectFreeVariables(THashSet<Variable> vars) {
68 for(GuardedExpression expression : expressions) {
69 for(Expression guard : expression.guards)
70 guard.collectFreeVariables(vars);
71 expression.value.collectFreeVariables(vars);
76 public Expression simplify(SimplificationContext context) {
77 for(GuardedExpression expression : expressions) {
78 for(int i=0;i<expression.guards.length;++i)
79 expression.guards[i] = expression.guards[i].simplify(context);
80 expression.value = expression.value.simplify(context);
86 public Expression resolve(TranslationContext context) {
87 for(GuardedExpression expression : expressions) {
88 for(int i=0;i<expression.guards.length;++i)
89 expression.guards[i] = expression.guards[i].resolve(context);
90 expression.value = expression.value.resolve(context);
96 public Expression checkBasicType(TypingContext context, Type requiredType) {
97 for(GuardedExpression expression : expressions) {
98 for(int i=0;i<expression.guards.length;++i)
99 expression.guards[i] = expression.guards[i].checkType(context, Types.BOOLEAN);
100 expression.value = expression.value.checkType(context, requiredType);
105 public void compile(CompilationContext context, CodeWriter firstWriter, ICont success,
107 // Create all code blocks
108 CodeWriter[] writers = new CodeWriter[expressions.length];
109 ICont[] failures = new ICont[expressions.length];
110 writers[0] = firstWriter;
111 failures[expressions.length-1] = lastFailure;
112 for(int i=1;i<expressions.length;++i) {
113 CodeWriter writer = firstWriter.createBlock();
115 failures[i-1] = writer.getContinuation();
119 for(int i=0;i<expressions.length;++i) {
120 CodeWriter w = writers[i];
121 ICont failure = failures[i];
123 for(Expression guard : expressions[i].guards) {
124 CodeWriter nextW = w.createBlock();
125 w.if_(guard.toVal(context, w), nextW.getContinuation(), failure);
129 w.jump(success, expressions[i].value.toVal(context, w));
134 public Expression replace(ReplaceContext context) {
135 GuardedExpression[] newExpressions = new GuardedExpression[expressions.length];
136 for(int i=0;i<expressions.length;++i)
137 newExpressions[i] = expressions[i].replace(context);
138 return new GuardedExpressionGroup(newExpressions);
142 public Expression decorate(ExpressionDecorator decorator) {
143 for(GuardedExpression expression : expressions)
144 expression.decorate(decorator);
145 return decorator.decorate(this);
149 public void collectEffects(THashSet<Type> effects) {
150 for(GuardedExpression ge : expressions) {
151 for(Expression guard : ge.guards)
152 guard.collectEffects(effects);
153 ge.value.collectEffects(effects);
158 public void setLocationDeep(long loc) {
159 if(location == Locations.NO_LOCATION) {
161 for(GuardedExpression expression : expressions)
162 expression.setLocationDeep(loc);
167 public void accept(ExpressionVisitor visitor) {
172 public void forVariables(VariableProcedure procedure) {
173 for(GuardedExpression expression : expressions)
174 expression.forVariables(procedure);
178 public Expression accept(ExpressionTransformer transformer) {
179 return transformer.transform(this);
183 public int getSyntacticFunctionArity() {
184 return expressions[0].value.getSyntacticFunctionArity();