Skip to content

Commit 3554800

Browse files
Merge pull request #4325 from swiftwasm/main
[pull] swiftwasm from main
2 parents 82c4553 + 910c2a4 commit 3554800

File tree

92 files changed

+2497
-448
lines changed

Some content is hidden

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

92 files changed

+2497
-448
lines changed

docs/CppInteropability/GettingStartedWithC++Interop.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Getting started with C++ Interoperability
22

3-
This document is desgined to get you started with bidirectional API-level interoperability between Swift and C++.
3+
This document is designed to get you started with bidirectional API-level interoperability between Swift and C++.
44

55
## Table of Contents
66

@@ -35,7 +35,7 @@ Add the C++ module to the include path and enable C++ interop:
3535
- Navigate to your project directory
3636
- In `Project` navigate to `Build Settings` -> `Swift Compiler`
3737
- Under `Custom Flags` -> `Other Swift Flags` add`-Xfrontend -enable-cxx-interop`
38-
- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/Cxx`).
38+
- Under `Search Paths` -> `Import Paths` add your search path to the C++ module (i.e, `./ProjectName/Cxx`). Repeat this step in `Other Swift Flags`
3939

4040
```
4141
//Add to Other Swift Flags and Import Paths respectively
@@ -46,15 +46,14 @@ Add the C++ module to the include path and enable C++ interop:
4646
- This should now allow your to import your C++ Module into any `.swift` file.
4747

4848
```
49-
//In ViewController.swift
50-
import UIKit
49+
//In ContentView.swift
50+
import SwiftUI
5151
import Cxx
5252
53-
class ViewController: UIViewController {
54-
override func viewDidLoad() {
55-
super.viewDidLoad()
56-
let result = cxxFunction(7)
57-
print(result)
53+
struct ContentView: View {
54+
var body: some View {
55+
Text("Cxx function result: \(cxxFunction(7))")
56+
.padding()
5857
}
5958
}
6059
```
@@ -86,8 +85,8 @@ int cxxFunction(int n);
8685
After creating your Swift package project, follow the steps [Creating a Module to contain your C++ source code](#creating-a-module-to-contain-your-c-source-code) in your `Source` directory
8786

8887
- In your Package Manifest, you need to configure the Swift target's dependencies and compiler flags
89-
- In this example the name of the package is `Cxx_Interop`
90-
- Swift code will be in `Sources/Cxx_Interop` called `main.swift`
88+
- In this example the name of the package is `CxxInterop`
89+
- Swift code will be in `Sources/CxxInterop` called `main.swift`
9190
- C++ source code follows the example shown in [Creating a Module to contain your C++ source code](#creating-a-module-to-contain-your-c-source-code)
9291
- Under targets, add the name of your C++ module and the directory containing the Swift code as a target.
9392
- In the target defining your Swift target, add a`dependencies` to the C++ Module, the `path`, `source`, and `swiftSettings` with `unsafeFlags` with the source to the C++ Module, and enable `-enable-cxx-interop`
@@ -98,25 +97,25 @@ After creating your Swift package project, follow the steps [Creating a Module t
9897
import PackageDescription
9998
10099
let package = Package(
101-
name: "Cxx_Interop",
100+
name: "CxxInterop",
102101
platforms: [.macOS(.v12)],
103102
products: [
104103
.library(
105104
name: "Cxx",
106105
targets: ["Cxx"]),
107106
.library(
108-
name: "Cxx_Interop",
109-
targets: ["Cxx_Interop"]),
107+
name: "CxxInterop",
108+
targets: ["CxxInterop"]),
110109
],
111110
targets: [
112111
.target(
113112
name: "Cxx",
114113
dependencies: []
115114
),
116115
.executableTarget(
117-
name: "Cxx_Interop",
116+
name: "CxxInterop",
118117
dependencies: ["Cxx"],
119-
path: "./Sources/Cxx_Interop",
118+
path: "./Sources/CxxInterop",
120119
sources: [ "main.swift" ],
121120
swiftSettings: [.unsafeFlags([
122121
"-I", "Sources/Cxx",
@@ -135,14 +134,14 @@ let package = Package(
135134
136135
import Cxx
137136
138-
public struct Cxx_Interop {
137+
public struct CxxInterop {
139138
140139
public func callCxxFunction(n: Int32) -> Int32 {
141140
return cxxFunction(n: n)
142141
}
143142
}
144143
145-
print(Cxx_Interop().callCxxFunction(n: 7))
144+
print(CxxInterop().callCxxFunction(n: 7))
146145
//outputs: 7
147146
148147
```
@@ -161,7 +160,7 @@ After creating your project follow the steps [Creating a Module to contain your
161160
162161
cmake_minimum_required(VERSION 3.18)
163162
164-
project(Cxx_Interop LANGUAGES CXX Swift)
163+
project(CxxInterop LANGUAGES CXX Swift)
165164
166165
set(CMAKE_CXX_STANDARD 11)
167166
set(CMAKE_CXX_STANDARD_REQUIRED YES)
@@ -176,10 +175,10 @@ target_compile_options(cxx-support PRIVATE
176175
target_include_directories(cxx-support PUBLIC
177176
${CMAKE_SOURCE_DIR}/Sources/Cxx)
178177
179-
add_executable(Cxx_Interop ./Sources/Cxx_Interop/main.swift)
180-
target_compile_options(Cxx_Interop PRIVATE
178+
add_executable(CxxInterop ./Sources/CxxInterop/main.swift)
179+
target_compile_options(CxxInterop PRIVATE
181180
"SHELL:-Xfrontend -enable-cxx-interop"
182-
target_link_libraries(Cxx_Interop PRIVATE cxx-support)
181+
target_link_libraries(CxxInterop PRIVATE cxx-support)
183182
184183
```
185184

@@ -188,14 +187,14 @@ target_link_libraries(Cxx_Interop PRIVATE cxx-support)
188187
189188
import Cxx
190189
191-
public struct Cxx_Interop {
190+
public struct CxxInterop {
192191
public static func main() {
193192
let result = cxxFunction(7)
194193
print(result)
195194
}
196195
}
197196
198-
Cxx_Interop.main()
197+
CxxInterop.main()
199198
200199
```
201200

include/swift/ABI/TypeIdentity.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SWIFT_ABI_TYPEIDENTITY_H
2020

2121
#include "swift/Basic/LLVM.h"
22+
#include <llvm/ADT/Optional.h>
2223
#include <llvm/ADT/StringRef.h>
2324

2425
namespace swift {

include/swift/AST/ActorIsolation.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bool isLetAccessibleAnywhere(const ModuleDecl *fromModule, VarDecl *let);
4747
/// the actors with which it can interact.
4848
class ActorIsolation {
4949
public:
50-
enum Kind {
50+
enum Kind : uint8_t {
5151
/// The actor isolation has not been specified. It is assumed to be
5252
/// unsafe to interact with this declaration from any actor.
5353
Unspecified = 0,
@@ -69,18 +69,19 @@ class ActorIsolation {
6969
};
7070

7171
private:
72-
Kind kind;
7372
union {
7473
NominalTypeDecl *actor;
7574
Type globalActor;
7675
void *pointer;
7776
};
77+
uint8_t kind : 3;
78+
uint8_t isolatedByPreconcurrency : 1;
7879

7980
ActorIsolation(Kind kind, NominalTypeDecl *actor)
80-
: kind(kind), actor(actor) { }
81+
: actor(actor), kind(kind), isolatedByPreconcurrency(false) { }
8182

8283
ActorIsolation(Kind kind, Type globalActor)
83-
: kind(kind), globalActor(globalActor) { }
84+
: globalActor(globalActor), kind(kind), isolatedByPreconcurrency(false) { }
8485

8586
public:
8687
static ActorIsolation forUnspecified() {
@@ -100,7 +101,7 @@ class ActorIsolation {
100101
unsafe ? GlobalActorUnsafe : GlobalActor, globalActor);
101102
}
102103

103-
Kind getKind() const { return kind; }
104+
Kind getKind() const { return (Kind)kind; }
104105

105106
operator Kind() const { return getKind(); }
106107

@@ -122,6 +123,16 @@ class ActorIsolation {
122123
return globalActor;
123124
}
124125

126+
bool preconcurrency() const {
127+
return isolatedByPreconcurrency;
128+
}
129+
130+
ActorIsolation withPreconcurrency(bool value) const {
131+
auto copy = *this;
132+
copy.isolatedByPreconcurrency = value;
133+
return copy;
134+
}
135+
125136
/// Determine whether this isolation will require substitution to be
126137
/// evaluated.
127138
bool requiresSubstitution() const;
@@ -134,10 +145,10 @@ class ActorIsolation {
134145
if (lhs.isGlobalActor() && rhs.isGlobalActor())
135146
return areTypesEqual(lhs.globalActor, rhs.globalActor);
136147

137-
if (lhs.kind != rhs.kind)
148+
if (lhs.getKind() != rhs.getKind())
138149
return false;
139150

140-
switch (lhs.kind) {
151+
switch (lhs.getKind()) {
141152
case Independent:
142153
case Unspecified:
143154
return true;

include/swift/AST/Decl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5930,6 +5930,14 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
59305930
DeclContext *Parent,
59315931
GenericParamList *GenericParams);
59325932

5933+
static SubscriptDecl *create(ASTContext &Context, DeclName Name,
5934+
SourceLoc StaticLoc,
5935+
StaticSpellingKind StaticSpelling,
5936+
SourceLoc SubscriptLoc, ParameterList *Indices,
5937+
SourceLoc ArrowLoc, Type ElementTy,
5938+
DeclContext *Parent,
5939+
GenericParamList *GenericParams);
5940+
59335941
static SubscriptDecl *createImported(ASTContext &Context, DeclName Name,
59345942
SourceLoc SubscriptLoc,
59355943
ParameterList *Indices,

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4500,9 +4500,10 @@ NOTE(note_distributed_actor_system_conformance_missing_adhoc_requirement,none,
45004500
ERROR(override_implicit_unowned_executor,none,
45014501
"cannot override an actor's 'unownedExecutor' property that wasn't "
45024502
"explicitly defined", ())
4503-
ERROR(actor_isolated_from_deinit,none,
4504-
"actor-isolated %0 %1 can not be referenced from a non-isolated deinit",
4505-
(DescriptiveDeclKind, DeclName))
4503+
ERROR(actor_isolated_from_decl,none,
4504+
"actor-isolated %0 %1 can not be referenced from a non-isolated "
4505+
"%select{deinit|autoclosure|closure}2",
4506+
(DescriptiveDeclKind, DeclName, unsigned))
45064507
ERROR(actor_isolated_non_self_reference,none,
45074508
"actor-isolated %0 %1 can not be "
45084509
"%select{referenced|mutated|used 'inout'}2 "

include/swift/AST/Expr.h

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
255255
Kind : 2
256256
);
257257

258-
SWIFT_INLINE_BITFIELD(ClosureExpr, AbstractClosureExpr, 1+1+1,
258+
SWIFT_INLINE_BITFIELD(ClosureExpr, AbstractClosureExpr, 1+1+1+1,
259259
/// True if closure parameters were synthesized from anonymous closure
260260
/// variables.
261261
HasAnonymousClosureVars : 1,
@@ -266,7 +266,11 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
266266

267267
/// True if this @Sendable async closure parameter should implicitly
268268
/// inherit the actor context from where it was formed.
269-
InheritActorContext : 1
269+
InheritActorContext : 1,
270+
271+
/// True if this closure's actor isolation behavior was determined by an
272+
/// \c \@preconcurrency declaration.
273+
IsolatedByPreconcurrency : 1
270274
);
271275

272276
SWIFT_INLINE_BITFIELD_FULL(BindOptionalExpr, Expr, 16,
@@ -3537,40 +3541,46 @@ class ClosureActorIsolation {
35373541
};
35383542

35393543
private:
3540-
/// The actor to which this closure is isolated.
3544+
/// The actor to which this closure is isolated, plus a bit indicating
3545+
/// whether the isolation was imposed by a preconcurrency declaration.
35413546
///
3542-
/// There are three possible states:
3547+
/// There are three possible states for the pointer:
35433548
/// - NULL: The closure is independent of any actor.
35443549
/// - VarDecl*: The 'self' variable for the actor instance to which
35453550
/// this closure is isolated. It will always have a type that conforms
35463551
/// to the \c Actor protocol.
35473552
/// - Type: The type of the global actor on which
3548-
llvm::PointerUnion<VarDecl *, Type> storage;
3553+
llvm::PointerIntPair<llvm::PointerUnion<VarDecl *, Type>, 1, bool> storage;
35493554

3550-
ClosureActorIsolation(VarDecl *selfDecl) : storage(selfDecl) { }
3551-
ClosureActorIsolation(Type globalActorType) : storage(globalActorType) { }
3555+
ClosureActorIsolation(VarDecl *selfDecl, bool preconcurrency)
3556+
: storage(selfDecl, preconcurrency) { }
3557+
ClosureActorIsolation(Type globalActorType, bool preconcurrency)
3558+
: storage(globalActorType, preconcurrency) { }
35523559

35533560
public:
3554-
ClosureActorIsolation() : storage() { }
3561+
ClosureActorIsolation(bool preconcurrency = false)
3562+
: storage(nullptr, preconcurrency) { }
35553563

3556-
static ClosureActorIsolation forIndependent() {
3557-
return ClosureActorIsolation();
3564+
static ClosureActorIsolation forIndependent(bool preconcurrency) {
3565+
return ClosureActorIsolation(preconcurrency);
35583566
}
35593567

3560-
static ClosureActorIsolation forActorInstance(VarDecl *selfDecl) {
3561-
return ClosureActorIsolation(selfDecl);
3568+
static ClosureActorIsolation forActorInstance(VarDecl *selfDecl,
3569+
bool preconcurrency) {
3570+
return ClosureActorIsolation(selfDecl, preconcurrency);
35623571
}
35633572

3564-
static ClosureActorIsolation forGlobalActor(Type globalActorType) {
3565-
return ClosureActorIsolation(globalActorType);
3573+
static ClosureActorIsolation forGlobalActor(Type globalActorType,
3574+
bool preconcurrency) {
3575+
return ClosureActorIsolation(globalActorType, preconcurrency);
35663576
}
35673577

35683578
/// Determine the kind of isolation.
35693579
Kind getKind() const {
3570-
if (storage.isNull())
3580+
if (storage.getPointer().isNull())
35713581
return Kind::Independent;
35723582

3573-
if (storage.is<VarDecl *>())
3583+
if (storage.getPointer().is<VarDecl *>())
35743584
return Kind::ActorInstance;
35753585

35763586
return Kind::GlobalActor;
@@ -3587,11 +3597,15 @@ class ClosureActorIsolation {
35873597
}
35883598

35893599
VarDecl *getActorInstance() const {
3590-
return storage.dyn_cast<VarDecl *>();
3600+
return storage.getPointer().dyn_cast<VarDecl *>();
35913601
}
35923602

35933603
Type getGlobalActor() const {
3594-
return storage.dyn_cast<Type>();
3604+
return storage.getPointer().dyn_cast<Type>();
3605+
}
3606+
3607+
bool preconcurrency() const {
3608+
return storage.getInt();
35953609
}
35963610
};
35973611

@@ -3864,6 +3878,16 @@ class ClosureExpr : public AbstractClosureExpr {
38643878
Bits.ClosureExpr.InheritActorContext = value;
38653879
}
38663880

3881+
/// Whether the closure's concurrency behavior was determined by an
3882+
/// \c \@preconcurrency declaration.
3883+
bool isIsolatedByPreconcurrency() const {
3884+
return Bits.ClosureExpr.IsolatedByPreconcurrency;
3885+
}
3886+
3887+
void setIsolatedByPreconcurrency(bool value = true) {
3888+
Bits.ClosureExpr.IsolatedByPreconcurrency = value;
3889+
}
3890+
38673891
/// Determine whether this closure expression has an
38683892
/// explicitly-specified result type.
38693893
bool hasExplicitResultType() const { return ArrowLoc.isValid(); }

include/swift/AST/Module.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,9 @@ inline SourceLoc extractNearestSourceLoc(const ModuleDecl *mod) {
939939
return extractNearestSourceLoc(static_cast<const Decl *>(mod));
940940
}
941941

942+
/// Collects modules that this module imports via `@_exported import`.
943+
void collectParsedExportedImports(const ModuleDecl *M, SmallPtrSetImpl<ModuleDecl *> &Imports);
944+
942945
} // end namespace swift
943946

944947
#endif

include/swift/Basic/DiverseStack.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,7 @@ class DiverseValueBuffer {
395395
public:
396396
DiverseValueBuffer(const T &value) {
397397
size_t size = value.allocated_size();
398-
data.reserve(size);
399-
data.set_size(size);
398+
data.resize_for_overwrite(size);
400399
memcpy(data.data(), reinterpret_cast<const void *>(&value), size);
401400
}
402401

include/swift/Basic/Fingerprint.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/Basic/StableHasher.h"
1717
#include "llvm/ADT/Hashing.h"
18+
#include "llvm/ADT/Optional.h"
1819
#include "llvm/ADT/SmallString.h"
1920
#include "llvm/ADT/StringRef.h"
2021

include/swift/Basic/SourceLoc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/Basic/Debug.h"
2121
#include "swift/Basic/LLVM.h"
2222
#include "llvm/ADT/DenseMapInfo.h"
23+
#include "llvm/ADT/Hashing.h"
2324
#include "llvm/ADT/StringRef.h"
2425
#include "llvm/Support/SMLoc.h"
2526
#include <functional>

0 commit comments

Comments
 (0)