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.Types;
21 import org.simantics.scl.compiler.types.exceptions.MatchException;
22 import org.simantics.scl.compiler.types.kinds.Kinds;
24 import gnu.trove.map.hash.TObjectIntHashMap;
25 import gnu.trove.set.hash.THashSet;
26 import gnu.trove.set.hash.TIntHashSet;
28 public class ECHRSelect extends Expression {
30 Variable[] existentialVariables;
31 Expression expression;
32 private ArrayList<PlanOp> planOps;
33 private CHRRuleset currentRuleset;
35 public ECHRSelect(Expression expression, CHRQuery query) {
36 this.expression = expression;
41 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
42 query.collectVars(allVars, vars);
43 expression.collectVars(allVars, vars);
47 protected void updateType() throws MatchException {
48 setType(Types.list(expression.getType()));
52 public Expression inferType(TypingContext context) {
53 for(Variable variable : existentialVariables)
54 variable.setType(Types.metaVar(Kinds.STAR));
55 query.checkType(context);
56 expression = expression.inferType(context);
61 public Expression simplify(SimplificationContext simplificationContext) {
62 this.expression = expression.simplify(simplificationContext);
63 query.simplify(simplificationContext);
65 CompilationContext compilationContext = simplificationContext.getCompilationContext();
66 QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
67 if(query.createQueryPlan(context, null, -1, null))
68 planOps = context.getPlanOps();
74 public IVal toVal(CompilationContext context, CodeWriter w) {
75 IVal list = w.apply(location, context.getValue(Names.MList_create).getValue(), NoRepConstant.UNIT);
76 planOps.add(new PlanOp(location) {
78 public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
79 w.apply(location, context.getValue(Names.MList_add).getValue(), list, expression.toVal(context, w));
82 PlanRealizer realizer = new PlanRealizer(context, currentRuleset, currentRuleset != null ? currentRuleset.runtimeRulesetVariable : null, null, planOps);
84 return w.apply(location, context.getValue(Names.MList_freeze).getValue(), list);
88 public void collectFreeVariables(THashSet<Variable> vars) {
89 query.collectFreeVariables(vars);
90 expression.collectFreeVariables(vars);
91 if(existentialVariables != null)
92 for(Variable variable : existentialVariables)
93 vars.remove(variable);
97 public Expression resolve(TranslationContext context) {
98 currentRuleset = context.currentRuleset;
100 context.pushExistentialFrame();
101 query.resolve(context);
102 context.disallowNewExistentials();
103 expression = expression.resolve(context);
104 existentialVariables = context.popExistentialFrame();
109 public void setLocationDeep(long loc) {
110 if(location == Locations.NO_LOCATION) {
111 query.setLocationDeep(loc);
112 expression.setLocationDeep(loc);
117 public void accept(ExpressionVisitor visitor) {
122 public Expression accept(ExpressionTransformer transformer) {
123 return transformer.transform(this);