--- /dev/null
+package org.simantics.scl.compiler.constants.singletons;\r
+\r
+import org.cojen.classfile.TypeDesc;\r
+import org.objectweb.asm.Label;\r
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;\r
+import org.simantics.scl.compiler.constants.FunctionValue;\r
+import org.simantics.scl.compiler.constants.GetPrimitiveConstant;\r
+import org.simantics.scl.compiler.constants.LocalVariableConstant;\r
+import org.simantics.scl.compiler.constants.NoRepConstant;\r
+import org.simantics.scl.compiler.internal.codegen.continuations.Cont;\r
+import org.simantics.scl.compiler.internal.codegen.references.IVal;\r
+import org.simantics.scl.compiler.internal.codegen.references.Val;\r
+import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;\r
+import org.simantics.scl.compiler.internal.codegen.utils.LocalVariable;\r
+import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;\r
+import org.simantics.scl.compiler.types.TVar;\r
+import org.simantics.scl.compiler.types.Type;\r
+import org.simantics.scl.compiler.types.Types;\r
+import org.simantics.scl.compiler.types.exceptions.MatchException;\r
+import org.simantics.scl.compiler.types.kinds.Kinds;\r
+\r
+public class JustConstant extends FunctionValue {\r
+ \r
+ private static final TVar A = Types.var(Kinds.STAR);\r
+ public static final JustConstant INSTANCE = new JustConstant();\r
+ \r
+ private JustConstant() {\r
+ super(new TVar[] {A}, Types.NO_EFFECTS, Types.apply(Types.MAYBE, A), A);\r
+ }\r
+ \r
+ @Override\r
+ public Type applyExact(MethodBuilder mb, Val[] parameters) { \r
+ parameters[0].push(mb);\r
+ mb.box(parameters[0].getType());\r
+ return getReturnType();\r
+ }\r
+ \r
+ @Override\r
+ public void deconstruct(MethodBuilder mb, IVal parameter,\r
+ Cont success, Label failure) {\r
+ Type componentType;\r
+ try {\r
+ componentType = Types.matchApply(Types.MAYBE, parameter.getType());\r
+ } catch (MatchException e) {\r
+ throw new InternalCompilerError();\r
+ }\r
+ TypeDesc componentTypeDesc =\r
+ mb.getJavaTypeTranslator().toTypeDesc(componentType);\r
+ \r
+ if(failure == null) {\r
+ IVal val = componentTypeDesc.isPrimitive() \r
+ ? new GetPrimitiveConstant(componentType, parameter, componentTypeDesc)\r
+ : parameter;\r
+ mb.jump(success, val);\r
+ }\r
+ else {\r
+ Label failureLabel = mb.createLabel();\r
+ \r
+ parameter.push(mb);\r
+ mb.dup();\r
+ mb.ifNullBranch(failureLabel, true);\r
+ \r
+ if(componentTypeDesc.equals(TypeDesc.VOID)) {\r
+ mb.pop();\r
+ mb.jump(success, new NoRepConstant(componentType));\r
+ }\r
+ else {\r
+ if(componentTypeDesc.isPrimitive())\r
+ mb.convert(JavaTypeTranslator.toObjectType(componentTypeDesc), componentTypeDesc);\r
+ \r
+ LocalVariable lv = mb.createLocalVariable("temp", componentTypeDesc);\r
+ mb.storeLocal(lv);\r
+ \r
+ mb.jump(success, new LocalVariableConstant(componentType, lv));\r
+ }\r
+ \r
+ //\r
+ mb.setLocation(failureLabel);\r
+ mb.pop();\r
+ mb.branch(failure);\r
+ }\r
+ }\r
+ public int constructorTag() {\r
+ return 1;\r
+ }\r
+ @Override\r
+ public String toString() {\r
+ return "Just";\r
+ }\r
+}
\ No newline at end of file