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.compilation.CompilationContext;
8 import org.simantics.scl.compiler.constants.Constant;
9 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
10 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
11 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
12 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
13 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
14 import org.simantics.scl.compiler.elaboration.expressions.lhstype.LhsType;
15 import org.simantics.scl.compiler.elaboration.expressions.lhstype.PatternMatchingLhs;
16 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
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.interpreted.IConstant;
21 import org.simantics.scl.compiler.internal.interpreted.IExpression;
22 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
23 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
24 import org.simantics.scl.compiler.top.ValueNotFound;
25 import org.simantics.scl.compiler.types.TForAll;
26 import org.simantics.scl.compiler.types.TMetaVar;
27 import org.simantics.scl.compiler.types.Type;
28 import org.simantics.scl.compiler.types.Types;
29 import org.simantics.scl.compiler.types.exceptions.MatchException;
30 import org.simantics.scl.compiler.types.util.MultiFunction;
31 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
33 import gnu.trove.map.hash.TObjectIntHashMap;
34 import gnu.trove.set.hash.THashSet;
35 import gnu.trove.set.hash.TIntHashSet;
37 public class EConstant extends Expression {
39 Type[] typeParameters;
41 public EConstant(SCLValue value, Type ... typeParameters) {
42 if(SCLCompilerConfiguration.DEBUG)
44 throw new NullPointerException();
46 this.typeParameters = typeParameters;
49 public EConstant(SCLValue value) {
50 if(SCLCompilerConfiguration.DEBUG)
52 throw new NullPointerException();
54 this.typeParameters = Type.EMPTY_ARRAY;
57 public EConstant(long loc, SCLValue value) {
59 if(SCLCompilerConfiguration.DEBUG)
61 throw new NullPointerException();
63 this.typeParameters = Type.EMPTY_ARRAY;
66 public EConstant(long loc, SCLValue value, Type ... typeParameters) {
68 if(SCLCompilerConfiguration.DEBUG)
70 throw new NullPointerException();
72 this.typeParameters = typeParameters;
75 public void addTypeParameters(Type ... newTypeParameters) {
76 typeParameters = Types.concat(typeParameters, newTypeParameters);
79 public Expression applyType(Type type) {
80 typeParameters = Types.concat(typeParameters, new Type[] {type});
82 setType(Types.instantiate(getType(), type));
86 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
87 int id = allRefs.get(value);
93 public void collectVars(TObjectIntHashMap<Variable> allVars,
97 public void toString(StringBuilder b, TypeUnparsingContext tuc) {
98 Name name = value.getName();
99 if(name.module.equals("Builtin") || name.module.equals("Prelude"))
103 /*for(Type type : typeParameters) {
105 b.append(type.toString(tuc));
111 protected void updateType() throws MatchException {
112 setType(Types.instantiate(value.getType(), typeParameters));
116 public IVal toVal(CompilationContext context, CodeWriter w) {
117 IVal val = value.getValue();
118 if(typeParameters.length > 0) {
119 val = val.createSpecialization(typeParameters);
125 public void collectFreeVariables(THashSet<Variable> vars) {
129 public Expression simplify(SimplificationContext context) {
130 if(value.getInlineInSimplification()) {
131 if(typeParameters.length > 0) {
132 context.getErrorLog().log(location,
133 "Inlining with type parameters not currently supported in simplification.");
137 return value.getExpression().copy().simplify(context);
143 public Expression resolve(TranslationContext context) {
148 public void getParameters(TranslationContext translationContext,
149 ArrayList<Expression> parameters) {
152 public SCLValue getValue() {
157 public Expression resolveAsPattern(TranslationContext context) {
162 public void removeFreeVariables(THashSet<Variable> vars) {
166 public Expression replace(ReplaceContext context) {
167 Type[] newTypeParameters;
168 if(typeParameters.length == 0)
169 newTypeParameters = Type.EMPTY_ARRAY;
171 newTypeParameters = new Type[typeParameters.length];
172 for(int i=0;i<newTypeParameters.length;++i)
173 newTypeParameters[i] = typeParameters[i].replace(context.tvarMap);
175 return new EConstant(value, newTypeParameters);
178 public Type[] getTypeParameters() {
179 return typeParameters;
183 public LhsType getLhsType() throws NotPatternException {
184 return new PatternMatchingLhs();
188 public IExpression toIExpression(ExpressionInterpretationContext target) {
189 Name name = value.getName();
191 return new IConstant(target.runtimeEnvironment.getRuntimeModule(name.module).getValue(name.name));
192 } catch (ValueNotFound e) {
193 throw new UnsupportedOperationException();
198 public Expression inferType(TypingContext context) {
199 if(context.recursiveValues != null &&
200 context.recursiveValues.contains(value)) {
201 // Handles the case where the constant is one of the recursive definitions we are currently checking
202 // This kind of value is not yet generalized, i.e. it is not necessary to instantiate it.
203 EPlaceholder placeholder = new EPlaceholder(location, this);
204 placeholder.setType(value.getType());
206 context.recursiveReferences.add(placeholder);
209 else if(context.isInPattern()) {
210 /* This is little hackish code that handles the following kind of constructors:
211 * data Thunk a = Thunk s (a -> s)
213 * match thunk with Thunk s f -> f s
214 * We cannot assign s with an unbound metaVar because its type depends on
215 * how it has been constructed. Therefore we parametrize the function with
216 * existential variable.
218 Type resultType = value.getType();
219 if(resultType instanceof TForAll) {
220 ArrayList<TMetaVar> vars = new ArrayList<TMetaVar>();
221 resultType = Types.instantiate(resultType, vars);
222 MultiFunction mfun = Types.matchFunction(resultType);
223 resultType = mfun.returnType;
225 for(TMetaVar var : vars) {
226 if(resultType.contains(var))
228 addTypeParameters(Types.var(var.getKind()));
234 return applyPUnit(context);
238 public boolean isEffectful() {
243 public void collectEffects(THashSet<Type> effects) {
247 public void setLocationDeep(long loc) {
248 if(location == Locations.NO_LOCATION)
253 public void accept(ExpressionVisitor visitor) {
258 public Precedence getPrecedence() {
259 return value.getPrecedence();
263 public boolean isPattern(int arity) {
264 IVal val = value.getValue();
265 if(!(val instanceof Constant))
267 Constant constant = (Constant)val;
268 return constant.constructorTag() >= 0 && constant.getArity() == arity;
272 public Expression accept(ExpressionTransformer transformer) {
273 return transformer.transform(this);
277 public boolean equalsExpression(Expression expression) {
278 if(expression.getClass() != getClass())
280 EConstant other = (EConstant)expression;
281 return value == other.value && Types.equals(typeParameters, other.typeParameters);