Skip to content

Commit

Permalink
Move isBasic() checks in common Type::is*() to the header
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Sep 11, 2024
1 parent 1a2d26f commit 90b6e7a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
51 changes: 45 additions & 6 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,58 @@ class Type {
constexpr bool isFloat() const { return id == f32 || id == f64; }
constexpr bool isVector() const { return id == v128; };
constexpr bool isNumber() const { return id >= i32 && id <= v128; }
bool isTuple() const;
bool isSingle() const { return isConcrete() && !isTuple(); }
bool isRef() const;
bool isFunction() const;
// See literal.h.
bool isData() const;

// Tuples, refs, etc. are quickly handled using isBasic(), leaving the non-
// basic case for the underlying implementation.

bool isTuple() const {
if (isBasic()) {
return false;
} else {
return isNonBasicTuple();
}
}

bool isRef() const {
if (isBasic()) {
return false;
} else {
return isNonBasicRef();
}
}

bool isFunction() const {
if (isBasic()) {
return false;
} else {
return isNonBasicFunction();
}
}

bool isData() const {
if (isBasic()) {
return false;
} else {
return isNonBasicData();
}
}

// Checks whether a type is a reference and is nullable. This returns false
// for a value that is not a reference, that is, for which nullability is
// irrelevant.
bool isNullable() const;

// Checks whether a type is a reference and is non-nullable. This returns
// false for a value that is not a reference, that is, for which nullability
// is irrelevant. (For that reason, this is only the negation of isNullable()
// on references, but both return false on non-references.)
bool isNonNullable() const;

bool isSignature() const;

// Whether this type is only inhabited by null values.
bool isNull() const;
bool isSignature() const;
bool isStruct() const;
bool isArray() const;
bool isExn() const;
Expand All @@ -177,6 +211,11 @@ class Type {
Nullability getNullability() const;

private:
bool isNonBasicTuple() const;
bool isNonBasicRef() const;
bool isNonBasicFunction() const;
bool isNonBasicData() const;

template<bool (Type::*pred)() const> bool hasPredicate() {
for (const auto& type : *this) {
if ((type.*pred)()) {
Expand Down
40 changes: 14 additions & 26 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,38 +756,26 @@ Type::Type(HeapType heapType, Nullability nullable) {
new (this) Type(globalTypeStore.insert(TypeInfo(heapType, nullable)));
}

bool Type::isTuple() const {
if (isBasic()) {
return false;
} else {
return getTypeInfo(*this)->isTuple();
}
bool Type::isNonBasicTuple() const {
assert(!isBasic());
return getTypeInfo(*this)->isTuple();
}

bool Type::isRef() const {
if (isBasic()) {
return false;
} else {
return getTypeInfo(*this)->isRef();
}
bool Type::isNonBasicRef() const {
assert(!isBasic());
return getTypeInfo(*this)->isRef();
}

bool Type::isFunction() const {
if (isBasic()) {
return false;
} else {
auto* info = getTypeInfo(*this);
return info->isRef() && info->ref.heapType.isFunction();
}
bool Type::isNonBasicFunction() const {
assert(!isBasic());
auto* info = getTypeInfo(*this);
return info->isRef() && info->ref.heapType.isFunction();
}

bool Type::isData() const {
if (isBasic()) {
return false;
} else {
auto* info = getTypeInfo(*this);
return info->isRef() && info->ref.heapType.isData();
}
bool Type::isNonBasicData() const {
assert(!isBasic());
auto* info = getTypeInfo(*this);
return info->isRef() && info->ref.heapType.isData();
}

bool Type::isNullable() const {
Expand Down

0 comments on commit 90b6e7a

Please sign in to comment.