1 package org.simantics.scl.compiler.elaboration.expressions.list;
\r
3 import gnu.trove.map.hash.TObjectIntHashMap;
\r
4 import gnu.trove.set.hash.THashSet;
\r
5 import gnu.trove.set.hash.TIntHashSet;
\r
7 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
\r
8 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
\r
9 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
\r
10 import org.simantics.scl.compiler.elaboration.expressions.Case;
\r
11 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
\r
12 import org.simantics.scl.compiler.elaboration.expressions.Expression;
\r
13 import org.simantics.scl.compiler.elaboration.expressions.Variable;
\r
14 import org.simantics.scl.compiler.elaboration.expressions.VariableProcedure;
\r
15 import org.simantics.scl.compiler.errors.Locations;
\r
16 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
\r
17 import org.simantics.scl.compiler.types.TMetaVar;
\r
18 import org.simantics.scl.compiler.types.Type;
\r
19 import org.simantics.scl.compiler.types.Types;
\r
20 import org.simantics.scl.compiler.types.kinds.Kinds;
\r
22 public class ListGenerator extends ListQualifier {
\r
23 public Expression pattern;
\r
24 public Expression value;
\r
26 public ListGenerator(Expression pattern, Expression value) {
\r
27 this.pattern = pattern;
\r
32 public void checkType(TypingContext context) {
\r
33 TMetaVar componentType = Types.metaVar(Kinds.STAR);
\r
34 value.checkType(context, Types.apply(Types.LIST, componentType));
\r
35 pattern.checkTypeAsPattern(context, componentType);
\r
39 public void collectRefs(TObjectIntHashMap<Object> allRefs,
\r
41 value.collectRefs(allRefs, refs);
\r
45 public void collectVars(TObjectIntHashMap<Variable> allVars,
\r
47 value.collectVars(allVars, vars);
\r
51 public void collectFreeVariables(THashSet<Variable> vars) {
\r
52 value.collectFreeVariables(vars);
\r
53 pattern.collectFreeVariables(vars);
\r
57 public CompiledQualifier compile(SimplificationContext context) {
\r
58 if(pattern instanceof EVariable)
\r
59 return new CompiledQualifier(value, pattern);
\r
61 THashSet<Variable> variables = pattern.getFreeVariables();
\r
62 Variable[] variableArray = variables.toArray(new Variable[variables.size()]);
\r
63 Expression[] variableExps = new Expression[variableArray.length];
\r
64 for(int i=0;i<variableArray.length;++i)
\r
65 variableExps[i] = new EVariable(variableArray[i]);
\r
66 Expression newPattern = context.tuple(variableExps);
\r
68 EVariable blank = context.blank();
\r
69 blank.getVariable().setType(pattern.getType());
\r
71 return new CompiledQualifier(
\r
74 new Case(pattern, context.singletonList(newPattern.copy())),
\r
75 new Case(blank, context.emptyList(newPattern.getType()))
\r
84 public void resolve(TranslationContext context) {
\r
85 value = value.resolve(context);
\r
86 pattern = pattern.resolveAsPattern(context);
\r
90 public void decorate(ExpressionDecorator decorator) {
\r
91 value = value.decorate(decorator);
\r
92 pattern = pattern.decorate(decorator);
\r
96 public void collectEffects(THashSet<Type> effects) {
\r
97 pattern.collectEffects(effects);
\r
98 value.collectEffects(effects);
\r
102 public void setLocationDeep(long loc) {
\r
103 if(location == Locations.NO_LOCATION) {
\r
105 pattern.setLocationDeep(loc);
\r
106 value.setLocationDeep(loc);
\r
111 public void accept(ListQualifierVisitor visitor) {
\r
112 visitor.visit(this);
\r
116 public void forVariables(VariableProcedure procedure) {
\r
117 value.forVariables(procedure);
\r
121 public ListQualifier accept(ListQualifierTransformer transformer) {
\r
122 return transformer.transform(this);
\r