]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EEquations.java
268cc24977d14fcd806b1ffc434ab50b167c382c
[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.TIntHashSet;
15
16 public class EEquations extends SimplifiableExpression {
17
18     public Equation[] equations;
19     public Type effect = Types.NO_EFFECTS;
20     
21     public EEquations(Equation[] equations) {
22         this.equations = equations;
23     }
24     
25     public EEquations(long location, Equation[] equations) {
26         this(equations);
27         this.location = location;
28     }
29
30     @Override
31     public Expression resolve(TranslationContext context) {
32         for(Equation equation : equations)
33             equation.resolve(context);       
34         return this;
35     }
36
37     @Override
38     public void setLocationDeep(long loc) {
39         if(location == Locations.NO_LOCATION) {
40             location = loc;
41             for(Equation equation : equations)
42                 equation.setLocationDeep(loc);
43         }
44     }
45
46     @Override
47     public Expression accept(ExpressionTransformer transformer) {
48         return transformer.transform(this);
49     }
50
51     @Override
52     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
53         for(Equation equation : equations)
54             equation.collectVars(allVars, vars);
55     }
56
57     @Override
58     protected void updateType() throws MatchException {
59         setType(Types.UNIT);
60     }
61
62     @Override
63     public void accept(ExpressionVisitor visitor) {
64         visitor.visit(this);
65     }
66     
67     @Override
68     public Expression inferType(TypingContext context) {
69         context.declareEffect(this.location, effect);
70         for(Equation equation : equations)
71             equation.checkType(context);
72         return this;
73     }
74     
75     @Override
76     public Expression checkIgnoredType(TypingContext context) {
77         return inferType(context);
78     }
79
80     @Override
81     public Expression simplify(SimplificationContext context) {
82         context.getErrorLog().log(location, "Equations should be transformed into other expressions before simplification phase.");
83         return Expressions.tuple();
84     }
85     
86     @Override
87     public Expression replace(ReplaceContext context) {
88         Equation[] newEquations = new Equation[equations.length];
89         for(int i=0;i<equations.length;++i)
90             newEquations[i] = equations[i].replace(context);
91         return new EEquations(location, newEquations);
92     }
93
94 }