diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/enums/ExhaustiveEnumsMembersPassthroughGenerator.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/enums/ExhaustiveEnumsMembersPassthroughGenerator.kt index 1d2417e7..441a6645 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/enums/ExhaustiveEnumsMembersPassthroughGenerator.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/enums/ExhaustiveEnumsMembersPassthroughGenerator.kt @@ -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 { @@ -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), ) } } diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/functions/GlobalMembersConvertorDelegate.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/functions/GlobalMembersConvertorDelegate.kt index 79b09b3e..31879af6 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/functions/GlobalMembersConvertorDelegate.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/functions/GlobalMembersConvertorDelegate.kt @@ -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, @@ -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), ) } } diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/functions/InterfaceExtensionMembersConvertorDelegate.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/functions/InterfaceExtensionMembersConvertorDelegate.kt index 5650a709..5876e199 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/functions/InterfaceExtensionMembersConvertorDelegate.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/functions/InterfaceExtensionMembersConvertorDelegate.kt @@ -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 @@ -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() }), ) } } @@ -92,9 +93,9 @@ class InterfaceExtensionMembersConvertorDelegate( ).apply { addFunctionDeclarationBodyWithErrorTypeHandling(getter) { addStatement( - "return %T.%N(self)", + "return %T.%L", getter.kotlinStaticMemberOwnerTypeName, - getter.reference, + getter.call("self"), ) } } @@ -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()), ) } } diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/sealed/SealedFunctionGeneratorDelegate.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/sealed/SealedFunctionGeneratorDelegate.kt index c5e417f7..eef9dce7 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/sealed/SealedFunctionGeneratorDelegate.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/sealed/SealedFunctionGeneratorDelegate.kt @@ -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 @@ -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") diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/suspend/SwiftSuspendGeneratorDelegate.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/suspend/SwiftSuspendGeneratorDelegate.kt index 1977a9fb..ad44bf4f 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/suspend/SwiftSuspendGeneratorDelegate.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/phases/features/suspend/SwiftSuspendGeneratorDelegate.kt @@ -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 @@ -162,10 +163,10 @@ private fun MutableList.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()) } } } diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirFunction.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirFunction.kt index f1230928..928488a8 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirFunction.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirFunction.kt @@ -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 @@ -54,6 +55,12 @@ sealed class SirFunction( override fun toString(): String = this.signature.toString() } +fun SirFunction.call(arguments: List): 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) } diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirProperty.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirProperty.kt index b22cf810..3ceb1822 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirProperty.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirProperty.kt @@ -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 @@ -27,7 +28,7 @@ class SirProperty( } override val reference: String - get() = CodeBlock.toString("%N", identifierAfterVisibilityChange) + get() = identifierAfterVisibilityChange.escapeSwiftIdentifier() override val name: String get() = identifierAfterVisibilityChange diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirSimpleFunction.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirSimpleFunction.kt index c52d4c4b..939be026 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirSimpleFunction.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/element/SirSimpleFunction.kt @@ -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( @@ -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) diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/util/swift/String+escapeSwiftIdentifier.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/util/swift/String+escapeSwiftIdentifier.kt new file mode 100644 index 00000000..26973c4b --- /dev/null +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/util/swift/String+escapeSwiftIdentifier.kt @@ -0,0 +1,6 @@ +package co.touchlab.skie.util.swift + +import io.outfoxx.swiftpoet.CodeBlock + +fun String.escapeSwiftIdentifier(): String = + CodeBlock.toString("%N", this)