]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EConstant.java
(refs #7375) Replaced collectVars method by a visitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EConstant.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Set;
6
7 import org.simantics.scl.compiler.common.names.Name;
8 import org.simantics.scl.compiler.common.precedence.Precedence;
9 import org.simantics.scl.compiler.compilation.CompilationContext;
10 import org.simantics.scl.compiler.constants.Constant;
11 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
12 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
13 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
14 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
15 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
16 import org.simantics.scl.compiler.elaboration.expressions.lhstype.LhsType;
17 import org.simantics.scl.compiler.elaboration.expressions.lhstype.PatternMatchingLhs;
18 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
19 import org.simantics.scl.compiler.errors.Locations;
20 import org.simantics.scl.compiler.internal.codegen.references.IVal;
21 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
22 import org.simantics.scl.compiler.internal.interpreted.IConstant;
23 import org.simantics.scl.compiler.internal.interpreted.IExpression;
24 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
25 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
26 import org.simantics.scl.compiler.top.ValueNotFound;
27 import org.simantics.scl.compiler.types.TForAll;
28 import org.simantics.scl.compiler.types.TMetaVar;
29 import org.simantics.scl.compiler.types.Type;
30 import org.simantics.scl.compiler.types.Types;
31 import org.simantics.scl.compiler.types.exceptions.MatchException;
32 import org.simantics.scl.compiler.types.util.MultiFunction;
33 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
34
35 public class EConstant extends Expression {
36     public SCLValue value;
37     Type[] typeParameters;
38     
39     public EConstant(SCLValue value, Type ... typeParameters) {
40         if(SCLCompilerConfiguration.DEBUG)
41             if(value == null)
42                 throw new NullPointerException();
43         this.value = value;
44         this.typeParameters = typeParameters;
45     }
46
47     public EConstant(SCLValue value) {
48         if(SCLCompilerConfiguration.DEBUG)
49             if(value == null)
50                 throw new NullPointerException();
51         this.value = value;
52         this.typeParameters = Type.EMPTY_ARRAY;
53     }
54
55     public EConstant(long loc, SCLValue value) {
56         super(loc);
57         if(SCLCompilerConfiguration.DEBUG)
58             if(value == null)
59                 throw new NullPointerException();
60         this.value = value;
61         this.typeParameters = Type.EMPTY_ARRAY;
62     }
63     
64     public EConstant(long loc, SCLValue value, Type ... typeParameters) {
65         super(loc);
66         if(SCLCompilerConfiguration.DEBUG)
67             if(value == null)
68                 throw new NullPointerException();
69         this.value = value;
70         this.typeParameters = typeParameters;
71     }
72
73     public void addTypeParameters(Type ... newTypeParameters) {
74         typeParameters = Types.concat(typeParameters, newTypeParameters);
75     }
76     
77     public Expression applyType(Type type) {
78         typeParameters = Types.concat(typeParameters, new Type[] {type});
79         if(getType() != null)
80             setType(Types.instantiate(getType(), type));
81         return this;
82     }
83     
84     @Override
85     public Set<Variable> getFreeVariables() {
86         return Collections.emptySet();
87     }
88
89     public void toString(StringBuilder b, TypeUnparsingContext tuc) {
90         Name name = value.getName();
91         if(name.module.equals("Builtin") || name.module.equals("Prelude"))
92             b.append(name.name);
93         else
94             b.append(name);
95         /*for(Type type : typeParameters) {
96             b.append(" <");
97             b.append(type.toString(tuc));
98             b.append(">");
99         }*/
100     }
101
102     @Override
103     protected void updateType() throws MatchException {
104         setType(Types.instantiate(value.getType(), typeParameters));
105     }
106     
107     @Override
108     public IVal toVal(CompilationContext context, CodeWriter w) {
109         IVal val = value.getValue();            
110         if(typeParameters.length > 0) {
111             val = val.createSpecialization(typeParameters);
112         }
113         return val;
114     }
115
116     @Override
117     public Expression simplify(SimplificationContext context) {
118         if(value.getInlineInSimplification()) {
119             if(typeParameters.length > 0) {
120                 context.getErrorLog().log(location, 
121                         "Inlining with type parameters not currently supported in simplification.");
122                 return this;
123             }
124             else
125                 return value.getExpression().copy().simplify(context);
126         }
127         return this;
128     }
129
130     @Override
131     public Expression resolve(TranslationContext context) {
132         return this;
133     }
134     
135     @Override
136     public void getParameters(TranslationContext translationContext,
137             ArrayList<Expression> parameters) {
138     }
139     
140     public SCLValue getValue() {
141         return value;
142     }
143     
144     @Override
145     public Expression resolveAsPattern(TranslationContext context) {
146         return this;
147     }
148
149     @Override
150     public Expression replace(ReplaceContext context) {
151         Type[] newTypeParameters;
152         if(typeParameters.length == 0)
153             newTypeParameters = Type.EMPTY_ARRAY;
154         else {
155             newTypeParameters = new Type[typeParameters.length];
156             for(int i=0;i<newTypeParameters.length;++i)
157                 newTypeParameters[i] = typeParameters[i].replace(context.tvarMap);
158         }
159         return new EConstant(value, newTypeParameters);
160     }
161     
162     public Type[] getTypeParameters() {
163         return typeParameters;
164     }
165
166     @Override
167     public LhsType getLhsType() throws NotPatternException {
168         return new PatternMatchingLhs();
169     }
170     
171     @Override
172     public IExpression toIExpression(ExpressionInterpretationContext target) {
173         Name name = value.getName();
174         try {
175             return new IConstant(target.runtimeEnvironment.getRuntimeModule(name.module).getValue(name.name));
176         } catch (ValueNotFound e) {
177             throw new UnsupportedOperationException();
178         }
179     }
180     
181     @Override
182     public Expression inferType(TypingContext context) {
183         if(context.recursiveValues != null &&
184                 context.recursiveValues.contains(value)) {
185             // Handles the case where the constant is one of the recursive definitions we are currently checking
186             // This kind of value is not yet generalized, i.e. it is not necessary to instantiate it.
187             EPlaceholder placeholder = new EPlaceholder(location, this);
188             placeholder.setType(value.getType());
189             
190             context.recursiveReferences.add(placeholder);
191             return placeholder;
192         }
193         else if(context.isInPattern()) {
194             /* This is little hackish code that handles the following kind of constructors:
195              *   data Thunk a = Thunk s (a -> s)
196              * in
197              *   match thunk with Thunk s f -> f s
198              * We cannot assign s with an unbound metaVar because its type depends on 
199              * how it has been constructed. Therefore we parametrize the function with
200              * existential variable.
201              */
202             Type resultType = value.getType();
203             if(resultType instanceof TForAll) {
204                 ArrayList<TMetaVar> vars = new ArrayList<TMetaVar>(); 
205                 resultType = Types.instantiate(resultType, vars);
206                 MultiFunction mfun = Types.matchFunction(resultType);
207                 resultType = mfun.returnType;
208                 
209                 for(TMetaVar var : vars) {
210                     if(resultType.contains(var))
211                         break;
212                     addTypeParameters(Types.var(var.getKind()));                    
213                 }
214             }
215             return this;
216         }
217         else
218             return applyPUnit(context);
219     }
220     
221     @Override
222     public boolean isEffectful() {
223         return false;
224     }
225     
226     @Override
227     public void setLocationDeep(long loc) {
228         if(location == Locations.NO_LOCATION)
229             location = loc;
230     }
231     
232     @Override
233     public void accept(ExpressionVisitor visitor) {
234         visitor.visit(this);
235     }
236     
237     @Override
238     public Precedence getPrecedence() {
239         return value.getPrecedence();
240     }
241     
242     @Override
243     public boolean isPattern(int arity) {
244         IVal val = value.getValue();
245         if(!(val instanceof Constant))
246             return false;
247         Constant constant = (Constant)val;
248         return constant.constructorTag() >= 0 && constant.getArity() == arity;
249     }
250     
251     @Override
252     public Expression accept(ExpressionTransformer transformer) {
253         return transformer.transform(this);
254     }
255
256     @Override
257     public boolean equalsExpression(Expression expression) {
258         if(expression.getClass() != getClass())
259             return false;
260         EConstant other = (EConstant)expression;
261         return value == other.value && Types.equals(typeParameters, other.typeParameters);
262     }
263 }