1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.common.names.Names;
6 import org.simantics.scl.compiler.compilation.CompilationContext;
7 import org.simantics.scl.compiler.constants.NoRepConstant;
8 import org.simantics.scl.compiler.elaboration.chr.CHRQuery;
9 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
10 import org.simantics.scl.compiler.elaboration.chr.plan.PlanContext;
11 import org.simantics.scl.compiler.elaboration.chr.plan.PlanOp;
12 import org.simantics.scl.compiler.elaboration.chr.plan.PlanRealizer;
13 import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext;
14 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
15 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
16 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
17 import org.simantics.scl.compiler.errors.Locations;
18 import org.simantics.scl.compiler.internal.codegen.references.IVal;
19 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
20 import org.simantics.scl.compiler.types.Type;
21 import org.simantics.scl.compiler.types.Types;
22 import org.simantics.scl.compiler.types.exceptions.MatchException;
23 import org.simantics.scl.compiler.types.kinds.Kinds;
25 import gnu.trove.map.hash.TObjectIntHashMap;
26 import gnu.trove.set.hash.THashSet;
27 import gnu.trove.set.hash.TIntHashSet;
29 public class ECHRSelect extends Expression {
31 Variable[] existentialVariables;
32 Expression expression;
33 private ArrayList<PlanOp> planOps;
34 private CHRRuleset currentRuleset;
36 public ECHRSelect(Expression expression, CHRQuery query) {
37 this.expression = expression;
42 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
43 query.collectRefs(allRefs, refs);
44 expression.collectRefs(allRefs, refs);
48 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
49 query.collectVars(allVars, vars);
50 expression.collectVars(allVars, vars);
54 protected void updateType() throws MatchException {
55 setType(Types.list(expression.getType()));
59 public Expression inferType(TypingContext context) {
60 for(Variable variable : existentialVariables)
61 variable.setType(Types.metaVar(Kinds.STAR));
62 query.checkType(context);
63 expression = expression.inferType(context);
68 public Expression simplify(SimplificationContext simplificationContext) {
69 this.expression = expression.simplify(simplificationContext);
70 query.simplify(simplificationContext);
72 CompilationContext compilationContext = simplificationContext.getCompilationContext();
73 QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
74 if(query.createQueryPlan(context, null, -1, null))
75 planOps = context.getPlanOps();
81 public IVal toVal(CompilationContext context, CodeWriter w) {
82 IVal list = w.apply(location, context.getValue(Names.MList_create).getValue(), NoRepConstant.UNIT);
83 planOps.add(new PlanOp(location) {
85 public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
86 w.apply(location, context.getValue(Names.MList_add).getValue(), list, expression.toVal(context, w));
89 PlanRealizer realizer = new PlanRealizer(context, currentRuleset, currentRuleset != null ? currentRuleset.runtimeRulesetVariable : null, null, planOps);
91 return w.apply(location, context.getValue(Names.MList_freeze).getValue(), list);
95 public void collectFreeVariables(THashSet<Variable> vars) {
96 query.collectFreeVariables(vars);
97 expression.collectFreeVariables(vars);
98 if(existentialVariables != null)
99 for(Variable variable : existentialVariables)
100 vars.remove(variable);
104 public Expression resolve(TranslationContext context) {
105 currentRuleset = context.currentRuleset;
107 context.pushExistentialFrame();
108 query.resolve(context);
109 context.disallowNewExistentials();
110 expression = expression.resolve(context);
111 existentialVariables = context.popExistentialFrame();
116 public void setLocationDeep(long loc) {
117 if(location == Locations.NO_LOCATION) {
118 query.setLocationDeep(loc);
119 expression.setLocationDeep(loc);
124 public void collectEffects(THashSet<Type> effects) {
125 expression.collectEffects(effects);
126 query.collectQueryEffects(effects);
127 effects.add(Types.PROC);
131 public void accept(ExpressionVisitor visitor) {
136 public Expression accept(ExpressionTransformer transformer) {
137 return transformer.transform(this);