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