Support C enums with forward declarations

This commit is contained in:
Svyatoslav Scherbina
2018-12-11 09:49:56 +03:00
committed by SvyatoslavScherbina
parent 56b4e9c806
commit fe2b680be8
6 changed files with 28 additions and 8 deletions
@@ -240,7 +240,13 @@ internal class NativeIndexImpl(val library: NativeLibrary, val verbose: Boolean
private fun getEnumDefAt(cursor: CValue<CXCursor>): EnumDefImpl {
if (clang_isCursorDefinition(cursor) == 0) {
TODO("support enum forward declarations")
val definitionCursor = clang_getCursorDefinition(cursor)
if (clang_isCursorDefinition(definitionCursor) != 0) {
return getEnumDefAt(definitionCursor)
} else {
TODO("support enum forward declarations: " +
clang_getTypeSpelling(clang_getCursorType(cursor)).convertAndDispose())
}
}
return enumRegistry.getOrPut(cursor) {
+5 -5
View File
@@ -2838,8 +2838,8 @@ kotlinNativeInterop {
defFile 'interop/basics/cunsupported.def'
}
cstructs {
defFile 'interop/basics/cstructs.def'
ctypes {
defFile 'interop/basics/ctypes.def'
}
if (isMac()) {
@@ -2935,10 +2935,10 @@ task interop_unsupported(type: RunInteropKonanTest) {
interop = 'cunsupported'
}
task interop_structs(type: RunInteropKonanTest) {
task interop_types(type: RunInteropKonanTest) {
disabled = (project.testTarget == 'wasm32') // No interop for wasm yet.
source = "interop/basics/structs.kt"
interop = 'cstructs'
source = "interop/basics/types.kt"
interop = 'ctypes'
}
task interop_echo_server(type: RunInteropKonanTest) {
@@ -8,4 +8,9 @@ struct StructWithConstFields {
struct StructWithConstFields getStructWithConstFields() {
struct StructWithConstFields result = { 111, 222 };
return result;
}
}
enum ForwardDeclaredEnum;
enum ForwardDeclaredEnum {
ZERO, ONE, TWO
};
@@ -1,10 +1,12 @@
import kotlinx.cinterop.*
import kotlin.test.*
import cstructs.*
import ctypes.*
fun main() {
getStructWithConstFields().useContents {
assertEquals(111, x)
assertEquals(222, y)
}
assertEquals(1u, ForwardDeclaredEnum.ONE.value)
}
@@ -61,3 +61,8 @@ int formatStringLength(NSString* format, ...);
#define STRING_MACRO @"String macro"
#define CFSTRING_MACRO CFSTR("CFString macro")
typedef NS_ENUM(int32_t, ForwardDeclaredEnum);
typedef NS_ENUM(int32_t, ForwardDeclaredEnum) {
ZERO, ONE, TWO,
};
@@ -19,6 +19,8 @@ fun run() {
testConversions()
testWeakRefs()
assertEquals(2, ForwardDeclaredEnum.TWO.value)
println(
getSupplier(
invoke1(42) { it * 2 }