]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/EmptyListConstructor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / EmptyListConstructor.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/EmptyListConstructor.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/EmptyListConstructor.java
new file mode 100644 (file)
index 0000000..24ea0fb
--- /dev/null
@@ -0,0 +1,46 @@
+package org.simantics.scl.compiler.elaboration.java;
+
+import org.cojen.classfile.TypeDesc;
+import org.objectweb.asm.Label;
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
+import org.simantics.scl.compiler.constants.Constant;
+import org.simantics.scl.compiler.internal.codegen.continuations.Cont;
+import org.simantics.scl.compiler.internal.codegen.references.IVal;
+import org.simantics.scl.compiler.internal.codegen.utils.Constants;
+import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
+import org.simantics.scl.compiler.types.TVar;
+import org.simantics.scl.compiler.types.Types;
+import org.simantics.scl.compiler.types.kinds.Kinds;
+
+
+public class EmptyListConstructor extends Constant {
+    
+    private static final TVar A = Types.var(Kinds.STAR);
+
+    public EmptyListConstructor() {
+        super(Types.forAll(A, Types.list(A)));
+    }
+    
+    @Override
+    public void push(MethodBuilder mb) {
+        mb.loadStaticField("java/util/Collections", "EMPTY_LIST", Constants.LIST);
+    }
+    
+    @Override
+    public void deconstruct(MethodBuilder mb, IVal parameter, Cont success,
+            Label failure) {
+        if(failure == null)
+            throw new InternalCompilerError("List deconstruction may always fail");
+        
+        parameter.push(mb);
+        mb.invokeInterface(Constants.LIST, "size", TypeDesc.INT, Constants.EMPTY_TYPEDESC_ARRAY);
+        mb.ifZeroComparisonBranch(failure, "!=");
+        mb.jump(success);
+    }
+    
+    @Override
+    public int constructorTag() {
+        return 0;
+    }
+    
+}