Represent void* as CValuesRef<*>?

This commit is contained in:
Svyatoslav Scherbina
2017-09-19 13:51:43 +03:00
committed by SvyatoslavScherbina
parent 17ccd3ee02
commit fd35ccbf2e
2 changed files with 22 additions and 11 deletions
@@ -72,19 +72,25 @@ data class Classifier(
val Classifier.type val Classifier.type
get() = KotlinClassifierType(this, arguments = emptyList(), nullable = false) 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) 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 fun render(scope: KotlinScope): String
} }
object StarProjection : KotlinTypeArgument {
override fun render(scope: KotlinScope) = "*"
}
interface KotlinType : KotlinTypeArgument
data class KotlinClassifierType( data class KotlinClassifierType(
val classifier: Classifier, val classifier: Classifier,
val arguments: List<KotlinType>, val arguments: List<KotlinTypeArgument>,
val nullable: Boolean val nullable: Boolean
) : KotlinType { ) : KotlinType {
@@ -266,7 +266,7 @@ class StubGenerator(
return res return res
} }
fun representCFunctionParameterAsValuesRef(type: Type): Type? { fun representCFunctionParameterAsValuesRef(type: Type): KotlinType? {
val pointeeType = when (type) { val pointeeType = when (type) {
is PointerType -> type.pointeeType is PointerType -> type.pointeeType
is ArrayType -> type.elemType is ArrayType -> type.elemType
@@ -274,8 +274,14 @@ class StubGenerator(
} }
val unwrappedPointeeType = pointeeType.unwrapTypedefs() 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<T>?` currently:
return null return null
} }
@@ -283,7 +289,8 @@ class StubGenerator(
return representCFunctionParameterAsValuesRef(pointeeType) return representCFunctionParameterAsValuesRef(pointeeType)
} }
return pointeeType
return KotlinTypes.cValuesRef.typeWith(mirror(pointeeType).pointedType).makeNullable()
} }
private fun Type.isAliasOf(names: Set<String>): Boolean { private fun Type.isAliasOf(names: Set<String>): Boolean {
@@ -610,9 +617,7 @@ class StubGenerator(
bodyGenerator.pushMemScoped() bodyGenerator.pushMemScoped()
"$parameterName?.wcstr?.getPointer(memScope)" "$parameterName?.wcstr?.getPointer(memScope)"
} else if (representAsValuesRef != null) { } else if (representAsValuesRef != null) {
val pointedType = mirror(representAsValuesRef).pointedType kotlinParameters.add(parameterName to representAsValuesRef)
val parameterType = KotlinTypes.cValuesRef.typeWith(pointedType).makeNullable()
kotlinParameters.add(parameterName to parameterType)
bodyGenerator.pushMemScoped() bodyGenerator.pushMemScoped()
"$parameterName?.getPointer(memScope)" "$parameterName?.getPointer(memScope)"
} else { } else {