mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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.
101 lines
3.2 KiB
Swift
101 lines
3.2 KiB
Swift
// RUN: %empty-directory(%t)
|
|
// RUN: split-file %s %t
|
|
|
|
// RUN: %target-swift-frontend %t/swiftMod.swift -module-name SwiftMod -typecheck -verify -emit-clang-header-path %t/swiftMod.h -I %t -enable-experimental-cxx-interop -Xcc -DFIRSTPASS
|
|
|
|
// RUN: %target-swift-frontend %t/swiftMod.swift -module-name SwiftMod -emit-module -o %t/SwiftMod.swiftmodule -I %t -enable-experimental-cxx-interop -Xcc -DFIRSTPASS
|
|
|
|
// RUN: %target-swift-ide-test -print-module -module-to-print=SwiftMod -module-to-print=SwiftToCxxTest -I %t -source-filename=x -enable-experimental-cxx-interop -Xcc -DSWIFT_CXX_INTEROP_HIDE_SWIFT_ERROR | %FileCheck --check-prefix=INTERFACE %s
|
|
|
|
// RUN: %target-swift-frontend -typecheck %t/swiftMod.swift -module-name SwiftMod -I %t -enable-experimental-cxx-interop -Xcc -DSWIFT_CXX_INTEROP_HIDE_SWIFT_ERROR -DSECOND_PASS -emit-sil -o - | %FileCheck --check-prefix=SIL %s
|
|
|
|
// UNSUPPORTED: OS=windows-msvc
|
|
|
|
//--- header.h
|
|
#ifndef FIRSTPASS
|
|
#include "swiftMod.h"
|
|
|
|
SwiftMod::ExposedToCxx createSwiftClassInCxx();
|
|
|
|
void passSwiftClassToCxx(SwiftMod::ExposedToCxx value);
|
|
|
|
SwiftMod::ExposedToCxx * returnClassByPtr();
|
|
SwiftMod::ExposedToCxx & returnClassByRef();
|
|
const SwiftMod::ExposedToCxx & returnClassByRefConst();
|
|
|
|
// INTERFACE: func createSwiftClassInCxx() -> ExposedToCxx
|
|
// INTERFACE: func passSwiftClassToCxx(_ value: ExposedToCxx)
|
|
// INTERFACE: func returnClassByPtr() -> OpaquePointer!
|
|
// INTERFACE-NOT: returnClassByRef
|
|
|
|
class __attribute__((swift_attr("import_owned"))) InClass {
|
|
public:
|
|
SwiftMod::ExposedToCxx value;
|
|
|
|
InClass(SwiftMod::ExposedToCxx value) : value(value) {}
|
|
|
|
inline SwiftMod::ExposedToCxx getValue() const {
|
|
return value;
|
|
}
|
|
};
|
|
|
|
// INTERFACE: struct InClass {
|
|
// INTERFACE: init(_ value: ExposedToCxx)
|
|
// INTERFACE: func getValue() -> ExposedToCxx
|
|
// INTERFACE: }
|
|
|
|
#endif
|
|
|
|
//--- module.modulemap
|
|
module SwiftToCxxTest {
|
|
header "header.h"
|
|
requires cplusplus
|
|
}
|
|
|
|
//--- swiftMod.swift
|
|
import SwiftToCxxTest
|
|
|
|
public class ExposedToCxx {
|
|
public init() {
|
|
i = 0
|
|
print("ExposedToCxx.init")
|
|
}
|
|
deinit {
|
|
print("ExposedToCxx\(i).deinit")
|
|
}
|
|
|
|
public final func testMethod() {
|
|
print("ExposedToCxx\(i).testMethod")
|
|
}
|
|
|
|
public var i: Int
|
|
}
|
|
|
|
#if SECOND_PASS
|
|
|
|
func testSwiftClassFromCxxInSwift() {
|
|
let classInstance = createSwiftClassInCxx()
|
|
passSwiftClassToCxx(classInstance)
|
|
}
|
|
|
|
testSwiftClassFromCxxInSwift()
|
|
|
|
func testSwiftClassInClass() {
|
|
let v = InClass(ExposedToCxx())
|
|
v.getValue().testMethod()
|
|
}
|
|
|
|
testSwiftClassInClass()
|
|
|
|
#endif
|
|
|
|
// SIL-LABEL: @$s8SwiftMod04testa14ClassFromCxxInA0yyF : $@convention(thin) () -> ()
|
|
// SIL: function_ref @$sSo21createSwiftClassInCxx0B3Mod09ExposedToE0CyFTo : $@convention(c) () -> @owned ExposedToCxx
|
|
// SIL: apply {{.*}} : $@convention(c) () -> @owned ExposedToCxx
|
|
// SIL: function_ref @$sSo19passSwiftClassToCxxyy0B3Mod07ExposeddE0CFTo : $@convention(c) (@in_guaranteed ExposedToCxx) -> ()
|
|
// SIL: apply {{.*}} : $@convention(c) (@in_guaranteed ExposedToCxx) -> ()
|
|
|
|
// SIL-LABEL: @$s8SwiftMod04testa7ClassInD0yyF : $@convention(thin) () -> () {
|
|
// SIL: $@convention(c) (@in_guaranteed ExposedToCxx) -> @out InClass
|
|
// SIL: $@convention(cxx_method) (@in_guaranteed InClass) -> @owned ExposedToCxx
|