Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions native/swift/sir-light-classes/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {

api(project(":compiler:psi:psi-api"))
api(project(":analysis:analysis-api"))
implementation(project(":analysis:analysis-internal-utils"))

testImplementation(platform(libs.junit.bom))
testRuntimeOnly(libs.junit.jupiter.engine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.analysis.api.components.builtinTypes
import org.jetbrains.kotlin.analysis.api.components.containingSymbol
import org.jetbrains.kotlin.analysis.api.components.render
import org.jetbrains.kotlin.analysis.api.export.utilities.isSuspend
import org.jetbrains.kotlin.analysis.api.renderer.types.impl.KaTypeRendererForSource
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.types.KaTypeParameterType
import org.jetbrains.kotlin.sir.*
Expand Down Expand Up @@ -140,8 +141,9 @@ internal open class SirFunctionFromKtSymbol(
override val bridges: List<SirBridge> by lazyWithSessions {
bridgeProxy?.createSirBridges {
val typeArgs = ktSymbol.typeParameters.map { it.upperBounds.singleOrNull() ?: builtinTypes.nullableAny }
val renderer = KaTypeRendererForSource.UPPER_BOUNDS_WITH_QUALIFIED_NAMES
val typesAsString = typeArgs.takeIf { it.isNotEmpty() }?.joinToString(prefix = "<", postfix = ">") {
it.render(position = Variance.INVARIANT)
it.render(renderer, position = Variance.INVARIANT)
} ?: ""
val actualArgs = argNames.drop(if (extensionReceiverParameter != null) 1 else 0).dropLast(contextParameters.size)
val argumentsString = actualArgs.joinToString()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2010-2026 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package org.jetbrains.sir.lightclasses.utils

import org.jetbrains.kotlin.analysis.api.KaSession
import org.jetbrains.kotlin.analysis.api.renderer.types.KaTypeRenderer
import org.jetbrains.kotlin.analysis.api.renderer.types.impl.KaTypeRendererForSource
import org.jetbrains.kotlin.analysis.api.renderer.types.renderers.KaTypeParameterTypeRenderer
import org.jetbrains.kotlin.analysis.api.renderer.types.renderers.KaTypeProjectionRenderer
import org.jetbrains.kotlin.analysis.api.types.KaStarTypeProjection
import org.jetbrains.kotlin.analysis.api.types.KaType
import org.jetbrains.kotlin.analysis.api.types.KaTypeArgumentWithVariance
import org.jetbrains.kotlin.analysis.api.types.KaTypeParameterType
import org.jetbrains.kotlin.analysis.api.types.KaTypeProjection
import org.jetbrains.kotlin.analysis.utils.printer.PrettyPrinter

internal val KaTypeRendererForSource.UPPER_BOUNDS_WITH_QUALIFIED_NAMES: KaTypeRenderer
get() = WITH_QUALIFIED_NAMES.with {
typeParameterTypeRenderer = UpperBoundTypeParameterTypeRenderer
typeProjectionRenderer = UpperBoundTypeProjectionRenderer
}

private object UpperBoundTypeParameterTypeRenderer : KaTypeParameterTypeRenderer {
override fun renderType(
analysisSession: KaSession,
type: KaTypeParameterType,
typeRenderer: KaTypeRenderer,
printer: PrettyPrinter
) {
val type = type.symbol.upperBounds.singleOrNull() ?: analysisSession.builtinTypes.nullableAny
typeRenderer.renderType(analysisSession, type, printer)
}
}

private object UpperBoundTypeProjectionRenderer : KaTypeProjectionRenderer {
override fun renderTypeProjection(
analysisSession: KaSession,
projection: KaTypeProjection,
typeRenderer: KaTypeRenderer,
printer: PrettyPrinter,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why PrettyPrinter is part of :analysis:analysis-internal-utils though

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was also surprised, but given the fact that it just leaked as a part of public API - decided not to say anything :)

) {
when (projection) {
is KaStarTypeProjection -> printer.append('*')
is KaTypeArgumentWithVariance -> {
var type: KaType? = projection.type
while (type is KaTypeParameterType) {
type = type.symbol.upperBounds.singleOrNull()
}
if (type == null) {
type = analysisSession.builtinTypes.nullableAny
}
typeRenderer.renderType(analysisSession, type, printer)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ fun returnBFun(): BFun = TODO()

class MultipleUpperBounds<T> where T : Producer<String>, T : Consumer<String>

fun <A, B: A> returnGenericConstraintToGeneric(arg1: A, arg2: B): A = TODO()

fun <A, B : Box<A>> returnSomeBoxForArg(arg: A): B = TODO()

// MODULE: f_bounded_type
// EXPORT_TO_SWIFT
// FILE: f_bounded_type.kt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ void * __root___returnBFun();

void * __root___returnBoxFun();

void * _Nullable __root___returnGenericConstraintToGeneric__TypesOfArguments__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable___(void * _Nullable arg1, void * _Nullable arg2);

void * __root___returnSomeBoxForArg__TypesOfArguments__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable___(void * _Nullable arg);

_Bool __root___takeBoxStarProjection__TypesOfArguments__main_Box__(void * box);

_Bool __root___takeBoxUpperBound__TypesOfArguments__main_Box__(void * box);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,21 @@ public fun __root___returnBoxFun(): kotlin.native.internal.NativePtr {
return kotlin.native.internal.ref.createRetainedExternalRCRef(_result)
}

@ExportedBridge("__root___returnGenericConstraintToGeneric__TypesOfArguments__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable___")
public fun __root___returnGenericConstraintToGeneric__TypesOfArguments__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable___(arg1: kotlin.native.internal.NativePtr, arg2: kotlin.native.internal.NativePtr): kotlin.native.internal.NativePtr {
val __arg1 = if (arg1 == kotlin.native.internal.NativePtr.NULL) null else kotlin.native.internal.ref.dereferenceExternalRCRef(arg1) as kotlin.Any
val __arg2 = if (arg2 == kotlin.native.internal.NativePtr.NULL) null else kotlin.native.internal.ref.dereferenceExternalRCRef(arg2) as kotlin.Any
val _result = run { returnGenericConstraintToGeneric<kotlin.Any?, kotlin.Any?>(__arg1, __arg2) }
return if (_result == null) kotlin.native.internal.NativePtr.NULL else kotlin.native.internal.ref.createRetainedExternalRCRef(_result)
}

@ExportedBridge("__root___returnSomeBoxForArg__TypesOfArguments__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable___")
public fun __root___returnSomeBoxForArg__TypesOfArguments__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable___(arg: kotlin.native.internal.NativePtr): kotlin.native.internal.NativePtr {
val __arg = if (arg == kotlin.native.internal.NativePtr.NULL) null else kotlin.native.internal.ref.dereferenceExternalRCRef(arg) as kotlin.Any
val _result = run { returnSomeBoxForArg<kotlin.Any?, Box<kotlin.Any?>>(__arg) }
return kotlin.native.internal.ref.createRetainedExternalRCRef(_result)
}

@ExportedBridge("__root___takeBoxStarProjection__TypesOfArguments__main_Box__")
public fun __root___takeBoxStarProjection__TypesOfArguments__main_Box__(box: kotlin.native.internal.NativePtr): Boolean {
val __box = kotlin.native.internal.ref.dereferenceExternalRCRef(box) as Box<kotlin.Any?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ public func returnBoxFun() -> main.BoxFun {
return { return main.Box.__createClassWrapper(externalRCRef: main_internal_functional_type_caller_mainU2EBox__TypesOfArguments__Swift_UnsafeMutableRawPointer__(pointerToBlock.__externalRCRef()!)) }
}()
}
public func returnGenericConstraintToGeneric(
arg1: (any KotlinRuntimeSupport._KotlinBridgeable)?,
arg2: (any KotlinRuntimeSupport._KotlinBridgeable)?
) -> (any KotlinRuntimeSupport._KotlinBridgeable)? {
return { switch __root___returnGenericConstraintToGeneric__TypesOfArguments__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable___(arg1.map { it in it.__externalRCRef() } ?? nil, arg2.map { it in it.__externalRCRef() } ?? nil) { case nil: .none; case let res: KotlinRuntime.KotlinBase.__createBridgeable(externalRCRef: res); } }()
}
public func returnSomeBoxForArg(
arg: (any KotlinRuntimeSupport._KotlinBridgeable)?
) -> main.Box {
return main.Box.__createClassWrapper(externalRCRef: __root___returnSomeBoxForArg__TypesOfArguments__Swift_Optional_anyU20KotlinRuntimeSupport__KotlinBridgeable___(arg.map { it in it.__externalRCRef() } ?? nil))
}
public func takeBoxStarProjection(
box: main.Box
) -> Swift.Void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@
<version>ArtifactsTest.version</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>analysis-internal-utils</artifactId>
<version>ArtifactsTest.version</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>