castable: Infer Is() TO type from predicate

For the Is() overload that takes a predicate function, infer the cast target type from the single parameter of the predicate.

Removes noise.

Change-Id: Ie6248c776ca1f9d50808e03e9685056fd3819217
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/62441
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-08-23 15:16:42 +00:00 committed by Tint LUCI CQ
parent 1fa6f5ce9b
commit 367f5df5c3
6 changed files with 29 additions and 30 deletions

View File

@ -58,13 +58,11 @@ bool Type::is_float_scalar() const {
} }
bool Type::is_float_matrix() const { bool Type::is_float_matrix() const {
return Is<Matrix>( return Is([](const Matrix* m) { return m->type()->is_float_scalar(); });
[](const Matrix* m) { return m->type()->is_float_scalar(); });
} }
bool Type::is_float_vector() const { bool Type::is_float_vector() const {
return Is<Vector>( return Is([](const Vector* v) { return v->type()->is_float_scalar(); });
[](const Vector* v) { return v->type()->is_float_scalar(); });
} }
bool Type::is_float_scalar_or_vector() const { bool Type::is_float_scalar_or_vector() const {
@ -80,11 +78,11 @@ bool Type::is_integer_scalar() const {
} }
bool Type::is_unsigned_integer_vector() const { bool Type::is_unsigned_integer_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->Is<U32>(); }); return Is([](const Vector* v) { return v->type()->Is<U32>(); });
} }
bool Type::is_signed_integer_vector() const { bool Type::is_signed_integer_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->Is<I32>(); }); return Is([](const Vector* v) { return v->type()->Is<I32>(); });
} }
bool Type::is_unsigned_scalar_or_vector() const { bool Type::is_unsigned_scalar_or_vector() const {
@ -100,7 +98,7 @@ bool Type::is_integer_scalar_or_vector() const {
} }
bool Type::is_bool_vector() const { bool Type::is_bool_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->Is<Bool>(); }); return Is([](const Vector* v) { return v->type()->Is<Bool>(); });
} }
bool Type::is_bool_scalar_or_vector() const { bool Type::is_bool_scalar_or_vector() const {

View File

@ -17,6 +17,8 @@
#include <utility> #include <utility>
#include "src/traits.h"
#if defined(__clang__) #if defined(__clang__)
/// Temporarily disable certain warnings when using Castable API /// Temporarily disable certain warnings when using Castable API
#define TINT_CASTABLE_PUSH_DISABLE_WARNINGS() \ #define TINT_CASTABLE_PUSH_DISABLE_WARNINGS() \
@ -298,8 +300,9 @@ class Castable : public BASE {
/// pred(const TO*) returns true /// pred(const TO*) returns true
/// @param pred predicate function with signature `bool(const TO*)` called iff /// @param pred predicate function with signature `bool(const TO*)` called iff
/// object is of, or derives from the class `TO`. /// object is of, or derives from the class `TO`.
template <typename TO, int FLAGS = 0, typename Pred = detail::Infer> template <int FLAGS = 0, typename Pred = detail::Infer>
inline bool Is(Pred&& pred) const { inline bool Is(Pred&& pred) const {
using TO = typename std::remove_pointer<traits::ParamTypeT<Pred, 0>>::type;
return tint::Is<TO, FLAGS>(static_cast<const CLASS*>(this), return tint::Is<TO, FLAGS>(static_cast<const CLASS*>(this),
std::forward<Pred>(pred)); std::forward<Pred>(pred));
} }

View File

@ -214,17 +214,17 @@ TEST(Castable, Is) {
TEST(Castable, IsWithPredicate) { TEST(Castable, IsWithPredicate) {
std::unique_ptr<Animal> frog = std::make_unique<Frog>(); std::unique_ptr<Animal> frog = std::make_unique<Frog>();
frog->Is<Animal>([&frog](const Animal* a) { frog->Is([&frog](const Animal* a) {
EXPECT_EQ(a, frog.get()); EXPECT_EQ(a, frog.get());
return true; return true;
}); });
ASSERT_TRUE((frog->Is<Animal>([](const Animal*) { return true; }))); ASSERT_TRUE((frog->Is([](const Animal*) { return true; })));
ASSERT_FALSE((frog->Is<Animal>([](const Animal*) { return false; }))); ASSERT_FALSE((frog->Is([](const Animal*) { return false; })));
// Predicate not called if cast is invalid // Predicate not called if cast is invalid
auto expect_not_called = [] { FAIL() << "Should not be called"; }; auto expect_not_called = [] { FAIL() << "Should not be called"; };
ASSERT_FALSE((frog->Is<Bear>([&](const Animal*) { ASSERT_FALSE((frog->Is([&](const Bear*) {
expect_not_called(); expect_not_called();
return true; return true;
}))); })));

View File

@ -483,7 +483,7 @@ bool match_sampler(const sem::Type* ty) {
if (ty->Is<Any>()) { if (ty->Is<Any>()) {
return true; return true;
} }
return ty->Is<sem::Sampler>([](const sem::Sampler* s) { return ty->Is([](const sem::Sampler* s) {
return s->kind() == ast::SamplerKind::kSampler; return s->kind() == ast::SamplerKind::kSampler;
}); });
} }
@ -496,7 +496,7 @@ bool match_sampler_comparison(const sem::Type* ty) {
if (ty->Is<Any>()) { if (ty->Is<Any>()) {
return true; return true;
} }
return ty->Is<sem::Sampler>([](const sem::Sampler* s) { return ty->Is([](const sem::Sampler* s) {
return s->kind() == ast::SamplerKind::kComparisonSampler; return s->kind() == ast::SamplerKind::kComparisonSampler;
}); });
} }
@ -575,7 +575,7 @@ bool match_texture_depth(const sem::Type* ty, ast::TextureDimension dim) {
if (ty->Is<Any>()) { if (ty->Is<Any>()) {
return true; return true;
} }
return ty->Is<sem::DepthTexture>([&](auto t) { return t->dim() == dim; }); return ty->Is([&](const sem::DepthTexture* t) { return t->dim() == dim; });
} }
#define DECLARE_DEPTH_TEXTURE(suffix, dim) \ #define DECLARE_DEPTH_TEXTURE(suffix, dim) \
@ -597,8 +597,9 @@ bool match_texture_depth_multisampled_2d(const sem::Type* ty) {
if (ty->Is<Any>()) { if (ty->Is<Any>()) {
return true; return true;
} }
return ty->Is<sem::DepthMultisampledTexture>( return ty->Is([&](const sem::DepthMultisampledTexture* t) {
[&](auto t) { return t->dim() == ast::TextureDimension::k2d; }); return t->dim() == ast::TextureDimension::k2d;
});
} }
sem::DepthMultisampledTexture* build_texture_depth_multisampled_2d( sem::DepthMultisampledTexture* build_texture_depth_multisampled_2d(

View File

@ -367,7 +367,7 @@ bool Type::IsFloatScalarOrVector() const {
} }
bool Type::IsFloatVector() const { bool Type::IsFloatVector() const {
return Is<Vector>([](const Vector* v) { return v->type->IsFloatScalar(); }); return Is([](const Vector* v) { return v->type->IsFloatScalar(); });
} }
bool Type::IsIntegerScalar() const { bool Type::IsIntegerScalar() const {
@ -383,7 +383,7 @@ bool Type::IsScalar() const {
} }
bool Type::IsSignedIntegerVector() const { bool Type::IsSignedIntegerVector() const {
return Is<Vector>([](const Vector* v) { return v->type->Is<I32>(); }); return Is([](const Vector* v) { return v->type->Is<I32>(); });
} }
bool Type::IsSignedScalarOrVector() const { bool Type::IsSignedScalarOrVector() const {
@ -391,7 +391,7 @@ bool Type::IsSignedScalarOrVector() const {
} }
bool Type::IsUnsignedIntegerVector() const { bool Type::IsUnsignedIntegerVector() const {
return Is<Vector>([](const Vector* v) { return v->type->Is<U32>(); }); return Is([](const Vector* v) { return v->type->Is<U32>(); });
} }
bool Type::IsUnsignedScalarOrVector() const { bool Type::IsUnsignedScalarOrVector() const {

View File

@ -77,13 +77,11 @@ bool Type::is_float_scalar() const {
} }
bool Type::is_float_matrix() const { bool Type::is_float_matrix() const {
return Is<Matrix>( return Is([](const Matrix* m) { return m->type()->is_float_scalar(); });
[](const Matrix* m) { return m->type()->is_float_scalar(); });
} }
bool Type::is_float_vector() const { bool Type::is_float_vector() const {
return Is<Vector>( return Is([](const Vector* v) { return v->type()->is_float_scalar(); });
[](const Vector* v) { return v->type()->is_float_scalar(); });
} }
bool Type::is_float_scalar_or_vector() const { bool Type::is_float_scalar_or_vector() const {
@ -107,11 +105,11 @@ bool Type::is_unsigned_integer_scalar() const {
} }
bool Type::is_signed_integer_vector() const { bool Type::is_signed_integer_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->Is<I32>(); }); return Is([](const Vector* v) { return v->type()->Is<I32>(); });
} }
bool Type::is_unsigned_integer_vector() const { bool Type::is_unsigned_integer_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->Is<U32>(); }); return Is([](const Vector* v) { return v->type()->Is<U32>(); });
} }
bool Type::is_unsigned_scalar_or_vector() const { bool Type::is_unsigned_scalar_or_vector() const {
@ -127,7 +125,7 @@ bool Type::is_integer_scalar_or_vector() const {
} }
bool Type::is_bool_vector() const { bool Type::is_bool_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->Is<Bool>(); }); return Is([](const Vector* v) { return v->type()->Is<Bool>(); });
} }
bool Type::is_bool_scalar_or_vector() const { bool Type::is_bool_scalar_or_vector() const {
@ -135,12 +133,11 @@ bool Type::is_bool_scalar_or_vector() const {
} }
bool Type::is_numeric_vector() const { bool Type::is_numeric_vector() const {
return Is<Vector>( return Is([](const Vector* v) { return v->type()->is_numeric_scalar(); });
[](const Vector* v) { return v->type()->is_numeric_scalar(); });
} }
bool Type::is_scalar_vector() const { bool Type::is_scalar_vector() const {
return Is<Vector>([](const Vector* v) { return v->type()->is_scalar(); }); return Is([](const Vector* v) { return v->type()->is_scalar(); });
} }
bool Type::is_numeric_scalar_or_vector() const { bool Type::is_numeric_scalar_or_vector() const {