From 2b78ec2ebb9e249b0fff16a1fcc75f44464fa5d2 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Mon, 18 Feb 2019 14:20:43 +0300 Subject: [PATCH] Use different C types for boolean types in C and Objective-C mode. (#2676) --- .../kotlin/native/interop/indexer/Indexer.kt | 33 ++++++++++++++++++- .../native/interop/indexer/NativeIndex.kt | 6 +++- .../kotlin/native/interop/indexer/Utils.kt | 30 ----------------- .../kotlin/native/interop/gen/Mappings.kt | 2 +- .../kotlin/native/interop/gen/ObjCStubs.kt | 2 +- .../native/interop/gen/StructRendering.kt | 2 +- .../kotlin/native/interop/gen/TypeUtils.kt | 7 ++-- 7 files changed, 44 insertions(+), 38 deletions(-) diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt index cc5197a99a7..ac4f52cab89 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt @@ -430,7 +430,7 @@ internal class NativeIndexImpl(val library: NativeLibrary, val verbose: Boolean if (library.language == Language.OBJECTIVE_C) { if (name == "BOOL" || name == "Boolean") { assert(clang_Type_getSizeOf(type) == 1L) - return BoolType + return ObjCBoolType } if (underlying is ObjCPointer && (name == "Class" || name == "id") || @@ -469,6 +469,36 @@ internal class NativeIndexImpl(val library: NativeLibrary, val verbose: Boolean Language.OBJECTIVE_C -> supplier() } + private fun convertUnqualifiedPrimitiveType(type: CValue): Type = when (type.kind) { + CXTypeKind.CXType_Char_U, CXTypeKind.CXType_Char_S -> { + assert(type.getSize() == 1L) + CharType + } + + CXTypeKind.CXType_UChar, CXTypeKind.CXType_UShort, + CXTypeKind.CXType_UInt, CXTypeKind.CXType_ULong, CXTypeKind.CXType_ULongLong -> IntegerType( + size = type.getSize().toInt(), + isSigned = false, + spelling = clang_getTypeSpelling(type).convertAndDispose() + ) + + CXTypeKind.CXType_SChar, CXTypeKind.CXType_Short, + CXTypeKind.CXType_Int, CXTypeKind.CXType_Long, CXTypeKind.CXType_LongLong -> IntegerType( + size = type.getSize().toInt(), + isSigned = true, + spelling = clang_getTypeSpelling(type).convertAndDispose() + ) + + CXTypeKind.CXType_Float, CXTypeKind.CXType_Double -> FloatingType( + size = type.getSize().toInt(), + spelling = clang_getTypeSpelling(type).convertAndDispose() + ) + + CXTypeKind.CXType_Bool -> CBoolType + + else -> UnsupportedType + } + fun convertType(type: CValue, typeAttributes: CValue? = null): Type { val primitiveType = convertUnqualifiedPrimitiveType(type) if (primitiveType != UnsupportedType) { @@ -476,6 +506,7 @@ internal class NativeIndexImpl(val library: NativeLibrary, val verbose: Boolean } val kind = type.kind + return when (kind) { CXType_Elaborated -> convertType(clang_Type_getNamedType(type)) diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt index b36dbc8ba35..b406bc9abe5 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt @@ -242,7 +242,11 @@ interface PrimitiveType : Type object CharType : PrimitiveType -object BoolType : PrimitiveType +open class BoolType: PrimitiveType + +object CBoolType : BoolType() + +object ObjCBoolType : BoolType() data class IntegerType(val size: Int, val isSigned: Boolean, val spelling: String) : PrimitiveType diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt index ebfc690cf2c..d4ef5aeae50 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt @@ -57,36 +57,6 @@ internal fun CValue.getSize(): Long { return size } -internal fun convertUnqualifiedPrimitiveType(type: CValue): Type = when (type.kind) { - CXTypeKind.CXType_Char_U, CXTypeKind.CXType_Char_S -> { - assert(type.getSize() == 1L) - CharType - } - - CXTypeKind.CXType_UChar, CXTypeKind.CXType_UShort, - CXTypeKind.CXType_UInt, CXTypeKind.CXType_ULong, CXTypeKind.CXType_ULongLong -> IntegerType( - size = type.getSize().toInt(), - isSigned = false, - spelling = clang_getTypeSpelling(type).convertAndDispose() - ) - - CXTypeKind.CXType_SChar, CXTypeKind.CXType_Short, - CXTypeKind.CXType_Int, CXTypeKind.CXType_Long, CXTypeKind.CXType_LongLong -> IntegerType( - size = type.getSize().toInt(), - isSigned = true, - spelling = clang_getTypeSpelling(type).convertAndDispose() - ) - - CXTypeKind.CXType_Float, CXTypeKind.CXType_Double -> FloatingType( - size = type.getSize().toInt(), - spelling = clang_getTypeSpelling(type).convertAndDispose() - ) - - CXTypeKind.CXType_Bool -> BoolType - - else -> UnsupportedType -} - internal inline fun withIndex( excludeDeclarationsFromPCH: Boolean = false, displayDiagnostics: Boolean = false, diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/Mappings.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/Mappings.kt index 80b550bbeed..bf57670d9f5 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/Mappings.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/Mappings.kt @@ -378,7 +378,7 @@ fun mirrorPrimitiveType(type: PrimitiveType, declarationMapper: DeclarationMappe val varClass = Classifier.topLevel("kotlinx.cinterop", varClassName) val varClassOf = Classifier.topLevel("kotlinx.cinterop", "${varClassName}Of") - val info = if (type == BoolType) { + val info = if (type is BoolType) { TypeInfo.Boolean() } else { TypeInfo.Primitive(type.getBridgedType(declarationMapper), varClassOf) diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt index d2fd65697ae..04ebdefc8d9 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt @@ -351,7 +351,7 @@ private fun Type.isIntegerLikeType(): Boolean = when (this) { } } } - is ObjCPointer, is PointerType, CharType, BoolType -> true + is ObjCPointer, is PointerType, CharType, is BoolType -> true is IntegerType -> this.size <= 4 is Typedef -> this.def.aliased.isIntegerLikeType() is EnumType -> this.def.baseType.isIntegerLikeType() diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StructRendering.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StructRendering.kt index 7605c47b0ed..19c46daae06 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StructRendering.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StructRendering.kt @@ -63,7 +63,7 @@ private fun tryRenderUnion(def: StructDef): String? = } private fun tryRenderVar(type: Type, name: String): String? = when (type) { - CharType, BoolType -> "char $name" + CharType, is BoolType -> "char $name" is IntegerType -> "${type.spelling} $name" is FloatingType -> "${type.spelling} $name" is RecordType -> "${tryRenderStructOrUnion(type.decl.def!!)} $name" diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/TypeUtils.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/TypeUtils.kt index 3f8bcf3a321..2c8cf478d94 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/TypeUtils.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/TypeUtils.kt @@ -31,9 +31,10 @@ val StructDecl.isAnonymous: Boolean * TODO: use libclang to implement? */ fun Type.getStringRepresentation(): String = when (this) { - is VoidType -> "void" - is CharType -> "char" - is BoolType -> "BOOL" + VoidType -> "void" + CharType -> "char" + CBoolType -> "_Bool" + ObjCBoolType -> "BOOL" is IntegerType -> this.spelling is FloatingType -> this.spelling