From fd35ccbf2e34fd56a5cbcd9cc3203f54c7e2a642 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Tue, 19 Sep 2017 13:51:43 +0300 Subject: [PATCH] Represent `void*` as `CValuesRef<*>?` --- .../native/interop/gen/KotlinCodeModel.kt | 14 ++++++++++---- .../native/interop/gen/jvm/StubGenerator.kt | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/KotlinCodeModel.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/KotlinCodeModel.kt index 622fbd6d77c..7f05fab7a36 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/KotlinCodeModel.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/KotlinCodeModel.kt @@ -72,19 +72,25 @@ data class Classifier( val Classifier.type get() = KotlinClassifierType(this, arguments = emptyList(), nullable = false) -fun Classifier.typeWith(vararg arguments: KotlinType) = +fun Classifier.typeWith(vararg arguments: KotlinTypeArgument) = KotlinClassifierType(this, arguments.toList(), nullable = false) -interface KotlinType { +interface KotlinTypeArgument { /** - * @return the string to be used in the given scope to denote this type. + * @return the string to be used in the given scope to denote this. */ fun render(scope: KotlinScope): String } +object StarProjection : KotlinTypeArgument { + override fun render(scope: KotlinScope) = "*" +} + +interface KotlinType : KotlinTypeArgument + data class KotlinClassifierType( val classifier: Classifier, - val arguments: List, + val arguments: List, val nullable: Boolean ) : KotlinType { diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt index d2cf4366d6d..908c35143cd 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt @@ -266,7 +266,7 @@ class StubGenerator( return res } - fun representCFunctionParameterAsValuesRef(type: Type): Type? { + fun representCFunctionParameterAsValuesRef(type: Type): KotlinType? { val pointeeType = when (type) { is PointerType -> type.pointeeType is ArrayType -> type.elemType @@ -274,8 +274,14 @@ class StubGenerator( } val unwrappedPointeeType = pointeeType.unwrapTypedefs() - if (unwrappedPointeeType is VoidType || unwrappedPointeeType is FunctionType) { - // Passing `void`s or function by value is not very sane: + + if (unwrappedPointeeType is VoidType) { + // Represent `void*` as `CValuesRef<*>?`: + return KotlinTypes.cValuesRef.typeWith(StarProjection).makeNullable() + } + + if (unwrappedPointeeType is FunctionType) { + // Don't represent function pointer as `CValuesRef?` currently: return null } @@ -283,7 +289,8 @@ class StubGenerator( return representCFunctionParameterAsValuesRef(pointeeType) } - return pointeeType + + return KotlinTypes.cValuesRef.typeWith(mirror(pointeeType).pointedType).makeNullable() } private fun Type.isAliasOf(names: Set): Boolean { @@ -610,9 +617,7 @@ class StubGenerator( bodyGenerator.pushMemScoped() "$parameterName?.wcstr?.getPointer(memScope)" } else if (representAsValuesRef != null) { - val pointedType = mirror(representAsValuesRef).pointedType - val parameterType = KotlinTypes.cValuesRef.typeWith(pointedType).makeNullable() - kotlinParameters.add(parameterName to parameterType) + kotlinParameters.add(parameterName to representAsValuesRef) bodyGenerator.pushMemScoped() "$parameterName?.getPointer(memScope)" } else {