X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fjava%2FBuiltins.java;h=4846f75a13b2b605297807d11fe5c60a9fd800be;hb=refs%2Fchanges%2F34%2F1534%2F3;hp=0dee9f9cefedd95aced7c457902a232c49dcae4b;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/Builtins.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/Builtins.java old mode 100755 new mode 100644 index 0dee9f9ce..4846f75a1 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/Builtins.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/Builtins.java @@ -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; @@ -12,14 +15,18 @@ import org.simantics.scl.compiler.constants.NoRepConstant; import org.simantics.scl.compiler.constants.SCLConstant; import org.simantics.scl.compiler.constants.SCLConstructor; import org.simantics.scl.compiler.constants.singletons.BindingConstant; +import org.simantics.scl.compiler.constants.singletons.ClassConstant; import org.simantics.scl.compiler.constants.singletons.FailFunction; import org.simantics.scl.compiler.constants.singletons.JustConstant; import org.simantics.scl.compiler.constants.singletons.NothingConstant; +import org.simantics.scl.compiler.constants.singletons.ThrowFunction; import org.simantics.scl.compiler.constants.singletons.TypeOfConstant; import org.simantics.scl.compiler.constants.singletons.TypeOfProxyConstant; import org.simantics.scl.compiler.constants.singletons.TypeProxyConstant; +import org.simantics.scl.compiler.constants.singletons.TypeValueConstant; import org.simantics.scl.compiler.elaboration.fundeps.Fundep; import org.simantics.scl.compiler.elaboration.modules.Documentation; +import org.simantics.scl.compiler.elaboration.modules.PrivateProperty; import org.simantics.scl.compiler.elaboration.modules.SCLValue; import org.simantics.scl.compiler.elaboration.modules.TypeClass; import org.simantics.scl.compiler.errors.Locations; @@ -44,18 +51,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 +73,37 @@ 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")); + + addTypeDescriptor("Array", new StandardTypeConstructor(Types.con(Types.BUILTIN, "Array"), Kinds.STAR_TO_STAR, TypeDesc.forClass(Object[].class))); - 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))); - - addTypeConstructor("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)); + addEffectConstructor("Exception", new EffectConstructor(Types.EXCEPTION)); - //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 +116,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; @@ -146,10 +148,12 @@ public class Builtins extends ConcreteModule { // *** Lists *** for(int arity=0;arity<=Constants.MAX_LIST_LITERAL_LENGTH;++arity) { - LIST_CONSTRUCTORS[arity] = addValue("_list_literal_" + arity + "_", + SCLValue value = addValue("_list_literal_" + arity + "_", arity == 0 ? new EmptyListConstructor() : new ListConstructor(arity) ); + value.addProperty(PrivateProperty.INSTANCE); + LIST_CONSTRUCTORS[arity] = value; } // *** Boolean *** @@ -171,6 +175,10 @@ public class Builtins extends ConcreteModule { new Constructor(Locations.NO_LOCATION, MaybeType.INSTANCE, Just.getName(), new Type[] {MaybeType.INSTANCE.parameters[0]}, null) ); + // *** Dynamic *** + + addValue("Dynamic", DynamicConstructor.INSTANCE); + // *** Vector *** TypeClass VecCompC = new TypeClass(Locations.NO_LOCATION, @@ -181,12 +189,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)); @@ -194,10 +202,18 @@ public class Builtins extends ConcreteModule { addValue("freezeMVector", new FreezeMVector()); addValue("setMVector", SetMVector.INSTANCE); + StandardTypeConstructor ClassC = new StandardTypeConstructor(Types.CLASS, Kinds.STAR_TO_STAR, + TypeDesc.forClass("java/lang/Class")); + ClassC.setType(Types.CLASS, A); + addTypeDescriptor("Class", ClassC); + addValue("classObject", ClassConstant.INSTANCE); + // *** fail *** addValue("fail", FailFunction.INSTANCE).documentation = "Throws a runtime exeception with the given string as a description."; + addValue("throw", ThrowFunction.INSTANCE).documentation = + "Throws a given exception."; // *** runProc *** @@ -215,11 +231,11 @@ public class Builtins extends ConcreteModule { ); block.addStatement(apply); - block.setExit(new Jump(runProcFunction.getReturnCont().createOccurrence(), + block.setExit(new Jump(-1, runProcFunction.getReturnCont().createOccurrence(), 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,22 +283,28 @@ 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)); + addValue("TUnion3", new JavaStaticMethod( + "org/simantics/scl/compiler/types/Types", + "union", + Types.NO_EFFECTS, + 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) .documentation = "Returns the type of the value given as a parameter."; + addValue("typeValue", TypeValueConstant.INSTANCE); addValue("typeOfProxy", TypeOfProxyConstant.INSTANCE) .documentation = "Returns the type of the type proxy given as a parameter."; addValue("TypeProxy", TypeProxyConstant.INSTANCE); @@ -311,7 +333,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 +350,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 +378,8 @@ public class Builtins extends ConcreteModule { return documentation; } + public static void flush() { + INSTANCE = new Builtins(); + } + }