org.simantics.scl.compiler.completions.parsing,
org.simantics.scl.compiler.constants,
org.simantics.scl.compiler.constants.generic,
+ org.simantics.scl.compiler.dynamic,
org.simantics.scl.compiler.elaboration.chr,
org.simantics.scl.compiler.elaboration.chr.plan,
org.simantics.scl.compiler.elaboration.chr.planning,
IVal newVal = instance.superExpressions[superId].getValue();
apply.getTarget().replaceBy(newVal.createOccurrence());
apply.remove();
+ context.markModified("inline-super");
}
}
apply.detachThisAndSuccessors();
block.getExit().destroy();
block.setExit(new Throw2(apply.getParameters()[0]));
+ context.markModified("inline-throw");
}
}
package org.simantics.scl.compiler.constants.singletons;
+import java.util.Arrays;
+
import org.simantics.scl.compiler.constants.FunctionValue;
import org.simantics.scl.compiler.internal.codegen.references.Val;
+import org.simantics.scl.compiler.internal.codegen.references.ValRef;
+import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply;
import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
-import org.simantics.scl.compiler.types.TCon;
+import org.simantics.scl.compiler.internal.codegen.utils.SSASimplificationContext;
import org.simantics.scl.compiler.types.TVar;
import org.simantics.scl.compiler.types.Type;
import org.simantics.scl.compiler.types.Types;
public class TypeOfConstant extends FunctionValue {
private static final TVar A = Types.var(Kinds.STAR);
- private static final TCon Type = Types.con(Types.BUILTIN, "Type");
public static final TypeOfConstant INSTANCE = new TypeOfConstant();
private TypeOfConstant() {
- super(new TVar[] {A}, Types.NO_EFFECTS, Type,
+ super(new TVar[] {A}, Types.NO_EFFECTS, Types.TYPE,
Types.pred(Types.TYPEABLE, A), A);
}
@Override
public Type applyExact(MethodBuilder mb, Val[] parameters) {
- mb.push(parameters[0], Type);
+ mb.push(parameters[0], Types.TYPE);
return getReturnType();
}
public String toString() {
return "typeOf";
}
+
+ @Override
+ public void inline(SSASimplificationContext context, LetApply apply) {
+ ValRef[] parameters = apply.getParameters();
+ if(parameters.length == 2) {
+ parameters[1].remove();
+ apply.setParameters(Arrays.copyOf(parameters, 1));
+ }
+ ValRef oldFunc = apply.getFunction();
+ apply.setFunction(TypeValueConstant.INSTANCE.createOccurrence(oldFunc.getTypeParameters()));
+ oldFunc.remove();
+ context.markModified("inline-typeOf");
+ }
}
\ No newline at end of file
--- /dev/null
+package org.simantics.scl.compiler.constants.singletons;
+
+import org.simantics.scl.compiler.constants.FunctionValue;
+import org.simantics.scl.compiler.internal.codegen.references.Val;
+import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
+import org.simantics.scl.compiler.types.TVar;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.compiler.types.Types;
+import org.simantics.scl.compiler.types.kinds.Kinds;
+
+public class TypeValueConstant extends FunctionValue {
+ private static final TVar A = Types.var(Kinds.STAR);
+ public static final TypeValueConstant INSTANCE = new TypeValueConstant();
+
+ private TypeValueConstant() {
+ super(new TVar[] {A}, Types.NO_EFFECTS, Types.TYPE,
+ Types.pred(Types.TYPEABLE, A));
+ }
+
+ @Override
+ public Type applyExact(MethodBuilder mb, Val[] parameters) {
+ mb.push(parameters[0], Types.TYPE);
+ return getReturnType();
+ }
+
+ @Override
+ public String toString() {
+ return "typeValue";
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.simantics.scl.compiler.dynamic;
+
+import org.simantics.scl.compiler.types.Type;
+
+public class SafeDynamic {
+ public final Type type_;
+ public final Object value;
+
+ public SafeDynamic(Type type_, Object value) {
+ this.type_ = type_;
+ this.value = value;
+ }
+
+ public String toString() {
+ return new StringBuilder().append("(SafeDynamic").append(" ").append((Object)this.type_).append(" ").append(this.value).append(")").toString();
+ }
+
+ public boolean equals(Object other) {
+ if(this == other)
+ return true;
+ if(other == null || !other.getClass().equals(SafeDynamic.class))
+ return false;
+ SafeDynamic dyn = (SafeDynamic)other;
+ return type_.equals(dyn.type_) && (value == null ? dyn.value == null : value.equals(dyn.value));
+ }
+
+ public int hashCode() {
+ return 31*type_.hashCode()+(value==null ? -957171758 : value.hashCode());
+ }
+}
import org.simantics.scl.compiler.constants.singletons.TypeOfConstant;
import org.simantics.scl.compiler.constants.singletons.TypeOfProxyConstant;
import org.simantics.scl.compiler.constants.singletons.TypeProxyConstant;
+import org.simantics.scl.compiler.constants.singletons.TypeValueConstant;
import org.simantics.scl.compiler.elaboration.fundeps.Fundep;
import org.simantics.scl.compiler.elaboration.modules.Documentation;
import org.simantics.scl.compiler.elaboration.modules.PrivateProperty;
// typeOf :: Typeable a => a -> Type
addValue("typeOf", TypeOfConstant.INSTANCE)
.documentation = "Returns the type of the value given as a parameter.";
+ addValue("typeValue", TypeValueConstant.INSTANCE);
addValue("typeOfProxy", TypeOfProxyConstant.INSTANCE)
.documentation = "Returns the type of the type proxy given as a parameter.";
addValue("TypeProxy", TypeProxyConstant.INSTANCE);
--- /dev/null
+import "Prelude"
+import "JavaBuiltin" as Java
+
+@JavaType "org.simantics.scl.compiler.dynamic.SafeDynamic"
+data SafeDynamic =
+ @FieldNames [type_, value]
+ SafeDynamic Type Dynamic
+
+toSafeDynamic :: Typeable a => a -> SafeDynamic
+toSafeDynamic val = SafeDynamic (typeOf val) (toDynamic val)
+
+fromSafeDynamic :: Typeable a => SafeDynamic -> Maybe a
+fromSafeDynamic (SafeDynamic type_ value) =
+ if type_ == typeOfProxy (TypeProxy :: TypeProxy a)
+ then Just (Java.unsafeCoerce value)
+ else Nothing
+
+forgetType :: SafeDynamic -> Dynamic
+forgetType (SafeDynamic _ value) = value
+
+typeOfSafeDynamic :: SafeDynamic -> Type
+typeOfSafeDynamic (SafeDynamic t _) = t
@Test public void RepeatedVariableDefinitionBug() { test(); }
@Test public void RepeatedVariableInPattern() { test(); }
@Test public void Scanl() { test(); }
+ @Test public void SafeDynamic1() { test(); }
@Test public void Search() { test(); }
@Test public void Sections() { test(); }
@Test public void Select1() { test(); }
--- /dev/null
+import "Prelude"
+import "SafeDynamic"
+
+a = toSafeDynamic (1 :: Double)
+b = toSafeDynamic (2 :: Integer)
+
+main = do
+ print (fromSafeDynamic a :: Maybe Double)
+ print (fromSafeDynamic a :: Maybe Integer)
+ print (fromSafeDynamic b :: Maybe Double)
+ print (fromSafeDynamic b :: Maybe Integer)
+----
+Just 1.0
+Nothing
+Nothing
+Just 2
+()
\ No newline at end of file