]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/CHRLiteral.java
(refs #7601) Wildcard syntax for SCL records
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / CHRLiteral.java
index e7b3320d77758d87b023904aa880f980a99f8ec5..6bea58310c73b24dd8a0e841793252e09d926c53 100644 (file)
@@ -5,6 +5,7 @@ import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
 import org.simantics.scl.compiler.elaboration.chr.relations.ExternalCHRRelation;
 import org.simantics.scl.compiler.elaboration.chr.relations.SpecialCHRRelation;
 import org.simantics.scl.compiler.elaboration.chr.relations.UnresolvedCHRRelation;
+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.TranslationContext.ExistentialFrame;
@@ -12,7 +13,6 @@ import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
 import org.simantics.scl.compiler.elaboration.expressions.ERecord;
 import org.simantics.scl.compiler.elaboration.expressions.Expression;
 import org.simantics.scl.compiler.elaboration.expressions.Variable;
-import org.simantics.scl.compiler.elaboration.expressions.VariableProcedure;
 import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor;
 import org.simantics.scl.compiler.elaboration.expressions.records.FieldAssignment;
 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
@@ -24,7 +24,6 @@ import org.simantics.scl.compiler.types.Types;
 import org.simantics.scl.compiler.types.kinds.Kinds;
 
 import gnu.trove.map.hash.TObjectIntHashMap;
-import gnu.trove.set.hash.THashSet;
 import gnu.trove.set.hash.TIntHashSet;
 
 public class CHRLiteral extends Symbol {
@@ -38,15 +37,20 @@ public class CHRLiteral extends Symbol {
     public boolean negated;
     public boolean passive = true;
     
-    public CHRLiteral(long location, CHRRelation relation, Expression[] parameters, boolean remove, boolean negated) {
+    public CHRLiteral(long location, CHRRelation relation, Expression[] parameters, boolean killAfterMatch, boolean negated) {
         this.location = location;
         this.relation = relation;
         this.parameters = parameters;
-        this.killAfterMatch = remove;
+        this.killAfterMatch = killAfterMatch;
         this.negated = negated;
     }
 
     public void resolve(TranslationContext context) {
+        if(relation == SpecialCHRRelation.ASSIGN) {
+            parameters[1] = parameters[1].resolve(context);
+            parameters[0] = parameters[0].resolveAsPattern(context);
+            return;
+        }
         if(parameters != null) {
             for(int i=0;i<parameters.length;++i)
                 parameters[i] = parameters[i].resolve(context);
@@ -89,7 +93,7 @@ public class CHRLiteral extends Symbol {
                 context.getErrorLog().log(location, "Relation " + relation + " does not define field names.");
                 return;
             }
-            parameters = ERecord.translateFieldsToFunctionParameters(context, fields, fieldNames);
+            parameters = ERecord.translateFieldsToFunctionParameters(context, fields, fieldNames, true);
             if(parameters == null)
                 return;
             for(int i=0;i<parameters.length;++i) {
@@ -108,14 +112,6 @@ public class CHRLiteral extends Symbol {
         }
     }
 
-    public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
-        for(Expression parameter : parameters)
-            parameter.collectRefs(allRefs, refs);
-        if(typeConstraintEvidenceParameters != null)
-            for(Expression parameter : typeConstraintEvidenceParameters)
-                parameter.collectRefs(allRefs, refs);
-    }
-
     public void checkType(TypingContext context) {
         if(relation == SpecialCHRRelation.EXECUTE) {
             if(parameters.length != 1)
@@ -123,6 +119,11 @@ public class CHRLiteral extends Symbol {
             parameters[0] = parameters[0].checkIgnoredType(context);
             typeConstraintEvidenceParameters = Expression.EMPTY_ARRAY;
         }
+        else if(relation == SpecialCHRRelation.ASSIGN) {
+            parameters[1] = parameters[1].inferType(context);
+            parameters[0] = parameters[0].checkTypeAsPattern(context, parameters[1].getType());
+            typeConstraintEvidenceParameters = Expression.EMPTY_ARRAY;
+        }
         else {
             TVar[] typeVariables = relation.getTypeVariables();
             typeParameters = typeVariables.length == 0 ? Type.EMPTY_ARRAY : new Type[typeVariables.length];
@@ -147,22 +148,6 @@ public class CHRLiteral extends Symbol {
                 parameter.collectVars(allVars, vars);
     }
 
-    public void forVariables(VariableProcedure procedure) {
-        for(Expression parameter : parameters)
-            parameter.forVariables(procedure);
-        if(typeConstraintEvidenceParameters != null)
-            for(Expression parameter : typeConstraintEvidenceParameters)
-                parameter.forVariables(procedure);
-    }
-
-    public void collectFreeVariables(THashSet<Variable> vars) {
-        for(Expression parameter : parameters)
-            parameter.collectFreeVariables(vars);
-        if(typeConstraintEvidenceParameters != null)
-            for(Expression parameter : typeConstraintEvidenceParameters)
-                parameter.collectFreeVariables(vars);
-    }
-
     public void setLocationDeep(long loc) {
         if(location == Locations.NO_LOCATION) {
             this.location = loc;
@@ -186,11 +171,14 @@ public class CHRLiteral extends Symbol {
         return b.toString();
     }
 
-    public void collectQueryEffects(THashSet<Type> effects) {
-        // TODO
-    }
-
-    public void collectEnforceEffects(THashSet<Type> effects) {
-        // TODO
+    public CHRLiteral replace(ReplaceContext context) {
+        CHRLiteral copy = new CHRLiteral(location, relation, context.replace(parameters), killAfterMatch, negated);
+        for(int i=0;i<parameters.length;++i)
+            copy.parameters[i] = copy.parameters[i].replace(context);
+        copy.passive = passive;
+        copy.typeConstraintEvidenceParameters = context.replace(typeConstraintEvidenceParameters);
+        copy.typeParameters = context.replace(typeParameters);
+        copy.fields = context.replace(fields);
+        return copy;
     }
 }