1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
6 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
7 import org.simantics.scl.compiler.common.precedence.Precedence;
8 import org.simantics.scl.compiler.compilation.CompilationContext;
9 import org.simantics.scl.compiler.constants.NoRepConstant;
10 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
11 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
12 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
13 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
14 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
15 import org.simantics.scl.compiler.elaboration.expressions.lhstype.LhsType;
16 import org.simantics.scl.compiler.elaboration.expressions.lhstype.PatternMatchingLhs;
17 import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor;
18 import org.simantics.scl.compiler.elaboration.expressions.visitors.CollectEffectsVisitor;
19 import org.simantics.scl.compiler.elaboration.expressions.visitors.CollectFreeVariablesVisitor;
20 import org.simantics.scl.compiler.elaboration.expressions.visitors.CollectRefsVisitor;
21 import org.simantics.scl.compiler.elaboration.expressions.visitors.ForVariablesUsesVisitor;
22 import org.simantics.scl.compiler.elaboration.expressions.visitors.StandardExpressionVisitor;
23 import org.simantics.scl.compiler.elaboration.query.QAtom;
24 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
25 import org.simantics.scl.compiler.internal.codegen.references.IVal;
26 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
27 import org.simantics.scl.compiler.internal.elaboration.decomposed.DecomposedExpression;
28 import org.simantics.scl.compiler.internal.interpreted.IExpression;
29 import org.simantics.scl.compiler.internal.parsing.Symbol;
30 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
31 import org.simantics.scl.compiler.types.TForAll;
32 import org.simantics.scl.compiler.types.TFun;
33 import org.simantics.scl.compiler.types.TPred;
34 import org.simantics.scl.compiler.types.TVar;
35 import org.simantics.scl.compiler.types.Type;
36 import org.simantics.scl.compiler.types.Types;
37 import org.simantics.scl.compiler.types.exceptions.MatchException;
38 import org.simantics.scl.compiler.types.kinds.Kinds;
39 import org.simantics.scl.compiler.types.util.Typed;
41 import gnu.trove.map.hash.TObjectIntHashMap;
42 import gnu.trove.set.hash.TIntHashSet;
44 public abstract class Expression extends Symbol implements Typed {
45 public static final Expression[] EMPTY_ARRAY = new Expression[0];
53 public Expression(long loc) {
58 public Type getType() {
62 } catch (MatchException e) {
63 throw new InternalCompilerError(e);
66 throw new InternalCompilerError(getClass().getSimpleName() +
67 ".updateType couldn't compute its type.");
72 public void setType(Type type) {
74 throw new NullPointerException();
79 * Infers the type of the expression without any context. Adds type
80 * applications and lambdas if needed.
82 public Expression inferType(TypingContext context) {
83 return checkBasicType(context, Types.metaVar(Kinds.STAR));
86 public Expression checkBasicType(TypingContext context, Type requiredType) {
87 return context.subsume(inferType(context), requiredType);
90 protected Expression applyPUnit(TypingContext context) {
91 Type type = Types.canonical(getType());
92 if(type instanceof TFun) {
93 TFun fun = (TFun)type;
94 if(fun.getCanonicalDomain() == Types.PUNIT) {
95 EApply result = new EApply(location, this, new ELiteral(NoRepConstant.PUNIT));
96 result.effect = fun.getCanonicalEffect();
97 context.declareEffect(this.location, result.effect);
104 public Expression checkIgnoredType(TypingContext context) {
105 Expression expression = inferType(context);
106 if(Types.canonical(expression.getType()) != Types.UNIT)
107 expression = new ESimpleLet(location, null, expression, new ELiteral(NoRepConstant.PUNIT));
112 * Checks the type of the expression against the given type. Adds type
113 * applications and lambdas if needed.
115 public final Expression checkType(TypingContext context, Type requiredType) {
116 //System.out.println("checkType: " + this + " :: " + requiredType);
117 if(!context.isInPattern()) {
118 requiredType = Types.canonical(requiredType);
119 if(requiredType instanceof TForAll) {
120 TForAll forAll = (TForAll)requiredType;
121 TVar var = forAll.var;
122 TVar newVar = Types.var(var.getKind());
123 requiredType = Types.canonical(forAll.type).replace(var, newVar);
124 return new ELambdaType(new TVar[] {newVar}, checkType(context, requiredType));
126 while(requiredType instanceof TFun) {
127 TFun fun = (TFun)requiredType;
128 if(fun.domain instanceof TPred) { // No need to canonicalize
129 ArrayList<Variable> constraints = new ArrayList<Variable>(2);
131 constraints.add(new Variable("constraint", fun.domain));
132 requiredType = Types.canonical(fun.range);
133 if(!(requiredType instanceof TFun))
135 fun = (TFun)requiredType;
136 if(!(fun.domain instanceof TPred))
139 context.pushConstraintFrame(constraints.toArray(new Variable[constraints.size()]));
140 Expression expression = checkType(context, requiredType);
141 context.popConstraintFrame();
142 for(int i=constraints.size()-1;i>=0;--i)
143 expression = new ESimpleLambda(constraints.get(i), expression);
146 else if(fun.domain == Types.PUNIT) {
147 context.pushEffectUpperBound(location, fun.effect);
148 Expression expr = checkType(context, fun.range);
149 context.popEffectUpperBound();
152 Variable var = new Variable("punit", Types.PUNIT);
153 return new ESimpleLambda(location, var, fun.effect, expr);
159 return checkBasicType(context, requiredType);
162 public final void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
163 accept(new CollectRefsVisitor(allRefs, refs));
166 public abstract void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars);
168 public final void forVariableUses(VariableProcedure procedure) {
169 accept(new ForVariablesUsesVisitor(procedure));
172 public Expression decomposeMatching() {
176 public String toString() {
177 StringBuilder b = new StringBuilder();
178 ExpressionToStringVisitor visitor = new ExpressionToStringVisitor(b);
183 protected abstract void updateType() throws MatchException;
185 public static class TypeValidationException extends Exception {
186 private static final long serialVersionUID = 3181298127162041248L;
190 public TypeValidationException(long loc) {
194 public long getLoc() {
198 public TypeValidationException(long loc, Throwable cause) {
204 public static void assertEquals(long loc, Type a, Type b) throws TypeValidationException {
205 if(!Types.equals(a, b))
206 throw new TypeValidationException(loc);
209 public abstract IVal toVal(CompilationContext context, CodeWriter w);
211 public Expression closure(TVar ... vars) {
214 return new ELambdaType(vars, this);
217 public Expression simplify(SimplificationContext context) {
218 System.out.println("#############################");
219 System.out.println(this);
220 throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support simplify method.");
223 public abstract Expression resolve(TranslationContext context);
226 * Returns head of the pattern.
228 public EVar getPatternHead() throws NotPatternException {
229 throw new NotPatternException(this);
232 public LhsType getLhsType() throws NotPatternException {
233 throw new NotPatternException(this);
236 protected void collectVariableNames(PatternMatchingLhs lhsType) throws NotPatternException {
237 throw new NotPatternException(this);
240 public void getParameters(TranslationContext translationContext,
241 ArrayList<Expression> parameters) {
242 throw new InternalCompilerError("Class " + getClass().getSimpleName() + " does not support getParameters.");
245 public Expression resolveAsPattern(TranslationContext context) {
246 context.getErrorLog().log(location, "Pattern was expected here.");
250 public Expression checkTypeAsPattern(TypingContext context, Type requiredType) {
251 if(context.isInPattern())
252 throw new InternalCompilerError("Already in a pattern.");
253 context.setInPattern(true);
254 Expression expression = checkType(context, requiredType);
255 context.setInPattern(false);
260 * Used during simplification and in toIExpression
262 public Set<Variable> getFreeVariables() {
263 CollectFreeVariablesVisitor visitor = new CollectFreeVariablesVisitor();
265 return visitor.getFreeVariables();
268 public static Expression[] concat(Expression[] a, Expression[] b) {
273 Expression[] result = new Expression[a.length + b.length];
274 for(int i=0;i<a.length;++i)
276 for(int i=0;i<b.length;++i)
277 result[i+a.length] = b[i];
281 public Expression replace(ReplaceContext context) {
282 throw new InternalCompilerError(getClass().getSimpleName() + " does not support replace.");
285 public static Expression[] replace(ReplaceContext context, Expression[] expressions) {
286 Expression[] result = new Expression[expressions.length];
287 for(int i=0;i<expressions.length;++i)
288 result[i] = expressions[i].replace(context);
292 public Expression copy() {
293 return replace(new ReplaceContext(null));
296 public Expression copy(TypingContext typingContext) {
297 return replace(new ReplaceContext(typingContext));
300 public abstract void setLocationDeep(long loc);
302 public Expression replaceInPattern(ReplaceContext context) {
303 context.inPattern = true;
304 Expression result = replace(context);
305 context.inPattern = false;
309 public int getFunctionDefinitionPatternArity() throws NotPatternException {
310 throw new NotPatternException(this);
313 public IVal lambdaToVal(CompilationContext context, CodeWriter w) {
314 DecomposedExpression decomposed = DecomposedExpression.decompose(context.errorLog, this);
315 CodeWriter newW = w.createFunction(decomposed.typeParameters, decomposed.effect, decomposed.returnType, decomposed.parameterTypes);
316 IVal[] parameters = newW.getParameters();
317 IVal functionVal = newW.getFunction().getTarget();
318 for(int i=0;i<parameters.length;++i)
319 decomposed.parameters[i].setVal(parameters[i]);
320 newW.return_(decomposed.body.toVal(context, newW));
324 public IExpression toIExpression(ExpressionInterpretationContext context) {
325 throw new UnsupportedOperationException();
328 public static IExpression[] toIExpressions(ExpressionInterpretationContext target, Expression[] expressions) {
329 IExpression[] result = new IExpression[expressions.length];
330 for(int i=0;i<expressions.length;++i)
331 result[i] = expressions[i].toIExpression(target);
335 public Expression applyType(Type type) {
336 return new EApplyType(location, this, type);
339 public boolean isEffectful() {
343 public boolean isFunctionPattern() {
347 public boolean isConstructorApplication() {
351 public Type getEffect() {
352 CollectEffectsVisitor visitor = new CollectEffectsVisitor();
354 return visitor.getCombinedEffect();
357 public abstract void accept(ExpressionVisitor visitor);
359 public void collectRelationRefs(
360 final TObjectIntHashMap<SCLRelation> allRefs, final TIntHashSet refs) {
361 accept(new StandardExpressionVisitor() {
363 public void visit(QAtom query) {
364 int id = allRefs.get(query.relation);
371 public boolean isFunctionDefinitionLhs() {
375 public Precedence getPrecedence() {
376 return Precedence.DEFAULT;
379 public boolean isPattern(int arity) {
383 public abstract Expression accept(ExpressionTransformer transformer);
385 // TODO implement for all expressions
386 public boolean equalsExpression(Expression expression) {
391 * This method returns a lower bound for the function arity of the value this expression defines.
392 * The lower bound is calculated purely looking the syntax of the expression, not the
393 * types of the constants and variables the expression refers to.
395 public int getSyntacticFunctionArity() {