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