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