changeset 58899:ccfafe96478e foreign+vector

Automatic merge with foreign
author mcimadamore
date Wed, 03 Apr 2019 17:59:22 +0200
parents 9dce1cda7e5a 598c7de32482
children ff8363d4969e
files
diffstat 3 files changed, 46 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jextract/share/classes/com/sun/tools/jextract/JavaSourceFactory.java	Wed Apr 03 12:34:32 2019 +0200
+++ b/src/jdk.jextract/share/classes/com/sun/tools/jextract/JavaSourceFactory.java	Wed Apr 03 17:59:22 2019 +0200
@@ -261,6 +261,7 @@
         log.print(Level.FINE, () -> "Define class " + name + " for native type " + nativeName);
 
         JavaSourceBuilder jsb = new JavaSourceBuilder(global_jsb.align() + 1);
+        addNativeLocation(false, jsb, structTree.location());
         jsb.addAnnotation(false, NATIVE_STRUCT, Map.of("value", structTree.layout().toString()));
         jsb.interfaceBegin(intf, true, "Struct<" + intf + ">");
         // fields
--- a/src/jdk.jextract/share/classes/com/sun/tools/jextract/TypedefHandler.java	Wed Apr 03 12:34:32 2019 +0200
+++ b/src/jdk.jextract/share/classes/com/sun/tools/jextract/TypedefHandler.java	Wed Apr 03 17:59:22 2019 +0200
@@ -32,6 +32,8 @@
 import java.util.Optional;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import com.sun.tools.jextract.parser.Parser;
 import com.sun.tools.jextract.tree.HeaderTree;
 import com.sun.tools.jextract.tree.SimpleTreeVisitor;
@@ -78,14 +80,13 @@
         // Replace trees from this.decls with Trees found in this.replacements.
         // We need this two step process so that named StructTree instances
         // will replace with original unnamed StructTree instances.
-        List<Tree> newDecls = new ArrayList<>();
-        decls.stream().forEach(tx -> {
+        List<Tree> newDecls = decls.stream().flatMap(tx -> {
             if (replacements.containsKey(tx.cursor())) {
-                newDecls.addAll(replacements.get(tx.cursor()));
+                return replacements.get(tx.cursor()).stream();
             } else {
-                newDecls.add(tx);
+                return Stream.of(tx);
             }
-        });
+        }).collect(Collectors.toList());
 
         return treeMaker.createHeader(ht.cursor(), ht.path(), newDecls);
     }
@@ -114,20 +115,17 @@
                  * is mapped to two Cursors by clang. First one for anonymous struct decl.
                  * and second one for typedef decl. We map it as a single named struct
                  * declaration.
-                 */
-                Cursor dc = defTree.cursor();
-
-                /*
+                 *
                  * There may be more than one typedef on underlying anonymous struct/union.
                  * Example:
                  *
                  * typedef struct { int x; int y; } Point;
                  * typedef Point Vector;
                  */
-                List<Tree> trees = replacements.containsKey(dc)?
-                    replacements.get(defTree.cursor()) : new ArrayList<>();
+
+                Cursor dc = defTree.cursor();
+                List<Tree> trees = replacements.computeIfAbsent(dc, k -> new ArrayList<>());
                 trees.add(((StructTree)defTree).withName(tt.name()));
-                replacements.put(dc, trees);
                 log.print(Level.FINE, () -> "Typedef " + defTree.type().spelling() + " as " + tt.name());
                 return null;
             } else if (defTree.name().equals(tt.name())) {
--- a/test/jdk/com/sun/tools/jextract/test8221154/SrcGenTest.java	Wed Apr 03 12:34:32 2019 +0200
+++ b/test/jdk/com/sun/tools/jextract/test8221154/SrcGenTest.java	Wed Apr 03 17:59:22 2019 +0200
@@ -24,6 +24,7 @@
 import java.foreign.annotations.NativeAddressof;
 import java.foreign.annotations.NativeGetter;
 import java.foreign.annotations.NativeHeader;
+import java.foreign.annotations.NativeLocation;
 import java.foreign.annotations.NativeSetter;
 import java.foreign.annotations.NativeStruct;
 import java.foreign.memory.Pointer;
@@ -148,6 +149,7 @@
         Method numGetter = findGlobalVariableGet(headerCls, "num");
         assertTrue(numGetter != null);
         assertTrue(numGetter.getAnnotation(NativeGetter.class) != null);
+        assertTrue(numGetter.getAnnotation(NativeLocation.class) != null);
 
         Method numPtrGetter = findGlobalVariablePointerGet(headerCls, "num");
         assertTrue(numPtrGetter != null);
@@ -158,11 +160,40 @@
         assertTrue(numSetter != null);
         assertTrue(numSetter.getAnnotation(NativeSetter.class) != null);
 
+        // check "x_coord" method
+        Method xCoordMethod = findFirstMethod(headerCls, "x_coord");
+        assertTrue(xCoordMethod.getReturnType() == int.class);
+        Class<?>[] xCoordParamTypes = xCoordMethod.getParameterTypes();
+        assertTrue(xCoordParamTypes.length == 1);
+        assertTrue(xCoordParamTypes[0] == Pointer.class);
+        assertTrue(xCoordMethod.getAnnotation(NativeLocation.class) != null);
+        assertTrue(!Modifier.isStatic(xCoordMethod.getModifiers()));
+
+        // check "y_coord" method
+        Method yCoordMethod = findFirstMethod(headerCls, "y_coord");
+        assertTrue(yCoordMethod.getReturnType() == int.class);
+        Class<?>[] yCoordParamTypes = yCoordMethod.getParameterTypes();
+        assertTrue(yCoordParamTypes.length == 1);
+        assertTrue(yCoordParamTypes[0] == Pointer.class);
+        assertTrue(yCoordMethod.getAnnotation(NativeLocation.class) != null);
+        assertTrue(!Modifier.isStatic(yCoordMethod.getModifiers()));
+
+        // check "sum" method
+        Method sumMethod = findFirstMethod(headerCls, "sum");
+        assertTrue(sumMethod.getReturnType() == int.class);
+        Class<?>[] sumParamTypes = sumMethod.getParameterTypes();
+        assertTrue(sumParamTypes.length == 2);
+        assertTrue(sumParamTypes[0] == int.class);
+        assertTrue(sumParamTypes[1] == Object[].class);
+        assertTrue(sumMethod.getAnnotation(NativeLocation.class) != null);
+        assertTrue(!Modifier.isStatic(sumMethod.getModifiers()));
+
         // struct Point
         Class<?> pointCls = Arrays.stream(headerCls.getClasses())
             .filter(c -> c.getSimpleName().equals("Point")).findFirst().get();
         assertTrue(Modifier.isInterface(pointCls.getModifiers()));
         assertTrue(pointCls.getAnnotation(NativeStruct.class) != null);
+        assertTrue(pointCls.getAnnotation(NativeLocation.class) != null);
 
         // Point extends Struct
         Class<?> pointSuper = pointCls.getInterfaces()[0];
@@ -172,10 +203,12 @@
         Method xGetter = findStructFieldGet(pointCls, "x");
         assertTrue(xGetter != null);
         assertTrue(xGetter.getAnnotation(NativeGetter.class) != null);
+        assertTrue(xGetter.getAnnotation(NativeLocation.class) != null);
 
         Method yGetter = findStructFieldGet(pointCls, "y");
         assertTrue(yGetter != null);
         assertTrue(yGetter.getAnnotation(NativeGetter.class) != null);
+        assertTrue(yGetter.getAnnotation(NativeLocation.class) != null);
 
         Method xPtrGetter = findStructFieldPointerGet(pointCls, "x");
         assertTrue(xPtrGetter != null);
@@ -214,6 +247,7 @@
         Class<?>[] xCoordParamTypes = xCoordMethod.getParameterTypes();
         assertTrue(xCoordParamTypes.length == 1);
         assertTrue(xCoordParamTypes[0] == Pointer.class);
+        assertTrue(Modifier.isStatic(xCoordMethod.getModifiers()));
 
         // check "y_coord" method
         Method yCoordMethod = findFirstMethod(forwarderCls, "y_coord");
@@ -221,6 +255,7 @@
         Class<?>[] yCoordParamTypes = yCoordMethod.getParameterTypes();
         assertTrue(yCoordParamTypes.length == 1);
         assertTrue(yCoordParamTypes[0] == Pointer.class);
+        assertTrue(Modifier.isStatic(xCoordMethod.getModifiers()));
 
         // global variable "num" getter
         Method numGet = findGlobalVariableGet(forwarderCls, "num");