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