-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add raw pointer type for (void *) C types Some correction to jextract parser for typedefs
- Loading branch information
1 parent
c00f722
commit e968f47
Showing
18 changed files
with
451 additions
and
107 deletions.
There are no files selected for viewing
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
32 changes: 24 additions & 8 deletions
32
annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/libcurl/LibcurlTest.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 |
---|---|---|
@@ -1,27 +1,43 @@ | ||
package io.github.digitalsmile.gpio.libcurl; | ||
|
||
import io.github.digitalsmile.annotation.NativeMemoryException; | ||
import io.github.digitalsmile.gpio.libcurl.enums.CURLCode; | ||
import io.github.digitalsmile.gpio.libcurl.enums.CURLOption; | ||
import io.github.digitalsmile.gpio.libcurl.enums.CURLcode; | ||
import io.github.digitalsmile.gpio.libcurl.enums.CURLoption; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class LibcurlTest { | ||
|
||
// @Test | ||
// public void libcurl() throws NativeMemoryException { | ||
// try (var libcurl = new LibcurlNative()) { | ||
// var code = libcurl.globalInit(CurlConstants.CURL_GLOBAL_DEFAULT); | ||
// assertEquals(code, CURLCode.CURLE_OK); | ||
// var curl = libcurl.easyInit(); | ||
// code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_URL, "https://example.com"); | ||
// assertEquals(code, CURLCode.CURLE_OK); | ||
// code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_FOLLOWLOCATION, 1L); | ||
// assertEquals(code, CURLCode.CURLE_OK); | ||
// | ||
// code = libcurl.easyPerform(curl); | ||
// assertEquals(code, CURLCode.CURLE_OK); | ||
// } | ||
// } | ||
|
||
@Test | ||
public void libcurl() throws NativeMemoryException { | ||
try (var libcurl = new LibcurlNative()) { | ||
var code = libcurl.globalInit(CurlConstants.CURL_GLOBAL_DEFAULT); | ||
assertEquals(code, CURLCode.CURLE_OK); | ||
assertEquals(code, CURLcode.CURLE_OK); | ||
var curl = libcurl.easyInit(); | ||
code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_URL, "https://example.com"); | ||
assertEquals(code, CURLCode.CURLE_OK); | ||
code = libcurl.easySetOpt(curl, CURLOption.CURLOPT_FOLLOWLOCATION, 1L); | ||
assertEquals(code, CURLCode.CURLE_OK); | ||
code = libcurl.easySetOpt(curl, CURLoption.CURLOPT_URL, "https://example.com"); | ||
assertEquals(code, CURLcode.CURLE_OK); | ||
code = libcurl.easySetOpt(curl, CURLoption.CURLOPT_FOLLOWLOCATION, 1L); | ||
assertEquals(code, CURLcode.CURLE_OK); | ||
|
||
code = libcurl.easyPerform(curl); | ||
assertEquals(code, CURLCode.CURLE_OK); | ||
assertEquals(code, CURLcode.CURLE_OK); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
annotation-processor-test/src/test/java/io/github/digitalsmile/gpio/shared/SharedTest.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
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
135 changes: 135 additions & 0 deletions
135
...tation-processor/src/main/java/io/github/digitalsmile/composers/AutoFunctionComposer.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,135 @@ | ||
package io.github.digitalsmile.composers; | ||
|
||
import com.squareup.javapoet.*; | ||
import io.github.digitalsmile.PackageName; | ||
import io.github.digitalsmile.PrettyName; | ||
import io.github.digitalsmile.annotation.function.NativeCall; | ||
import io.github.digitalsmile.functions.FunctionNode; | ||
import io.github.digitalsmile.functions.ParameterNode; | ||
import io.github.digitalsmile.headers.mapping.*; | ||
|
||
import javax.annotation.processing.Messager; | ||
import javax.lang.model.element.Modifier; | ||
import javax.tools.Diagnostic; | ||
import java.lang.foreign.MemorySegment; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class AutoFunctionComposer { | ||
private final Messager messager; | ||
|
||
public AutoFunctionComposer(Messager messager) { | ||
this.messager = messager; | ||
} | ||
|
||
public String compose(String packageName, String originalName, List<FunctionNode> nodes) { | ||
var classBuilder = TypeSpec.interfaceBuilder(originalName + "AutoFunctions") | ||
//.addModifiers(Modifier.PUBLIC) | ||
.addSuperinterface(ClassName.get(packageName, originalName)); | ||
|
||
for (FunctionNode node : nodes) { | ||
var method = MethodSpec.methodBuilder(PrettyName.getVariableName(node.functionName())); | ||
|
||
var returnType = node.returnNode().getType(); | ||
switch (returnType) { | ||
case ArrayOriginalType arrayOriginalType -> { | ||
method.returns(createType(arrayOriginalType)); | ||
// if (arrayOriginalType.carrierClass().equals(String.class)) { | ||
// method.returns(String.class); | ||
// } else if (arrayOriginalType.carrierClass().equals(Object.class)) { | ||
// method.returns(Object.class.arrayType()); | ||
// } else { | ||
// var typePackageName = PackageName.getPackageName(arrayOriginalType.typeName()); | ||
// method.returns(ClassName.get(typePackageName, PrettyName.getObjectName(arrayOriginalType.typeName()))); | ||
// } | ||
} | ||
case ObjectOriginalType objectOriginalType -> { | ||
method.returns(createType(objectOriginalType)); | ||
// messager.printMessage(Diagnostic.Kind.ERROR, objectOriginalType.typeName() + " " + objectOriginalType.carrierClass()); | ||
// if (objectOriginalType.carrierClass().equals(void.class)) { | ||
// method.returns(void.class); | ||
// } else { | ||
// var typePackageName = PackageName.getPackageName(objectOriginalType.typeName()); | ||
// method.returns(ClassName.get(typePackageName, PrettyName.getObjectName(objectOriginalType.typeName()))); | ||
// } | ||
} | ||
case PrimitiveOriginalType _ -> { | ||
method.returns(returnType.carrierClass()); | ||
} | ||
default -> { | ||
} | ||
} | ||
|
||
|
||
//messager.printMessage(Diagnostic.Kind.ERROR, "F: " + node.functionName()); | ||
for (ParameterNode parameterNode : node.functionParameters()) { | ||
//messager.printMessage(Diagnostic.Kind.ERROR, node.functionName() + " " + parameterNode.nativeMemoryNode().getName()); | ||
var name = parameterNode.nativeMemoryNode().getName(); | ||
if (name.isEmpty()) { | ||
name = "arg" + node.functionParameters().indexOf(parameterNode); | ||
} | ||
var parameterType = parameterNode.nativeMemoryNode().getType(); | ||
var parameterName = PrettyName.getVariableName(name); | ||
switch (parameterType) { | ||
case ArrayOriginalType arrayOriginalType -> { | ||
method.addParameter(createType(arrayOriginalType), name); | ||
// if (arrayOriginalType.isObjectType()) { | ||
// if (arrayOriginalType.carrierClass().equals(String.class)) { | ||
// method.addParameter(String.class, name); | ||
// } else if (arrayOriginalType.carrierClass().equals(Object.class)) { | ||
// method.addParameter(Object.class.arrayType(), name); | ||
// } else { | ||
// var typePackageName = PackageName.getPackageName(arrayOriginalType.typeName()); | ||
// method.addParameter(ClassName.get(typePackageName, PrettyName.getObjectName(arrayOriginalType.typeName())), name); | ||
// } | ||
// } else { | ||
// method.addParameter(arrayOriginalType.carrierClass().arrayType(), name); | ||
// } | ||
} | ||
case ObjectOriginalType objectOriginalType -> { | ||
method.addParameter(createType(objectOriginalType), name); | ||
// if (objectOriginalType.carrierClass().equals(void.class)) { | ||
// method.addParameter(MemorySegment.class, name); | ||
// } else { | ||
// var typePackageName = PackageName.getPackageName(objectOriginalType.typeName()); | ||
// method.addParameter(ClassName.get(typePackageName, PrettyName.getObjectName(objectOriginalType.typeName())), parameterName); | ||
// } | ||
} | ||
case PrimitiveOriginalType _ -> { | ||
method.addParameter(parameterType.carrierClass(), parameterName); | ||
} | ||
default -> { | ||
} | ||
} | ||
|
||
} | ||
|
||
classBuilder.addMethod(method | ||
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).build()); | ||
} | ||
|
||
|
||
var builder = JavaFile.builder(packageName, classBuilder.build()); | ||
var outputFile = builder.indent("\t").skipJavaLangImports(true).build(); | ||
return outputFile.toString(); | ||
} | ||
|
||
|
||
private TypeName createType(OriginalType type, boolean isArray) { | ||
var typeName = type.typeName(); | ||
var clazz = type.carrierClass(); | ||
if (type.carrierClass().equals(Object.class)) { | ||
var resolvedPackageName = PackageName.getPackageName(typeName); | ||
if (typeName.equals(String.class.getSimpleName())) { | ||
resolvedPackageName = ""; | ||
} | ||
return ClassName.get(resolvedPackageName, PrettyName.getObjectName(typeName) + (isArray ? "[]" : "")); | ||
} else { | ||
return TypeName.get(isArray ? clazz.arrayType() : clazz); | ||
} | ||
} | ||
|
||
private TypeName createType(OriginalType type) { | ||
return createType(type, false); | ||
} | ||
} |
Oops, something went wrong.