]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Expression.java
(refs #7375) Replaced collectVars method by a visitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / Expression.java
old mode 100755 (executable)
new mode 100644 (file)
index 8adf339..427acbd
@@ -1,11 +1,12 @@
 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;
+import org.simantics.scl.compiler.compilation.CompilationContext;
 import org.simantics.scl.compiler.constants.NoRepConstant;
-import org.simantics.scl.compiler.elaboration.contexts.EnvironmentalContext;
 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
@@ -14,13 +15,17 @@ 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.environment.Environment;
 import org.simantics.scl.compiler.internal.codegen.references.IVal;
 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
 import org.simantics.scl.compiler.internal.elaboration.decomposed.DecomposedExpression;
-import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
 import org.simantics.scl.compiler.internal.interpreted.IExpression;
 import org.simantics.scl.compiler.internal.parsing.Symbol;
 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
@@ -35,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 {
@@ -84,13 +88,14 @@ public abstract class Expression extends Symbol implements Typed {
         return context.subsume(inferType(context), requiredType);
     }
     
-    protected Expression applyPUnit(EnvironmentalContext context) {
+    protected Expression applyPUnit(TypingContext context) {
         Type type = Types.canonical(getType());
         if(type instanceof TFun) {
             TFun fun = (TFun)type;
             if(fun.getCanonicalDomain() == Types.PUNIT) {
                 EApply result = new EApply(location, this, new ELiteral(NoRepConstant.PUNIT));
                 result.effect = fun.getCanonicalEffect();
+                context.declareEffect(this.location, result.effect);
                 return result;
             }
         }
@@ -103,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<Variable> constraints = new ArrayList<Variable>(2);
@@ -143,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);
@@ -151,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<Object> allRefs, TIntHashSet refs);
-       public abstract void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars);
-       public abstract void forVariables(VariableProcedure procedure);
-       
-       public Expression decomposeMatching() {
+    public final void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
+        accept(new CollectRefsVisitor(allRefs, refs));
+    }
+
+    public final void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
+        accept(new CollectVarsVisitor(allVars, vars));
+    }
+
+    public final void forVariableUses(VariableProcedure procedure) {
+        accept(new ForVariablesUsesVisitor(procedure));
+    }
+
+    public Expression decomposeMatching() {
         return this;
     }
 
@@ -196,7 +209,7 @@ public abstract class Expression extends Symbol implements Typed {
             throw new TypeValidationException(loc);
     }
 
-       public abstract IVal toVal(Environment env, CodeWriter w);
+       public abstract IVal toVal(CompilationContext context, CodeWriter w);
                
        public Expression closure(TVar ... vars) {
            if(vars.length == 0)
@@ -204,8 +217,6 @@ public abstract class Expression extends Symbol implements Typed {
         return new ELambdaType(vars, this);
        }
     
-    public abstract void collectFreeVariables(THashSet<Variable> vars);
-    
     public Expression simplify(SimplificationContext context) {
         System.out.println("#############################");
         System.out.println(this);
@@ -238,10 +249,6 @@ public abstract class Expression extends Symbol implements Typed {
         context.getErrorLog().log(location, "Pattern was expected here.");
         return new EError();
     }
-
-    public void removeFreeVariables(THashSet<Variable> vars) {
-        throw new InternalCompilerError(getClass().getSimpleName() + " is not a pattern.");
-    }
     
     public Expression checkTypeAsPattern(TypingContext context, Type requiredType) {
         if(context.isInPattern())
@@ -252,11 +259,14 @@ public abstract class Expression extends Symbol implements Typed {
         return expression;
     }
 
-    public THashSet<Variable> getFreeVariables() {
-        THashSet<Variable> result = new THashSet<Variable>();
-        collectFreeVariables(result);
-        return result;
-    }    
+    /**
+     * Used during simplification and in toIExpression
+     */
+    public Set<Variable> getFreeVariables() {
+        CollectFreeVariablesVisitor visitor = new CollectFreeVariablesVisitor(); 
+        accept(visitor);
+        return visitor.getFreeVariables();
+    }
 
     public static Expression[] concat(Expression[] a, Expression[] b) {
         if(a.length == 0)
@@ -303,14 +313,14 @@ public abstract class Expression extends Symbol implements Typed {
         throw new NotPatternException(this);
     }
     
-    public IVal lambdaToVal(Environment env, CodeWriter w) {
-        DecomposedExpression decomposed = DecomposedExpression.decompose(this);
+    public IVal lambdaToVal(CompilationContext context, CodeWriter w) {
+        DecomposedExpression decomposed = DecomposedExpression.decompose(context.errorLog, this);
         CodeWriter newW = w.createFunction(decomposed.typeParameters, decomposed.effect, decomposed.returnType, decomposed.parameterTypes);
         IVal[] parameters = newW.getParameters();
         IVal functionVal = newW.getFunction().getTarget();
         for(int i=0;i<parameters.length;++i)
             decomposed.parameters[i].setVal(parameters[i]);
-        newW.return_(decomposed.body.toVal(env, newW));
+        newW.return_(decomposed.body.toVal(context, newW));
         return functionVal;
     }
     
@@ -328,8 +338,6 @@ public abstract class Expression extends Symbol implements Typed {
     public Expression applyType(Type type) {
         return new EApplyType(location, this, type);
     }
-    
-    public abstract Expression decorate(ExpressionDecorator decorator);
 
        public boolean isEffectful() {
                return true;
@@ -343,12 +351,10 @@ public abstract class Expression extends Symbol implements Typed {
         return false;
     }
     
-    public abstract void collectEffects(THashSet<Type> effects);
-    
     public Type getEffect() {
-        THashSet<Type> effects = new THashSet<Type>();
-        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);