]> gerrit.simantics Code Review - simantics/platform.git/blob
ef30deb435ca36318350ccfb8c5fdceb06577f60
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.java;
2
3 import org.simantics.scl.compiler.common.names.Names;
4 import org.simantics.scl.compiler.elaboration.expressions.EApply;
5 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
6 import org.simantics.scl.compiler.elaboration.expressions.Expression;
7 import org.simantics.scl.compiler.elaboration.expressions.Variable;
8 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
9 import org.simantics.scl.compiler.elaboration.relations.AbstractRelation;
10 import org.simantics.scl.compiler.types.TVar;
11 import org.simantics.scl.compiler.types.Type;
12 import org.simantics.scl.compiler.types.Types;
13 import org.simantics.scl.compiler.types.kinds.Kinds;
14
15 public class OptionalRelation extends AbstractRelation {
16     public static final OptionalRelation INSTANCE = new OptionalRelation();
17     
18     private OptionalRelation() {}
19     
20     private static final TVar A = Types.var(Kinds.STAR);
21     
22     private static final TVar[] TYPE_VARIABLES = new TVar[] {A};
23     
24     @Override
25     public TVar[] getTypeVariables() {
26         return TYPE_VARIABLES;
27     }
28     
29     private static final Type[] PARAMETER_TYPES = new Type[] {A, Types.apply(Types.MAYBE, A)};
30     
31     @Override
32     public Type[] getParameterTypes() {
33         return PARAMETER_TYPES;
34     }
35
36     @Override
37     public int getPhase() {
38         return 0;
39     }
40     
41     @Override
42     public double getSelectivity(int boundVariables) {
43         switch(boundVariables) {
44         case FF: 
45         case BF: return Double.POSITIVE_INFINITY;
46         case FB: return 0.95;
47         case BB: return 0.5;
48         default: throw new IllegalArgumentException();
49         }
50     }
51     
52     @Override
53     public int getRequiredVariablesMask() {
54         return FB;
55     }
56
57     @Override
58     public void generate(long location,
59             QueryCompilationContext context,
60             Type[] typeParameters, Variable[] parameters, int boundVariables) {
61         switch(boundVariables) {
62         case FB:
63             context.iterateMaybe(parameters[0], new EVariable(parameters[1]));
64             break;
65         case BB: 
66             context.condition(
67                     new EApply(
68                             context.getConstant(Names.Prelude_elemMaybe, typeParameters),
69                             new Expression[] {
70                                 new EVariable(parameters[0]),
71                                 new EVariable(parameters[1])
72                             }
73                             ));
74             break;
75         default: throw new IllegalArgumentException();
76         }
77     }
78
79     @Override
80     public String toString() {
81         return "Optional";
82     }
83 }