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
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<KotlinType>,
val arguments: List<KotlinTypeArgument>,
val nullable: Boolean
) : KotlinType {
@@ -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<T>?` 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<String>): 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 {