Use different C types for boolean types in C and Objective-C mode. (#2676)

This commit is contained in:
Nikolay Igotti
2019-02-18 14:20:43 +03:00
committed by GitHub
parent 3aa0da2b06
commit 2b78ec2ebb
7 changed files with 44 additions and 38 deletions
@@ -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<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 {
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))
@@ -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
@@ -57,36 +57,6 @@ internal fun CValue<CXType>.getSize(): Long {
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(
excludeDeclarationsFromPCH: Boolean = false,
displayDiagnostics: Boolean = false,
@@ -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)
@@ -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()
@@ -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"
@@ -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