-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathMetadataValues.h
2952 lines (2468 loc) · 102 KB
/
MetadataValues.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- MetadataValues.h - Compiler/runtime ABI Metadata -------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This header is shared between the runtime and the compiler and
// includes target-independent information which can be usefully shared
// between them.
//
// This header ought not to include any compiler-specific headers (such as
// those from `swift/AST`, `swift/SIL`, etc.) since doing so may introduce
// accidental ABI dependencies on compiler internals.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_ABI_METADATAVALUES_H
#define SWIFT_ABI_METADATAVALUES_H
#include "swift/ABI/KeyPath.h"
#include "swift/ABI/ProtocolDispatchStrategy.h"
#include "swift/ABI/InvertibleProtocols.h"
// FIXME: this include shouldn't be here, but removing it causes symbol
// mangling mismatches on Windows for some reason?
#include "swift/AST/Ownership.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/FlagSet.h"
#include "llvm/ADT/ArrayRef.h"
#include <stdlib.h>
#include <stdint.h>
namespace swift {
enum {
/// The number of words (pointers) in a value buffer.
NumWords_ValueBuffer = 3,
/// The number of words in a metadata completion context.
NumWords_MetadataCompletionContext = 4,
/// The number of words in a yield-once coroutine buffer.
NumWords_YieldOnceBuffer = 4,
/// The number of words in a yield-many coroutine buffer.
NumWords_YieldManyBuffer = 8,
/// The number of words (in addition to the heap-object header)
/// in a default actor.
NumWords_DefaultActor = 12,
/// The number of words (in addition to the heap-object header)
/// in a non-default distributed actor.
NumWords_NonDefaultDistributedActor = 12,
/// The number of words in a task.
NumWords_AsyncTask = 24,
/// The number of words in a task group.
NumWords_TaskGroup = 32,
/// The number of words in an AsyncLet (flags + child task context & allocation)
NumWords_AsyncLet = 80, // 640 bytes ought to be enough for anyone
/// The size of a unique hash.
NumBytes_UniqueHash = 16,
/// The maximum number of generic parameters that can be
/// implicitly declared, for generic signatures that support that.
MaxNumImplicitGenericParamDescriptors = 64,
};
struct InProcess;
template <typename Runtime> struct TargetMetadata;
using Metadata = TargetMetadata<InProcess>;
/// Non-type metadata kinds have this bit set.
const unsigned MetadataKindIsNonType = 0x400;
/// Non-heap metadata kinds have this bit set.
const unsigned MetadataKindIsNonHeap = 0x200;
// The above two flags are negative because the "class" kind has to be zero,
// and class metadata is both type and heap metadata.
/// Runtime-private metadata has this bit set. The compiler must not statically
/// generate metadata objects with these kinds, and external tools should not
/// rely on the stability of these values or the precise binary layout of
/// their associated data structures.
const unsigned MetadataKindIsRuntimePrivate = 0x100;
/// Kinds of Swift metadata records. Some of these are types, some
/// aren't.
enum class MetadataKind : uint32_t {
#define METADATAKIND(name, value) name = value,
#define ABSTRACTMETADATAKIND(name, start, end) \
name##_Start = start, name##_End = end,
#include "MetadataKind.def"
/// The largest possible non-isa-pointer metadata kind value.
///
/// This is included in the enumeration to prevent against attempts to
/// exhaustively match metadata kinds. Future Swift runtimes or compilers
/// may introduce new metadata kinds, so for forward compatibility, the
/// runtime must tolerate metadata with unknown kinds.
/// This specific value is not mapped to a valid metadata kind at this time,
/// however.
LastEnumerated = 0x7FF,
};
const unsigned LastEnumeratedMetadataKind =
(unsigned)MetadataKind::LastEnumerated;
inline bool isHeapMetadataKind(MetadataKind k) {
return !((uint32_t)k & MetadataKindIsNonHeap);
}
inline bool isTypeMetadataKind(MetadataKind k) {
return !((uint32_t)k & MetadataKindIsNonType);
}
inline bool isRuntimePrivateMetadataKind(MetadataKind k) {
return (uint32_t)k & MetadataKindIsRuntimePrivate;
}
/// Try to translate the 'isa' value of a type/heap metadata into a value
/// of the MetadataKind enum.
inline MetadataKind getEnumeratedMetadataKind(uint64_t kind) {
if (kind > LastEnumeratedMetadataKind)
return MetadataKind::Class;
return MetadataKind(kind);
}
StringRef getStringForMetadataKind(MetadataKind kind);
/// Kinds of Swift nominal type descriptor records.
enum class NominalTypeKind : uint32_t {
#define NOMINALTYPEMETADATAKIND(name, value) name = value,
#include "MetadataKind.def"
};
/// The maximum supported type alignment.
const size_t MaximumAlignment = 16;
/// The alignment of a DefaultActor.
const size_t Alignment_DefaultActor = MaximumAlignment;
const size_t Alignment_NonDefaultDistributedActor = MaximumAlignment;
/// The alignment of a TaskGroup.
const size_t Alignment_TaskGroup = MaximumAlignment;
/// The alignment of an AsyncLet.
const size_t Alignment_AsyncLet = MaximumAlignment;
/// Flags stored in the value-witness table.
template <typename int_type>
class TargetValueWitnessFlags {
public:
// The polarity of these bits is chosen so that, when doing struct layout, the
// flags of the field types can be mostly bitwise-or'ed together to derive the
// flags for the struct. (The "non-inline" and "has-extra-inhabitants" bits
// still require additional fixup.)
enum : uint32_t {
AlignmentMask = 0x000000FF,
// unused 0x0000FF00,
IsNonPOD = 0x00010000,
IsNonInline = 0x00020000,
// unused 0x00040000,
HasSpareBits = 0x00080000,
IsNonBitwiseTakable = 0x00100000,
HasEnumWitnesses = 0x00200000,
Incomplete = 0x00400000,
IsNonCopyable = 0x00800000,
IsNonBitwiseBorrowable = 0x01000000,
// unused 0xFE000000,
};
static constexpr const uint32_t MaxNumExtraInhabitants = 0x7FFFFFFF;
private:
uint32_t Data;
explicit constexpr TargetValueWitnessFlags(uint32_t data) : Data(data) {}
public:
constexpr TargetValueWitnessFlags() : Data(0) {}
/// The required alignment of the first byte of an object of this
/// type, expressed as a mask of the low bits that must not be set
/// in the pointer.
///
/// This representation can be easily converted to the 'alignof'
/// result by merely adding 1, but it is more directly useful for
/// performing dynamic structure layouts, and it grants an
/// additional bit of precision in a compact field without needing
/// to switch to an exponent representation.
///
/// For example, if the type needs to be 8-byte aligned, the
/// appropriate alignment mask should be 0x7.
size_t getAlignmentMask() const {
return (Data & AlignmentMask);
}
constexpr TargetValueWitnessFlags withAlignmentMask(size_t alignMask) const {
return TargetValueWitnessFlags((Data & ~AlignmentMask) | alignMask);
}
size_t getAlignment() const { return getAlignmentMask() + 1; }
constexpr TargetValueWitnessFlags withAlignment(size_t alignment) const {
return withAlignmentMask(alignment - 1);
}
/// True if the type requires out-of-line allocation of its storage.
/// This can be the case because the value requires more storage or if it is
/// not bitwise takable.
bool isInlineStorage() const { return !(Data & IsNonInline); }
constexpr TargetValueWitnessFlags withInlineStorage(bool isInline) const {
return TargetValueWitnessFlags((Data & ~IsNonInline) |
(isInline ? 0 : IsNonInline));
}
/// True if values of this type can be copied with memcpy (if it's copyable)
/// and destroyed with a no-op.
bool isPOD() const { return !(Data & IsNonPOD); }
constexpr TargetValueWitnessFlags withPOD(bool isPOD) const {
return TargetValueWitnessFlags((Data & ~IsNonPOD) |
(isPOD ? 0 : IsNonPOD));
}
/// True if values of this type can be taken with memcpy. Unlike C++ 'move',
/// 'take' is a destructive operation that invalidates the source object, so
/// most types can be taken with a simple bitwise copy. Only types with side
/// table references, like @weak references, or types with opaque value
/// semantics, like imported C++ types, are not bitwise-takable.
bool isBitwiseTakable() const { return !(Data & IsNonBitwiseTakable); }
constexpr TargetValueWitnessFlags withBitwiseTakable(bool isBT) const {
return TargetValueWitnessFlags((Data & ~IsNonBitwiseTakable) |
(isBT ? 0 : IsNonBitwiseTakable));
}
/// True if values of this type can be passed by value when borrowed.
/// If this bit is true, then borrows of the value are independent of the
/// value's address, so a value can be passed in registers or memcpy'd
/// while borrowed. This is in contrast to Rust, for instance, where a
/// `&T` type is always represented as a pointer, and borrowing a
/// value always moves the borrowed value into memory.
bool isBitwiseBorrowable() const {
/// This bit was introduced with Swift 6; prior to the introduction of
/// `Atomic` and `Mutex`, a type was always bitwise-borrowable if it
/// was bitwise-takable. Compilers and runtimes before Swift 6 would
/// never set the `IsNonBitwiseBorrowable` bit in the value witness
/// table, but any type that sets `IsNonBitwiseTakable` is definitely
/// not bitwise borrowable.
return isBitwiseTakable()
&& !(Data & IsNonBitwiseBorrowable);
}
constexpr TargetValueWitnessFlags withBitwiseBorrowable(bool isBB) const {
return TargetValueWitnessFlags((Data & ~IsNonBitwiseBorrowable) |
(isBB ? 0 : IsNonBitwiseBorrowable));
}
/// True if values of this type can be copied.
bool isCopyable() const { return !(Data & IsNonCopyable); }
constexpr TargetValueWitnessFlags withCopyable(bool isCopyable) const {
return TargetValueWitnessFlags((Data & ~IsNonCopyable) |
(isCopyable ? 0 : IsNonCopyable));
}
/// True if this type's binary representation is that of an enum, and the
/// enum value witness table entries are available in this type's value
/// witness table.
bool hasEnumWitnesses() const { return Data & HasEnumWitnesses; }
constexpr TargetValueWitnessFlags
withEnumWitnesses(bool hasEnumWitnesses) const {
return TargetValueWitnessFlags((Data & ~HasEnumWitnesses) |
(hasEnumWitnesses ? HasEnumWitnesses : 0));
}
/// True if the type with this value-witness table is incomplete,
/// meaning that its external layout (size, etc.) is meaningless
/// pending completion of the metadata layout.
bool isIncomplete() const { return Data & Incomplete; }
constexpr TargetValueWitnessFlags
withIncomplete(bool isIncomplete) const {
return TargetValueWitnessFlags((Data & ~Incomplete) |
(isIncomplete ? Incomplete : 0));
}
constexpr uint32_t getOpaqueValue() const {
return Data;
}
};
using ValueWitnessFlags = TargetValueWitnessFlags<size_t>;
/// Flags for dynamic-cast operations.
enum class DynamicCastFlags : size_t {
/// All flags clear.
Default = 0x0,
/// True if the cast is not permitted to fail.
Unconditional = 0x1,
/// True if the cast should 'take' the source value on success;
/// false if the value should be copied.
TakeOnSuccess = 0x2,
/// True if the cast should destroy the source value on failure;
/// false if the value should be left in place.
DestroyOnFailure = 0x4,
};
inline bool operator&(DynamicCastFlags a, DynamicCastFlags b) {
return (size_t(a) & size_t(b)) != 0;
}
inline DynamicCastFlags operator|(DynamicCastFlags a, DynamicCastFlags b) {
return DynamicCastFlags(size_t(a) | size_t(b));
}
inline DynamicCastFlags operator-(DynamicCastFlags a, DynamicCastFlags b) {
return DynamicCastFlags(size_t(a) & ~size_t(b));
}
inline DynamicCastFlags &operator|=(DynamicCastFlags &a, DynamicCastFlags b) {
return a = (a | b);
}
/// Swift class flags.
/// These flags are valid only when isTypeMetadata().
/// When !isTypeMetadata() these flags will collide with other Swift ABIs.
enum class ClassFlags : uint32_t {
/// Is this a Swift class from the Darwin pre-stable ABI?
/// This bit is clear in stable ABI Swift classes.
/// The Objective-C runtime also reads this bit.
IsSwiftPreStableABI = 0x1,
/// Does this class use Swift refcounting?
UsesSwiftRefcounting = 0x2,
/// Has this class a custom name, specified with the @objc attribute?
HasCustomObjCName = 0x4,
/// Whether this metadata is a specialization of a generic metadata pattern
/// which was created during compilation.
IsStaticSpecialization = 0x8,
/// Whether this metadata is a specialization of a generic metadata pattern
/// which was created during compilation and made to be canonical by
/// modifying the metadata accessor.
IsCanonicalStaticSpecialization = 0x10,
};
inline bool operator&(ClassFlags a, ClassFlags b) {
return (uint32_t(a) & uint32_t(b)) != 0;
}
inline ClassFlags operator|(ClassFlags a, ClassFlags b) {
return ClassFlags(uint32_t(a) | uint32_t(b));
}
inline ClassFlags &operator|=(ClassFlags &a, ClassFlags b) {
return a = (a | b);
}
/// Flags that go in a MethodDescriptor structure.
class MethodDescriptorFlags {
public:
typedef uint32_t int_type;
enum class Kind {
Method,
Init,
Getter,
Setter,
ModifyCoroutine,
ReadCoroutine,
Read2Coroutine,
Modify2Coroutine,
};
private:
enum : int_type {
KindMask = 0x0F, // 16 kinds should be enough for anybody
IsInstanceMask = 0x10,
IsDynamicMask = 0x20,
IsAsyncMask = 0x40,
ExtraDiscriminatorShift = 16,
ExtraDiscriminatorMask = 0xFFFF0000,
};
int_type Value;
public:
MethodDescriptorFlags(Kind kind) : Value(unsigned(kind)) {}
MethodDescriptorFlags withIsInstance(bool isInstance) const {
auto copy = *this;
if (isInstance) {
copy.Value |= IsInstanceMask;
} else {
copy.Value &= ~IsInstanceMask;
}
return copy;
}
MethodDescriptorFlags withIsDynamic(bool isDynamic) const {
auto copy = *this;
if (isDynamic)
copy.Value |= IsDynamicMask;
else
copy.Value &= ~IsDynamicMask;
return copy;
}
MethodDescriptorFlags withIsAsync(bool isAsync) const {
auto copy = *this;
if (isAsync)
copy.Value |= IsAsyncMask;
else
copy.Value &= ~IsAsyncMask;
return copy;
}
MethodDescriptorFlags withExtraDiscriminator(uint16_t value) const {
auto copy = *this;
copy.Value = (copy.Value & ~ExtraDiscriminatorMask)
| (int_type(value) << ExtraDiscriminatorShift);
return copy;
}
Kind getKind() const { return Kind(Value & KindMask); }
/// Is the method marked 'dynamic'?
bool isDynamic() const { return Value & IsDynamicMask; }
/// Is the method an instance member?
///
/// Note that 'init' is not considered an instance member.
bool isInstance() const { return Value & IsInstanceMask; }
bool isAsync() const { return Value & IsAsyncMask; }
uint16_t getExtraDiscriminator() const {
return (Value >> ExtraDiscriminatorShift);
}
int_type getIntValue() const { return Value; }
};
enum : unsigned {
/// Number of words reserved in generic metadata patterns.
NumGenericMetadataPrivateDataWords = 16,
};
/// Kinds of type metadata/protocol conformance records.
enum class TypeReferenceKind : unsigned {
/// The conformance is for a nominal type referenced directly;
/// getTypeDescriptor() points to the type context descriptor.
DirectTypeDescriptor = 0x00,
/// The conformance is for a nominal type referenced indirectly;
/// getTypeDescriptor() points to the type context descriptor.
IndirectTypeDescriptor = 0x01,
/// The conformance is for an Objective-C class that should be looked up
/// by class name.
DirectObjCClassName = 0x02,
/// The conformance is for an Objective-C class that has no nominal type
/// descriptor.
/// getIndirectObjCClass() points to a variable that contains the pointer to
/// the class object, which then requires a runtime call to get metadata.
///
/// On platforms without Objective-C interoperability, this case is
/// unused.
IndirectObjCClass = 0x03,
// We only reserve three bits for this in the various places we store it.
First_Kind = DirectTypeDescriptor,
Last_Kind = IndirectObjCClass,
};
/// Flag that indicates whether an existential type is class-constrained or not.
enum class ProtocolClassConstraint : bool {
/// The protocol is class-constrained, so only class types can conform to it.
///
/// This must be 0 for ABI compatibility with Objective-C protocol_t records.
Class = false,
/// Any type can conform to the protocol.
Any = true,
};
/// Identifiers for protocols with special meaning to the Swift runtime.
enum class SpecialProtocol: uint8_t {
/// Not a special protocol.
///
/// This must be 0 for ABI compatibility with Objective-C protocol_t records.
None = 0,
/// The Error protocol.
Error = 1,
};
/// Flags for protocol descriptors.
class ProtocolDescriptorFlags {
typedef uint32_t int_type;
enum : int_type {
IsSwift = 1U << 0U,
ClassConstraint = 1U << 1U,
DispatchStrategyMask = 0xFU << 2U,
DispatchStrategyShift = 2,
SpecialProtocolMask = 0x000003C0U,
SpecialProtocolShift = 6,
IsResilient = 1U << 10U,
/// Reserved by the ObjC runtime.
_ObjCReserved = 0xFFFF0000U,
};
int_type Data;
constexpr ProtocolDescriptorFlags(int_type Data) : Data(Data) {}
public:
constexpr ProtocolDescriptorFlags() : Data(0) {}
constexpr ProtocolDescriptorFlags withSwift(bool s) const {
return ProtocolDescriptorFlags((Data & ~IsSwift) | (s ? IsSwift : 0));
}
constexpr ProtocolDescriptorFlags withClassConstraint(
ProtocolClassConstraint c) const {
return ProtocolDescriptorFlags((Data & ~ClassConstraint)
| (bool(c) ? ClassConstraint : 0));
}
constexpr ProtocolDescriptorFlags withDispatchStrategy(
ProtocolDispatchStrategy s) const {
return ProtocolDescriptorFlags((Data & ~DispatchStrategyMask)
| (int_type(s) << DispatchStrategyShift));
}
constexpr ProtocolDescriptorFlags
withSpecialProtocol(SpecialProtocol sp) const {
return ProtocolDescriptorFlags((Data & ~SpecialProtocolMask)
| (int_type(sp) << SpecialProtocolShift));
}
constexpr ProtocolDescriptorFlags withResilient(bool s) const {
return ProtocolDescriptorFlags((Data & ~IsResilient) | (s ? IsResilient : 0));
}
/// Was the protocol defined in Swift 1 or 2?
bool isSwift() const { return Data & IsSwift; }
/// Is the protocol class-constrained?
ProtocolClassConstraint getClassConstraint() const {
return ProtocolClassConstraint(bool(Data & ClassConstraint));
}
/// What dispatch strategy does this protocol use?
ProtocolDispatchStrategy getDispatchStrategy() const {
return ProtocolDispatchStrategy((Data & DispatchStrategyMask)
>> DispatchStrategyShift);
}
/// Does the protocol require a witness table for method dispatch?
bool needsWitnessTable() const {
return swift::protocolRequiresWitnessTable(getDispatchStrategy());
}
/// Return the identifier if this is a special runtime-known protocol.
SpecialProtocol getSpecialProtocol() const {
return SpecialProtocol(uint8_t((Data & SpecialProtocolMask)
>> SpecialProtocolShift));
}
/// Can new requirements with default witnesses be added resiliently?
bool isResilient() const { return Data & IsResilient; }
int_type getIntValue() const {
return Data;
}
#ifndef NDEBUG
SWIFT_DEBUG_DUMP;
#endif
};
/// Flags that go in a ProtocolRequirement structure.
class ProtocolRequirementFlags {
public:
typedef uint32_t int_type;
enum class Kind {
BaseProtocol,
Method,
Init,
Getter,
Setter,
ReadCoroutine,
ModifyCoroutine,
AssociatedTypeAccessFunction,
AssociatedConformanceAccessFunction,
Read2Coroutine,
Modify2Coroutine,
};
private:
enum : int_type {
KindMask = 0x0F, // 16 kinds should be enough for anybody
IsInstanceMask = 0x10,
IsAsyncMask = 0x20,
ExtraDiscriminatorShift = 16,
ExtraDiscriminatorMask = 0xFFFF0000,
};
int_type Value;
public:
ProtocolRequirementFlags(Kind kind) : Value(unsigned(kind)) {}
ProtocolRequirementFlags withIsInstance(bool isInstance) const {
auto copy = *this;
if (isInstance) {
copy.Value |= IsInstanceMask;
} else {
copy.Value &= ~IsInstanceMask;
}
return copy;
}
ProtocolRequirementFlags withIsAsync(bool isAsync) const {
auto copy = *this;
if (isAsync)
copy.Value |= IsAsyncMask;
else
copy.Value &= ~IsAsyncMask;
return copy;
}
ProtocolRequirementFlags withExtraDiscriminator(uint16_t value) const {
auto copy = *this;
copy.Value = (copy.Value & ~ExtraDiscriminatorMask)
| (int_type(value) << ExtraDiscriminatorShift);
return copy;
}
Kind getKind() const { return Kind(Value & KindMask); }
/// Is the method an instance member?
///
/// Note that 'init' is not considered an instance member.
bool isInstance() const { return Value & IsInstanceMask; }
bool isAsync() const { return Value & IsAsyncMask; }
bool isSignedWithAddress() const {
return getKind() != Kind::BaseProtocol;
}
uint16_t getExtraDiscriminator() const {
return (Value >> ExtraDiscriminatorShift);
}
int_type getIntValue() const { return Value; }
/// Is the method implementation is represented as a native function pointer?
bool isFunctionImpl() const {
switch (getKind()) {
case ProtocolRequirementFlags::Kind::Method:
case ProtocolRequirementFlags::Kind::Init:
case ProtocolRequirementFlags::Kind::Getter:
case ProtocolRequirementFlags::Kind::Setter:
case ProtocolRequirementFlags::Kind::ReadCoroutine:
case ProtocolRequirementFlags::Kind::ModifyCoroutine:
return !isAsync();
default:
return false;
}
}
enum : uintptr_t {
/// Bit used to indicate that an associated type witness is a pointer to
/// a mangled name (vs. a pointer to metadata).
AssociatedTypeMangledNameBit = 0x01,
};
enum : uint8_t {
/// Prefix byte used to identify an associated type whose mangled name
/// is relative to the protocol's context rather than the conforming
/// type's context.
AssociatedTypeInProtocolContextByte = 0xFF
};
};
/// Flags that go in a TargetConformanceDescriptor structure.
class ConformanceFlags {
public:
typedef uint32_t int_type;
private:
enum : int_type {
UnusedLowBits = 0x07, // historical conformance kind
TypeMetadataKindMask = 0x7u << 3, // 8 type reference kinds
TypeMetadataKindShift = 3,
IsRetroactiveMask = 0x01u << 6,
IsSynthesizedNonUniqueMask = 0x01u << 7,
NumConditionalRequirementsMask = 0xFFu << 8,
NumConditionalRequirementsShift = 8,
HasResilientWitnessesMask = 0x01u << 16,
HasGenericWitnessTableMask = 0x01u << 17,
IsConformanceOfProtocolMask = 0x01u << 18,
NumConditionalPackDescriptorsMask = 0xFFu << 24,
NumConditionalPackDescriptorsShift = 24
};
int_type Value;
public:
ConformanceFlags(int_type value = 0) : Value(value) {}
ConformanceFlags withTypeReferenceKind(TypeReferenceKind kind) const {
return ConformanceFlags((Value & ~TypeMetadataKindMask)
| (int_type(kind) << TypeMetadataKindShift));
}
ConformanceFlags withIsRetroactive(bool isRetroactive) const {
return ConformanceFlags((Value & ~IsRetroactiveMask)
| (isRetroactive? IsRetroactiveMask : 0));
}
ConformanceFlags withIsSynthesizedNonUnique(
bool isSynthesizedNonUnique) const {
return ConformanceFlags(
(Value & ~IsSynthesizedNonUniqueMask)
| (isSynthesizedNonUnique ? IsSynthesizedNonUniqueMask : 0));
}
ConformanceFlags withNumConditionalRequirements(unsigned n) const {
return ConformanceFlags((Value & ~NumConditionalRequirementsMask)
| (n << NumConditionalRequirementsShift));
}
ConformanceFlags withNumConditionalPackDescriptors(unsigned n) const {
return ConformanceFlags((Value & ~NumConditionalPackDescriptorsMask)
| (n << NumConditionalPackDescriptorsShift));
}
ConformanceFlags withHasResilientWitnesses(bool hasResilientWitnesses) const {
return ConformanceFlags((Value & ~HasResilientWitnessesMask)
| (hasResilientWitnesses? HasResilientWitnessesMask
: 0));
}
ConformanceFlags withHasGenericWitnessTable(
bool hasGenericWitnessTable) const {
return ConformanceFlags((Value & ~HasGenericWitnessTableMask)
| (hasGenericWitnessTable
? HasGenericWitnessTableMask
: 0));
}
ConformanceFlags withIsConformanceOfProtocol(
bool isConformanceOfProtocol) const {
return ConformanceFlags((Value & ~IsConformanceOfProtocolMask)
| (isConformanceOfProtocol
? IsConformanceOfProtocolMask
: 0));
}
/// Retrieve the type reference kind kind.
TypeReferenceKind getTypeReferenceKind() const {
return TypeReferenceKind(
(Value & TypeMetadataKindMask) >> TypeMetadataKindShift);
}
/// Is the conformance "retroactive"?
///
/// A conformance is retroactive when it occurs in a module that is
/// neither the module in which the protocol is defined nor the module
/// in which the conforming type is defined. With retroactive conformance,
/// it is possible to detect a conflict at run time.
bool isRetroactive() const { return Value & IsRetroactiveMask; }
/// Is the conformance synthesized in a non-unique manner?
///
/// The Swift compiler will synthesize conformances on behalf of some
/// imported entities (e.g., C typedefs with the swift_wrapper attribute).
/// Such conformances are retroactive by nature, but the presence of multiple
/// such conformances is not a conflict because all synthesized conformances
/// will be equivalent.
bool isSynthesizedNonUnique() const {
return Value & IsSynthesizedNonUniqueMask;
}
/// Is this a conformance of a protocol to another protocol?
///
/// The Swift compiler can synthesize a conformance of one protocol to
/// another, meaning that every type that conforms to the first protocol
/// can also produce a witness table conforming to the second. Such
/// conformances cannot generally be written in the surface language, but
/// can be made available for specific tasks. The only such instance at the
/// time of this writing is that a (local) distributed actor can conform to
/// a local actor, but the witness table can only be used via a specific
/// builtin to form an existential.
bool isConformanceOfProtocol() const {
return Value & IsConformanceOfProtocolMask;
}
/// Retrieve the # of conditional requirements.
unsigned getNumConditionalRequirements() const {
return (Value & NumConditionalRequirementsMask)
>> NumConditionalRequirementsShift;
}
/// Retrieve the # of conditional pack shape descriptors.
unsigned getNumConditionalPackShapeDescriptors() const {
return (Value & NumConditionalPackDescriptorsMask)
>> NumConditionalPackDescriptorsShift;
}
/// Whether this conformance has any resilient witnesses.
bool hasResilientWitnesses() const {
return Value & HasResilientWitnessesMask;
}
/// Whether this conformance has a generic witness table that may need to
/// be instantiated.
bool hasGenericWitnessTable() const {
return Value & HasGenericWitnessTableMask;
}
int_type getIntValue() const { return Value; }
};
/// Flags in an existential type metadata record.
class ExistentialTypeFlags {
public:
typedef uint32_t int_type;
private:
enum : int_type {
NumWitnessTablesMask = 0x00FFFFFFU,
ClassConstraintMask = 0x80000000U, // Warning: Set if NOT class-constrained!
HasSuperclassMask = 0x40000000U,
SpecialProtocolMask = 0x3F000000U,
SpecialProtocolShift = 24U,
};
int_type Data;
public:
constexpr ExistentialTypeFlags(int_type Data) : Data(Data) {}
constexpr ExistentialTypeFlags() : Data(0) {}
constexpr ExistentialTypeFlags withNumWitnessTables(unsigned numTables) const {
return ExistentialTypeFlags((Data & ~NumWitnessTablesMask) | numTables);
}
constexpr ExistentialTypeFlags
withClassConstraint(ProtocolClassConstraint c) const {
return ExistentialTypeFlags((Data & ~ClassConstraintMask)
| (bool(c) ? ClassConstraintMask : 0));
}
constexpr ExistentialTypeFlags
withHasSuperclass(bool hasSuperclass) const {
return ExistentialTypeFlags((Data & ~HasSuperclassMask)
| (hasSuperclass ? HasSuperclassMask : 0));
}
constexpr ExistentialTypeFlags
withSpecialProtocol(SpecialProtocol sp) const {
return ExistentialTypeFlags((Data & ~SpecialProtocolMask)
| (int_type(sp) << SpecialProtocolShift));
}
unsigned getNumWitnessTables() const {
return Data & NumWitnessTablesMask;
}
ProtocolClassConstraint getClassConstraint() const {
return ProtocolClassConstraint(bool(Data & ClassConstraintMask));
}
bool hasSuperclassConstraint() const {
return bool(Data & HasSuperclassMask);
}
/// Return whether this existential type represents an uncomposed special
/// protocol.
SpecialProtocol getSpecialProtocol() const {
return SpecialProtocol(uint8_t((Data & SpecialProtocolMask)
>> SpecialProtocolShift));
}
int_type getIntValue() const {
return Data;
}
};
/// Flags in an extended existential shape.
class ExtendedExistentialTypeShapeFlags {
public:
typedef uint32_t int_type;
/// Special cases for the representation.
enum class SpecialKind {
None = 0,
/// The existential has a class constraint.
/// The inline storage is sizeof(void*) / alignof(void*),
/// the value is always stored inline, the value is reference-
/// counted (using unknown reference counting), and the
/// type metadata for the requirement generic parameters are
/// not stored in the existential container because they can
/// be recovered from the instance type of the class.
Class = 1,
/// The existential has a metatype constraint.
/// The inline storage is sizeof(void*) / alignof(void*),
/// the value is always stored inline, the value is a Metadata*,
/// and the type metadata for the requirement generic parameters
/// are not stored in the existential container because they can
/// be recovered from the stored metatype.
Metatype = 2,
/// The inline value storage has a non-storage layout. The shape
/// must include a value witness table. Type metadata for the
/// requirement generic parameters are still stored in the existential
/// container.
ExplicitLayout = 3,
// 255 is the maximum
};
private:
enum : int_type {
SpecialKindMask = 0x000000FFU,
SpecialKindShift = 0,
HasGeneralizationSignature = 0x00000100U,
HasTypeExpression = 0x00000200U,
HasSuggestedValueWitnesses = 0x00000400U,
HasImplicitReqSigParams = 0x00000800U,
HasImplicitGenSigParams = 0x00001000U,
HasTypePacks = 0x00002000U,
};
int_type Data;
public:
constexpr ExtendedExistentialTypeShapeFlags() : Data(0) {}
constexpr ExtendedExistentialTypeShapeFlags(int_type Data) : Data(Data) {}
constexpr ExtendedExistentialTypeShapeFlags
withSpecialKind(SpecialKind kind) const {
return ExtendedExistentialTypeShapeFlags(
(Data & ~SpecialKindMask) | (int_type(kind) << SpecialKindShift));
}
constexpr ExtendedExistentialTypeShapeFlags
withHasTypeExpression(bool hasTypeExpression) const {
return ExtendedExistentialTypeShapeFlags(
hasTypeExpression ? (Data | HasTypeExpression)
: (Data & ~HasTypeExpression));
}
constexpr ExtendedExistentialTypeShapeFlags
withGeneralizationSignature(bool hasGeneralization) const {
return ExtendedExistentialTypeShapeFlags(
hasGeneralization ? (Data | HasGeneralizationSignature)
: (Data & ~HasGeneralizationSignature));
}
constexpr ExtendedExistentialTypeShapeFlags
withSuggestedValueWitnesses(bool hasSuggestedVWT) const {
return ExtendedExistentialTypeShapeFlags(
hasSuggestedVWT ? (Data | HasSuggestedValueWitnesses)
: (Data & ~HasSuggestedValueWitnesses));
}
constexpr ExtendedExistentialTypeShapeFlags
withImplicitReqSigParams(bool implicit) const {
return ExtendedExistentialTypeShapeFlags(
implicit ? (Data | HasImplicitReqSigParams)
: (Data & ~HasImplicitReqSigParams));
}
constexpr ExtendedExistentialTypeShapeFlags
withImplicitGenSigParams(bool implicit) const {
return ExtendedExistentialTypeShapeFlags(
implicit ? (Data | HasImplicitGenSigParams)
: (Data & ~HasImplicitGenSigParams));
}
constexpr ExtendedExistentialTypeShapeFlags
withTypePacks(bool hasTypePacks) const {
return ExtendedExistentialTypeShapeFlags(
hasTypePacks ? (Data | HasTypePacks)
: (Data & ~HasTypePacks));
}
/// Is this a special kind of existential?
SpecialKind getSpecialKind() const {
return SpecialKind((Data & SpecialKindMask) >> SpecialKindShift);
}
bool isOpaque() const { return getSpecialKind() == SpecialKind::None; }
bool isClassConstrained() const {
return getSpecialKind() == SpecialKind::Class;
}
bool isMetatypeConstrained() const {
return getSpecialKind() == SpecialKind::Metatype;
}