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.internal.elaboration.utils.ExpressionDecorator;
21 import org.simantics.scl.compiler.types.Type;
22 import org.simantics.scl.compiler.types.Types;
23 import org.simantics.scl.compiler.types.exceptions.MatchException;
24 import org.simantics.scl.compiler.types.kinds.Kinds;
26 import gnu.trove.map.hash.TObjectIntHashMap;
27 import gnu.trove.set.hash.THashSet;
28 import gnu.trove.set.hash.TIntHashSet;
30 public class ECHRSelect extends Expression {
32 Variable[] existentialVariables;
33 Expression expression;
34 private ArrayList<PlanOp> planOps;
35 private CHRRuleset currentRuleset;
37 public ECHRSelect(Expression expression, CHRQuery query) {
38 this.expression = expression;
43 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
44 query.collectRefs(allRefs, refs);
45 expression.collectRefs(allRefs, refs);
49 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
50 query.collectVars(allVars, vars);
51 expression.collectVars(allVars, vars);
55 public void forVariables(VariableProcedure procedure) {
56 query.forVariables(procedure);
57 expression.forVariables(procedure);
61 protected void updateType() throws MatchException {
62 setType(Types.list(expression.getType()));
66 public Expression inferType(TypingContext context) {
67 for(Variable variable : existentialVariables)
68 variable.setType(Types.metaVar(Kinds.STAR));
69 query.checkType(context);
70 expression = expression.inferType(context);
75 public Expression simplify(SimplificationContext simplificationContext) {
76 this.expression = expression.simplify(simplificationContext);
77 query.simplify(simplificationContext);
79 CompilationContext compilationContext = simplificationContext.getCompilationContext();
80 QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
81 if(query.createQueryPlan(context, null, -1, null))
82 planOps = context.getPlanOps();
88 public IVal toVal(CompilationContext context, CodeWriter w) {
89 IVal list = w.apply(location, context.getValue(Names.MList_create).getValue(), NoRepConstant.UNIT);
90 planOps.add(new PlanOp(location) {
92 public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
93 w.apply(location, context.getValue(Names.MList_add).getValue(), list, expression.toVal(context, w));
96 PlanRealizer realizer = new PlanRealizer(context, currentRuleset, currentRuleset != null ? currentRuleset.runtimeRulesetVariable : null, null, planOps);
98 return w.apply(location, context.getValue(Names.MList_freeze).getValue(), list);
102 public void collectFreeVariables(THashSet<Variable> vars) {
103 query.collectFreeVariables(vars);
104 expression.collectFreeVariables(vars);
105 if(existentialVariables != null)
106 for(Variable variable : existentialVariables)
107 vars.remove(variable);
111 public Expression resolve(TranslationContext context) {
112 currentRuleset = context.currentRuleset;
114 context.pushExistentialFrame();
115 query.resolve(context);
116 context.disallowNewExistentials();
117 expression = expression.resolve(context);
118 existentialVariables = context.popExistentialFrame();
123 public void setLocationDeep(long loc) {
124 if(location == Locations.NO_LOCATION) {
125 query.setLocationDeep(loc);
126 expression.setLocationDeep(loc);
131 public Expression decorate(ExpressionDecorator decorator) {
132 this.expression = decorator.decorate(expression);
137 public void collectEffects(THashSet<Type> effects) {
138 expression.collectEffects(effects);
139 query.collectQueryEffects(effects);
140 effects.add(Types.PROC);
144 public void accept(ExpressionVisitor visitor) {
149 public Expression accept(ExpressionTransformer transformer) {
150 return transformer.transform(this);