1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.common.names.Name;
6 import org.simantics.scl.compiler.common.precedence.Precedence;
7 import org.simantics.scl.compiler.constants.Constant;
8 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
9 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
10 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
11 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
12 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
13 import org.simantics.scl.compiler.elaboration.expressions.lhstype.LhsType;
14 import org.simantics.scl.compiler.elaboration.expressions.lhstype.PatternMatchingLhs;
15 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
16 import org.simantics.scl.compiler.environment.Environment;
17 import org.simantics.scl.compiler.errors.Locations;
18 import org.simantics.scl.compiler.internal.codegen.references.IVal;
19 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
20 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
21 import org.simantics.scl.compiler.internal.interpreted.IConstant;
22 import org.simantics.scl.compiler.internal.interpreted.IExpression;
23 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
24 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
25 import org.simantics.scl.compiler.top.ValueNotFound;
26 import org.simantics.scl.compiler.types.TForAll;
27 import org.simantics.scl.compiler.types.TMetaVar;
28 import org.simantics.scl.compiler.types.Type;
29 import org.simantics.scl.compiler.types.Types;
30 import org.simantics.scl.compiler.types.exceptions.MatchException;
31 import org.simantics.scl.compiler.types.util.MultiFunction;
32 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
34 import gnu.trove.map.hash.TObjectIntHashMap;
35 import gnu.trove.set.hash.THashSet;
36 import gnu.trove.set.hash.TIntHashSet;
38 public class EConstant extends Expression {
40 Type[] typeParameters;
42 public EConstant(SCLValue value, Type ... typeParameters) {
43 if(SCLCompilerConfiguration.DEBUG)
45 throw new NullPointerException();
47 this.typeParameters = typeParameters;
50 public EConstant(SCLValue value) {
51 if(SCLCompilerConfiguration.DEBUG)
53 throw new NullPointerException();
55 this.typeParameters = Type.EMPTY_ARRAY;
58 public EConstant(long loc, SCLValue value) {
60 if(SCLCompilerConfiguration.DEBUG)
62 throw new NullPointerException();
64 this.typeParameters = Type.EMPTY_ARRAY;
67 public EConstant(long loc, SCLValue value, Type ... typeParameters) {
69 if(SCLCompilerConfiguration.DEBUG)
71 throw new NullPointerException();
73 this.typeParameters = typeParameters;
76 public void addTypeParameters(Type ... newTypeParameters) {
77 typeParameters = Types.concat(typeParameters, newTypeParameters);
80 public Expression applyType(Type type) {
81 typeParameters = Types.concat(typeParameters, new Type[] {type});
83 setType(Types.instantiate(getType(), type));
87 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
88 int id = allRefs.get(value);
94 public void collectVars(TObjectIntHashMap<Variable> allVars,
98 public void toString(StringBuilder b, TypeUnparsingContext tuc) {
99 Name name = value.getName();
100 if(name.module.equals("Builtin") || name.module.equals("Prelude"))
104 /*for(Type type : typeParameters) {
106 b.append(type.toString(tuc));
112 protected void updateType() throws MatchException {
113 setType(Types.instantiate(value.getType(), typeParameters));
117 public IVal toVal(Environment env, CodeWriter w) {
118 IVal val = value.getValue();
119 if(typeParameters.length > 0) {
120 val = val.createSpecialization(typeParameters);
126 public void collectFreeVariables(THashSet<Variable> vars) {
130 public Expression simplify(SimplificationContext context) {
131 if(value.getInlineInSimplification()) {
132 if(typeParameters.length > 0) {
133 context.getErrorLog().log(location,
134 "Inlining with type parameters not currently supported in simplification.");
138 return value.getExpression().copy().simplify(context);
144 public Expression resolve(TranslationContext context) {
149 public void getParameters(TranslationContext translationContext,
150 ArrayList<Expression> parameters) {
153 public SCLValue getValue() {
158 public Expression resolveAsPattern(TranslationContext context) {
163 public void removeFreeVariables(THashSet<Variable> vars) {
167 public Expression replace(ReplaceContext context) {
168 Type[] newTypeParameters;
169 if(typeParameters.length == 0)
170 newTypeParameters = Type.EMPTY_ARRAY;
172 newTypeParameters = new Type[typeParameters.length];
173 for(int i=0;i<newTypeParameters.length;++i)
174 newTypeParameters[i] = typeParameters[i].replace(context.tvarMap);
176 return new EConstant(value, newTypeParameters);
179 public Type[] getTypeParameters() {
180 return typeParameters;
184 public LhsType getLhsType() throws NotPatternException {
185 return new PatternMatchingLhs();
189 public IExpression toIExpression(ExpressionInterpretationContext target) {
190 Name name = value.getName();
192 return new IConstant(target.runtimeEnvironment.getRuntimeModule(name.module).getValue(name.name));
193 } catch (ValueNotFound e) {
194 throw new UnsupportedOperationException();
199 public Expression inferType(TypingContext context) {
200 if(context.recursiveValues != null &&
201 context.recursiveValues.contains(value)) {
202 // Handles the case where the constant is one of the recursive definitions we are currently checking
203 // This kind of value is not yet generalized, i.e. it is not necessary to instantiate it.
204 EPlaceholder placeholder = new EPlaceholder(location, this);
205 placeholder.setType(value.getType());
207 context.recursiveReferences.add(placeholder);
210 else if(context.isInPattern()) {
211 /* This is little hackish code that handles the following kind of constructors:
212 * data Thunk a = Thunk s (a -> s)
214 * match thunk with Thunk s f -> f s
215 * We cannot assign s with an unbound metaVar because its type depends on
216 * how it has been constructed. Therefore we parametrize the function with
217 * existential variable.
219 Type resultType = value.getType();
220 if(resultType instanceof TForAll) {
221 ArrayList<TMetaVar> vars = new ArrayList<TMetaVar>();
222 resultType = Types.instantiate(resultType, vars);
223 MultiFunction mfun = Types.matchFunction(resultType);
224 resultType = mfun.returnType;
226 for(TMetaVar var : vars) {
227 if(resultType.contains(var))
229 addTypeParameters(Types.var(var.getKind()));
235 return applyPUnit(context);
239 public Expression decorate(ExpressionDecorator decorator) {
240 return decorator.decorate(this);
244 public boolean isEffectful() {
249 public void collectEffects(THashSet<Type> effects) {
253 public void setLocationDeep(long loc) {
254 if(location == Locations.NO_LOCATION)
259 public void accept(ExpressionVisitor visitor) {
264 public Precedence getPrecedence() {
265 return value.getPrecedence();
269 public void forVariables(VariableProcedure procedure) {
273 public boolean isPattern(int arity) {
274 IVal val = value.getValue();
275 if(!(val instanceof Constant))
277 Constant constant = (Constant)val;
278 return constant.constructorTag() >= 0 && constant.getArity() == arity;
282 public Expression accept(ExpressionTransformer transformer) {
283 return transformer.transform(this);
287 public boolean equalsExpression(Expression expression) {
288 if(expression.getClass() != getClass())
290 EConstant other = (EConstant)expression;
291 return value == other.value && Types.equals(typeParameters, other.typeParameters);