Use different C types for boolean types in C and Objective-C mode. (#2676)
This commit is contained in:
+32
-1
@@ -430,7 +430,7 @@ internal class NativeIndexImpl(val library: NativeLibrary, val verbose: Boolean
|
|||||||
if (library.language == Language.OBJECTIVE_C) {
|
if (library.language == Language.OBJECTIVE_C) {
|
||||||
if (name == "BOOL" || name == "Boolean") {
|
if (name == "BOOL" || name == "Boolean") {
|
||||||
assert(clang_Type_getSizeOf(type) == 1L)
|
assert(clang_Type_getSizeOf(type) == 1L)
|
||||||
return BoolType
|
return ObjCBoolType
|
||||||
}
|
}
|
||||||
|
|
||||||
if (underlying is ObjCPointer && (name == "Class" || name == "id") ||
|
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()
|
Language.OBJECTIVE_C -> supplier()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun convertUnqualifiedPrimitiveType(type: CValue<CXType>): 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<CXType>, typeAttributes: CValue<CXTypeAttributes>? = null): Type {
|
fun convertType(type: CValue<CXType>, typeAttributes: CValue<CXTypeAttributes>? = null): Type {
|
||||||
val primitiveType = convertUnqualifiedPrimitiveType(type)
|
val primitiveType = convertUnqualifiedPrimitiveType(type)
|
||||||
if (primitiveType != UnsupportedType) {
|
if (primitiveType != UnsupportedType) {
|
||||||
@@ -476,6 +506,7 @@ internal class NativeIndexImpl(val library: NativeLibrary, val verbose: Boolean
|
|||||||
}
|
}
|
||||||
|
|
||||||
val kind = type.kind
|
val kind = type.kind
|
||||||
|
|
||||||
return when (kind) {
|
return when (kind) {
|
||||||
CXType_Elaborated -> convertType(clang_Type_getNamedType(type))
|
CXType_Elaborated -> convertType(clang_Type_getNamedType(type))
|
||||||
|
|
||||||
|
|||||||
+5
-1
@@ -242,7 +242,11 @@ interface PrimitiveType : Type
|
|||||||
|
|
||||||
object CharType : PrimitiveType
|
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
|
data class IntegerType(val size: Int, val isSigned: Boolean, val spelling: String) : PrimitiveType
|
||||||
|
|
||||||
|
|||||||
@@ -57,36 +57,6 @@ internal fun CValue<CXType>.getSize(): Long {
|
|||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun convertUnqualifiedPrimitiveType(type: CValue<CXType>): 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 <R> withIndex(
|
internal inline fun <R> withIndex(
|
||||||
excludeDeclarationsFromPCH: Boolean = false,
|
excludeDeclarationsFromPCH: Boolean = false,
|
||||||
displayDiagnostics: Boolean = false,
|
displayDiagnostics: Boolean = false,
|
||||||
|
|||||||
+1
-1
@@ -378,7 +378,7 @@ fun mirrorPrimitiveType(type: PrimitiveType, declarationMapper: DeclarationMappe
|
|||||||
val varClass = Classifier.topLevel("kotlinx.cinterop", varClassName)
|
val varClass = Classifier.topLevel("kotlinx.cinterop", varClassName)
|
||||||
val varClassOf = Classifier.topLevel("kotlinx.cinterop", "${varClassName}Of")
|
val varClassOf = Classifier.topLevel("kotlinx.cinterop", "${varClassName}Of")
|
||||||
|
|
||||||
val info = if (type == BoolType) {
|
val info = if (type is BoolType) {
|
||||||
TypeInfo.Boolean()
|
TypeInfo.Boolean()
|
||||||
} else {
|
} else {
|
||||||
TypeInfo.Primitive(type.getBridgedType(declarationMapper), varClassOf)
|
TypeInfo.Primitive(type.getBridgedType(declarationMapper), varClassOf)
|
||||||
|
|||||||
+1
-1
@@ -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 IntegerType -> this.size <= 4
|
||||||
is Typedef -> this.def.aliased.isIntegerLikeType()
|
is Typedef -> this.def.aliased.isIntegerLikeType()
|
||||||
is EnumType -> this.def.baseType.isIntegerLikeType()
|
is EnumType -> this.def.baseType.isIntegerLikeType()
|
||||||
|
|||||||
+1
-1
@@ -63,7 +63,7 @@ private fun tryRenderUnion(def: StructDef): String? =
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun tryRenderVar(type: Type, name: String): String? = when (type) {
|
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 IntegerType -> "${type.spelling} $name"
|
||||||
is FloatingType -> "${type.spelling} $name"
|
is FloatingType -> "${type.spelling} $name"
|
||||||
is RecordType -> "${tryRenderStructOrUnion(type.decl.def!!)} $name"
|
is RecordType -> "${tryRenderStructOrUnion(type.decl.def!!)} $name"
|
||||||
|
|||||||
+4
-3
@@ -31,9 +31,10 @@ val StructDecl.isAnonymous: Boolean
|
|||||||
* TODO: use libclang to implement?
|
* TODO: use libclang to implement?
|
||||||
*/
|
*/
|
||||||
fun Type.getStringRepresentation(): String = when (this) {
|
fun Type.getStringRepresentation(): String = when (this) {
|
||||||
is VoidType -> "void"
|
VoidType -> "void"
|
||||||
is CharType -> "char"
|
CharType -> "char"
|
||||||
is BoolType -> "BOOL"
|
CBoolType -> "_Bool"
|
||||||
|
ObjCBoolType -> "BOOL"
|
||||||
is IntegerType -> this.spelling
|
is IntegerType -> this.spelling
|
||||||
is FloatingType -> this.spelling
|
is FloatingType -> this.spelling
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user