-
Notifications
You must be signed in to change notification settings - Fork 61
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
8252634: jextract should generate type annotations for C types #300
Closed
+380
−58
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
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/AnnotationWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* 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 jdk.internal.jextract.impl; | ||
|
||
import jdk.incubator.jextract.Type; | ||
import java.util.stream.Collectors; | ||
|
||
public class AnnotationWriter implements Type.Visitor<String, Void> { | ||
@Override | ||
public String visitPrimitive(Type.Primitive t, Void aVoid) { | ||
if (t.kind().layout().isEmpty()) { | ||
return "void"; //skip for now | ||
} else { | ||
return t.kind().typeName(); | ||
} | ||
} | ||
|
||
@Override | ||
public String visitDelegated(Type.Delegated t, Void aVoid) { | ||
if (t.kind() == Type.Delegated.Kind.TYPEDEF) { | ||
return t.name().get(); | ||
} else if (t.kind() == Type.Delegated.Kind.POINTER) { | ||
String typeStr = t.type().accept(this, null); | ||
// FIXME Revisit this logic for pointer to function types | ||
if (t.type() instanceof Type.Function) { | ||
return typeStr.replace("(", "(*)("); | ||
} else { | ||
return typeStr + "*"; | ||
} | ||
} else { | ||
String prefix = switch (t.kind()) { | ||
case ATOMIC -> "_Atomic"; | ||
case COMPLEX -> "complex"; | ||
case SIGNED -> "signed"; | ||
case UNSIGNED -> "unsigned"; | ||
case VOLATILE -> "volatile"; | ||
default -> throw new IllegalStateException("Invalid input" + t); | ||
}; | ||
return prefix + " " + t.type().accept(this, null); | ||
} | ||
} | ||
|
||
@Override | ||
public String visitFunction(Type.Function t, Void aVoid) { | ||
String ret = t.returnType().accept(this, null); | ||
String args = t.argumentTypes().stream().map(p -> p.accept(this, null)) | ||
.collect(Collectors.joining(",", "(", ")")); | ||
return ret + args; | ||
} | ||
|
||
@Override | ||
public String visitDeclared(Type.Declared t, Void aVoid) { | ||
String name = t.tree().name(); | ||
return switch (t.tree().kind()) { | ||
case STRUCT -> "struct " + name; | ||
case UNION -> "union " + name; | ||
case ENUM -> "enum " + name; | ||
default -> name; | ||
}; | ||
} | ||
|
||
@Override | ||
public String visitArray(Type.Array t, Void aVoid) { | ||
if (t.kind() == Type.Array.Kind.VECTOR) { | ||
return ""; //skip for now | ||
} else { | ||
return t.elementType().accept(this, null) + "[]"; | ||
} | ||
} | ||
|
||
@Override | ||
public String visitType(Type t, Void aVoid) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
String getCAnnotation(Type t) { | ||
return "@C(\"" + t.accept(this, null) + "\")"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/resources/C.java.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Generated by jextract | ||
sundararajana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation to indicate C types | ||
*/ | ||
@Target({ ElementType.TYPE_USE }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface C { | ||
/** | ||
* The C type associated with a given Java type | ||
* @return The C type associated with a given Java type | ||
*/ | ||
String value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* 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.lang.annotation.Annotation; | ||
import java.lang.reflect.AnnotatedElement; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Parameter; | ||
import jdk.incubator.foreign.MemoryAddress; | ||
import jdk.incubator.foreign.MemorySegment; | ||
import java.nio.file.Path; | ||
import org.testng.annotations.Test; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
/* | ||
* @test | ||
* @bug 8252634 | ||
* @summary jextract should generate type annotations for C types | ||
* @library /test/lib | ||
* @modules jdk.incubator.jextract | ||
* @build JextractToolRunner | ||
* @run testng/othervm -Dforeign.restricted=permit Test8252634 | ||
*/ | ||
public class Test8252634 extends JextractToolRunner { | ||
private Class<? extends Annotation> cAnnoClass; | ||
private Method cValueMethod; | ||
|
||
@Test | ||
public void test() throws Throwable { | ||
Path outputPath = getOutputFilePath("output8252634"); | ||
Path headerFile = getInputFilePath("test8252634.h"); | ||
run("-d", outputPath.toString(), headerFile.toString()).checkSuccess(); | ||
try(Loader loader = classLoader(outputPath)) { | ||
this.cAnnoClass = (Class<? extends Annotation>)loader.loadClass("C"); | ||
this.cValueMethod = findMethod(cAnnoClass, "value"); | ||
|
||
Class<?> headerClass = loader.loadClass("test8252634_h"); | ||
checkGlobalFunctions(headerClass); | ||
checkGlobalVariables(headerClass); | ||
|
||
Class<?> pointClass = loader.loadClass("test8252634_h$Point"); | ||
checkPointGetters(pointClass); | ||
checkPointSetters(pointClass); | ||
checkPointAllocate(pointClass); | ||
} finally { | ||
deleteDir(outputPath); | ||
} | ||
} | ||
|
||
private void checkGlobalFunctions(Class<?> headerClass) throws Throwable { | ||
Method make = findMethod(headerClass, "make", int.class, int.class); | ||
Parameter[] params = make.getParameters(); | ||
checkAnnotation(params[0].getAnnotatedType(), "int"); | ||
checkAnnotation(params[1].getAnnotatedType(), "int"); | ||
checkAnnotation(make.getAnnotatedReturnType(), "struct Point*"); | ||
Method func = findFirstMethod(headerClass, "func"); | ||
params = func.getParameters(); | ||
checkAnnotation(params[0].getAnnotatedType(), "int(*)(int)"); | ||
} | ||
|
||
private void checkGlobalVariables(Class<?> headerClass) throws Throwable { | ||
Method pGetter = findMethod(headerClass, "p$get"); | ||
checkAnnotation(pGetter.getAnnotatedReturnType(), "int_ptr"); | ||
Method pSetter = findMethod(headerClass, "p$set", MemoryAddress.class); | ||
checkAnnotation(pSetter.getParameters()[0].getAnnotatedType(), "int_ptr"); | ||
} | ||
|
||
private void checkPointGetters(Class<?> pointClass) throws Throwable { | ||
Method xGetter = findMethod(pointClass, "x$get", MemorySegment.class); | ||
checkAnnotation(xGetter.getParameters()[0].getAnnotatedType(), "struct Point"); | ||
checkAnnotation(xGetter.getAnnotatedReturnType(), "int"); | ||
Method yGetter = findMethod(pointClass, "y$get", MemorySegment.class); | ||
checkAnnotation(yGetter.getParameters()[0].getAnnotatedType(), "struct Point"); | ||
checkAnnotation(yGetter.getAnnotatedReturnType(), "int"); | ||
Method xIndexedGetter = findMethod(pointClass, "x$get", MemorySegment.class, long.class); | ||
checkAnnotation(xIndexedGetter.getParameters()[0].getAnnotatedType(), "struct Point"); | ||
checkAnnotation(xIndexedGetter.getAnnotatedReturnType(), "int"); | ||
Method yIndexedGetter = findMethod(pointClass, "y$get", MemorySegment.class, long.class); | ||
checkAnnotation(yIndexedGetter.getParameters()[0].getAnnotatedType(), "struct Point"); | ||
checkAnnotation(yIndexedGetter.getAnnotatedReturnType(), "int"); | ||
} | ||
|
||
private void checkPointSetters(Class<?> pointClass) throws Throwable { | ||
Method xSetter = findMethod(pointClass, "x$set", MemorySegment.class, int.class); | ||
checkAnnotation(xSetter.getParameters()[0].getAnnotatedType(), "struct Point"); | ||
checkAnnotation(xSetter.getParameters()[1].getAnnotatedType(), "int"); | ||
Method ySetter = findMethod(pointClass, "y$set", MemorySegment.class, int.class); | ||
checkAnnotation(ySetter.getParameters()[0].getAnnotatedType(), "struct Point"); | ||
checkAnnotation(ySetter.getParameters()[1].getAnnotatedType(), "int"); | ||
Method xIndexedSetter = findMethod(pointClass, "x$set", MemorySegment.class, long.class, int.class); | ||
checkAnnotation(xIndexedSetter.getParameters()[0].getAnnotatedType(), "struct Point"); | ||
checkAnnotation(xIndexedSetter.getParameters()[2].getAnnotatedType(), "int"); | ||
Method yIndexedSetter = findMethod(pointClass, "y$set", MemorySegment.class, long.class, int.class); | ||
checkAnnotation(yIndexedSetter.getParameters()[0].getAnnotatedType(), "struct Point"); | ||
checkAnnotation(yIndexedSetter.getParameters()[2].getAnnotatedType(), "int"); | ||
} | ||
|
||
private void checkPointAllocate(Class<?> pointClass) throws Throwable { | ||
Method allocate = findMethod(pointClass, "allocate"); | ||
checkAnnotation(allocate.getAnnotatedReturnType(), "struct Point"); | ||
Method allocateArray = findMethod(pointClass, "allocateArray", int.class); | ||
checkAnnotation(allocateArray.getAnnotatedReturnType(), "struct Point[]"); | ||
} | ||
|
||
private void checkAnnotation(AnnotatedElement ae, String expected) throws Throwable { | ||
Object anno = ae.getAnnotation(cAnnoClass); | ||
assertEquals(cValueMethod.invoke(anno).toString(), expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif // __cplusplus | ||
|
||
struct Point { | ||
int x; int y; | ||
}; | ||
|
||
struct Point* make(int x, int y); | ||
|
||
void func(int (*callback)(int)); | ||
|
||
sundararajana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
typedef int* int_ptr; | ||
int_ptr p; | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif // __cplusplus |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be problematic for multiple nested function types, e.g.
The return type could also be a function pointer, so it's not always the first occurrence that needs to be replaced either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might just want to revisit the return type and argumentTypes, similar to what you do in visitFunction, but use
(*)(
as a joiner instead of(
.