@@ -36,24 +36,41 @@ namespace wabt {
36
36
37
37
struct Module ;
38
38
39
- enum class VarType {
39
+ enum class VarType : uint16_t {
40
40
Index,
41
41
Name,
42
42
};
43
43
44
44
struct Var {
45
+ // Var can represent variables or types.
46
+
47
+ // Represent a variable:
48
+ // has_opt_type() is false
49
+ // Only used by wast-parser
50
+
51
+ // Represent a type:
52
+ // has_opt_type() is true, is_index() is true
53
+ // type can be get by to_type()
54
+ // Binary reader only constructs this variant
55
+
56
+ // Represent both a variable and a type:
57
+ // has_opt_type() is true, is_name() is true
58
+ // A reference, which index is unknown
59
+ // Only used by wast-parser
60
+
45
61
explicit Var ();
46
62
explicit Var (Index index, const Location& loc);
47
63
explicit Var (std::string_view name, const Location& loc);
64
+ explicit Var (Type type, const Location& loc);
48
65
Var (Var&&);
49
66
Var (const Var&);
50
67
Var& operator =(const Var&);
51
68
Var& operator =(Var&&);
52
69
~Var ();
53
70
54
- VarType type () const { return type_; }
55
71
bool is_index () const { return type_ == VarType::Index; }
56
72
bool is_name () const { return type_ == VarType::Name; }
73
+ bool has_opt_type () const { return opt_type_ < 0 ; }
57
74
58
75
Index index () const {
59
76
assert (is_index ());
@@ -63,17 +80,25 @@ struct Var {
63
80
assert (is_name ());
64
81
return name_;
65
82
}
83
+ Type::Enum opt_type () const {
84
+ assert (has_opt_type ());
85
+ return static_cast <Type::Enum>(opt_type_);
86
+ }
66
87
67
88
void set_index (Index);
68
89
void set_name (std::string&&);
69
90
void set_name (std::string_view);
91
+ void set_opt_type (Type::Enum);
92
+ Type to_type () const ;
70
93
71
94
Location loc;
72
95
73
96
private:
74
97
void Destroy ();
75
98
76
99
VarType type_;
100
+ // Can be set to Type::Enum types, 0 represent no optional type.
101
+ int16_t opt_type_;
77
102
union {
78
103
Index index_;
79
104
std::string name_;
@@ -544,10 +569,10 @@ using MemoryCopyExpr = MemoryBinaryExpr<ExprType::MemoryCopy>;
544
569
template <ExprType TypeEnum>
545
570
class RefTypeExpr : public ExprMixin <TypeEnum> {
546
571
public:
547
- RefTypeExpr (Type type, const Location& loc = Location())
572
+ RefTypeExpr (Var type, const Location& loc = Location())
548
573
: ExprMixin<TypeEnum>(loc), type(type) {}
549
574
550
- Type type;
575
+ Var type;
551
576
};
552
577
553
578
using RefNullExpr = RefTypeExpr<ExprType::RefNull>;
@@ -734,9 +759,7 @@ class CallRefExpr : public ExprMixin<ExprType::CallRef> {
734
759
explicit CallRefExpr (const Location& loc = Location())
735
760
: ExprMixin<ExprType::CallRef>(loc) {}
736
761
737
- // This field is setup only during Validate phase,
738
- // so keep that in mind when you use it.
739
- Var function_type_index;
762
+ Var sig_type;
740
763
};
741
764
742
765
template <ExprType TypeEnum>
@@ -924,6 +947,8 @@ struct Func {
924
947
925
948
std::string name;
926
949
FuncDeclaration decl;
950
+ // Contains references with unknown indicies.
951
+ TypeVector local_type_list;
927
952
LocalTypes local_types;
928
953
BindingHash bindings;
929
954
ExprList exprs;
@@ -941,7 +966,7 @@ struct Global {
941
966
explicit Global (std::string_view name) : name(name) {}
942
967
943
968
std::string name;
944
- Type type = Type::Void ;
969
+ Var type;
945
970
bool mutable_ = false ;
946
971
ExprList init_expr;
947
972
};
0 commit comments