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