]> gerrit.simantics Code Review - simantics/platform.git/blob
95948d45c391e17841a64244576a6b8f6bb1271a
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
4 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
6 import org.simantics.scl.compiler.errors.Locations;
7 import org.simantics.scl.compiler.types.TPred;
8 import org.simantics.scl.compiler.types.exceptions.MatchException;
9
10 import gnu.trove.map.hash.TObjectIntHashMap;
11 import gnu.trove.set.hash.THashSet;
12 import gnu.trove.set.hash.TIntHashSet;
13
14 public class EGetConstraint extends SimplifiableExpression {
15     TPred constraint;
16     EVariable evidence;
17         
18     public EGetConstraint(long loc, TPred constraint) {
19         super(loc);
20         this.constraint = constraint;
21     }
22     
23     @Override
24     public void collectVars(TObjectIntHashMap<Variable> allVars,
25             TIntHashSet vars) {
26         if(evidence != null)
27             evidence.collectVars(allVars, vars);        
28     }
29     
30     @Override
31     public Expression inferType(TypingContext context) {
32         Variable variable = new Variable("evidence");
33         variable.setType(constraint);
34         evidence = new EVariable(getLocation(), variable);
35         evidence.setType(variable.getType());
36         context.addConstraintDemand(evidence);
37         return this;
38     }
39     
40     @Override
41     protected void updateType() throws MatchException {
42         setType(constraint);
43     }
44     
45     @Override
46     public void collectFreeVariables(THashSet<Variable> vars) {
47         if(evidence != null)
48             evidence.collectFreeVariables(vars);
49     }
50     
51     @Override
52     public Expression simplify(SimplificationContext context) {
53         return evidence.simplify(context);
54     }
55     
56     @Override
57     public Expression resolve(TranslationContext context) {
58         return this;
59     }
60     
61     @Override
62     public void setLocationDeep(long loc) {
63         if(location == Locations.NO_LOCATION) {
64             location = loc;
65             if(evidence != null)
66                 evidence.setLocationDeep(loc);
67         }
68     }
69     
70     @Override
71     public void accept(ExpressionVisitor visitor) {
72         visitor.visit(this);
73     }
74     
75     @Override
76     public Expression accept(ExpressionTransformer transformer) {
77         return transformer.transform(this);
78     }
79
80 }