diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt index 3b0cfd58d2b..2f7c0dfb4e4 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BitcodePhases.kt @@ -211,13 +211,25 @@ internal val escapeAnalysisPhase = makeKonanModuleOpPhase( op = { context, _ -> val entryPoint = context.ir.symbols.entryPoint?.owner val externalModulesDFG = ExternalModulesDFG(emptyList(), emptyMap(), emptyMap(), emptyMap()) + val nonDevirtualizedCallSitesUnfoldFactor = + if (entryPoint != null) { + // For a final program it can be safely assumed that what classes we see is what we got, + // so can take those. In theory we can always unfold call sites using type hierarchy, but + // the analysis might converge much, much slower, so take only reasonably small for now. + 5 + } + else { + // Can't tolerate any non-devirtualized call site for a library. + // TODO: What about private virtual functions? + // Note: 0 is also bad - this means that there're no inheritors in the current source set, + // but there might be some provided by the users of the library being produced. + -1 + } val callGraph = CallGraphBuilder( context, context.moduleDFG!!, externalModulesDFG, context.devirtualizationAnalysisResult!!, - // Can't tolerate any non-devirtualized call site for a library. - // TODO: What about private virtual functions? - nonDevirtualizedCallSitesUnfoldFactor = if (entryPoint == null) 0 else 5 + nonDevirtualizedCallSitesUnfoldFactor ).build() EscapeAnalysis.computeLifetimes( context, context.moduleDFG!!, externalModulesDFG, callGraph, context.lifetimes diff --git a/backend.native/tests/objcexport/expectedLazy.h b/backend.native/tests/objcexport/expectedLazy.h index 126a4441c11..43f80af0daa 100644 --- a/backend.native/tests/objcexport/expectedLazy.h +++ b/backend.native/tests/objcexport/expectedLazy.h @@ -532,6 +532,26 @@ __attribute__((swift_name("Kt39206Kt"))) + (int32_t)myFunc __attribute__((swift_name("myFunc()"))) __attribute__((deprecated("Don't call this\nPlease"))); @end; +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("Ckt41907"))) +@interface KtCkt41907 : KtBase +- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer)); ++ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead"))); +@end; + +__attribute__((swift_name("Ikt41907"))) +@protocol KtIkt41907 +@required +- (void)fooC:(KtCkt41907 *)c __attribute__((swift_name("foo(c:)"))); +@end; + +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("Kt41907Kt"))) +@interface KtKt41907Kt : KtBase ++ (void)escapeCC:(KtCkt41907 *)c __attribute__((swift_name("escapeC(c:)"))); ++ (void)testKt41907O:(id)o __attribute__((swift_name("testKt41907(o:)"))); +@end; + __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("LibraryKt"))) @interface KtLibraryKt : KtBase diff --git a/backend.native/tests/objcexport/kt41907.kt b/backend.native/tests/objcexport/kt41907.kt new file mode 100644 index 00000000000..9e361392f4e --- /dev/null +++ b/backend.native/tests/objcexport/kt41907.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package kt41907 + +class Ckt41907 + +interface Ikt41907 { + fun foo(c: Ckt41907) +} + +private class Bkt41907 { + var c: Ckt41907? = null +} + +private val b = Bkt41907() + +fun escapeC(c: Ckt41907) { + b.c = c +} + +fun testKt41907(o: Ikt41907) { + val c = Ckt41907() + o.foo(c) +} diff --git a/backend.native/tests/objcexport/kt41907.swift b/backend.native/tests/objcexport/kt41907.swift new file mode 100644 index 00000000000..d88e2ca79e2 --- /dev/null +++ b/backend.native/tests/objcexport/kt41907.swift @@ -0,0 +1,24 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +import Kt + +class Ikt41907Impl : Ikt41907 { + func foo(c: Ckt41907) { + Kt41907Kt.escapeC(c: c) + } +} + +private func test1() { + Kt41907Kt.testKt41907(o: Ikt41907Impl()) +} + +class Kt41907Tests : SimpleTestProvider { + override init() { + super.init() + + test("Test1", test1) + } +} \ No newline at end of file