Skip to content

Commit cdd1c6d

Browse files
committed
Runtime: Move MetadataOrPack to Private.h
1 parent ae2c3a5 commit cdd1c6d

File tree

3 files changed

+80
-72
lines changed

3 files changed

+80
-72
lines changed

stdlib/public/runtime/Casting.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,35 @@ std::string swift::nameForMetadata(const Metadata *type,
122122
return result;
123123
}
124124

125+
std::string MetadataOrPack::nameForMetadata() const {
126+
if (isNull())
127+
return "<<nullptr>>";
128+
129+
if (isMetadata())
130+
return ::nameForMetadata(getMetadata());
131+
132+
std::string result = "Pack{";
133+
MetadataPackPointer pack = getMetadataPack();
134+
for (size_t i = 0, e = pack.getNumElements(); i < e; ++i) {
135+
if (i != 0)
136+
result += ", ";
137+
result += ::nameForMetadata(pack.getElements()[i]);
138+
}
139+
result += "}";
140+
141+
return result;
142+
}
143+
125144
#else // SWIFT_STDLIB_HAS_TYPE_PRINTING
126145

127146
std::string swift::nameForMetadata(const Metadata *type, bool qualified) {
128147
return "<<< type printer not available >>>";
129148
}
130149

150+
std::string MetadataOrPack::nameForMetadata() const {
151+
return "<<< type printer not available >>>";
152+
}
153+
131154
#endif // SWIFT_STDLIB_HAS_TYPE_PRINTING
132155

133156
/// Used as part of cache key for `TypeNameCache`.

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,77 +1110,6 @@ getLocalGenericParams(const ContextDescriptor *context) {
11101110

11111111
namespace {
11121112

1113-
/// A pointer to type metadata or a heap-allocated metadata pack.
1114-
struct MetadataOrPack {
1115-
const void *Ptr;
1116-
1117-
MetadataOrPack() : Ptr(nullptr) {}
1118-
1119-
explicit MetadataOrPack(const Metadata *ptr) : Ptr(ptr) {}
1120-
1121-
explicit MetadataOrPack(MetadataResponse response) : Ptr(response.Value) {}
1122-
1123-
explicit MetadataOrPack(MetadataPackPointer ptr) : Ptr(ptr.getPointer()) {
1124-
if (ptr.getLifetime() != PackLifetime::OnHeap)
1125-
fatalError(0, "Cannot have an on-stack pack here\n");
1126-
}
1127-
1128-
explicit operator bool() const { return Ptr != nullptr; }
1129-
1130-
bool isNull() const {
1131-
return !Ptr;
1132-
}
1133-
1134-
bool isMetadataOrNull() const {
1135-
return (reinterpret_cast<uintptr_t>(Ptr) & 1) == 0;
1136-
}
1137-
1138-
bool isMetadata() const {
1139-
return Ptr && isMetadataOrNull();
1140-
}
1141-
1142-
bool isMetadataPack() const {
1143-
return Ptr && (reinterpret_cast<uintptr_t>(Ptr) & 1) == 1;
1144-
}
1145-
1146-
const Metadata *getMetadata() const {
1147-
if (isMetadata())
1148-
return reinterpret_cast<const Metadata *>(Ptr);
1149-
fatalError(0, "Expected metadata but got a metadata pack\n");
1150-
}
1151-
1152-
const Metadata *getMetadataOrNull() const {
1153-
if (isMetadataOrNull())
1154-
return reinterpret_cast<const Metadata *>(Ptr);
1155-
fatalError(0, "Expected metadata but got a metadata pack\n");
1156-
}
1157-
1158-
MetadataPackPointer getMetadataPack() const {
1159-
if (isMetadataPack())
1160-
return MetadataPackPointer(Ptr);
1161-
fatalError(0, "Expected a metadata pack but got metadata\n");
1162-
}
1163-
1164-
std::string nameForMetadata() const {
1165-
if (isNull())
1166-
return "<<nullptr>>";
1167-
1168-
if (isMetadata())
1169-
return ::nameForMetadata(getMetadata());
1170-
1171-
std::string result = "Pack{";
1172-
MetadataPackPointer pack = getMetadataPack();
1173-
for (size_t i = 0, e = pack.getNumElements(); i < e; ++i) {
1174-
if (i != 0)
1175-
result += ", ";
1176-
result += ::nameForMetadata(pack.getElements()[i]);
1177-
}
1178-
result += "}";
1179-
1180-
return result;
1181-
}
1182-
};
1183-
11841113
/// Function object that produces substitutions for the generic parameters
11851114
/// that occur within a mangled name, using the complete set of generic
11861115
/// arguments "as written".

stdlib/public/runtime/Private.h

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,70 @@ class TypeInfo {
282282

283283
/// Callback used to provide the substitution of a generic parameter
284284
/// (described by depth/index) to its metadata.
285+
///
286+
/// The return type here is a lie; it's actually a MetadataOrPack.
285287
using SubstGenericParameterFn =
286-
std::function<const Metadata *(unsigned depth, unsigned index)>;
288+
std::function<const void *(unsigned depth, unsigned index)>;
287289

288290
/// Callback used to provide the substitution of a witness table based on
289291
/// its index into the enclosing generic environment.
290292
using SubstDependentWitnessTableFn =
291293
std::function<const WitnessTable *(const Metadata *type, unsigned index)>;
292294

295+
/// A pointer to type metadata or a heap-allocated metadata pack.
296+
struct SWIFT_RUNTIME_LIBRARY_VISIBILITY MetadataOrPack {
297+
const void *Ptr;
298+
299+
MetadataOrPack() : Ptr(nullptr) {}
300+
301+
explicit MetadataOrPack(const void *ptr) : Ptr(ptr) {}
302+
303+
explicit MetadataOrPack(MetadataResponse response) : Ptr(response.Value) {}
304+
305+
explicit MetadataOrPack(MetadataPackPointer ptr) : Ptr(ptr.getPointer()) {
306+
if (ptr.getLifetime() != PackLifetime::OnHeap)
307+
fatalError(0, "Cannot have an on-stack pack here\n");
308+
}
309+
310+
explicit operator bool() const { return Ptr != nullptr; }
311+
312+
bool isNull() const {
313+
return !Ptr;
314+
}
315+
316+
bool isMetadataOrNull() const {
317+
return (reinterpret_cast<uintptr_t>(Ptr) & 1) == 0;
318+
}
319+
320+
bool isMetadata() const {
321+
return Ptr && isMetadataOrNull();
322+
}
323+
324+
bool isMetadataPack() const {
325+
return Ptr && (reinterpret_cast<uintptr_t>(Ptr) & 1) == 1;
326+
}
327+
328+
const Metadata *getMetadata() const {
329+
if (isMetadata())
330+
return reinterpret_cast<const Metadata *>(Ptr);
331+
fatalError(0, "Expected metadata but got a metadata pack\n");
332+
}
333+
334+
const Metadata *getMetadataOrNull() const {
335+
if (isMetadataOrNull())
336+
return reinterpret_cast<const Metadata *>(Ptr);
337+
fatalError(0, "Expected metadata but got a metadata pack\n");
338+
}
339+
340+
MetadataPackPointer getMetadataPack() const {
341+
if (isMetadataPack())
342+
return MetadataPackPointer(Ptr);
343+
fatalError(0, "Expected a metadata pack but got metadata\n");
344+
}
345+
346+
std::string nameForMetadata() const;
347+
};
348+
293349
/// Function object that produces substitutions for the generic parameters
294350
/// that occur within a mangled name, using the generic arguments from
295351
/// the given metadata.

0 commit comments

Comments
 (0)