X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fexpressions%2FExpression.java;h=507d335e3479379ce7cd4ae1a66e196493e5c920;hp=0d48c4a153492e47307e7521831fc1dc0f7cc1db;hb=a8d72a172fdc815c8a9f0f584f010f7e35286f92;hpb=747231cca0974ca9ed5f78caa6517ee9dcb8e4fc diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Expression.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Expression.java index 0d48c4a15..507d335e3 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Expression.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Expression.java @@ -14,6 +14,9 @@ import org.simantics.scl.compiler.elaboration.errors.NotPatternException; import org.simantics.scl.compiler.elaboration.expressions.lhstype.LhsType; import org.simantics.scl.compiler.elaboration.expressions.lhstype.PatternMatchingLhs; import org.simantics.scl.compiler.elaboration.expressions.printing.ExpressionToStringVisitor; +import org.simantics.scl.compiler.elaboration.expressions.visitors.CollectEffectsVisitor; +import org.simantics.scl.compiler.elaboration.expressions.visitors.CollectRefsVisitor; +import org.simantics.scl.compiler.elaboration.expressions.visitors.ForVariablesUsesVisitor; import org.simantics.scl.compiler.elaboration.query.QAtom; import org.simantics.scl.compiler.elaboration.relations.SCLRelation; import org.simantics.scl.compiler.internal.codegen.references.IVal; @@ -102,23 +105,23 @@ public abstract class Expression extends Symbol implements Typed { expression = new ESimpleLet(location, null, expression, new ELiteral(NoRepConstant.PUNIT)); return expression; } - - /** - * Checks the type of the expression against the given type. Adds type - * applications and lambdas if needed. - */ - public final Expression checkType(TypingContext context, Type requiredType) { - //System.out.println("checkType: " + this + " :: " + requiredType); - if(!context.isInPattern()) { - requiredType = Types.canonical(requiredType); - if(requiredType instanceof TForAll) { + + /** + * Checks the type of the expression against the given type. Adds type + * applications and lambdas if needed. + */ + public final Expression checkType(TypingContext context, Type requiredType) { + //System.out.println("checkType: " + this + " :: " + requiredType); + if(!context.isInPattern()) { + requiredType = Types.canonical(requiredType); + if(requiredType instanceof TForAll) { TForAll forAll = (TForAll)requiredType; TVar var = forAll.var; TVar newVar = Types.var(var.getKind()); requiredType = Types.canonical(forAll.type).replace(var, newVar); return new ELambdaType(new TVar[] {newVar}, checkType(context, requiredType)); } - while(requiredType instanceof TFun) { + while(requiredType instanceof TFun) { TFun fun = (TFun)requiredType; if(fun.domain instanceof TPred) { // No need to canonicalize ArrayList constraints = new ArrayList(2); @@ -142,7 +145,7 @@ public abstract class Expression extends Symbol implements Typed { context.pushEffectUpperBound(location, fun.effect); Expression expr = checkType(context, fun.range); context.popEffectUpperBound(); - + // Wrap Variable var = new Variable("punit", Types.PUNIT); return new ESimpleLambda(location, var, fun.effect, expr); @@ -150,15 +153,21 @@ public abstract class Expression extends Symbol implements Typed { else break; } - } - return checkBasicType(context, requiredType); - } + } + return checkBasicType(context, requiredType); + } - public abstract void collectRefs(TObjectIntHashMap allRefs, TIntHashSet refs); - public abstract void collectVars(TObjectIntHashMap allVars, TIntHashSet vars); - public abstract void forVariables(VariableProcedure procedure); - - public Expression decomposeMatching() { + public final void collectRefs(TObjectIntHashMap allRefs, TIntHashSet refs) { + accept(new CollectRefsVisitor(allRefs, refs)); + } + + public abstract void collectVars(TObjectIntHashMap allVars, TIntHashSet vars); + + public final void forVariableUses(VariableProcedure procedure) { + accept(new ForVariablesUsesVisitor(procedure)); + } + + public Expression decomposeMatching() { return this; } @@ -258,7 +267,7 @@ public abstract class Expression extends Symbol implements Typed { THashSet result = new THashSet(); collectFreeVariables(result); return result; - } + } public static Expression[] concat(Expression[] a, Expression[] b) { if(a.length == 0) @@ -343,12 +352,10 @@ public abstract class Expression extends Symbol implements Typed { return false; } - public abstract void collectEffects(THashSet effects); - public Type getEffect() { - THashSet effects = new THashSet(); - collectEffects(effects); - return Types.union(effects.toArray(new Type[effects.size()])); + CollectEffectsVisitor visitor = new CollectEffectsVisitor(); + accept(visitor); + return visitor.getCombinedEffect(); } public abstract void accept(ExpressionVisitor visitor);