From: Hannu Niemistö Date: Mon, 9 Oct 2017 20:53:31 +0000 (+0300) Subject: (refs #7536) Implemented deconstruct in CallJava for static fields X-Git-Tag: v1.31.0~133 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F89%2F1089%2F3;p=simantics%2Fplatform.git (refs #7536) Implemented deconstruct in CallJava for static fields Allow use of uppercase static fields (and enums) imported from Java to be used in pattern matching Change-Id: Ifb647c18fe66904c190ffc3b27ca65e606333435 --- diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/generic/CallJava.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/generic/CallJava.java index 1dd447715..0e3ad1f52 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/generic/CallJava.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/generic/CallJava.java @@ -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); + } } diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java index a61dc965d..23ae54a97 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java @@ -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 index 000000000..86cd33a7a --- /dev/null +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/DeconstructEnum.scl @@ -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