473c403da7aa85e54db07258b2d1a888efc10cf3
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ESelect.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.Just;
4 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.apply;
5 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.let;
6 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.loc;
7 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.newVar;
8 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.seq;
9 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.tuple;
10 import static org.simantics.scl.compiler.elaboration.expressions.Expressions.var;
11
12 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
13 import org.simantics.scl.compiler.common.names.Names;
14 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
15 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
16 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
17 import org.simantics.scl.compiler.elaboration.query.QExists;
18 import org.simantics.scl.compiler.elaboration.query.Query;
19 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
20 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationMode;
21 import org.simantics.scl.compiler.elaboration.query.compilation.UnsolvableQueryException;
22 import org.simantics.scl.compiler.errors.Locations;
23 import org.simantics.scl.compiler.internal.parsing.parser.SCLTerminals;
24 import org.simantics.scl.compiler.types.Type;
25 import org.simantics.scl.compiler.types.Types;
26 import org.simantics.scl.compiler.types.exceptions.MatchException;
27 import org.simantics.scl.compiler.types.kinds.Kinds;
28
29 import gnu.trove.map.hash.TObjectIntHashMap;
30 import gnu.trove.set.hash.THashSet;
31 import gnu.trove.set.hash.TIntHashSet;
32
33 public class ESelect extends SimplifiableExpression {
34
35     private final Type ARRAY_LIST = Types.con("ArrayList", "T"); 
36     
37     int selectVariant;
38     Expression expression;
39     Query query;
40     Variable[] variables;
41     
42     public ESelect(int selectVariant, Expression expression, Query query) {
43         this.selectVariant = selectVariant;
44         this.expression = expression;
45         this.query = query;
46     }
47
48     @Override
49     public void collectVars(TObjectIntHashMap<Variable> allVars,
50             TIntHashSet vars) {
51         expression.collectVars(allVars, vars);
52         query.collectVars(allVars, vars);
53     }
54
55     @Override
56     protected void updateType() throws MatchException {
57         setType(selectVariant==SCLTerminals.SELECT_FIRST 
58                 ? Types.apply(Types.MAYBE, expression.getType()) 
59                 : Types.list(expression.getType()));
60     }
61     
62     @Override
63     public Expression checkBasicType(TypingContext context, Type requiredType) {
64         Type componentType;
65         switch(selectVariant) {
66         case SCLTerminals.SELECT:
67         case SCLTerminals.SELECT_DISTINCT:
68             try {
69                 componentType = Types.unifyApply(Types.LIST, requiredType);
70             } catch (MatchException e) {
71                 context.getErrorLog().log(location, "Select expression produces a list of values.");
72                 return new EError(location);
73             }
74             break;
75         case SCLTerminals.SELECT_FIRST:
76             try {
77                 componentType = Types.unifyApply(Types.MAYBE, requiredType);
78             } catch (MatchException e) {
79                 context.getErrorLog().log(location, "Select first expression produces an optional value.");
80                 return new EError(location);
81             }
82             break;
83         default: throw new InternalCompilerError();
84         }
85         for(Variable variable : variables)
86             variable.setType(Types.metaVar(Kinds.STAR));
87         expression.checkType(context, componentType);
88         query.checkType(context);
89         
90         // Compile query
91         Type elType = expression.getType();
92         Expression result;
93         if(selectVariant == SCLTerminals.SELECT_FIRST) {
94             QueryCompilationContext queryCompilationContext =
95                     new QueryCompilationContext(context, QueryCompilationMode.GET_FIRST,
96                             expression.getType(),
97                             Just(expression));
98             try {
99                 new QExists(variables, query).generate(queryCompilationContext);
100             } catch (UnsolvableQueryException e) {
101                 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
102                 return new EError(getLocation());
103             }
104             result = queryCompilationContext.getContinuation();
105         }
106         else {
107             Variable accumulator = newVar("accum", Types.apply(ARRAY_LIST, elType));
108             result =
109                     apply(context.getCompilationContext(), Types.PROC, Names.ArrayList_freeze, elType,
110                             var(accumulator));
111             Expression innerExpression = 
112                     apply(context.getCompilationContext(), Types.PROC, Names.ArrayList_add, elType,
113                             var(accumulator), expression);
114             try {
115                 QueryCompilationContext queryCompilationContext =
116                         new QueryCompilationContext(context, QueryCompilationMode.ITERATE, null, innerExpression);
117                 new QExists(variables, query).generate(queryCompilationContext);
118                 result = seq(queryCompilationContext.getContinuation(), result);
119             } catch(UnsolvableQueryException e) {
120                 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
121                 return new EError(getLocation());
122             }
123             result = let(accumulator,
124                     apply(context.getCompilationContext(), Types.PROC, Names.ArrayList_new, elType, tuple()),
125                     result
126                     );
127         }
128         return loc(location, result);
129     }
130
131     @Override
132     public void collectFreeVariables(THashSet<Variable> vars) {
133         expression.collectFreeVariables(vars);
134         query.collectFreeVariables(vars);
135         for(Variable variable : variables)
136             vars.remove(variable);
137     }
138
139     @Override
140     public Expression resolve(TranslationContext context) {
141         context.pushExistentialFrame();
142         expression = expression.resolve(context);
143         query = query.resolve(context);
144         variables = context.popExistentialFrame();
145         return this;
146     }
147     
148     @Override
149     public void setLocationDeep(long loc) {
150         if(location == Locations.NO_LOCATION) {
151             location = loc;
152             expression.setLocationDeep(loc);
153             query.setLocationDeep(loc);
154         }
155     }
156     
157     @Override
158     public void accept(ExpressionVisitor visitor) {
159         visitor.visit(this);
160     }
161     
162     @Override
163     public Expression simplify(SimplificationContext context) {
164         throw new UnsupportedOperationException();
165     }
166     
167     @Override
168     public Expression accept(ExpressionTransformer transformer) {
169         return transformer.transform(this);
170     }
171
172 }