1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
4 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
7 import org.simantics.scl.compiler.elaboration.java.Builtins;
8 import org.simantics.scl.compiler.elaboration.java.ListConstructor;
9 import org.simantics.scl.compiler.errors.Locations;
10 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
11 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
12 import org.simantics.scl.compiler.internal.interpreted.IExpression;
13 import org.simantics.scl.compiler.internal.interpreted.IListLiteral;
14 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.compiler.types.Types;
17 import org.simantics.scl.compiler.types.exceptions.MatchException;
19 import gnu.trove.map.hash.TObjectIntHashMap;
20 import gnu.trove.set.hash.THashSet;
21 import gnu.trove.set.hash.TIntHashSet;
23 public class EListLiteral extends SimplifiableExpression {
25 Expression[] components;
28 public EListLiteral(Expression[] components) {
29 this.components = components;
32 private EListLiteral(Expression[] components, Type componentType) {
33 this.components = components;
34 this.componentType = componentType;
37 public Expression[] getComponents() {
42 public void collectRefs(TObjectIntHashMap<Object> allRefs,
44 for(Expression component : components)
45 component.collectRefs(allRefs, refs);
49 public void collectVars(TObjectIntHashMap<Variable> allVars,
51 for(Expression component : components)
52 component.collectVars(allVars, vars);
56 public void collectFreeVariables(THashSet<Variable> vars) {
57 for(Expression component : components)
58 component.collectFreeVariables(vars);
62 public Expression simplify(SimplificationContext context) {
63 context.pushLocation(location);
65 for(int i=0;i<components.length;++i)
66 components[i] = components[i].simplify(context);
68 if(components.length <= Constants.MAX_LIST_LITERAL_LENGTH) {
69 Expression result = new EConstant(location, Builtins.LIST_CONSTRUCTORS[components.length],
71 if(components.length > 0)
72 result = new EApply(location, result, components);
76 Expression result = new EApplyType(new ELiteral(location, new ListConstructor(components.length)), componentType);
77 result = new EApply(location, result, components);
81 context.popLocation();
86 public Expression resolve(TranslationContext context) {
87 for(int i=0;i<components.length;++i)
88 components[i] = components[i].resolve(context);
93 public Expression resolveAsPattern(TranslationContext context) {
94 for(int i=0;i<components.length;++i)
95 components[i] = components[i].resolveAsPattern(context);
100 protected void updateType() throws MatchException {
101 setType(Types.list(componentType));
105 public Expression checkBasicType(TypingContext context, Type requiredType) {
107 componentType = Types.unifyApply(Types.LIST, requiredType);
108 } catch (MatchException e) {
109 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
110 return new EError(location);
112 for(int i=0;i<components.length;++i)
113 components[i] = components[i].checkType(context, componentType);
118 public Expression decorate(ExpressionDecorator decorator) {
119 for(int i=0;i<components.length;++i)
120 components[i] = components[i].decorate(decorator);
121 return decorator.decorate(this);
125 public void collectEffects(THashSet<Type> effects) {
126 for(Expression component : components)
127 component.collectEffects(effects);
131 public void setLocationDeep(long loc) {
132 if(location == Locations.NO_LOCATION) {
134 for(Expression component : components)
135 component.setLocationDeep(loc);
140 public void accept(ExpressionVisitor visitor) {
145 public IExpression toIExpression(ExpressionInterpretationContext target) {
146 IExpression[] componentExpressions = new IExpression[components.length];
147 for(int i=0;i<components.length;++i)
148 componentExpressions[i] = components[i].toIExpression(target);
149 return new IListLiteral(componentExpressions);
153 public void forVariables(VariableProcedure procedure) {
154 for(Expression component : components)
155 component.forVariables(procedure);
159 public Expression replace(ReplaceContext context) {
160 Expression[] newComponents = new Expression[components.length];
161 for(int i=0;i<components.length;++i)
162 newComponents[i] = components[i].replace(context);
163 return new EListLiteral(newComponents, componentType.replace(context.tvarMap));
167 public boolean isPattern(int arity) {
170 for(Expression component : components)
171 if(!component.isPattern(0))
177 public Expression accept(ExpressionTransformer transformer) {
178 return transformer.transform(this);