]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/query/QAtom.java
(refs #7375) Replaced collectFreeVariables method by a visitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / query / QAtom.java
index 95760513b5383e80136b8005ff9ed50ce4a8b82a..208513b21bff747d28700e4d2ac2b58a37cffa66 100644 (file)
@@ -1,11 +1,5 @@
 package org.simantics.scl.compiler.elaboration.query;
 
-import gnu.trove.map.hash.THashMap;
-import gnu.trove.map.hash.TIntObjectHashMap;
-import gnu.trove.map.hash.TObjectIntHashMap;
-import gnu.trove.set.hash.THashSet;
-import gnu.trove.set.hash.TIntHashSet;
-
 import java.util.ArrayList;
 import java.util.Set;
 
@@ -31,28 +25,27 @@ import org.simantics.scl.compiler.types.TVar;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
 
+import gnu.trove.map.hash.THashMap;
+import gnu.trove.map.hash.TIntObjectHashMap;
+import gnu.trove.map.hash.TObjectIntHashMap;
+import gnu.trove.set.hash.TIntHashSet;
+
 public class QAtom extends Query {
     public SCLRelation relation;
     public Type[] typeParameters;
     public Expression[] parameters;
-    
+
     public QAtom(SCLRelation relation, Expression ... parameters) {
         this.relation = relation;
         this.parameters = parameters;
     }
-    
+
     public QAtom(SCLRelation relation, Type[] typeParameters, Expression ... parameters) {
         this.relation = relation;
         this.typeParameters = typeParameters;
         this.parameters = parameters;
     }
 
-    @Override
-    public void collectFreeVariables(THashSet<Variable> vars) {
-        for(Expression parameter : parameters)
-            parameter.collectFreeVariables(vars);
-    }
-    
     @Override
     public void checkType(TypingContext context) {
         // Type parameters
@@ -60,7 +53,7 @@ public class QAtom extends Query {
         typeParameters = new Type[typeVariables.length];
         for(int i=0;i<typeVariables.length;++i)
             typeParameters[i] = Types.metaVar(typeVariables[i].getKind());
-        
+
         // Check parameter types
         Type[] parameterTypes = relation.getParameterTypes();
         if(parameterTypes.length != parameters.length)
@@ -70,7 +63,7 @@ public class QAtom extends Query {
                 parameters[i] = parameters[i]
                         .checkType(context, parameterTypes[i].replace(typeVariables, typeParameters));
     }
-    
+
     public Expression generateEnforce(EnforcingContext context) {
         Variable[] variables = new Variable[parameters.length];
         for(int i=0;i<variables.length;++i)
@@ -92,7 +85,7 @@ public class QAtom extends Query {
     private static class VariableMaskProcedure implements VariableProcedure {
         ConstraintCollectionContext context;
         long requiredVariablesMask = 0L;
-        
+
         public VariableMaskProcedure(ConstraintCollectionContext context) {
             this.context = context;
         }
@@ -104,7 +97,7 @@ public class QAtom extends Query {
                 requiredVariablesMask |= 1L << id;
         }
     }
-    
+
     @Override
     public void collectConstraints(ConstraintCollectionContext context) {
         try {
@@ -129,45 +122,24 @@ public class QAtom extends Query {
                     }
                     else {
                         optionalVariableByParameter[i] = -1;
-                        parameter.forVariables(procedure);
+                        parameter.forVariableUses(procedure);
                     }
                 }
             }
-                   
+
             // Combine required and optional variables
             TIntHashSet allVariablesSet = new TIntHashSet();
             for(int v : optionalVariableByParameter)
                 if(v >= 0)
                     allVariablesSet.add(v);
-            
+
             context.addConstraint(new RelationConstraint(allVariablesSet.toArray(), varParameters, this,
                     optionalVariableByParameter, procedure.requiredVariablesMask));
         } catch(Exception e) {
             context.getQueryCompilationContext().getTypingContext().getErrorLog().log(location, e);
         }
     }
-    
-    private static void collectRefs(SCLRelation relation, TObjectIntHashMap<Object> allRefs,
-            TIntHashSet refs) {
-        if(relation instanceof CompositeRelation) {
-            for(SCLRelation subrelation : ((CompositeRelation) relation).getSubrelations())
-                collectRefs(subrelation, allRefs, refs);
-        }
-        else {
-            int id = allRefs.get(relation);
-            if(id >= 0)
-                refs.add(id);
-        }
-    }
 
-    @Override
-    public void collectRefs(TObjectIntHashMap<Object> allRefs,
-            TIntHashSet refs) {
-        collectRefs(relation, allRefs, refs);
-        for(Expression parameter : parameters)
-            parameter.collectRefs(allRefs, refs);
-    }
-    
     @Override
     public void collectVars(TObjectIntHashMap<Variable> allVars,
             TIntHashSet vars) {
@@ -215,7 +187,7 @@ public class QAtom extends Query {
             return new Diff[] { new Diff(diffable.id, new QConjunction(eqs)) };
         }
     }
-    
+
     private static boolean containsReferenceTo(
             CompositeRelation relation,
             THashMap<SCLRelation, Diffable> diffables) {
@@ -235,7 +207,7 @@ public class QAtom extends Query {
         else
             return this;
     }
-    
+
     @Override
     public void setLocationDeep(long loc) {
         if(location == Locations.NO_LOCATION) {
@@ -244,18 +216,12 @@ public class QAtom extends Query {
                 parameter.setLocationDeep(loc);
         }
     }
-    
+
     @Override
     public void accept(QueryVisitor visitor) {
         visitor.visit(this);
     }
 
-    @Override
-    public void forVariables(VariableProcedure procedure) {
-        for(Expression parameter : parameters)
-            parameter.forVariables(procedure);
-    }
-
     @Override
     public void splitToPhases(TIntObjectHashMap<ArrayList<Query>> result) {
         int phase = relation.getPhase();
@@ -266,7 +232,7 @@ public class QAtom extends Query {
         }
         list.add(this);
     }
-    
+
     @Override
     public Query accept(QueryTransformer transformer) {
         return transformer.transform(this);