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.types.Type;
13 import org.simantics.scl.compiler.types.Types;
14 import org.simantics.scl.compiler.types.exceptions.MatchException;
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.THashSet;
18 import gnu.trove.set.hash.TIntHashSet;
20 public class EEnforce extends SimplifiableExpression {
24 public EEnforce(Query query) {
28 public Query getQuery() {
33 public void collectRefs(TObjectIntHashMap<Object> allRefs,
35 query.collectRefs(allRefs, refs);
39 public void collectVars(TObjectIntHashMap<Variable> allVars,
41 query.collectVars(allVars, vars);
45 public void collectEffects(THashSet<Type> effects) {
46 throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectEffects.");
50 protected void updateType() throws MatchException {
51 setType(Types.tupleConstructor(0));
55 public Expression inferType(TypingContext context) {
56 query.checkType(context);
57 //context.declareEffect(location, query.getEffect(Query.W));
58 return compile(context);
61 public Expression compile(TypingContext context) {
62 return query.generateEnforce(new EnforcingContext(context));
66 public void collectFreeVariables(THashSet<Variable> vars) {
67 query.collectFreeVariables(vars);
71 public Expression simplify(SimplificationContext context) {
72 /*query = query.simplify(context);
73 return query.generateEnforce(context);*/
74 throw new InternalCompilerError();
78 public Expression resolve(TranslationContext context) {
79 context.pushExistentialFrame();
80 query = query.resolve(context);
81 Variable[] variables = context.popExistentialFrame();
82 if(variables.length > 0)
83 query = new QExists(variables, query);
88 public void setLocationDeep(long loc) {
89 if(location == Locations.NO_LOCATION) {
91 query.setLocationDeep(loc);
96 public void accept(ExpressionVisitor visitor) {
101 public void forVariables(VariableProcedure procedure) {
102 query.forVariables(procedure);
106 public Expression replace(ReplaceContext context) {
107 return new EEnforce(query.replace(context));
111 public Expression accept(ExpressionTransformer transformer) {
112 return transformer.transform(this);