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 collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
43 query.collectVars(allVars, vars);
44 expression.collectVars(allVars, vars);
48 protected void updateType() throws MatchException {
49 setType(Types.list(expression.getType()));
53 public Expression inferType(TypingContext context) {
54 for(Variable variable : existentialVariables)
55 variable.setType(Types.metaVar(Kinds.STAR));
56 query.checkType(context);
57 expression = expression.inferType(context);
62 public Expression simplify(SimplificationContext simplificationContext) {
63 this.expression = expression.simplify(simplificationContext);
64 query.simplify(simplificationContext);
66 CompilationContext compilationContext = simplificationContext.getCompilationContext();
67 QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
68 if(query.createQueryPlan(context, null, -1, null))
69 planOps = context.getPlanOps();
75 public IVal toVal(CompilationContext context, CodeWriter w) {
76 IVal list = w.apply(location, context.getValue(Names.MList_create).getValue(), NoRepConstant.UNIT);
77 planOps.add(new PlanOp(location) {
79 public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
80 w.apply(location, context.getValue(Names.MList_add).getValue(), list, expression.toVal(context, w));
83 PlanRealizer realizer = new PlanRealizer(context, currentRuleset, currentRuleset != null ? currentRuleset.runtimeRulesetVariable : null, null, planOps);
85 return w.apply(location, context.getValue(Names.MList_freeze).getValue(), list);
89 public void collectFreeVariables(THashSet<Variable> vars) {
90 query.collectFreeVariables(vars);
91 expression.collectFreeVariables(vars);
92 if(existentialVariables != null)
93 for(Variable variable : existentialVariables)
94 vars.remove(variable);
98 public Expression resolve(TranslationContext context) {
99 currentRuleset = context.currentRuleset;
101 context.pushExistentialFrame();
102 query.resolve(context);
103 context.disallowNewExistentials();
104 expression = expression.resolve(context);
105 existentialVariables = context.popExistentialFrame();
110 public void setLocationDeep(long loc) {
111 if(location == Locations.NO_LOCATION) {
112 query.setLocationDeep(loc);
113 expression.setLocationDeep(loc);
118 public void collectEffects(THashSet<Type> effects) {
119 expression.collectEffects(effects);
120 query.collectQueryEffects(effects);
121 effects.add(Types.PROC);
125 public void accept(ExpressionVisitor visitor) {
130 public Expression accept(ExpressionTransformer transformer) {
131 return transformer.transform(this);