]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/Builtins.java
Getting rid of *Array and replace with Vector * (SCL)
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / Builtins.java
1 package org.simantics.scl.compiler.elaboration.java;
2
3 import org.cojen.classfile.TypeDesc;
4 import org.simantics.databoard.binding.mutable.Variant;
5 import org.simantics.scl.compiler.common.datatypes.Constructor;
6 import org.simantics.scl.compiler.common.names.Name;
7 import org.simantics.scl.compiler.common.names.Names;
8 import org.simantics.scl.compiler.common.precedence.Associativity;
9 import org.simantics.scl.compiler.common.precedence.Precedence;
10 import org.simantics.scl.compiler.constants.BooleanConstant;
11 import org.simantics.scl.compiler.constants.Constant;
12 import org.simantics.scl.compiler.constants.JavaStaticField;
13 import org.simantics.scl.compiler.constants.JavaStaticMethod;
14 import org.simantics.scl.compiler.constants.NoRepConstant;
15 import org.simantics.scl.compiler.constants.SCLConstant;
16 import org.simantics.scl.compiler.constants.SCLConstructor;
17 import org.simantics.scl.compiler.constants.singletons.BindingConstant;
18 import org.simantics.scl.compiler.constants.singletons.ClassConstant;
19 import org.simantics.scl.compiler.constants.singletons.FailFunction;
20 import org.simantics.scl.compiler.constants.singletons.JustConstant;
21 import org.simantics.scl.compiler.constants.singletons.NothingConstant;
22 import org.simantics.scl.compiler.constants.singletons.ThrowFunction;
23 import org.simantics.scl.compiler.constants.singletons.TypeOfConstant;
24 import org.simantics.scl.compiler.constants.singletons.TypeOfProxyConstant;
25 import org.simantics.scl.compiler.constants.singletons.TypeProxyConstant;
26 import org.simantics.scl.compiler.elaboration.fundeps.Fundep;
27 import org.simantics.scl.compiler.elaboration.modules.Documentation;
28 import org.simantics.scl.compiler.elaboration.modules.PrivateProperty;
29 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
30 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
31 import org.simantics.scl.compiler.errors.Locations;
32 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
33 import org.simantics.scl.compiler.internal.codegen.references.BoundVar;
34 import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;
35 import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;
36 import org.simantics.scl.compiler.internal.codegen.ssa.exits.Jump;
37 import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply;
38 import org.simantics.scl.compiler.internal.codegen.types.MaybeType;
39 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
40 import org.simantics.scl.compiler.internal.codegen.types.VectorType;
41 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
42 import org.simantics.scl.compiler.module.ConcreteModule;
43 import org.simantics.scl.compiler.types.TApply;
44 import org.simantics.scl.compiler.types.TCon;
45 import org.simantics.scl.compiler.types.TFun;
46 import org.simantics.scl.compiler.types.TPred;
47 import org.simantics.scl.compiler.types.TUnion;
48 import org.simantics.scl.compiler.types.TVar;
49 import org.simantics.scl.compiler.types.Type;
50 import org.simantics.scl.compiler.types.Types;
51 import org.simantics.scl.compiler.types.kinds.Kind;
52 import org.simantics.scl.compiler.types.kinds.Kinds;
53 import org.simantics.scl.runtime.chr.CHRContext;
54 import org.simantics.scl.runtime.profiling.BranchPoint;
55
56 public class Builtins extends ConcreteModule {
57
58     public static final SCLValue[] TUPLE_CONSTRUCTORS = new SCLValue[Constants.MAX_TUPLE_LENGTH+1];
59     public static final SCLValue[] LIST_CONSTRUCTORS = new SCLValue[Constants.MAX_LIST_LITERAL_LENGTH+1];
60     
61     public static Builtins INSTANCE = new Builtins();
62     
63     public static SCLValue Nothing;
64     public static SCLValue Just;
65     
66     public static SCLValue EQUALS;
67     
68     private Builtins() {      
69         super(Types.BUILTIN);
70         
71         TVar A = Types.var(Kinds.STAR);
72         
73         StandardTypeConstructor Boolean = new StandardTypeConstructor(Types.BOOLEAN, Kinds.STAR, TypeDesc.BOOLEAN);
74         Boolean.documentation = "Data type representing truth values `True` and `False`.";
75         addTypeDescriptor("Boolean", Boolean);
76         addTypeDescriptor("Byte", new StandardTypeConstructor(Types.BYTE, Kinds.STAR, TypeDesc.BYTE,
77                 "8-bit signed integer"));
78         addTypeDescriptor("Character", new StandardTypeConstructor(Types.CHARACTER, Kinds.STAR, TypeDesc.CHAR,
79                 "16-bit Unicode character."));
80         addTypeDescriptor("Short", new StandardTypeConstructor(Types.SHORT, Kinds.STAR, TypeDesc.SHORT,
81                 "16-bit signed integer"));
82         addTypeDescriptor("Integer", new StandardTypeConstructor(Types.INTEGER, Kinds.STAR, TypeDesc.INT,
83                 "32-bit signed integer"));
84         addTypeDescriptor("Long", new StandardTypeConstructor(Types.LONG, Kinds.STAR, TypeDesc.LONG,
85                 "64-bit signed integer"));
86         addTypeDescriptor("Float", new StandardTypeConstructor(Types.FLOAT, Kinds.STAR, TypeDesc.FLOAT,
87                 "32-bit floating point number"));
88         addTypeDescriptor("Double", new StandardTypeConstructor(Types.DOUBLE, Kinds.STAR, TypeDesc.DOUBLE,
89                 "64-bit floating point number"));
90         addTypeDescriptor("String", new StandardTypeConstructor(Types.STRING, Kinds.STAR, TypeDesc.STRING,
91                 "Unicode string"));
92
93         addTypeDescriptor("Array", new StandardTypeConstructor(Types.con(Types.BUILTIN, "Array"), Kinds.STAR_TO_STAR, TypeDesc.forClass(Object[].class)));
94         
95         addTypeDescriptor("Maybe", MaybeType.INSTANCE);
96         
97         addTypeDescriptor("Variant", new StandardTypeConstructor(Types.VARIANT, Kinds.STAR, TypeDesc.forClass(Variant.class)));
98         
99         addEffectConstructor("Proc", new EffectConstructor(Types.PROC));
100         addEffectConstructor("Exception", new EffectConstructor(Types.EXCEPTION));
101         
102         //addTypeDescriptor("->", new StandardTypeConstructor(Kinds.STAR_TO_STAR_TO_STAR, Constants.FUNCTION));
103         addTypeDescriptor("[]", new StandardTypeConstructor(Types.LIST, Kinds.STAR_TO_STAR, Constants.LIST));        
104         addTypeDescriptor("@", new StandardTypeConstructor(Types.PUNIT, Kinds.STAR, Constants.TUPLE[0]));
105         addTypeDescriptor("TypeProxy", new StandardTypeConstructor(Types.TYPE_PROXY, Kinds.STAR_TO_STAR, Constants.TUPLE[0]));
106         
107         // *** Tuples ***
108         
109         Kind tupleKind = Kinds.STAR;
110         for(int arity=0;arity<=Constants.MAX_TUPLE_LENGTH;++arity) {
111             if(arity != 1) {
112                 TVar[] vars = new TVar[arity];
113                 for(int i=0;i<vars.length;++i)
114                     vars[i] = Types.var(Kinds.STAR);
115                 TCon constructor = Types.tupleConstructor(arity);
116                 StandardTypeConstructor typeConstructor = 
117                         new StandardTypeConstructor(constructor, tupleKind, Constants.TUPLE[arity]);
118                 addTypeDescriptor(constructor.name, typeConstructor);
119                 Type returnType = Types.apply(constructor, vars);
120                 typeConstructor.setType(constructor, vars);
121                 Constant cons;
122                 String javaName = "org/simantics/scl/runtime/tuple/Tuple"+arity;
123                 if(arity == 0) {
124                     cons = new NoRepConstant(returnType);
125                     typeConstructor.setConstructors(new Constructor(Locations.NO_LOCATION, typeConstructor,
126                             Name.create(Types.BUILTIN, constructor.name), 
127                             vars, javaName)
128                             );
129                 }
130                 else { 
131                     cons = new SCLConstructor(constructor.name, 
132                             javaName, vars, 0, returnType, vars);
133                     typeConstructor.setConstructors(
134                             new Constructor(Locations.NO_LOCATION, typeConstructor, 
135                                     Name.create(Types.BUILTIN, constructor.name), 
136                                     vars, javaName)
137                             );
138                 }
139                 typeConstructor.isOpen = false;
140                 SCLValue value = new SCLValue(Name.create(Types.BUILTIN, constructor.name), cons);
141                 addValue(value);
142                 TUPLE_CONSTRUCTORS[arity] = value;
143             }
144             tupleKind = Kinds.arrow(Kinds.STAR, tupleKind);
145         }
146         
147         // *** Lists ***
148         
149         for(int arity=0;arity<=Constants.MAX_LIST_LITERAL_LENGTH;++arity) {
150             SCLValue value = addValue("_list_literal_" + arity + "_",
151                     arity == 0 ? new EmptyListConstructor() : 
152                         new ListConstructor(arity)
153                     );
154             value.addProperty(PrivateProperty.INSTANCE);
155             LIST_CONSTRUCTORS[arity] = value;
156         }
157         
158         // *** Boolean ***
159         
160         SCLValue True = addValue("True", new BooleanConstant(true));
161         SCLValue False = addValue("False", new BooleanConstant(false));
162         Boolean.setConstructors(
163                 new Constructor(Locations.NO_LOCATION, Boolean, False.getName(), Type.EMPTY_ARRAY, null),
164                 new Constructor(Locations.NO_LOCATION, Boolean, True.getName(), Type.EMPTY_ARRAY, null)
165                 );
166         Boolean.isOpen = false;
167         
168         // *** Maybe ***
169         
170         Nothing = addValue("Nothing", NothingConstant.INSTANCE);
171         Just = addValue("Just", JustConstant.INSTANCE);
172         MaybeType.INSTANCE.setConstructors(
173                 new Constructor(Locations.NO_LOCATION, MaybeType.INSTANCE, Nothing.getName(), Type.EMPTY_ARRAY, null),
174                 new Constructor(Locations.NO_LOCATION, MaybeType.INSTANCE, Just.getName(), new Type[] {MaybeType.INSTANCE.parameters[0]}, null)
175                 );
176
177         // *** Dynamic ***
178         
179         addValue("Dynamic", DynamicConstructor.INSTANCE);
180         
181         // *** Vector ***
182         
183         TypeClass VecCompC = new TypeClass(Locations.NO_LOCATION, 
184                 TPred.EMPTY_ARRAY, 
185                 Types.VEC_COMP, 
186                 "java/lang/Class",
187                 new TVar[] {A},
188                 Fundep.EMPTY_ARRAY);
189         addTypeClass("VecComp", VecCompC);
190         
191         addTypeDescriptor("Vector", new VectorType(Types.VECTOR));
192         addValue("getVector", new GetVector(Types.NO_EFFECTS, Types.VECTOR));
193         addValue("lengthVector", new LengthVector(Types.VECTOR));
194         //addValue("createVectorFromList", CreateVectorFromList.INSTANCE);
195         
196         addTypeDescriptor("MVector", new VectorType(Types.MVECTOR));
197         addValue("createMVector", CreateMVector.INSTANCE);
198         addValue("createMVectorProto", CreateMVectorProto.INSTANCE);
199         addValue("getMVector", new GetVector(Types.PROC, Types.MVECTOR));
200         addValue("lengthMVector", new LengthVector(Types.MVECTOR));
201         addValue("freezeMVector", new FreezeMVector());
202         addValue("setMVector", SetMVector.INSTANCE);       
203         
204         StandardTypeConstructor ClassC = new StandardTypeConstructor(Types.CLASS, Kinds.STAR_TO_STAR, 
205                 TypeDesc.forClass("java/lang/Class"));
206         ClassC.setType(Types.CLASS, A);
207         addTypeDescriptor("Class", ClassC);
208         addValue("classObject", ClassConstant.INSTANCE);
209         
210         // *** fail ***
211         
212         addValue("fail", FailFunction.INSTANCE).documentation =
213                 "Throws a runtime exeception with the given string as a description.";
214         addValue("throw", ThrowFunction.INSTANCE).documentation =
215                 "Throws a given exception.";
216                 
217         // *** runProc ***
218         
219         {
220             TVar a = Types.var(Kinds.STAR);
221             TVar e = Types.var(Kinds.EFFECT);
222             SSAFunction runProcFunction = new SSAFunction(new TVar[] {a,e}, e, a);
223             Type parameterType = Types.functionE(Types.PUNIT, Types.union(new Type[] {Types.PROC,e}), a);
224             SSABlock block = new SSABlock(parameterType);
225             BoundVar[] parameters = block.getParameters();
226             
227             BoundVar x = new BoundVar(a);
228             LetApply apply = new LetApply(x, Types.PROC, parameters[0].createOccurrence(), 
229                     new NoRepConstant(Types.PUNIT).createOccurrence()
230                     );
231             block.addStatement(apply);
232             
233             block.setExit(new Jump(runProcFunction.getReturnCont().createOccurrence(), 
234                     x.createOccurrence()));
235             
236             runProcFunction.addBlock(block);
237             SCLConstant runProc = new SCLConstant(Names.Builtin_runProc, runProcFunction.getType());
238             runProc.setDefinition(runProcFunction);            
239             runProc.setInlineArity(1, 0xffffffff);
240             runProc.setBase(new JavaStaticMethod("org/simantics/scl/runtime/procedure/Procedures",
241                     "runProc", a, parameterType));
242             addValue("runProc", runProc);
243         }
244         
245         // *** Typeable ***
246         
247         {
248             /* class Typeable a 
249              */
250             TypeClass TypeableC = new TypeClass(Locations.NO_LOCATION, 
251                     TPred.EMPTY_ARRAY, 
252                     Types.TYPEABLE, 
253                     Type.class.getName(),
254                     new TVar[] {A},
255                     Fundep.EMPTY_ARRAY);
256             TypeableC.documentation = "A class of types that can be reified with `typeOf` function.";
257             addTypeClass("Typeable", TypeableC);
258         
259             /* data Type = TCon String String
260              *           | TApply Type Type
261              */
262             final TCon Type = Types.con(Types.BUILTIN, "Type");
263             final TypeDesc TypeD = TypeDesc.forClass(Type.class);
264             addValue("TCon", new JavaStaticMethod(
265                     Types.class.getName().replace('.', '/'),
266                     "con",
267                     TypeDesc.forClass(TCon.class), new TypeDesc[] {TypeDesc.STRING, TypeDesc.STRING},
268                     Type, Types.STRING, Types.STRING)); 
269             addValue("TApply", new JavaStaticMethod(
270                     "org/simantics/scl/compiler/types/Types",
271                     "apply",
272                     TypeDesc.forClass(TApply.class), new TypeDesc[] {TypeD, TypeD},
273                     Type, Type, Type));
274             addValue("TFun", new JavaStaticMethod(
275                     "org/simantics/scl/compiler/types/Types",
276                     "functionE",
277                     TypeDesc.forClass(TFun.class), new TypeDesc[] {TypeD, TypeD, TypeD},
278                     Type, Type, Type, Type));
279             addValue("TPure", new JavaStaticField(
280                     "org/simantics/scl/compiler/types/Types",
281                     "NO_EFFECTS",
282                     Types.NO_EFFECTS,
283                     TypeDesc.forClass(TUnion.class),
284                     Type, -1));
285             addValue("TUnion2", new JavaStaticMethod(
286                     "org/simantics/scl/compiler/types/Types",
287                     "union",
288                     Types.NO_EFFECTS,
289                     Type, Type, Type));
290             addValue("TUnion3", new JavaStaticMethod(
291                     "org/simantics/scl/compiler/types/Types",
292                     "union",
293                     Types.NO_EFFECTS,
294                     Type, Type, Type, Type));
295             
296             StandardTypeConstructor TypeC = new StandardTypeConstructor(Type, Kinds.STAR, 
297                     TypeDesc.forClass("org/simantics/scl/compiler/types/Type"));
298             TypeC.setType(Type);
299             TypeC.isOpen = true;
300             TypeC.documentation = "Represents an SCL data type.";
301             addTypeDescriptor("Type", TypeC);
302
303             // typeOf :: Typeable a => a -> Type
304             addValue("typeOf", TypeOfConstant.INSTANCE)
305             .documentation = "Returns the type of the value given as a parameter.";
306             addValue("typeOfProxy", TypeOfProxyConstant.INSTANCE)
307             .documentation = "Returns the type of the type proxy given as a parameter.";
308             addValue("TypeProxy", TypeProxyConstant.INSTANCE);
309         }
310         
311         // *** Serializable ***
312         
313         {
314             /* class Serializable a 
315              */
316             TypeClass SerializableC = new TypeClass(Locations.NO_LOCATION, 
317                     TPred.EMPTY_ARRAY, 
318                     Types.SERIALIZABLE, 
319                     "org/simantics/databoard/binding/Binding",
320                     new TVar[] {A},
321                     Fundep.EMPTY_ARRAY);
322             SerializableC.documentation = "A class of types having a `Binding`";
323             addTypeClass("Serializable", SerializableC);
324         
325             /* data TypeRep = TCon String
326              *              | TApply TypeRep TypeRep
327              */           
328
329             StandardTypeConstructor BindingC = new StandardTypeConstructor(Types.BINDING, Kinds.STAR_TO_STAR, 
330                     TypeDesc.forClass("org/simantics/databoard/binding/Binding"));
331             BindingC.setType(Types.BINDING, A);
332             BindingC.documentation = "`Binding` represents a data type in the form supported by Databoard library. " + 
333                     "It is used to serialize and deserialize values.";
334             addTypeDescriptor("Binding", BindingC);
335             
336             // typeOf :: Typeable a => a -> TypeReps
337             addValue("binding", BindingConstant.INSTANCE)
338             .documentation = "Gives a binding for the required type.";
339             
340         }
341         
342         // Relations
343         
344         {
345             addRelation("Eq", EqRelation.INSTANCE);
346             addRelation("Optional", OptionalRelation.INSTANCE);
347             addRelation("Execute", new ExecuteRelation(0));
348             addRelation("Execute10", new ExecuteRelation(10));
349         }
350         
351         {
352             EQUALS = new SCLValue(Names.Builtin_equals, EqualsFunction.INSTANCE);
353             EQUALS.setPrecedence(new Precedence(4, Associativity.NONASSOC));
354             addValue(EQUALS);
355             addValue("hashCode", HashCodeFunction.INSTANCE);
356         }
357         
358         // Coverage
359         {
360             StandardTypeConstructor branchPoint = new StandardTypeConstructor(Types.BRANCH_POINT, Kinds.STAR,
361                     TypeDesc.forClass(BranchPoint.class)); 
362             addTypeDescriptor("BranchPoint", branchPoint);
363             
364             addValue("visitBranchPoint", VisitBranchPoint.INSTANCE);
365         }
366         
367         setParentClassLoader(getClass().getClassLoader());
368         
369         // CHR
370         
371         addTypeDescriptor("CHRContext", new StandardTypeConstructor(Types.CHRContext, Kinds.STAR, TypeDesc.forClass(CHRContext.class)));
372     }
373     
374     @Override
375     public Documentation getDocumentation() {
376         return documentation;
377     }
378
379         public static void flush() {
380                 INSTANCE = new Builtins();
381         }
382
383 }