Skip to content

Commit

Permalink
Fix reference usage in generated code for interface extension propert…
Browse files Browse the repository at this point in the history
…y wrappers.
  • Loading branch information
FilipDolnik committed Nov 23, 2023
1 parent 6e0c5f7 commit 47ac5f2
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import co.touchlab.skie.sir.element.SirGetter
import co.touchlab.skie.sir.element.SirProperty
import co.touchlab.skie.sir.element.SirSetter
import co.touchlab.skie.sir.element.SirSimpleFunction
import co.touchlab.skie.sir.element.call
import co.touchlab.skie.sir.element.copyValueParametersFrom
import co.touchlab.skie.sir.element.shallowCopy
import co.touchlab.skie.util.swift.addFunctionDeclarationBodyWithErrorTypeHandling
import io.outfoxx.swiftpoet.CodeBlock
import io.outfoxx.swiftpoet.Modifier
import io.outfoxx.swiftpoet.joinToCode

object ExhaustiveEnumsMembersPassthroughGenerator {

Expand Down Expand Up @@ -70,7 +69,7 @@ object ExhaustiveEnumsMembersPassthroughGenerator {
"return %L%L(self as _ObjectiveCType).%L",
if (function.throws) "try " else "",
if (function.isAsync) "await " else "",
function.call(function.valueParameters.map { CodeBlock.toString("%N", it.name) }),
function.call(function.valueParameters),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import co.touchlab.skie.sir.element.SirGetter
import co.touchlab.skie.sir.element.SirProperty
import co.touchlab.skie.sir.element.SirSetter
import co.touchlab.skie.sir.element.SirSimpleFunction
import co.touchlab.skie.sir.element.call
import co.touchlab.skie.sir.element.copyValueParametersFrom
import co.touchlab.skie.sir.element.shallowCopy
import co.touchlab.skie.util.swift.addFunctionDeclarationBodyWithErrorTypeHandling
import io.outfoxx.swiftpoet.CodeBlock
import io.outfoxx.swiftpoet.joinToCode

class GlobalMembersConvertorDelegate(
private val parentProvider: FileScopeConversionParentProvider,
Expand Down Expand Up @@ -42,7 +41,7 @@ class GlobalMembersConvertorDelegate(
if (function.throws) "try " else "",
if (function.isAsync) "await " else "",
function.kotlinStaticMemberOwnerTypeName,
function.call(valueParameters.map { CodeBlock.toString("%N", it.name) }),
function.call(valueParameters),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import co.touchlab.skie.sir.element.copyValueParametersFrom
import co.touchlab.skie.sir.element.isExported
import co.touchlab.skie.sir.element.shallowCopy
import co.touchlab.skie.util.swift.addFunctionDeclarationBodyWithErrorTypeHandling
import co.touchlab.skie.util.swift.escapeSwiftIdentifier
import io.outfoxx.swiftpoet.CodeBlock
import io.outfoxx.swiftpoet.joinToCode

Expand Down Expand Up @@ -44,7 +45,7 @@ class InterfaceExtensionMembersConvertorDelegate(
if (function.isAsync) "await " else "",
// Interfaces cannot be bridged
function.kotlinStaticMemberOwnerTypeName,
function.call(listOf("self") + valueParameters.map { CodeBlock.toString("%N", it.name) }),
function.call(listOf("self") + valueParameters.map { it.name.escapeSwiftIdentifier() }),
)
}
}
Expand Down Expand Up @@ -92,9 +93,9 @@ class InterfaceExtensionMembersConvertorDelegate(
).apply {
addFunctionDeclarationBodyWithErrorTypeHandling(getter) {
addStatement(
"return %T.%N(self)",
"return %T.%L",
getter.kotlinStaticMemberOwnerTypeName,
getter.reference,
getter.call("self"),
)
}
}
Expand All @@ -108,10 +109,9 @@ class InterfaceExtensionMembersConvertorDelegate(
).apply {
addFunctionDeclarationBodyWithErrorTypeHandling(setter) {
addStatement(
"%T.%N(self, %N)",
"%T.%L",
setter.kotlinStaticMemberOwnerTypeName,
setter.reference,
parameterName,
setter.call("self", parameterName.escapeSwiftIdentifier()),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirSimpleFunction
import co.touchlab.skie.sir.element.SirTypeParameter
import co.touchlab.skie.sir.element.SirValueParameter
import co.touchlab.skie.sir.element.call
import co.touchlab.skie.sir.element.copyTypeParametersFrom
import co.touchlab.skie.sir.element.toTypeFromEnclosingTypeParameters
import co.touchlab.skie.sir.element.toTypeParameterUsage
import co.touchlab.skie.sir.type.SirType
import co.touchlab.skie.sir.type.toNullable
import co.touchlab.skie.util.swift.escapeSwiftIdentifier
import io.outfoxx.swiftpoet.CodeBlock
import io.outfoxx.swiftpoet.TypeName

Expand Down Expand Up @@ -162,12 +164,10 @@ class SealedFunctionGeneratorDelegate(
bodyBuilder.add {
val valueParameter = valueParameters.first()

val escapedParameterName = CodeBlock.toString("%N", valueParameter.name)

addCode(
CodeBlock.builder()
.beginControlFlow("if", "let $escapedParameterName")
.add("return %L as %T", requiredFunction.call(escapedParameterName), requiredFunction.returnType.evaluate().swiftPoetTypeName)
.beginControlFlow("if", "let ${valueParameter.name.escapeSwiftIdentifier()}")
.add("return %L as %T", requiredFunction.call(valueParameter), requiredFunction.returnType.evaluate().swiftPoetTypeName)
.nextControlFlow("else")
.add("return nil")
.endControlFlow("else")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import co.touchlab.skie.sir.type.NullableSirType
import co.touchlab.skie.sir.type.OirDeclaredSirType
import co.touchlab.skie.sir.type.SirType
import co.touchlab.skie.util.swift.addFunctionDeclarationBodyWithErrorTypeHandling
import co.touchlab.skie.util.swift.escapeSwiftIdentifier
import io.outfoxx.swiftpoet.AttributeSpec
import io.outfoxx.swiftpoet.CodeBlock
import io.outfoxx.swiftpoet.FunctionTypeName
Expand Down Expand Up @@ -162,10 +163,10 @@ private fun MutableList<String>.addValueParameters(bridgeModel: BridgeModel) {
if (parameter.type.evaluate().swiftPoetTypeName != erasedParameterType) {
add(CodeBlock.toString("%N as! %T", parameter.name, erasedParameterType))
} else {
add(CodeBlock.toString("%N", parameter.name))
add(parameter.name.escapeSwiftIdentifier())
}
} else {
add(CodeBlock.toString("%N", parameter.name))
add(parameter.name.escapeSwiftIdentifier())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.touchlab.skie.sir.element

import co.touchlab.skie.phases.memberconflicts.signature
import co.touchlab.skie.util.swift.escapeSwiftIdentifier
import io.outfoxx.swiftpoet.CodeBlock
import io.outfoxx.swiftpoet.FunctionSpec
import io.outfoxx.swiftpoet.Modifier
Expand Down Expand Up @@ -54,6 +55,12 @@ sealed class SirFunction(
override fun toString(): String = this.signature.toString()
}

fun SirFunction.call(arguments: List<SirValueParameter>): String =
call(arguments.map { it.name.escapeSwiftIdentifier() })

fun SirFunction.call(vararg arguments: SirValueParameter): String =
call(arguments.toList())

fun SirFunction.copyValueParametersFrom(other: SirFunction) {
copyValueParametersFrom(other.valueParameters)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import co.touchlab.skie.kir.element.DeprecationLevel
import co.touchlab.skie.phases.memberconflicts.signature
import co.touchlab.skie.sir.element.util.sirDeclarationParent
import co.touchlab.skie.sir.type.SirType
import co.touchlab.skie.util.swift.escapeSwiftIdentifier
import io.outfoxx.swiftpoet.CodeBlock
import io.outfoxx.swiftpoet.Modifier

Expand All @@ -27,7 +28,7 @@ class SirProperty(
}

override val reference: String
get() = CodeBlock.toString("%N", identifierAfterVisibilityChange)
get() = identifierAfterVisibilityChange.escapeSwiftIdentifier()

override val name: String
get() = identifierAfterVisibilityChange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import co.touchlab.skie.sir.type.SirType
import io.outfoxx.swiftpoet.Modifier
import co.touchlab.skie.kir.element.DeprecationLevel
import co.touchlab.skie.phases.memberconflicts.signature
import co.touchlab.skie.util.swift.escapeSwiftIdentifier
import io.outfoxx.swiftpoet.CodeBlock

class SirSimpleFunction(
Expand All @@ -27,7 +28,7 @@ class SirSimpleFunction(
}

override val identifierForReference: String
get() = CodeBlock.toString("%N", identifierAfterVisibilityChange)
get() = identifierAfterVisibilityChange.escapeSwiftIdentifier()

override var parent: SirDeclarationParent by sirDeclarationParent(parent)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package co.touchlab.skie.util.swift

import io.outfoxx.swiftpoet.CodeBlock

fun String.escapeSwiftIdentifier(): String =
CodeBlock.toString("%N", this)

0 comments on commit 47ac5f2

Please sign in to comment.