@@ -36,24 +36,41 @@ namespace wabt {
3636
3737struct Module ;
3838
39- enum class VarType {
39+ enum class VarType : uint16_t {
4040 Index,
4141 Name,
4242};
4343
4444struct 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+
4561 explicit Var ();
4662 explicit Var (Index index, const Location& loc);
4763 explicit Var (std::string_view name, const Location& loc);
64+ explicit Var (Type type, const Location& loc);
4865 Var (Var&&);
4966 Var (const Var&);
5067 Var& operator =(const Var&);
5168 Var& operator =(Var&&);
5269 ~Var ();
5370
54- VarType type () const { return type_; }
5571 bool is_index () const { return type_ == VarType::Index; }
5672 bool is_name () const { return type_ == VarType::Name; }
73+ bool has_opt_type () const { return opt_type_ < 0 ; }
5774
5875 Index index () const {
5976 assert (is_index ());
@@ -63,17 +80,25 @@ struct Var {
6380 assert (is_name ());
6481 return name_;
6582 }
83+ Type::Enum opt_type () const {
84+ assert (has_opt_type ());
85+ return static_cast <Type::Enum>(opt_type_);
86+ }
6687
6788 void set_index (Index);
6889 void set_name (std::string&&);
6990 void set_name (std::string_view);
91+ void set_opt_type (Type::Enum);
92+ Type to_type () const ;
7093
7194 Location loc;
7295
7396 private:
7497 void Destroy ();
7598
7699 VarType type_;
100+ // Can be set to Type::Enum types, 0 represent no optional type.
101+ int16_t opt_type_;
77102 union {
78103 Index index_;
79104 std::string name_;
@@ -544,10 +569,10 @@ using MemoryCopyExpr = MemoryBinaryExpr<ExprType::MemoryCopy>;
544569template <ExprType TypeEnum>
545570class RefTypeExpr : public ExprMixin <TypeEnum> {
546571 public:
547- RefTypeExpr (Type type, const Location& loc = Location())
572+ RefTypeExpr (Var type, const Location& loc = Location())
548573 : ExprMixin<TypeEnum>(loc), type(type) {}
549574
550- Type type;
575+ Var type;
551576};
552577
553578using RefNullExpr = RefTypeExpr<ExprType::RefNull>;
@@ -734,9 +759,7 @@ class CallRefExpr : public ExprMixin<ExprType::CallRef> {
734759 explicit CallRefExpr (const Location& loc = Location())
735760 : ExprMixin<ExprType::CallRef>(loc) {}
736761
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;
740763};
741764
742765template <ExprType TypeEnum>
@@ -924,6 +947,8 @@ struct Func {
924947
925948 std::string name;
926949 FuncDeclaration decl;
950+ // Contains references with unknown indicies.
951+ TypeVector local_type_list;
927952 LocalTypes local_types;
928953 BindingHash bindings;
929954 ExprList exprs;
@@ -941,7 +966,7 @@ struct Global {
941966 explicit Global (std::string_view name) : name(name) {}
942967
943968 std::string name;
944- Type type = Type::Void ;
969+ Var type;
945970 bool mutable_ = false ;
946971 ExprList init_expr;
947972};
0 commit comments