]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7536) Implemented deconstruct in CallJava for static fields 89/1089/3
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Mon, 9 Oct 2017 20:53:31 +0000 (23:53 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 10 Oct 2017 04:35:08 +0000 (07:35 +0300)
Allow use of uppercase static fields (and enums) imported from Java to
be used in pattern matching

Change-Id: Ifb647c18fe66904c190ffc3b27ca65e606333435

bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/generic/CallJava.java
tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java
tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/DeconstructEnum.scl [new file with mode: 0644]

index 1dd4477151e0a255dd21c0f7884dbb47d6a5c686..0e3ad1f52c5daa3aad92cdb733374fb2b9c8373a 100644 (file)
@@ -1,9 +1,13 @@
 package org.simantics.scl.compiler.constants.generic;
 
 import org.cojen.classfile.TypeDesc;
+import org.objectweb.asm.Label;
 import org.simantics.scl.compiler.constants.FunctionValue;
+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.references.Val;
 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
+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.Type;
@@ -92,4 +96,16 @@ public class CallJava extends FunctionValue {
         for(StackItem item : stackItems)
             item.prepare(mb);
     }
+    
+    @Override
+    public void deconstruct(MethodBuilder mb, IVal parameter, Cont success,
+            Label failure) {
+        if(parameterTypes.length != 0)
+            super.deconstruct(mb, parameter, success, failure);
+        push(mb);
+        mb.push(parameter, getType());
+        mb.invokeVirtual(TypeDesc.OBJECT, "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[1]);
+        mb.ifZeroComparisonBranch(failure, "==");
+        mb.jump(success);
+    }
 }
index a61dc965d43b26806e4595c0f5ce2d88dbb7d289..23ae54a976c5563696f6144e5393c048a6a5f0d9 100644 (file)
@@ -46,6 +46,7 @@ public class ModuleRegressionTests extends TestBase {
     @Test public void ConjunctionMacro() { test(); }
     @Test public void Constant() { test(); }
     @Test public void ConstructorNameClash() { test(); }
+    @Test public void DeconstructEnum() { test(); }
     @Test public void DefaultMethods1() { test(); }
     @Test public void Deriving3() { test(); }
     @Test public void Deriving4() { test(); }
diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/DeconstructEnum.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/DeconstructEnum.scl
new file mode 100644 (file)
index 0000000..86cd33a
--- /dev/null
@@ -0,0 +1,14 @@
+import "Prelude"
+
+importJava "java.nio.file.StandardOpenOption" where
+    data OpenOption
+
+    READ :: OpenOption
+    WRITE :: OpenOption
+    
+idOf READ = 3
+idOf WRITE = 19
+
+main = idOf READ + idOf WRITE + idOf READ
+--
+25
\ No newline at end of file