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 collectRefs(TObjectIntHashMap<Object> allRefs,
43 for(Expression component : components)
44 component.collectRefs(allRefs, refs);
48 public void collectVars(TObjectIntHashMap<Variable> allVars,
50 for(Expression component : components)
51 component.collectVars(allVars, vars);
55 public void collectFreeVariables(THashSet<Variable> vars) {
56 for(Expression component : components)
57 component.collectFreeVariables(vars);
61 public Expression simplify(SimplificationContext context) {
62 context.pushLocation(location);
64 for(int i=0;i<components.length;++i)
65 components[i] = components[i].simplify(context);
67 if(components.length <= Constants.MAX_LIST_LITERAL_LENGTH) {
68 Expression result = new EConstant(location, Builtins.LIST_CONSTRUCTORS[components.length],
70 if(components.length > 0)
71 result = new EApply(location, result, components);
75 Expression result = new EApplyType(new ELiteral(location, new ListConstructor(components.length)), componentType);
76 result = new EApply(location, result, components);
80 context.popLocation();
85 public Expression resolve(TranslationContext context) {
86 for(int i=0;i<components.length;++i)
87 components[i] = components[i].resolve(context);
92 public Expression resolveAsPattern(TranslationContext context) {
93 for(int i=0;i<components.length;++i)
94 components[i] = components[i].resolveAsPattern(context);
99 protected void updateType() throws MatchException {
100 setType(Types.list(componentType));
104 public Expression checkBasicType(TypingContext context, Type requiredType) {
106 componentType = Types.unifyApply(Types.LIST, requiredType);
107 } catch (MatchException e) {
108 context.getErrorLog().log(location, "Expected a value with type " + requiredType + " but got a list.");
109 return new EError(location);
111 for(int i=0;i<components.length;++i)
112 components[i] = components[i].checkType(context, componentType);
117 public void collectEffects(THashSet<Type> effects) {
118 for(Expression component : components)
119 component.collectEffects(effects);
123 public void setLocationDeep(long loc) {
124 if(location == Locations.NO_LOCATION) {
126 for(Expression component : components)
127 component.setLocationDeep(loc);
132 public void accept(ExpressionVisitor visitor) {
137 public IExpression toIExpression(ExpressionInterpretationContext target) {
138 IExpression[] componentExpressions = new IExpression[components.length];
139 for(int i=0;i<components.length;++i)
140 componentExpressions[i] = components[i].toIExpression(target);
141 return new IListLiteral(componentExpressions);
145 public void forVariables(VariableProcedure procedure) {
146 for(Expression component : components)
147 component.forVariables(procedure);
151 public Expression replace(ReplaceContext context) {
152 Expression[] newComponents = new Expression[components.length];
153 for(int i=0;i<components.length;++i)
154 newComponents[i] = components[i].replace(context);
155 return new EListLiteral(newComponents, componentType.replace(context.tvarMap));
159 public boolean isPattern(int arity) {
162 for(Expression component : components)
163 if(!component.isPattern(0))
169 public Expression accept(ExpressionTransformer transformer) {
170 return transformer.transform(this);