Commit Graph

18 Commits

Author SHA1 Message Date
Gabor Horvath
7474a51691 [cxx-interop] Avoid copies when accessing pointee
Previously, we would get two copies, one accessing the pointee and one
when we pass the pointee as a method as the implicit self argument.
These copies are unsafe as they might introduce slicing. When
addressable paramaters features are enabled, we no longer make these
copies for the standard STL types. Custom smart pointers can replicate
this by making the lifetime dependency between the implicit object
parameter and the returned reference of operator* explicit via a
lifetime annotation.

rdar://154213694&128293252&112690482
2025-06-25 17:09:55 +01:00
Alex Lorenz
9c44e01e07 [cxx-interop][stdlib] windows - use new hash inline functions like other platforms
The PR https://github.com/swiftlang/swift/pull/77857 added windows-specific workaround for https://github.com/swiftlang/swift/issues/77856, that happened after https://github.com/swiftlang/swift/pull/77843. Unfortunately this caused a new issue on windows - https://github.com/swiftlang/swift/issues/78119. It looks like windows is suffering from a similar serialization issue as libstdc++, although its even more complex as the callAsFunction is not only a derived function from a base class, the base class although has a static call operator. In any case, the libstdc++ callAsFunction deserialization fix should align with the static operator () deserialization too, so for now make windows use the same workaround as other platforms to avoid the deserialization crash (77856).

This change was tested on i686 windows too, ensuring that IR verifier crash no longer happens
2024-12-12 23:17:17 -08:00
Egor Zhdan
9472c4ca08 [cxx-interop] Avoid "libstdc++ not found" warning when -nostdinc++ is passed
When explicitly asked not to load the C++ standard library, Swift should not emit warnings for missing libstdc++.

This fixes a compiler warning when building the Cxx module on Linux.
2024-12-05 17:51:30 +00:00
Egor Zhdan
cb486c6599 [cxx-interop] Allow creating a String from std::string_view
This adds overlay support for initializing a Swift String from C++ `std::string_view`, `std::u16string_view`, `std::u32string_view`.

rdar://138417835
2024-10-24 13:11:09 +01:00
Egor Zhdan
16e7cbeafa [cxx-interop] Modularize __msvc_bit_utils on Windows
`__msvc_bit_utils.hpp` was added in a recent version of MSVC, and it is causing build errors for SwiftCompilerSources:
```
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\numeric:598:12: error: function '_Select_countr_zero_impl<unsigned long long, (lambda at C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\numeric:598:55)>' with deduced return type cannot be used before it is defined
    return _Select_countr_zero_impl<_Common_unsigned>([=](auto _Countr_zero_impl) {
```

This change references the `__msvc_bit_utils.hpp` header from the modulemap. Since we still need to support older versions of Visual Studio that do not provide `__msvc_bit_utils.hpp`, this also teaches ClangImporter to inject an empty header file named `__msvc_bit_utils.hpp` into the system include directory, unless it already exists.

rdar://137066642
2024-10-04 13:15:20 +01:00
susmonteiro
76feea8fa7 [cxx-interop] Add std::span tests
Add compiler flag to specify C++20

Disable swift-ci linux tests
2024-06-18 14:32:49 +01:00
Egor Zhdan
d594f475ee [cxx-interop] Check for unsafe types of default arguments consistently
If the C++ type of a function parameter defines a custom copy constructor, assume that it is safe to use from Swift. This matches the heuristic that we use to detect if a C++ method is safe based on the return type.

rdar://121391798
2024-01-26 19:26:27 +00:00
Egor Zhdan
a61d9333e9 [cxx-interop] Re-export C++ stdlib in tests that require it 2024-01-16 18:07:29 +00:00
Egor Zhdan
2d0863aba2 [cxx-interop] Initial tests for std::function usage
This makes sure that `std::function` is imported consistently on supported platforms, and that it allows basic usage: calling a function with `callAsFunction`, initializing an empty function, and passing a function retrieved from C++ back to C++ as a parameter.

rdar://103979602
2024-01-11 12:02:12 +00:00
zoecarver
4ba62e1abe [cxx-interop] Add tests for std::unique_ptr. 2023-11-16 10:08:25 -08:00
Alex Lorenz
8449b21d62 [cxx-interop][windows] additional fix for indirect return in methods for MSVC ABI
Fixes https://github.com/apple/swift/pull/67685
2023-08-03 15:07:22 -07:00
Egor Zhdan
dab1cb5e12 [cxx-interop] Simplify a std::string test
`std::string` can be accessed directly from Swift, there is no need to go through a using-decl.
2023-06-13 17:22:21 +01:00
Alex Lorenz
827af150ab [interop] ensure std::pair is imported into Swift 2023-03-14 07:29:48 -07:00
Egor Zhdan
cc23f46c92 [cxx-interop] Add a test for std::optional usage from Swift 2023-02-14 17:47:08 +00:00
Alex Lorenz
9759c4dd69 [interop] add a testcase for std::set iteration in Swift 2022-12-15 14:27:40 -08:00
Egor Zhdan
e28605c0c9 [cxx-interop] Allow using std::map subscript
This fixes an error that occurred when trying to use the subscript on an instance `std::map`:
```
error: cannot assign through subscript: 'map' is immutable
```
This was happening even with a mutable `std::map` instance.

`std::map::operator[]` has two overloads:
* `T& operator[]( const Key& key )`
* `T& operator[]( Key&& key )`

The second one is imported with an `inout` parameter, and we picked it as an implementation of the subscript getter because it was the last of the two overloads to get imported.

Swift does not allow subscripts with `inout` parameters. This is checked at the AST level, and those checks do not run for synthesized Swift code. This caused Swift to produce a surprising error which actually indicated that the argument of the subscript, not the instance itself, must be mutable.

rdar://100529571
2022-10-03 11:18:56 +01:00
zoecarver
fefe39e89e [cxx-interop] Add a basic test for using std::string. 2022-02-02 14:31:04 -08:00
zoecarver
323e2b16a7 [cxx-interop] Fix linker errors when using std-vector.
Adds tests for using std-vector and some other interesting types.

This patch fixes four mis conceptions that the compiler was previously making:

	1. Implicit destructors have no side effects. (Yes, this means we were not cleaning up some objects.)
	2. Implicit destructors have bodies. (Technically they do, but the body doesn't include CallExprs that they make when lowered to IR.)
	3. Functions other than methods can be uninstantiated templates.
	4. Uninstantiated templates may have executable code. (I.e., we can never take the fast path.)

And makes sure that we visit the destructor of any VarDecl (including parameters).
2022-01-17 11:56:15 -08:00