Skip to content

Commit 005eaa9

Browse files
committed
Remove implicit Enum conversion of types
1 parent 8e0ae5a commit 005eaa9

File tree

16 files changed

+58
-41
lines changed

16 files changed

+58
-41
lines changed

include/wabt/decompiler-ls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace wabt {
2626

2727
// Names starting with "u" are unsigned, the rest are "signed or doesn't matter"
2828
inline const char* GetDecompTypeName(Type t) {
29-
switch (t) {
29+
switch (t.code()) {
3030
case Type::I8: return "byte";
3131
case Type::I8U: return "ubyte";
3232
case Type::I16: return "short";

include/wabt/ir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ struct Const {
109109
}
110110

111111
int lane_count() const {
112-
switch (lane_type()) {
112+
switch (lane_type().code()) {
113113
case Type::I8: return 16;
114114
case Type::I16: return 8;
115115
case Type::I32: return 4;

include/wabt/string-format.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#define PRItypecode "%s%#x"
3131
#define WABT_PRINTF_TYPE_CODE(x) \
32-
(static_cast<int32_t>(x) < 0 ? "-" : ""), std::abs(static_cast<int32_t>(x))
32+
((x).code() < 0 ? "-" : ""), std::abs((x).code())
3333

3434
#define WABT_DEFAULT_SNPRINTF_ALLOCA_BUFSIZE 128
3535
#define WABT_SNPRINTF_ALLOCA(buffer, len, format) \

include/wabt/type.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Type {
6565
Type(Enum e, Index type_index) : enum_(e), type_index_(type_index) {
6666
assert(e == Enum::Reference);
6767
}
68-
constexpr operator Enum() const { return enum_; }
68+
constexpr Enum code() const { return enum_; }
6969

7070
bool IsRef() const {
7171
return enum_ == Type::ExternRef || enum_ == Type::FuncRef ||
@@ -80,10 +80,27 @@ class Type {
8080
}
8181

8282
// TODO: remove Enum() auto-casting, then change it to operator ==
83-
bool IsEqual(const Type& other) const {
83+
bool operator==(const Type& other) const {
8484
return enum_ == other.enum_ && type_index_ == other.type_index_;
8585
}
8686

87+
bool operator!=(const Type& other) const {
88+
return enum_ != other.enum_ || type_index_ != other.type_index_;
89+
}
90+
91+
bool operator<(const Type& other) const {
92+
return (enum_ != other.enum_) ? enum_ < other.enum_
93+
: type_index_ < other.type_index_;
94+
}
95+
96+
bool operator==(const Enum& other) const {
97+
return enum_ == other;
98+
}
99+
100+
bool operator!=(const Enum& other) const {
101+
return enum_ != other;
102+
}
103+
87104
std::string GetName() const {
88105
switch (enum_) {
89106
case Type::I32: return "i32";

src/binary-reader.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ Result BinaryReader::ReadField(TypeMut* out_value) {
555555
}
556556

557557
bool BinaryReader::IsConcreteType(Type type) {
558-
switch (type) {
558+
switch (type.code()) {
559559
case Type::I32:
560560
case Type::I64:
561561
case Type::F32:
@@ -687,7 +687,7 @@ Result BinaryReader::ReadGlobalHeader(Type* out_type, bool* out_mutable) {
687687
uint8_t mutable_ = 0;
688688
CHECK_RESULT(ReadType(&global_type, "global type"));
689689
ERROR_UNLESS(IsConcreteType(global_type), "invalid global type: %#x",
690-
static_cast<int>(global_type));
690+
global_type.code());
691691

692692
CHECK_RESULT(ReadU8(&mutable_, "global mutability"));
693693
ERROR_UNLESS(mutable_ <= 1, "global mutability must be 0 or 1");
@@ -2540,7 +2540,7 @@ Result BinaryReader::ReadTypeSection(Offset section_size) {
25402540
form = Type::Func;
25412541
}
25422542

2543-
switch (form) {
2543+
switch (form.code()) {
25442544
case Type::Func: {
25452545
Index num_params;
25462546
CHECK_RESULT(ReadCount(&num_params, "function param count"));

src/binary-writer-spec.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ void BinaryWriterSpec::WriteConst(const Const& const_) {
211211

212212
/* Always write the values as strings, even though they may be representable
213213
* as JSON numbers. This way the formatting is consistent. */
214-
switch (const_.type()) {
214+
switch (const_.type().code()) {
215215
case Type::I32:
216216
WriteString("i32");
217217
WriteSeparator();
@@ -274,7 +274,7 @@ void BinaryWriterSpec::WriteConst(const Const& const_) {
274274
json_stream_->Writef("[");
275275

276276
for (int lane = 0; lane < const_.lane_count(); ++lane) {
277-
switch (const_.lane_type()) {
277+
switch (const_.lane_type().code()) {
278278
case Type::I8:
279279
json_stream_->Writef("\"%u\"", const_.v128_lane<uint8_t>(lane));
280280
break;

src/binary-writer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void WriteOpcode(Stream* stream, Opcode opcode) {
5757
}
5858

5959
void WriteType(Stream* stream, Type type, const char* desc) {
60-
WriteS32Leb128(stream, type, desc ? desc : type.GetName().c_str());
60+
WriteS32Leb128(stream, type.code(), desc ? desc : type.GetName().c_str());
6161
if (type.IsReferenceWithIndex()) {
6262
WriteS32Leb128(stream, type.GetReferenceIndex(),
6363
desc ? desc : type.GetName().c_str());
@@ -815,7 +815,7 @@ void BinaryWriter::WriteExpr(const Func* func, const Expr* expr) {
815815
break;
816816
case ExprType::Const: {
817817
const Const& const_ = cast<ConstExpr>(expr)->const_;
818-
switch (const_.type()) {
818+
switch (const_.type().code()) {
819819
case Type::I32: {
820820
WriteOpcode(stream_, Opcode::I32Const);
821821
WriteS32Leb128(stream_, const_.u32(), "i32 literal");

src/c-writer.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ struct CloseBrace {};
172172

173173
int GetShiftMask(Type type) {
174174
// clang-format off
175-
switch (type) {
175+
switch (type.code()) {
176176
case Type::I32: return 31;
177177
case Type::I64: return 63;
178178
default: WABT_UNREACHABLE; return 0;
@@ -641,7 +641,7 @@ void CWriter::PopLabel() {
641641
// static
642642
constexpr bool CWriter::AreInitializersAlwaysNull(Type type) {
643643
// clang-format off
644-
switch (type) {
644+
switch (type.code()) {
645645
case Type::FuncRef: return false;
646646
case Type::ExternRef: return true;
647647
case Type::ExnRef: return true;
@@ -654,7 +654,7 @@ constexpr bool CWriter::AreInitializersAlwaysNull(Type type) {
654654
// static
655655
constexpr char CWriter::MangleType(Type type) {
656656
// clang-format off
657-
switch (type) {
657+
switch (type.code()) {
658658
case Type::I32: return 'i';
659659
case Type::I64: return 'j';
660660
case Type::F32: return 'f';
@@ -1227,7 +1227,7 @@ void CWriter::Write(const StackVar& sv) {
12271227
// static
12281228
const char* CWriter::GetCTypeName(const Type& type) {
12291229
// clang-format off
1230-
switch (type) {
1230+
switch (type.code()) {
12311231
case Type::I32: return "u32";
12321232
case Type::I64: return "u64";
12331233
case Type::F32: return "f32";
@@ -1248,7 +1248,7 @@ void CWriter::Write(Type type) {
12481248

12491249
void CWriter::Write(TypeEnum type) {
12501250
// clang-format off
1251-
switch (type.type) {
1251+
switch (type.type.code()) {
12521252
case Type::I32: Write("WASM_RT_I32"); break;
12531253
case Type::I64: Write("WASM_RT_I64"); break;
12541254
case Type::F32: Write("WASM_RT_F32"); break;
@@ -1265,7 +1265,7 @@ void CWriter::Write(TypeEnum type) {
12651265

12661266
void CWriter::Write(SignedType type) {
12671267
// clang-format off
1268-
switch (type.type) {
1268+
switch (type.type.code()) {
12691269
case Type::I32: Write("s32"); break;
12701270
case Type::I64: Write("s64"); break;
12711271
default:
@@ -1285,7 +1285,7 @@ void CWriter::Write(const TypeVector& types) {
12851285
}
12861286

12871287
void CWriter::Write(const Const& const_) {
1288-
switch (const_.type()) {
1288+
switch (const_.type().code()) {
12891289
case Type::I32:
12901290
Writef("%uu", static_cast<int32_t>(const_.u32()));
12911291
break;
@@ -3966,7 +3966,7 @@ void CWriter::Write(const ExprList& exprs) {
39663966
break;
39673967

39683968
case ExprType::RefIsNull:
3969-
switch (StackType(0)) {
3969+
switch (StackType(0).code()) {
39703970
case Type::FuncRef:
39713971
Write(StackVar(0, Type::I32), " = (", StackVar(0), ".func == NULL",
39723972
");", Newline());
@@ -6113,7 +6113,7 @@ Result CWriter::WriteModule(const Module& module) {
61136113

61146114
// static
61156115
const char* CWriter::GetReferenceTypeName(const Type& type) {
6116-
switch (type) {
6116+
switch (type.code()) {
61176117
case Type::FuncRef:
61186118
return "funcref";
61196119
case Type::ExternRef:
@@ -6127,7 +6127,7 @@ const char* CWriter::GetReferenceTypeName(const Type& type) {
61276127

61286128
// static
61296129
const char* CWriter::GetReferenceNullValue(const Type& type) {
6130-
switch (type) {
6130+
switch (type.code()) {
61316131
case Type::FuncRef:
61326132
return "wasm_rt_funcref_null_value";
61336133
case Type::ExternRef:

src/decompiler.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ struct Decompiler {
418418
switch (n.etype) {
419419
case ExprType::Const: {
420420
auto& c = cast<ConstExpr>(n.e)->const_;
421-
switch (c.type()) {
421+
switch (c.type().code()) {
422422
case Type::I32:
423423
return Value{{std::to_string(static_cast<int32_t>(c.u32()))},
424424
Precedence::Atomic};

src/interp/interp-util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace wabt {
2424
namespace interp {
2525

2626
std::string TypedValueToString(const TypedValue& tv) {
27-
switch (tv.type) {
27+
switch (tv.type.code()) {
2828
case Type::I32:
2929
return StringPrintf("i32:%u", tv.value.Get<s32>());
3030

0 commit comments

Comments
 (0)