]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/Builtins.java
Builtins and JavaModule SCL modules may leak memory
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / Builtins.java
old mode 100755 (executable)
new mode 100644 (file)
index 0dee9f9..4d48d1e
@@ -4,6 +4,9 @@ import org.cojen.classfile.TypeDesc;
 import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.scl.compiler.common.datatypes.Constructor;
 import org.simantics.scl.compiler.common.names.Name;
+import org.simantics.scl.compiler.common.names.Names;
+import org.simantics.scl.compiler.common.precedence.Associativity;
+import org.simantics.scl.compiler.common.precedence.Precedence;
 import org.simantics.scl.compiler.constants.BooleanConstant;
 import org.simantics.scl.compiler.constants.Constant;
 import org.simantics.scl.compiler.constants.JavaStaticField;
@@ -44,18 +47,21 @@ import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
 import org.simantics.scl.compiler.types.kinds.Kind;
 import org.simantics.scl.compiler.types.kinds.Kinds;
+import org.simantics.scl.runtime.chr.CHRContext;
 import org.simantics.scl.runtime.profiling.BranchPoint;
 
 public class Builtins extends ConcreteModule {
 
-    public static SCLValue[] TUPLE_CONSTRUCTORS = new SCLValue[Constants.MAX_TUPLE_LENGTH+1];
-    public static SCLValue[] LIST_CONSTRUCTORS = new SCLValue[Constants.MAX_LIST_LITERAL_LENGTH+1];
+    public static final SCLValue[] TUPLE_CONSTRUCTORS = new SCLValue[Constants.MAX_TUPLE_LENGTH+1];
+    public static final SCLValue[] LIST_CONSTRUCTORS = new SCLValue[Constants.MAX_LIST_LITERAL_LENGTH+1];
     
-    public static final Builtins INSTANCE = new Builtins();       
+    public static Builtins INSTANCE = new Builtins();
     
     public static SCLValue Nothing;
     public static SCLValue Just;
     
+    public static SCLValue EQUALS;
+    
     private Builtins() {      
         super(Types.BUILTIN);
         
@@ -63,45 +69,45 @@ public class Builtins extends ConcreteModule {
         
         StandardTypeConstructor Boolean = new StandardTypeConstructor(Types.BOOLEAN, Kinds.STAR, TypeDesc.BOOLEAN);
         Boolean.documentation = "Data type representing truth values `True` and `False`.";
-        addTypeConstructor("Boolean", Boolean);
-        addTypeConstructor("Byte", new StandardTypeConstructor(Types.BYTE, Kinds.STAR, TypeDesc.BYTE,
+        addTypeDescriptor("Boolean", Boolean);
+        addTypeDescriptor("Byte", new StandardTypeConstructor(Types.BYTE, Kinds.STAR, TypeDesc.BYTE,
                 "8-bit signed integer"));
-        addTypeConstructor("Character", new StandardTypeConstructor(Types.CHARACTER, Kinds.STAR, TypeDesc.CHAR,
+        addTypeDescriptor("Character", new StandardTypeConstructor(Types.CHARACTER, Kinds.STAR, TypeDesc.CHAR,
                 "16-bit Unicode character."));
-        addTypeConstructor("Short", new StandardTypeConstructor(Types.SHORT, Kinds.STAR, TypeDesc.SHORT,
+        addTypeDescriptor("Short", new StandardTypeConstructor(Types.SHORT, Kinds.STAR, TypeDesc.SHORT,
                 "16-bit signed integer"));
-        addTypeConstructor("Integer", new StandardTypeConstructor(Types.INTEGER, Kinds.STAR, TypeDesc.INT,
+        addTypeDescriptor("Integer", new StandardTypeConstructor(Types.INTEGER, Kinds.STAR, TypeDesc.INT,
                 "32-bit signed integer"));
-        addTypeConstructor("Long", new StandardTypeConstructor(Types.LONG, Kinds.STAR, TypeDesc.LONG,
+        addTypeDescriptor("Long", new StandardTypeConstructor(Types.LONG, Kinds.STAR, TypeDesc.LONG,
                 "64-bit signed integer"));
-        addTypeConstructor("Float", new StandardTypeConstructor(Types.FLOAT, Kinds.STAR, TypeDesc.FLOAT,
+        addTypeDescriptor("Float", new StandardTypeConstructor(Types.FLOAT, Kinds.STAR, TypeDesc.FLOAT,
                 "32-bit floating point number"));
-        addTypeConstructor("Double", new StandardTypeConstructor(Types.DOUBLE, Kinds.STAR, TypeDesc.DOUBLE,
+        addTypeDescriptor("Double", new StandardTypeConstructor(Types.DOUBLE, Kinds.STAR, TypeDesc.DOUBLE,
                 "64-bit floating point number"));
-        addTypeConstructor("String", new StandardTypeConstructor(Types.STRING, Kinds.STAR, TypeDesc.STRING,
+        addTypeDescriptor("String", new StandardTypeConstructor(Types.STRING, Kinds.STAR, TypeDesc.STRING,
                 "Unicode string"));
         
-        addTypeConstructor("BooleanArray", new StandardTypeConstructor(Types.BOOLEAN_ARRAY, Kinds.STAR, TypeDesc.forClass(boolean[].class)));
-        addTypeConstructor("ByteArray", new StandardTypeConstructor(Types.BYTE_ARRAY, Kinds.STAR, TypeDesc.forClass(byte[].class)));
-        addTypeConstructor("CharacterArray", new StandardTypeConstructor(Types.CHARACTER_ARRAY, Kinds.STAR, TypeDesc.forClass(char[].class)));
-        addTypeConstructor("ShortArray", new StandardTypeConstructor(Types.SHORT_ARRAY, Kinds.STAR, TypeDesc.forClass(short[].class)));
-        addTypeConstructor("IntegerArray", new StandardTypeConstructor(Types.INTEGER_ARRAY, Kinds.STAR, TypeDesc.forClass(int[].class)));
-        addTypeConstructor("LongArray", new StandardTypeConstructor(Types.LONG_ARRAY, Kinds.STAR, TypeDesc.forClass(long[].class)));
-        addTypeConstructor("FloatArray", new StandardTypeConstructor(Types.FLOAT_ARRAY, Kinds.STAR, TypeDesc.forClass(float[].class)));
-        addTypeConstructor("DoubleArray", new StandardTypeConstructor(Types.DOUBLE_ARRAY, Kinds.STAR, TypeDesc.forClass(double[].class)));
+        addTypeDescriptor("BooleanArray", new StandardTypeConstructor(Types.BOOLEAN_ARRAY, Kinds.STAR, TypeDesc.forClass(boolean[].class)));
+        addTypeDescriptor("ByteArray", new StandardTypeConstructor(Types.BYTE_ARRAY, Kinds.STAR, TypeDesc.forClass(byte[].class)));
+        addTypeDescriptor("CharacterArray", new StandardTypeConstructor(Types.CHARACTER_ARRAY, Kinds.STAR, TypeDesc.forClass(char[].class)));
+        addTypeDescriptor("ShortArray", new StandardTypeConstructor(Types.SHORT_ARRAY, Kinds.STAR, TypeDesc.forClass(short[].class)));
+        addTypeDescriptor("IntegerArray", new StandardTypeConstructor(Types.INTEGER_ARRAY, Kinds.STAR, TypeDesc.forClass(int[].class)));
+        addTypeDescriptor("LongArray", new StandardTypeConstructor(Types.LONG_ARRAY, Kinds.STAR, TypeDesc.forClass(long[].class)));
+        addTypeDescriptor("FloatArray", new StandardTypeConstructor(Types.FLOAT_ARRAY, Kinds.STAR, TypeDesc.forClass(float[].class)));
+        addTypeDescriptor("DoubleArray", new StandardTypeConstructor(Types.DOUBLE_ARRAY, Kinds.STAR, TypeDesc.forClass(double[].class)));
         
-        addTypeConstructor("Array", new StandardTypeConstructor(Types.con(Types.BUILTIN, "Array"), Kinds.STAR_TO_STAR, TypeDesc.forClass(Object[].class)));
+        addTypeDescriptor("Array", new StandardTypeConstructor(Types.con(Types.BUILTIN, "Array"), Kinds.STAR_TO_STAR, TypeDesc.forClass(Object[].class)));
         
-        addTypeConstructor("Maybe", MaybeType.INSTANCE);
+        addTypeDescriptor("Maybe", MaybeType.INSTANCE);
         
-        addTypeConstructor("Variant", new StandardTypeConstructor(Types.VARIANT, Kinds.STAR, TypeDesc.forClass(Variant.class)));
+        addTypeDescriptor("Variant", new StandardTypeConstructor(Types.VARIANT, Kinds.STAR, TypeDesc.forClass(Variant.class)));
         
         addEffectConstructor("Proc", new EffectConstructor(Types.PROC));
         
-        //addTypeConstructor("->", new StandardTypeConstructor(Kinds.STAR_TO_STAR_TO_STAR, Constants.FUNCTION));
-        addTypeConstructor("[]", new StandardTypeConstructor(Types.LIST, Kinds.STAR_TO_STAR, Constants.LIST));        
-        addTypeConstructor("@", new StandardTypeConstructor(Types.PUNIT, Kinds.STAR, Constants.TUPLE[0]));
-        addTypeConstructor("TypeProxy", new StandardTypeConstructor(Types.TYPE_PROXY, Kinds.STAR_TO_STAR, Constants.TUPLE[0]));
+        //addTypeDescriptor("->", new StandardTypeConstructor(Kinds.STAR_TO_STAR_TO_STAR, Constants.FUNCTION));
+        addTypeDescriptor("[]", new StandardTypeConstructor(Types.LIST, Kinds.STAR_TO_STAR, Constants.LIST));        
+        addTypeDescriptor("@", new StandardTypeConstructor(Types.PUNIT, Kinds.STAR, Constants.TUPLE[0]));
+        addTypeDescriptor("TypeProxy", new StandardTypeConstructor(Types.TYPE_PROXY, Kinds.STAR_TO_STAR, Constants.TUPLE[0]));
         
         // *** Tuples ***
         
@@ -114,7 +120,7 @@ public class Builtins extends ConcreteModule {
                 TCon constructor = Types.tupleConstructor(arity);
                 StandardTypeConstructor typeConstructor = 
                         new StandardTypeConstructor(constructor, tupleKind, Constants.TUPLE[arity]);
-                addTypeConstructor(constructor.name, typeConstructor);
+                addTypeDescriptor(constructor.name, typeConstructor);
                 Type returnType = Types.apply(constructor, vars);
                 typeConstructor.setType(constructor, vars);
                 Constant cons;
@@ -181,12 +187,12 @@ public class Builtins extends ConcreteModule {
                 Fundep.EMPTY_ARRAY);
         addTypeClass("VecComp", VecCompC);
         
-        addTypeConstructor("Vector", new VectorType(Types.VECTOR));
+        addTypeDescriptor("Vector", new VectorType(Types.VECTOR));
         addValue("getVector", new GetVector(Types.NO_EFFECTS, Types.VECTOR));
         addValue("lengthVector", new LengthVector(Types.VECTOR));
         //addValue("createVectorFromList", CreateVectorFromList.INSTANCE);
         
-        addTypeConstructor("MVector", new VectorType(Types.MVECTOR));
+        addTypeDescriptor("MVector", new VectorType(Types.MVECTOR));
         addValue("createMVector", CreateMVector.INSTANCE);
         addValue("createMVectorProto", CreateMVectorProto.INSTANCE);
         addValue("getMVector", new GetVector(Types.PROC, Types.MVECTOR));
@@ -219,7 +225,7 @@ public class Builtins extends ConcreteModule {
                     x.createOccurrence()));
             
             runProcFunction.addBlock(block);
-            SCLConstant runProc = new SCLConstant(Name.create(Types.BUILTIN, "runProc"), runProcFunction.getType());
+            SCLConstant runProc = new SCLConstant(Names.Builtin_runProc, runProcFunction.getType());
             runProc.setDefinition(runProcFunction);            
             runProc.setInlineArity(1, 0xffffffff);
             runProc.setBase(new JavaStaticMethod("org/simantics/scl/runtime/procedure/Procedures",
@@ -267,18 +273,18 @@ public class Builtins extends ConcreteModule {
                     Types.NO_EFFECTS,
                     TypeDesc.forClass(TUnion.class),
                     Type, -1));
-            /*addValue("TUnion", new JavaStaticMethod(
+            addValue("TUnion2", new JavaStaticMethod(
                     "org/simantics/scl/compiler/types/Types",
                     "union",
                     Types.NO_EFFECTS,
-                    Type, Types.list(Type)));*/
+                    Type, Type, Type));
 
             StandardTypeConstructor TypeC = new StandardTypeConstructor(Type, Kinds.STAR, 
                     TypeDesc.forClass("org/simantics/scl/compiler/types/Type"));
             TypeC.setType(Type);
             TypeC.isOpen = true;
             TypeC.documentation = "Represents an SCL data type.";
-            addTypeConstructor("Type", TypeC);
+            addTypeDescriptor("Type", TypeC);
 
             // typeOf :: Typeable a => a -> Type
             addValue("typeOf", TypeOfConstant.INSTANCE)
@@ -311,7 +317,7 @@ public class Builtins extends ConcreteModule {
             BindingC.setType(Types.BINDING, A);
             BindingC.documentation = "`Binding` represents a data type in the form supported by Databoard library. " + 
                     "It is used to serialize and deserialize values.";
-            addTypeConstructor("Binding", BindingC);
+            addTypeDescriptor("Binding", BindingC);
             
             // typeOf :: Typeable a => a -> TypeReps
             addValue("binding", BindingConstant.INSTANCE)
@@ -328,16 +334,27 @@ public class Builtins extends ConcreteModule {
             addRelation("Execute10", new ExecuteRelation(10));
         }
         
-        addValue("newEq", EqualsFunction.INSTANCE);
-        addValue("newHash", HashCodeFunction.INSTANCE);
+        {
+            EQUALS = new SCLValue(Names.Builtin_equals, EqualsFunction.INSTANCE);
+            EQUALS.setPrecedence(new Precedence(4, Associativity.NONASSOC));
+            addValue(EQUALS);
+            addValue("hashCode", HashCodeFunction.INSTANCE);
+        }
+        
         // Coverage
         {
             StandardTypeConstructor branchPoint = new StandardTypeConstructor(Types.BRANCH_POINT, Kinds.STAR,
                     TypeDesc.forClass(BranchPoint.class)); 
-            addTypeConstructor("BranchPoint", branchPoint);
+            addTypeDescriptor("BranchPoint", branchPoint);
             
             addValue("visitBranchPoint", VisitBranchPoint.INSTANCE);
         }
+        
+        setParentClassLoader(getClass().getClassLoader());
+        
+        // CHR
+        
+        addTypeDescriptor("CHRContext", new StandardTypeConstructor(Types.CHRContext, Kinds.STAR, TypeDesc.forClass(CHRContext.class)));
     }
     
     @Override
@@ -345,4 +362,8 @@ public class Builtins extends ConcreteModule {
         return documentation;
     }
 
+       public static void flush() {
+               INSTANCE = new Builtins();
+       }
+
 }