]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/planning/items/MemberPrePlanItem.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / planning / items / MemberPrePlanItem.java
1 package org.simantics.scl.compiler.elaboration.chr.planning.items;
2
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.common.names.Names;
5 import org.simantics.scl.compiler.elaboration.chr.plan.CheckOp;
6 import org.simantics.scl.compiler.elaboration.chr.plan.IterateListOp;
7 import org.simantics.scl.compiler.elaboration.chr.planning.PrePlanItem;
8 import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext;
9 import org.simantics.scl.compiler.elaboration.expressions.EApply;
10 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
11 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
12 import org.simantics.scl.compiler.elaboration.expressions.Expression;
13
14 import gnu.trove.set.hash.TIntHashSet;
15
16 public class MemberPrePlanItem extends PrePlanItem {
17     public Expression expression1, expression2;
18     public TIntHashSet variableSet1, variableSet2;
19
20     public MemberPrePlanItem(Expression expression1, Expression expression2, TIntHashSet variableSet1, TIntHashSet variableSet2, int secondaryPriority) {
21         super(secondaryPriority);
22         this.expression1 = expression1;
23         this.expression2 = expression2;
24         this.variableSet1 = variableSet1;
25         this.variableSet2 = variableSet2;
26         updatePrimaryPriority();
27     }
28
29     private void updatePrimaryPriority() {
30         if(variableSet2.isEmpty()) {
31             if(variableSet1.isEmpty())
32                 primaryPriority = 0;
33             else 
34                 primaryPriority = 2.0;
35         }
36     }
37
38     @Override
39     public void initializeListeners(QueryPlanningContext context) {
40         context.listen(variableSet1, this);
41         context.listen(variableSet2, this);
42     }
43
44     @Override
45     public void variableSolved(QueryPlanningContext context, int variableId) {
46         variableSet1.remove(variableId);
47         variableSet2.remove(variableId);
48         updatePrimaryPriority();
49         context.priorityQueue.adjust(this);
50     }
51
52     @Override
53     public void generate(QueryPlanningContext context) {
54         if(!variableSet2.isEmpty())
55             throw new InternalCompilerError("Unsolvable query.");
56         if(variableSet1.isEmpty())
57             context.addPlanOp(new CheckOp(location,
58                     new EApply(location,
59                             new EConstant(context.getCompilationContext().getValue(Names.Prelude_elem)),
60                             expression1,
61                             expression2)));
62         else
63             context.addPlanOp(new IterateListOp(location, ((EVariable)expression1).getVariable(), expression2));
64         context.bind(variableSet1);
65     }
66 }