]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/chr/relations/CHRConstraint.java
Support for () type in CHR relations
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / chr / relations / CHRConstraint.java
index 7a0e27ba24b75735c2cc64a833a86d0106dd028e..4a75a560414ad7c6ce69eea33d377a9738a84cd7 100644 (file)
@@ -6,9 +6,10 @@ import java.util.Collection;
 import org.cojen.classfile.TypeDesc;
 import org.simantics.scl.compiler.compilation.CompilationContext;
 import org.simantics.scl.compiler.constants.Constant;
-import org.simantics.scl.compiler.constants.JavaConstructor;
 import org.simantics.scl.compiler.constants.JavaMethod;
+import org.simantics.scl.compiler.constants.NoRepConstant;
 import org.simantics.scl.compiler.constants.generic.CallJava;
+import org.simantics.scl.compiler.constants.generic.MethodRef.ConstructorRef;
 import org.simantics.scl.compiler.constants.generic.MethodRef.FieldRef;
 import org.simantics.scl.compiler.constants.generic.MethodRef.ObjectMethodRef;
 import org.simantics.scl.compiler.constants.generic.ParameterStackItem;
@@ -20,6 +21,7 @@ import org.simantics.scl.compiler.internal.codegen.chr.CHRCodeGenerator;
 import org.simantics.scl.compiler.internal.codegen.references.IVal;
 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
+import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
 import org.simantics.scl.compiler.internal.parsing.Symbol;
 import org.simantics.scl.compiler.types.TCon;
 import org.simantics.scl.compiler.types.TPred;
@@ -91,15 +93,30 @@ public class CHRConstraint extends Symbol implements CHRRelation {
         
         Type[] constructorTypes = new Type[parameterTypes.length+1];
         constructorTypes[0] = Types.INTEGER;
-        for(int i=0;i<parameterTypes.length;++i)
-            constructorTypes[i+1] = parameterTypes[i];
-        this.constructor = new JavaConstructor(factClassName, Types.PROC, factType, constructorTypes);
+        ArrayList<StackItem> stackItems = new ArrayList<StackItem>(constructorTypes.length);
+        stackItems.add(new ParameterStackItem(0, Types.INTEGER));
+        for(int i=0;i<parameterTypes.length;++i) {
+            Type parameterType = parameterTypes[i];
+            constructorTypes[i+1] = parameterType;
+            if(!parameterType.equals(Types.UNIT))
+                stackItems.add(new ParameterStackItem(stackItems.size(), parameterType));
+        }
+        TypeDesc[] constructorTypeDescs = JavaTypeTranslator.filterVoid(jtt.toTypeDescs(constructorTypes));
+        this.constructor = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, factType, constructorTypes,
+                stackItems.toArray(new StackItem[stackItems.size()]),
+                new ConstructorRef(factClassName, constructorTypeDescs),
+                null);
+        //this.constructor = new JavaConstructor(factClassName, Types.PROC, factType, constructorTypes);
         this.accessId = new CallJava(TVar.EMPTY_ARRAY, Types.NO_EFFECTS, Types.INTEGER, new Type[] {factType},
                 null, new FieldRef(factClassName, "id", CHRCodeGenerator.FACT_ID_TYPE), null);
         this.accessors = new Constant[parameterTypes.length];
-        for(int i=0;i<parameterTypes.length;++i)
+        for(int i=0;i<parameterTypes.length;++i) {
+            TypeDesc typeDesc = jtt.toTypeDesc(parameterTypes[i]);
+            if(typeDesc.equals(TypeDesc.VOID))
+                continue;
             this.accessors[i] = new CallJava(TVar.EMPTY_ARRAY, Types.NO_EFFECTS, parameterTypes[i], new Type[] {factType},
-                    null, new FieldRef(factClassName, "c" + i, jtt.toTypeDesc(parameterTypes[i])), null);
+                    null, new FieldRef(factClassName, CHRCodeGenerator.fieldName(i), jtt.toTypeDesc(parameterTypes[i])), null);
+        }
         this.addProcedure = new CallJava(TVar.EMPTY_ARRAY, Types.PROC, Types.UNIT, new Type[] {parentRuleset.storeType, factType},
                 new StackItem[] {new ParameterStackItem(1, factType), new ParameterStackItem(0, parentRuleset.storeType)},
                 new ObjectMethodRef(false, factClassName, "add", TypeDesc.VOID, new TypeDesc[] {parentRuleset.storeTypeDesc}),
@@ -158,10 +175,6 @@ public class CHRConstraint extends Symbol implements CHRRelation {
                         null, new FieldRef(factClassName, indexName + "Next", factTypeDesc), null)
                 );
     }
-
-    public Constant accessComponent(int i) {
-        return accessors[i];
-    }
     
     public IVal fetchFromIndex(CompilationContext context, int boundMask) {
         IndexInfo indexInfo = indices.get(boundMask);
@@ -206,9 +219,16 @@ public class CHRConstraint extends Symbol implements CHRRelation {
     public boolean isPassive() {
         return plans.isEmpty();
     }
-    
+
     public TPred[] getTypeConstraints() {
         return TPred.EMPTY_ARRAY;
     }
 
+    public IVal accessComponent(long location, CodeWriter w, IVal fact, int i) {
+        Constant accessor = accessors[i];
+        if(accessor == null)
+            return NoRepConstant.UNIT;
+        else
+            return w.apply(location, accessor, fact);
+    }
 }