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 public class ECHRSelect extends Expression {
25 public CHRQuery query;
26 public Variable[] existentialVariables;
27 public Expression expression;
28 private ArrayList<PlanOp> planOps;
29 private CHRRuleset currentRuleset;
31 public ECHRSelect(Expression expression, CHRQuery query) {
32 this.expression = expression;
37 protected void updateType() throws MatchException {
38 setType(Types.list(expression.getType()));
42 public Expression inferType(TypingContext context) {
43 for(Variable variable : existentialVariables)
44 variable.setType(Types.metaVar(Kinds.STAR));
45 query.checkType(context);
46 expression = expression.inferType(context);
51 public Expression simplify(SimplificationContext simplificationContext) {
52 this.expression = expression.simplify(simplificationContext);
53 query.simplify(simplificationContext);
55 CompilationContext compilationContext = simplificationContext.getCompilationContext();
56 QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
57 if(query.createQueryPlan(context, null, -1, null))
58 planOps = context.getPlanOps();
64 public IVal toVal(CompilationContext context, CodeWriter w) {
65 IVal list = w.apply(location, context.getValue(Names.MList_create).getValue(), NoRepConstant.UNIT);
66 planOps.add(new PlanOp(location) {
68 public void generateCode(CompilationContext context, PlanContext planContext, CodeWriter w) {
69 w.apply(location, context.getValue(Names.MList_add).getValue(), list, expression.toVal(context, w));
72 PlanRealizer realizer = new PlanRealizer(context, currentRuleset, currentRuleset != null ? currentRuleset.runtimeRulesetVariable : null, null, planOps);
74 return w.apply(location, context.getValue(Names.MList_freeze).getValue(), list);
78 public Expression resolve(TranslationContext context) {
79 currentRuleset = context.currentRuleset;
81 context.pushExistentialFrame();
82 query.resolve(context);
83 context.disallowNewExistentials();
84 expression = expression.resolve(context);
85 existentialVariables = context.popExistentialFrame();
90 public void setLocationDeep(long loc) {
91 if(location == Locations.NO_LOCATION) {
92 query.setLocationDeep(loc);
93 expression.setLocationDeep(loc);
98 public void accept(ExpressionVisitor visitor) {
103 public Expression accept(ExpressionTransformer transformer) {
104 return transformer.transform(this);