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