Skip to content

Commit

Permalink
chore(generator): [generator] remove usage of JavaPoet
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 20, 2025
1 parent 5d298e8 commit b932ead
Show file tree
Hide file tree
Showing 1,436 changed files with 24,774 additions and 24,802 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ The latest Javadoc can be found [here](https://over-run.github.io/overrungl/).

The documentation of OpenGL can be found from [Khronos' references](https://registry.khronos.org/OpenGL-Refpages/gl4/) and [docs.gl](https://docs.gl/).

[JavaPoet](https://github.com/palantir/javapoet) is used to generate source files.

### Credits

[<img src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.png" alt="JetBrains Logo (Main) logo." width="128" height="128">](https://jb.gg/OpenSourceSupport)
2 changes: 0 additions & 2 deletions buildSrc/src/main/kotlin/generator.conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
plugins { kotlin("jvm") }

val javaPoetVersion: String by rootProject
val kotlinTargetJdkVersion: String by rootProject

repositories {
mavenCentral()
}

dependencies {
implementation("com.palantir.javapoet:javapoet:$javaPoetVersion")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package overrungl.glfw

import com.palantir.javapoet.TypeName
import overrungl.gen.*

const val glfwPackage = "overrungl.glfw"
const val glfwLookup = "GLFWInternal.lookup()"

val glfw_boolean = CustomTypeSpec(
carrier = TypeName.INT,
javaType = TypeName.BOOLEAN,
carrier = "int",
javaType = "boolean",
processor = object : ValueProcessor {
override fun marshal(context: ProcessorContext) {
val builder = context.builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package overrungl.opengl

import com.palantir.javapoet.TypeName
import overrungl.gen.CustomTypeSpec
import overrungl.gen.commentedFileHeader
import overrungl.gen.writeString
Expand Down Expand Up @@ -152,7 +151,7 @@ class InstanceDowncall(

sb.appendLine(""" if (Unmarshal.isNullPointer(handles.PFN_${m.entrypoint})) throw new SymbolNotFoundError("Symbol not found: ${m.entrypoint}");""")
sb.append(" try { ")
if (m.returnType.carrier != TypeName.VOID) {
if (m.returnType.carrier != "void") {
sb.append("return (${m.returnType.carrier}) ")
}
sb.append("Handles.MH_${m.entrypoint}.invokeExact(handles.PFN_${m.entrypoint}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package overrungl.opengl

import com.palantir.javapoet.TypeName
import org.w3c.dom.Element
import org.w3c.dom.Node
import org.w3c.dom.Text
Expand Down Expand Up @@ -403,7 +402,7 @@ fun computeFunctionDescriptor(command: GLCommand): String {
val paramTypeSpecs = command.params.map { computePType(it.type) }
return buildString {
append("FunctionDescriptor.of")
if (returnTypeSpec.carrier == TypeName.VOID) {
if (returnTypeSpec.carrier == "void") {
append("Void(")
} else {
append("(")
Expand Down
45 changes: 19 additions & 26 deletions generators/src/main/kotlin/overrungl/gen/CustomTypeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,9 @@

package overrungl.gen

import com.palantir.javapoet.ArrayTypeName
import com.palantir.javapoet.ClassName
import com.palantir.javapoet.TypeName
import java.lang.foreign.Arena
import java.lang.foreign.MemorySegment
import java.lang.foreign.SegmentAllocator

data class CustomTypeSpec(
val carrier: TypeName,
val javaType: TypeName,
val carrier: String,
val javaType: String,
val processor: ValueProcessor = IdentityValueProcessor,
val layout: String?,
val cType: String? = null,
Expand All @@ -34,7 +27,7 @@ data class CustomTypeSpec(
val array: CustomTypeSpec by lazy {
CustomTypeSpec(
MemorySegment_,
ArrayTypeName.of(javaType),
"$javaType[]",
processor,
layout,
cType,
Expand All @@ -44,36 +37,36 @@ data class CustomTypeSpec(

infix fun c(cType: String?): CustomTypeSpec = copy(cType = cType)

fun typeNameWithC(typeName: TypeName): String =
fun typeNameWithC(typeName: String): String =
if (cType != null) """@CType("$cType") $typeName""" else typeName.toString()

fun selectTypeName(overload: Boolean): TypeName =
fun selectTypeName(overload: Boolean): String =
if (overload) javaType else carrier

fun carrierWithC(): String = typeNameWithC(carrier)
fun javaTypeWithC(): String = typeNameWithC(javaType)
}

val Arena_: ClassName = ClassName.get(Arena::class.java)
val MemorySegment_: ClassName = ClassName.get(MemorySegment::class.java)
val SegmentAllocator_: ClassName = ClassName.get(SegmentAllocator::class.java)
val String_: ClassName = ClassName.get(String::class.java)
const val Arena_: String = "Arena"
const val MemorySegment_: String = "MemorySegment"
const val SegmentAllocator_: String = "SegmentAllocator"
const val String_: String = "String"

private fun javaPrimitive(typeName: TypeName, layoutName: String): CustomTypeSpec =
private fun javaPrimitive(typeName: String, layoutName: String): CustomTypeSpec =
CustomTypeSpec(typeName, typeName, layout = "ValueLayout.$layoutName")

val arena = CustomTypeSpec(Arena_, Arena_, layout = null)
val allocator = CustomTypeSpec(SegmentAllocator_, SegmentAllocator_, layout = null)

val jboolean = javaPrimitive(TypeName.BOOLEAN, "JAVA_BOOLEAN")
val jchar = javaPrimitive(TypeName.CHAR, "JAVA_CHAR")
val jbyte = javaPrimitive(TypeName.BYTE, "JAVA_BYTE")
val jshort = javaPrimitive(TypeName.SHORT, "JAVA_SHORT")
val jint = javaPrimitive(TypeName.INT, "JAVA_INT")
val jlong = javaPrimitive(TypeName.LONG, "JAVA_LONG")
val jfloat = javaPrimitive(TypeName.FLOAT, "JAVA_FLOAT")
val jdouble = javaPrimitive(TypeName.DOUBLE, "JAVA_DOUBLE")
val void = CustomTypeSpec(TypeName.VOID, TypeName.VOID, layout = "No layout for void")
val jboolean = javaPrimitive("boolean", "JAVA_BOOLEAN")
val jchar = javaPrimitive("char", "JAVA_CHAR")
val jbyte = javaPrimitive("byte", "JAVA_BYTE")
val jshort = javaPrimitive("short", "JAVA_SHORT")
val jint = javaPrimitive("int", "JAVA_INT")
val jlong = javaPrimitive("long", "JAVA_LONG")
val jfloat = javaPrimitive("float", "JAVA_FLOAT")
val jdouble = javaPrimitive("double", "JAVA_DOUBLE")
val void = CustomTypeSpec("void", "void", layout = "No layout for void")

val address = javaPrimitive(MemorySegment_, "ADDRESS")
val string_u8 = CustomTypeSpec(
Expand Down
8 changes: 3 additions & 5 deletions generators/src/main/kotlin/overrungl/gen/DowncallMethod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package overrungl.gen

import com.palantir.javapoet.TypeName

enum class AllocatorRequirement {
NO,
STACK,
Expand Down Expand Up @@ -47,11 +45,11 @@ data class DowncallMethod(
val functionDescriptor: String by lazy {
buildString {
append("FunctionDescriptor.of")
if (returnType.carrier == TypeName.VOID) {
if (returnType.carrier == "void") {
append("Void")
}
append("(")
if (returnType.carrier != TypeName.VOID) {
if (returnType.carrier != "void") {
append(returnType.layout)
if (parameters.isNotEmpty()) {
append(", ")
Expand Down Expand Up @@ -89,7 +87,7 @@ data class DowncallParameter(

fun marshalRef(overload: Boolean): Boolean = ref && type.selectTypeName(overload) != MemorySegment_

fun toString(typeName: TypeName): String {
fun toString(typeName: String): String {
return buildString {
if (ref) {
append("@Out ")
Expand Down
3 changes: 1 addition & 2 deletions generators/src/main/kotlin/overrungl/gen/StaticDowncall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package overrungl.gen

import com.palantir.javapoet.TypeName
import java.nio.file.Files
import kotlin.io.path.Path

Expand Down Expand Up @@ -204,7 +203,7 @@ class StaticDowncall(
// methods
methods.forEach { m ->
val chosenReturnType = m.returnType.selectTypeName(m.overload)
val returnVoid = chosenReturnType == TypeName.VOID
val returnVoid = chosenReturnType == "void"
sb.appendLine(
" public static ${m.returnType.typeNameWithC(chosenReturnType)} ${m.name}(${
m.parameters.joinToString { p ->
Expand Down
8 changes: 3 additions & 5 deletions generators/src/main/kotlin/overrungl/gen/Struct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package overrungl.gen

import com.palantir.javapoet.ArrayTypeName
import com.palantir.javapoet.ClassName
import java.nio.file.Files
import kotlin.io.path.Path

Expand All @@ -32,7 +30,7 @@ class Struct(
private val kindString = if (union) "union" else "struct"
private val members = mutableListOf<StructMember>()
val pointerType: CustomTypeSpec by lazy {
val className = ClassName.get(packageName, name)
val className = "$packageName.$name"
CustomTypeSpec(
MemorySegment_,
className,
Expand All @@ -42,7 +40,7 @@ class Struct(
)
}
val byValueType: CustomTypeSpec by lazy {
val className = ClassName.get(packageName, name)
val className = "$packageName.$name"
CustomTypeSpec(
MemorySegment_,
className,
Expand Down Expand Up @@ -450,7 +448,7 @@ data class FixedSizeStructMember(
override val type: CustomTypeSpec
get() = CustomTypeSpec(
carrier = MemorySegment_,
javaType = ArrayTypeName.of(componentType.javaType),
javaType = "${componentType.javaType}[]",
processor = IdentityValueProcessor,
layout = "MemoryLayout.sequenceLayout(${size}, ${componentType.layout})",
cType = "${componentType.cType}[$size]",
Expand Down
6 changes: 2 additions & 4 deletions generators/src/main/kotlin/overrungl/gen/Upcall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package overrungl.gen

import com.palantir.javapoet.ClassName
import com.palantir.javapoet.TypeName
import java.nio.file.Files
import kotlin.io.path.Path

fun generateUpcallType(packageName: String, name: String): CustomTypeSpec {
val typeName = ClassName.get(packageName, name)
val typeName = "$packageName.$name"
return CustomTypeSpec(
carrier = MemorySegment_,
javaType = typeName,
Expand Down Expand Up @@ -102,7 +100,7 @@ class Upcall(
| FunctionDescriptor DESCRIPTOR = FunctionDescriptor.of
""".trimMargin()
)
val returnVoid = targetMethodNotNull.returnType.carrier == TypeName.VOID
val returnVoid = targetMethodNotNull.returnType.carrier == "void"
if (returnVoid) {
sb.append("Void(")
} else {
Expand Down
6 changes: 2 additions & 4 deletions generators/src/main/kotlin/overrungl/gen/ValueProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package overrungl.gen

import com.palantir.javapoet.TypeName

data class ProcessorContext(
val allocatorName: String?,
val builder: StringBuilder,
Expand Down Expand Up @@ -77,7 +75,7 @@ class ArrayValueProcessor(private val asType: String) : ValueProcessor {
}
}

class StructProcessor(private val typeName: TypeName) : ValueProcessor {
class StructProcessor(private val typeName: String) : ValueProcessor {
override fun marshal(context: ProcessorContext) {
val builder = context.builder
builder.append("Marshal.marshal(")
Expand All @@ -97,7 +95,7 @@ class StructProcessor(private val typeName: TypeName) : ValueProcessor {
}
}

class UpcallProcessor(private val typeName: TypeName) : ValueProcessor {
class UpcallProcessor(private val typeName: String) : ValueProcessor {
override fun marshal(context: ProcessorContext) {
val builder = context.builder
builder.append("Marshal.marshal(${context.allocatorName}, ")
Expand Down
5 changes: 2 additions & 3 deletions generators/stb/src/main/kotlin/overrungl/stb/STBImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package overrungl.stb

import com.palantir.javapoet.TypeName
import overrungl.gen.*

val stbi_uc = char c "stbi_uc"
Expand Down Expand Up @@ -48,8 +47,8 @@ fun STBImage() {
interfaceMethod = "invoke"(jboolean, void_ptr("user"))
targetMethod = "invoke_"(
CustomTypeSpec(
carrier = TypeName.INT,
javaType = TypeName.BOOLEAN,
carrier = "int",
javaType = "boolean",
processor = object : ValueProcessor {
override fun marshal(context: ProcessorContext) {
val builder = context.builder
Expand Down
3 changes: 1 addition & 2 deletions generators/stb/src/main/kotlin/overrungl/stb/STBTruetype.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package overrungl.stb

import com.palantir.javapoet.ClassName
import overrungl.gen.*

val const_char_ptr_ptr = address c "const char **"
private val _STBRPRect = ClassName.get(stbPackage, "STBRPRect")
private val _STBRPRect = "$stbPackage.STBRPRect"
val stbrp_rect_ptr = CustomTypeSpec(
carrier = MemorySegment_,
javaType = _STBRPRect,
Expand Down
19 changes: 8 additions & 11 deletions generators/vulkan/src/main/kotlin/overrungl/vulkan/VkType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package overrungl.vulkan

import com.palantir.javapoet.ClassName
import com.palantir.javapoet.TypeName
import overrungl.gen.CustomTypeSpec

data class VkType(
Expand All @@ -27,16 +25,15 @@ data class VkType(
val struct: Boolean
) {
fun toCustomTypeSpec(): CustomTypeSpec {
//todo no TypeName
val typeName = when (javaName) {
"void" -> TypeName.VOID
"byte" -> TypeName.BYTE
"short" -> TypeName.SHORT
"int" -> TypeName.INT
"long" -> TypeName.LONG
"float" -> TypeName.FLOAT
"double" -> TypeName.DOUBLE
"MemorySegment" -> ClassName.get("java.lang.foreign", "MemorySegment")
"void" -> "void"
"byte" -> "byte"
"short" -> "short"
"int" -> "int"
"long" -> "long"
"float" -> "float"
"double" -> "double"
"MemorySegment" -> "MemorySegment"
else -> error(javaName)
}
return CustomTypeSpec(carrier = typeName, javaType = typeName, layout = layout, cType = originalName)
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ kotlinTargetJdkVersion=22

overrunPlatformVersion=1.0.0
graalvmVersion=24.1.1
javaPoetVersion=0.6.0
jetbrainsAnnotationsVersion=26.0.1
jmhVersion=1.37
jomlVersion=1.10.8
Expand Down
Loading

0 comments on commit b932ead

Please sign in to comment.