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