Skip to content

Commit

Permalink
Refactor parser functions in PanamaLibclangParser.kt
Browse files Browse the repository at this point in the history
Renamed and refactored several handling methods in PanamaLibclangParser.kt for handling specific declaration types. This includes 'Typedef' and 'Scoped'. Also these changes affect toNativeStructure and toNativeEnumeration in NativeStructure.kt and NativeEnumeration.kt respectively. The refactor enhances the readability and clarifies the specific functionalities of each method.
  • Loading branch information
Alexandre Mommers committed Jan 1, 2024
1 parent 6385f9c commit 8255ec0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.openjdk.jextract.Declaration
import org.openjdk.jextract.Declaration.Scoped
import org.openjdk.jextract.Declaration.Typedef
import org.openjdk.jextract.JextractTool
import org.openjdk.jextract.impl.TypeImpl
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
Expand All @@ -38,8 +39,8 @@ fun parseFileWithPanama(file: String): DeclarationRepository = InMemoryDeclarati
.asSequence()
.map {
when (it) {
is Scoped -> it.toLocalDeclaration()
is Typedef -> it.toLocalDeclaration()
is Scoped -> it.scopedToLocalDeclaration()
is Typedef -> it.typeDefToLocalDeclaration()
is Declaration.Function -> it.toNativeTypeAlias()
else -> {
logger.error { "not found $it" }
Expand All @@ -52,15 +53,18 @@ fun parseFileWithPanama(file: String): DeclarationRepository = InMemoryDeclarati

}

private fun Typedef.toLocalDeclaration(): NameableDeclaration? {
return toNativeTypeAlias()
private fun Typedef.typeDefToLocalDeclaration(): NameableDeclaration? = type().let { type ->
when (type) {
is TypeImpl.DeclaredImpl -> type.tree().scopedToLocalDeclaration(name())
else -> toNativeTypeAlias()
}
}

private fun Scoped.toLocalDeclaration(): NameableDeclaration? {
private fun Scoped.scopedToLocalDeclaration(name: String? = null): NameableDeclaration? {
return when (kind()) {
Declaration.Scoped.Kind.ENUM -> toNativeEnumeration()
Declaration.Scoped.Kind.STRUCT -> toNativeStructure()
Declaration.Scoped.Kind.UNION -> toNativeStructure(isUnion = true)
Declaration.Scoped.Kind.ENUM -> toNativeEnumeration(name)
Declaration.Scoped.Kind.STRUCT -> toNativeStructure(name)
Declaration.Scoped.Kind.UNION -> toNativeStructure(name, isUnion = true)

else -> {
logger.error { "not found ${kind()}" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package klang.parser.libclang.panama
import klang.domain.NativeEnumeration
import org.openjdk.jextract.Declaration

internal fun Declaration.Scoped.toNativeEnumeration() = NativeEnumeration(
name(),
internal fun Declaration.Scoped.toNativeEnumeration(name: String?) = NativeEnumeration(
name ?: name(),
members().toEnumValues()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import klang.domain.NativeStructure
import klang.domain.TypeRef
import org.openjdk.jextract.Declaration

internal fun Declaration.Scoped.toNativeStructure(isUnion: Boolean = false) = NativeStructure(
name(),
internal fun Declaration.Scoped.toNativeStructure(name: String?, isUnion: Boolean = false) = NativeStructure(
name ?: name(),
members().toStructureField(),
isUnion
)
Expand Down

0 comments on commit 8255ec0

Please sign in to comment.