Skip to content

Use TYPE_SWITCH macro for types (NFC) #2579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ class Type {
funcref,
anyref,
nullref,
exnref,
_last_value_type,
exnref
};

private:
// Not in the enum because we don't want to have to have a case for it
static constexpr uint32_t last_value_type = exnref;

public:
Type() = default;

// ValueType can be implicitly upgraded to Type
Expand All @@ -60,15 +64,17 @@ class Type {
const std::vector<Type>& expand() const;

// Predicates
bool isSingle() const { return id >= i32 && id < _last_value_type; }
bool isMulti() const { return id >= _last_value_type; }
bool isSingle() const { return id >= i32 && id <= last_value_type; }
bool isMulti() const { return id > last_value_type; }
bool isConcrete() const { return id >= i32; }
bool isInteger() const { return id == i32 || id == i64; }
bool isFloat() const { return id == f32 || id == f64; }
bool isVector() const { return id == v128; };
bool isNumber() const { return id >= i32 && id <= v128; }
bool isRef() const { return id >= funcref && id <= exnref; }

constexpr uint32_t getID() const { return id; }

// (In)equality must be defined for both Type and ValueType because it is
// otherwise ambiguous whether to convert both this and other to int or
// convert other to Type.
Expand Down Expand Up @@ -155,4 +161,8 @@ template<> class std::hash<wasm::Signature> {
size_t operator()(const wasm::Signature& sig) const;
};

// Recommended alternative to the plain switch-case for switching on types
// Usage: Use 'TYPE_SWITCH(type)' in place of 'switch (type)'.
#define TYPE_SWITCH(TYPE) switch (static_cast<Type::ValueType>(TYPE.getID()))

#endif // wasm_wasm_type_h
8 changes: 4 additions & 4 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ bool Type::operator<(const Type& other) const {
unsigned Type::getByteSize() const {
assert(isSingle() && "getByteSize does not works with single types");
Type singleType = *expand().begin();
switch (singleType) {
TYPE_SWITCH(singleType) {
case Type::i32:
return 4;
case Type::i64:
Expand All @@ -172,7 +172,7 @@ unsigned Type::getByteSize() const {
Type Type::reinterpret() const {
assert(isSingle() && "reinterpretType only works with single types");
Type singleType = *expand().begin();
switch (singleType) {
TYPE_SWITCH(singleType) {
case Type::i32:
return f32;
case Type::i64:
Expand All @@ -196,7 +196,7 @@ Type Type::reinterpret() const {
FeatureSet Type::getFeatures() const {
FeatureSet feats = FeatureSet::MVP;
for (Type t : expand()) {
switch (t) {
TYPE_SWITCH(t) {
case Type::v128:
feats |= FeatureSet::SIMD;
break;
Expand Down Expand Up @@ -299,7 +299,7 @@ bool Signature::operator<(const Signature& other) const {
}

std::ostream& operator<<(std::ostream& os, Type type) {
switch (type) {
TYPE_SWITCH(type) {
case Type::none:
os << "none";
break;
Expand Down