d148339a46946132187f04a1ed9433056ff3401a
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / query / QIf.java
1 package org.simantics.scl.compiler.elaboration.query;
2
3 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
4 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
5 import org.simantics.scl.compiler.elaboration.expressions.Expression;
6 import org.simantics.scl.compiler.elaboration.expressions.QueryTransformer;
7 import org.simantics.scl.compiler.elaboration.expressions.Variable;
8 import org.simantics.scl.compiler.elaboration.query.compilation.ConstraintCollectionContext;
9 import org.simantics.scl.compiler.elaboration.query.compilation.UnsolvableQueryException;
10 import org.simantics.scl.compiler.errors.Locations;
11 import org.simantics.scl.compiler.types.Types;
12
13 import gnu.trove.map.hash.TObjectIntHashMap;
14 import gnu.trove.set.hash.THashSet;
15 import gnu.trove.set.hash.TIntHashSet;
16
17 public class QIf extends Query {
18     public Expression condition;
19     public Query thenQuery;
20     public Query elseQuery;
21     
22     public QIf(Expression condition, Query thenQuery, Query elseQuery) {
23         this.condition = condition;
24         this.thenQuery = thenQuery;
25         this.elseQuery = elseQuery;
26     }
27
28     @Override
29     public void collectFreeVariables(THashSet<Variable> vars) {
30         condition.collectFreeVariables(vars);
31         thenQuery.collectFreeVariables(vars);
32         elseQuery.collectFreeVariables(vars);
33     }
34
35     @Override
36     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
37         condition.collectVars(allVars, vars);
38         thenQuery.collectVars(allVars, vars);
39         elseQuery.collectVars(allVars, vars);
40     }
41
42     @Override
43     public void checkType(TypingContext context) {
44         condition.checkType(context, Types.BOOLEAN);
45         thenQuery.checkType(context);
46         elseQuery.checkType(context);
47     }
48
49     @Override
50     public void collectConstraints(ConstraintCollectionContext context) throws UnsolvableQueryException {
51         // TODO Auto-generated method stub
52         
53     }
54
55     @Override
56     public Query replace(ReplaceContext context) {
57         return new QIf(
58                 condition.replace(context),
59                 thenQuery.replace(context),
60                 elseQuery.replace(context));
61     }
62
63     @Override
64     public void setLocationDeep(long loc) {
65         if(location == Locations.NO_LOCATION) {
66             this.location = loc;
67             condition.setLocationDeep(loc);
68             elseQuery.setLocationDeep(loc);
69             thenQuery.setLocationDeep(loc);
70         }
71     }
72
73     @Override
74     public void accept(QueryVisitor visitor) {
75         visitor.visit(this);
76     }
77     
78     @Override
79     public Query accept(QueryTransformer transformer) {
80         return transformer.transform(this);
81     }
82     
83 }