1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.compilation.CompilationContext;
4 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
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.errors.Locations;
9 import org.simantics.scl.compiler.internal.codegen.references.IVal;
10 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
11 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
12 import org.simantics.scl.compiler.internal.interpreted.IExpression;
13 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
14 import org.simantics.scl.compiler.types.Type;
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 ECHRRuleset extends Expression {
25 public ECHRRuleset(CHRRuleset ruleset, Expression in) {
26 this.ruleset = ruleset;
31 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
32 ruleset.collectRefs(allRefs, refs);
33 in.collectRefs(allRefs, refs);
36 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
37 ruleset.collectVars(allVars, vars);
38 in.collectVars(allVars, vars);
41 public void forVariables(VariableProcedure procedure) {
42 ruleset.forVariables(procedure);
43 in.forVariables(procedure);
46 protected void updateType() throws MatchException {
47 setType(in.getType());
50 public IVal toVal(CompilationContext context, CodeWriter w) {
51 ruleset.generateCode(w);
52 return in.toVal(context, w);
55 public void collectFreeVariables(THashSet<Variable> vars) {
56 ruleset.collectFreeVariables(vars);
57 in.collectFreeVariables(vars);
60 public Expression resolve(TranslationContext context) {
61 if(context.currentRuleset != null) {
62 context.getErrorLog().log(location, "Current version of SCL compiler does not support nested rulesets.");
65 context.currentRuleset = ruleset;
68 context.pushCHRConstraintFrame();
69 ruleset.resolve(context);
70 in = in.resolve(context);
71 context.popCHRConstraintFrame(ruleset.constraints);
74 context.currentRuleset = null;
79 public void setLocationDeep(long loc) {
80 if(location == Locations.NO_LOCATION) {
82 ruleset.setLocationDeep(loc);
83 in.setLocationDeep(loc);
87 public Expression decorate(ExpressionDecorator decorator) {
88 in = in.decorate(decorator);
92 public void collectEffects(THashSet<Type> effects) {
93 ruleset.collectEffects(effects);
94 in.collectEffects(effects);
97 public void accept(ExpressionVisitor visitor) {
102 public Expression inferType(TypingContext context) {
103 ruleset.checkType(context);
104 in = in.inferType(context);
109 public Expression simplify(SimplificationContext context) {
110 ruleset.simplify(context);
111 ruleset.compile(context);
112 in = in.simplify(context);
117 public Expression accept(ExpressionTransformer transformer) {
118 return transformer.transform(this);
122 public IExpression toIExpression(ExpressionInterpretationContext context) {
123 throw new UnsupportedOperationException();