@@ -39,52 +39,18 @@ template <typename Runtime>
39
39
using SharedProtocolDescriptorRef
40
40
= std::shared_ptr<TargetProtocolDescriptor<Runtime>>;
41
41
42
- // / A generic reader of metadata.
43
- // /
44
- // / BuilderType must implement a particular interface which is currently
45
- // / too fluid to allow useful documentation; consult the actual
46
- // / implementations. The chief thing is that it provides several member
47
- // / types which should obey the following constraints:
48
- // / - T() yields a value which is false when contextually converted to bool
49
- // / - a false value signals that an error occurred when building a value
50
- template <typename Runtime, typename BuilderType>
51
- class MetadataReader {
52
- public:
42
+ // / A utility class for constructing abstract types from
43
+ // / a textual mangling.
44
+ template <typename BuilderType>
45
+ class TypeDecoder {
53
46
using Type = typename BuilderType::Type;
54
- using StoredPointer = typename Runtime::StoredPointer;
55
- using StoredSize = typename Runtime::StoredSize;
47
+ using NodeKind = Demangle::Node::Kind;
56
48
57
- private:
58
- std::unordered_map<StoredPointer, Type> TypeCache;
59
- std::unordered_map<StoredPointer, SharedTargetMetadataRef<Runtime>>
60
- MetadataCache;
61
-
62
- std::unordered_map<StoredPointer,
63
- std::pair<SharedTargetNominalTypeDescriptorRef<Runtime>,
64
- StoredPointer>>
65
- NominalTypeDescriptorCache;
49
+ BuilderType &Builder;
66
50
67
- public:
68
- BuilderType Builder;
69
-
70
- std::shared_ptr<MemoryReader> Reader;
71
-
72
- template <class ... T>
73
- MetadataReader (std::shared_ptr<MemoryReader> reader, T &&... args)
74
- : Builder(std::forward<T>(args)...),
75
- Reader (std::move(reader)) {
76
-
77
- }
78
-
79
- MetadataReader (const MetadataReader &other) = delete;
80
- MetadataReader &operator =(const MetadataReader &other) = delete ;
81
-
82
- // / Clear all of the caches in this reader.
83
- void clear () {
84
- TypeCache.clear ();
85
- MetadataCache.clear ();
86
- NominalTypeDescriptorCache.clear ();
87
- }
51
+ public:
52
+ explicit TypeDecoder (BuilderType &Builder)
53
+ : Builder(Builder) {}
88
54
89
55
// / Given a demangle tree, attempt to turn it into a type.
90
56
Type decodeMangledType (const Demangle::NodePointer &Node) {
@@ -257,11 +223,11 @@ class MetadataReader {
257
223
return Type ();
258
224
}
259
225
}
226
+
260
227
private:
261
228
bool decodeMangledNominalType (const Demangle::NodePointer &node,
262
229
std::string &mangledName,
263
230
Type &parent) {
264
- using NodeKind = Demangle::Node::Kind;
265
231
if (node->getKind () == NodeKind::Type)
266
232
return decodeMangledNominalType (node->getChild (0 ), mangledName, parent);
267
233
@@ -286,8 +252,6 @@ class MetadataReader {
286
252
std::vector<Type> &args,
287
253
std::vector<bool > &argsAreInOut,
288
254
FunctionTypeFlags &flags) {
289
- using NodeKind = Demangle::Node::Kind;
290
-
291
255
// Look through a couple of sugar nodes.
292
256
if (node->getKind () == NodeKind::Type ||
293
257
node->getKind () == NodeKind::ArgumentTuple) {
@@ -335,8 +299,67 @@ class MetadataReader {
335
299
// Otherwise, handle the type as a single argument.
336
300
return decodeSingle (node);
337
301
}
302
+ };
303
+
304
+ template <typename BuilderType>
305
+ static inline typename BuilderType::Type
306
+ decodeMangledType (BuilderType &Builder,
307
+ const Demangle::NodePointer &Node) {
308
+ return TypeDecoder<BuilderType>(Builder).decodeMangledType (Node);
309
+ }
310
+
311
+ // / A generic reader of metadata.
312
+ // /
313
+ // / BuilderType must implement a particular interface which is currently
314
+ // / too fluid to allow useful documentation; consult the actual
315
+ // / implementations. The chief thing is that it provides several member
316
+ // / types which should obey the following constraints:
317
+ // / - T() yields a value which is false when contextually converted to bool
318
+ // / - a false value signals that an error occurred when building a value
319
+ template <typename Runtime, typename BuilderType>
320
+ class MetadataReader {
321
+ public:
322
+ using Type = typename BuilderType::Type;
323
+ using StoredPointer = typename Runtime::StoredPointer;
324
+ using StoredSize = typename Runtime::StoredSize;
325
+
326
+ private:
327
+ std::unordered_map<StoredPointer, Type> TypeCache;
328
+ std::unordered_map<StoredPointer, SharedTargetMetadataRef<Runtime>>
329
+ MetadataCache;
330
+
331
+ std::unordered_map<StoredPointer,
332
+ std::pair<SharedTargetNominalTypeDescriptorRef<Runtime>,
333
+ StoredPointer>>
334
+ NominalTypeDescriptorCache;
338
335
339
336
public:
337
+ BuilderType Builder;
338
+
339
+ std::shared_ptr<MemoryReader> Reader;
340
+
341
+ template <class ... T>
342
+ MetadataReader (std::shared_ptr<MemoryReader> reader, T &&... args)
343
+ : Builder(std::forward<T>(args)...),
344
+ Reader (std::move(reader)) {
345
+
346
+ }
347
+
348
+ MetadataReader (const MetadataReader &other) = delete;
349
+ MetadataReader &operator =(const MetadataReader &other) = delete ;
350
+
351
+ // / Clear all of the caches in this reader.
352
+ void clear () {
353
+ TypeCache.clear ();
354
+ MetadataCache.clear ();
355
+ NominalTypeDescriptorCache.clear ();
356
+ }
357
+
358
+ // / Given a demangle tree, attempt to turn it into a type.
359
+ Type decodeMangledType (const Demangle::NodePointer &Node) {
360
+ return swift::remote::decodeMangledType (Builder, Node);
361
+ }
362
+
340
363
// / Given a remote pointer to metadata, attempt to turn it into a type.
341
364
Type readTypeFromMetadata (StoredPointer MetadataAddress) {
342
365
auto Cached = TypeCache.find (MetadataAddress);
0 commit comments