1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
4 import java.util.Collections;
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;
35 public class EConstant extends Expression {
36 public SCLValue value;
37 Type[] typeParameters;
39 public EConstant(SCLValue value, Type ... typeParameters) {
40 if(SCLCompilerConfiguration.DEBUG)
42 throw new NullPointerException();
44 this.typeParameters = typeParameters;
47 public EConstant(SCLValue value) {
48 if(SCLCompilerConfiguration.DEBUG)
50 throw new NullPointerException();
52 this.typeParameters = Type.EMPTY_ARRAY;
55 public EConstant(long loc, SCLValue value) {
57 if(SCLCompilerConfiguration.DEBUG)
59 throw new NullPointerException();
61 this.typeParameters = Type.EMPTY_ARRAY;
64 public EConstant(long loc, SCLValue value, Type ... typeParameters) {
66 if(SCLCompilerConfiguration.DEBUG)
68 throw new NullPointerException();
70 this.typeParameters = typeParameters;
73 public void addTypeParameters(Type ... newTypeParameters) {
74 typeParameters = Types.concat(typeParameters, newTypeParameters);
77 public Expression applyType(Type type) {
78 typeParameters = Types.concat(typeParameters, new Type[] {type});
80 setType(Types.instantiate(getType(), type));
85 public Set<Variable> getFreeVariables() {
86 return Collections.emptySet();
89 public void toString(StringBuilder b, TypeUnparsingContext tuc) {
90 Name name = value.getName();
91 if(name.module.equals("Builtin") || name.module.equals("Prelude"))
95 /*for(Type type : typeParameters) {
97 b.append(type.toString(tuc));
103 protected void updateType() throws MatchException {
104 setType(Types.instantiate(value.getType(), typeParameters));
108 public IVal toVal(CompilationContext context, CodeWriter w) {
109 IVal val = value.getValue();
110 if(typeParameters.length > 0) {
111 val = val.createSpecialization(typeParameters);
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.");
125 return value.getExpression().copy().simplify(context);
131 public Expression resolve(TranslationContext context) {
136 public void getParameters(TranslationContext translationContext,
137 ArrayList<Expression> parameters) {
140 public SCLValue getValue() {
145 public Expression resolveAsPattern(TranslationContext context) {
150 public Expression replace(ReplaceContext context) {
151 Type[] newTypeParameters;
152 if(typeParameters.length == 0)
153 newTypeParameters = Type.EMPTY_ARRAY;
155 newTypeParameters = new Type[typeParameters.length];
156 for(int i=0;i<newTypeParameters.length;++i)
157 newTypeParameters[i] = typeParameters[i].replace(context.tvarMap);
159 return new EConstant(value, newTypeParameters);
162 public Type[] getTypeParameters() {
163 return typeParameters;
167 public LhsType getLhsType() throws NotPatternException {
168 return new PatternMatchingLhs();
172 public IExpression toIExpression(ExpressionInterpretationContext target) {
173 Name name = value.getName();
175 return new IConstant(target.runtimeEnvironment.getRuntimeModule(name.module).getValue(name.name));
176 } catch (ValueNotFound e) {
177 throw new UnsupportedOperationException();
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());
190 context.recursiveReferences.add(placeholder);
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)
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.
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;
209 for(TMetaVar var : vars) {
210 if(resultType.contains(var))
212 addTypeParameters(Types.var(var.getKind()));
218 return applyPUnit(context);
222 public boolean isEffectful() {
227 public void setLocationDeep(long loc) {
228 if(location == Locations.NO_LOCATION)
233 public void accept(ExpressionVisitor visitor) {
238 public Precedence getPrecedence() {
239 return value.getPrecedence();
243 public boolean isPattern(int arity) {
244 IVal val = value.getValue();
245 if(!(val instanceof Constant))
247 Constant constant = (Constant)val;
248 return constant.constructorTag() >= 0 && constant.getArity() == arity;
252 public Expression accept(ExpressionTransformer transformer) {
253 return transformer.transform(this);
257 public boolean equalsExpression(Expression expression) {
258 if(expression.getClass() != getClass())
260 EConstant other = (EConstant)expression;
261 return value == other.value && Types.equals(typeParameters, other.typeParameters);