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.Type;
15 import gnu.trove.map.hash.TObjectIntHashMap;
16 import gnu.trove.set.hash.THashSet;
17 import gnu.trove.set.hash.TIntHashSet;
19 public class ListAssignment extends ListQualifier {
20 public Expression pattern;
21 public Expression value;
23 public ListAssignment(Expression pattern, Expression value) {
24 this.pattern = pattern;
29 public void checkType(TypingContext context) {
30 value = value.inferType(context);
31 pattern.checkTypeAsPattern(context, value.getType());
35 public void collectRefs(TObjectIntHashMap<Object> allRefs,
37 value.collectRefs(allRefs, refs);
41 public void collectVars(TObjectIntHashMap<Variable> allVars,
43 value.collectVars(allVars, vars);
47 public void collectFreeVariables(THashSet<Variable> vars) {
48 value.collectFreeVariables(vars);
49 pattern.collectFreeVariables(vars);
53 public CompiledQualifier compile(SimplificationContext context) {
54 if(pattern instanceof EVariable)
55 return new CompiledQualifier(context.singletonList(value), pattern);
57 THashSet<Variable> variables = pattern.getFreeVariables();
58 Variable[] variableArray = variables.toArray(new Variable[variables.size()]);
59 Expression[] variableExps = new Expression[variableArray.length];
60 for(int i=0;i<variableArray.length;++i)
61 variableExps[i] = new EVariable(variableArray[i]);
62 Expression newPattern = context.tuple(variableExps);
64 EVariable blank = context.blank();
65 blank.getVariable().setType(pattern.getType());
67 return new CompiledQualifier(
69 new Case(pattern, context.singletonList(newPattern.copy())),
70 new Case(blank, context.emptyList(newPattern.getType()))
78 public void resolve(TranslationContext context) {
79 value = value.resolve(context);
80 pattern = pattern.resolveAsPattern(context);
84 public void decorate(ExpressionDecorator decorator) {
85 value = value.decorate(decorator);
86 pattern = pattern.decorate(decorator);
90 public void collectEffects(THashSet<Type> effects) {
91 pattern.collectEffects(effects);
92 value.collectEffects(effects);
96 public void setLocationDeep(long loc) {
97 if(location == Locations.NO_LOCATION) {
99 pattern.setLocationDeep(loc);
100 value.setLocationDeep(loc);
105 public void accept(ListQualifierVisitor visitor) {
110 public void forVariables(VariableProcedure procedure) {
111 value.forVariables(procedure);
115 public ListQualifier accept(ListQualifierTransformer transformer) {
116 return transformer.transform(this);