1 package org.simantics.scl.compiler.elaboration.expressions;
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;
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;
29 import gnu.trove.map.hash.TObjectIntHashMap;
30 import gnu.trove.set.hash.TIntHashSet;
32 public class ESelect extends SimplifiableExpression {
34 private final Type ARRAY_LIST = Types.con("ArrayList", "T");
37 public Expression expression;
39 public Variable[] variables;
41 public ESelect(int selectVariant, Expression expression, Query query) {
42 this.selectVariant = selectVariant;
43 this.expression = expression;
48 public void collectVars(TObjectIntHashMap<Variable> allVars,
50 expression.collectVars(allVars, vars);
51 query.collectVars(allVars, vars);
55 protected void updateType() throws MatchException {
56 setType(selectVariant==SCLTerminals.SELECT_FIRST
57 ? Types.apply(Types.MAYBE, expression.getType())
58 : Types.list(expression.getType()));
62 public Expression checkBasicType(TypingContext context, Type requiredType) {
64 switch(selectVariant) {
65 case SCLTerminals.SELECT:
66 case SCLTerminals.SELECT_DISTINCT:
68 componentType = Types.unifyApply(Types.LIST, requiredType);
69 } catch (MatchException e) {
70 context.getErrorLog().log(location, "Select expression produces a list of values.");
71 return new EError(location);
74 case SCLTerminals.SELECT_FIRST:
76 componentType = Types.unifyApply(Types.MAYBE, requiredType);
77 } catch (MatchException e) {
78 context.getErrorLog().log(location, "Select first expression produces an optional value.");
79 return new EError(location);
82 default: throw new InternalCompilerError();
84 for(Variable variable : variables)
85 variable.setType(Types.metaVar(Kinds.STAR));
86 expression.checkType(context, componentType);
87 query.checkType(context);
90 Type elType = expression.getType();
92 if(selectVariant == SCLTerminals.SELECT_FIRST) {
93 QueryCompilationContext queryCompilationContext =
94 new QueryCompilationContext(context, QueryCompilationMode.GET_FIRST,
98 new QExists(variables, query).generate(queryCompilationContext);
99 } catch (UnsolvableQueryException e) {
100 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
101 return new EError(getLocation());
103 result = queryCompilationContext.getContinuation();
106 Variable accumulator = newVar("accum", Types.apply(ARRAY_LIST, elType));
108 apply(context.getCompilationContext(), Types.PROC, Names.ArrayList_freeze, elType,
110 Expression innerExpression =
111 apply(context.getCompilationContext(), Types.PROC, Names.ArrayList_add, elType,
112 var(accumulator), expression);
114 QueryCompilationContext queryCompilationContext =
115 new QueryCompilationContext(context, QueryCompilationMode.ITERATE, null, innerExpression);
116 new QExists(variables, query).generate(queryCompilationContext);
117 result = seq(queryCompilationContext.getContinuation(), result);
118 } catch(UnsolvableQueryException e) {
119 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
120 return new EError(getLocation());
122 result = let(accumulator,
123 apply(context.getCompilationContext(), Types.PROC, Names.ArrayList_new, elType, tuple()),
127 return loc(location, result);
131 public Expression resolve(TranslationContext context) {
132 context.pushExistentialFrame();
133 expression = expression.resolve(context);
134 query = query.resolve(context);
135 variables = context.popExistentialFrame();
140 public void setLocationDeep(long loc) {
141 if(location == Locations.NO_LOCATION) {
143 expression.setLocationDeep(loc);
144 query.setLocationDeep(loc);
149 public void accept(ExpressionVisitor visitor) {
154 public Expression simplify(SimplificationContext context) {
155 throw new UnsupportedOperationException();
159 public Expression accept(ExpressionTransformer transformer) {
160 return transformer.transform(this);