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.TIntHashSet;
21 public class EListLiteral extends SimplifiableExpression {
23 public Expression[] components;
26 public EListLiteral(Expression[] components) {
27 this.components = components;
30 private EListLiteral(Expression[] components, Type componentType) {
31 this.components = components;
32 this.componentType = componentType;
35 public Expression[] getComponents() {
40 public void collectVars(TObjectIntHashMap<Variable> allVars,
42 for(Expression component : components)
43 component.collectVars(allVars, vars);
47 public Expression simplify(SimplificationContext context) {
48 context.pushLocation(location);
50 for(int i=0;i<components.length;++i)
51 components[i] = components[i].simplify(context);
53 if(components.length <= Constants.MAX_LIST_LITERAL_LENGTH) {
54 Expression result = new EConstant(location, Builtins.LIST_CONSTRUCTORS[components.length],
56 if(components.length > 0)
57 result = new EApply(location, result, components);
61 Expression result = new EApplyType(new ELiteral(location, new ListConstructor(components.length)), componentType);
62 result = new EApply(location, result, components);
66 context.popLocation();
71 public Expression resolve(TranslationContext context) {
72 for(int i=0;i<components.length;++i)
73 components[i] = components[i].resolve(context);
78 public Expression resolveAsPattern(TranslationContext context) {
79 for(int i=0;i<components.length;++i)
80 components[i] = components[i].resolveAsPattern(context);
85 protected void updateType() throws MatchException {
86 setType(Types.list(componentType));
90 public Expression checkBasicType(TypingContext context, Type requiredType) {
92 componentType = Types.unifyApply(Types.LIST, requiredType);
93 } catch (MatchException e) {
94 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
95 return new EError(location);
97 for(int i=0;i<components.length;++i)
98 components[i] = components[i].checkType(context, componentType);
103 public void setLocationDeep(long loc) {
104 if(location == Locations.NO_LOCATION) {
106 for(Expression component : components)
107 component.setLocationDeep(loc);
112 public void accept(ExpressionVisitor visitor) {
117 public IExpression toIExpression(ExpressionInterpretationContext target) {
118 IExpression[] componentExpressions = new IExpression[components.length];
119 for(int i=0;i<components.length;++i)
120 componentExpressions[i] = components[i].toIExpression(target);
121 return new IListLiteral(componentExpressions);
125 public Expression replace(ReplaceContext context) {
126 Expression[] newComponents = new Expression[components.length];
127 for(int i=0;i<components.length;++i)
128 newComponents[i] = components[i].replace(context);
129 return new EListLiteral(newComponents, componentType.replace(context.tvarMap));
133 public boolean isPattern(int arity) {
136 for(Expression component : components)
137 if(!component.isPattern(0))
143 public Expression accept(ExpressionTransformer transformer) {
144 return transformer.transform(this);