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 public class EListLiteral extends SimplifiableExpression {
20 public Expression[] components;
23 public EListLiteral(Expression[] components) {
24 this.components = components;
27 private EListLiteral(Expression[] components, Type componentType) {
28 this.components = components;
29 this.componentType = componentType;
32 public Expression[] getComponents() {
37 public Expression simplify(SimplificationContext context) {
38 context.pushLocation(location);
40 for(int i=0;i<components.length;++i)
41 components[i] = components[i].simplify(context);
43 if(components.length <= Constants.MAX_LIST_LITERAL_LENGTH) {
44 Expression result = new EConstant(location, Builtins.LIST_CONSTRUCTORS[components.length],
46 if(components.length > 0)
47 result = new EApply(location, result, components);
51 Expression result = new EApplyType(new ELiteral(location, new ListConstructor(components.length)), componentType);
52 result = new EApply(location, result, components);
56 context.popLocation();
61 public Expression resolve(TranslationContext context) {
62 for(int i=0;i<components.length;++i)
63 components[i] = components[i].resolve(context);
68 public Expression resolveAsPattern(TranslationContext context) {
69 for(int i=0;i<components.length;++i)
70 components[i] = components[i].resolveAsPattern(context);
75 protected void updateType() throws MatchException {
76 setType(Types.list(componentType));
80 public Expression checkBasicType(TypingContext context, Type requiredType) {
82 componentType = Types.unifyApply(Types.LIST, requiredType);
83 } catch (MatchException e) {
84 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
85 return new EError(location);
87 for(int i=0;i<components.length;++i)
88 components[i] = components[i].checkType(context, componentType);
93 public void setLocationDeep(long loc) {
94 if(location == Locations.NO_LOCATION) {
96 for(Expression component : components)
97 component.setLocationDeep(loc);
102 public void accept(ExpressionVisitor visitor) {
107 public IExpression toIExpression(ExpressionInterpretationContext target) {
108 IExpression[] componentExpressions = new IExpression[components.length];
109 for(int i=0;i<components.length;++i)
110 componentExpressions[i] = components[i].toIExpression(target);
111 return new IListLiteral(componentExpressions);
115 public Expression replace(ReplaceContext context) {
116 Expression[] newComponents = new Expression[components.length];
117 for(int i=0;i<components.length;++i)
118 newComponents[i] = components[i].replace(context);
119 return new EListLiteral(newComponents, componentType.replace(context.tvarMap));
123 public boolean isPattern(int arity) {
126 for(Expression component : components)
127 if(!component.isPattern(0))
133 public Expression accept(ExpressionTransformer transformer) {
134 return transformer.transform(this);