Skip to content

Commit

Permalink
Fix translation for ObjCProtocol 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipDolnik committed Nov 15, 2023
1 parent 5a062ad commit 48c9986
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ class ExtraDescriptorBuiltins(

private val Darwin = getModule("<org.jetbrains.kotlin.native.platform.darwin>")

private val stdlib = getModule("<stdlib>")

val Protocol: ClassDescriptor = getClass("kotlinx.cinterop.ObjCProtocol", stdlib)

val NSObject: ClassDescriptor = getClass("platform.darwin.NSObject", Darwin)

val NSCopying: ClassDescriptor = getClass("platform.Foundation.NSCopyingProtocol", Foundation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ class OirBuiltins(

private val Foundation: OirModule.External = oirProvider.getExternalModule("Foundation")

val Protocol: OirClass = oirProvider.getExternalClass(extraDescriptorBuiltins.Protocol, Foundation).apply {
name = "Protocol"
kind = OirClass.Kind.Class
}

val NSObject: OirClass = oirProvider.getExternalClass(extraDescriptorBuiltins.NSObject, Foundation)

val NSError: OirClass = oirProvider.getExternalClass(extraDescriptorBuiltins.NSError).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import co.touchlab.skie.oir.type.OirType
import co.touchlab.skie.sir.element.SirClass

class OirClass(
override var name: String,
override val name: String,
override val parent: OirTopLevelDeclarationParent,
var kind: Kind,
val kind: Kind,
) : OirTypeDeclaration, OirCallableDeclarationParent {

lateinit var originalSirClass: SirClass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package co.touchlab.skie.oir.type

sealed class SpecialReferenceOirType(private val name: String) : NonNullReferenceOirType() {
sealed class SpecialReferenceOirType(val name: String) : NonNullReferenceOirType() {

object Id : SpecialReferenceOirType("id")

object InstanceType : SpecialReferenceOirType("instancetype")

object Class : SpecialReferenceOirType("Class")

object Protocol : SpecialReferenceOirType("Protocol") {

override fun render(attrsAndName: String, needsNonnullAttribute: Boolean): String =
buildString {
append(name)
append(" *")
appendAttrsAndName(attrsAndName)
}
}

override fun render(attrsAndName: String, needsNonnullAttribute: Boolean): String =
name.withAttrsAndName(attrsAndName.plusNonnullAttributeIfNeeded(needsNonnullAttribute))
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class OirTypeTranslator(
// TODO: more precise types can be used.

if (descriptor.isObjCMetaClass()) return SpecialReferenceOirType.Class
if (descriptor.isObjCProtocolClass()) return oirBuiltins.Protocol.defaultType
if (descriptor.isObjCProtocolClass()) return SpecialReferenceOirType.Protocol

if (descriptor.isExternalObjCClass() || descriptor.isObjCForwardDeclaration()) {
return oirProvider.getExternalClass(descriptor).defaultType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class FixOirFunctionSignaturesForApiNotesPhase(
}
is TypeDefOirType -> this.declaration.name in reservedIdentifiers
is NullableReferenceOirType -> this.nonNullType.collidesWith(reservedIdentifiers)
is SpecialReferenceOirType -> this.name in reservedIdentifiers
else -> this.renderWithoutAttributes() in reservedIdentifiers
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package co.touchlab.skie.sir.type

import co.touchlab.skie.sir.element.SirTypeParameter
import io.outfoxx.swiftpoet.AnyTypeName
import io.outfoxx.swiftpoet.ProtocolTypeName
import io.outfoxx.swiftpoet.SelfTypeName
import io.outfoxx.swiftpoet.TypeName

Expand Down Expand Up @@ -36,4 +37,6 @@ sealed class SpecialSirType<SELF : SirType>(
object Self : SpecialSirType<Self>(SelfTypeName.INSTANCE)

object Any : SpecialSirType<Any>(AnyTypeName.INSTANCE)

object Protocol : SpecialSirType<Protocol>(ProtocolTypeName.INSTANCE)
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class SirTypeTranslator(
SpecialReferenceOirType.Class -> sirBuiltins.Swift.AnyClass.defaultType
SpecialReferenceOirType.Id -> SpecialSirType.Any
SpecialReferenceOirType.InstanceType -> SpecialSirType.Self
SpecialReferenceOirType.Protocol -> SpecialSirType.Protocol
}

private fun mapType(oirType: TypeParameterUsageOirType): SirType =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.outfoxx.swiftpoet

/*
* Copyright 2023 Touchlab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

class ProtocolTypeName private constructor() : TypeName() {

override fun emit(out: CodeWriter): CodeWriter {
out.emit("Protocol")
return out
}

companion object {

val INSTANCE = ProtocolTypeName()
}
}

0 comments on commit 48c9986

Please sign in to comment.