31
31
32
32
namespace wasm {
33
33
34
- struct Tuple ;
35
- struct Signature ;
36
- struct Struct ;
37
- struct Array ;
38
-
39
- typedef std::vector<class Type > TypeList;
40
-
41
34
class Type {
42
35
// The `id` uniquely represents each type, so type equality is just a
43
36
// comparison of the ids. For basic types the `id` is just the `BasicID`
@@ -73,16 +66,14 @@ class Type {
73
66
Type (std::initializer_list<Type>);
74
67
75
68
// Construct from tuple description
76
- explicit Type (const Tuple&);
77
-
78
- // Construct from signature description
79
- explicit Type (const Signature, bool nullable);
69
+ explicit Type (const struct Tuple &);
80
70
81
- // Construct from struct description
82
- explicit Type (const Struct&, bool nullable);
71
+ // Construct from a heap type description. Also covers construction from
72
+ // Signature, Struct or Array via implicit conversion to HeapType.
73
+ explicit Type (const struct HeapType &, bool nullable);
83
74
84
- // Construct from array description
85
- explicit Type (const Array&, bool nullable );
75
+ // Construct from rtt description
76
+ explicit Type (const struct Rtt & );
86
77
87
78
// Predicates
88
79
// Compound Concrete
@@ -96,16 +87,18 @@ class Type {
96
87
// │ f32 ║ x │ │ x │ x │ F │ │ F_loat
97
88
// │ f64 ║ x │ │ x │ x │ F │ │ V_ector
98
89
// │ v128 ║ x │ │ x │ x │ V │ ┘
99
- // ├─────────────╫───┼───┼───┼───┤───────┤
100
- // │ funcref ║ x │ │ x │ x │ f │ ┐ Ref
101
- // │ externref ║ x │ │ x │ x │ │ │ f_unc
102
- // │ nullref ║ x │ │ x │ x │ │ │
103
- // │ exnref ║ x │ │ x │ x │ │ │
104
- // ├─────────────╫───┼───┼───┼───┤───────┤ │
105
- // │ Signature ║ │ x │ x │ x │ f │ │
106
- // │ Struct ║ │ x │ x │ x │ │ │
107
- // │ Array ║ │ x │ x │ x │ │ ┘
90
+ // ├─ Aliases ───╫───┼───┼───┼───┤───────┤
91
+ // │ funcref ║ x │ │ x │ x │ f n │ ┐ Ref
92
+ // │ externref ║ x │ │ x │ x │ f? n │ │ f_unc, n_ullable
93
+ // │ anyref ║ x │ │ x │ x │ f? n │ │ ┐
94
+ // │ eqref ║ x │ │ x │ x │ n │ │ │ TODO (GC)
95
+ // │ i31ref ║ x │ │ x │ x │ │ │ ┘
96
+ // │ nullref ║ x │ │ x │ x │ f? n │ │ ◄ TODO (removed)
97
+ // │ exnref ║ x │ │ x │ x │ n │ │
98
+ // ├─ Compound ──╫───┼───┼───┼───┤───────┤ │
99
+ // │ Ref ║ │ x │ x │ x │ f? n? │◄┘
108
100
// │ Tuple ║ │ x │ │ x │ │
101
+ // │ Rtt ║ │ x │ x │ x │ │
109
102
// └─────────────╨───┴───┴───┴───┴───────┘
110
103
constexpr bool isBasic () const { return id <= _last_basic_id; }
111
104
constexpr bool isCompound () const { return id > _last_basic_id; }
@@ -118,6 +111,7 @@ class Type {
118
111
bool isSingle () const { return isConcrete () && !isTuple (); }
119
112
bool isRef () const ;
120
113
bool isNullable () const ;
114
+ bool isRtt () const ;
121
115
122
116
private:
123
117
template <bool (Type::*pred)() const > bool hasPredicate () {
@@ -227,6 +221,8 @@ struct ResultType {
227
221
std::string toString () const ;
228
222
};
229
223
224
+ typedef std::vector<Type> TypeList;
225
+
230
226
struct Tuple {
231
227
TypeList types;
232
228
Tuple () : types() {}
@@ -303,6 +299,72 @@ struct Array {
303
299
std::string toString () const ;
304
300
};
305
301
302
+ struct HeapType {
303
+ enum Kind {
304
+ FuncKind,
305
+ ExternKind,
306
+ AnyKind,
307
+ EqKind,
308
+ I31Kind,
309
+ ExnKind,
310
+ _last_basic_kind = ExnKind,
311
+ SignatureKind,
312
+ StructKind,
313
+ ArrayKind,
314
+ } kind;
315
+ union {
316
+ Signature signature;
317
+ Struct struct_;
318
+ Array array;
319
+ };
320
+ HeapType (Kind kind) : kind(kind) { assert (kind <= _last_basic_kind); }
321
+ HeapType (const Signature& signature)
322
+ : kind(SignatureKind), signature(signature) {}
323
+ HeapType (Signature&& signature)
324
+ : kind(SignatureKind), signature(std::move(signature)) {}
325
+ HeapType (const Struct& struct_) : kind(StructKind), struct_(struct_) {}
326
+ HeapType (Struct&& struct_) : kind(StructKind), struct_(std::move(struct_)) {}
327
+ HeapType (const Array& array) : kind(ArrayKind), array(array) {}
328
+ HeapType (Array&& array) : kind(ArrayKind), array(std::move(array)) {}
329
+ HeapType (const HeapType& other);
330
+ ~HeapType ();
331
+
332
+ bool isSignature () const { return kind == SignatureKind; }
333
+ Signature getSignature () const {
334
+ assert (isSignature () && " Not a signature" );
335
+ return signature;
336
+ }
337
+ bool isStruct () const { return kind == StructKind; }
338
+ Struct getStruct () const {
339
+ assert (isStruct () && " Not a struct" );
340
+ return struct_;
341
+ }
342
+ bool isArray () const { return kind == ArrayKind; }
343
+ Array getArray () const {
344
+ assert (isArray () && " Not an array" );
345
+ return array;
346
+ }
347
+
348
+ bool operator ==(const HeapType& other) const ;
349
+ bool operator !=(const HeapType& other) const { return !(*this == other); }
350
+ HeapType& operator =(const HeapType& other);
351
+ std::string toString () const ;
352
+ };
353
+
354
+ struct Rtt {
355
+ uint32_t depth;
356
+ HeapType heapType;
357
+ Rtt (uint32_t depth, const HeapType& heapType)
358
+ : depth(depth), heapType(heapType) {}
359
+ Rtt (uint32_t depth, HeapType&& heapType)
360
+ : depth(depth), heapType(std::move(heapType)) {}
361
+ bool operator ==(const Rtt& other) const {
362
+ return depth == other.depth && heapType == other.heapType ;
363
+ }
364
+ bool operator !=(const Rtt& other) const { return !(*this == other); }
365
+ std::string toString () const ;
366
+ };
367
+
306
368
std::ostream& operator <<(std::ostream&, Type);
307
369
std::ostream& operator <<(std::ostream&, ParamType);
308
370
std::ostream& operator <<(std::ostream&, ResultType);
@@ -311,6 +373,8 @@ std::ostream& operator<<(std::ostream&, Signature);
311
373
std::ostream& operator <<(std::ostream&, Field);
312
374
std::ostream& operator <<(std::ostream&, Struct);
313
375
std::ostream& operator <<(std::ostream&, Array);
376
+ std::ostream& operator <<(std::ostream&, HeapType);
377
+ std::ostream& operator <<(std::ostream&, Rtt);
314
378
315
379
} // namespace wasm
316
380
@@ -340,6 +404,14 @@ template<> class hash<wasm::Array> {
340
404
public:
341
405
size_t operator ()(const wasm::Array&) const ;
342
406
};
407
+ template <> class hash <wasm::HeapType> {
408
+ public:
409
+ size_t operator ()(const wasm::HeapType&) const ;
410
+ };
411
+ template <> class hash <wasm::Rtt> {
412
+ public:
413
+ size_t operator ()(const wasm::Rtt&) const ;
414
+ };
343
415
344
416
} // namespace std
345
417
0 commit comments