Files
swift-mirror/test/SILGen/objc_direct.swift
Doug Gregor f267f62f65 [SILGen] Consistently use SIL asmname for foreign function/variable references
Whenever we have a reference to a foreign function/variable in SIL, use
a mangled name at the SIL level with the C name in the asmname
attribute. The expands the use of asmname to three kinds of cases that
it hadn't been used in yet:

* Declarations imported from C headers/modules
* @_cdecl @implementation of C headers/modules
* @_cdecl functions in general

Some code within the SIL pipeline makes assumptions that the C names of
various runtime functions are reflected at the SIL level. For example,
the linking of Embedded Swift runtime functions is done by-name, and
some of those names refer to C functions (like `swift_retain`) and
others refer to Swift functions that use `@_silgen_name` (like
`swift_getDefaultExecutor`). Extend the serialized module format to
include a table that maps from the asmname of functions/variables over
to their mangled names, so we can look up functions by asmname if we
want. These tables could also be used for checking for declarations
that conflict on their asmname in the future. Right now, we leave it
up to LLVM or the linker to do the checking.

`@_silgen_name` is not affected by these changes, nor should it be:
that hidden feature is specifically meant to affect the name at the
SIL level.

The vast majority of test changes are SIL tests where we had expected
to see the C/C++/Objective-C names in the tests for references to
foreign entities, and now we see Swift mangled names (ending in To).
The SIL declarations themselves will have a corresponding asmname.

Notably, the IRGen tests have *not* changed, because we generally the
same IR as before. It's only the modeling at the SIL lever that has
changed.

Another part of rdar://137014448.
2025-10-29 19:35:55 -07:00

72 lines
3.3 KiB
Swift

// RUN: %target-swift-emit-silgen -import-objc-header %S/../Inputs/objc_direct.h -o - %s | %FileCheck %s
// REQUIRES: objc_interop
// NOTE: `[[BYTE01]]` should match the byte `0b01`.
func markUsed<T>(_ t: T) {}
protocol BarProtocol {
func directProtocolMethod() -> String!
}
extension Bar: BarProtocol {}
let bar = Bar()
markUsed(Bar(value: 0))
// CHECK: function_ref @$sSo3BarC5valueABSgs5Int32V_tcfC
markUsed(Bar.init(value: 0))
// CHECK: function_ref @$sSo3BarC5valueABSgs5Int32V_tcfC
bar.directProperty = 123
// CHECK: function_ref @$sSo3BarC14directPropertys5Int32VvsTo
markUsed(bar.directProperty)
// CHECK: function_ref @$sSo3BarC14directPropertys5Int32VvgTo
bar.directProperty2 = 456
// CHECK: function_ref @$sSo3BarC15directProperty2s5Int32VvsTo
markUsed(bar.directProperty2)
// CHECK: function_ref @$sSo3BarC15directProperty2s5Int32VvgTo
bar[0] = 789
// CHECK: function_ref @$sSo3BarCys5Int32VADcisTo
markUsed(bar[0])
// CHECK: function_ref @$sSo3BarCys5Int32VADcigTo
markUsed(bar.directMethod())
// CHECK: function_ref @$sSo3BarC12directMethodSSSgyFTo
markUsed(bar.directMethod2())
// CHECK: function_ref @$sSo3BarC13directMethod2SSSgyFTo
markUsed(Bar.directClassMethod())
// CHECK: function_ref @$sSo3BarC17directClassMethodSSSgyFZTo
markUsed(Bar.directClassMethod2())
// CHECK: function_ref @$sSo3BarC18directClassMethod2SSSgyFZTo
markUsed(bar.directProtocolMethod())
// CHECK: function_ref @$sSo3BarC20directProtocolMethodSSSgyFTo
// CHECK-LABEL: sil shared [serialized] [ossa] @$sSo3BarC5valueABSgs5Int32V_tcfC : $@convention(method) (Int32, @thick Bar.Type) -> @owned Optional<Bar> {
// CHECK: {{.*}} = function_ref @$sSo3BarC5valueABSgs5Int32V_tcfcTO : $@convention(method)
// CHECK: } // end sil function '$sSo3BarC5valueABSgs5Int32V_tcfC'
// CHECK: sil [asmname "[[BYTE01:.]]-[Bar setDirectProperty:]"] @$sSo3BarC14directPropertys5Int32VvsTo : $@convention(objc_method)
// CHECK: sil [asmname "[[BYTE01]]-[Bar directProperty]"] @$sSo3BarC14directPropertys5Int32VvgTo : $@convention(objc_method)
// CHECK: sil [asmname "[[BYTE01]]-[Bar setDirectProperty2:]"] @$sSo3BarC15directProperty2s5Int32VvsTo : $@convention(objc_method)
// CHECK: sil [asmname "[[BYTE01]]-[Bar directProperty2]"] @$sSo3BarC15directProperty2s5Int32VvgTo : $@convention(objc_method)
// CHECK: sil [asmname "[[BYTE01]]-[Bar directMethod]"] [clang Bar.directMethod] @$sSo3BarC12directMethodSSSgyFTo : $@convention(objc_method)
// CHECK: sil [asmname "[[BYTE01]]-[Bar directMethod2]"] [clang Bar.directMethod2] @$sSo3BarC13directMethod2SSSgyFTo : $@convention(objc_method)
// CHECK: sil [asmname "[[BYTE01]]+[Bar directClassMethod]"] [clang Bar.directClassMethod] @$sSo3BarC17directClassMethodSSSgyFZTo : $@convention(objc_method)
// CHECK-DAG: sil [asmname "[[BYTE01]]+[Bar directClassMethod2]"] [clang Bar.directClassMethod2] @$sSo3BarC18directClassMethod2SSSgyFZTo : $@convention(objc_method)
// CHECK: sil [asmname "[[BYTE01]]-[Bar directProtocolMethod]"] [clang Bar.directProtocolMethod] @$sSo3BarC20directProtocolMethodSSSgyFTo : $@convention(objc_method)
// CHECK-LABEL: sil{{.*}}@$sSo3BarC5valueABSgs5Int32V_tcfcTO : $@convention(method) (Int32, @owned Bar) -> @owned Optional<Bar> {
// CHECK: function_ref @$sSo3BarC5valueABSgs5Int32V_tcfcTo : $@convention(objc_method)
// CHECK: } // end sil function '$sSo3BarC5valueABSgs5Int32V_tcfcTO'