(refs #7375) Replaced collectFreeVariables method by a visitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / query / pre / PreQuery.java
1 package org.simantics.scl.compiler.elaboration.query.pre;
2
3 import java.util.ArrayList;
4
5 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
6 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.elaboration.expressions.Variable;
9 import org.simantics.scl.compiler.elaboration.query.QConjunction;
10 import org.simantics.scl.compiler.elaboration.query.QExists;
11 import org.simantics.scl.compiler.elaboration.query.Query;
12 import org.simantics.scl.compiler.elaboration.query.QueryVisitor;
13 import org.simantics.scl.compiler.elaboration.query.compilation.ConstraintCollectionContext;
14
15 import gnu.trove.map.hash.TObjectIntHashMap;
16 import gnu.trove.set.hash.TIntHashSet;
17
18 /**
19  * Query classes that may exist before resolving
20  */
21 public abstract class PreQuery extends Query {
22
23     public ArrayList<Variable> extraVariables = new ArrayList<Variable>(2);
24     public ArrayList<Query> sideQueries = new ArrayList<Query>(2);
25     
26     @Override
27     public void checkType(TypingContext context) {
28         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support checkType.");
29     }
30     
31     @Override
32     public void collectConstraints(ConstraintCollectionContext context) {
33         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectConstraints.");
34     }
35     
36     @Override
37     public void collectVars(TObjectIntHashMap<Variable> allVars,
38             TIntHashSet vars) {
39         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectVars.");
40     }
41     
42     @Override
43     public Query replace(ReplaceContext replaceContext) {
44         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support replace.");
45     }
46     
47     @Override
48     final public void accept(QueryVisitor visitor) {
49         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support accept.");
50     }
51     
52     protected Query withSideQueries(Query query) {
53         if(!sideQueries.isEmpty()) {
54             sideQueries.add(query);
55             query = new QConjunction(sideQueries.toArray(new Query[sideQueries.size()]));
56         }
57         if(!extraVariables.isEmpty())
58             query = new QExists(extraVariables.toArray(new Variable[extraVariables.size()]), query);
59         return query;
60     }
61 }