Skip to content

Commit

Permalink
Implement InternalIfWrapped visibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipDolnik committed May 17, 2024
1 parent 36810d7 commit e7e8195
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion SKIE/acceptance-tests
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ annotation class SkieVisibility {
*
* For example, a top-level function originally exposed as `FileKt.functionName` will be internal, if SKIE generated the global function wrapper for it.
*
* Note that this setting will only affect callable declarations (functions, properties, constructors) - not classes.
* Note that this setting will only affect non-abstract callable declarations (functions, properties, constructors) - not classes.
*/
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.CONSTRUCTOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object SkieVisibility : ConfigurationKey.Enum<co.touchlab.skie.configuration.Ski
*
* For example, a top-level function originally exposed as `FileKt.functionName` will be internal, if SKIE generated the global function wrapper for it.
*
* Note that this setting will only affect callable declarations (functions, properties, constructors) - not classes.
* Note that this setting will only affect non-abstract callable declarations (functions, properties, constructors) - not classes.
*/
InternalIfWrapped(SkieVisibility.InternalIfWrapped::class),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class GlobalMembersConvertorDelegate(
configureBridge(function, sirFunction, this)
}
}

sirFunction.isWrappedBySkie = true
}

private fun SirSimpleFunction.addFunctionBody(function: SirSimpleFunction) {
Expand Down Expand Up @@ -66,6 +68,8 @@ class GlobalMembersConvertorDelegate(
property.bridgedSirProperty = this
}
}

sirProperty.isWrappedBySkie = true
}

private fun SirProperty.addPropertyGetter(originalGetter: SirGetter, sirProperty: SirProperty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class InterfaceExtensionMembersConvertorDelegate(
configureBridge(function, sirFunction, this)
}
}

sirFunction.isWrappedBySkie = true
}

private fun SirSimpleFunction.addFunctionBody(function: SirSimpleFunction) {
Expand Down Expand Up @@ -61,6 +63,9 @@ class InterfaceExtensionMembersConvertorDelegate(
// Bridged converted properties are not supported at the moment as they are not easy to implement and not needed yet

generateInterfaceExtensionPropertyWrapper(getter, setter, kirGetter)

getter.isWrappedBySkie = true
setter?.isWrappedBySkie = true
}

private fun generateInterfaceExtensionPropertyWrapper(getter: SirSimpleFunction, setter: SirSimpleFunction?, kirGetter: KirSimpleFunction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class SwiftSuspendFunctionGenerator {
it.applyToEntireOverrideHierarchy {
isHidden = true
isReplaced = true
isWrappedBySkie = true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package co.touchlab.skie.phases.sir.member

import co.touchlab.skie.configuration.SkieVisibility
import co.touchlab.skie.phases.SirPhase
import co.touchlab.skie.sir.element.SirCallableDeclaration
import co.touchlab.skie.sir.element.SirVisibility
import co.touchlab.skie.sir.element.isAbstract

object ConfigureInternalVisibilityForWrappedCallableDeclarationsPhase : SirPhase {

context(SirPhase.Context)
override suspend fun execute() {
sirProvider.allLocalCallableDeclarations
.filter { it.isWrappedBySkie && it.shouldBeInternalIfWrappedBySkie && it.visibility == SirVisibility.Public && !it.isAbstract }
.forEach {
it.visibility = SirVisibility.Internal
}
}

context(SirPhase.Context)
private val SirCallableDeclaration.shouldBeInternalIfWrappedBySkie: Boolean
// TODO Once SirCallableDeclaration origin is added check even for intermediate wrappers and configure those based on the Kir configuration as well.
get() = kirProvider.findCallableDeclaration<SirCallableDeclaration>(this)?.configuration?.get(SkieVisibility) == SkieVisibility.Level.InternalIfWrapped
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import co.touchlab.skie.sir.element.getAllDeclarationsRecursively
/**
* Changes `SirVisibility.Private` to `SirVisibility.Removed` in all Kotlin declarations.
*/
object NormalizeKotlinSirVisibilityPhase : SirPhase {
object NormalizeKotlinSirPrivateVisibilityPhase : SirPhase {

context(SirPhase.Context)
override suspend fun execute() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import co.touchlab.skie.phases.SirPhase
import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirModule
import co.touchlab.skie.sir.element.SirVisibility
import co.touchlab.skie.sir.element.coerceAtMost
import co.touchlab.skie.sir.element.kirClassOrNull
import co.touchlab.skie.sir.element.superClassType
import co.touchlab.skie.sir.element.superProtocolTypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ sealed interface SirCallableDeclaration : SirDeclarationWithVisibility, SirEleme

val deprecationLevel: DeprecationLevel

// TODO Remove and check wrappedBy functions in Sir instead (once the concept of bridged callable declarations is refactored).
var isWrappedBySkie: Boolean

fun toReadableString(): String =
Signature(this).toString()
}
Expand All @@ -57,3 +60,6 @@ val SirCallableDeclaration.receiverDeclaration: SirClass?
is SirExtension -> parent.classDeclaration
else -> null
}

val SirCallableDeclaration.isAbstract: Boolean
get() = (this as? SirOverridableDeclaration<*>)?.isAbstract == true
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class SirConstructor(
var isConvenience: Boolean = false,
override var throws: Boolean = false,
override val deprecationLevel: DeprecationLevel = DeprecationLevel.None,
override var isWrappedBySkie: Boolean = false,
) : SirFunction(attributes.toMutableList(), modifiers.toMutableList()) {

override val identifier = "init"
Expand Down Expand Up @@ -46,6 +47,7 @@ class SirConstructor(
isConvenience: Boolean = false,
throws: Boolean = false,
deprecationLevel: DeprecationLevel = DeprecationLevel.None,
isWrappedBySkie: Boolean = false,
): SirConstructor =
SirConstructor(
parent = this@SirDeclarationNamespace,
Expand All @@ -56,6 +58,7 @@ class SirConstructor(
isConvenience = isConvenience,
throws = throws,
deprecationLevel = deprecationLevel,
isWrappedBySkie = isWrappedBySkie,
)
}
}
Expand All @@ -69,6 +72,7 @@ fun SirConstructor.shallowCopy(
isConvenience: Boolean = this.isConvenience,
throws: Boolean = this.throws,
deprecationLevel: DeprecationLevel = this.deprecationLevel,
isWrappedBySkie: Boolean = false,
): SirConstructor =
SirConstructor(
parent = parent,
Expand All @@ -79,4 +83,5 @@ fun SirConstructor.shallowCopy(
isConvenience = isConvenience,
throws = throws,
deprecationLevel = deprecationLevel,
isWrappedBySkie = isWrappedBySkie,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SirProperty(
override var scope: SirScope = parent.coerceScope(SirScope.Member),
override val deprecationLevel: DeprecationLevel = DeprecationLevel.None,
override val isFakeOverride: Boolean = false,
override var isWrappedBySkie: Boolean = false,
attributes: List<String> = emptyList(),
modifiers: List<Modifier> = emptyList(),
) : SirOverridableDeclaration<SirProperty>, SirCallableDeclaration, SirElementWithModality {
Expand Down Expand Up @@ -93,6 +94,7 @@ class SirProperty(
scope: SirScope = coerceScope(SirScope.Member),
deprecationLevel: DeprecationLevel = DeprecationLevel.None,
isFakeOverride: Boolean = false,
isWrappedBySkie: Boolean = false,
attributes: List<String> = emptyList(),
modifiers: List<Modifier> = emptyList(),
): SirProperty =
Expand All @@ -108,6 +110,7 @@ class SirProperty(
scope = scope,
deprecationLevel = deprecationLevel,
isFakeOverride = isFakeOverride,
isWrappedBySkie = isWrappedBySkie,
attributes = attributes,
modifiers = modifiers,
)
Expand All @@ -126,6 +129,7 @@ fun SirProperty.shallowCopy(
scope: SirScope = parent.coerceScope(this.scope),
deprecationLevel: DeprecationLevel = this.deprecationLevel,
isFakeOverride: Boolean = this.isFakeOverride,
isWrappedBySkie: Boolean = false,
attributes: List<String> = this.attributes,
modifiers: List<Modifier> = this.modifiers,
): SirProperty =
Expand All @@ -141,6 +145,7 @@ fun SirProperty.shallowCopy(
scope = scope,
deprecationLevel = deprecationLevel,
isFakeOverride = isFakeOverride,
isWrappedBySkie = isWrappedBySkie,
attributes = attributes,
modifiers = modifiers,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class SirSimpleFunction(
var returnType: SirType,
override var visibility: SirVisibility = SirVisibility.Public,
override var modality: SirModality = parent.coerceModalityForSimpleFunctionOrProperty(),
override val isAbstract: Boolean,
override val isAbstract: Boolean = false,
override var isReplaced: Boolean = false,
override var isHidden: Boolean = false,
override var scope: SirScope = parent.coerceScope(SirScope.Member),
override val isFakeOverride: Boolean = false,
override var isWrappedBySkie: Boolean = false,
attributes: List<String> = emptyList(),
modifiers: List<Modifier> = emptyList(),
var isAsync: Boolean = false,
Expand Down Expand Up @@ -77,6 +78,7 @@ class SirSimpleFunction(
isHidden: Boolean = false,
scope: SirScope = coerceScope(SirScope.Member),
isFakeOverride: Boolean = false,
isWrappedBySkie: Boolean = false,
attributes: List<String> = emptyList(),
modifiers: List<Modifier> = emptyList(),
isAsync: Boolean = false,
Expand All @@ -94,6 +96,7 @@ class SirSimpleFunction(
isHidden = isHidden,
scope = scope,
isFakeOverride = isFakeOverride,
isWrappedBySkie = isWrappedBySkie,
attributes = attributes,
modifiers = modifiers,
isAsync = isAsync,
Expand All @@ -114,6 +117,7 @@ fun SirSimpleFunction.shallowCopy(
isHidden: Boolean = this.isHidden,
scope: SirScope = parent.coerceScope(this.scope),
isFakeOverride: Boolean = this.isFakeOverride,
isWrappedBySkie: Boolean = false,
attributes: List<String> = this.attributes,
modifiers: List<Modifier> = this.modifiers,
isAsync: Boolean = this.isAsync,
Expand All @@ -131,6 +135,7 @@ fun SirSimpleFunction.shallowCopy(
isHidden = isHidden,
scope = scope,
isFakeOverride = isFakeOverride,
isWrappedBySkie = isWrappedBySkie,
attributes = attributes,
modifiers = modifiers,
isAsync = isAsync,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package co.touchlab.skie.util
import co.touchlab.skie.configuration.SkieVisibility
import co.touchlab.skie.sir.element.SirVisibility

fun SkieVisibility.Level.toSirVisibility(isWrapped: Boolean = false): SirVisibility =
fun SkieVisibility.Level.toSirVisibility(): SirVisibility =
when (this) {
SkieVisibility.Level.Public -> SirVisibility.Public
SkieVisibility.Level.PublicButHidden -> SirVisibility.Public
SkieVisibility.Level.PublicButReplaced -> SirVisibility.Public
SkieVisibility.Level.Internal -> SirVisibility.Internal
SkieVisibility.Level.InternalAndReplaced -> SirVisibility.Internal
SkieVisibility.Level.InternalIfWrapped -> if (isWrapped) SirVisibility.Internal else SirVisibility.Public
SkieVisibility.Level.InternalIfWrapped -> SirVisibility.Public
SkieVisibility.Level.Private -> SirVisibility.Removed
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ import co.touchlab.skie.phases.runtime.ConfigureStableNameTypeAliasesForKotlinRu
import co.touchlab.skie.phases.runtime.KotlinRuntimeHidingPhase
import co.touchlab.skie.phases.runtime.SwiftRuntimeGenerator
import co.touchlab.skie.phases.sir.CommitSirIsReplacedPropertyPhase
import co.touchlab.skie.phases.sir.member.ConfigureInternalVisibilityForWrappedCallableDeclarationsPhase
import co.touchlab.skie.phases.sir.member.CreateAsyncSirFunctionsPhase
import co.touchlab.skie.phases.sir.member.CreateSirMembersPhase
import co.touchlab.skie.phases.sir.member.InitializeSirMembersCachePhase
import co.touchlab.skie.phases.sir.member.InitializeSirOverridesPhase
import co.touchlab.skie.phases.sir.member.NormalizeKotlinSirVisibilityPhase
import co.touchlab.skie.phases.sir.member.NormalizeKotlinSirPrivateVisibilityPhase
import co.touchlab.skie.phases.sir.member.VerifySirVisibilityInAbstractMembersPhase
import co.touchlab.skie.phases.sir.member.PropagateSirVisibilityToMembersPhase
import co.touchlab.skie.phases.sir.member.StripKonanCallableDeclarationManglingPhase
Expand Down Expand Up @@ -188,7 +189,7 @@ class LinkerPhaseScheduler : SkiePhaseScheduler {
FixNamesOfInaccessibleNestedClassesPhase,
CommitSirIsReplacedPropertyPhase,

NormalizeKotlinSirVisibilityPhase,
NormalizeKotlinSirPrivateVisibilityPhase,
PropagateSirVisibilityToClassesPhase,
PropagateSirVisibilityToMembersPhase,
PropagateSirVisibilityToTypeAliasesPhase,
Expand Down Expand Up @@ -228,7 +229,8 @@ class LinkerPhaseScheduler : SkiePhaseScheduler {

MoveBridgesToTopLevelPhase,

NormalizeKotlinSirVisibilityPhase,
NormalizeKotlinSirPrivateVisibilityPhase,
ConfigureInternalVisibilityForWrappedCallableDeclarationsPhase,
PropagateSirVisibilityToClassesPhase,
PropagateSirVisibilityToMembersPhase,
PropagateSirVisibilityToFileClassesPhase,
Expand Down

0 comments on commit e7e8195

Please sign in to comment.