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.internal.elaboration.utils.ExpressionDecorator;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.compiler.types.Types;
17 import org.simantics.scl.compiler.types.exceptions.MatchException;
18 import org.simantics.scl.compiler.types.exceptions.UnificationException;
19 import org.simantics.scl.compiler.types.kinds.Kinds;
21 import gnu.trove.map.hash.TObjectIntHashMap;
22 import gnu.trove.set.hash.THashSet;
23 import gnu.trove.set.hash.TIntHashSet;
25 public class EWhen extends SimplifiableExpression {
28 public Expression action;
31 public EWhen(Query query, Expression action) {
36 public EWhen(long location, Query query, Expression action, Variable[] variables) {
38 throw new NullPointerException();
39 if(location == Locations.NO_LOCATION)
40 new Exception().printStackTrace();
41 this.location = location;
44 this.variables = variables;
48 public void collectRefs(TObjectIntHashMap<Object> allRefs,
50 query.collectRefs(allRefs, refs);
51 action.collectRefs(allRefs, refs);
55 public void collectVars(TObjectIntHashMap<Variable> allVars,
57 query.collectVars(allVars, vars);
58 action.collectVars(allVars, vars);
62 public void collectEffects(THashSet<Type> effects) {
63 throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectEffects.");
67 protected void updateType() throws MatchException {
72 public Expression checkBasicType(TypingContext context, Type requiredType) {
74 Types.unify(requiredType, Types.UNIT);
75 } catch (UnificationException e) {
76 context.typeError(location, requiredType, Types.UNIT);
79 for(Variable variable : variables)
80 variable.setType(Types.metaVar(Kinds.STAR));
81 query.checkType(context);
82 action.checkIgnoredType(context);
85 return compile(context);
88 public Expression compile(TypingContext context) {
90 QueryCompilationContext queryCompilationContext =
91 new QueryCompilationContext(context, QueryCompilationMode.ITERATE, null, action);
92 new QExists(variables, query).generate(queryCompilationContext);
93 return queryCompilationContext.getContinuation();
94 } catch(UnsolvableQueryException e) {
95 context.getErrorLog().log(getLocation(), "Failed to compile the query.\n" + e.getMessage());
96 return new EError(getLocation());
101 public void collectFreeVariables(THashSet<Variable> vars) {
102 action.collectFreeVariables(vars);
103 query.collectFreeVariables(vars);
104 for(Variable var : variables)
109 public Expression resolve(TranslationContext context) {
110 context.pushExistentialFrame();
111 query = query.resolve(context);
112 action = action.resolve(context);
113 variables = context.popExistentialFrame();
118 public Expression decorate(ExpressionDecorator decorator) {
119 return decorator.decorate(this);
123 public Expression replace(ReplaceContext context) {
124 Variable[] newVariables = new Variable[variables.length];
125 for(int i=0;i<variables.length;++i) {
126 Variable variable = variables[i];
127 Variable newVariable = new Variable(variable.name, variable.getType());
128 newVariables[i] = newVariable;
129 context.varMap.put(variable, new EVariable(newVariable));
131 EWhen result = new EWhen(location, query.replace(context), action.replace(context), newVariables);
132 for(int i=0;i<variables.length;++i)
133 context.varMap.remove(variables[i]);
138 public void setLocationDeep(long loc) {
139 if(location == Locations.NO_LOCATION) {
141 query.setLocationDeep(loc);
142 action.setLocationDeep(loc);
147 public void accept(ExpressionVisitor visitor) {
151 public Query getQuery() {
155 public Expression getAction() {
160 public void forVariables(VariableProcedure procedure) {
161 query.forVariables(procedure);
162 action.forVariables(procedure);
166 public Expression simplify(SimplificationContext context) {
167 throw new UnsupportedOperationException();
171 public Expression accept(ExpressionTransformer transformer) {
172 return transformer.transform(this);