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