X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fexpressions%2FExpression.java;h=dcb38165e7665ae66bf6af53c458e277dc192700;hb=refs%2Fchanges%2F34%2F1534%2F3;hp=d3e226cf490b5e70a1aae97fd2d654e7ff448adc;hpb=d9a283acefae11c2cc094ed1c7b74759f8166f17;p=simantics%2Fplatform.git 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 d3e226cf4..dcb38165e 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 @@ -1,6 +1,7 @@ package org.simantics.scl.compiler.elaboration.expressions; import java.util.ArrayList; +import java.util.Set; import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; import org.simantics.scl.compiler.common.precedence.Precedence; @@ -14,6 +15,12 @@ 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.CollectFreeVariablesVisitor; +import org.simantics.scl.compiler.elaboration.expressions.visitors.CollectRefsVisitor; +import org.simantics.scl.compiler.elaboration.expressions.visitors.CollectVarsVisitor; +import org.simantics.scl.compiler.elaboration.expressions.visitors.ForVariablesUsesVisitor; +import org.simantics.scl.compiler.elaboration.expressions.visitors.StandardExpressionVisitor; 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; @@ -33,7 +40,6 @@ import org.simantics.scl.compiler.types.kinds.Kinds; import org.simantics.scl.compiler.types.util.Typed; import gnu.trove.map.hash.TObjectIntHashMap; -import gnu.trove.set.hash.THashSet; import gnu.trove.set.hash.TIntHashSet; public abstract class Expression extends Symbol implements Typed { @@ -55,10 +61,10 @@ public abstract class Expression extends Symbol implements Typed { try { updateType(); } catch (MatchException e) { - throw new InternalCompilerError(e); + throw new InternalCompilerError(location, e); } if(type == null) - throw new InternalCompilerError(getClass().getSimpleName() + + throw new InternalCompilerError(location, getClass().getSimpleName() + ".updateType couldn't compute its type."); } return type; @@ -102,23 +108,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 +148,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 +156,23 @@ 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 final void collectVars(TObjectIntHashMap allVars, TIntHashSet vars) { + accept(new CollectVarsVisitor(allVars, vars)); + } + + public final void forVariableUses(VariableProcedure procedure) { + accept(new ForVariablesUsesVisitor(procedure)); + } + + public Expression decomposeMatching() { return this; } @@ -203,8 +217,6 @@ public abstract class Expression extends Symbol implements Typed { return new ELambdaType(vars, this); } - public abstract void collectFreeVariables(THashSet vars); - public Expression simplify(SimplificationContext context) { System.out.println("#############################"); System.out.println(this); @@ -230,21 +242,17 @@ public abstract class Expression extends Symbol implements Typed { public void getParameters(TranslationContext translationContext, ArrayList parameters) { - throw new InternalCompilerError("Class " + getClass().getSimpleName() + " does not support getParameters."); + throw new InternalCompilerError(location, "Class " + getClass().getSimpleName() + " does not support getParameters."); } public Expression resolveAsPattern(TranslationContext context) { context.getErrorLog().log(location, "Pattern was expected here."); return new EError(); } - - public void removeFreeVariables(THashSet vars) { - throw new InternalCompilerError(getClass().getSimpleName() + " is not a pattern."); - } public Expression checkTypeAsPattern(TypingContext context, Type requiredType) { if(context.isInPattern()) - throw new InternalCompilerError("Already in a pattern."); + throw new InternalCompilerError(location, "Already in a pattern."); context.setInPattern(true); Expression expression = checkType(context, requiredType); context.setInPattern(false); @@ -254,10 +262,10 @@ public abstract class Expression extends Symbol implements Typed { /** * Used during simplification and in toIExpression */ - public THashSet getFreeVariables() { - THashSet result = new THashSet(); - collectFreeVariables(result); - return result; + public Set getFreeVariables() { + CollectFreeVariablesVisitor visitor = new CollectFreeVariablesVisitor(); + accept(visitor); + return visitor.getFreeVariables(); } public static Expression[] concat(Expression[] a, Expression[] b) { @@ -274,7 +282,7 @@ public abstract class Expression extends Symbol implements Typed { } public Expression replace(ReplaceContext context) { - throw new InternalCompilerError(getClass().getSimpleName() + " does not support replace."); + throw new InternalCompilerError(location, getClass().getSimpleName() + " does not support replace."); } public static Expression[] replace(ReplaceContext context, Expression[] expressions) { @@ -312,7 +320,7 @@ public abstract class Expression extends Symbol implements Typed { IVal functionVal = newW.getFunction().getTarget(); for(int i=0;i 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);