]> gerrit.simantics Code Review - simantics/platform.git/blob
6c22babec4264fcd5b8cd290e65eae96c2fce294
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.java;
2
3 import org.simantics.scl.compiler.common.names.Name;
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 MemberRelation extends AbstractRelation {
16     public static final MemberRelation INSTANCE = new MemberRelation();
17     
18     private MemberRelation() {}
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.list(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 10.0;
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.iterateList(parameters[0], new EVariable(parameters[1]));
64             break;
65         case BB: 
66             context.condition(
67                     new EApply(
68                             context.getConstant(Name.create("Prelude", "elem"), typeParameters),
69                             new Expression[] {
70                                 context.getEvidence(location, Types.pred(Types.EQ, typeParameters[0])),
71                                 new EVariable(parameters[0]),
72                                 new EVariable(parameters[1])
73                             }
74                             ));
75             break;
76         default: throw new IllegalArgumentException();
77         }
78     }
79
80     @Override
81     public String toString() {
82         return "<-";
83     }
84 }