Skip to content

Commit 54bb646

Browse files
Merge pull request #4729 from swiftwasm/main
[pull] swiftwasm from main
2 parents f4b117e + 88304c3 commit 54bb646

File tree

518 files changed

+14708
-4503
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

518 files changed

+14708
-4503
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,16 @@ else()
237237
COMMAND
238238
"sed"
239239
-n -e "/header/!d" -e "s/.*header/#include/" -e "w ${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp"
240-
"${CMAKE_SOURCE_DIR}/include/swift/module.modulemap"
241-
DEPENDS "${CMAKE_SOURCE_DIR}/include/swift/module.modulemap"
240+
"${CMAKE_CURRENT_SOURCE_DIR}/../include/swift/module.modulemap"
241+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../include/swift/module.modulemap"
242242
COMMENT "Generate HeaderDependencies.cpp"
243243
)
244244

245245
# step 2: build a library containing that source file. This library depends on all the included header files.
246246
# The swift modules can now depend on that target.
247247
# Note that this library is unused, i.e. not linked to anything.
248248
add_library(importedHeaderDependencies "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp")
249-
target_include_directories(importedHeaderDependencies PRIVATE "${CMAKE_SOURCE_DIR}/include/swift")
249+
target_include_directories(importedHeaderDependencies PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../include/swift")
250250

251251
if(${BOOTSTRAPPING_MODE} MATCHES "HOSTTOOLS|CROSSCOMPILE")
252252

@@ -295,6 +295,8 @@ else()
295295
set(compatibility_libs
296296
"swiftCompatibility50-${platform}"
297297
"swiftCompatibility51-${platform}"
298+
"swiftCompatibility56-${platform}"
299+
"swiftCompatibilityConcurrency-${platform}"
298300
"swiftCompatibilityDynamicReplacements-${platform}")
299301

300302
list(APPEND b0_deps ${compatibility_libs})

benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ endmacro()
106106

107107
macro(configure_sdks_darwin)
108108
set(macosx_arch "x86_64" "arm64")
109-
set(iphoneos_arch "arm64" "arm64e" "armv7")
109+
set(iphoneos_arch "arm64" "arm64e")
110110
set(appletvos_arch "arm64")
111111
set(watchos_arch "armv7k" "arm64_32")
112112

cmake/modules/DarwinSDKs.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ option(SWIFT_ENABLE_IOS32
33
TRUE)
44

55
if(SWIFT_ENABLE_IOS32)
6-
set(SUPPORTED_IOS_ARCHS "armv7;armv7s;arm64;arm64e")
7-
set(SUPPORTED_IOS_SIMULATOR_ARCHS "i386;x86_64;arm64")
6+
set(SUPPORTED_IOS_ARCHS "arm64;arm64e")
7+
set(SUPPORTED_IOS_SIMULATOR_ARCHS "x86_64;arm64")
88
else()
99
set(SUPPORTED_IOS_ARCHS "arm64;arm64e")
1010
set(SUPPORTED_IOS_SIMULATOR_ARCHS "x86_64;arm64")

cmake/modules/SwiftConfigureSDK.cmake

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ function(remove_sdk_unsupported_archs name os sdk_path architectures_var)
8686
foreach(arch ${${architectures_var}})
8787
if(sdk_supported_archs MATCHES "${arch}\n")
8888
list(APPEND architectures ${arch})
89-
elseif(arch MATCHES "^armv7(s)?$" AND os STREQUAL "iphoneos")
90-
# 32-bit iOS is not listed explicitly in SDK settings.
91-
message(STATUS "Assuming ${name} SDK at ${sdk_path} supports architecture ${arch}")
92-
list(APPEND architectures ${arch})
9389
elseif(arch STREQUAL "i386" AND os STREQUAL "iphonesimulator")
9490
# 32-bit iOS simulator is not listed explicitly in SDK settings.
9591
message(STATUS "Assuming ${name} SDK at ${sdk_path} supports architecture ${arch}")

docs/CppInteroperability/CppInteroperabilityStatus.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,25 @@ This status table describes which of the following Swift language features have
154154
| Copy and destroy semantics | Yes |
155155
| Initializers | Partially, as static `init` methods. No failable support |
156156

157+
**Class types**
158+
159+
| **Swift Language Feature** | **Implemented Experimental Support For Using It In C++** |
160+
|--------------------------------|----------------------------------------------------------|
161+
| Class reference values | Yes |
162+
| ARC semantics | Yes (C++ copy constructor,assignment operator, destructor perform ARC operations) |
163+
| Initializers | No |
164+
157165
**Methods**
158166

159167
| **Swift Language Feature** | **Implemented Experimental Support For Using It In C++** |
160168
|--------------------------------|----------------------------------------------------------|
161-
| Instance methods | Yes, for structs only |
169+
| Instance methods | Yes, for structs and classes only |
162170
| Static methods | No |
163171

164172
**Properties**
165173

166174
| **Swift Language Feature** | **Implemented Experimental Support For Using It In C++** |
167175
|--------------------------------|----------------------------------------------------------|
168-
| Getter accessors | Yes, via `get<name>`. Boolean properties that start with `is` or `has` are remapped directly to a getter method using their original name. For structs only |
169-
| Setter accessors | Yes, via `set<name>`. For structs only |
176+
| Getter accessors | Yes, via `get<name>`. Boolean properties that start with `is` or `has` are remapped directly to a getter method using their original name. For structs and classes only |
177+
| Setter accessors | Yes, via `set<name>`. For structs and classes only |
170178
| Mutation accessors | No |
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Swift Type Representation In C++
2+
3+
This document describes in details how Swift types are represented in C++.
4+
It also covers related topics, like debug info representation for Swift types in
5+
C++.
6+
7+
## Type Categories
8+
9+
### Value Types
10+
11+
1) Primitive Swift types like `Int`, `Float` , `OpaquePointer`, `UnsafePointer<int>?` are mapped to primitive C++ types. `int` , `float`, `void *`, int `* _Nullable` .
12+
13+
* Debug info: Does C++ debug info suffices?
14+
15+
2) Non-resilient fixed-layout Swift value type, e.g. `String` is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:
16+
17+
```c++
18+
class swift::String {
19+
...
20+
alignas(8) char buffer[24]; // Swift value is stored here.
21+
}
22+
```
23+
24+
* Debug info: ...
25+
26+
27+
3) Resilient (or opaque layout) inline-allocated Swift value type small enough to fit into inline buffer. e.g `URL` is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:
28+
29+
```c++
30+
class Foundation::URL {
31+
...
32+
union {
33+
alignas(8) char buffer[8]; // Swift value is stored here.
34+
void *resilientPtr;
35+
};
36+
};
37+
```
38+
39+
* Debug info: ...
40+
41+
42+
4) Resilient (or opaque layout) boxed Swift value , e.g. `SHA256` is mapped to a C++ class that stores the value boxed up on the heap, e.g.:
43+
44+
```c++
45+
class CryptoKit::SHA256 {
46+
...
47+
union {
48+
alignas(8) char buffer[8];
49+
void *resilientPtr; // Swift value is stored on the heap pointed by this pointer.
50+
};
51+
};
52+
```
53+
54+
* Debug info: ...
55+
56+
57+
5) Generic non-resilient fixed-layout Swift value type, e.g. `Array<Int>`, `String?`, is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:
58+
59+
```c++
60+
class swift::Array<swift::Int> {
61+
...
62+
alignas(8) char buffer[8]; // Swift value is stored here.
63+
}
64+
```
65+
66+
* Debug info: ...
67+
68+
69+
6) Generic opaque-layout / resilient / opaque-layout template type params Swift value type, e.g. SHA256`?`, is mapped to a C++ class that stores the value boxed up on the heap, e.g.:
70+
71+
```c++
72+
class swift::Optional<CryptoKit::SHA256> {
73+
...
74+
union {
75+
alignas(8) char buffer[8];
76+
void *resilientPtr; // Swift value is stored on the heap pointed by this pointer.
77+
};
78+
}
79+
```
80+
81+
* Debug info: ...
82+
83+
### Class Types
84+
85+
Class type is mapped to a C++ class that has a pointer to the underlying Swift instance in the base class:
86+
87+
```c++
88+
class BaseClass {
89+
private:
90+
void *_opaquePointer; // Swift class instance pointer is stored here.
91+
};
92+
class Vehicle: public BaseClass {
93+
public:
94+
}
95+
```
96+
97+
* Debug info: ...
98+
99+
### Existential Types
100+
101+
Error type is mapped to a specific C++ `swift::Error` class that stores the pointer to the error:
102+
103+
```c++
104+
class Error {
105+
private:
106+
void *_opaquePointer; // Swift error instance pointer is stored here.:
107+
};
108+
```
109+
110+
* Debug info: ...
111+
112+
113+
Existential type. e.g. `any Hashable` maps to a C++ class that stores the opaque existential value (`swift::any<swift::Hashable>`):
114+
115+
```c++
116+
class swift::OpaqueExistential {
117+
// approximate layout.
118+
alignas(8) char buffer[8*5]; // opaque existential is stored here (inline or boxed by Swift)
119+
};
120+
class swift::any<swift::Hashable>: public swift::OpaqueExistential {
121+
};
122+
```
123+
124+
* Debug info: ...

docs/CppInteroperability/UserGuide-CallingSwiftFromC++.md

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A Swift library author might want to expose their interface to C++, to allow a C
77
**NOTE:** This document does not go over the following Swift language features yet:
88

99
* Closures
10-
* Class types & inheritance
10+
* overriden methods/properties in classes
1111
* Existential types (any P)
1212
* Nested types
1313
* Operators
@@ -570,6 +570,102 @@ Barcode normalizeBarcode(Barcode barcode) {
570570
}
571571
```
572572

573+
## Using Swift Class Types
574+
575+
Swift class types that are usable from C++ are available in their corresponding module namespace. They’re bridged over as a C++ class that stores a referenced counted pointer inside of it. Its initializers, methods and properties are exposed as members of the C++ class.
576+
577+
### Reference counting in C++
578+
579+
C++ class types that represent Swift classes perform automatic
580+
reference counting (ARC) operations when the C++ value that represents
581+
the reference to the Swift class is copied and destroyed.
582+
583+
For example, the following Swift class:
584+
585+
```swift
586+
// Swift module 'People'.
587+
class Person {
588+
let name: String
589+
init(name: String) {
590+
self.name = name
591+
print("\(name) is being initialized")
592+
}
593+
deinit {
594+
print("\(name) is being deinitialized")
595+
}
596+
}
597+
598+
func createRandomPerson() -> Person {
599+
return Person(name: getRandomName())
600+
}
601+
```
602+
603+
Can be used from C++ with reference counting performed
604+
automatically:
605+
606+
```c++
607+
#include "People-Swift.h"
608+
609+
using namespace People;
610+
611+
void doSomething(Person p) {
612+
...
613+
}
614+
615+
void createAndUsePerson() {
616+
Person p = createRandomPerson();
617+
618+
doSomething(p); // 'p' is copied. Person referenced by p is referenced twice.
619+
// Destructor for copy of 'p' is called. Person referenced by p is referenced once.
620+
621+
// Destructor for 'p' gets called here. Person referenced by p is deallocated.
622+
}
623+
```
624+
625+
The Swift `Person` class instance that C++ variable `p` referenced gets deallocated
626+
at the end of `createAndUsePerson` as the two C++ values that referenced it
627+
inside of `createAndUsePerson` were destroyed.
628+
629+
### Class inheritance
630+
631+
A Swift class that inherits from another class is bridged to C++ with that inheritance
632+
relationship preserved in the C++ class hierarchy generated for these Swift classes. For example, given the following two Swift classes:
633+
634+
```swift
635+
// Swift module 'Transport'
636+
public class Vehicle {
637+
}
638+
public final class Bicycle: Vehicle {
639+
}
640+
```
641+
642+
Get a corresponding C++ class hierachy in C++:
643+
644+
```c++
645+
class Vehicle { ... };
646+
class Bicycle final : public Vehicle {};
647+
```
648+
649+
This allows C++ code to implicitly cast derived class instances to base class reference values, like in the example below:
650+
651+
```c++
652+
#include "Transport-Swift.h"
653+
654+
using namespace Transport;
655+
656+
void doSomethingWithVehicle(Transport::Vehicle vehicle) {
657+
...
658+
}
659+
660+
void useBicycle() {
661+
auto bike = Bicycle::init();
662+
doSomethingWithVehicle(bike);
663+
}
664+
```
665+
666+
Swift classes that are marked as `final` are also marked `final` in C++.
667+
Swift classes that are not marked as `final` should not be derived from in C++.
668+
573669
## Accessing Properties In C++
574670

575671
Swift allows structures and classes to define stored and computed properties. Stored properties store constant and variable values as part of an instance, whereas computed properties calculate (rather than store) a value. The stored and the computed properties from Swift types are bridged over as getter `get...` and setter `set...` methods in C++. Setter methods are not marked as `const` and should only be invoked on non `const` instances of the bridged types.

0 commit comments

Comments
 (0)