Skip to content

Commit c32f128

Browse files
authored
Merge pull request #4142 from swiftwasm/main
[pull] swiftwasm from main
2 parents 5ac9f1d + baf7ef1 commit c32f128

Some content is hidden

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

41 files changed

+802
-324
lines changed

include/swift/ABI/Metadata.h

Lines changed: 215 additions & 116 deletions
Large diffs are not rendered by default.

include/swift/AST/Decl.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,21 @@ void *allocateMemoryForDecl(AllocatorTy &allocator, size_t baseSize,
10611061
class alignas(8) _GenericContext {
10621062
// Not really public. See GenericContext.
10631063
public:
1064-
llvm::PointerIntPair<GenericParamList *, 1, bool> GenericParamsAndBit;
1064+
/// The state of the generic parameters.
1065+
enum class GenericParamsState: uint8_t {
1066+
/// The stored generic parameters represent parsed generic parameters,
1067+
/// written in the source.
1068+
Parsed = 0,
1069+
/// The stored generic parameters represent generic parameters that are
1070+
/// synthesized by the type checker but were not written in the source.
1071+
TypeChecked = 1,
1072+
/// The stored generic parameters represent both the parsed and
1073+
/// type-checked generic parameters.
1074+
ParsedAndTypeChecked = 2,
1075+
};
1076+
1077+
llvm::PointerIntPair<GenericParamList *, 2, GenericParamsState>
1078+
GenericParamsAndState;
10651079

10661080
/// The trailing where clause.
10671081
///
@@ -2015,6 +2029,9 @@ class TopLevelCodeDecl : public DeclContext, public Decl {
20152029
SourceLoc getStartLoc() const;
20162030
SourceRange getSourceRange() const;
20172031

2032+
LLVM_READONLY
2033+
ASTContext &getASTContext() const { return DeclContext::getASTContext(); }
2034+
20182035
static bool classof(const Decl *D) {
20192036
return D->getKind() == DeclKind::TopLevelCode;
20202037
}

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ namespace swift {
393393
/// cases.
394394
bool EnableNonFrozenEnumExhaustivityDiagnostics = false;
395395

396+
/// Enable making top-level code support concurrency
397+
bool EnableExperimentalAsyncTopLevel = false;
398+
396399
/// Regex for the passes that should report passed and missed optimizations.
397400
///
398401
/// These are shared_ptrs so that this class remains copyable.

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ def enable_experimental_pairwise_build_block :
301301

302302
def enable_resilience : Flag<["-"], "enable-resilience">,
303303
HelpText<"Deprecated, use -enable-library-evolution instead">;
304+
305+
def enable_experimental_async_top_level :
306+
Flag<["-"], "enable-experimental-async-top-level">,
307+
HelpText<"Enable experimental concurrency in top-level code">;
304308
}
305309

306310
// HIDDEN FLAGS

include/swift/Remote/MetadataReader.h

Lines changed: 77 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class MetadataReader {
168168
using StoredPointer = typename Runtime::StoredPointer;
169169
using StoredSignedPointer = typename Runtime::StoredSignedPointer;
170170
using StoredSize = typename Runtime::StoredSize;
171+
using TargetClassMetadata =
172+
typename Runtime::template TargetClassMetadata<Runtime>;
171173

172174
private:
173175
/// The maximum number of bytes to read when reading metadata. Anything larger
@@ -502,7 +504,7 @@ class MetadataReader {
502504
if (!meta || meta->getKind() != MetadataKind::Class)
503505
return StoredPointer();
504506

505-
auto classMeta = cast<TargetClassMetadata<Runtime>>(meta);
507+
auto classMeta = cast<TargetClassMetadata>(meta);
506508
return stripSignedPointer(classMeta->Superclass);
507509
}
508510

@@ -514,39 +516,39 @@ class MetadataReader {
514516
if (!meta || meta->getKind() != MetadataKind::Class)
515517
return None;
516518

517-
#if SWIFT_OBJC_INTEROP
518-
// The following algorithm only works on the non-fragile Apple runtime.
519-
520-
// Grab the RO-data pointer. This part is not ABI.
521-
StoredPointer roDataPtr = readObjCRODataPtr(MetadataAddress);
522-
if (!roDataPtr)
523-
return None;
524-
525-
// Get the address of the InstanceStart field.
526-
auto address = roDataPtr + sizeof(uint32_t) * 1;
519+
if (Runtime::ObjCInterop) {
520+
// The following algorithm only works on the non-fragile Apple runtime.
527521

528-
unsigned start;
529-
if (!Reader->readInteger(RemoteAddress(address), &start))
530-
return None;
522+
// Grab the RO-data pointer. This part is not ABI.
523+
StoredPointer roDataPtr = readObjCRODataPtr(MetadataAddress);
524+
if (!roDataPtr)
525+
return None;
531526

532-
return start;
533-
#else
534-
// All swift class instances start with an isa pointer,
535-
// followed by the retain counts (which are the size of a long long).
536-
size_t isaAndRetainCountSize = sizeof(StoredSize) + sizeof(long long);
537-
size_t start = isaAndRetainCountSize;
527+
// Get the address of the InstanceStart field.
528+
auto address = roDataPtr + sizeof(uint32_t) * 1;
538529

539-
auto classMeta = cast<TargetClassMetadata<Runtime>>(meta);
540-
while (stripSignedPointer(classMeta->Superclass)) {
541-
classMeta = cast<TargetClassMetadata<Runtime>>(
542-
readMetadata(stripSignedPointer(classMeta->Superclass)));
530+
unsigned start;
531+
if (!Reader->readInteger(RemoteAddress(address), &start))
532+
return None;
543533

544-
// Subtract the size contribution of the isa and retain counts from
545-
// the super class.
546-
start += classMeta->InstanceSize - isaAndRetainCountSize;
534+
return start;
535+
} else {
536+
// All swift class instances start with an isa pointer,
537+
// followed by the retain counts (which are the size of a long long).
538+
size_t isaAndRetainCountSize = sizeof(StoredSize) + sizeof(long long);
539+
size_t start = isaAndRetainCountSize;
540+
541+
auto classMeta = cast<TargetClassMetadata>(meta);
542+
while (stripSignedPointer(classMeta->Superclass)) {
543+
classMeta = cast<TargetClassMetadata>(
544+
readMetadata(stripSignedPointer(classMeta->Superclass)));
545+
546+
// Subtract the size contribution of the isa and retain counts from
547+
// the super class.
548+
start += classMeta->InstanceSize - isaAndRetainCountSize;
549+
}
550+
return start;
547551
}
548-
return start;
549-
#endif
550552
}
551553

552554
/// Given a pointer to the metadata, attempt to read the value
@@ -588,7 +590,7 @@ class MetadataReader {
588590
if (!Meta)
589591
return None;
590592

591-
if (auto ClassMeta = dyn_cast<TargetClassMetadata<Runtime>>(Meta)) {
593+
if (auto ClassMeta = dyn_cast<TargetClassMetadata>(Meta)) {
592594
if (ClassMeta->isPureObjC()) {
593595
// If we can determine the Objective-C class name, this is probably an
594596
// error existential with NSError-compatible layout.
@@ -719,34 +721,36 @@ class MetadataReader {
719721
Demangler &dem,
720722
Resolver resolver) {
721723
#if SWIFT_OBJC_INTEROP
722-
// Check whether we have an Objective-C protocol.
723-
if (ProtocolAddress.isObjC()) {
724-
auto Name = readObjCProtocolName(ProtocolAddress.getObjCProtocol());
725-
StringRef NameStr(Name);
726-
727-
// If this is a Swift-defined protocol, demangle it.
728-
if (NameStr.startswith("_TtP")) {
729-
auto Demangled = dem.demangleSymbol(NameStr);
730-
if (!Demangled)
731-
return resolver.failure();
732-
733-
// FIXME: This appears in _swift_buildDemanglingForMetadata().
734-
while (Demangled->getKind() == Node::Kind::Global ||
735-
Demangled->getKind() == Node::Kind::TypeMangling ||
736-
Demangled->getKind() == Node::Kind::Type ||
737-
Demangled->getKind() == Node::Kind::ProtocolList ||
738-
Demangled->getKind() == Node::Kind::TypeList ||
739-
Demangled->getKind() == Node::Kind::Type) {
740-
if (Demangled->getNumChildren() != 1)
724+
if (Runtime::ObjCInterop) {
725+
// Check whether we have an Objective-C protocol.
726+
if (ProtocolAddress.isObjC()) {
727+
auto Name = readObjCProtocolName(ProtocolAddress.getObjCProtocol());
728+
StringRef NameStr(Name);
729+
730+
// If this is a Swift-defined protocol, demangle it.
731+
if (NameStr.startswith("_TtP")) {
732+
auto Demangled = dem.demangleSymbol(NameStr);
733+
if (!Demangled)
741734
return resolver.failure();
742-
Demangled = Demangled->getFirstChild();
735+
736+
// FIXME: This appears in _swift_buildDemanglingForMetadata().
737+
while (Demangled->getKind() == Node::Kind::Global ||
738+
Demangled->getKind() == Node::Kind::TypeMangling ||
739+
Demangled->getKind() == Node::Kind::Type ||
740+
Demangled->getKind() == Node::Kind::ProtocolList ||
741+
Demangled->getKind() == Node::Kind::TypeList ||
742+
Demangled->getKind() == Node::Kind::Type) {
743+
if (Demangled->getNumChildren() != 1)
744+
return resolver.failure();
745+
Demangled = Demangled->getFirstChild();
746+
}
747+
748+
return resolver.swiftProtocol(Demangled);
743749
}
744750

745-
return resolver.swiftProtocol(Demangled);
751+
// Otherwise, this is an imported protocol.
752+
return resolver.objcProtocol(NameStr);
746753
}
747-
748-
// Otherwise, this is an imported protocol.
749-
return resolver.objcProtocol(NameStr);
750754
}
751755
#endif
752756

@@ -1419,7 +1423,7 @@ class MetadataReader {
14191423
return readMetadataBoundsOfSuperclass(superclass);
14201424
},
14211425
[&](MetadataRef metadata) -> llvm::Optional<ClassMetadataBounds> {
1422-
auto cls = dyn_cast<TargetClassMetadata<Runtime>>(metadata);
1426+
auto cls = dyn_cast<TargetClassMetadata>(metadata);
14231427
if (!cls)
14241428
return None;
14251429

@@ -1659,6 +1663,10 @@ class MetadataReader {
16591663
return Reader->readString(RemoteAddress(namePtr), className);
16601664
}
16611665

1666+
template <typename T>
1667+
using TargetClassMetadataT =
1668+
typename Runtime::template TargetClassMetadata<T>;
1669+
16621670
MetadataRef readMetadata(StoredPointer address) {
16631671
auto cached = MetadataCache.find(address);
16641672
if (cached != MetadataCache.end())
@@ -1672,7 +1680,9 @@ class MetadataReader {
16721680

16731681
switch (getEnumeratedMetadataKind(KindValue)) {
16741682
case MetadataKind::Class:
1675-
return _readMetadata<TargetClassMetadata>(address);
1683+
1684+
return _readMetadata<TargetClassMetadataT>(address);
1685+
16761686
case MetadataKind::Enum:
16771687
return _readMetadata<TargetEnumMetadata>(address);
16781688
case MetadataKind::ErrorObject:
@@ -1779,7 +1789,7 @@ class MetadataReader {
17791789
bool skipArtificialSubclasses = false) {
17801790
switch (metadata->getKind()) {
17811791
case MetadataKind::Class: {
1782-
auto classMeta = cast<TargetClassMetadata<Runtime>>(metadata);
1792+
auto classMeta = cast<TargetClassMetadata>(metadata);
17831793
while (true) {
17841794
if (!classMeta->isTypeMetadata())
17851795
return 0;
@@ -1801,7 +1811,7 @@ class MetadataReader {
18011811
if (!superMeta)
18021812
return 0;
18031813

1804-
auto superclassMeta = dyn_cast<TargetClassMetadata<Runtime>>(superMeta);
1814+
auto superclassMeta = dyn_cast<TargetClassMetadata>(superMeta);
18051815
if (!superclassMeta)
18061816
return 0;
18071817

@@ -2692,7 +2702,7 @@ class MetadataReader {
26922702

26932703
BuiltType readNominalTypeFromClassMetadata(MetadataRef origMetadata,
26942704
bool skipArtificialSubclasses = false) {
2695-
auto classMeta = cast<TargetClassMetadata<Runtime>>(origMetadata);
2705+
auto classMeta = cast<TargetClassMetadata>(origMetadata);
26962706
if (classMeta->isTypeMetadata())
26972707
return readNominalTypeFromMetadata(origMetadata, skipArtificialSubclasses);
26982708

@@ -2715,6 +2725,10 @@ class MetadataReader {
27152725
return BuiltObjCClass;
27162726
}
27172727

2728+
using TargetClassMetadataObjCInterop =
2729+
swift::TargetClassMetadata<Runtime,
2730+
TargetAnyClassMetadataObjCInterop<Runtime>>;
2731+
27182732
/// Given that the remote process is running the non-fragile Apple runtime,
27192733
/// grab the ro-data from a class pointer.
27202734
StoredPointer readObjCRODataPtr(StoredPointer classAddress) {
@@ -2723,9 +2737,10 @@ class MetadataReader {
27232737

27242738
#if SWIFT_OBJC_INTEROP
27252739
StoredPointer dataPtr;
2726-
if (!Reader->readInteger(RemoteAddress(classAddress +
2727-
TargetClassMetadata<Runtime>::offsetToData()),
2728-
&dataPtr))
2740+
if (!Reader->readInteger(
2741+
RemoteAddress(classAddress +
2742+
TargetClassMetadataObjCInterop::offsetToData()),
2743+
&dataPtr))
27292744
return StoredPointer();
27302745

27312746
// Apply the data-pointer mask.

include/swift/SIL/SILDeclRef.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,14 @@ struct SILDeclRef {
240240
/// Produces a SILDeclRef for the entry-point of a main FileUnit.
241241
static SILDeclRef getMainFileEntryPoint(FileUnit *file);
242242

243+
/// Produces a SILDeclRef for the entry-point of an async main FileUnit.
244+
static SILDeclRef getAsyncMainFileEntryPoint(FileUnit *file);
245+
243246
bool isNull() const { return loc.isNull(); }
244247
explicit operator bool() const { return !isNull(); }
245-
248+
246249
bool hasDecl() const { return loc.is<ValueDecl *>(); }
250+
bool hasFileUnit() const { return loc.is<FileUnit *>(); }
247251
bool hasClosureExpr() const;
248252
bool hasAutoClosureExpr() const;
249253
bool hasFuncDecl() const;
@@ -260,11 +264,14 @@ struct SILDeclRef {
260264
return loc.get<FileUnit *>();
261265
}
262266

267+
/// Get ModuleDecl that contains the SILDeclRef
268+
ModuleDecl *getModuleContext() const;
269+
263270
/// Retrieves the ASTContext from the underlying AST node being stored.
264271
ASTContext &getASTContext() const;
265272

266273
llvm::Optional<AnyFunctionRef> getAnyFunctionRef() const;
267-
274+
268275
SILLocation getAsRegularLocation() const;
269276

270277
enum class ManglingKind {

lib/AST/Decl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ GenericContext::GenericContext(DeclContextKind Kind, DeclContext *Parent,
985985
: _GenericContext(), DeclContext(Kind, Parent) {
986986
if (Params) {
987987
Params->setDeclContext(this);
988-
GenericParamsAndBit.setPointerAndInt(Params, false);
988+
GenericParamsAndState.setPointerAndInt(Params, GenericParamsState::Parsed);
989989
}
990990
}
991991

@@ -1006,9 +1006,14 @@ GenericParamList *GenericContext::getGenericParams() const {
10061006
}
10071007

10081008
GenericParamList *GenericContext::getParsedGenericParams() const {
1009-
if (GenericParamsAndBit.getInt())
1009+
switch (GenericParamsAndState.getInt()) {
1010+
case GenericParamsState::Parsed:
1011+
case GenericParamsState::ParsedAndTypeChecked:
1012+
return GenericParamsAndState.getPointer();
1013+
1014+
case GenericParamsState::TypeChecked:
10101015
return nullptr;
1011-
return GenericParamsAndBit.getPointer();
1016+
}
10121017
}
10131018

10141019
bool GenericContext::hasComputedGenericSignature() const {

lib/AST/DeclContext.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,14 +1221,15 @@ bool DeclContext::isClassConstrainedProtocolExtension() const {
12211221
bool DeclContext::isAsyncContext() const {
12221222
switch (getContextKind()) {
12231223
case DeclContextKind::Initializer:
1224-
case DeclContextKind::TopLevelCodeDecl:
12251224
case DeclContextKind::EnumElementDecl:
12261225
case DeclContextKind::ExtensionDecl:
12271226
case DeclContextKind::SerializedLocal:
12281227
case DeclContextKind::Module:
1229-
case DeclContextKind::FileUnit:
12301228
case DeclContextKind::GenericTypeDecl:
12311229
return false;
1230+
case DeclContextKind::FileUnit:
1231+
case DeclContextKind::TopLevelCodeDecl:
1232+
return getASTContext().LangOpts.EnableExperimentalAsyncTopLevel;
12321233
case DeclContextKind::AbstractClosureExpr:
12331234
return cast<AbstractClosureExpr>(this)->isBodyAsync();
12341235
case DeclContextKind::AbstractFunctionDecl: {

lib/AST/NameLookup.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,7 +2597,9 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
25972597
outerParams->setDepth(depth--);
25982598

25992599
return genericParams;
2600-
} else if (auto *proto = dyn_cast<ProtocolDecl>(value)) {
2600+
}
2601+
2602+
if (auto *proto = dyn_cast<ProtocolDecl>(value)) {
26012603
// The generic parameter 'Self'.
26022604
auto &ctx = value->getASTContext();
26032605
auto selfId = ctx.Id_Self;
@@ -2615,7 +2617,8 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
26152617
SourceLoc());
26162618
return result;
26172619
}
2618-
return nullptr;
2620+
2621+
return value->getParsedGenericParams();
26192622
}
26202623

26212624
NominalTypeDecl *

0 commit comments

Comments
 (0)