]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/visitors/CollectRefsVisitor.java
(refs #7375) Replaced collectFreeVariables method by a visitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / visitors / CollectRefsVisitor.java
1 package org.simantics.scl.compiler.elaboration.expressions.visitors;
2
3 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
4 import org.simantics.scl.compiler.elaboration.expressions.ETransformation;
5 import org.simantics.scl.compiler.elaboration.query.QAtom;
6 import org.simantics.scl.compiler.elaboration.relations.CompositeRelation;
7 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
8
9 import gnu.trove.map.hash.TObjectIntHashMap;
10 import gnu.trove.set.hash.TIntHashSet;
11
12 public class CollectRefsVisitor extends StandardExpressionVisitor {
13     private final TObjectIntHashMap<Object> allRefs;
14     private final TIntHashSet refs;
15     
16     public CollectRefsVisitor(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
17         this.allRefs = allRefs;
18         this.refs = refs;
19     }
20     
21     @Override
22     public void visit(ETransformation expression) {
23         {
24             int ref = allRefs.get(ETransformation.TRANSFORMATION_RULES_TYPECHECKED);
25             if(ref >= 0)
26                 refs.add(ref);
27         }
28         super.visit(expression);
29     }
30     
31     @Override
32     public void visit(EConstant expression) {
33         int id = allRefs.get(expression.value);
34         if(id >= 0)
35             refs.add(id);
36     }
37     
38     @Override
39     public void visit(QAtom query) {
40         collectRelationRefs(query.relation);
41         super.visit(query);
42     }
43     
44     private void collectRelationRefs(SCLRelation relation) {
45         if(relation instanceof CompositeRelation) {
46             for(SCLRelation subrelation : ((CompositeRelation) relation).getSubrelations())
47                 collectRelationRefs(subrelation);
48         }
49         else {
50             int id = allRefs.get(relation);
51             if(id >= 0)
52                 refs.add(id);
53         }
54     }
55 }