1 package org.simantics.scl.compiler.elaboration.expressions;
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.list.CompiledQualifier;
7 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
10 import org.simantics.scl.compiler.types.Type;
11 import org.simantics.scl.compiler.types.Types;
12 import org.simantics.scl.compiler.types.exceptions.MatchException;
14 import gnu.trove.map.hash.TObjectIntHashMap;
15 import gnu.trove.set.hash.THashSet;
16 import gnu.trove.set.hash.TIntHashSet;
18 public class EListComprehension extends SimplifiableExpression {
20 public Expression head;
21 public ListQualifier qualifier;
23 public EListComprehension(Expression head, ListQualifier qualifier) {
25 this.qualifier = qualifier;
28 public EListComprehension(long loc, Expression head,
29 ListQualifier qualifier) {
32 this.qualifier = qualifier;
36 public void collectRefs(TObjectIntHashMap<Object> allRefs,
38 head.collectRefs(allRefs, refs);
39 qualifier.collectRefs(allRefs, refs);
43 public void collectVars(TObjectIntHashMap<Variable> allVars,
45 head.collectVars(allVars, vars);
46 qualifier.collectVars(allVars, vars);
50 public Expression checkBasicType(TypingContext context, Type requiredType) {
51 qualifier.checkType(context);
54 componentType = Types.unifyApply(Types.LIST, requiredType);
55 } catch (MatchException e) {
56 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
57 return new EError(location);
59 head = head.checkType(context, componentType);
64 protected void updateType() throws MatchException {
65 setType(Types.list(head.getType()));
69 public void collectFreeVariables(THashSet<Variable> vars) {
70 head.collectFreeVariables(vars);
71 qualifier.collectFreeVariables(vars);
75 public Expression simplify(SimplificationContext context) {
76 context.pushLocation(location);
78 CompiledQualifier cq = qualifier.compile(context);
79 Expression exp = context.mapList(
80 context.lambda(cq.pattern, head),
82 //System.out.println("simplified: " + exp);
84 exp.validateType(context.getEnvironment());
85 } catch (TypeValidationException e) {
86 throw new InternalCompilerError(e);
88 exp = exp.simplify(context);
91 context.popLocation();
96 public Expression resolve(TranslationContext context) {
98 qualifier.resolve(context);
99 head = head.resolve(context);
105 public Expression decorate(ExpressionDecorator decorator) {
106 head = head.decorate(decorator);
107 qualifier.decorate(decorator);
108 return decorator.decorate(this);
112 public void collectEffects(THashSet<Type> effects) {
113 head.collectEffects(effects);
114 qualifier.collectEffects(effects);
118 public void setLocationDeep(long loc) {
119 if(location == Locations.NO_LOCATION) {
121 head.setLocationDeep(loc);
122 qualifier.setLocationDeep(loc);
127 public void accept(ExpressionVisitor visitor) {
132 public void forVariables(VariableProcedure procedure) {
133 head.forVariables(procedure);
134 qualifier.forVariables(procedure);
138 public Expression accept(ExpressionTransformer transformer) {
139 return transformer.transform(this);