From 41129aa78c3a7c5cd5abbcfb68351de509f7f035 Mon Sep 17 00:00:00 2001 From: Alexandre Mommers Date: Wed, 14 Feb 2024 17:01:23 +0100 Subject: [PATCH] Consolidate domain name types to NotBlankString The code has replaced multiple instances of generic String types used for domain names with NotBlankString. This enforces a constraint where domain names cannot be blank and reduces redundancy in validations. Any function, struct or enum now requires a non-blank identifier for easier referencing and error-checking. Also, the assert function has been replaced by check for better error handling. --- .../main/kotlin/io/ygdrasil/KlangPlugin.kt | 8 +- klang/jextract/src/main/kotlin/klang/test.kt | 11 +-- .../main/kotlin/klang/CDefaultDeclaration.kt | 13 ++- .../kotlin/klang/DeclarationRepository.kt | 28 +++++-- .../klang/ObjectiveCDefaultDeclarations.kt | 33 ++++---- .../kotlin/klang/domain/NativeDeclaration.kt | 2 +- .../kotlin/klang/domain/NativeEnumeration.kt | 2 +- .../kotlin/klang/domain/NativeFunction.kt | 2 +- .../kotlin/klang/domain/NativeStructure.kt | 2 +- .../kotlin/klang/domain/NativeTypeAlias.kt | 2 +- .../kotlin/klang/domain/NativeVariable.kt | 2 +- .../kotlin/klang/domain/NotBlankString.kt | 16 ++++ .../kotlin/klang/domain/ObjectiveCCategory.kt | 2 +- .../kotlin/klang/domain/ObjectiveCClass.kt | 8 +- .../kotlin/klang/domain/ObjectiveCProtocol.kt | 2 +- .../main/kotlin/klang/domain/PrimitiveType.kt | 8 +- .../src/main/kotlin/klang/domain/TypeRef.kt | 20 ++--- .../klang/generator/NativeEnumeration.kt | 2 +- .../kotlin/klang/generator/NativeFunction.kt | 2 +- .../main/kotlin/klang/generator/Structure.kt | 2 +- .../main/kotlin/klang/generator/TypeAlias.kt | 2 +- .../main/kotlin/klang/helper/HeaderManager.kt | 2 +- .../main/kotlin/klang/mapper/Enumeration.kt | 2 +- .../src/main/kotlin/klang/mapper/Function.kt | 2 +- .../main/kotlin/klang/mapper/ObjectiveC.kt | 2 +- .../src/main/kotlin/klang/mapper/Structure.kt | 8 +- .../src/main/kotlin/klang/mapper/Type.kt | 10 +-- .../src/main/kotlin/klang/mapper/TypeAlias.kt | 4 +- .../parser/json/type/NativeEnumeration.kt | 5 +- .../klang/parser/json/type/NativeFunction.kt | 3 +- .../klang/parser/json/type/NativeStructure.kt | 9 +- .../klang/parser/json/type/NativeTypeAlias.kt | 3 +- .../parser/json/type/ObjectiveCCategory.kt | 4 +- .../klang/parser/json/type/ObjectiveCClass.kt | 6 +- .../parser/json/type/ObjectiveCProtocol.kt | 11 +-- .../parser/libclang/JnaLibClangParser.kt | 6 +- .../klang/parser/libclang/LibClangParser.kt | 10 +-- .../parser/libclang/PanamaLibclangParser.kt | 2 +- .../libclang/panama/NativeEnumeration.kt | 3 +- .../parser/libclang/panama/NativeFunction.kt | 3 +- .../parser/libclang/panama/NativeStructure.kt | 7 +- .../parser/libclang/panama/NativeTypeAlias.kt | 3 +- .../klang/parser/libclang/panama/TypeRef.kt | 9 +- .../parser/libclang/type/NativeFunction.kt | 3 +- .../klang/domain/EnumerationTypeResolving.kt | 5 +- .../kotlin/klang/domain/NotBlankStringTest.kt | 32 +++++++ .../klang/domain/StringTypeResolving.kt | 5 +- .../test/kotlin/klang/domain/TypeRefTest.kt | 20 ++--- .../klang/generator/CallbackGenerationTest.kt | 6 +- .../generator/EnumerationGenerationTest.kt | 6 +- .../klang/generator/FunctionGenerationTest.kt | 3 +- ...ctionGenerationWithStructurePointerTest.kt | 8 +- .../generator/ObjectiveCGenerationTest.kt | 3 +- .../generator/StructureGenerationTest.kt | 8 +- .../StructureGenerationWithCallbackTest.kt | 10 ++- .../StructureGenerationWithEnumerationTest.kt | 10 ++- ...ructureGenerationWithPrimitiveArrayTest.kt | 8 +- .../StructureWithStructureGenerationTest.kt | 10 ++- .../klang/generator/UnionGenerationTest.kt | 8 +- .../klang/mapper/EnumerationMapperTest.kt | 5 +- .../src/test/kotlin/klang/mapper/TypeTest.kt | 9 +- .../kotlin/klang/parser/ParserTestCommon.kt | 3 +- .../src/test/kotlin/klang/parser/TestData.kt | 83 ++++++++++--------- .../klang/parser/json/darwin/CocoaItTest.kt | 2 +- .../parser/json/darwin/FoundationItTest.kt | 2 +- .../klang/parser/json/darwin/MetalItTest.kt | 2 +- .../klang/parser/libclang/SDL2ItTest.kt | 2 +- .../src/main/kotlin/klang/parser/TypeMaker.kt | 2 +- .../src/test/kotlin/klang/TestUtils.kt | 2 +- 69 files changed, 308 insertions(+), 232 deletions(-) create mode 100644 klang/klang/src/main/kotlin/klang/domain/NotBlankString.kt create mode 100644 klang/klang/src/test/kotlin/klang/domain/NotBlankStringTest.kt diff --git a/klang/gradle-plugin/src/main/kotlin/io/ygdrasil/KlangPlugin.kt b/klang/gradle-plugin/src/main/kotlin/io/ygdrasil/KlangPlugin.kt index 161ad370..70c694fd 100644 --- a/klang/gradle-plugin/src/main/kotlin/io/ygdrasil/KlangPlugin.kt +++ b/klang/gradle-plugin/src/main/kotlin/io/ygdrasil/KlangPlugin.kt @@ -138,10 +138,10 @@ class KlangPlugin : Plugin { .map { Triple(it.sourceFile, workingDirectory.resolve(it.sourcePath), it.onSuccess) } .forEach { (fileToParse, sourcePath, onSuccess) -> val localFileToParse = File(fileToParse) - assert(localFileToParse.exists()) { "File to parse does not exist" } - assert(localFileToParse.isFile()) { "${localFileToParse.absolutePath} is not a file" } - assert(localFileToParse.canRead()) { "${localFileToParse.absolutePath} is not readable" } - assert(localFileToParse.length() > 0) { "${localFileToParse.absolutePath} is empty" } + check(localFileToParse.exists()) { "File to parse does not exist" } + check(localFileToParse.isFile()) { "${localFileToParse.absolutePath} is not a file" } + check(localFileToParse.canRead()) { "${localFileToParse.absolutePath} is not readable" } + check(localFileToParse.length() > 0) { "${localFileToParse.absolutePath} is empty" } extension.declarations = when (extension.parsingMethod) { ParsingMethod.Docker -> { diff --git a/klang/jextract/src/main/kotlin/klang/test.kt b/klang/jextract/src/main/kotlin/klang/test.kt index cd685769..120cc9d9 100644 --- a/klang/jextract/src/main/kotlin/klang/test.kt +++ b/klang/jextract/src/main/kotlin/klang/test.kt @@ -1,22 +1,13 @@ package klang import org.openjdk.jextract.Declaration -import org.openjdk.jextract.JextractTool -import org.openjdk.jextract.impl.CodeGenerator -import org.openjdk.jextract.impl.IncludeHelper import org.openjdk.jextract.impl.Parser -import org.openjdk.jextract.impl.Writer -import java.io.File import java.io.IOException import java.io.UncheckedIOException import java.nio.file.Files import java.nio.file.Path -import java.nio.file.Paths -import java.util.* import java.util.stream.Collectors import java.util.stream.Stream -import javax.tools.JavaFileObject -import kotlin.io.path.exists fun parse(headers: List, vararg parserOptions: String?): Declaration.Scoped { val source = if (headers.size > 1) generateTmpSource(headers) else headers.iterator().next()!! @@ -24,7 +15,7 @@ fun parse(headers: List, vararg parserOptions: String?): Declaration.Scope } private fun generateTmpSource(headers: List): Path { - assert(headers.size > 1) + check(headers.size > 1) try { val tmpFile = Files.createTempFile("jextract", ".h") tmpFile.toFile().deleteOnExit() diff --git a/klang/klang/src/main/kotlin/klang/CDefaultDeclaration.kt b/klang/klang/src/main/kotlin/klang/CDefaultDeclaration.kt index a449a22b..0fc0de9c 100644 --- a/klang/klang/src/main/kotlin/klang/CDefaultDeclaration.kt +++ b/klang/klang/src/main/kotlin/klang/CDefaultDeclaration.kt @@ -1,12 +1,13 @@ package klang import klang.domain.FixeSizeType +import klang.domain.NotBlankString import klang.domain.PlatformDependantSizeType import klang.domain.VoidType fun DeclarationRepository.insertCDefaultDeclaration() { save(VoidType) - save(PlatformDependantSizeType(32..64, "size_t")) + save(PlatformDependantSizeType(32..64, NotBlankString("size_t"))) byteType.forEach { save(FixeSizeType(8, it)) } shortType.forEach { save(FixeSizeType(16, it)) } intType.forEach { save(FixeSizeType(32, it)) } @@ -19,24 +20,32 @@ fun DeclarationRepository.insertCDefaultDeclaration() { // 8 bits private val byteType = listOf("char", "unsigned char", "uint16_t", "int16_t") + .map { NotBlankString(it) } // 16 bits private val shortType = listOf("short", "unsigned short", "uint8_t", "int8_t") + .map { NotBlankString(it) } // 32 bits private val intType = listOf("int", "unsigned int", "uint32_t", "int32_t") + .map { NotBlankString(it) } // 32 to 64 bits private val longType = listOf("long", "unsigned long") + .map { NotBlankString(it) } // 32 to 64 bits private val charType = listOf("wchar_t") + .map { NotBlankString(it) } // 64 bits private val int64Type = listOf("uint64_t", "int64_t") + .map { NotBlankString(it) } // 32 bits private val floatType = listOf("float") + .map { NotBlankString(it) } // 64 bits -private val doubleType = listOf("double") \ No newline at end of file +private val doubleType = listOf("double") + .map { NotBlankString(it) } \ No newline at end of file diff --git a/klang/klang/src/main/kotlin/klang/DeclarationRepository.kt b/klang/klang/src/main/kotlin/klang/DeclarationRepository.kt index b5fceda8..bcde2f09 100644 --- a/klang/klang/src/main/kotlin/klang/DeclarationRepository.kt +++ b/klang/klang/src/main/kotlin/klang/DeclarationRepository.kt @@ -9,6 +9,8 @@ val libraryDeclarationsFilter: (ResolvableDeclaration) -> Boolean = { resolvabl } } +val allDeclarationsFilter: (ResolvableDeclaration) -> Boolean = { _ -> true } + interface DeclarationRepository { val declarations: Set @@ -18,19 +20,26 @@ interface DeclarationRepository { fun update(nativeEnumeration: NativeDeclaration, provider: () -> NativeDeclaration): NativeDeclaration fun resolveTypes(filter: (ResolvableDeclaration) -> Boolean = libraryDeclarationsFilter) - fun findEnumerationByName(name: String) = findDeclarationByName(name) + fun findEnumerationByName(name: String) = findEnumerationByName(NotBlankString(name)) + fun findEnumerationByName(name: NotBlankString) = findDeclarationByName(name) - fun findStructureByName(name: String) = findDeclarationByName(name) + fun findStructureByName(name: String) = findStructureByName(NotBlankString(name)) + fun findStructureByName(name: NotBlankString) = findDeclarationByName(name) - fun findFunctionByName(name: String) = findDeclarationByName(name) + fun findFunctionByName(name: String) = findFunctionByName(NotBlankString(name)) + fun findFunctionByName(name: NotBlankString) = findDeclarationByName(name) - fun findTypeAliasByName(name: String) = findDeclarationByName(name) + fun findTypeAliasByName(name: String) = findTypeAliasByName(NotBlankString(name)) + fun findTypeAliasByName(name: NotBlankString) = findDeclarationByName(name) - fun findObjectiveCClassByName(name: String) = findDeclarationByName(name) + fun findObjectiveCClassByName(name: String) = findObjectiveCClassByName(NotBlankString(name)) + fun findObjectiveCClassByName(name: NotBlankString) = findDeclarationByName(name) - fun findObjectiveCProtocolByName(name: String) = findDeclarationByName(name) + fun findObjectiveCProtocolByName(name: String) = findObjectiveCProtocolByName(NotBlankString(name)) + fun findObjectiveCProtocolByName(name: NotBlankString) = findDeclarationByName(name) - fun findObjectiveCCategoryByName(name: String) = findDeclarationByName(name) + fun findObjectiveCCategoryByName(name: String) = findObjectiveCCategoryByName(NotBlankString(name)) + fun findObjectiveCCategoryByName(name: NotBlankString) = findDeclarationByName(name) fun findLibraryDeclaration() = declarations.asSequence() .filterIsInstance() @@ -38,7 +47,10 @@ interface DeclarationRepository { .toList() } -inline fun DeclarationRepository.findDeclarationByName(declarationName: String) = declarations + +inline fun DeclarationRepository.findDeclarationByName(declarationName: String) = findDeclarationByName(NotBlankString(declarationName)) + +inline fun DeclarationRepository.findDeclarationByName(declarationName: NotBlankString) = declarations .asSequence() .filterIsInstance() .firstOrNull { it.name == declarationName } \ No newline at end of file diff --git a/klang/klang/src/main/kotlin/klang/ObjectiveCDefaultDeclarations.kt b/klang/klang/src/main/kotlin/klang/ObjectiveCDefaultDeclarations.kt index e266e202..73c50df4 100644 --- a/klang/klang/src/main/kotlin/klang/ObjectiveCDefaultDeclarations.kt +++ b/klang/klang/src/main/kotlin/klang/ObjectiveCDefaultDeclarations.kt @@ -2,10 +2,11 @@ package klang import klang.domain.DeclarationOrigin import klang.domain.NameableDeclaration +import klang.domain.NotBlankString import klang.domain.ObjectiveCProtocol object ObjectiveCRootClass: NameableDeclaration { - override val name: String = "NSObject" + override val name: NotBlankString = NotBlankString("NSObject") override val source: DeclarationOrigin = DeclarationOrigin.PlatformHeader } @@ -13,20 +14,20 @@ object ObjectiveCRootClass: NameableDeclaration { fun DeclarationRepository.insertObjectiveCDefaultDeclaration() { save(ObjectiveCRootClass) // TODO: insert default declarations with correct method - save(ObjectiveCProtocol("NSSecureCoding", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSXPCProxyCreating", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSLocking", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSProgressReporting", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSStream", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSProxy", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSXMLNode", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSClassDescription", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSUserScriptTask", setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSSecureCoding"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSXPCProxyCreating"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSLocking"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSProgressReporting"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSStream"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSProxy"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSXMLNode"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSClassDescription"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSUserScriptTask"), setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSScanner", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSOrderedSet", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSMutableOrderedSet", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSProcessInfo", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSXMLParser", setOf(), listOf(), listOf())) - save(ObjectiveCProtocol("NSProtocolChecker", setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSScanner"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSOrderedSet"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSMutableOrderedSet"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSProcessInfo"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSXMLParser"), setOf(), listOf(), listOf())) + save(ObjectiveCProtocol(NotBlankString("NSProtocolChecker"), setOf(), listOf(), listOf())) } \ No newline at end of file diff --git a/klang/klang/src/main/kotlin/klang/domain/NativeDeclaration.kt b/klang/klang/src/main/kotlin/klang/domain/NativeDeclaration.kt index 83938a74..dce90c33 100644 --- a/klang/klang/src/main/kotlin/klang/domain/NativeDeclaration.kt +++ b/klang/klang/src/main/kotlin/klang/domain/NativeDeclaration.kt @@ -37,7 +37,7 @@ sealed interface NativeDeclaration { * This interface represents a nameable declaration. */ interface NameableDeclaration : SourceableDeclaration { - val name: String + val name: NotBlankString } interface ResolvableDeclaration { diff --git a/klang/klang/src/main/kotlin/klang/domain/NativeEnumeration.kt b/klang/klang/src/main/kotlin/klang/domain/NativeEnumeration.kt index 960254b2..80c31182 100644 --- a/klang/klang/src/main/kotlin/klang/domain/NativeEnumeration.kt +++ b/klang/klang/src/main/kotlin/klang/domain/NativeEnumeration.kt @@ -5,7 +5,7 @@ import klang.DeclarationRepository val AnonymousEnumeration = "AnonymousEnumeration" data class NativeEnumeration( - override val name: String, + override val name: NotBlankString, var values: List> = emptyList(), //TODO add support for other types var type: TypeRef = typeOf("int").unchecked("Type 'int' not found"), diff --git a/klang/klang/src/main/kotlin/klang/domain/NativeFunction.kt b/klang/klang/src/main/kotlin/klang/domain/NativeFunction.kt index 4b0e1a8c..52c9e93c 100644 --- a/klang/klang/src/main/kotlin/klang/domain/NativeFunction.kt +++ b/klang/klang/src/main/kotlin/klang/domain/NativeFunction.kt @@ -3,7 +3,7 @@ package klang.domain import klang.DeclarationRepository data class NativeFunction( - override val name: String, + override val name: NotBlankString, var returnType: TypeRef, val arguments: List, override val source: DeclarationOrigin = DeclarationOrigin.UnknownOrigin diff --git a/klang/klang/src/main/kotlin/klang/domain/NativeStructure.kt b/klang/klang/src/main/kotlin/klang/domain/NativeStructure.kt index bc42cb0f..7d6b35b6 100644 --- a/klang/klang/src/main/kotlin/klang/domain/NativeStructure.kt +++ b/klang/klang/src/main/kotlin/klang/domain/NativeStructure.kt @@ -21,7 +21,7 @@ data class SubStructureField(override val name: String, val structure: NativeStr * @property isUnion Indicates whether the structure is a union */ data class NativeStructure( - override val name: String, + override val name: NotBlankString, var fields: List = listOf(), var isUnion: Boolean = false, override val source: DeclarationOrigin = DeclarationOrigin.UnknownOrigin, diff --git a/klang/klang/src/main/kotlin/klang/domain/NativeTypeAlias.kt b/klang/klang/src/main/kotlin/klang/domain/NativeTypeAlias.kt index ebbc5c39..a1b8abd9 100644 --- a/klang/klang/src/main/kotlin/klang/domain/NativeTypeAlias.kt +++ b/klang/klang/src/main/kotlin/klang/domain/NativeTypeAlias.kt @@ -3,7 +3,7 @@ package klang.domain import klang.DeclarationRepository data class NativeTypeAlias( - override val name: String, + override val name: NotBlankString, var typeRef: TypeRef, override val source: DeclarationOrigin = DeclarationOrigin.UnknownOrigin ) :NameableDeclaration, NativeDeclaration, ResolvableDeclaration { diff --git a/klang/klang/src/main/kotlin/klang/domain/NativeVariable.kt b/klang/klang/src/main/kotlin/klang/domain/NativeVariable.kt index b9136e59..a1a0a82c 100644 --- a/klang/klang/src/main/kotlin/klang/domain/NativeVariable.kt +++ b/klang/klang/src/main/kotlin/klang/domain/NativeVariable.kt @@ -1,7 +1,7 @@ package klang.domain data class NativeVariable( - override val name: String, + override val name: NotBlankString, val type: String, override val source: DeclarationOrigin ): NameableDeclaration, NativeDeclaration \ No newline at end of file diff --git a/klang/klang/src/main/kotlin/klang/domain/NotBlankString.kt b/klang/klang/src/main/kotlin/klang/domain/NotBlankString.kt new file mode 100644 index 00000000..82815b4f --- /dev/null +++ b/klang/klang/src/main/kotlin/klang/domain/NotBlankString.kt @@ -0,0 +1,16 @@ +package klang.domain + +fun notBlankString(value: String) = value.takeIf(String::isNotBlank) + ?.let { NotBlankString(value) } + +@JvmInline +value class NotBlankString(val value: String) : Comparable by value { + + init { + check(value.isNotBlank()) { error("value cannot be blank")} + } + + override fun toString(): String { + return value + } +} \ No newline at end of file diff --git a/klang/klang/src/main/kotlin/klang/domain/ObjectiveCCategory.kt b/klang/klang/src/main/kotlin/klang/domain/ObjectiveCCategory.kt index 87b20bdb..dd02e53b 100644 --- a/klang/klang/src/main/kotlin/klang/domain/ObjectiveCCategory.kt +++ b/klang/klang/src/main/kotlin/klang/domain/ObjectiveCCategory.kt @@ -5,7 +5,7 @@ import klang.DeclarationRepository internal val AnonymousCategoryName = "AnonymousCategory" data class ObjectiveCCategory( - override val name: String, + override val name: NotBlankString, var superType: TypeRef, val methods: List, override val source: DeclarationOrigin = DeclarationOrigin.UnknownOrigin diff --git a/klang/klang/src/main/kotlin/klang/domain/ObjectiveCClass.kt b/klang/klang/src/main/kotlin/klang/domain/ObjectiveCClass.kt index e655c28e..b9c02655 100644 --- a/klang/klang/src/main/kotlin/klang/domain/ObjectiveCClass.kt +++ b/klang/klang/src/main/kotlin/klang/domain/ObjectiveCClass.kt @@ -3,7 +3,7 @@ package klang.domain import klang.DeclarationRepository data class ObjectiveCClass( - override val name: String, + override val name: NotBlankString, var superType: TypeRef?, var protocols: Set, var properties: List, @@ -13,7 +13,7 @@ data class ObjectiveCClass( ) : NameableDeclaration, ResolvableDeclaration { data class Property( - override val name: String, + override val name: NotBlankString, val type: String, val assign: Boolean? = null, val readwrite: Boolean? = null, @@ -23,7 +23,7 @@ data class ObjectiveCClass( ) : NameableDeclaration data class Method( - override val name: String, + override val name: NotBlankString, var returnType: TypeRef, val instance: Boolean, val arguments: List = listOf(), @@ -61,7 +61,7 @@ data class ObjectiveCClass( categories = declarations .asSequence() .filterIsInstance() - .filter { it.superType.referenceAsString == name } + .filter { it.superType.referenceAsString == name.value } .toSet() methods.forEach { with(it) { resolve() } } diff --git a/klang/klang/src/main/kotlin/klang/domain/ObjectiveCProtocol.kt b/klang/klang/src/main/kotlin/klang/domain/ObjectiveCProtocol.kt index bb7082d1..70843946 100644 --- a/klang/klang/src/main/kotlin/klang/domain/ObjectiveCProtocol.kt +++ b/klang/klang/src/main/kotlin/klang/domain/ObjectiveCProtocol.kt @@ -1,7 +1,7 @@ package klang.domain data class ObjectiveCProtocol( - override val name: String, + override val name: NotBlankString, val protocols: Set, var properties: List, var methods: List, diff --git a/klang/klang/src/main/kotlin/klang/domain/PrimitiveType.kt b/klang/klang/src/main/kotlin/klang/domain/PrimitiveType.kt index 8cf59ed4..a8689d78 100644 --- a/klang/klang/src/main/kotlin/klang/domain/PrimitiveType.kt +++ b/klang/klang/src/main/kotlin/klang/domain/PrimitiveType.kt @@ -3,15 +3,15 @@ package klang.domain sealed class PrimitiveType: NameableDeclaration data object VoidType: PrimitiveType() { - override val name: String = "void" + override val name: NotBlankString = NotBlankString("void") override val source: DeclarationOrigin = DeclarationOrigin.PlatformHeader } -class FixeSizeType(val size: Int, override val name: String, val isFloating: Boolean = false, override val source: DeclarationOrigin = DeclarationOrigin.PlatformHeader): PrimitiveType() -class PlatformDependantSizeType(val size: IntRange, override val name: String, override val source: DeclarationOrigin = DeclarationOrigin.PlatformHeader): PrimitiveType() +class FixeSizeType(val size: Int, override val name: NotBlankString, val isFloating: Boolean = false, override val source: DeclarationOrigin = DeclarationOrigin.PlatformHeader): PrimitiveType() +class PlatformDependantSizeType(val size: IntRange, override val name: NotBlankString, override val source: DeclarationOrigin = DeclarationOrigin.PlatformHeader): PrimitiveType() data object StringType: PrimitiveType() { - override val name: String = "char *" + override val name: NotBlankString = NotBlankString("char *") override val source: DeclarationOrigin = DeclarationOrigin.PlatformHeader } diff --git a/klang/klang/src/main/kotlin/klang/domain/TypeRef.kt b/klang/klang/src/main/kotlin/klang/domain/TypeRef.kt index b71a8397..46853788 100644 --- a/klang/klang/src/main/kotlin/klang/domain/TypeRef.kt +++ b/klang/klang/src/main/kotlin/klang/domain/TypeRef.kt @@ -82,7 +82,7 @@ fun typeOf(reference: String): Either = either { } UnresolvedTypeRef( reference, - typeName, + NotBlankString(typeName), isConstant, isPointer, isStructure, @@ -99,7 +99,7 @@ fun typeOf(reference: String): Either = either { sealed interface TypeRef { val referenceAsString: String - val typeName: String + val typeName: NotBlankString val isConstant: Boolean val isPointer: Boolean val isStructure: Boolean @@ -113,7 +113,7 @@ sealed interface TypeRef { fun DeclarationRepository.resolveType(): TypeRef = when { isCallback -> ResolvedTypeRef(this@TypeRef, typeName.toFunctionPointerType()) - isPointer && typeName == "char" -> ResolvedTypeRef(this@TypeRef, StringType) + isPointer && typeName == NotBlankString("char") -> ResolvedTypeRef(this@TypeRef, StringType) else -> findDeclarationByName(typeName) ?.let { ResolvedTypeRef(this@TypeRef, it) } ?: (this@TypeRef.also { logger.warn { "fail to resolve type : $it" } }) @@ -121,10 +121,10 @@ sealed interface TypeRef { } -internal fun String.toFunctionPointerType(): FunctionPointerType { - val returnType = substringBefore("(").let { typeOf(it).unchecked() } +internal fun NotBlankString.toFunctionPointerType(): FunctionPointerType { + val returnType = value.substringBefore("(").let { typeOf(it).unchecked() } - val arguments = substringAfter("(*)") + val arguments = value.substringAfter("(*)") .replace("(", "") .replace(")", "") .split(",") @@ -138,7 +138,7 @@ internal fun String.toFunctionPointerType(): FunctionPointerType { class UnresolvedTypeRef internal constructor( override val referenceAsString: String, - override val typeName: String, + override val typeName: NotBlankString, override val isConstant: Boolean = false, override val isPointer: Boolean = false, override val isStructure: Boolean = false, @@ -161,12 +161,6 @@ class UnresolvedTypeRef internal constructor( return typeName.hashCode() } - init { - if (typeName == "char *") { - error("erratum") - } - } - } class ResolvedTypeRef internal constructor(private val typeRef: TypeRef, val type: NativeDeclaration) : diff --git a/klang/klang/src/main/kotlin/klang/generator/NativeEnumeration.kt b/klang/klang/src/main/kotlin/klang/generator/NativeEnumeration.kt index 4d3f6d6c..de00acc5 100644 --- a/klang/klang/src/main/kotlin/klang/generator/NativeEnumeration.kt +++ b/klang/klang/src/main/kotlin/klang/generator/NativeEnumeration.kt @@ -7,7 +7,7 @@ import java.io.File fun List.generateKotlinFile(outputDirectory: File, packageName: String) { - assert(outputDirectory.isDirectory) { "Output directory must be a directory" } + check(outputDirectory.isDirectory) { "Output directory must be a directory" } FileSpec.builder(packageName, "Enumerations") .also { builder -> forEach { builder.addType(it.toSpecAsEnumeration(packageName)) } } diff --git a/klang/klang/src/main/kotlin/klang/generator/NativeFunction.kt b/klang/klang/src/main/kotlin/klang/generator/NativeFunction.kt index 2e724753..965e00c3 100644 --- a/klang/klang/src/main/kotlin/klang/generator/NativeFunction.kt +++ b/klang/klang/src/main/kotlin/klang/generator/NativeFunction.kt @@ -8,7 +8,7 @@ import java.io.File fun List.generateKotlinFile(outputDirectory: File, packageName: String, libraryName: String) { - assert(outputDirectory.isDirectory) { "Output directory must be a directory" } + check(outputDirectory.isDirectory) { "Output directory must be a directory" } val libraryInterfaceName = "${libraryName}Library" diff --git a/klang/klang/src/main/kotlin/klang/generator/Structure.kt b/klang/klang/src/main/kotlin/klang/generator/Structure.kt index fd51ddbd..5254a487 100644 --- a/klang/klang/src/main/kotlin/klang/generator/Structure.kt +++ b/klang/klang/src/main/kotlin/klang/generator/Structure.kt @@ -8,7 +8,7 @@ import java.io.File fun List.generateKotlinFile(outputDirectory: File, packageName: String) { - assert(outputDirectory.isDirectory) { "Output directory must be a directory" } + check(outputDirectory.isDirectory) { "Output directory must be a directory" } FileSpec.builder(packageName, "Structures") .also { builder -> forEach { builder.addTypes(it.toSpec(packageName)) } } diff --git a/klang/klang/src/main/kotlin/klang/generator/TypeAlias.kt b/klang/klang/src/main/kotlin/klang/generator/TypeAlias.kt index 98263733..2d173753 100644 --- a/klang/klang/src/main/kotlin/klang/generator/TypeAlias.kt +++ b/klang/klang/src/main/kotlin/klang/generator/TypeAlias.kt @@ -9,7 +9,7 @@ import java.io.File fun List.generateKotlinFile(outputDirectory: File, packageName: String) { - assert(outputDirectory.isDirectory) { "Output directory must be a directory" } + check(outputDirectory.isDirectory) { "Output directory must be a directory" } FileSpec.builder(packageName, "TypeAlias") .also { fileSpec -> diff --git a/klang/klang/src/main/kotlin/klang/helper/HeaderManager.kt b/klang/klang/src/main/kotlin/klang/helper/HeaderManager.kt index 2fbe9bbb..2fd535fc 100644 --- a/klang/klang/src/main/kotlin/klang/helper/HeaderManager.kt +++ b/klang/klang/src/main/kotlin/klang/helper/HeaderManager.kt @@ -9,7 +9,7 @@ import kotlin.io.path.exists object HeaderManager { fun putPlatformHeaderAt(headerDirectoryPath: Path) { - assert(headerDirectoryPath.exists()) { "path ${headerDirectoryPath.absolutePathString()} does not exists" } + check(headerDirectoryPath.exists()) { "path ${headerDirectoryPath.absolutePathString()} does not exists" } val cHeadersFile = "/c-${inferPlatformSuffix()}-headers.zip" unzipFromClasspath(cHeadersFile, headerDirectoryPath.toFile()) diff --git a/klang/klang/src/main/kotlin/klang/mapper/Enumeration.kt b/klang/klang/src/main/kotlin/klang/mapper/Enumeration.kt index 48f08548..499e626a 100644 --- a/klang/klang/src/main/kotlin/klang/mapper/Enumeration.kt +++ b/klang/klang/src/main/kotlin/klang/mapper/Enumeration.kt @@ -3,7 +3,7 @@ package klang.mapper import com.squareup.kotlinpoet.* import klang.domain.NativeEnumeration -internal fun NativeEnumeration.toSpecAsEnumeration(packageName: String) = ClassName(packageName, name) +internal fun NativeEnumeration.toSpecAsEnumeration(packageName: String) = ClassName(packageName, name.value) .let { enumerationClass -> val valueType = type.toType(packageName) val valueName = "value" diff --git a/klang/klang/src/main/kotlin/klang/mapper/Function.kt b/klang/klang/src/main/kotlin/klang/mapper/Function.kt index cc12dde5..bd35431e 100644 --- a/klang/klang/src/main/kotlin/klang/mapper/Function.kt +++ b/klang/klang/src/main/kotlin/klang/mapper/Function.kt @@ -17,7 +17,7 @@ internal fun List.toInterfaceSpec(packageName: String, name: Str } private fun NativeFunction.toSpec(packageName: String) = FunSpec - .builder(name) + .builder(name.value) .addModifiers(KModifier.PUBLIC, KModifier.ABSTRACT) .returns(returnType.toType(packageName)) .addParameters(arguments.mapIndexed { index, argument -> argument.toSpec(packageName, index) }) diff --git a/klang/klang/src/main/kotlin/klang/mapper/ObjectiveC.kt b/klang/klang/src/main/kotlin/klang/mapper/ObjectiveC.kt index e3cecf75..a8ad1f6a 100644 --- a/klang/klang/src/main/kotlin/klang/mapper/ObjectiveC.kt +++ b/klang/klang/src/main/kotlin/klang/mapper/ObjectiveC.kt @@ -6,7 +6,7 @@ import com.squareup.kotlinpoet.ParameterSpec import com.squareup.kotlinpoet.TypeSpec import klang.domain.ObjectiveCClass -internal fun ObjectiveCClass.toSpec() = ClassName("", name) +internal fun ObjectiveCClass.toSpec() = ClassName("", name.value) .let { structureClass -> TypeSpec.classBuilder(structureClass) .superclass(nsobjectDefinition) diff --git a/klang/klang/src/main/kotlin/klang/mapper/Structure.kt b/klang/klang/src/main/kotlin/klang/mapper/Structure.kt index c8445be4..ad030e53 100644 --- a/klang/klang/src/main/kotlin/klang/mapper/Structure.kt +++ b/klang/klang/src/main/kotlin/klang/mapper/Structure.kt @@ -3,7 +3,7 @@ package klang.mapper import com.squareup.kotlinpoet.* import klang.domain.* -internal fun NativeStructure.toSpec(packageName: String) = ClassName("", name) +internal fun NativeStructure.toSpec(packageName: String) = ClassName("", name.value) .let { structureClass -> generateFunctionPointerTypeInterface(packageName) + when { fields.isEmpty() -> toSpecWithNoAttributes(structureClass) @@ -16,7 +16,7 @@ private fun NativeStructure.generateFunctionPointerTypeInterface(packageName: St //TODO support SubStructureField .filterIsInstance() .map { it.name to it.type } - .mapNotNull { it.toFunctionPointerTypeInterface(packageName, name) } + .mapNotNull { it.toFunctionPointerTypeInterface(packageName, name.value) } private fun Pair.toFunctionPointerTypeInterface(packageName: String, structureName: String) = let { (fieldName, typeRef) -> @@ -171,7 +171,7 @@ private fun ResolvedTypeRef.toPropertySpec( rootType is NativeStructure -> toType(packageName) // If FunctionPointerType generate an interface or use the one defined by the typealias rootType is FunctionPointerType -> when (type) { - is NativeTypeAlias -> ClassName(packageName, type.name) + is NativeTypeAlias -> ClassName(packageName, type.name.value) else -> ClassName(packageName, generateNativePointerName(nativeStructure, name)) }.copy(nullable = true) @@ -228,7 +228,7 @@ private fun generateNativePointerName(structureName: String, fieldName: String) "${structureName}${fieldName.replaceFirstChar { it.uppercaseChar() }}Function" private fun generateNativePointerName(nativeStructure: NativeStructure, fieldName: String) = - generateNativePointerName(nativeStructure.name, fieldName) + generateNativePointerName(nativeStructure.name.value, fieldName) private fun TypeRef.defaultPropertySpec( name: String diff --git a/klang/klang/src/main/kotlin/klang/mapper/Type.kt b/klang/klang/src/main/kotlin/klang/mapper/Type.kt index 9dc3f486..095cef91 100644 --- a/klang/klang/src/main/kotlin/klang/mapper/Type.kt +++ b/klang/klang/src/main/kotlin/klang/mapper/Type.kt @@ -13,10 +13,10 @@ internal fun TypeRef.toType(packageName: String, nullable: Boolean = false) = wh else -> ClassName("kotlin", "String") } - is NativeStructure -> ClassName(packageName, typeName) + is NativeStructure -> ClassName(packageName, typeName.value) is FunctionPointerType -> jnaCallback is PrimitiveType -> jnaPointer - else -> ClassName(packageName, typeName) + else -> ClassName(packageName, typeName.value) } else -> jnaPointer @@ -26,10 +26,10 @@ internal fun TypeRef.toType(packageName: String, nullable: Boolean = false) = wh is FunctionPointerType -> jnaCallback is VoidType -> ClassName("kotlin", "Unit") is PrimitiveType -> toPrimitiveType(packageName) - else -> ClassName(packageName, typeName) + else -> ClassName(packageName, typeName.value) } - else -> ClassName(packageName, typeName) + else -> ClassName(packageName, typeName.value) }.let { if (nullable) it.copy(nullable = true) else it } // @see https://github.com/java-native-access/jna/blob/master/www/Mappings.md @@ -84,6 +84,6 @@ private fun ResolvedTypeRef.toPrimitiveType(packageName: String): ClassName = th // Default else -> null } - } ?: ClassName(packageName, typeName) + } ?: ClassName(packageName, typeName.value) } \ No newline at end of file diff --git a/klang/klang/src/main/kotlin/klang/mapper/TypeAlias.kt b/klang/klang/src/main/kotlin/klang/mapper/TypeAlias.kt index 4e275d80..0a7fb8ee 100644 --- a/klang/klang/src/main/kotlin/klang/mapper/TypeAlias.kt +++ b/klang/klang/src/main/kotlin/klang/mapper/TypeAlias.kt @@ -20,12 +20,12 @@ internal fun NativeTypeAlias.toCallbackSpec( val typeRef = typeRef ensure(typeRef is ResolvedTypeRef) { "typeRef should be resolved" } ensure(typeRef.type is FunctionPointerType) { "Type must be a function pointer" } - typeRef.type.toCallbackSpec(name, packageName) + typeRef.type.toCallbackSpec(name.value, packageName) } private fun NativeTypeAlias.toTypeAliasSpec(packageName: String) = TypeAliasSpec - .builder(name, typeRef.toType(packageName)) + .builder(name.value, typeRef.toType(packageName)) .build() diff --git a/klang/klang/src/main/kotlin/klang/parser/json/type/NativeEnumeration.kt b/klang/klang/src/main/kotlin/klang/parser/json/type/NativeEnumeration.kt index e64eb05f..8a58424f 100644 --- a/klang/klang/src/main/kotlin/klang/parser/json/type/NativeEnumeration.kt +++ b/klang/klang/src/main/kotlin/klang/parser/json/type/NativeEnumeration.kt @@ -2,6 +2,7 @@ package klang.parser.json.type import klang.domain.AnonymousEnumeration import klang.domain.NativeEnumeration +import klang.domain.NotBlankString import klang.parser.json.domain.TranslationUnitKind import klang.parser.json.domain.TranslationUnitNode import klang.parser.json.domain.json @@ -10,12 +11,12 @@ import kotlinx.serialization.json.jsonPrimitive import kotlinx.serialization.json.longOrNull internal fun TranslationUnitNode.toNativeTypeDefEnumeration(sibling: TranslationUnitNode) = NativeEnumeration( - name = sibling.json.typeAliasName(), + name = NotBlankString(sibling.json.typeAliasName()), values = this.extractFields() ) internal fun TranslationUnitNode.toNativeEnumeration() = NativeEnumeration( - name = json.typeAliasName(), + name = NotBlankString(json.typeAliasName()), values = this.extractFields() ) diff --git a/klang/klang/src/main/kotlin/klang/parser/json/type/NativeFunction.kt b/klang/klang/src/main/kotlin/klang/parser/json/type/NativeFunction.kt index c835ea63..6de7f05f 100644 --- a/klang/klang/src/main/kotlin/klang/parser/json/type/NativeFunction.kt +++ b/klang/klang/src/main/kotlin/klang/parser/json/type/NativeFunction.kt @@ -1,6 +1,7 @@ package klang.parser.json.type import klang.domain.NativeFunction +import klang.domain.NotBlankString import klang.domain.typeOf import klang.domain.unchecked import klang.parser.json.domain.TranslationUnitKind @@ -10,7 +11,7 @@ import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonPrimitive internal fun TranslationUnitNode.toNativeFunction() = NativeFunction( - name = json.functionName(), + name = NotBlankString(json.functionName()), returnType = typeOf(json.type().adapt()).unchecked("fail to create type ${json.type()}"), arguments = arguments() ) diff --git a/klang/klang/src/main/kotlin/klang/parser/json/type/NativeStructure.kt b/klang/klang/src/main/kotlin/klang/parser/json/type/NativeStructure.kt index 7b89f49d..4e4216bb 100644 --- a/klang/klang/src/main/kotlin/klang/parser/json/type/NativeStructure.kt +++ b/klang/klang/src/main/kotlin/klang/parser/json/type/NativeStructure.kt @@ -1,9 +1,6 @@ package klang.parser.json.type -import klang.domain.NativeStructure -import klang.domain.StructureField -import klang.domain.TypeRefField -import klang.domain.typeOf +import klang.domain.* import klang.parser.json.domain.TranslationUnitKind import klang.parser.json.domain.TranslationUnitNode import klang.parser.json.domain.json @@ -12,13 +9,13 @@ import kotlinx.serialization.json.jsonPrimitive internal fun TranslationUnitNode.toNativeTypeDefStructure(sibling: TranslationUnitNode) = NativeStructure( - name = sibling.json.name(), + name = NotBlankString(sibling.json.name()), fields = this.extractFields(), isUnion = json.isUnion() ) internal fun TranslationUnitNode.toNativeStructure() = NativeStructure( - name = json.name(), + name = NotBlankString(json.name()), fields = this.extractFields(), isUnion = json.isUnion() ) diff --git a/klang/klang/src/main/kotlin/klang/parser/json/type/NativeTypeAlias.kt b/klang/klang/src/main/kotlin/klang/parser/json/type/NativeTypeAlias.kt index 15896d57..9b46deaa 100644 --- a/klang/klang/src/main/kotlin/klang/parser/json/type/NativeTypeAlias.kt +++ b/klang/klang/src/main/kotlin/klang/parser/json/type/NativeTypeAlias.kt @@ -1,12 +1,13 @@ package klang.parser.json.type import klang.domain.NativeTypeAlias +import klang.domain.NotBlankString import klang.domain.typeOf import klang.domain.unchecked import klang.parser.json.domain.TranslationUnitNode import klang.parser.json.domain.json internal fun TranslationUnitNode.toNativeTypeAlias()= NativeTypeAlias( - name = json.name(), + name = NotBlankString(json.name()), typeRef = json.type().let(::typeOf).unchecked("fail to parse type $this") ) \ No newline at end of file diff --git a/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCCategory.kt b/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCCategory.kt index 81e5dbde..2e12f603 100644 --- a/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCCategory.kt +++ b/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCCategory.kt @@ -11,7 +11,7 @@ import kotlinx.serialization.json.jsonPrimitive internal fun TranslationUnitNode.toObjectiveCCategory(): ObjectiveCCategory { return ObjectiveCCategory( - name = json.nullableName() ?: AnonymousCategoryName, + name = NotBlankString(json.nullableName() ?: AnonymousCategoryName), superType = json.superType(), methods = json.methods() ) @@ -32,7 +32,7 @@ private fun JsonObject.methods(): List = inner() ?.map { it.toMethod() } ?: listOf() private fun JsonObject.toMethod() = ObjectiveCClass.Method( - name = name(), + name = NotBlankString(name()), returnType = returnType(), instance = booleanValueOf("instance"), arguments = arguments() diff --git a/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCClass.kt b/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCClass.kt index fd3e7c88..ba7f5905 100644 --- a/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCClass.kt +++ b/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCClass.kt @@ -12,7 +12,7 @@ import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive internal fun TranslationUnitNode.toObjectiveCClass(): ObjectiveCClass { - val name = json.name() + val name = NotBlankString(json.name()) return ObjectiveCClass( name = name, superType = json.superType(), @@ -44,7 +44,7 @@ private fun JsonObject.methods(): List = inner() ?.map { it.toMethod() } ?: listOf() private fun JsonObject.toMethod() = ObjectiveCClass.Method( - name = name(), + name = NotBlankString(name()), returnType = returnType(), instance = booleanValueOf("instance"), arguments = arguments() @@ -64,7 +64,7 @@ private fun JsonObject.properties(): List = inner() ?.map { it.toProperty() } ?: listOf() private fun JsonObject.toProperty(): ObjectiveCClass.Property = ObjectiveCClass.Property( - name = name(), + name = NotBlankString(name()), type = type(), assign = nullableBooleanValueOf("assign"), readwrite = nullableBooleanValueOf("readwrite"), diff --git a/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCProtocol.kt b/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCProtocol.kt index 18b5db0b..79867c6c 100644 --- a/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCProtocol.kt +++ b/klang/klang/src/main/kotlin/klang/parser/json/type/ObjectiveCProtocol.kt @@ -1,9 +1,6 @@ package klang.parser.json.type -import klang.domain.ObjectiveCClass -import klang.domain.ObjectiveCProtocol -import klang.domain.typeOf -import klang.domain.unchecked +import klang.domain.* import klang.parser.json.domain.TranslationUnitKind import klang.parser.json.domain.TranslationUnitNode import klang.parser.json.domain.json @@ -15,7 +12,7 @@ import kotlinx.serialization.json.jsonPrimitive internal fun TranslationUnitNode.toObjectiveCProtocol(): ObjectiveCProtocol { return ObjectiveCProtocol( - name = json.name(), + name = NotBlankString(json.name()), protocols = json.protocols(), properties = json.properties(), methods = json.methods() @@ -34,7 +31,7 @@ private fun JsonObject.methods(): List = inner() ?.map { it.toMethod() } ?: listOf() private fun JsonObject.toMethod() = ObjectiveCClass.Method( - name = name(), + name = NotBlankString(name()), returnType = returnType(), instance = booleanValueOf("instance"), arguments = arguments() @@ -54,7 +51,7 @@ private fun JsonObject.properties(): List = inner() ?.map { it.toProperty() } ?: listOf() private fun JsonObject.toProperty(): ObjectiveCClass.Property = ObjectiveCClass.Property( - name = name(), + name = NotBlankString(name()), type = type(), assign = nullableBooleanValueOf("assign"), readwrite = nullableBooleanValueOf("readwrite"), diff --git a/klang/klang/src/main/kotlin/klang/parser/libclang/JnaLibClangParser.kt b/klang/klang/src/main/kotlin/klang/parser/libclang/JnaLibClangParser.kt index 4d502453..9fb5d766 100644 --- a/klang/klang/src/main/kotlin/klang/parser/libclang/JnaLibClangParser.kt +++ b/klang/klang/src/main/kotlin/klang/parser/libclang/JnaLibClangParser.kt @@ -60,7 +60,7 @@ private fun isEnumOrStruct(info: DeclarationInfo) = info.cursor.children().isNot && info.cursor.children().first().kind in listOf(CursorKind.ENUM_DECL, CursorKind.STRUCT_DECL) private fun ParsingContext.declareTypeAlias(info: DeclarationInfo) { - val name = info.cursor.spelling + val name = NotBlankString(info.cursor.spelling) val type = info.cursor.underlyingType.spelling currentDefinition = NativeTypeAlias( name = name, @@ -89,12 +89,12 @@ private fun ParsingContext.updateEnumerationField(info: DeclarationInfo) { } private fun ParsingContext.declareStructure(info: DeclarationInfo) { - currentDefinition = NativeStructure(lastTypeDefName.consume() ?: info.cursor.spelling) + currentDefinition = NativeStructure(NotBlankString(lastTypeDefName.consume() ?: info.cursor.spelling)) .also(declarationRepository::save) } private fun ParsingContext.declareEnumeration(info: DeclarationInfo) { - currentDefinition = NativeEnumeration(lastTypeDefName.consume() ?: info.cursor.spelling) + currentDefinition = NativeEnumeration(NotBlankString(lastTypeDefName.consume() ?: info.cursor.spelling)) .also(declarationRepository::save) } diff --git a/klang/klang/src/main/kotlin/klang/parser/libclang/LibClangParser.kt b/klang/klang/src/main/kotlin/klang/parser/libclang/LibClangParser.kt index 727d69d0..12ff7a8f 100644 --- a/klang/klang/src/main/kotlin/klang/parser/libclang/LibClangParser.kt +++ b/klang/klang/src/main/kotlin/klang/parser/libclang/LibClangParser.kt @@ -33,17 +33,17 @@ fun parseFile( } private fun computeHeadersPaths(headerPathsAsString: Array) = - headerPathsAsString.map { Path.of(it).also { assert(it.exists()) { "File not found ${it.absolutePathString()}" } } }.toTypedArray() + headerPathsAsString.map { Path.of(it).also { check(it.exists()) { "File not found ${it.absolutePathString()}" } } }.toTypedArray() private fun computePath(filePathAsString: String?) = filePathAsString?.let { Path.of(it) } - ?.also { assert(it.exists()) } + ?.also { check(it.exists()) } private fun computeFile(filePathAsString: String?, fileAsString: String) = when (filePathAsString != null) { true -> filePathAsString.let { "$it/$fileAsString" } .let(::File) false -> File(fileAsString) -}.also { assert(it.exists()) } +}.also { check(it.exists()) } private fun parseFile( file: File, @@ -53,8 +53,8 @@ private fun parseFile( ) = when (parserTechnology) { ParserTechnology.JNA -> { - assert(filePath == null) { "file path is not supported on JNA" } - assert(headerPaths.isEmpty()) { "header paths is not supported on JNA" } + check(filePath == null) { "file path is not supported on JNA" } + check(headerPaths.isEmpty()) { "header paths is not supported on JNA" } parseFileWithJna(file.absolutePath) } diff --git a/klang/klang/src/main/kotlin/klang/parser/libclang/PanamaLibclangParser.kt b/klang/klang/src/main/kotlin/klang/parser/libclang/PanamaLibclangParser.kt index 735b5cae..53fa9bda 100644 --- a/klang/klang/src/main/kotlin/klang/parser/libclang/PanamaLibclangParser.kt +++ b/klang/klang/src/main/kotlin/klang/parser/libclang/PanamaLibclangParser.kt @@ -33,7 +33,7 @@ fun parseFileWithPanama(file: String, filePath: Path?, headerPaths: Array) *clangArguments ) - assert(topLevel.kind() == Declaration.Scoped.Kind.TOPLEVEL) + check(topLevel.kind() == Declaration.Scoped.Kind.TOPLEVEL) topLevel.members() .asSequence() diff --git a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeEnumeration.kt b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeEnumeration.kt index 9f1e029a..72f17270 100644 --- a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeEnumeration.kt +++ b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeEnumeration.kt @@ -2,10 +2,11 @@ package klang.parser.libclang.panama import klang.domain.DeclarationOrigin import klang.domain.NativeEnumeration +import klang.domain.NotBlankString import org.openjdk.jextract.Declaration internal fun Declaration.Scoped.toNativeEnumeration(name: String?, origin: DeclarationOrigin) = NativeEnumeration( - name ?: name(), + NotBlankString(name ?: name()), members().toEnumValues(), source = origin ) diff --git a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeFunction.kt b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeFunction.kt index de26e119..1225a214 100644 --- a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeFunction.kt +++ b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeFunction.kt @@ -3,11 +3,12 @@ package klang.parser.libclang.panama import klang.domain.DeclarationOrigin import klang.domain.NameableDeclaration import klang.domain.NativeFunction +import klang.domain.NotBlankString import org.openjdk.jextract.Declaration import org.openjdk.jextract.Declaration.Variable internal fun Declaration.Function.toNativeTypeAlias(origin: DeclarationOrigin): NameableDeclaration = NativeFunction( - name(), + NotBlankString(name()), returnType = type().toTypeRef(), arguments = parameters().map { it.toArgument() }, source = origin diff --git a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeStructure.kt b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeStructure.kt index ef585121..81ecaec8 100644 --- a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeStructure.kt +++ b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeStructure.kt @@ -1,9 +1,6 @@ package klang.parser.libclang.panama -import klang.domain.DeclarationOrigin -import klang.domain.NativeStructure -import klang.domain.StructureField -import klang.domain.TypeRefField +import klang.domain.* import org.openjdk.jextract.Declaration import org.openjdk.jextract.impl.TypeImpl @@ -13,7 +10,7 @@ internal fun Declaration.Scoped.toNativeStructure(name: String?, isUnion: Boolea isUnion ).let { (name, fields, isUnion) -> NativeStructure( - name, + NotBlankString(name), fields, isUnion, origin diff --git a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeTypeAlias.kt b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeTypeAlias.kt index d5da3a88..849fbe7a 100644 --- a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeTypeAlias.kt +++ b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/NativeTypeAlias.kt @@ -3,7 +3,8 @@ package klang.parser.libclang.panama import klang.domain.DeclarationOrigin import klang.domain.NameableDeclaration import klang.domain.NativeTypeAlias +import klang.domain.NotBlankString import org.openjdk.jextract.Declaration internal fun Declaration.Typedef.toNativeTypeAlias(origin: DeclarationOrigin): NameableDeclaration? = (name() to type().toTypeRef()) - .let { (name, typeRef) -> NativeTypeAlias(name, typeRef, origin) } + .let { (name, typeRef) -> NativeTypeAlias(NotBlankString(name), typeRef, origin) } diff --git a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/TypeRef.kt b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/TypeRef.kt index 78168f09..57ce09e5 100644 --- a/klang/klang/src/main/kotlin/klang/parser/libclang/panama/TypeRef.kt +++ b/klang/klang/src/main/kotlin/klang/parser/libclang/panama/TypeRef.kt @@ -1,9 +1,6 @@ package klang.parser.libclang.panama -import klang.domain.TypeRef -import klang.domain.UnresolvedTypeRef -import klang.domain.typeOf -import klang.domain.unchecked +import klang.domain.* import org.openjdk.jextract.Type import org.openjdk.jextract.Type.Delegated import org.openjdk.jextract.impl.TypeImpl @@ -16,12 +13,12 @@ internal fun Type.toTypeRef(): TypeRef = when (this) { when (type) { is TypeImpl.FunctionImpl -> UnresolvedTypeRef( toString(), - type.toTypeString(), + NotBlankString(type.toTypeString()), isPointer = true, isCallback = true ) else -> type.toTypeString().removeConstPrefix().removePointerSuffix() - .let { typeName -> UnresolvedTypeRef(toString(), typeName, isPointer = true) } + .let { typeName -> UnresolvedTypeRef(toString(), NotBlankString(typeName), isPointer = true) } } } diff --git a/klang/klang/src/main/kotlin/klang/parser/libclang/type/NativeFunction.kt b/klang/klang/src/main/kotlin/klang/parser/libclang/type/NativeFunction.kt index 70d0863d..c10f8d13 100644 --- a/klang/klang/src/main/kotlin/klang/parser/libclang/type/NativeFunction.kt +++ b/klang/klang/src/main/kotlin/klang/parser/libclang/type/NativeFunction.kt @@ -1,6 +1,7 @@ package klang.parser.libclang.type import klang.domain.NativeFunction +import klang.domain.NotBlankString import klang.domain.typeOf import klang.domain.unchecked import klang.jvm.Cursor @@ -9,7 +10,7 @@ import klang.parser.libclang.ParsingContext internal fun ParsingContext.declareFunction(info: DeclarationInfo) { currentDefinition = NativeFunction( - name = info.cursor.spelling, + name = NotBlankString(info.cursor.spelling), returnType = typeOf(info.cursor.returnType()).unchecked("fail to create type"), arguments = info.cursor.arguments() ).also(declarationRepository::save) diff --git a/klang/klang/src/test/kotlin/klang/domain/EnumerationTypeResolving.kt b/klang/klang/src/test/kotlin/klang/domain/EnumerationTypeResolving.kt index c0a8769a..155cf38f 100644 --- a/klang/klang/src/test/kotlin/klang/domain/EnumerationTypeResolving.kt +++ b/klang/klang/src/test/kotlin/klang/domain/EnumerationTypeResolving.kt @@ -3,17 +3,18 @@ package klang.domain import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter class EnumerationTypeResolving : FreeSpec({ val enumeration = NativeEnumeration( - name = "MyEnumeration", + name = NotBlankString("MyEnumeration"), values = listOf("first" to 1L) ) InMemoryDeclarationRepository().also { repository -> repository.save(enumeration) - repository.resolveTypes() + repository.resolveTypes(allDeclarationsFilter) } "should resolve enumeration type as primitive" { diff --git a/klang/klang/src/test/kotlin/klang/domain/NotBlankStringTest.kt b/klang/klang/src/test/kotlin/klang/domain/NotBlankStringTest.kt new file mode 100644 index 00000000..1845be23 --- /dev/null +++ b/klang/klang/src/test/kotlin/klang/domain/NotBlankStringTest.kt @@ -0,0 +1,32 @@ +package klang.domain + +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.FreeSpec +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe + +class NotBlankStringTest : FreeSpec({ + + "should return null on blank string" { + listOf("", " ", "\t").forEach { + notBlankString(it) shouldBe null + } + } + + "should not return null on not blank string" { + notBlankString("a") shouldNotBe null + } + + "should throw an error if string is blank" { + listOf("", " ", "\t").forEach { + shouldThrow { + NotBlankString(it) + } + } + } + + "should not throw an error if string is not blank" { + NotBlankString("a") + } + +}) diff --git a/klang/klang/src/test/kotlin/klang/domain/StringTypeResolving.kt b/klang/klang/src/test/kotlin/klang/domain/StringTypeResolving.kt index 13fd03c7..9762a969 100644 --- a/klang/klang/src/test/kotlin/klang/domain/StringTypeResolving.kt +++ b/klang/klang/src/test/kotlin/klang/domain/StringTypeResolving.kt @@ -3,18 +3,19 @@ package klang.domain import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.parser.testType class StringTypeResolving : FreeSpec({ val nativeTypeAlias = NativeTypeAlias( - name = "MyString", + name = NotBlankString("MyString"), typeRef = testType("char *") ) InMemoryDeclarationRepository().also { repository -> repository.save(nativeTypeAlias) - repository.resolveTypes() + repository.resolveTypes(allDeclarationsFilter) } "should resolve string type as primitive" { diff --git a/klang/klang/src/test/kotlin/klang/domain/TypeRefTest.kt b/klang/klang/src/test/kotlin/klang/domain/TypeRefTest.kt index aaf689ec..d16187fe 100644 --- a/klang/klang/src/test/kotlin/klang/domain/TypeRefTest.kt +++ b/klang/klang/src/test/kotlin/klang/domain/TypeRefTest.kt @@ -9,20 +9,20 @@ class TypeRefTest : FreeSpec({ "test FunctionPointerType parsing" { TestData.basicFunctionPointer.toFunctionPointerType().apply { returnType.apply { - typeName shouldBe "void" + typeName shouldBe NotBlankString("void") isPointer shouldBe false } arguments.size shouldBe 3 arguments[0].apply { - typeName shouldBe "void" + typeName shouldBe NotBlankString("void") isPointer shouldBe true } arguments[1].apply { - typeName shouldBe "char" + typeName shouldBe NotBlankString("char") isPointer shouldBe true } arguments[2].apply { - typeName shouldBe "int" + typeName shouldBe NotBlankString("int") isPointer shouldBe false } } @@ -38,7 +38,7 @@ class TypeRefTest : FreeSpec({ isStructure shouldBe false isEnumeration shouldBe false isNullable shouldBe null - typeName shouldBe "void" + typeName shouldBe NotBlankString("void") } } } @@ -54,7 +54,7 @@ class TypeRefTest : FreeSpec({ isStructure shouldBe false isEnumeration shouldBe false isNullable shouldBe null - typeName shouldBe "int" + typeName shouldBe NotBlankString("int") } } } @@ -71,7 +71,7 @@ class TypeRefTest : FreeSpec({ isStructure shouldBe false isEnumeration shouldBe false isNullable shouldBe null - typeName shouldBe "int" + typeName shouldBe NotBlankString("int") } } } @@ -87,7 +87,7 @@ class TypeRefTest : FreeSpec({ isStructure shouldBe false isEnumeration shouldBe false isNullable shouldBe null - typeName shouldBe "unsigned int" + typeName shouldBe NotBlankString("unsigned int") } } } @@ -103,7 +103,7 @@ class TypeRefTest : FreeSpec({ isStructure shouldBe true isEnumeration shouldBe false isNullable shouldBe null - typeName shouldBe "AnyStruct" + typeName shouldBe NotBlankString("AnyStruct") } } } @@ -119,7 +119,7 @@ class TypeRefTest : FreeSpec({ isStructure shouldBe false isEnumeration shouldBe true isNullable shouldBe null - typeName shouldBe "AnyEnum" + typeName shouldBe NotBlankString("AnyEnum") } } } diff --git a/klang/klang/src/test/kotlin/klang/generator/CallbackGenerationTest.kt b/klang/klang/src/test/kotlin/klang/generator/CallbackGenerationTest.kt index 1c8e3b28..aea9211b 100644 --- a/klang/klang/src/test/kotlin/klang/generator/CallbackGenerationTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/CallbackGenerationTest.kt @@ -3,7 +3,9 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.NativeTypeAlias +import klang.domain.NotBlankString import klang.mapper.toSpec import klang.parser.TestData import klang.parser.testType @@ -11,13 +13,13 @@ import klang.parser.testType class CallbackGenerationTest : FreeSpec({ val callback = NativeTypeAlias( - name = "MyCallback", + name = NotBlankString("MyCallback"), typeRef = testType(TestData.basicFunctionPointer), ) InMemoryDeclarationRepository().apply { save(callback) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin callback" { diff --git a/klang/klang/src/test/kotlin/klang/generator/EnumerationGenerationTest.kt b/klang/klang/src/test/kotlin/klang/generator/EnumerationGenerationTest.kt index 7939232d..1ecf9aa4 100644 --- a/klang/klang/src/test/kotlin/klang/generator/EnumerationGenerationTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/EnumerationGenerationTest.kt @@ -3,13 +3,15 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.NativeEnumeration +import klang.domain.NotBlankString import klang.mapper.toSpecAsEnumeration class EnumerationGenerationTest : FreeSpec({ val enumeration = NativeEnumeration( - name = "MyEnum", + name = NotBlankString("MyEnum"), values = listOf( Pair("FIRST", 1), Pair("SECOND", 2), @@ -19,7 +21,7 @@ class EnumerationGenerationTest : FreeSpec({ InMemoryDeclarationRepository().apply { save(enumeration) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin enumeration" { diff --git a/klang/klang/src/test/kotlin/klang/generator/FunctionGenerationTest.kt b/klang/klang/src/test/kotlin/klang/generator/FunctionGenerationTest.kt index 0f5c5bd1..9b691e2d 100644 --- a/klang/klang/src/test/kotlin/klang/generator/FunctionGenerationTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/FunctionGenerationTest.kt @@ -3,6 +3,7 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.mapper.generateInterfaceLibrarySpec import klang.mapper.toInterfaceSpec import klang.parser.TestData @@ -13,7 +14,7 @@ class FunctionGenerationTest : FreeSpec({ InMemoryDeclarationRepository().apply { functions.forEach { save(it) } - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin functions" { diff --git a/klang/klang/src/test/kotlin/klang/generator/FunctionGenerationWithStructurePointerTest.kt b/klang/klang/src/test/kotlin/klang/generator/FunctionGenerationWithStructurePointerTest.kt index 8bb3e617..0b7c8b1f 100644 --- a/klang/klang/src/test/kotlin/klang/generator/FunctionGenerationWithStructurePointerTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/FunctionGenerationWithStructurePointerTest.kt @@ -3,8 +3,10 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.NativeFunction import klang.domain.NativeStructure +import klang.domain.NotBlankString import klang.domain.TypeRefField import klang.mapper.toInterfaceSpec import klang.parser.testType @@ -13,7 +15,7 @@ class FunctionGenerationWithStructurePointerTest : FreeSpec({ val structure = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf( TypeRefField("field1", testType("int")), TypeRefField("field2", testType("char")), @@ -21,7 +23,7 @@ class FunctionGenerationWithStructurePointerTest : FreeSpec({ ) val function = NativeFunction( - name = "function", + name = NotBlankString("function"), returnType = testType("void"), arguments = listOf( NativeFunction.Argument("structure", testType("MyStructure *")), @@ -31,7 +33,7 @@ class FunctionGenerationWithStructurePointerTest : FreeSpec({ InMemoryDeclarationRepository().apply { save(function) save(structure) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin functions" { diff --git a/klang/klang/src/test/kotlin/klang/generator/ObjectiveCGenerationTest.kt b/klang/klang/src/test/kotlin/klang/generator/ObjectiveCGenerationTest.kt index 4b038d3a..12ab8be8 100644 --- a/klang/klang/src/test/kotlin/klang/generator/ObjectiveCGenerationTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/ObjectiveCGenerationTest.kt @@ -2,6 +2,7 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe +import klang.domain.NotBlankString import klang.domain.ObjectiveCClass import klang.domain.typeOf import klang.mapper.toSpec @@ -9,7 +10,7 @@ import klang.mapper.toSpec class ObjectiveCGenerationTest : FreeSpec({ val objectiveC = ObjectiveCClass( - name = "MyObjectiveCClass", + name = NotBlankString("MyObjectiveCClass"), superType = typeOf("NSObject").getOrNull(), protocols = setOf(), properties = listOf(), diff --git a/klang/klang/src/test/kotlin/klang/generator/StructureGenerationTest.kt b/klang/klang/src/test/kotlin/klang/generator/StructureGenerationTest.kt index d7844473..a7bcfe63 100644 --- a/klang/klang/src/test/kotlin/klang/generator/StructureGenerationTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/StructureGenerationTest.kt @@ -3,7 +3,9 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.NativeStructure +import klang.domain.NotBlankString import klang.domain.TypeRefField import klang.mapper.toSpec import klang.parser.testType @@ -11,7 +13,7 @@ import klang.parser.testType class StructureGenerationTest : FreeSpec({ val structure = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf( TypeRefField("first", testType("long")), TypeRefField("second", testType("int")), @@ -24,7 +26,7 @@ class StructureGenerationTest : FreeSpec({ InMemoryDeclarationRepository().apply { save(structure) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin structure" { @@ -88,7 +90,7 @@ public open class MyStructure : com.sun.jna.Structure { val structureWithNoFields = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf() ) diff --git a/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithCallbackTest.kt b/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithCallbackTest.kt index 2de3726c..96af6f1b 100644 --- a/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithCallbackTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithCallbackTest.kt @@ -3,8 +3,10 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.NativeStructure import klang.domain.NativeTypeAlias +import klang.domain.NotBlankString import klang.domain.TypeRefField import klang.mapper.toSpec import klang.parser.TestData.basicFunctionPointer @@ -13,7 +15,7 @@ import klang.parser.testType class StructureGenerationWithCallbackTest : FreeSpec({ val structure = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf( TypeRefField("callback", testType(basicFunctionPointer)), TypeRefField("callback2", testType("MyAlias")) @@ -21,14 +23,14 @@ class StructureGenerationWithCallbackTest : FreeSpec({ ) val typeAlias = NativeTypeAlias( - name = "MyAlias", + name = NotBlankString("MyAlias"), typeRef = testType(basicFunctionPointer) ) InMemoryDeclarationRepository().apply { save(structure) save(typeAlias) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin structure with callback" { @@ -80,7 +82,7 @@ public open class MyStructure : com.sun.jna.Structure { val structureWithNoFields = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf() ) diff --git a/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithEnumerationTest.kt b/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithEnumerationTest.kt index 62fb6780..e52833b7 100644 --- a/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithEnumerationTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithEnumerationTest.kt @@ -3,8 +3,10 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.NativeEnumeration import klang.domain.NativeStructure +import klang.domain.NotBlankString import klang.domain.TypeRefField import klang.mapper.toSpec import klang.parser.testType @@ -12,21 +14,21 @@ import klang.parser.testType class StructureGenerationWithEnumerationTest : FreeSpec({ val structure = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf( TypeRefField("enumeration", testType("MyEnumeration")), ) ) val enumeration = NativeEnumeration( - name = "MyEnumeration", + name = NotBlankString("MyEnumeration"), values = listOf("first" to 1L) ) InMemoryDeclarationRepository().apply { save(structure) save(enumeration) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin structure with enumeration" { @@ -60,7 +62,7 @@ public open class MyStructure : com.sun.jna.Structure { val structureWithNoFields = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf() ) diff --git a/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithPrimitiveArrayTest.kt b/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithPrimitiveArrayTest.kt index 669ba8cf..ed1f506f 100644 --- a/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithPrimitiveArrayTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/StructureGenerationWithPrimitiveArrayTest.kt @@ -3,7 +3,9 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.NativeStructure +import klang.domain.NotBlankString import klang.domain.TypeRefField import klang.mapper.toSpec import klang.parser.testType @@ -11,7 +13,7 @@ import klang.parser.testType class StructureGenerationWithPrimitiveArrayTest : FreeSpec({ val structure = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf( TypeRefField("first", testType("int[10]").also { it.isArray = true @@ -22,7 +24,7 @@ class StructureGenerationWithPrimitiveArrayTest : FreeSpec({ InMemoryDeclarationRepository().apply { save(structure) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin structure with primitive array" { @@ -56,7 +58,7 @@ public open class MyStructure : com.sun.jna.Structure { val structureWithNoFields = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf() ) diff --git a/klang/klang/src/test/kotlin/klang/generator/StructureWithStructureGenerationTest.kt b/klang/klang/src/test/kotlin/klang/generator/StructureWithStructureGenerationTest.kt index e78edde2..af5f5d93 100644 --- a/klang/klang/src/test/kotlin/klang/generator/StructureWithStructureGenerationTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/StructureWithStructureGenerationTest.kt @@ -3,7 +3,9 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.NativeStructure +import klang.domain.NotBlankString import klang.domain.TypeRefField import klang.mapper.toSpec import klang.parser.testType @@ -11,14 +13,14 @@ import klang.parser.testType class StructureWithStructureGenerationTest : FreeSpec({ val structure = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf( TypeRefField("structure", testType("struct MyOtherStructure")), ) ) val otherStructure = NativeStructure( - name = "MyOtherStructure", + name = NotBlankString("MyOtherStructure"), fields = listOf( TypeRefField("structure", testType("long")), ) @@ -27,7 +29,7 @@ class StructureWithStructureGenerationTest : FreeSpec({ InMemoryDeclarationRepository().apply { save(otherStructure) save(structure) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin structure" { @@ -61,7 +63,7 @@ public open class MyStructure : com.sun.jna.Structure { val structureWithNoFields = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf() ) diff --git a/klang/klang/src/test/kotlin/klang/generator/UnionGenerationTest.kt b/klang/klang/src/test/kotlin/klang/generator/UnionGenerationTest.kt index 7c9d615b..1981dcd8 100644 --- a/klang/klang/src/test/kotlin/klang/generator/UnionGenerationTest.kt +++ b/klang/klang/src/test/kotlin/klang/generator/UnionGenerationTest.kt @@ -3,7 +3,9 @@ package klang.generator import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.NativeStructure +import klang.domain.NotBlankString import klang.domain.TypeRefField import klang.mapper.toSpec import klang.parser.testType @@ -11,7 +13,7 @@ import klang.parser.testType class UnionGenerationTest : FreeSpec({ val structure = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf( TypeRefField("first", testType("long")), TypeRefField("second", testType("int")), @@ -25,7 +27,7 @@ class UnionGenerationTest : FreeSpec({ InMemoryDeclarationRepository().apply { save(structure) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "generate kotlin union" { @@ -93,7 +95,7 @@ public open class MyStructure : com.sun.jna.Union { val structureWithNoFields = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf() ) diff --git a/klang/klang/src/test/kotlin/klang/mapper/EnumerationMapperTest.kt b/klang/klang/src/test/kotlin/klang/mapper/EnumerationMapperTest.kt index 4579d346..782a6b5a 100644 --- a/klang/klang/src/test/kotlin/klang/mapper/EnumerationMapperTest.kt +++ b/klang/klang/src/test/kotlin/klang/mapper/EnumerationMapperTest.kt @@ -3,11 +3,12 @@ package klang.mapper import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import klang.domain.NativeEnumeration +import klang.domain.NotBlankString class EnumerationMapperTest : FreeSpec({ val enumeration = NativeEnumeration( - name = "MyEnum", + name = NotBlankString("MyEnum"), values = listOf( "FIRST" to 1, "SECOND" to 2, @@ -17,7 +18,7 @@ class EnumerationMapperTest : FreeSpec({ "generate kotlin enumeration specifications" { enumeration.toSpecAsEnumeration("mypackage").apply { - name shouldBe enumeration.name + name shouldBe enumeration.name.value enumConstants.size shouldBe enumeration.values.size enumConstants.map { it.key } shouldBe enumeration.values.map { it.first } } diff --git a/klang/klang/src/test/kotlin/klang/mapper/TypeTest.kt b/klang/klang/src/test/kotlin/klang/mapper/TypeTest.kt index 8c7619b0..ebad13b6 100644 --- a/klang/klang/src/test/kotlin/klang/mapper/TypeTest.kt +++ b/klang/klang/src/test/kotlin/klang/mapper/TypeTest.kt @@ -4,6 +4,7 @@ import io.kotest.core.spec.style.FreeSpec import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import klang.InMemoryDeclarationRepository +import klang.allDeclarationsFilter import klang.domain.* import klang.parser.TestData import klang.parser.testType @@ -11,7 +12,7 @@ import klang.parser.testType class TypeTest : FreeSpec({ val structure = NativeStructure( - name = "MyStructure", + name = NotBlankString("MyStructure"), fields = listOf( TypeRefField("callback", testType(TestData.basicFunctionPointer)), TypeRefField("callback2", testType("MyAlias")), @@ -19,12 +20,12 @@ class TypeTest : FreeSpec({ ) val typeAlias = NativeTypeAlias( - name = "MyAlias", + name = NotBlankString("MyAlias"), typeRef = testType(TestData.basicFunctionPointer) ) val primitiveArrayTypeAlias = NativeTypeAlias( - name = "MyAliasWithPrimitiveArray", + name = NotBlankString("MyAliasWithPrimitiveArray"), typeRef = testType("int[10]") ) @@ -32,7 +33,7 @@ class TypeTest : FreeSpec({ save(structure) save(typeAlias) save(primitiveArrayTypeAlias) - resolveTypes() + resolveTypes(allDeclarationsFilter) } "toType" { diff --git a/klang/klang/src/test/kotlin/klang/parser/ParserTestCommon.kt b/klang/klang/src/test/kotlin/klang/parser/ParserTestCommon.kt index 78a8e4bc..52d6de3d 100644 --- a/klang/klang/src/test/kotlin/klang/parser/ParserTestCommon.kt +++ b/klang/klang/src/test/kotlin/klang/parser/ParserTestCommon.kt @@ -6,6 +6,7 @@ import io.kotest.core.spec.style.scopes.FreeSpecContainerScope import io.kotest.matchers.shouldBe import klang.DeclarationRepository import klang.domain.NativeStructure +import klang.domain.NotBlankString import klang.parser.json.ParserRepository @Ignored @@ -22,7 +23,7 @@ open class ParserTestCommon(body: FreeSpec.() -> Unit = {}) : FreeSpec({ body() }) - suspend fun FreeSpecContainerScope.validateEnumerations(repository: DeclarationRepository, enumerations: List>>>) { + suspend fun FreeSpecContainerScope.validateEnumerations(repository: DeclarationRepository, enumerations: List>>>) { enumerations.forEach { (name, values) -> "test $name" { repository.findEnumerationByName(name) diff --git a/klang/klang/src/test/kotlin/klang/parser/TestData.kt b/klang/klang/src/test/kotlin/klang/parser/TestData.kt index faef38eb..e3bd626c 100644 --- a/klang/klang/src/test/kotlin/klang/parser/TestData.kt +++ b/klang/klang/src/test/kotlin/klang/parser/TestData.kt @@ -2,7 +2,9 @@ package klang.parser import klang.domain.* -fun testType(name: String) = typeOf(name).let { +fun testType(name: String) = testType(NotBlankString(name)) + +fun testType(name: NotBlankString) = typeOf(name.value).let { it.unchecked("fail to create type $name, cause: ${it.leftOrNull()}") } @@ -10,7 +12,7 @@ object TestData { val functions = listOf( NativeFunction( - name = "function", + name = NotBlankString("function"), returnType = testType("char"), arguments = listOf( NativeFunction.Argument("a", testType("int *")), @@ -19,25 +21,25 @@ object TestData { ) ), NativeFunction( - name = "function2", + name = NotBlankString("function2"), returnType = testType("void *"), arguments = listOf() ), NativeFunction( - name = "function3", + name = NotBlankString("function3"), returnType = testType("struct StructName *"), arguments = listOf() ) ) val objectiveCEnumeration = listOf( - "MyEnum" to listOf( + NotBlankString("MyEnum") to listOf( "kValue1" to 0L, "kValue2" to 1L, "kValue3" to 2L ), - "MyEnum2" to listOf( + NotBlankString("MyEnum2") to listOf( "kValue4" to 0L, "kValue5" to 1L, "kValue6" to 2L @@ -46,34 +48,34 @@ object TestData { val objectiveCCategory = listOf( ObjectiveCCategory( - name = "MyCategory", + name = NotBlankString("MyCategory"), superType = testType("MyClass"), methods = listOf( - ObjectiveCClass.Method("newMethod", testType("void"), true), + ObjectiveCClass.Method(NotBlankString("newMethod"), testType("void"), true), ) ) ) val objectiveCProtocol = listOf( ObjectiveCProtocol( - name = "MyProtocol", + name = NotBlankString("MyProtocol"), protocols = setOf("NSObject"), properties = listOf(), methods = listOf( - ObjectiveCClass.Method("method1", testType("void"), true), - ObjectiveCClass.Method("method2", testType("NSString *"), true) + ObjectiveCClass.Method(NotBlankString("method1"), testType("void"), true), + ObjectiveCClass.Method(NotBlankString("method2"), testType("NSString *"), true) ) ) ) val objectiveCClass = listOf( ObjectiveCClass( - name = "TestClass", + name = NotBlankString("TestClass"), superType = testType("NSObject"), protocols = setOf(testType("NSCopying")), properties = listOf( ObjectiveCClass.Property( - "testProperty", + NotBlankString("testProperty"), "NSString *", nonatomic = true, assign = true, @@ -82,9 +84,9 @@ object TestData { ) ), methods = listOf( - ObjectiveCClass.Method("testMethod", testType("void"), true), + ObjectiveCClass.Method(NotBlankString("testMethod"), testType("void"), true), ObjectiveCClass.Method( - "testMethod:withParameter:", testType("BOOL"), false, listOf( + NotBlankString("testMethod:withParameter:"), testType("BOOL"), false, listOf( ObjectiveCClass.Method.Argument("parameter", testType("NSString *")), ObjectiveCClass.Method.Argument("testParameter", testType("NSString *")), ) @@ -94,8 +96,8 @@ object TestData { ) val enumerations = listOf( - "EnumName" to listOf("Value1" to 0x2L, "Value2" to 0x1L), - "EnumNameWithoutExplicitValues" to listOf( + NotBlankString("EnumName") to listOf("Value1" to 0x2L, "Value2" to 0x1L), + NotBlankString("EnumNameWithoutExplicitValues") to listOf( "EnumNameWithoutExplicitValues_Value1" to 0L, "EnumNameWithoutExplicitValues_Value2" to 1L ) @@ -103,7 +105,7 @@ object TestData { val union = listOf( NativeStructure( - name = "MyUnion", + name = NotBlankString("MyUnion"), fields = listOf( TypeRefField("i", testType("int")), TypeRefField("f", testType("float")), @@ -117,7 +119,7 @@ object TestData { val structures = listOf( NativeStructure( - name = "StructName", + name = NotBlankString("StructName"), fields = listOf( TypeRefField("field1", testType("enum EnumName *")), TypeRefField("field2", testType("EnumName2")), @@ -125,7 +127,7 @@ object TestData { ) ), NativeStructure( - name = "StructName2", + name = NotBlankString("StructName2"), fields = listOf( TypeRefField("field1", testType("struct StructName")), TypeRefField("field2", testType("struct StructName *")), @@ -137,7 +139,7 @@ object TestData { val typeDefStructures = listOf( NativeStructure( - name = "StructName", + name = NotBlankString("StructName"), fields = listOf( TypeRefField("field1", testType("enum EnumName *")), TypeRefField("field2", testType("EnumName2")), @@ -145,7 +147,7 @@ object TestData { ) ), NativeStructure( - name = "StructName2", + name = NotBlankString("StructName2"), fields = listOf( TypeRefField("field1", testType("StructName")), TypeRefField("field2", testType("StructName *")), @@ -156,77 +158,76 @@ object TestData { val typeDef = listOf( NativeTypeAlias( - name = "NewType", + name = NotBlankString("NewType"), typeRef = testType("void *") ), NativeTypeAlias( - name = "NewStructureType", + name = NotBlankString("NewStructureType"), typeRef = testType("struct OldStructureType *") ) ) - const val basicFunctionPointer = "void (*)(void *, char *, int)" - + val basicFunctionPointer = NotBlankString("void (*)(void *, char *, int)") val exaustiveTypeDef = listOf( NativeTypeAlias( - name = "signed_char_t", + name = NotBlankString("signed_char_t"), typeRef = testType("char") ), NativeTypeAlias( - name = "signed_int_t", + name = NotBlankString("signed_int_t"), typeRef = testType("int") ), NativeTypeAlias( - name = "signed_short_t", + name = NotBlankString("signed_short_t"), typeRef = testType("short") ), NativeTypeAlias( - name = "signed_long_t", + name = NotBlankString("signed_long_t"), typeRef = testType("long") ), NativeTypeAlias( - name = "signed_long_long_t", + name = NotBlankString("signed_long_long_t"), typeRef = testType("long long") ), NativeTypeAlias( - name = "unsigned_char_t", + name = NotBlankString("unsigned_char_t"), typeRef = testType("unsigned char") ), NativeTypeAlias( - name = "unsigned_int_t", + name = NotBlankString("unsigned_int_t"), typeRef = testType("unsigned int") ), NativeTypeAlias( - name = "unsigned_short_t", + name = NotBlankString("unsigned_short_t"), typeRef = testType("unsigned short") ), NativeTypeAlias( - name = "unsigned_long_t", + name = NotBlankString("unsigned_long_t"), typeRef = testType("unsigned long") ), NativeTypeAlias( - name = "unsigned_long_long_t", + name = NotBlankString("unsigned_long_long_t"), typeRef = testType("unsigned long long") ), NativeTypeAlias( - name = "arr_of_int_t", + name = NotBlankString("arr_of_int_t"), typeRef = testType("int[10]") ), NativeTypeAlias( - name = "arr_of_float_t", + name = NotBlankString("arr_of_float_t"), typeRef = testType("float[10]") ), NativeTypeAlias( - name = "arr_of_char_t", + name = NotBlankString("arr_of_char_t"), typeRef = testType("char[10]") ), NativeTypeAlias( - name = "arr_of_double_t", + name = NotBlankString("arr_of_double_t"), typeRef = testType("double[10]") ), NativeTypeAlias( - name = "arr_of_unsigned_char_t", + name = NotBlankString("arr_of_unsigned_char_t"), typeRef = testType("char[10]") ) ) diff --git a/klang/klang/src/test/kotlin/klang/parser/json/darwin/CocoaItTest.kt b/klang/klang/src/test/kotlin/klang/parser/json/darwin/CocoaItTest.kt index 2b163789..53e369b6 100644 --- a/klang/klang/src/test/kotlin/klang/parser/json/darwin/CocoaItTest.kt +++ b/klang/klang/src/test/kotlin/klang/parser/json/darwin/CocoaItTest.kt @@ -26,7 +26,7 @@ class CocoaItTest : FreeSpec({ declarations .asSequence() .filterIsInstance() - .filter { it.name == "NSWindow" } + .filter { it.name.value == "NSWindow" } .forEach { println(it) } } diff --git a/klang/klang/src/test/kotlin/klang/parser/json/darwin/FoundationItTest.kt b/klang/klang/src/test/kotlin/klang/parser/json/darwin/FoundationItTest.kt index 9a7b0e0d..206cafca 100644 --- a/klang/klang/src/test/kotlin/klang/parser/json/darwin/FoundationItTest.kt +++ b/klang/klang/src/test/kotlin/klang/parser/json/darwin/FoundationItTest.kt @@ -25,7 +25,7 @@ class FoundationItTest : FreeSpec({ declarations .asSequence() .filterIsInstance() - .filter { it.name.startsWith("NS") } + .filter { it.name.value.startsWith("NS") } .forEach { println(it) } } diff --git a/klang/klang/src/test/kotlin/klang/parser/json/darwin/MetalItTest.kt b/klang/klang/src/test/kotlin/klang/parser/json/darwin/MetalItTest.kt index 5d6d48bd..89a31e7e 100644 --- a/klang/klang/src/test/kotlin/klang/parser/json/darwin/MetalItTest.kt +++ b/klang/klang/src/test/kotlin/klang/parser/json/darwin/MetalItTest.kt @@ -25,7 +25,7 @@ class MetalItTest : FreeSpec({ declarations .asSequence() .filterIsInstance() - .filter { it.name.startsWith("NS") } + .filter { it.name.value.startsWith("NS") } .forEach { println(it) } } diff --git a/klang/klang/src/test/kotlin/klang/parser/libclang/SDL2ItTest.kt b/klang/klang/src/test/kotlin/klang/parser/libclang/SDL2ItTest.kt index 2a661c76..cec8a659 100644 --- a/klang/klang/src/test/kotlin/klang/parser/libclang/SDL2ItTest.kt +++ b/klang/klang/src/test/kotlin/klang/parser/libclang/SDL2ItTest.kt @@ -39,7 +39,7 @@ class SDL2ItTest : ParserTestCommon({ libraryDeclarations.filterIsInstance() .forEach { logger.info("testing ${it.name} enumeration") - it.name.isNotBlank() shouldNotBe true + it.name.value.isNotBlank() shouldNotBe true it.values.isEmpty() shouldNotBe true } } diff --git a/klang/toolkit-old/src/main/kotlin/klang/parser/TypeMaker.kt b/klang/toolkit-old/src/main/kotlin/klang/parser/TypeMaker.kt index e469b266..14730b26 100644 --- a/klang/toolkit-old/src/main/kotlin/klang/parser/TypeMaker.kt +++ b/klang/toolkit-old/src/main/kotlin/klang/parser/TypeMaker.kt @@ -54,7 +54,7 @@ class TypeMaker(private val treeMaker: TreeMaker, val parsingContext: ParsingCon } fun declareClass(scopeClass: Scoped, type: Type) { - assert(type.kind == TypeKind.Record) { "illegal state with kind ${type.kind} instead of ${TypeKind.Record}" } + check(type.kind == TypeKind.Record) { "illegal state with kind ${type.kind} instead of ${TypeKind.Record}" } parsingContext.addTyped(type, Typed.Declared(scopeClass.cursor.fullName, scopeClass)) } diff --git a/klang/toolkit-old/src/test/kotlin/klang/TestUtils.kt b/klang/toolkit-old/src/test/kotlin/klang/TestUtils.kt index 2f4b3cd7..1d5a23d4 100644 --- a/klang/toolkit-old/src/test/kotlin/klang/TestUtils.kt +++ b/klang/toolkit-old/src/test/kotlin/klang/TestUtils.kt @@ -18,7 +18,7 @@ fun createOrCompare(actual: String, expectedFileName: String) { val out = PrintWriter(File(expectedFileName)) out.print(actual) out.close() - assert(actual == "") { + check(actual == "") { "Expected file wasn't found, it will be created" } } catch (ee: IOException) {