[tint][type] Add NumericScalar base class.
Take advantage of the RTTI system where we can.
Change-Id: I2c77097cfc083e86cd9c09f2789c586084c5babd
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/136601
Auto-Submit: Ben Clayton <bclayton@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn
index f419ea9..c18546b 100644
--- a/src/tint/BUILD.gn
+++ b/src/tint/BUILD.gn
@@ -862,6 +862,8 @@
"type/multisampled_texture.h",
"type/node.cc",
"type/node.h",
+ "type/numeric_scalar.cc",
+ "type/numeric_scalar.h",
"type/pointer.cc",
"type/pointer.h",
"type/reference.cc",
diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt
index 9428d0e..c901e3b 100644
--- a/src/tint/CMakeLists.txt
+++ b/src/tint/CMakeLists.txt
@@ -491,6 +491,8 @@
type/multisampled_texture.h
type/node.cc
type/node.h
+ type/numeric_scalar.cc
+ type/numeric_scalar.h
type/pointer.cc
type/pointer.h
type/reference.cc
diff --git a/src/tint/type/abstract_numeric.h b/src/tint/type/abstract_numeric.h
index ad697ba..128bc34 100644
--- a/src/tint/type/abstract_numeric.h
+++ b/src/tint/type/abstract_numeric.h
@@ -17,13 +17,13 @@
#include <string>
-#include "src/tint/type/scalar.h"
+#include "src/tint/type/numeric_scalar.h"
namespace tint::type {
/// The base class for abstract-int and abstract-float types.
/// @see https://www.w3.org/TR/WGSL/#types-for-creation-time-constants
-class AbstractNumeric : public utils::Castable<AbstractNumeric, Scalar> {
+class AbstractNumeric : public utils::Castable<AbstractNumeric, NumericScalar> {
public:
/// Constructor
/// @param hash the unique hash of the node
diff --git a/src/tint/type/f16.h b/src/tint/type/f16.h
index 435c1db..31fe336 100644
--- a/src/tint/type/f16.h
+++ b/src/tint/type/f16.h
@@ -17,12 +17,12 @@
#include <string>
-#include "src/tint/type/scalar.h"
+#include "src/tint/type/numeric_scalar.h"
namespace tint::type {
/// A float 16 type
-class F16 final : public utils::Castable<F16, Scalar> {
+class F16 final : public utils::Castable<F16, NumericScalar> {
public:
/// Constructor
F16();
diff --git a/src/tint/type/f32.h b/src/tint/type/f32.h
index b3c9e36..c22da37 100644
--- a/src/tint/type/f32.h
+++ b/src/tint/type/f32.h
@@ -17,12 +17,12 @@
#include <string>
-#include "src/tint/type/scalar.h"
+#include "src/tint/type/numeric_scalar.h"
namespace tint::type {
/// A float 32 type
-class F32 final : public utils::Castable<F32, Scalar> {
+class F32 final : public utils::Castable<F32, NumericScalar> {
public:
/// Constructor
F32();
diff --git a/src/tint/type/i32.h b/src/tint/type/i32.h
index cab1e15..9563349 100644
--- a/src/tint/type/i32.h
+++ b/src/tint/type/i32.h
@@ -17,12 +17,12 @@
#include <string>
-#include "src/tint/type/scalar.h"
+#include "src/tint/type/numeric_scalar.h"
namespace tint::type {
/// A signed int 32 type.
-class I32 final : public utils::Castable<I32, Scalar> {
+class I32 final : public utils::Castable<I32, NumericScalar> {
public:
/// Constructor
I32();
diff --git a/src/tint/type/numeric_scalar.cc b/src/tint/type/numeric_scalar.cc
new file mode 100644
index 0000000..40f1e14
--- /dev/null
+++ b/src/tint/type/numeric_scalar.cc
@@ -0,0 +1,25 @@
+// Copyright 2023 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/tint/type/numeric_scalar.h"
+
+TINT_INSTANTIATE_TYPEINFO(tint::type::NumericScalar);
+
+namespace tint::type {
+
+NumericScalar::NumericScalar(size_t hash, type::Flags flags) : Base(hash, flags) {}
+
+NumericScalar::~NumericScalar() = default;
+
+} // namespace tint::type
diff --git a/src/tint/type/numeric_scalar.h b/src/tint/type/numeric_scalar.h
new file mode 100644
index 0000000..4772cdc
--- /dev/null
+++ b/src/tint/type/numeric_scalar.h
@@ -0,0 +1,38 @@
+// Copyright 2023 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_TINT_TYPE_NUMERIC_SCALAR_H_
+#define SRC_TINT_TYPE_NUMERIC_SCALAR_H_
+
+#include "src/tint/type/scalar.h"
+
+namespace tint::type {
+
+/// Base class for all numeric-scalar types
+/// @see https://www.w3.org/TR/WGSL/#scalar-types
+class NumericScalar : public utils::Castable<NumericScalar, Scalar> {
+ public:
+ /// Destructor
+ ~NumericScalar() override;
+
+ protected:
+ /// Constructor
+ /// @param hash the immutable hash for the node
+ /// @param flags the flags of this type
+ NumericScalar(size_t hash, type::Flags flags);
+};
+
+} // namespace tint::type
+
+#endif // SRC_TINT_TYPE_NUMERIC_SCALAR_H_
diff --git a/src/tint/type/type.cc b/src/tint/type/type.cc
index 9f3edb9..a689715 100644
--- a/src/tint/type/type.cc
+++ b/src/tint/type/type.cc
@@ -67,12 +67,8 @@
return 0;
}
-bool Type::is_numeric_scalar() const {
- return IsAnyOf<F16, F32, U32, I32, AbstractNumeric>();
-}
-
bool Type::is_float_scalar() const {
- return IsAnyOf<F16, F32, AbstractNumeric>();
+ return IsAnyOf<F16, F32, AbstractFloat>();
}
bool Type::is_float_matrix() const {
@@ -153,7 +149,7 @@
}
bool Type::is_numeric_vector() const {
- return Is([](const Vector* v) { return v->type()->is_numeric_scalar(); });
+ return Is([](const Vector* v) { return v->type()->Is<type::NumericScalar>(); });
}
bool Type::is_scalar_vector() const {
@@ -161,7 +157,7 @@
}
bool Type::is_numeric_scalar_or_vector() const {
- return is_numeric_scalar() || is_numeric_vector();
+ return Is<type::NumericScalar>() || is_numeric_vector();
}
bool Type::is_handle() const {
diff --git a/src/tint/type/type.h b/src/tint/type/type.h
index 25f0c89..16c8543 100644
--- a/src/tint/type/type.h
+++ b/src/tint/type/type.h
@@ -93,8 +93,6 @@
/// @see https://www.w3.org/TR/WGSL/#fixed-footprint-types
inline bool HasFixedFootprint() const { return flags_.Contains(Flag::kFixedFootprint); }
- /// @returns true if this type is a numeric scalar
- bool is_numeric_scalar() const;
/// @returns true if this type is a float scalar
bool is_float_scalar() const;
/// @returns true if this type is a float matrix
diff --git a/src/tint/type/u32.h b/src/tint/type/u32.h
index 92ce72d..67ab20e 100644
--- a/src/tint/type/u32.h
+++ b/src/tint/type/u32.h
@@ -17,12 +17,12 @@
#include <string>
-#include "src/tint/type/scalar.h"
+#include "src/tint/type/numeric_scalar.h"
namespace tint::type {
/// A unsigned int 32 type.
-class U32 final : public utils::Castable<U32, Scalar> {
+class U32 final : public utils::Castable<U32, NumericScalar> {
public:
/// Constructor
U32();
diff --git a/src/tint/writer/spirv/builder.cc b/src/tint/writer/spirv/builder.cc
index 7ace584..663fde1 100644
--- a/src/tint/writer/spirv/builder.cc
+++ b/src/tint/writer/spirv/builder.cc
@@ -1464,7 +1464,7 @@
(from_type->is_unsigned_integer_vector() &&
to_type->is_integer_scalar_or_vector())) {
op = spv::Op::OpBitcast;
- } else if ((from_type->is_numeric_scalar() && to_type->Is<type::Bool>()) ||
+ } else if ((from_type->Is<type::NumericScalar>() && to_type->Is<type::Bool>()) ||
(from_type->is_numeric_vector() && to_type->is_bool_vector())) {
// Convert scalar (vector) to bool (vector)
@@ -1990,7 +1990,7 @@
(lhs_type->is_float_vector() && rhs_type->is_float_scalar()));
if (expr->IsArithmetic() && !is_float_scalar_vector_multiply) {
- if (lhs_type->Is<type::Vector>() && rhs_type->is_numeric_scalar()) {
+ if (lhs_type->Is<type::Vector>() && rhs_type->Is<type::NumericScalar>()) {
uint32_t splat_vector_id = GenerateSplat(rhs_id, lhs_type);
if (splat_vector_id == 0) {
return 0;
@@ -1998,7 +1998,7 @@
rhs_id = splat_vector_id;
rhs_type = lhs_type;
- } else if (lhs_type->is_numeric_scalar() && rhs_type->Is<type::Vector>()) {
+ } else if (lhs_type->Is<type::NumericScalar>() && rhs_type->Is<type::Vector>()) {
uint32_t splat_vector_id = GenerateSplat(lhs_id, rhs_type);
if (splat_vector_id == 0) {
return 0;