]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/ECHRRuleset.java
(refs #7375) Replace collectRefs by CollectRefsVisitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / ECHRRuleset.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
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.interpreted.IExpression;
12 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
13 import org.simantics.scl.compiler.types.Type;
14 import org.simantics.scl.compiler.types.exceptions.MatchException;
15
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.THashSet;
18 import gnu.trove.set.hash.TIntHashSet;
19
20 public class ECHRRuleset extends Expression {
21     CHRRuleset ruleset;
22     Expression in;
23     
24     public ECHRRuleset(CHRRuleset ruleset, Expression in) {
25         this.ruleset = ruleset;
26         this.in = in;
27     }
28     
29     @Override
30     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
31         ruleset.collectVars(allVars, vars);
32         in.collectVars(allVars, vars);
33     }
34
35     @Override
36     protected void updateType() throws MatchException {
37         setType(in.getType());
38     }
39
40     @Override
41     public IVal toVal(CompilationContext context, CodeWriter w) {
42         ruleset.generateCode(w);
43         return in.toVal(context, w);
44     }
45
46     @Override
47     public void collectFreeVariables(THashSet<Variable> vars) {
48         ruleset.collectFreeVariables(vars);
49         in.collectFreeVariables(vars);
50     }
51
52     @Override
53     public Expression resolve(TranslationContext context) {
54         if(context.currentRuleset != null) {
55             context.getErrorLog().log(location, "Current version of SCL compiler does not support nested rulesets.");
56             return this;
57         }
58         context.currentRuleset = ruleset;
59         
60         context.pushFrame();
61         context.pushCHRConstraintFrame();
62         ruleset.resolve(context);
63         in = in.resolve(context);
64         context.popCHRConstraintFrame(ruleset.constraints);
65         context.popFrame();
66         
67         context.currentRuleset = null;
68         
69         return this;
70     }
71     @Override
72     public void setLocationDeep(long loc) {
73         if(location == Locations.NO_LOCATION) {
74             this.location = loc;
75             ruleset.setLocationDeep(loc);
76             in.setLocationDeep(loc);
77         }
78     }
79     @Override
80     public void collectEffects(THashSet<Type> effects) {
81         ruleset.collectEffects(effects);
82         in.collectEffects(effects);
83     }
84     @Override
85     public void accept(ExpressionVisitor visitor) {
86         visitor.visit(this);
87     }
88     
89     @Override
90     public Expression inferType(TypingContext context) {
91         ruleset.checkType(context);
92         in = in.inferType(context);
93         return this;
94     }
95     
96     @Override
97     public Expression simplify(SimplificationContext context) {
98         ruleset.simplify(context);
99         ruleset.compile(context);
100         in = in.simplify(context);
101         return this;
102     }
103     
104     @Override
105     public Expression accept(ExpressionTransformer transformer) {
106         return transformer.transform(this);
107     }
108     
109     @Override
110     public IExpression toIExpression(ExpressionInterpretationContext context) {
111         throw new UnsupportedOperationException();
112     }
113
114 }