]> gerrit.simantics Code Review - simantics/platform.git/blob
890d11f8c7a2dc084611786332159f4a3ec48dd5
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
4 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
7 import org.simantics.scl.compiler.environment.Environment;
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;
17
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
21
22 public class GuardedExpressionGroup extends Expression {
23     public GuardedExpression[] expressions;
24
25     public GuardedExpressionGroup(GuardedExpression[] expressions) {
26         this.expressions = expressions;
27     }
28
29     @Override
30     public void collectRefs(TObjectIntHashMap<Object> allRefs,
31             TIntHashSet refs) {
32         for(GuardedExpression expression : expressions) {
33             for(Expression guard : expression.guards)
34                 guard.collectRefs(allRefs, refs);
35             expression.value.collectRefs(allRefs, refs);
36         }
37     }
38
39     @Override
40     public void collectVars(TObjectIntHashMap<Variable> allVars,
41             TIntHashSet vars) {
42         for(GuardedExpression expression : expressions) {
43             for(Expression guard : expression.guards)
44                 guard.collectVars(allVars, vars);
45             expression.value.collectVars(allVars, vars);
46         }
47     }
48
49     @Override
50     protected void updateType() throws MatchException {
51         setType(expressions[0].value.getType());
52     }
53
54     @Override
55     public IVal toVal(Environment env, CodeWriter w) {
56         CodeWriter success = w.createBlock(getType());
57         IVal result = success.getParameters()[0];
58         CodeWriter failure = w.createBlock();
59         compile(env, w, success.getContinuation(), failure.getContinuation());
60         w.continueAs(success);
61         failure.throw_(location, Throw.MatchingException, "Matching failure at: " + toString());
62         return result;
63         //throw new InternalCompilerError("GuardedExpressionGroup should be handled in match compilation.");
64     }
65
66     @Override
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);
72         }
73     }
74
75     @Override
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);
81         }
82         return this;
83     }
84
85     @Override
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);
91         }
92         return this;
93     }
94     
95     @Override
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);
101         }
102         return this;
103     }
104     
105     public void compile(Environment env, CodeWriter firstWriter, ICont success,
106             ICont lastFailure) {
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();
114             writers[i] = writer;
115             failures[i-1] = writer.getContinuation();
116         }
117         
118         // Compile
119         for(int i=0;i<expressions.length;++i) {
120             CodeWriter w = writers[i];
121             ICont failure = failures[i];
122             
123             for(Expression guard : expressions[i].guards) {
124                 CodeWriter nextW = w.createBlock();
125                 w.if_(guard.toVal(env, w), nextW.getContinuation(), failure);
126                 w = nextW;
127             }
128             
129             w.jump(success, expressions[i].value.toVal(env, w));
130         }
131     }
132     
133     @Override
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);            
139     }
140
141     @Override
142     public Expression decorate(ExpressionDecorator decorator) {
143         for(GuardedExpression expression : expressions)
144             expression.decorate(decorator);
145         return decorator.decorate(this);
146     }
147
148     @Override
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);
154         }
155     }
156     
157     @Override
158     public void setLocationDeep(long loc) {
159         if(location == Locations.NO_LOCATION) {
160             location = loc;
161             for(GuardedExpression expression : expressions)
162                 expression.setLocationDeep(loc);
163         }
164     }
165     
166     @Override
167     public void accept(ExpressionVisitor visitor) {
168         visitor.visit(this);
169     }
170
171     @Override
172     public void forVariables(VariableProcedure procedure) {
173         for(GuardedExpression expression : expressions)
174             expression.forVariables(procedure);
175     }
176
177     @Override
178     public Expression accept(ExpressionTransformer transformer) {
179         return transformer.transform(this);
180     }
181     
182     @Override
183     public int getSyntacticFunctionArity() {
184         return expressions[0].value.getSyntacticFunctionArity();
185     }
186 }