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.java.DynamicConstructor;
19 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
20 import org.simantics.scl.compiler.errors.Locations;
21 import org.simantics.scl.compiler.internal.codegen.references.IVal;
22 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
23 import org.simantics.scl.compiler.internal.interpreted.IConstant;
24 import org.simantics.scl.compiler.internal.interpreted.IExpression;
25 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
26 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
27 import org.simantics.scl.compiler.top.ValueNotFound;
28 import org.simantics.scl.compiler.types.TForAll;
29 import org.simantics.scl.compiler.types.TMetaVar;
30 import org.simantics.scl.compiler.types.Type;
31 import org.simantics.scl.compiler.types.Types;
32 import org.simantics.scl.compiler.types.exceptions.MatchException;
33 import org.simantics.scl.compiler.types.util.MultiFunction;
34 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
36 public class EConstant extends Expression {
37 public SCLValue value;
38 Type[] typeParameters;
40 public EConstant(SCLValue value, Type ... typeParameters) {
41 if(SCLCompilerConfiguration.DEBUG)
43 throw new NullPointerException();
45 this.typeParameters = typeParameters;
48 public EConstant(SCLValue value) {
49 if(SCLCompilerConfiguration.DEBUG)
51 throw new NullPointerException();
53 this.typeParameters = Type.EMPTY_ARRAY;
56 public EConstant(long loc, SCLValue value) {
58 if(SCLCompilerConfiguration.DEBUG)
60 throw new NullPointerException();
62 this.typeParameters = Type.EMPTY_ARRAY;
65 public EConstant(long loc, SCLValue value, Type ... typeParameters) {
67 if(SCLCompilerConfiguration.DEBUG)
69 throw new NullPointerException();
71 this.typeParameters = typeParameters;
74 public void addTypeParameters(Type ... newTypeParameters) {
75 typeParameters = Types.concat(typeParameters, newTypeParameters);
78 public Expression applyType(Type type) {
79 typeParameters = Types.concat(typeParameters, new Type[] {type});
81 setType(Types.instantiate(getType(), type));
86 public Set<Variable> getFreeVariables() {
87 return Collections.emptySet();
90 public void toString(StringBuilder b, TypeUnparsingContext tuc) {
91 Name name = value.getName();
92 if(name.module.equals("Builtin") || name.module.equals("Prelude"))
96 /*for(Type type : typeParameters) {
98 b.append(type.toString(tuc));
104 protected void updateType() throws MatchException {
105 setType(Types.instantiate(value.getType(), typeParameters));
109 public IVal toVal(CompilationContext context, CodeWriter w) {
110 IVal val = value.getValue();
111 if(typeParameters.length > 0) {
112 val = val.createSpecialization(typeParameters);
118 public Expression simplify(SimplificationContext context) {
119 if(value.getInlineInSimplification()) {
120 if(typeParameters.length > 0) {
121 context.getErrorLog().log(location,
122 "Inlining with type parameters not currently supported in simplification.");
126 return value.getExpression().copy().simplify(context);
132 public Expression resolve(TranslationContext context) {
137 public void getParameters(TranslationContext translationContext,
138 ArrayList<Expression> parameters) {
141 public SCLValue getValue() {
146 public Expression resolveAsPattern(TranslationContext context) {
151 public Expression replace(ReplaceContext context) {
152 Type[] newTypeParameters;
153 if(typeParameters.length == 0)
154 newTypeParameters = Type.EMPTY_ARRAY;
156 newTypeParameters = new Type[typeParameters.length];
157 for(int i=0;i<newTypeParameters.length;++i)
158 newTypeParameters[i] = typeParameters[i].replace(context.tvarMap);
160 return new EConstant(value, newTypeParameters);
163 public Type[] getTypeParameters() {
164 return typeParameters;
168 public LhsType getLhsType() throws NotPatternException {
169 return new PatternMatchingLhs();
173 public IExpression toIExpression(ExpressionInterpretationContext target) {
174 Name name = value.getName();
176 return new IConstant(target.runtimeEnvironment.getRuntimeModule(name.module).getValue(name.name));
177 } catch (ValueNotFound e) {
178 throw new UnsupportedOperationException();
183 public Expression inferType(TypingContext context) {
184 if(context.recursiveValues != null &&
185 context.recursiveValues.contains(value)) {
186 // Handles the case where the constant is one of the recursive definitions we are currently checking
187 // This kind of value is not yet generalized, i.e. it is not necessary to instantiate it.
188 EPlaceholder placeholder = new EPlaceholder(location, this);
189 placeholder.setType(value.getType());
191 context.recursiveReferences.add(placeholder);
194 else if(context.isInPattern() && value.getValue() != DynamicConstructor.INSTANCE /* HACK!! */) {
195 /* This is little hackish code that handles the following kind of constructors:
196 * data Thunk a = Thunk s (a -> s)
198 * match thunk with Thunk s f -> f s
199 * We cannot assign s with an unbound metaVar because its type depends on
200 * how it has been constructed. Therefore we parametrize the function with
201 * existential variable.
203 Type resultType = value.getType();
204 if(resultType instanceof TForAll) {
205 ArrayList<TMetaVar> vars = new ArrayList<TMetaVar>();
206 resultType = Types.instantiate(resultType, vars);
207 MultiFunction mfun = Types.matchFunction(resultType);
208 resultType = mfun.returnType;
210 for(TMetaVar var : vars) {
211 if(resultType.contains(var))
213 addTypeParameters(Types.var(var.getKind()));
219 return applyPUnit(context);
223 public boolean isEffectful() {
228 public void setLocationDeep(long loc) {
229 if(location == Locations.NO_LOCATION)
234 public void accept(ExpressionVisitor visitor) {
239 public Precedence getPrecedence() {
240 return value.getPrecedence();
244 public boolean isPattern(int arity) {
245 IVal val = value.getValue();
246 if(!(val instanceof Constant))
248 Constant constant = (Constant)val;
249 return constant.constructorTag() >= 0 && constant.getArity() == arity;
253 public Expression accept(ExpressionTransformer transformer) {
254 return transformer.transform(this);
258 public boolean equalsExpression(Expression expression) {
259 if(expression.getClass() != getClass())
261 EConstant other = (EConstant)expression;
262 return value == other.value && Types.equals(typeParameters, other.typeParameters);