changeset 20939:f83fd99b2962

Truffle-DSL: add support for null literals.
author Christian Humer <christian.humer@gmail.com>
date Tue, 14 Apr 2015 15:12:48 +0200
parents 18c0f02fa4d2
children 476374f3fe9a
files graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/NullLiteralGuardsTest.java graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpressionResolver.java graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/DSLExpressionGenerator.java graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/ElementUtils.java
diffstat 4 files changed, 171 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/NullLiteralGuardsTest.java	Tue Apr 14 15:12:48 2015 +0200
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.dsl.test;
+
+import static com.oracle.truffle.api.dsl.test.TestHelper.*;
+import static org.junit.Assert.*;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.dsl.*;
+import com.oracle.truffle.api.dsl.test.NullLiteralGuardsTestFactory.CompareNotNullNodeFactory;
+import com.oracle.truffle.api.dsl.test.NullLiteralGuardsTestFactory.CompareObjectsNullNodeFactory;
+import com.oracle.truffle.api.dsl.test.NullLiteralGuardsTestFactory.CompareStringNullNodeFactory;
+import com.oracle.truffle.api.dsl.test.TypeSystemTest.ChildrenNode;
+
+@SuppressWarnings("unused")
+public class NullLiteralGuardsTest {
+
+    @Test
+    public void testCompareObjectsNull() {
+        CallTarget root = createCallTarget(CompareObjectsNullNodeFactory.getInstance());
+        assertEquals("do1", root.call((Object) null));
+        assertEquals("do2", root.call("42"));
+    }
+
+    abstract static class CompareObjectsNullNode extends ChildrenNode {
+        @Specialization(guards = "value == null")
+        String do1(Object value) {
+            return "do1";
+        }
+
+        @Specialization
+        String do2(Object value) {
+            return "do2";
+        }
+    }
+
+    @Test
+    public void testCompareStringNull() {
+        CallTarget root = createCallTarget(CompareStringNullNodeFactory.getInstance());
+        assertEquals("do1", root.call("42"));
+        assertEquals("do2", root.call((Object) null));
+    }
+
+    abstract static class CompareStringNullNode extends ChildrenNode {
+        @Specialization(guards = "value != null")
+        String do1(String value) {
+            return "do1";
+        }
+
+        @Specialization
+        String do2(Object value) {
+            return "do2";
+        }
+    }
+
+    @Test
+    public void testCompareNotNull() {
+        CallTarget root = createCallTarget(CompareNotNullNodeFactory.getInstance());
+        assertEquals("do1", root.call("42"));
+        assertEquals("do2", root.call((Object) null));
+    }
+
+    abstract static class CompareNotNullNode extends ChildrenNode {
+        @Specialization(guards = "value != null")
+        String do1(Object value) {
+            return "do1";
+        }
+
+        @Specialization
+        String do2(Object value) {
+            return "do2";
+        }
+    }
+
+    abstract static class ErrorNullIntComparison1 extends ChildrenNode {
+        @ExpectError("Error parsing expression 'value == null': Incompatible operand types int and null.")
+        @Specialization(guards = "value == null")
+        String do1(int value) {
+            return "do1";
+        }
+    }
+
+    abstract static class ErrorNullIntComparison2 extends ChildrenNode {
+        @ExpectError("Error parsing expression '1 == null': Incompatible operand types int and null.")
+        @Specialization(guards = "1 == null")
+        String do1(int value) {
+            return "do1";
+        }
+    }
+
+    abstract static class ErrorNullNullComparison extends ChildrenNode {
+        @ExpectError("Error parsing expression 'null == null': The operator == is undefined for the argument type(s) null null.")
+        @Specialization(guards = "null == null")
+        String do1(int value) {
+            return "do1";
+        }
+    }
+
+    abstract static class ErrorObjectVoidComparison extends ChildrenNode {
+        protected static void returnVoid() {
+        }
+
+        @ExpectError("Error parsing expression 'value == returnVoid()': Incompatible operand types Object and void.")
+        @Specialization(guards = "value == returnVoid()")
+        String do1(Object value) {
+            return "do1";
+        }
+    }
+
+}
--- a/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpressionResolver.java	Tue Apr 14 15:12:48 2015 +0200
+++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpressionResolver.java	Tue Apr 14 15:12:48 2015 +0200
@@ -197,27 +197,32 @@
     public void visitVariable(Variable variable) {
         List<VariableElement> lookupVariables;
         DSLExpression receiver = variable.getReceiver();
-        if (receiver == null) {
-            lookupVariables = this.variables;
+        if (variable.getName().equals("null")) {
+            variable.setResolvedVariable(new CodeVariableElement(new CodeTypeMirror(TypeKind.NULL), "null"));
         } else {
-            TypeMirror type = receiver.getResolvedType();
-            if (type.getKind() == TypeKind.DECLARED) {
-                type = context.reloadType(type); // ensure ECJ has the type loaded
-                lookupVariables = new ArrayList<>();
-                variablesIn(lookupVariables, context.getEnvironment().getElementUtils().getAllMembers((TypeElement) ((DeclaredType) type).asElement()), true);
-            } else if (type.getKind() == TypeKind.ARRAY) {
-                lookupVariables = Arrays.<VariableElement> asList(new CodeVariableElement(context.getType(int.class), "length"));
+            if (receiver == null) {
+                lookupVariables = this.variables;
             } else {
-                lookupVariables = Collections.emptyList();
+                TypeMirror type = receiver.getResolvedType();
+                if (type.getKind() == TypeKind.DECLARED) {
+                    type = context.reloadType(type); // ensure ECJ has the type loaded
+                    lookupVariables = new ArrayList<>();
+                    variablesIn(lookupVariables, context.getEnvironment().getElementUtils().getAllMembers((TypeElement) ((DeclaredType) type).asElement()), true);
+                } else if (type.getKind() == TypeKind.ARRAY) {
+                    lookupVariables = Arrays.<VariableElement> asList(new CodeVariableElement(context.getType(int.class), "length"));
+                } else {
+                    lookupVariables = Collections.emptyList();
+                }
+            }
+
+            for (VariableElement variableElement : lookupVariables) {
+                if (variableElement.getSimpleName().toString().equals(variable.getName())) {
+                    variable.setResolvedVariable(variableElement);
+                    break;
+                }
             }
         }
 
-        for (VariableElement variableElement : lookupVariables) {
-            if (variableElement.getSimpleName().toString().equals(variable.getName())) {
-                variable.setResolvedVariable(variableElement);
-                break;
-            }
-        }
         if (variable.getResolvedVariable() == null) {
             throw new InvalidExpressionException(String.format("%s cannot be resolved.", variable.getName()));
         }
--- a/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/DSLExpressionGenerator.java	Tue Apr 14 15:12:48 2015 +0200
+++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/generator/DSLExpressionGenerator.java	Tue Apr 14 15:12:48 2015 +0200
@@ -25,6 +25,7 @@
 import java.util.*;
 
 import javax.lang.model.element.*;
+import javax.lang.model.type.*;
 
 import com.oracle.truffle.dsl.processor.expression.*;
 import com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary;
@@ -97,7 +98,9 @@
     public void visitVariable(Variable variable) {
         VariableElement resolvedVariable = variable.getResolvedVariable();
         CodeTree tree;
-        if (variable.getReceiver() == null) {
+        if (variable.getResolvedType().getKind() == TypeKind.NULL) {
+            tree = CodeTreeBuilder.singleString("null");
+        } else if (variable.getReceiver() == null) {
 
             if (isStatic(resolvedVariable)) {
                 tree = staticReference(resolvedVariable);
--- a/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/ElementUtils.java	Tue Apr 14 15:12:48 2015 +0200
+++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/ElementUtils.java	Tue Apr 14 15:12:48 2015 +0200
@@ -365,6 +365,8 @@
                 return getTypeId(((ArrayType) mirror).getComponentType()) + "Array";
             case VOID:
                 return "Void";
+            case NULL:
+                return "Null";
             case WILDCARD:
                 StringBuilder b = new StringBuilder();
                 WildcardType type = (WildcardType) mirror;
@@ -411,6 +413,8 @@
                 return getSimpleName(((ArrayType) mirror).getComponentType()) + "[]";
             case VOID:
                 return "void";
+            case NULL:
+                return "null";
             case WILDCARD:
                 return getWildcardName((WildcardType) mirror);
             case TYPEVAR:
@@ -501,6 +505,8 @@
                 return getQualifiedName(((ArrayType) mirror).getComponentType());
             case VOID:
                 return "void";
+            case NULL:
+                return "null";
             case TYPEVAR:
                 return getSimpleName(mirror);
             case ERROR:
@@ -662,6 +668,7 @@
             case INT:
             case LONG:
             case VOID:
+            case NULL:
             case TYPEVAR:
                 return null;
             case DECLARED:
@@ -945,6 +952,13 @@
             return true;
         } else if (kindIsIntegral(type1.getKind())) {
             return kindIsIntegral(type2.getKind());
+        } else if (type1.getKind() == TypeKind.NULL) {
+            if (type2.getKind() == TypeKind.NULL) {
+                return false;
+            }
+            return true;
+        } else if (type2.getKind() == TypeKind.NULL) {
+            return true;
         } else {
             return false;
         }