Skip to content

Commit 546a58a

Browse files
Merge remote-tracking branch 'apple/main' into katei/merge-main-2020-10-12
2 parents af2cfc5 + 23605e1 commit 546a58a

File tree

181 files changed

+5328
-1905
lines changed

Some content is hidden

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

181 files changed

+5328
-1905
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ Function Specializations
995995
::
996996

997997
specialization ::= type '_' type* 'Tg' SPEC-INFO // Generic re-abstracted specialization
998+
specialization ::= type '_' type* 'Ts' SPEC-INFO // Generic re-abstracted prespecialization
998999
specialization ::= type '_' type* 'TG' SPEC-INFO // Generic not re-abstracted specialization
9991000
specialization ::= type '_' type* 'Ti' SPEC-INFO // Inlined function with generic substitutions.
10001001

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ documentation, please create a thread on the Swift forums under the
120120
lazy type-checking and efficient caching.
121121
- [Literals.md](/docs/Literals.md):
122122
Describes type-checking and inference specifically for literals.
123-
- [Serialization.rst](/docs/Serialization.rst):
123+
- [Serialization.md](/docs/Serialization.md):
124124
Gives an overview of the LLVM bitcode format used for swiftmodules.
125125
- [StableBitcode.md](/docs/StableBitcode.md):
126126
Describes how to maintain compatibility when changing the serialization

docs/Serialization.rst renamed to docs/Serialization.md

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
:orphan:
2-
3-
=================================
4-
Swift Binary Serialization Format
5-
=================================
1+
# Swift Binary Serialization Format
62

73
The fundamental unit of distribution for Swift code is a *module.* A module
84
contains declarations as an interface for clients to write code against. It may
@@ -33,10 +29,9 @@ tied to the compiler internals to be useful for this purpose, and it is likely
3329
we'll invent a new format instead.
3430

3531

36-
Why LLVM bitcode?
37-
=================
32+
## Why LLVM bitcode?
3833

39-
The `LLVM bitstream <http://llvm.org/docs/BitCodeFormat.html>`_ format was
34+
The [LLVM bitstream](http://llvm.org/docs/BitCodeFormat.html) format was
4035
invented as a container format for LLVM IR. It is a binary format supporting
4136
two basic structures: *blocks,* which define regions of the file, and
4237
*records,* which contain data fields that can be up to 64 bits. It has a few
@@ -60,10 +55,9 @@ have most of these properties as well. But we're already linking against
6055
LLVM...might as well use it!
6156

6257

63-
Versioning
64-
==========
58+
## Versioning
6559

66-
.. warning::
60+
#### _Warning_
6761

6862
This section is relevant to any forward-compatible format used for a
6963
library's public interface. However, as mentioned above this may not be
@@ -101,13 +95,11 @@ requires extra work on the compiler's part to detect which features are in use;
10195
a simpler implementation would just use the latest version number supported:
10296
1.9.
10397

104-
*This versioning scheme was inspired by* `Semantic Versioning
105-
<http://semver.org>`_. *However, it is not compatible with Semantic Versioning
98+
*This versioning scheme was inspired by* [Semantic Versioning](http://semver.org). *However, it is not compatible with Semantic Versioning
10699
because it promises* forward-compatibility *rather than* backward-compatibility.
107100

108101

109-
A High-Level Tour of the Current Module Format
110-
==============================================
102+
## A High-Level Tour of the Current Module Format
111103

112104
Every serialized module is represented as a single block called the "module
113105
block". The module block is made up of several other block kinds, largely for
@@ -132,7 +124,7 @@ organizational purposes.
132124
- The **SIL block** contains SIL-level implementations that can be imported
133125
into a client's SILModule context. In most cases this is just a performance
134126
concern, but sometimes it affects language semantics as well, as in the case
135-
of ``@_transparent``. The SIL block precedes the AST block because it affects
127+
of `@_transparent`. The SIL block precedes the AST block because it affects
136128
which AST nodes get serialized.
137129

138130
- The **SIL index block** contains tables for accessing various SIL entities by
@@ -145,9 +137,7 @@ organizational purposes.
145137
Nodes are accessed by a file-unique "DeclIDs" (also covering DeclContexts)
146138
and "TypeIDs"; the two sets of IDs use separate numbering schemes.
147139

148-
.. note::
149-
150-
The AST block is currently referred to as the "decls block" in the source.
140+
_note_: The AST block is currently referred to as the "decls block" in the source.
151141

152142
- The **identifier block** contains a single blob of strings. This is intended
153143
for Identifiers---strings uniqued by the ASTContext---but can in theory
@@ -160,13 +150,11 @@ organizational purposes.
160150
top-level declarations.
161151

162152

163-
SIL
164-
===
153+
## SIL
165154

166155
[to be written]
167156

168157

169-
Cross-reference resilience
170-
==========================
158+
## Cross-reference resilience
171159

172160
[to be written]

include/swift/AST/Attr.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,11 @@ SIMPLE_DECL_ATTR(globalActor, GlobalActor,
584584
APIStableToAdd | APIBreakingToRemove,
585585
104)
586586

587+
SIMPLE_DECL_ATTR(_specializeExtension, SpecializeExtension,
588+
OnExtension | UserInaccessible |
589+
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
590+
105)
591+
587592
#undef TYPE_ATTR
588593
#undef DECL_ATTR_ALIAS
589594
#undef CONTEXTUAL_DECL_ATTR_ALIAS

include/swift/AST/Attr.h

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,12 @@ class SynthesizedProtocolAttr : public DeclAttribute {
14081408

14091409
/// The @_specialize attribute, which forces specialization on the specified
14101410
/// type list.
1411-
class SpecializeAttr : public DeclAttribute {
1411+
class SpecializeAttr final
1412+
: public DeclAttribute,
1413+
private llvm::TrailingObjects<SpecializeAttr, Identifier> {
1414+
friend class SpecializeAttrTargetDeclRequest;
1415+
friend TrailingObjects;
1416+
14121417
public:
14131418
// NOTE: When adding new kinds, you must update the inline bitfield macro.
14141419
enum class SpecializationKind {
@@ -1420,18 +1425,48 @@ class SpecializeAttr : public DeclAttribute {
14201425
TrailingWhereClause *trailingWhereClause;
14211426
GenericSignature specializedSignature;
14221427

1428+
DeclNameRef targetFunctionName;
1429+
LazyMemberLoader *resolver = nullptr;
1430+
uint64_t resolverContextData;
1431+
size_t numSPIGroups;
1432+
14231433
SpecializeAttr(SourceLoc atLoc, SourceRange Range,
14241434
TrailingWhereClause *clause, bool exported,
1425-
SpecializationKind kind,
1426-
GenericSignature specializedSignature);
1435+
SpecializationKind kind, GenericSignature specializedSignature,
1436+
DeclNameRef targetFunctionName,
1437+
ArrayRef<Identifier> spiGroups);
14271438

14281439
public:
14291440
static SpecializeAttr *create(ASTContext &Ctx, SourceLoc atLoc,
14301441
SourceRange Range, TrailingWhereClause *clause,
14311442
bool exported, SpecializationKind kind,
1443+
DeclNameRef targetFunctionName,
1444+
ArrayRef<Identifier> spiGroups,
14321445
GenericSignature specializedSignature
14331446
= nullptr);
14341447

1448+
static SpecializeAttr *create(ASTContext &ctx, bool exported,
1449+
SpecializationKind kind,
1450+
ArrayRef<Identifier> spiGroups,
1451+
GenericSignature specializedSignature,
1452+
DeclNameRef replacedFunction);
1453+
1454+
static SpecializeAttr *create(ASTContext &ctx, bool exported,
1455+
SpecializationKind kind,
1456+
ArrayRef<Identifier> spiGroups,
1457+
GenericSignature specializedSignature,
1458+
DeclNameRef replacedFunction,
1459+
LazyMemberLoader *resolver, uint64_t data);
1460+
1461+
/// Name of SPIs declared by the attribute.
1462+
///
1463+
/// Note: A single SPI name per attribute is currently supported but this
1464+
/// may change with the syntax change.
1465+
ArrayRef<Identifier> getSPIGroups() const {
1466+
return { this->template getTrailingObjects<Identifier>(),
1467+
numSPIGroups };
1468+
}
1469+
14351470
TrailingWhereClause *getTrailingWhereClause() const;
14361471

14371472
GenericSignature getSpecializedSignature() const {
@@ -1458,6 +1493,13 @@ class SpecializeAttr : public DeclAttribute {
14581493
return getSpecializationKind() == SpecializationKind::Partial;
14591494
}
14601495

1496+
DeclNameRef getTargetFunctionName() const {
1497+
return targetFunctionName;
1498+
}
1499+
1500+
/// \p forDecl is the value decl that the attribute belongs to.
1501+
ValueDecl *getTargetFunctionDecl(const ValueDecl *forDecl) const;
1502+
14611503
static bool classof(const DeclAttribute *DA) {
14621504
return DA->getKind() == DAK_Specialize;
14631505
}

include/swift/AST/Decl.h

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,8 @@ class ValueDecl : public Decl {
21872187
/// implementations for the requirements of a public protocol, even when
21882188
/// the default implementations are not visible to name lookup.
21892189
bool isAccessibleFrom(const DeclContext *DC,
2190-
bool forConformance = false) const;
2190+
bool forConformance = false,
2191+
bool includeInlineable = false) const;
21912192

21922193
/// Returns whether this declaration should be treated as \c open from
21932194
/// \p useDC. This is very similar to #getFormalAccess, but takes
@@ -3741,62 +3742,79 @@ class ClassDecl final : public NominalTypeDecl {
37413742
}
37423743
};
37433744

3745+
/// A convenience wrapper around the \c SelfReferencePosition::Kind enum.
3746+
struct SelfReferencePosition final {
3747+
enum Kind : uint8_t { None, Covariant, Contravariant, Invariant };
37443748

3745-
/// Describes whether a requirement refers to 'Self', for use in the
3746-
/// is-inheritable and is-available-existential checks.
3747-
struct SelfReferenceKind {
3748-
bool result;
3749-
bool parameter;
3750-
bool requirement;
3751-
bool other;
3749+
private:
3750+
Kind kind;
37523751

3753-
/// The type does not refer to 'Self' at all.
3754-
static SelfReferenceKind None() {
3755-
return SelfReferenceKind(false, false, false, false);
3752+
public:
3753+
SelfReferencePosition(Kind kind) : kind(kind) {}
3754+
3755+
SelfReferencePosition flipped() const {
3756+
switch (kind) {
3757+
case None:
3758+
case Invariant:
3759+
return *this;
3760+
case Covariant:
3761+
return Contravariant;
3762+
case Contravariant:
3763+
return Covariant;
3764+
}
37563765
}
37573766

3758-
/// The type refers to 'Self', but only as the type of a property or
3759-
/// the result type of a method/subscript.
3760-
static SelfReferenceKind Result() {
3761-
return SelfReferenceKind(true, false, false, false);
3762-
}
3767+
explicit operator bool() const { return kind > None; }
37633768

3764-
/// The type refers to 'Self', but only as the parameter type
3765-
/// of a method/subscript.
3766-
static SelfReferenceKind Parameter() {
3767-
return SelfReferenceKind(false, true, false, false);
3768-
}
3769+
operator Kind() const { return kind; }
3770+
};
37693771

3770-
/// The type refers to 'Self' within a same-type requiement.
3771-
static SelfReferenceKind Requirement() {
3772-
return SelfReferenceKind(false, false, true, false);
3773-
}
3772+
/// Describes the least favorable positions at which a requirement refers
3773+
/// to 'Self' in terms of variance, for use in the is-inheritable and
3774+
/// is-available-existential checks.
3775+
struct SelfReferenceInfo final {
3776+
using Position = SelfReferencePosition;
3777+
3778+
bool hasCovariantSelfResult;
3779+
Position selfRef;
3780+
Position assocTypeRef;
37743781

3775-
/// The type refers to 'Self' in a position that is invariant.
3776-
static SelfReferenceKind Other() {
3777-
return SelfReferenceKind(false, false, false, true);
3782+
/// A reference to 'Self'.
3783+
static SelfReferenceInfo forSelfRef(Position position) {
3784+
assert(position);
3785+
return SelfReferenceInfo(false, position, Position::None);
37783786
}
37793787

3780-
SelfReferenceKind flip() const {
3781-
return SelfReferenceKind(parameter, result, requirement, other);
3788+
/// A reference to 'Self' through an associated type.
3789+
static SelfReferenceInfo forAssocTypeRef(Position position) {
3790+
assert(position);
3791+
return SelfReferenceInfo(false, Position::None, position);
37823792
}
37833793

3784-
SelfReferenceKind operator|=(SelfReferenceKind kind) {
3785-
result |= kind.result;
3786-
requirement |= kind.requirement;
3787-
parameter |= kind.parameter;
3788-
other |= kind.other;
3794+
SelfReferenceInfo operator|=(const SelfReferenceInfo &pos) {
3795+
hasCovariantSelfResult |= pos.hasCovariantSelfResult;
3796+
if (pos.selfRef > selfRef) {
3797+
selfRef = pos.selfRef;
3798+
}
3799+
if (pos.assocTypeRef > assocTypeRef) {
3800+
assocTypeRef = pos.assocTypeRef;
3801+
}
37893802
return *this;
37903803
}
37913804

3792-
operator bool() const {
3793-
return result || parameter || requirement || other;
3805+
explicit operator bool() const {
3806+
return hasCovariantSelfResult || selfRef || assocTypeRef;
37943807
}
37953808

3809+
SelfReferenceInfo()
3810+
: hasCovariantSelfResult(false), selfRef(Position::None),
3811+
assocTypeRef(Position::None) {}
3812+
37963813
private:
3797-
SelfReferenceKind(bool result, bool parameter, bool requirement, bool other)
3798-
: result(result), parameter(parameter), requirement(requirement),
3799-
other(other) { }
3814+
SelfReferenceInfo(bool hasCovariantSelfResult, Position selfRef,
3815+
Position assocTypeRef)
3816+
: hasCovariantSelfResult(hasCovariantSelfResult), selfRef(selfRef),
3817+
assocTypeRef(assocTypeRef) {}
38003818
};
38013819

38023820
/// The set of known protocols for which derived conformances are supported.
@@ -3978,15 +3996,12 @@ class ProtocolDecl final : public NominalTypeDecl {
39783996

39793997
/// Find direct Self references within the given requirement.
39803998
///
3981-
/// \param allowCovariantParameters If true, 'Self' is assumed to be
3982-
/// covariant anywhere; otherwise, only in the return type of the top-level
3983-
/// function type.
3984-
///
3985-
/// \param skipAssocTypes If true, associated types of 'Self' are ignored;
3986-
/// otherwise, they count as an 'other' usage of 'Self'.
3987-
SelfReferenceKind findProtocolSelfReferences(const ValueDecl *decl,
3988-
bool allowCovariantParameters,
3989-
bool skipAssocTypes) const;
3999+
/// \param treatNonResultCovariantSelfAsInvariant If true, 'Self' is only
4000+
/// assumed to be covariant in a top-level non-function type, or in the
4001+
/// eventual result type of a top-level function type.
4002+
SelfReferenceInfo
4003+
findProtocolSelfReferences(const ValueDecl *decl,
4004+
bool treatNonResultCovariantSelfAsInvariant) const;
39904005

39914006
/// Determine whether we are allowed to refer to an existential type
39924007
/// conforming to this protocol. This is only permitted if the type of

include/swift/AST/DeclContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,10 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
490490
/// Determine whether the innermost context is generic.
491491
bool isInnermostContextGeneric() const;
492492

493+
/// Determine whether this or any parent context is a `@_specialize` extension
494+
/// context.
495+
bool isInSpecializeExtensionContext() const;
496+
493497
/// Get the most optimal resilience expansion for code in this context.
494498
/// If the body is able to be inlined into functions in other resilience
495499
/// domains, this ensures that only sufficiently-conservative access patterns

include/swift/AST/DiagnosticsParse.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,8 @@ ERROR(expected_sil_function_type, none,
626626
"sil function expected to have SIL function type", ())
627627
ERROR(sil_dynamically_replaced_func_not_found,none,
628628
"dynamically replaced function not found %0", (Identifier))
629+
ERROR(sil_specialize_target_func_not_found,none,
630+
"_specialize target function not found %0", (Identifier))
629631
ERROR(sil_availability_expected_version,none,
630632
"expected version number in 'available' attribute", ())
631633

@@ -1579,6 +1581,10 @@ ERROR(attr_specialize_parameter_already_defined,none,
15791581

15801582
ERROR(attr_specialize_expected_partial_or_full,none,
15811583
"expected 'partial' or 'full' as values of the 'kind' parameter in '_specialize' attribute", ())
1584+
ERROR(attr_specialize_expected_function,none,
1585+
"expected a function name as the value of the 'target' parameter in '_specialize' attribute", ())
1586+
ERROR(attr_specialize_expected_spi_name,none,
1587+
"expected an SPI identifier as the value of the 'spi' parameter in '_specialize' attribute", ())
15821588

15831589
// _implements
15841590
ERROR(attr_implements_expected_member_name,PointsToFirstBadToken,

0 commit comments

Comments
 (0)