]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EConstant.java
Fixed a bug related to the extent of variable frames (see test case)
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EConstant.java
index f951de7374a09dfb0577e154cd1eb6312d202ca1..eb3f5ca4a74ff3e192c7647153f5f1d14cd36bcd 100644 (file)
@@ -1,9 +1,12 @@
 package org.simantics.scl.compiler.elaboration.expressions;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Set;
 
 import org.simantics.scl.compiler.common.names.Name;
 import org.simantics.scl.compiler.common.precedence.Precedence;
+import org.simantics.scl.compiler.compilation.CompilationContext;
 import org.simantics.scl.compiler.constants.Constant;
 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
@@ -12,12 +15,11 @@ import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
 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.java.DynamicConstructor;
 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
-import org.simantics.scl.compiler.environment.Environment;
 import org.simantics.scl.compiler.errors.Locations;
 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.utils.ExpressionDecorator;
 import org.simantics.scl.compiler.internal.interpreted.IConstant;
 import org.simantics.scl.compiler.internal.interpreted.IExpression;
 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
@@ -31,12 +33,8 @@ import org.simantics.scl.compiler.types.exceptions.MatchException;
 import org.simantics.scl.compiler.types.util.MultiFunction;
 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
 
-import gnu.trove.map.hash.TObjectIntHashMap;
-import gnu.trove.set.hash.THashSet;
-import gnu.trove.set.hash.TIntHashSet;
-
 public class EConstant extends Expression {
-    SCLValue value;
+    public SCLValue value;
     Type[] typeParameters;
     
     public EConstant(SCLValue value, Type ... typeParameters) {
@@ -83,24 +81,18 @@ public class EConstant extends Expression {
             setType(Types.instantiate(getType(), type));
         return this;
     }
-
-       public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
-        int id = allRefs.get(value);
-        if(id >= 0)
-            refs.add(id);
+    
+    @Override
+    public Set<Variable> getFreeVariables() {
+        return Collections.emptySet();
     }
-       
-       @Override
-       public void collectVars(TObjectIntHashMap<Variable> allVars,
-               TIntHashSet vars) {   
-       }
 
-       public void toString(StringBuilder b, TypeUnparsingContext tuc) {
-           Name name = value.getName();
-           if(name.module.equals("Builtin") || name.module.equals("Prelude"))
-               b.append(name.name);
-           else
-               b.append(name);
+    public void toString(StringBuilder b, TypeUnparsingContext tuc) {
+        Name name = value.getName();
+        if(name.module.equals("Builtin") || name.module.equals("Prelude"))
+            b.append(name.name);
+        else
+            b.append(name);
         /*for(Type type : typeParameters) {
             b.append(" <");
             b.append(type.toString(tuc));
@@ -113,17 +105,13 @@ public class EConstant extends Expression {
         setType(Types.instantiate(value.getType(), typeParameters));
     }
     
-       @Override
-       public IVal toVal(Environment env, CodeWriter w) {
-           IVal val = value.getValue();                
-           if(typeParameters.length > 0) {
-               val = val.createSpecialization(typeParameters);
-           }
-           return val;
-    }
-
     @Override
-    public void collectFreeVariables(THashSet<Variable> vars) {
+    public IVal toVal(CompilationContext context, CodeWriter w) {
+        IVal val = value.getValue();           
+        if(typeParameters.length > 0) {
+            val = val.createSpecialization(typeParameters);
+        }
+        return val;
     }
 
     @Override
@@ -158,10 +146,6 @@ public class EConstant extends Expression {
     public Expression resolveAsPattern(TranslationContext context) {
         return this;
     }
-       
-    @Override
-    public void removeFreeVariables(THashSet<Variable> vars) {     
-    }
 
     @Override
     public Expression replace(ReplaceContext context) {
@@ -207,7 +191,7 @@ public class EConstant extends Expression {
             context.recursiveReferences.add(placeholder);
             return placeholder;
         }
-        else if(context.isInPattern()) {
+        else if(context.isInPattern() && value.getValue() != DynamicConstructor.INSTANCE /* HACK!! */) {
             /* This is little hackish code that handles the following kind of constructors:
              *   data Thunk a = Thunk s (a -> s)
              * in
@@ -232,21 +216,12 @@ public class EConstant extends Expression {
             return this;
         }
         else
-            return applyPUnit(context.getCompilationContext());
-    }
-
-    @Override
-    public Expression decorate(ExpressionDecorator decorator) {
-        return decorator.decorate(this);
+            return applyPUnit(context);
     }
     
     @Override
     public boolean isEffectful() {
-       return false;
-    }
-
-    @Override
-    public void collectEffects(THashSet<Type> effects) {
+        return false;
     }
     
     @Override
@@ -264,10 +239,6 @@ public class EConstant extends Expression {
     public Precedence getPrecedence() {
         return value.getPrecedence();
     }
-
-    @Override
-    public void forVariables(VariableProcedure procedure) {
-    }
     
     @Override
     public boolean isPattern(int arity) {
@@ -290,4 +261,9 @@ public class EConstant extends Expression {
         EConstant other = (EConstant)expression;
         return value == other.value && Types.equals(typeParameters, other.typeParameters);
     }
+    
+    @Override
+    public boolean isConstructorApplication() {
+        return true;
+    }
 }