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.Expression;
7 import org.simantics.scl.compiler.elaboration.expressions.Variable;
8 import org.simantics.scl.compiler.elaboration.expressions.VariableProcedure;
9 import org.simantics.scl.compiler.errors.Locations;
10 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
11 import org.simantics.scl.compiler.types.TMetaVar;
12 import org.simantics.scl.compiler.types.Type;
13 import org.simantics.scl.compiler.types.Types;
14 import org.simantics.scl.compiler.types.exceptions.UnificationException;
15 import org.simantics.scl.compiler.types.kinds.Kinds;
17 import gnu.trove.map.hash.TObjectIntHashMap;
18 import gnu.trove.set.hash.THashSet;
19 import gnu.trove.set.hash.TIntHashSet;
21 public class ListThen extends ListQualifier {
22 public ListQualifier left;
23 public Expression transformer;
24 public Expression by; // optional
27 public ListThen(Expression transformer, Expression by) {
28 this.transformer = transformer;
32 public void setLeft(ListQualifier inner) {
37 public void checkType(TypingContext context) {
38 left.checkType(context);
40 cType = Types.metaVar(Kinds.STAR);
41 Type transformerType = Types.function(Types.list(cType), Types.list(cType));
43 by = by.checkType(context, Types.metaVar(Kinds.STAR));
44 transformerType = Types.function(Types.function(cType, by.getType()), transformerType);
46 transformer = transformer.checkType(context, transformerType);
47 if(!(Types.canonical(cType) instanceof TMetaVar)) {
48 context.getErrorLog().log(location, "Transformation function must be generic on list elements.");
53 public void collectRefs(TObjectIntHashMap<Object> allRefs,
55 left.collectRefs(allRefs, refs);
56 transformer.collectRefs(allRefs, refs);
58 by.collectRefs(allRefs, refs);
62 public void collectVars(TObjectIntHashMap<Variable> allVars,
64 left.collectVars(allVars, vars);
65 transformer.collectVars(allVars, vars);
67 by.collectVars(allVars, vars);
71 public void collectFreeVariables(THashSet<Variable> vars) {
72 left.collectFreeVariables(vars);
73 transformer.collectFreeVariables(vars);
75 by.collectFreeVariables(vars);
79 public CompiledQualifier compile(SimplificationContext context) {
80 CompiledQualifier q = left.compile(context);
83 Types.unify(cType, q.pattern.getType());
84 } catch (UnificationException e) {
85 context.getErrorLog().log(location, "Transformation function must be generic on list elements.");
89 q.value = context.apply(transformer, q.value);
91 q.value = context.apply(transformer, context.lambda(q.pattern.copy(), by), q.value);
96 public void resolve(TranslationContext context) {
97 transformer = transformer.resolve(context);
98 left.resolve(context);
100 by = by.resolve(context);
104 public void decorate(ExpressionDecorator decorator) {
105 transformer = transformer.decorate(decorator);
107 by = by.decorate(decorator);
108 left.decorate(decorator);
112 public void collectEffects(THashSet<Type> effects) {
113 left.collectEffects(effects);
114 transformer.collectEffects(effects);
116 by.collectEffects(effects);
120 public void setLocationDeep(long loc) {
121 if(location == Locations.NO_LOCATION) {
123 left.setLocationDeep(loc);
124 transformer.setLocationDeep(loc);
126 by.setLocationDeep(loc);
131 public void accept(ListQualifierVisitor visitor) {
136 public void forVariables(VariableProcedure procedure) {
137 left.forVariables(procedure);
138 transformer.forVariables(procedure);
140 by.forVariables(procedure);
144 public ListQualifier accept(ListQualifierTransformer transformer) {
145 return transformer.transform(this);