Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8244412: jextract should generate static utils class for primitive typedefs #144

Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
@@ -90,67 +90,76 @@
/**
* {@code void} type.
*/
Void,
Void("void"),
/**
* {@code Bool} type.
*/
Bool,
Bool("_Bool"),
/**
* {@code char} type.
*/
Char,
Char("char"),
/**
* {@code char16} type.
*/
Char16,
Char16("char16"),
/**
* {@code char32} type.
*/
Char32,
Char32("char32"),
/**
* {@code short} type.
*/
Short,
Short("short"),
/**
* {@code int} type.
*/
Int,
Int("int"),
/**
* {@code long} type.
*/
Long,
Long("long"),
/**
* {@code long long} type.
*/
LongLong,
LongLong("long long"),
/**
* {@code int128} type.
*/
Int128,
Int128("__int128"),
/**
* {@code float} type.
*/
Float,
Float("float"),
/**
* {@code double} type.
*/
Double,
Double("double"),
/**
* {@code long double} type.
*/
LongDouble,
LongDouble("long double"),
/**
* {@code float128} type.
*/
Float128,
Float128("float128"),
/**
* {@code float16} type.
*/
HalfFloat,
HalfFloat("__fp16"),
/**
* {@code wchar} type.
*/
WChar
WChar("wchar_t");

private String typeName;
Kind(String typeName) {
this.typeName = typeName;
}

public String typeName() {
return typeName;
}
}

/**
@@ -28,6 +28,7 @@
import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.List;
import jdk.incubator.jextract.Type;

/**
* A helper class to generate header interface class in source form.
@@ -109,6 +110,37 @@ public void addStaticFunctionWrapper(String javaName, String nativeName, MethodT
decrAlign();
}

public void emitPrimitiveTypedef(Type.Primitive primType, String name) {
Type.Primitive.Kind kind = primType.kind();
if (primitiveKindSupported(kind)) {
incrAlign();
indent();
sb.append(PUB_MODS);
String className = "C" + name;
sb.append("class ");
sb.append(className);
sb.append(" extends ");
sb.append("C" + kind.typeName().replace(" ", "_"));
sb.append(" {\n");
incrAlign();
indent();
// private constructor
sb.append("private ");
sb.append(className);
sb.append("() {}");
decrAlign();
sb.append("}\n");
decrAlign();
}
}

private boolean primitiveKindSupported(Type.Primitive.Kind kind) {
return switch(kind) {
case Short, Int, Long, LongLong, Float, Double, LongDouble, Char -> true;
default -> false;
};
}

private void addFunctionalFactory(String className, MethodType mtype, FunctionDescriptor fDesc) {
indent();
sb.append(PUB_MODS + "MemoryAddress allocate(" + className + " fi) {\n");
@@ -327,6 +327,8 @@ public Void visitTypedef(Declaration.Typedef tree, Declaration parent) {
if (!s.name().equals(tree.name())) {
return visitScoped(s, tree);
}
} else if (type instanceof Type.Primitive) {
builder.emitPrimitiveTypedef((Type.Primitive)type, tree.name());
}
return null;
}
@@ -8,9 +8,9 @@ import jdk.incubator.foreign.MemorySegment;

import static ${C_LANG}.*;

public final class C-X {
public class C-X {
// don't create!
private C-X() {
C-X() {
}

private static VarHandle arrayHandle(MemoryLayout elemLayout, Class<?> elemCarrier) {
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2020, 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.
*/

import java.nio.file.Path;
import org.testng.annotations.Test;
import static org.testng.Assert.assertNotNull;

/*
* @test
* @modules jdk.incubator.jextract
* @library /test/lib
* @build JextractToolRunner
* @bug 8244412
* @summary jextract should generate static utils class for primitive typedefs
* @run testng/othervm -Dforeign.restricted=permit Test8244412
*/
public class Test8244412 extends JextractToolRunner {
@Test
public void testPrimitiveTypedefs() {
Path typedefsOutput = getOutputFilePath("typedefsgen");
Path typedefsH = getInputFilePath("typedefs.h");
run("-d", typedefsOutput.toString(), typedefsH.toString()).checkSuccess();
try(Loader loader = classLoader(typedefsOutput)) {
Class<?> bytetCls = loader.loadClass("typedefs_h$Cbyte_t");
assertNotNull(bytetCls);
Class<?> sizetCls = loader.loadClass("typedefs_h$Csize_t");
assertNotNull(sizetCls);
} finally {
deleteDir(typedefsOutput);
}
}
}
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2020, 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.
*/


import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.NativeAllocationScope;

import org.testng.annotations.Test;
import test.jextract.test8244412.Clong;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static test.jextract.test8244412.test8244412_h.*;

/*
* @test
* @library ..
* @modules jdk.incubator.jextract
* @bug 8244412
* @summary jextract should generate static utils class for primitive typedefs
* @run driver JtregJextract -t test.jextract.test8244412 -- test8244412.h
* @run testng/othervm -Dforeign.restricted=permit LibTest8244412Test
*/
public class LibTest8244412Test {
@Test
public void test() {
try (var scope = NativeAllocationScope.unboundedScope()) {
var addr = Cmysize_t.allocate(0L, scope);
assertEquals(Cmysize_t.get(addr), 0L);
Cmysize_t.set(addr, 13455566L);
assertEquals(Cmysize_t.get(addr), 13455566L);
assertTrue(Cmysize_t.sizeof() == Clong.sizeof());
}
}
}
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2020, 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.
*/

typedef long mysize_t;
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2020, 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.
*/

typedef char byte_t;
typedef long size_t;