Skip to content

Commit 7c85261

Browse files
committed
Add runtime support
1 parent 3e9c463 commit 7c85261

27 files changed

+481
-182
lines changed

include/swift/ABI/GenericContext.h

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ class TargetGenericRequirementDescriptor {
255255
case GenericRequirementKind::SameType:
256256
case GenericRequirementKind::SameShape:
257257
case GenericRequirementKind::InvertedProtocols:
258-
case GenericRequirementKind::Value:
259258
return true;
260259
}
261260

@@ -327,6 +326,18 @@ struct ConditionalInvertibleProtocolSet: InvertibleProtocolSet {
327326
template<typename Runtime>
328327
struct TargetConditionalInvertibleProtocolRequirement: TargetGenericRequirementDescriptor<Runtime> { };
329328

329+
struct GenericValueHeader {
330+
/// The total number of generic parameters in this signature where
331+
/// getKind() == GenericParamKind::Value.
332+
uint32_t NumValues;
333+
};
334+
335+
/// The GenericValueHeader is followed by an array of these descriptors,
336+
/// whose length is given by the header's NumValues field.
337+
struct GenericValueDescriptor {
338+
GenericValueType Type;
339+
};
340+
330341
/// An array of generic parameter descriptors, all
331342
/// GenericParamDescriptor::implicit(), which is by far
332343
/// the most common case. Some generic context storage can
@@ -364,20 +375,26 @@ class RuntimeGenericSignature {
364375
const TargetGenericRequirementDescriptor<Runtime> *Requirements;
365376
GenericPackShapeHeader PackShapeHeader;
366377
const GenericPackShapeDescriptor *PackShapeDescriptors;
378+
GenericValueHeader ValueHeader;
379+
const GenericValueDescriptor *ValueDescriptors;
367380

368381
public:
369382
RuntimeGenericSignature()
370-
: Header{0, 0, 0, GenericContextDescriptorFlags(false, false)},
383+
: Header{0, 0, 0, GenericContextDescriptorFlags(false, false, false)},
371384
Params(nullptr), Requirements(nullptr),
372-
PackShapeHeader{0, 0}, PackShapeDescriptors(nullptr) {}
385+
PackShapeHeader{0, 0}, PackShapeDescriptors(nullptr), ValueHeader{0},
386+
ValueDescriptors(nullptr) {}
373387

374388
RuntimeGenericSignature(const TargetGenericContextDescriptorHeader<Runtime> &header,
375389
const GenericParamDescriptor *params,
376390
const TargetGenericRequirementDescriptor<Runtime> *requirements,
377391
const GenericPackShapeHeader &packShapeHeader,
378-
const GenericPackShapeDescriptor *packShapeDescriptors)
392+
const GenericPackShapeDescriptor *packShapeDescriptors,
393+
const GenericValueHeader &valueHeader,
394+
const GenericValueDescriptor *valueDescriptors)
379395
: Header(header), Params(params), Requirements(requirements),
380-
PackShapeHeader(packShapeHeader), PackShapeDescriptors(packShapeDescriptors) {}
396+
PackShapeHeader(packShapeHeader), PackShapeDescriptors(packShapeDescriptors),
397+
ValueHeader(valueHeader), ValueDescriptors(valueDescriptors) {}
381398

382399
llvm::ArrayRef<GenericParamDescriptor> getParams() const {
383400
return llvm::ArrayRef(Params, Header.NumParams);
@@ -395,6 +412,14 @@ class RuntimeGenericSignature {
395412
return llvm::ArrayRef(PackShapeDescriptors, PackShapeHeader.NumPacks);
396413
}
397414

415+
const GenericValueHeader &getGenericValueHeader() const {
416+
return ValueHeader;
417+
}
418+
419+
llvm::ArrayRef<GenericValueDescriptor> getGenericValueDescriptors() const {
420+
return llvm::ArrayRef(ValueDescriptors, ValueHeader.NumValues);
421+
}
422+
398423
size_t getArgumentLayoutSizeInWords() const {
399424
return Header.getArgumentLayoutSizeInWords();
400425
}
@@ -490,6 +515,8 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
490515
ConditionalInvertibleProtocolSet,
491516
ConditionalInvertibleProtocolsRequirementCount,
492517
TargetConditionalInvertibleProtocolRequirement<Runtime>,
518+
GenericValueHeader,
519+
GenericValueDescriptor,
493520
FollowingTrailingObjects...>
494521
{
495522
protected:
@@ -508,6 +535,8 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
508535
ConditionalInvertibleProtocolSet,
509536
ConditionalInvertibleProtocolsRequirementCount,
510537
GenericConditionalInvertibleProtocolRequirement,
538+
GenericValueHeader,
539+
GenericValueDescriptor,
511540
FollowingTrailingObjects...>;
512541
friend TrailingObjects;
513542

@@ -656,13 +685,33 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
656685
header.NumPacks};
657686
}
658687

688+
GenericValueHeader getGenericValueHeader() const {
689+
if (!asSelf()->isGeneric())
690+
return {0};
691+
if (!getGenericContextHeader().Flags.hasValues())
692+
return {0};
693+
return *this->template getTrailingObjects<GenericValueHeader>();
694+
}
695+
696+
llvm::ArrayRef<GenericValueDescriptor> getGenericValueDescriptors() const {
697+
auto header = getGenericValueHeader();
698+
699+
if (header.NumValues == 0)
700+
return {};
701+
702+
return {this->template getTrailingObjects<GenericValueDescriptor>(),
703+
header.NumValues};
704+
}
705+
659706
RuntimeGenericSignature<Runtime> getGenericSignature() const {
660707
if (!asSelf()->isGeneric()) return RuntimeGenericSignature<Runtime>();
661708
return {getGenericContextHeader(),
662709
getGenericParams().data(),
663710
getGenericRequirements().data(),
664711
getGenericPackShapeHeader(),
665-
getGenericPackShapeDescriptors().data()};
712+
getGenericPackShapeDescriptors().data(),
713+
getGenericValueHeader(),
714+
getGenericValueDescriptors().data()};
666715
}
667716

668717
protected:
@@ -721,6 +770,23 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
721770
return counts.empty() ? 0 : counts.back().count;
722771
}
723772

773+
size_t numTrailingObjects(OverloadToken<GenericValueHeader>) const {
774+
if (!asSelf()->isGeneric())
775+
return 0;
776+
777+
return getGenericContextHeader().Flags.hasValues() ? 1 : 0;
778+
}
779+
780+
size_t numTrailingObjects(OverloadToken<GenericValueDescriptor>) const {
781+
if (!asSelf()->isGeneric())
782+
return 0;
783+
784+
if (!getGenericContextHeader().Flags.hasValues())
785+
return 0;
786+
787+
return getGenericValueHeader().NumValues;
788+
}
789+
724790
#if defined(_MSC_VER) && _MSC_VER < 1920
725791
#undef OverloadToken
726792
#endif

include/swift/ABI/Metadata.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,7 @@ struct TargetExtendedExistentialTypeShape
21452145

21462146
RuntimeGenericSignature<Runtime> getRequirementSignature() const {
21472147
return {ReqSigHeader, getReqSigParams(), getReqSigRequirements(),
2148-
{0, 0}, nullptr};
2148+
{0, 0}, nullptr, {0}, nullptr};
21492149
}
21502150

21512151
unsigned getNumReqSigParams() const {
@@ -2225,7 +2225,8 @@ struct TargetExtendedExistentialTypeShape
22252225
RuntimeGenericSignature<Runtime> getGeneralizationSignature() const {
22262226
if (!hasGeneralizationSignature()) return RuntimeGenericSignature<Runtime>();
22272227
return {*getGenSigHeader(), getGenSigParams(), getGenSigRequirements(),
2228-
getGenSigPackShapeHeader(), getGenSigPackShapeDescriptors()};
2228+
getGenSigPackShapeHeader(), getGenSigPackShapeDescriptors(),
2229+
{0}, nullptr};
22292230
}
22302231

22312232
unsigned getNumGenSigParams() const {

include/swift/ABI/MetadataValues.h

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,12 +2003,12 @@ class GenericContextDescriptorFlags {
20032003
: Value(value) {}
20042004

20052005
constexpr GenericContextDescriptorFlags(
2006-
bool hasTypePacks, bool hasConditionalInvertedProtocols
2006+
bool hasTypePacks, bool hasConditionalInvertedProtocols, bool hasValues
20072007
) : GenericContextDescriptorFlags(
20082008
GenericContextDescriptorFlags((uint16_t)0)
20092009
.withHasTypePacks(hasTypePacks)
2010-
.withConditionalInvertedProtocols(
2011-
hasConditionalInvertedProtocols)) {}
2010+
.withConditionalInvertedProtocols(hasConditionalInvertedProtocols)
2011+
.withHasValues(hasValues)) {}
20122012

20132013
/// Whether this generic context has at least one type parameter
20142014
/// pack, in which case the generic context will have a trailing
@@ -2024,6 +2024,12 @@ class GenericContextDescriptorFlags {
20242024
return (Value & 0x2) != 0;
20252025
}
20262026

2027+
/// Whether this generic context has at least one value parameter, in which
2028+
/// case the generic context will have a trailing GenericValueHeader.
2029+
constexpr bool hasValues() const {
2030+
return (Value & 0x4) != 0;
2031+
}
2032+
20272033
constexpr GenericContextDescriptorFlags
20282034
withHasTypePacks(bool hasTypePacks) const {
20292035
return GenericContextDescriptorFlags((uint16_t)(
@@ -2036,6 +2042,12 @@ class GenericContextDescriptorFlags {
20362042
(Value & ~0x2) | (value ? 0x2 : 0)));
20372043
}
20382044

2045+
constexpr GenericContextDescriptorFlags
2046+
withHasValues(bool hasValues) const {
2047+
return GenericContextDescriptorFlags((uint16_t)(
2048+
(Value & ~0x4) | (hasValues ? 0x4 : 0)));
2049+
}
2050+
20392051
constexpr uint16_t getIntValue() const {
20402052
return Value;
20412053
}
@@ -2141,8 +2153,6 @@ enum class GenericRequirementKind : uint8_t {
21412153
/// This is more of an "anti-requirement", specifing which checks don't need
21422154
/// to happen for a given type.
21432155
InvertedProtocols = 5,
2144-
/// A value requirement.
2145-
Value = 6,
21462156
/// A layout requirement.
21472157
Layout = 0x1F,
21482158
};
@@ -2156,32 +2166,43 @@ class GenericRequirementFlags {
21562166
public:
21572167
constexpr GenericRequirementFlags(GenericRequirementKind kind,
21582168
bool hasKeyArgument,
2159-
bool isPackRequirement)
2169+
bool isPackRequirement,
2170+
bool isValueRequirement)
21602171
: GenericRequirementFlags(GenericRequirementFlags(0)
21612172
.withKind(kind)
21622173
.withKeyArgument(hasKeyArgument)
2163-
.withPackRequirement(isPackRequirement))
2174+
.withPackRequirement(isPackRequirement)
2175+
.withValueRequirement(isValueRequirement))
21642176
{}
21652177

2166-
constexpr bool hasKeyArgument() const {
2167-
return (Value & 0x80u) != 0;
2168-
}
2169-
21702178
/// If this is true, the subject type of the requirement is a pack.
21712179
/// When the requirement is a conformance requirement, the corresponding
21722180
/// entry in the generic arguments array becomes a TargetWitnessTablePack.
21732181
constexpr bool isPackRequirement() const {
21742182
return (Value & 0x20u) != 0;
21752183
}
21762184

2185+
constexpr bool hasKeyArgument() const {
2186+
return (Value & 0x80u) != 0;
2187+
}
2188+
2189+
/// If this is true, the subject type of the requirement is a value.
2190+
///
2191+
/// Note: We could introduce a new SameValue requirement instead of burning a
2192+
/// a bit for value requirements, but if somehow an existing requirement makes
2193+
/// sense for values besides "SameType" then this would've been better.
2194+
constexpr bool isValueRequirement() const {
2195+
return (Value & 0x100u) != 0;
2196+
}
2197+
21772198
constexpr GenericRequirementKind getKind() const {
21782199
return GenericRequirementKind(Value & 0x1Fu);
21792200
}
21802201

21812202
constexpr GenericRequirementFlags
2182-
withKeyArgument(bool hasKeyArgument) const {
2183-
return GenericRequirementFlags((Value & 0x7Fu)
2184-
| (hasKeyArgument ? 0x80u : 0));
2203+
withKind(GenericRequirementKind kind) const {
2204+
return assert((uint8_t(kind) & 0x1Fu) == uint8_t(kind)),
2205+
GenericRequirementFlags((Value & 0xE0u) | uint8_t(kind));
21852206
}
21862207

21872208
constexpr GenericRequirementFlags
@@ -2191,9 +2212,15 @@ class GenericRequirementFlags {
21912212
}
21922213

21932214
constexpr GenericRequirementFlags
2194-
withKind(GenericRequirementKind kind) const {
2195-
return assert((uint8_t(kind) & 0x1Fu) == uint8_t(kind)),
2196-
GenericRequirementFlags((Value & 0xE0u) | uint8_t(kind));
2215+
withKeyArgument(bool hasKeyArgument) const {
2216+
return GenericRequirementFlags((Value & 0x7Fu)
2217+
| (hasKeyArgument ? 0x80u : 0));
2218+
}
2219+
2220+
constexpr GenericRequirementFlags
2221+
withValueRequirement(bool isValueRequirement) const {
2222+
return GenericRequirementFlags((Value & 0xFFu)
2223+
| (isValueRequirement ? 0x100u : 0));
21972224
}
21982225

21992226
constexpr uint32_t getIntValue() const {
@@ -2211,6 +2238,10 @@ enum class GenericPackKind : uint16_t {
22112238
WitnessTable = 1
22122239
};
22132240

2241+
enum class GenericValueType : uint32_t {
2242+
Int = 0,
2243+
};
2244+
22142245
class GenericEnvironmentFlags {
22152246
uint32_t Value;
22162247

include/swift/AST/ASTDemangler.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ class ASTBuilder {
231231

232232
Type createParenType(Type base);
233233

234-
Type createIntegerType(unsigned value, bool isNegative);
234+
Type createIntegerType(size_t value);
235+
236+
Type createNegativeIntegerType(size_t value);
235237

236238
BuiltGenericSignature
237239
createGenericSignature(ArrayRef<BuiltType> params,

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ NODE(DependentGenericInverseConformanceRequirement)
400400

401401
// Added in Swift 6.TBD
402402
NODE(Integer)
403+
NODE(NegativeInteger)
403404
NODE(DependentGenericValueRequirement)
404405

405406
#undef CONTEXT_NODE

include/swift/Demangling/TypeDecoder.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,18 +1527,11 @@ class TypeDecoder {
15271527
}
15281528

15291529
case NodeKind::Integer: {
1530-
if (Node->getNumChildren() == 0) {
1531-
// Non-negative number.
1532-
return Builder.createIntegerType(Node->getIndex(), /*isNegative*/ false);
1533-
}
1534-
1535-
auto prefix = Node->getFirstChild();
1536-
1537-
if (prefix->getKind() != NodeKind::PrefixOperator) {
1538-
return MAKE_NODE_TYPE_ERROR0(Node, "unexpected child");
1539-
}
1530+
return Builder.createIntegerType((size_t)Node->getIndex());
1531+
}
15401532

1541-
return Builder.createIntegerType(Node->getIndex(), /*isNegative*/ true);
1533+
case NodeKind::NegativeInteger: {
1534+
return Builder.createNegativeIntegerType((size_t)Node->getIndex());
15421535
}
15431536

15441537
// TODO: Handle OpaqueReturnType, when we're in the middle of reconstructing

include/swift/Remote/MetadataReader.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,9 +1358,6 @@ class MetadataReader {
13581358
case GenericRequirementKind::InvertedProtocols:
13591359
return TypeLookupError(
13601360
"Unexpected invertible protocol in runtime generic signature");
1361-
case GenericRequirementKind::Value:
1362-
return TypeLookupError(
1363-
"Unexpected value requirement in runtime generic signature");
13641361
}
13651362
}
13661363

@@ -2892,8 +2889,6 @@ class MetadataReader {
28922889
llvm_unreachable("Implement me");
28932890
case GenericRequirementKind::InvertedProtocols:
28942891
llvm_unreachable("Implement me");
2895-
case GenericRequirementKind::Value:
2896-
llvm_unreachable("Implement me");
28972892
}
28982893
}
28992894

include/swift/RemoteInspection/TypeRefBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ class TypeRefBuilder {
917917
return nullptr;
918918
}
919919

920-
const TypeRef *createIntegerType(unsigned value) {
920+
const TypeRef *createIntegerType(unsigned value, bool isNegative) {
921921
return nullptr;
922922
}
923923

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3475,8 +3475,10 @@ IntegerType *IntegerType::get(StringRef value, bool isNegative,
34753475
if (auto intType = ctx.getImpl().IntegerTypes.FindNodeOrInsertPos(id, insertPos))
34763476
return intType;
34773477

3478+
auto strCopy = ctx.AllocateCopy(value);
3479+
34783480
auto intType = new (ctx, AllocationArena::Permanent)
3479-
IntegerType(value, isNegative, ctx);
3481+
IntegerType(strCopy, isNegative, ctx);
34803482

34813483
ctx.getImpl().IntegerTypes.InsertNode(intType, insertPos);
34823484
return intType;

lib/AST/ASTDemangler.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,12 @@ Type ASTBuilder::createParenType(Type base) {
10401040
return ParenType::get(Ctx, base);
10411041
}
10421042

1043-
Type ASTBuilder::createIntegerType(unsigned value, bool isNegative) {
1044-
return IntegerType::get(std::to_string(value), isNegative, Ctx);
1043+
Type ASTBuilder::createIntegerType(size_t value) {
1044+
return IntegerType::get(std::to_string(value), /*isNegative*/ false, Ctx);
1045+
}
1046+
1047+
Type ASTBuilder::createNegativeIntegerType(size_t value) {
1048+
return IntegerType::get(std::to_string(value), /*isNegative*/ true, Ctx);
10451049
}
10461050

10471051
GenericSignature

0 commit comments

Comments
 (0)