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 public void forVariables(VariableProcedure procedure) {
55 query.forVariables(procedure);
56 expression.forVariables(procedure);
60 protected void updateType() throws MatchException {
61 setType(Types.list(expression.getType()));
65 public Expression inferType(TypingContext context) {
66 for(Variable variable : existentialVariables)
67 variable.setType(Types.metaVar(Kinds.STAR));
68 query.checkType(context);
69 expression = expression.inferType(context);
74 public Expression simplify(SimplificationContext simplificationContext) {
75 this.expression = expression.simplify(simplificationContext);
76 query.simplify(simplificationContext);
78 CompilationContext compilationContext = simplificationContext.getCompilationContext();
79 QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
80 if(query.createQueryPlan(context, null, -1, null))
81 planOps = context.getPlanOps();
87 public IVal toVal(CompilationContext context, CodeWriter w) {
88 IVal list = w.apply(location, context.getValue(Names.MList_create).getValue(), NoRepConstant.UNIT);
89 planOps.add(new PlanOp(location) {
91 public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
92 w.apply(location, context.getValue(Names.MList_add).getValue(), list, expression.toVal(context, w));
95 PlanRealizer realizer = new PlanRealizer(context, currentRuleset, currentRuleset != null ? currentRuleset.runtimeRulesetVariable : null, null, planOps);
97 return w.apply(location, context.getValue(Names.MList_freeze).getValue(), list);
101 public void collectFreeVariables(THashSet<Variable> vars) {
102 query.collectFreeVariables(vars);
103 expression.collectFreeVariables(vars);
104 if(existentialVariables != null)
105 for(Variable variable : existentialVariables)
106 vars.remove(variable);
110 public Expression resolve(TranslationContext context) {
111 currentRuleset = context.currentRuleset;
113 context.pushExistentialFrame();
114 query.resolve(context);
115 context.disallowNewExistentials();
116 expression = expression.resolve(context);
117 existentialVariables = context.popExistentialFrame();
122 public void setLocationDeep(long loc) {
123 if(location == Locations.NO_LOCATION) {
124 query.setLocationDeep(loc);
125 expression.setLocationDeep(loc);
130 public void collectEffects(THashSet<Type> effects) {
131 expression.collectEffects(effects);
132 query.collectQueryEffects(effects);
133 effects.add(Types.PROC);
137 public void accept(ExpressionVisitor visitor) {
142 public Expression accept(ExpressionTransformer transformer) {
143 return transformer.transform(this);