Skip to content

Commit b8e16e8

Browse files
authored
Merge pull request #40059 from CodaFi/sequential-consistency
2 parents 77040ef + 22405ce commit b8e16e8

36 files changed

+261
-175
lines changed

include/swift/AST/Decl.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,12 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
486486
SWIFT_INLINE_BITFIELD_EMPTY(TypeDecl, ValueDecl);
487487
SWIFT_INLINE_BITFIELD_EMPTY(AbstractTypeParamDecl, TypeDecl);
488488

489-
SWIFT_INLINE_BITFIELD_FULL(GenericTypeParamDecl, AbstractTypeParamDecl, 16+16,
489+
SWIFT_INLINE_BITFIELD_FULL(GenericTypeParamDecl, AbstractTypeParamDecl, 16+16+1,
490490
: NumPadBits,
491491

492492
Depth : 16,
493-
Index : 16
493+
Index : 16,
494+
TypeSequence : 1
494495
);
495496

496497
SWIFT_INLINE_BITFIELD_EMPTY(GenericTypeDecl, TypeDecl);
@@ -2883,7 +2884,7 @@ class GenericTypeParamDecl : public AbstractTypeParamDecl {
28832884
/// \param name The name of the generic parameter.
28842885
/// \param nameLoc The location of the name.
28852886
GenericTypeParamDecl(DeclContext *dc, Identifier name, SourceLoc nameLoc,
2886-
unsigned depth, unsigned index);
2887+
bool isTypeSequence, unsigned depth, unsigned index);
28872888

28882889
/// The depth of this generic type parameter, i.e., the number of outer
28892890
/// levels of generic parameter lists that enclose this type parameter.
@@ -2905,6 +2906,15 @@ class GenericTypeParamDecl : public AbstractTypeParamDecl {
29052906
assert(Bits.GenericTypeParamDecl.Depth == depth && "Truncation");
29062907
}
29072908

2909+
/// Returns \c true if this generic type parameter is declared as a type
2910+
/// sequence.
2911+
///
2912+
/// \code
2913+
/// func foo<@_typeSequence T>(_ : T...) { }
2914+
/// struct Foo<@_typeSequence T> { }
2915+
/// \encode
2916+
bool isTypeSequence() const { return Bits.GenericTypeParamDecl.TypeSequence; }
2917+
29082918
/// The index of this generic type parameter within its generic parameter
29092919
/// list.
29102920
///
@@ -7594,7 +7604,8 @@ inline bool Decl::isPotentiallyOverridable() const {
75947604
}
75957605

75967606
inline GenericParamKey::GenericParamKey(const GenericTypeParamDecl *d)
7597-
: Depth(d->getDepth()), Index(d->getIndex()) { }
7607+
: TypeSequence(d->isTypeSequence()), Depth(d->getDepth()),
7608+
Index(d->getIndex()) {}
75987609

75997610
inline const GenericContext *Decl::getAsGenericContext() const {
76007611
switch (getKind()) {

include/swift/AST/GenericParamKey.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ class GenericTypeParamType;
2424
/// A fully-abstracted generic type parameter key, maintaining only the depth
2525
/// and index of the generic parameter.
2626
struct GenericParamKey {
27-
unsigned Depth : 16;
27+
unsigned TypeSequence : 1;
28+
unsigned Depth : 15;
2829
unsigned Index : 16;
2930

30-
GenericParamKey(unsigned depth, unsigned index)
31-
: Depth(depth), Index(index) { }
31+
GenericParamKey(bool isTypeSequence, unsigned depth, unsigned index)
32+
: TypeSequence(isTypeSequence), Depth(depth), Index(index) {}
3233

3334
GenericParamKey(const GenericTypeParamDecl *d);
3435
GenericParamKey(const GenericTypeParamType *d);
3536

3637
friend bool operator==(GenericParamKey lhs, GenericParamKey rhs) {
37-
return lhs.Depth == rhs.Depth && lhs.Index == rhs.Index;
38+
return lhs.TypeSequence == rhs.TypeSequence && lhs.Depth == rhs.Depth &&
39+
lhs.Index == rhs.Index;
3840
}
3941

4042
friend bool operator!=(GenericParamKey lhs, GenericParamKey rhs) {
@@ -100,18 +102,20 @@ namespace llvm {
100102
template<>
101103
struct DenseMapInfo<swift::GenericParamKey> {
102104
static inline swift::GenericParamKey getEmptyKey() {
103-
return {0xFFFF, 0xFFFF};
105+
return {true, 0xFFFF, 0xFFFF};
104106
}
105107
static inline swift::GenericParamKey getTombstoneKey() {
106-
return {0xFFFE, 0xFFFE};
108+
return {true, 0xFFFE, 0xFFFE};
107109
}
108110

109111
static inline unsigned getHashValue(swift::GenericParamKey k) {
110-
return DenseMapInfo<unsigned>::getHashValue(k.Depth << 16 | k.Index);
112+
return DenseMapInfo<unsigned>::getHashValue(
113+
k.Depth << 16 | k.Index | ((k.TypeSequence ? 1 : 0) << 30));
111114
}
112115
static bool isEqual(swift::GenericParamKey a,
113116
swift::GenericParamKey b) {
114-
return a.Depth == b.Depth && a.Index == b.Index;
117+
return a.TypeSequence == b.TypeSequence && a.Depth == b.Depth &&
118+
a.Index == b.Index;
115119
}
116120
};
117121

include/swift/AST/Types.h

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5704,15 +5704,17 @@ const Type *ArchetypeType::getSubclassTrailingObjects() const {
57045704
///
57055705
/// \sa GenericTypeParamDecl
57065706
class GenericTypeParamType : public SubstitutableType {
5707+
static constexpr unsigned TYPE_SEQUENCE_BIT = (1 << 30);
5708+
57075709
using DepthIndexTy = llvm::PointerEmbeddedInt<unsigned, 31>;
57085710

57095711
/// The generic type parameter or depth/index.
57105712
llvm::PointerUnion<GenericTypeParamDecl *, DepthIndexTy> ParamOrDepthIndex;
57115713

57125714
public:
57135715
/// Retrieve a generic type parameter at the given depth and index.
5714-
static GenericTypeParamType *get(unsigned depth, unsigned index,
5715-
const ASTContext &ctx);
5716+
static GenericTypeParamType *get(bool isTypeSequence, unsigned depth,
5717+
unsigned index, const ASTContext &ctx);
57165718

57175719
/// Retrieve the declaration of the generic type parameter, or null if
57185720
/// there is no such declaration.
@@ -5747,6 +5749,15 @@ class GenericTypeParamType : public SubstitutableType {
57475749
/// Here 'T' and 'U' have indexes 0 and 1, respectively. 'V' has index 0.
57485750
unsigned getIndex() const;
57495751

5752+
/// Returns \c true if this generic type parameter is declared as a type
5753+
/// sequence.
5754+
///
5755+
/// \code
5756+
/// func foo<@_typeSequence T>(_ : T...) { }
5757+
/// struct Foo<@_typeSequence T> { }
5758+
/// \encode
5759+
bool isTypeSequence() const;
5760+
57505761
// Implement isa/cast/dyncast/etc.
57515762
static bool classof(const TypeBase *T) {
57525763
return T->getKind() == TypeKind::GenericTypeParam;
@@ -5760,18 +5771,19 @@ class GenericTypeParamType : public SubstitutableType {
57605771
RecursiveTypeProperties::HasTypeParameter),
57615772
ParamOrDepthIndex(param) { }
57625773

5763-
explicit GenericTypeParamType(unsigned depth,
5764-
unsigned index,
5765-
const ASTContext &ctx)
5766-
: SubstitutableType(TypeKind::GenericTypeParam, &ctx,
5767-
RecursiveTypeProperties::HasTypeParameter),
5768-
ParamOrDepthIndex(depth << 16 | index) { }
5774+
explicit GenericTypeParamType(bool isTypeSequence, unsigned depth,
5775+
unsigned index, const ASTContext &ctx)
5776+
: SubstitutableType(TypeKind::GenericTypeParam, &ctx,
5777+
RecursiveTypeProperties::HasTypeParameter),
5778+
ParamOrDepthIndex(depth << 16 | index |
5779+
((isTypeSequence ? 1 : 0) << 30)) {}
57695780
};
57705781
BEGIN_CAN_TYPE_WRAPPER(GenericTypeParamType, SubstitutableType)
5771-
static CanGenericTypeParamType get(unsigned depth, unsigned index,
5772-
const ASTContext &C) {
5773-
return CanGenericTypeParamType(GenericTypeParamType::get(depth, index, C));
5774-
}
5782+
static CanGenericTypeParamType get(bool isTypeSequence, unsigned depth,
5783+
unsigned index, const ASTContext &C) {
5784+
return CanGenericTypeParamType(
5785+
GenericTypeParamType::get(isTypeSequence, depth, index, C));
5786+
}
57755787
END_CAN_TYPE_WRAPPER(GenericTypeParamType, SubstitutableType)
57765788

57775789
/// A type that refers to a member type of some type that is dependent on a
@@ -6323,7 +6335,8 @@ constexpr bool TypeBase::isSugaredType<id##Type>() { \
63236335
#include "swift/AST/TypeNodes.def"
63246336

63256337
inline GenericParamKey::GenericParamKey(const GenericTypeParamType *p)
6326-
: Depth(p->getDepth()), Index(p->getIndex()) { }
6338+
: TypeSequence(p->isTypeSequence()), Depth(p->getDepth()),
6339+
Index(p->getIndex()) {}
63276340

63286341
inline TypeBase *TypeBase::getDesugaredType() {
63296342
if (!isa<SugarType>(this))

lib/AST/ASTContext.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3704,15 +3704,17 @@ GenericFunctionType::GenericFunctionType(
37043704
}
37053705
}
37063706

3707-
GenericTypeParamType *GenericTypeParamType::get(unsigned depth, unsigned index,
3707+
GenericTypeParamType *GenericTypeParamType::get(bool isTypeSequence,
3708+
unsigned depth, unsigned index,
37083709
const ASTContext &ctx) {
3709-
auto known = ctx.getImpl().GenericParamTypes.find({ depth, index });
3710+
const auto depthKey = depth | ((isTypeSequence ? 1 : 0) << 30);
3711+
auto known = ctx.getImpl().GenericParamTypes.find({depthKey, index});
37103712
if (known != ctx.getImpl().GenericParamTypes.end())
37113713
return known->second;
37123714

37133715
auto result = new (ctx, AllocationArena::Permanent)
3714-
GenericTypeParamType(depth, index, ctx);
3715-
ctx.getImpl().GenericParamTypes[{depth, index}] = result;
3716+
GenericTypeParamType(isTypeSequence, depth, index, ctx);
3717+
ctx.getImpl().GenericParamTypes[{depthKey, index}] = result;
37163718
return result;
37173719
}
37183720

@@ -4366,8 +4368,10 @@ CanOpenedArchetypeType OpenedArchetypeType::get(Type existential,
43664368
::new (mem) OpenedArchetypeType(ctx, existential,
43674369
protos, layoutSuperclass,
43684370
layoutConstraint, *knownID);
4369-
result->InterfaceType = GenericTypeParamType::get(0, 0, ctx);
4370-
4371+
result->InterfaceType =
4372+
GenericTypeParamType::get(/*type sequence*/ false,
4373+
/*depth*/ 0, /*index*/ 0, ctx);
4374+
43714375
openedExistentialArchetypes[*knownID] = result;
43724376
return CanOpenedArchetypeType(result);
43734377
}
@@ -5011,8 +5015,9 @@ ASTContext::getClangTypeForIRGen(Type ty) {
50115015
CanGenericSignature ASTContext::getSingleGenericParameterSignature() const {
50125016
if (auto theSig = getImpl().SingleGenericParameterSignature)
50135017
return theSig;
5014-
5015-
auto param = GenericTypeParamType::get(0, 0, *this);
5018+
5019+
auto param = GenericTypeParamType::get(/*type sequence*/ false,
5020+
/*depth*/ 0, /*index*/ 0, *this);
50165021
auto sig = GenericSignature::get(param, { });
50175022
auto canonicalSig = CanGenericSignature(sig);
50185023
getImpl().SingleGenericParameterSignature = canonicalSig;
@@ -5041,7 +5046,9 @@ CanGenericSignature ASTContext::getOpenedArchetypeSignature(Type type) {
50415046
if (found != getImpl().ExistentialSignatures.end())
50425047
return found->second;
50435048

5044-
auto genericParam = GenericTypeParamType::get(0, 0, *this);
5049+
auto genericParam =
5050+
GenericTypeParamType::get(/*type sequence*/ false,
5051+
/*depth*/ 0, /*index*/ 0, *this);
50455052
Requirement requirement(RequirementKind::Conformance, genericParam,
50465053
existential);
50475054
auto genericSig = buildGenericSignature(*this,
@@ -5126,7 +5133,8 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
51265133
}
51275134

51285135
return CanGenericTypeParamType::get(
5129-
gp->getDepth() - baseDepth + derivedDepth, gp->getIndex(), *this);
5136+
gp->isTypeSequence(), gp->getDepth() - baseDepth + derivedDepth,
5137+
gp->getIndex(), *this);
51305138
};
51315139

51325140
auto lookupConformanceFn =

lib/AST/ASTDemangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ Type ASTBuilder::createMetatypeType(Type instance,
626626

627627
Type ASTBuilder::createGenericTypeParameterType(unsigned depth,
628628
unsigned index) {
629-
return GenericTypeParamType::get(depth, index, Ctx);
629+
return GenericTypeParamType::get(/*type sequence*/ false, depth, index, Ctx);
630630
}
631631

632632
Type ASTBuilder::createDependentMemberType(StringRef member,

lib/AST/Builtins.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ static GenericTypeParamDecl*
234234
createGenericParam(ASTContext &ctx, const char *name, unsigned index) {
235235
ModuleDecl *M = ctx.TheBuiltinModule;
236236
Identifier ident = ctx.getIdentifier(name);
237-
auto genericParam =
238-
new (ctx) GenericTypeParamDecl(&M->getMainFile(FileUnitKind::Builtin),
239-
ident, SourceLoc(), 0, index);
237+
auto genericParam = new (ctx) GenericTypeParamDecl(
238+
&M->getMainFile(FileUnitKind::Builtin), ident, SourceLoc(),
239+
/*type sequence*/ false, 0, index);
240240
return genericParam;
241241
}
242242

lib/AST/Decl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4043,12 +4043,14 @@ AbstractTypeParamDecl::getConformingProtocols() const {
40434043

40444044
GenericTypeParamDecl::GenericTypeParamDecl(DeclContext *dc, Identifier name,
40454045
SourceLoc nameLoc,
4046-
unsigned depth, unsigned index)
4047-
: AbstractTypeParamDecl(DeclKind::GenericTypeParam, dc, name, nameLoc) {
4046+
bool isTypeSequence, unsigned depth,
4047+
unsigned index)
4048+
: AbstractTypeParamDecl(DeclKind::GenericTypeParam, dc, name, nameLoc) {
40484049
Bits.GenericTypeParamDecl.Depth = depth;
40494050
assert(Bits.GenericTypeParamDecl.Depth == depth && "Truncation");
40504051
Bits.GenericTypeParamDecl.Index = index;
40514052
assert(Bits.GenericTypeParamDecl.Index == index && "Truncation");
4053+
Bits.GenericTypeParamDecl.TypeSequence = isTypeSequence;
40524054
auto &ctx = dc->getASTContext();
40534055
auto type = new (ctx, AllocationArena::Permanent) GenericTypeParamType(this);
40544056
setInterfaceType(MetatypeType::get(type, ctx));

lib/AST/GenericParamList.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ GenericParamList::clone(DeclContext *dc) const {
7474
SmallVector<GenericTypeParamDecl *, 2> params;
7575
for (auto param : getParams()) {
7676
auto *newParam = new (ctx) GenericTypeParamDecl(
77-
dc, param->getName(), SourceLoc(),
78-
GenericTypeParamDecl::InvalidDepth,
79-
param->getIndex());
77+
dc, param->getName(), SourceLoc(), param->isTypeSequence(),
78+
GenericTypeParamDecl::InvalidDepth, param->getIndex());
8079
newParam->setImplicit(true);
8180
params.push_back(newParam);
8281
}

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,9 +2816,9 @@ static Type formDependentType(GenericTypeParamType *base,
28162816
/// parameter key, then following the path of associated types.
28172817
static Type formDependentType(ASTContext &ctx, GenericParamKey genericParam,
28182818
RelativeRewritePath path) {
2819-
return formDependentType(GenericTypeParamType::get(genericParam.Depth,
2820-
genericParam.Index,
2821-
ctx),
2819+
return formDependentType(GenericTypeParamType::get(genericParam.TypeSequence,
2820+
genericParam.Depth,
2821+
genericParam.Index, ctx),
28222822
path);
28232823
}
28242824

@@ -3306,9 +3306,9 @@ bool GenericSignatureBuilder::addSameTypeRewriteRule(CanType type1,
33063306
}
33073307

33083308
// Add the rewrite rule.
3309-
Type firstBase =
3310-
GenericTypeParamType::get(path1.getBase()->Depth, path1.getBase()->Index,
3311-
getASTContext());
3309+
Type firstBase = GenericTypeParamType::get(
3310+
path1.getBase()->TypeSequence, path1.getBase()->Depth,
3311+
path1.getBase()->Index, getASTContext());
33123312
CanType baseAnchor =
33133313
getCanonicalTypeParameter(firstBase)->getCanonicalType();
33143314
auto root = Impl->getOrCreateRewriteTreeRoot(baseAnchor);
@@ -3317,10 +3317,9 @@ bool GenericSignatureBuilder::addSameTypeRewriteRule(CanType type1,
33173317

33183318
Type GenericSignatureBuilder::getCanonicalTypeParameter(Type type) {
33193319
auto initialPath = RewritePath::createPath(type);
3320-
auto genericParamType =
3321-
GenericTypeParamType::get(initialPath.getBase()->Depth,
3322-
initialPath.getBase()->Index,
3323-
getASTContext());
3320+
auto genericParamType = GenericTypeParamType::get(
3321+
initialPath.getBase()->TypeSequence, initialPath.getBase()->Depth,
3322+
initialPath.getBase()->Index, getASTContext());
33243323

33253324
unsigned startIndex = 0;
33263325
Type currentType = genericParamType;
@@ -3357,8 +3356,8 @@ Type GenericSignatureBuilder::getCanonicalTypeParameter(Type type) {
33573356
// If this is an absolute path, use the new base.
33583357
if (auto newBase = match->second.getBase()) {
33593358
genericParamType =
3360-
GenericTypeParamType::get(newBase->Depth, newBase->Index,
3361-
getASTContext());
3359+
GenericTypeParamType::get(newBase->TypeSequence, newBase->Depth,
3360+
newBase->Index, getASTContext());
33623361
}
33633362

33643363
// Move back to the beginning; we may have opened up other rewrites.

lib/AST/Module.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,8 @@ static ProtocolConformanceRef getBuiltinTupleTypeConformance(
10381038
SmallVector<TupleTypeElt, 4> genericElements;
10391039
SmallVector<Requirement, 4> conditionalRequirements;
10401040
for (const auto &elt : tupleType->getElements()) {
1041-
auto genericParam = GenericTypeParamType::get(0, genericParams.size(), ctx);
1041+
auto genericParam = GenericTypeParamType::get(/*type sequence*/ false, 0,
1042+
genericParams.size(), ctx);
10421043
genericParams.push_back(genericParam);
10431044
typeSubstitutions.push_back(elt.getRawType());
10441045
genericElements.push_back(elt.getWithType(genericParam));

lib/AST/NameLookup.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,8 +2566,9 @@ GenericParamListRequest::evaluate(Evaluator &evaluator, GenericContext *value) c
25662566
// The generic parameter 'Self'.
25672567
auto &ctx = value->getASTContext();
25682568
auto selfId = ctx.Id_Self;
2569-
auto selfDecl = new (ctx) GenericTypeParamDecl(
2570-
proto, selfId, SourceLoc(), /*depth=*/0, /*index=*/0);
2569+
auto selfDecl = new (ctx)
2570+
GenericTypeParamDecl(proto, selfId, SourceLoc(),
2571+
/*type sequence=*/false, /*depth=*/0, /*index=*/0);
25712572
auto protoType = proto->getDeclaredInterfaceType();
25722573
InheritedEntry selfInherited[1] = {
25732574
InheritedEntry(TypeLoc::withoutLoc(protoType)) };

0 commit comments

Comments
 (0)