1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.elaboration.query.QExists;
9 import org.simantics.scl.compiler.elaboration.query.Query;
10 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationContext;
11 import org.simantics.scl.compiler.elaboration.query.compilation.QueryCompilationMode;
12 import org.simantics.scl.compiler.elaboration.query.compilation.UnsolvableQueryException;
13 import org.simantics.scl.compiler.errors.Locations;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.compiler.types.Types;
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
17 import org.simantics.scl.compiler.types.exceptions.UnificationException;
18 import org.simantics.scl.compiler.types.kinds.Kinds;
20 import gnu.trove.map.hash.TObjectIntHashMap;
21 import gnu.trove.set.hash.THashSet;
22 import gnu.trove.set.hash.TIntHashSet;
24 public class EWhen extends SimplifiableExpression {
27 public Expression action;
30 public EWhen(Query query, Expression action) {
35 public EWhen(long location, Query query, Expression action, Variable[] variables) {
37 throw new NullPointerException();
38 if(location == Locations.NO_LOCATION)
39 new Exception().printStackTrace();
40 this.location = location;
43 this.variables = variables;
47 public void collectRefs(TObjectIntHashMap<Object> allRefs,
49 query.collectRefs(allRefs, refs);
50 action.collectRefs(allRefs, refs);
54 public void collectVars(TObjectIntHashMap<Variable> allVars,
56 query.collectVars(allVars, vars);
57 action.collectVars(allVars, vars);
61 public void collectEffects(THashSet<Type> effects) {
62 throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectEffects.");
66 protected void updateType() throws MatchException {
71 public Expression checkBasicType(TypingContext context, Type requiredType) {
73 Types.unify(requiredType, Types.UNIT);
74 } catch (UnificationException e) {
75 context.typeError(location, requiredType, Types.UNIT);
78 for(Variable variable : variables)
79 variable.setType(Types.metaVar(Kinds.STAR));
80 query.checkType(context);
81 action.checkIgnoredType(context);
84 return compile(context);
87 public Expression compile(TypingContext context) {
89 QueryCompilationContext queryCompilationContext =
90 new QueryCompilationContext(context, QueryCompilationMode.ITERATE, null, action);
91 new QExists(variables, query).generate(queryCompilationContext);
92 return queryCompilationContext.getContinuation();
93 } catch(UnsolvableQueryException e) {
94 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
95 return new EError(getLocation());
100 public void collectFreeVariables(THashSet<Variable> vars) {
101 action.collectFreeVariables(vars);
102 query.collectFreeVariables(vars);
103 for(Variable var : variables)
108 public Expression resolve(TranslationContext context) {
109 context.pushExistentialFrame();
110 query = query.resolve(context);
111 action = action.resolve(context);
112 variables = context.popExistentialFrame();
117 public Expression replace(ReplaceContext context) {
118 Variable[] newVariables = new Variable[variables.length];
119 for(int i=0;i<variables.length;++i) {
120 Variable variable = variables[i];
121 Variable newVariable = new Variable(variable.name, variable.getType());
122 newVariables[i] = newVariable;
123 context.varMap.put(variable, new EVariable(newVariable));
125 EWhen result = new EWhen(location, query.replace(context), action.replace(context), newVariables);
126 for(int i=0;i<variables.length;++i)
127 context.varMap.remove(variables[i]);
132 public void setLocationDeep(long loc) {
133 if(location == Locations.NO_LOCATION) {
135 query.setLocationDeep(loc);
136 action.setLocationDeep(loc);
141 public void accept(ExpressionVisitor visitor) {
145 public Query getQuery() {
149 public Expression getAction() {
154 public void forVariables(VariableProcedure procedure) {
155 query.forVariables(procedure);
156 action.forVariables(procedure);
160 public Expression simplify(SimplificationContext context) {
161 throw new UnsupportedOperationException();
165 public Expression accept(ExpressionTransformer transformer) {
166 return transformer.transform(this);