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.interpreted.IExpression;
12 import org.simantics.scl.compiler.internal.interpreted.IListLiteral;
13 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.compiler.types.Types;
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
22 public class EListLiteral extends SimplifiableExpression {
24 Expression[] components;
27 public EListLiteral(Expression[] components) {
28 this.components = components;
31 private EListLiteral(Expression[] components, Type componentType) {
32 this.components = components;
33 this.componentType = componentType;
36 public Expression[] getComponents() {
41 public void collectVars(TObjectIntHashMap<Variable> allVars,
43 for(Expression component : components)
44 component.collectVars(allVars, vars);
48 public void collectFreeVariables(THashSet<Variable> vars) {
49 for(Expression component : components)
50 component.collectFreeVariables(vars);
54 public Expression simplify(SimplificationContext context) {
55 context.pushLocation(location);
57 for(int i=0;i<components.length;++i)
58 components[i] = components[i].simplify(context);
60 if(components.length <= Constants.MAX_LIST_LITERAL_LENGTH) {
61 Expression result = new EConstant(location, Builtins.LIST_CONSTRUCTORS[components.length],
63 if(components.length > 0)
64 result = new EApply(location, result, components);
68 Expression result = new EApplyType(new ELiteral(location, new ListConstructor(components.length)), componentType);
69 result = new EApply(location, result, components);
73 context.popLocation();
78 public Expression resolve(TranslationContext context) {
79 for(int i=0;i<components.length;++i)
80 components[i] = components[i].resolve(context);
85 public Expression resolveAsPattern(TranslationContext context) {
86 for(int i=0;i<components.length;++i)
87 components[i] = components[i].resolveAsPattern(context);
92 protected void updateType() throws MatchException {
93 setType(Types.list(componentType));
97 public Expression checkBasicType(TypingContext context, Type requiredType) {
99 componentType = Types.unifyApply(Types.LIST, requiredType);
100 } catch (MatchException e) {
101 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
102 return new EError(location);
104 for(int i=0;i<components.length;++i)
105 components[i] = components[i].checkType(context, componentType);
110 public void collectEffects(THashSet<Type> effects) {
111 for(Expression component : components)
112 component.collectEffects(effects);
116 public void setLocationDeep(long loc) {
117 if(location == Locations.NO_LOCATION) {
119 for(Expression component : components)
120 component.setLocationDeep(loc);
125 public void accept(ExpressionVisitor visitor) {
130 public IExpression toIExpression(ExpressionInterpretationContext target) {
131 IExpression[] componentExpressions = new IExpression[components.length];
132 for(int i=0;i<components.length;++i)
133 componentExpressions[i] = components[i].toIExpression(target);
134 return new IListLiteral(componentExpressions);
138 public Expression replace(ReplaceContext context) {
139 Expression[] newComponents = new Expression[components.length];
140 for(int i=0;i<components.length;++i)
141 newComponents[i] = components[i].replace(context);
142 return new EListLiteral(newComponents, componentType.replace(context.tvarMap));
146 public boolean isPattern(int arity) {
149 for(Expression component : components)
150 if(!component.isPattern(0))
156 public Expression accept(ExpressionTransformer transformer) {
157 return transformer.transform(this);