]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRSelect.java
(refs #7371) Support expression cloning for ECHRSelect
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ECHRSelect.java
index bf3b818f56c01c844b078705751ca1f3d20fd873..db651684f0ee124252abc9adb73721ce05d094be 100644 (file)
@@ -2,6 +2,7 @@ package org.simantics.scl.compiler.elaboration.expressions;
 
 import java.util.ArrayList;
 
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
 import org.simantics.scl.compiler.common.names.Names;
 import org.simantics.scl.compiler.compilation.CompilationContext;
 import org.simantics.scl.compiler.constants.NoRepConstant;
@@ -11,6 +12,7 @@ import org.simantics.scl.compiler.elaboration.chr.plan.PlanContext;
 import org.simantics.scl.compiler.elaboration.chr.plan.PlanOp;
 import org.simantics.scl.compiler.elaboration.chr.plan.PlanRealizer;
 import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext;
+import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
@@ -51,17 +53,16 @@ public class ECHRSelect extends Expression {
     public Expression simplify(SimplificationContext simplificationContext) {
         this.expression = expression.simplify(simplificationContext);
         query.simplify(simplificationContext);
-        
-        CompilationContext compilationContext = simplificationContext.getCompilationContext();
-        QueryPlanningContext context = new QueryPlanningContext(compilationContext, existentialVariables);
-        if(query.createQueryPlan(context, null, -1, null))
-            planOps = context.getPlanOps();
 
         return this;
     }
     
     @Override
     public IVal toVal(CompilationContext context, CodeWriter w) {
+        QueryPlanningContext queryContext = new QueryPlanningContext(context, existentialVariables);
+        if(query.createQueryPlan(queryContext, null, -1, null))
+            planOps = queryContext.getPlanOps();
+        
         IVal list = w.apply(location, context.getValue(Names.MList_create).getValue(), NoRepConstant.UNIT);
         planOps.add(new PlanOp(location) {
             @Override
@@ -103,4 +104,25 @@ public class ECHRSelect extends Expression {
     public Expression accept(ExpressionTransformer transformer) {
         return transformer.transform(this);
     }
+    
+    @Override
+    public Expression replace(ReplaceContext context) {
+        Variable[] newExistentialVariables = new Variable[existentialVariables.length];
+        for(int i=0;i<existentialVariables.length;++i) {
+            Variable newVariable = existentialVariables[i].copy();
+            context.varMap.put(existentialVariables[i], new EVariable(newVariable));
+            newExistentialVariables[i] = newVariable;
+        }
+        ECHRSelect copy = new ECHRSelect(expression.replace(context), query.replace(context));
+        copy.existentialVariables = newExistentialVariables;
+        copy.currentRuleset = currentRuleset;
+        copy.planOps = planOps;
+        if(planOps != null) {
+            copy.planOps = new ArrayList<PlanOp>(planOps.size());
+            throw new InternalCompilerError(location, "Copying of ECHRSelect is not supported.");
+            //for(PlanOp op : planOps)
+            //    copy.planOps.add(op.replace(context));
+        }
+        return copy;
+    }
 }