859690ec816265ce327596859246a4404a260a8d
[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.expressions.VariableProcedure;
10 import org.simantics.scl.compiler.elaboration.query.QConjunction;
11 import org.simantics.scl.compiler.elaboration.query.QExists;
12 import org.simantics.scl.compiler.elaboration.query.Query;
13 import org.simantics.scl.compiler.elaboration.query.QueryVisitor;
14 import org.simantics.scl.compiler.elaboration.query.compilation.ConstraintCollectionContext;
15
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.THashSet;
18 import gnu.trove.set.hash.TIntHashSet;
19
20 /**
21  * Query classes that may exist before resolving
22  */
23 public abstract class PreQuery extends Query {
24
25     public ArrayList<Variable> extraVariables = new ArrayList<Variable>(2);
26     public ArrayList<Query> sideQueries = new ArrayList<Query>(2);
27         
28     @Override
29     public void collectFreeVariables(THashSet<Variable> vars) {
30         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectFreeVariables.");
31     }
32     
33     @Override
34     public void checkType(TypingContext context) {
35         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support checkType.");
36     }
37     
38     @Override
39     public void collectConstraints(ConstraintCollectionContext context) {
40         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectConstraints.");
41     }
42     
43     @Override
44     public void collectVars(TObjectIntHashMap<Variable> allVars,
45             TIntHashSet vars) {
46         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support collectVars.");
47     }
48     
49     @Override
50     public Query replace(ReplaceContext replaceContext) {
51         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support replace.");
52     }
53     
54     @Override
55     final public void accept(QueryVisitor visitor) {
56         throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support accept.");
57     }
58     
59     protected Query withSideQueries(Query query) {
60         if(!sideQueries.isEmpty()) {
61             sideQueries.add(query);
62             query = new QConjunction(sideQueries.toArray(new Query[sideQueries.size()]));
63         }
64         if(!extraVariables.isEmpty())
65             query = new QExists(extraVariables.toArray(new Variable[extraVariables.size()]), query);
66         return query;
67     }
68 }