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