Skip to content

Commit 75c2cbf

Browse files
committed
Implement value generics
Some requirement machine work Rename requirement to Value Rename more things to Value Fix integer checking for requirement some docs and parser changes Minor fixes
1 parent 2eade1e commit 75c2cbf

File tree

163 files changed

+2130
-374
lines changed

Some content is hidden

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

163 files changed

+2130
-374
lines changed

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ class ExistentialMetatypeInst : SingleValueInstruction, UnaryInstruction {}
718718

719719
final public class ObjCProtocolInst : SingleValueInstruction {}
720720

721+
final public class TypeValueInst: SingleValueInstruction, UnaryInstruction {}
722+
721723
public class GlobalAccessInstruction : SingleValueInstruction {
722724
final public var global: GlobalVariable {
723725
bridged.GlobalAccessInst_getGlobal().globalVar

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public func registerSILClasses() {
150150
register(MetatypeInst.self)
151151
register(ValueMetatypeInst.self)
152152
register(ExistentialMetatypeInst.self)
153+
register(TypeValueInst.self)
153154
register(OpenPackElementInst.self)
154155
register(PackLengthInst.self)
155156
register(DynamicPackIndexInst.self)

docs/ABI/Mangling.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,10 @@ Types
710710
type ::= type 'Xm' METATYPE-REPR // existential metatype with representation
711711
type ::= 'Xe' // error or unresolved type
712712

713+
#if SWIFT_RUNTIME_VERSION >= 6.TBD
714+
type ::= '$' 'n'? NATURAL_ZERO // integer type
715+
#endif
716+
713717
bound-generic-type ::= type 'y' (type* '_')* type* retroactive-conformance* 'G' // one type-list per nesting level of type
714718
bound-generic-type ::= substitution
715719

@@ -1041,6 +1045,10 @@ now codified into the ABI; the index 0 is therefore reserved.
10411045

10421046
requirement ::= type 'Rh' GENERIC-PARAM-INDEX // same-shape requirement (only supported on a generic parameter)
10431047

1048+
#if SWIFT_RUNTIME_VERSION >= 6.TBD
1049+
requirement ::= type 'RV' GENERIC-PARAM-INDEX // value requirement
1050+
#endif
1051+
10441052
GENERIC-PARAM-INDEX ::= 'z' // depth = 0, idx = 0
10451053
GENERIC-PARAM-INDEX ::= INDEX // depth = 0, idx = N+1
10461054
GENERIC-PARAM-INDEX ::= 'd' INDEX INDEX // depth = M+1, idx = N

include/swift/ABI/GenericContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ struct TargetGenericContextDescriptorHeader {
7474
/// same order as the requirement descriptors which satisfy
7575
/// hasKeyArgument().
7676
///
77+
/// a sequence of values, in the same order as the parameter descriptors
78+
/// which satisify getKind() == GenericParamKind::Value and
79+
/// hasKeyArgument();
80+
///
7781
/// The elements above which are packs are precisely those appearing
7882
/// in the sequence of trailing GenericPackShapeDescriptors.
7983
uint16_t NumKeyArguments;
@@ -157,6 +161,9 @@ class TargetGenericRequirementDescriptor {
157161
uint16_t GenericParamIndex;
158162
InvertibleProtocolSet Protocols;
159163
} InvertedProtocols;
164+
165+
/// FIXME ALEX DOCS
166+
uint32_t ValueType;
160167
};
161168

162169
constexpr GenericRequirementFlags getFlags() const {
@@ -248,6 +255,7 @@ class TargetGenericRequirementDescriptor {
248255
case GenericRequirementKind::SameType:
249256
case GenericRequirementKind::SameShape:
250257
case GenericRequirementKind::InvertedProtocols:
258+
case GenericRequirementKind::Value:
251259
return true;
252260
}
253261

include/swift/ABI/MetadataValues.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,9 @@ enum class GenericParamKind : uint8_t {
20482048
/// A type parameter pack.
20492049
TypePack = 1,
20502050

2051+
/// A value type parameter.
2052+
Value = 2,
2053+
20512054
Max = 0x3F,
20522055
};
20532056

@@ -2138,6 +2141,8 @@ enum class GenericRequirementKind : uint8_t {
21382141
/// This is more of an "anti-requirement", specifing which checks don't need
21392142
/// to happen for a given type.
21402143
InvertedProtocols = 5,
2144+
/// A value requirement.
2145+
Value = 6,
21412146
/// A layout requirement.
21422147
Layout = 0x1F,
21432148
};

include/swift/AST/ASTBridging.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,10 +1897,10 @@ BridgedGenericParamList BridgedGenericParamList_createParsed(
18971897

18981898
SWIFT_NAME(
18991899
"BridgedGenericTypeParamDecl.createParsed(_:declContext:eachKeywordLoc:"
1900-
"name:nameLoc:inheritedType:index:)")
1900+
"letKeywordLoc:name:nameLoc:inheritedType:index:)")
19011901
BridgedGenericTypeParamDecl BridgedGenericTypeParamDecl_createParsed(
19021902
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
1903-
BridgedSourceLoc cEachLoc, BridgedIdentifier cName,
1903+
BridgedSourceLoc cEachLoc, BridgedSourceLoc cLetLoc, BridgedIdentifier cName,
19041904
BridgedSourceLoc cNameLoc, BridgedNullableTypeRepr opaqueInheritedType,
19051905
size_t index);
19061906

include/swift/AST/ASTDemangler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class ASTBuilder {
231231

232232
Type createParenType(Type base);
233233

234+
Type createIntegerType(unsigned value, bool isNegative);
235+
234236
BuiltGenericSignature
235237
createGenericSignature(ArrayRef<BuiltType> params,
236238
ArrayRef<BuiltRequirement> requirements);

include/swift/AST/Attr.h

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,98 +2529,92 @@ class MacroRoleAttr final
25292529
/// Specifies the raw storage used by a type.
25302530
class RawLayoutAttr final : public DeclAttribute {
25312531
/// The element type to share size and alignment with, if any.
2532-
TypeRepr *LikeType;
2532+
TypeRepr *LikeType = nullptr;
25332533
/// The number of elements in an array to share stride and alignment with,
2534-
/// or zero if no such size was specified. If `LikeType` is null, this is
2535-
/// the size in bytes of the raw storage.
2536-
unsigned SizeOrCount;
2534+
/// or nullptr if no such size was specified.
2535+
TypeRepr *CountType = nullptr;
2536+
/// The size in bytes of the raw storage.
2537+
unsigned Size = 0;
25372538
/// If `LikeType` is null, the alignment in bytes to use for the raw storage.
2538-
unsigned Alignment;
2539+
unsigned Alignment = 0;
25392540
/// If a value of this raw layout type should move like its `LikeType`.
25402541
bool MovesAsLike = false;
25412542
/// The resolved like type.
25422543
mutable Type CachedResolvedLikeType = Type();
2544+
/// The resolved count type.
2545+
mutable Type CachedResolvedCountType = Type();
25432546

2544-
friend class ResolveRawLayoutLikeTypeRequest;
2547+
friend class ResolveRawLayoutTypeRequest;
25452548

25462549
public:
25472550
/// Construct a `@_rawLayout(like: T)` attribute.
25482551
RawLayoutAttr(TypeRepr *LikeType, bool movesAsLike, SourceLoc AtLoc,
25492552
SourceRange Range)
25502553
: DeclAttribute(DeclAttrKind::RawLayout, AtLoc, Range,
25512554
/*implicit*/ false),
2552-
LikeType(LikeType), SizeOrCount(0), Alignment(~0u),
2553-
MovesAsLike(movesAsLike) {}
2555+
LikeType(LikeType), MovesAsLike(movesAsLike) {}
25542556

25552557
/// Construct a `@_rawLayout(likeArrayOf: T, count: N)` attribute.
2556-
RawLayoutAttr(TypeRepr *LikeType, unsigned Count, bool movesAsLike,
2558+
RawLayoutAttr(TypeRepr *LikeType, TypeRepr *CountType, bool movesAsLike,
25572559
SourceLoc AtLoc, SourceRange Range)
25582560
: DeclAttribute(DeclAttrKind::RawLayout, AtLoc, Range,
25592561
/*implicit*/ false),
2560-
LikeType(LikeType), SizeOrCount(Count), Alignment(0),
2561-
MovesAsLike(movesAsLike) {}
2562+
LikeType(LikeType), CountType(CountType), MovesAsLike(movesAsLike) {}
25622563

25632564
/// Construct a `@_rawLayout(size: N, alignment: M)` attribute.
25642565
RawLayoutAttr(unsigned Size, unsigned Alignment, SourceLoc AtLoc,
25652566
SourceRange Range)
25662567
: DeclAttribute(DeclAttrKind::RawLayout, AtLoc, Range,
25672568
/*implicit*/ false),
2568-
LikeType(nullptr), SizeOrCount(Size), Alignment(Alignment) {}
2569+
Size(Size), Alignment(Alignment) {}
25692570

25702571
/// Return the type whose single-element layout the attribute type should get
25712572
/// its layout from. Returns null if the attribute specifies an array or manual
25722573
/// layout.
25732574
TypeRepr *getScalarLikeType() const {
2574-
if (!LikeType)
2575-
return nullptr;
2576-
if (Alignment != ~0u)
2575+
if (!LikeType || CountType)
25772576
return nullptr;
25782577
return LikeType;
25792578
}
25802579

25812580
/// Return the type whose array layout the attribute type should get its
25822581
/// layout from, along with the size of that array. Returns None if the
25832582
/// attribute specifies scalar or manual layout.
2584-
std::optional<std::pair<TypeRepr *, unsigned>>
2583+
std::optional<std::pair<TypeRepr *, TypeRepr *>>
25852584
getArrayLikeTypeAndCount() const {
2586-
if (!LikeType)
2587-
return std::nullopt;
2588-
if (Alignment == ~0u)
2585+
if (!LikeType || !CountType)
25892586
return std::nullopt;
2590-
return std::make_pair(LikeType, SizeOrCount);
2587+
return std::make_pair(LikeType, CountType);
25912588
}
25922589

25932590
/// Return the size and alignment of the attributed type. Returns
25942591
/// None if the attribute specifies layout like some other type.
25952592
std::optional<std::pair<unsigned, unsigned>> getSizeAndAlignment() const {
25962593
if (LikeType)
25972594
return std::nullopt;
2598-
return std::make_pair(SizeOrCount, Alignment);
2595+
return std::make_pair(Size, Alignment);
25992596
}
26002597

26012598
Type getResolvedLikeType(StructDecl *sd) const;
2599+
Type getResolvedCountType(StructDecl *sd) const;
26022600

26032601
/// Return the type whose single-element layout the attribute type should get
26042602
/// its layout from. Returns None if the attribute specifies an array or manual
26052603
/// layout.
26062604
std::optional<Type> getResolvedScalarLikeType(StructDecl *sd) const {
2607-
if (!LikeType)
2608-
return std::nullopt;
2609-
if (Alignment != ~0u)
2605+
if (!LikeType || CountType)
26102606
return std::nullopt;
26112607
return getResolvedLikeType(sd);
26122608
}
26132609

26142610
/// Return the type whose array layout the attribute type should get its
26152611
/// layout from, along with the size of that array. Returns None if the
26162612
/// attribute specifies scalar or manual layout.
2617-
std::optional<std::pair<Type, unsigned>>
2613+
std::optional<std::pair<Type, Type>>
26182614
getResolvedArrayLikeTypeAndCount(StructDecl *sd) const {
2619-
if (!LikeType)
2620-
return std::nullopt;
2621-
if (Alignment == ~0u)
2615+
if (!LikeType || !CountType)
26222616
return std::nullopt;
2623-
return std::make_pair(getResolvedLikeType(sd), SizeOrCount);
2617+
return std::make_pair(getResolvedLikeType(sd), getResolvedCountType(sd));
26242618
}
26252619

26262620
/// Whether a value of this raw layout should move like its `LikeType`.

0 commit comments

Comments
 (0)