]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRQuery.java
(refs #7866) Better error location for the NPE in compilation
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / CHRQuery.java
index 8bba6697d9bb3832782ee20ab4dd5af493219ab1..7bee6eae06b08a481652df67e5009d76629dfde4 100644 (file)
@@ -1,10 +1,11 @@
 package org.simantics.scl.compiler.elaboration.chr;
 
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
 import org.simantics.scl.compiler.elaboration.chr.plan.PostCommitOp;
 import org.simantics.scl.compiler.elaboration.chr.plan.PreCommitOp;
 import org.simantics.scl.compiler.elaboration.chr.planning.QueryPlanningContext;
 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
-import org.simantics.scl.compiler.elaboration.chr.relations.SpecialCHRRelation;
+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;
@@ -13,17 +14,16 @@ import org.simantics.scl.compiler.elaboration.expressions.Variable;
 import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor;
 import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.compiler.internal.parsing.Symbol;
-import org.simantics.scl.compiler.types.Type;
 
 import gnu.trove.map.hash.TObjectIntHashMap;
-import gnu.trove.set.hash.THashSet;
 import gnu.trove.set.hash.TIntHashSet;
 
 public class CHRQuery extends Symbol {
     public CHRLiteral[] literals;
 
-    public CHRQuery(CHRLiteral[] literals) {
+    public CHRQuery(long location, CHRLiteral[] literals) {
         this.literals = literals;
+        this.location = location;
     }
 
     public void resolve(TranslationContext context) {
@@ -40,15 +40,7 @@ public class CHRQuery extends Symbol {
         for(CHRLiteral literal : literals)
             literal.collectVars(allVars, vars);
     }
-
-    public void collectFreeVariables(THashSet<Variable> vars) {
-        for(CHRLiteral literal : literals)
-            literal.collectFreeVariables(vars);
-        for(CHRLiteral literal : literals)
-            if(literal.relation == SpecialCHRRelation.ASSIGN)
-                literal.parameters[0].removeFreeVariables(vars);
-    }
-
+    
     public void setLocationDeep(long loc) {
         if(location == Locations.NO_LOCATION) {
             this.location = loc;
@@ -58,17 +50,21 @@ public class CHRQuery extends Symbol {
     }
     
     public boolean createQueryPlan(QueryPlanningContext context, Expression inputFact, int activeLiteralId, CHRConstraint initConstraint) {
-        for(int i=0;i<literals.length;++i) {
-            CHRLiteral literal = literals[i];
-            if(i == activeLiteralId)
-                context.activate(literal, inputFact, i);
-            else
-                context.add(literal, i);
+        try {
+            for(int i=0;i<literals.length;++i) {
+                CHRLiteral literal = literals[i];
+                if(i == activeLiteralId)
+                    context.activate(literal, inputFact, i);
+                else
+                    context.add(literal, i);
+            }
+            if(activeLiteralId == -1 && inputFact != null) {
+                context.addInitFact(initConstraint, inputFact);
+            }  
+            return context.createQueryPlan();
+        } catch(Exception e) {
+            throw InternalCompilerError.injectLocation(location, e);
         }
-        if(activeLiteralId == -1 && inputFact != null) {
-            context.addInitFact(initConstraint, inputFact);
-        }      
-        return context.createQueryPlan();
     }
     
     public void simplify(SimplificationContext context) {
@@ -90,13 +86,10 @@ public class CHRQuery extends Symbol {
         return b.toString();
     }
 
-    public void collectQueryEffects(THashSet<Type> effects) {
-        for(CHRLiteral literal : literals)
-            literal.collectQueryEffects(effects);
-    }
-    
-    public void collectEnforceEffects(THashSet<Type> effects) {
-        for(CHRLiteral literal : literals)
-            literal.collectEnforceEffects(effects);
+    public CHRQuery replace(ReplaceContext context) {
+        CHRLiteral[] newLiterals = new CHRLiteral[literals.length];
+        for(int i=0;i<literals.length;++i)
+            newLiterals[i] = literals[i].replace(context);
+        return new CHRQuery(location, newLiterals);
     }
 }