diff options
Diffstat (limited to 'src/type.h')
-rw-r--r-- | src/type.h | 198 |
1 files changed, 105 insertions, 93 deletions
@@ -22,103 +22,115 @@ namespace wabt { +class Type; + using Index = uint32_t; +using TypeVector = std::vector<Type>; -// Matches binary format, do not change. -enum class Type : int32_t { - I32 = -0x01, // 0x7f - I64 = -0x02, // 0x7e - F32 = -0x03, // 0x7d - F64 = -0x04, // 0x7c - V128 = -0x05, // 0x7b - Funcref = -0x10, // 0x70 - Anyref = -0x11, // 0x6f - Nullref = -0x12, // 0x6e - Exnref = -0x18, // 0x68 - Func = -0x20, // 0x60 - Void = -0x40, // 0x40 - ___ = Void, // Convenient for the opcode table in opcode.h - Any = 0, // Not actually specified, but useful for type-checking - Hostref = 2, // Not actually specified, but used in testing and type-checking - I8 = 3, // Not actually specified, but used internally with load/store - I8U = 4, // Not actually specified, but used internally with load/store - I16 = 5, // Not actually specified, but used internally with load/store - I16U = 6, // Not actually specified, but used internally with load/store - I32U = 7, // Not actually specified, but used internally with load/store -}; -typedef std::vector<Type> TypeVector; - -inline bool IsRefType(Type t) { - return t == Type::Anyref || t == Type::Funcref || t == Type::Nullref || - t == Type::Exnref || t == Type::Hostref; -} - -inline bool IsNullableRefType(Type t) { - /* Currently all reftypes are nullable */ - return IsRefType(t); -} - -inline const char* GetTypeName(Type type) { - switch (type) { - case Type::I32: - return "i32"; - case Type::I64: - return "i64"; - case Type::F32: - return "f32"; - case Type::F64: - return "f64"; - case Type::V128: - return "v128"; - case Type::Funcref: - return "funcref"; - case Type::Func: - return "func"; - case Type::Exnref: - return "exnref"; - case Type::Void: - return "void"; - case Type::Any: - return "any"; - case Type::Anyref: - return "anyref"; - case Type::Nullref: - return "nullref"; - default: - return "<type_index>"; +class Type { + public: + // Matches binary format, do not change. + enum Enum { + I32 = -0x01, // 0x7f + I64 = -0x02, // 0x7e + F32 = -0x03, // 0x7d + F64 = -0x04, // 0x7c + V128 = -0x05, // 0x7b + Funcref = -0x10, // 0x70 + Anyref = -0x11, // 0x6f + Nullref = -0x12, // 0x6e + Exnref = -0x18, // 0x68 + Func = -0x20, // 0x60 + Void = -0x40, // 0x40 + ___ = Void, // Convenient for the opcode table in opcode.h + + Any = 0, // Not actually specified, but useful for type-checking + Hostref = 2, // Not actually specified, but used in testing and type-checking + I8 = 3, // Not actually specified, but used internally with load/store + I8U = 4, // Not actually specified, but used internally with load/store + I16 = 5, // Not actually specified, but used internally with load/store + I16U = 6, // Not actually specified, but used internally with load/store + I32U = 7, // Not actually specified, but used internally with load/store + }; + + Type() = default; // Provided so Type can be member of a union. + Type(int32_t code) : enum_(static_cast<Enum>(code)) {} + Type(Enum e) : enum_(e) {} + operator Enum() const { return enum_; } + + bool IsRef() const { + return enum_ == Type::Anyref || enum_ == Type::Funcref || + enum_ == Type::Nullref || enum_ == Type::Exnref || + enum_ == Type::Hostref; } -} - -inline bool IsTypeIndex(Type type) { - return static_cast<int32_t>(type) >= 0; -} - -inline Index GetTypeIndex(Type type) { - assert(IsTypeIndex(type)); - return static_cast<Index>(type); -} - -inline TypeVector GetInlineTypeVector(Type type) { - assert(!IsTypeIndex(type)); - switch (type) { - case Type::Void: - return TypeVector(); - - case Type::I32: - case Type::I64: - case Type::F32: - case Type::F64: - case Type::V128: - case Type::Funcref: - case Type::Anyref: - case Type::Nullref: - case Type::Exnref: - return TypeVector(&type, &type + 1); - - default: - WABT_UNREACHABLE; + + bool IsNullableRef() const { + // Currently all reftypes are nullable + return IsRef(); + } + + const char* GetName() const { + switch (enum_) { + case Type::I32: return "i32"; + case Type::I64: return "i64"; + case Type::F32: return "f32"; + case Type::F64: return "f64"; + case Type::V128: return "v128"; + case Type::Funcref: return "funcref"; + case Type::Func: return "func"; + case Type::Exnref: return "exnref"; + case Type::Void: return "void"; + case Type::Any: return "any"; + case Type::Anyref: return "anyref"; + case Type::Nullref: return "nullref"; + default: return "<type_index>"; + } } -} + + // Functions for handling types that are an index into the type section. + // These are always positive integers. They occur in the binary format in + // block signatures, e.g. + // + // (block (result i32 i64) ...) + // + // is encoded as + // + // (type $T (func (result i32 i64))) + // ... + // (block (type $T) ...) + // + bool IsIndex() const { return static_cast<int32_t>(enum_) >= 0; } + + Index GetIndex() const { + assert(IsIndex()); + return static_cast<Index>(enum_); + } + + TypeVector GetInlineVector() const { + assert(!IsIndex()); + switch (enum_) { + case Type::Void: + return TypeVector(); + + case Type::I32: + case Type::I64: + case Type::F32: + case Type::F64: + case Type::V128: + case Type::Funcref: + case Type::Anyref: + case Type::Nullref: + case Type::Exnref: + return TypeVector(this, this + 1); + + default: + WABT_UNREACHABLE; + } + } + + private: + Enum enum_; +}; } // namespace wabt |