]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EEquations.java
(refs #7375) Replace collectRefs by CollectRefsVisitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EEquations.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
4 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
7 import org.simantics.scl.compiler.elaboration.equation.Equation;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.types.Type;
10 import org.simantics.scl.compiler.types.Types;
11 import org.simantics.scl.compiler.types.exceptions.MatchException;
12
13 import gnu.trove.map.hash.TObjectIntHashMap;
14 import gnu.trove.set.hash.THashSet;
15 import gnu.trove.set.hash.TIntHashSet;
16
17 public class EEquations extends SimplifiableExpression {
18
19     public Equation[] equations;
20     public Type effect = Types.NO_EFFECTS;
21     
22     public EEquations(Equation[] equations) {
23         this.equations = equations;
24     }
25     
26     public EEquations(long location, Equation[] equations) {
27         this(equations);
28         this.location = location;
29     }
30
31     @Override
32     public Expression resolve(TranslationContext context) {
33         for(Equation equation : equations)
34             equation.resolve(context);       
35         return this;
36     }
37
38     @Override
39     public void setLocationDeep(long loc) {
40         if(location == Locations.NO_LOCATION) {
41             location = loc;
42             for(Equation equation : equations)
43                 equation.setLocationDeep(loc);
44         }
45     }
46
47     @Override
48     public Expression accept(ExpressionTransformer transformer) {
49         return transformer.transform(this);
50     }
51
52     @Override
53     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
54         for(Equation equation : equations)
55             equation.collectVars(allVars, vars);
56     }
57
58     @Override
59     protected void updateType() throws MatchException {
60         setType(Types.UNIT);
61     }
62
63     @Override
64     public void collectFreeVariables(THashSet<Variable> vars) {
65         for(Equation equation : equations)
66             equation.collectFreeVariables(vars);
67     }
68
69     @Override
70     public void collectEffects(THashSet<Type> effects) {
71         for(Equation equation : equations)
72             equation.collectEffects(effects);
73     }
74
75     @Override
76     public void accept(ExpressionVisitor visitor) {
77         visitor.visit(this);
78     }
79     
80     @Override
81     public Expression inferType(TypingContext context) {
82         context.declareEffect(this.location, effect);
83         for(Equation equation : equations)
84             equation.checkType(context);
85         return this;
86     }
87     
88     @Override
89     public Expression checkIgnoredType(TypingContext context) {
90         return inferType(context);
91     }
92
93     @Override
94     public Expression simplify(SimplificationContext context) {
95         context.getErrorLog().log(location, "Equations should be transformed into other expressions before simplification phase.");
96         return Expressions.tuple();
97     }
98     
99     @Override
100     public Expression replace(ReplaceContext context) {
101         Equation[] newEquations = new Equation[equations.length];
102         for(int i=0;i<equations.length;++i)
103             newEquations[i] = equations[i].replace(context);
104         return new EEquations(location, newEquations);
105     }
106
107 }