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.EnforcingContext;
11 import org.simantics.scl.compiler.errors.Locations;
12 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
13 import org.simantics.scl.compiler.types.Type;
14 import org.simantics.scl.compiler.types.Types;
15 import org.simantics.scl.compiler.types.exceptions.MatchException;
17 import gnu.trove.map.hash.TObjectIntHashMap;
18 import gnu.trove.set.hash.THashSet;
19 import gnu.trove.set.hash.TIntHashSet;
21 public class EEnforce extends SimplifiableExpression {
25 public EEnforce(Query query) {
29 public Query getQuery() {
34 public void collectRefs(TObjectIntHashMap<Object> allRefs,
36 query.collectRefs(allRefs, refs);
40 public void collectVars(TObjectIntHashMap<Variable> allVars,
42 query.collectVars(allVars, vars);
46 public void collectEffects(THashSet<Type> effects) {
47 throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectEffects.");
51 protected void updateType() throws MatchException {
52 setType(Types.tupleConstructor(0));
56 public Expression inferType(TypingContext context) {
57 query.checkType(context);
58 //context.declareEffect(location, query.getEffect(Query.W));
59 return compile(context);
62 public Expression compile(TypingContext context) {
63 return query.generateEnforce(new EnforcingContext(context));
67 public void collectFreeVariables(THashSet<Variable> vars) {
68 query.collectFreeVariables(vars);
72 public Expression simplify(SimplificationContext context) {
73 /*query = query.simplify(context);
74 return query.generateEnforce(context);*/
75 throw new InternalCompilerError();
79 public Expression resolve(TranslationContext context) {
80 context.pushExistentialFrame();
81 query = query.resolve(context);
82 Variable[] variables = context.popExistentialFrame();
83 if(variables.length > 0)
84 query = new QExists(variables, query);
89 public Expression decorate(ExpressionDecorator decorator) {
90 return decorator.decorate(this);
94 public void setLocationDeep(long loc) {
95 if(location == Locations.NO_LOCATION) {
97 query.setLocationDeep(loc);
102 public void accept(ExpressionVisitor visitor) {
107 public void forVariables(VariableProcedure procedure) {
108 query.forVariables(procedure);
112 public Expression replace(ReplaceContext context) {
113 return new EEnforce(query.replace(context));
117 public Expression accept(ExpressionTransformer transformer) {
118 return transformer.transform(this);