]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/generic/ConvertToListFilter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / constants / generic / ConvertToListFilter.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/generic/ConvertToListFilter.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/generic/ConvertToListFilter.java
new file mode 100644 (file)
index 0000000..023cfce
--- /dev/null
@@ -0,0 +1,42 @@
+package org.simantics.scl.compiler.constants.generic;
+
+import java.util.ArrayList;
+
+import org.cojen.classfile.TypeDesc;
+import org.objectweb.asm.Label;
+import org.simantics.scl.compiler.internal.codegen.utils.Constants;
+import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
+
+public enum ConvertToListFilter implements OutputFilter {
+    INSTANCE;
+
+    private static final TypeDesc ARRAY_LIST = TypeDesc.forClass(ArrayList.class);
+    private static final TypeDesc[] CONVERSION_PARAMS =
+            new TypeDesc[] {Constants.COLLECTION};
+    
+    @Override
+    public void filter(MethodBuilder mb) {
+        Label conversionNeeded = mb.createLabel();
+        Label endFilter = mb.createLabel();
+        
+        mb.dup();
+        mb.instanceOf(Constants.LIST);        
+        mb.ifZeroComparisonBranch(conversionNeeded, "==");
+        
+        // no conversion needed
+        mb.checkCast(Constants.LIST);
+        mb.branch(endFilter);
+        
+        // conversion needed
+        mb.setLocation(conversionNeeded);
+        
+        mb.newObject(ARRAY_LIST);
+        mb.dupX1();
+        mb.swap();
+        mb.invokeConstructor(ARRAY_LIST, CONVERSION_PARAMS);
+        
+        // end
+        mb.setLocation(endFilter);
+    }
+
+}