[dawn][common] Require ityp container indices to be unsigned

And update some enum types to conform to the this.

Bug: 503403892
Change-Id: I2f5f4af6959220e2698a4c39e7ff95d19641b707
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/304739
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/generator/templates/dawn/native/Features.h b/generator/templates/dawn/native/Features.h
index ef45a61..4d6030f 100644
--- a/generator/templates/dawn/native/Features.h
+++ b/generator/templates/dawn/native/Features.h
@@ -35,7 +35,7 @@
 
 namespace dawn::native {
 
-enum class Feature {
+enum class Feature : uint32_t {
   {% for enum in types["feature name"].values if (enum.valid and not is_enum_value_proxy(enum)) %}
     {{as_cppEnum(enum.name)}},
   {% endfor %}
diff --git a/generator/templates/dawn/native/ObjectType.cpp b/generator/templates/dawn/native/ObjectType.cpp
index 84e328f..32a4574 100644
--- a/generator/templates/dawn/native/ObjectType.cpp
+++ b/generator/templates/dawn/native/ObjectType.cpp
@@ -31,6 +31,8 @@
 {% set native_dir = impl_dir + namespace_name.Dirs() %}
 #include "{{native_dir}}/ObjectType_autogen.h"
 
+#include "dawn/common/Assert.h"
+
 namespace {{native_namespace}} {
 
     const char* ObjectTypeAsString(ObjectType type) {
diff --git a/generator/templates/dawn/native/ObjectType.h b/generator/templates/dawn/native/ObjectType.h
index 11e0bf1..7ee667d 100644
--- a/generator/templates/dawn/native/ObjectType.h
+++ b/generator/templates/dawn/native/ObjectType.h
@@ -27,8 +27,8 @@
 
 {% set namespace_name = Name(metadata.native_namespace) %}
 {% set DIR = namespace_name.concatcase().upper() %}
-#ifndef {{DIR}}_OBJECTTPYE_AUTOGEN_H_
-#define {{DIR}}_OBJECTTPYE_AUTOGEN_H_
+#ifndef {{DIR}}_OBJECTTYPE_AUTOGEN_H_
+#define {{DIR}}_OBJECTTYPE_AUTOGEN_H_
 
 #include "dawn/common/ityp_array.h"
 
@@ -56,4 +56,4 @@
 } // namespace {{native_namespace}}
 
 
-#endif  // {{DIR}}_OBJECTTPYE_AUTOGEN_H_
+#endif  // {{DIR}}_OBJECTTYPE_AUTOGEN_H_
diff --git a/include/dawn/native/DawnNative.h b/include/dawn/native/DawnNative.h
index 4ddbfd5..f107cf1 100644
--- a/include/dawn/native/DawnNative.h
+++ b/include/dawn/native/DawnNative.h
@@ -244,7 +244,7 @@
 DAWN_NATIVE_EXPORT void InjectErrorAt(uint64_t index);
 
 // The different types of external images
-enum ExternalImageType {
+enum ExternalImageType : uint16_t {
     OpaqueFD,
     DmaBuf,
     IOSurface,
diff --git a/src/dawn/common/UnderlyingType.h b/src/dawn/common/UnderlyingType.h
index 9adb283..6535d07 100644
--- a/src/dawn/common/UnderlyingType.h
+++ b/src/dawn/common/UnderlyingType.h
@@ -66,6 +66,9 @@
 template <typename T>
 using UnderlyingType = typename detail::UnderlyingTypeImpl<T>::type;
 
+template <typename T>
+concept UnsignedUnderlyingType = std::unsigned_integral<UnderlyingType<T>>;
+
 }  // namespace dawn
 
 #endif  // SRC_DAWN_COMMON_UNDERLYINGTYPE_H_
diff --git a/src/dawn/common/ityp_array.h b/src/dawn/common/ityp_array.h
index 9c7e3fd..03c731a 100644
--- a/src/dawn/common/ityp_array.h
+++ b/src/dawn/common/ityp_array.h
@@ -33,7 +33,6 @@
 #include <limits>
 #include <utility>
 
-#include "dawn/common/TypedInteger.h"
 #include "dawn/common/UnderlyingType.h"
 
 namespace dawn::ityp {
@@ -47,6 +46,7 @@
     using I = UnderlyingType<Index>;
     using Base = ::std::array<Value, Size>;
 
+    static_assert(UnsignedUnderlyingType<Index>, "Index type must be unsigned");
     static_assert(Size <= std::numeric_limits<I>::max());
 
   public:
diff --git a/src/dawn/common/ityp_bitset.h b/src/dawn/common/ityp_bitset.h
index 252e316..706423b 100644
--- a/src/dawn/common/ityp_bitset.h
+++ b/src/dawn/common/ityp_bitset.h
@@ -35,8 +35,6 @@
 #include "dawn/common/Assert.h"
 #include "dawn/common/BitSetRangeIterator.h"
 #include "dawn/common/Math.h"
-#include "dawn/common/Platform.h"
-#include "dawn/common/TypedInteger.h"
 #include "dawn/common/UnderlyingType.h"
 
 namespace dawn {
@@ -157,6 +155,7 @@
     using I = UnderlyingType<Index>;
     using Base = ::std::bitset<N>;
 
+    static_assert(UnsignedUnderlyingType<Index>, "Index type must be unsigned");
     static_assert(sizeof(I) <= sizeof(size_t));
 
     explicit constexpr bitset(const Base& rhs) : Base(rhs) {}
diff --git a/src/dawn/common/ityp_span.h b/src/dawn/common/ityp_span.h
index bca2827..560ce6b 100644
--- a/src/dawn/common/ityp_span.h
+++ b/src/dawn/common/ityp_span.h
@@ -33,10 +33,11 @@
 #ifndef SRC_DAWN_COMMON_ITYP_SPAN_H_
 #define SRC_DAWN_COMMON_ITYP_SPAN_H_
 
+#include <cstddef>
 #include <limits>
 #include <span>
 
-#include "dawn/common/TypedInteger.h"
+#include "dawn/common/Assert.h"
 #include "dawn/common/UnderlyingType.h"
 
 namespace dawn::ityp {
@@ -48,6 +49,8 @@
     using I = UnderlyingType<Index>;
     using Base = ::std::span<Value>;
 
+    static_assert(UnsignedUnderlyingType<Index>, "Index type must be unsigned");
+
   public:
     constexpr span() = default;
     constexpr span(Value* data, Index size) : Base{data, static_cast<I>(size)} {}
diff --git a/src/dawn/common/ityp_stack_vec.h b/src/dawn/common/ityp_stack_vec.h
index cb4337e..e261792 100644
--- a/src/dawn/common/ityp_stack_vec.h
+++ b/src/dawn/common/ityp_stack_vec.h
@@ -40,6 +40,8 @@
 class stack_vec : private absl::InlinedVector<Value, StaticCapacity> {
     using I = UnderlyingType<Index>;
     using Base = absl::InlinedVector<Value, StaticCapacity>;
+
+    static_assert(UnsignedUnderlyingType<Index>, "Index type must be unsigned");
     static_assert(StaticCapacity <= std::numeric_limits<I>::max());
 
   public:
diff --git a/src/dawn/common/ityp_vector.h b/src/dawn/common/ityp_vector.h
index c45618c..1cc9c5b 100644
--- a/src/dawn/common/ityp_vector.h
+++ b/src/dawn/common/ityp_vector.h
@@ -28,10 +28,11 @@
 #ifndef SRC_DAWN_COMMON_ITYP_VECTOR_H_
 #define SRC_DAWN_COMMON_ITYP_VECTOR_H_
 
+#include <cstddef>
 #include <limits>
 #include <vector>
 
-#include "dawn/common/TypedInteger.h"
+#include "dawn/common/Assert.h"
 #include "dawn/common/UnderlyingType.h"
 
 namespace dawn::ityp {
@@ -43,6 +44,8 @@
     using I = UnderlyingType<Index>;
     using Base = std::vector<Value>;
 
+    static_assert(UnsignedUnderlyingType<Index>, "Index type must be unsigned");
+
   private:
     // Disallow access to base constructors and untyped index/size-related operators.
     using Base::Base;
diff --git a/src/dawn/native/PerStage.h b/src/dawn/native/PerStage.h
index 8043e64..abc8c34 100644
--- a/src/dawn/native/PerStage.h
+++ b/src/dawn/native/PerStage.h
@@ -38,7 +38,7 @@
 
 namespace dawn::native {
 
-enum class SingleShaderStage { Vertex, Fragment, Compute };
+enum class SingleShaderStage : uint8_t { Vertex, Fragment, Compute };
 
 static_assert(static_cast<uint32_t>(SingleShaderStage::Vertex) < kNumStages);
 static_assert(static_cast<uint32_t>(SingleShaderStage::Fragment) < kNumStages);
diff --git a/src/dawn/native/metal/PhysicalDeviceMTL.mm b/src/dawn/native/metal/PhysicalDeviceMTL.mm
index 16fec7c..3a4fa95 100644
--- a/src/dawn/native/metal/PhysicalDeviceMTL.mm
+++ b/src/dawn/native/metal/PhysicalDeviceMTL.mm
@@ -230,7 +230,7 @@
 }
 
 // https://developer.apple.com/documentation/metal/mtlgpufamily/apple9?language=objc
-enum class MTLGPUFamily {
+enum class MTLGPUFamily : uint8_t {
     Apple1,
     Apple2,
     Apple3,
diff --git a/src/dawn/native/metal/UtilsMetal.h b/src/dawn/native/metal/UtilsMetal.h
index d0dc9ee..6ccbb27 100644
--- a/src/dawn/native/metal/UtilsMetal.h
+++ b/src/dawn/native/metal/UtilsMetal.h
@@ -44,7 +44,7 @@
 struct BeginRenderPassCmd;
 struct ProgrammableStage;
 struct EntryPointMetadata;
-enum class SingleShaderStage;
+enum class SingleShaderStage : uint8_t;
 }  // namespace dawn::native
 
 namespace dawn::native::metal {
diff --git a/src/dawn/native/opengl/EGLFunctions.h b/src/dawn/native/opengl/EGLFunctions.h
index c932c0b..b0c9d4d 100644
--- a/src/dawn/native/opengl/EGLFunctions.h
+++ b/src/dawn/native/opengl/EGLFunctions.h
@@ -34,7 +34,7 @@
 
 namespace dawn::native::opengl {
 
-enum class EGLExt {
+enum class EGLExt : uint32_t {
     // Promoted to EGL 1.5
     ClientExtensions,
     PlatformBase,
diff --git a/src/dawn/native/vulkan/BackendVk.h b/src/dawn/native/vulkan/BackendVk.h
index fed1c9b..7ab3c37 100644
--- a/src/dawn/native/vulkan/BackendVk.h
+++ b/src/dawn/native/vulkan/BackendVk.h
@@ -51,7 +51,7 @@
 // See crbug.com/850881, crbug.com/863086, crbug.com/1465064, crbug.com/346990068
 inline constexpr uint32_t kRequiredVulkanVersion = VK_API_VERSION_1_1;
 
-enum class ICD {
+enum class ICD : uint8_t {
     None,
     SwiftShader,
 };
diff --git a/src/dawn/native/vulkan/VulkanExtensions.h b/src/dawn/native/vulkan/VulkanExtensions.h
index b5e71a3..fa82cc4 100644
--- a/src/dawn/native/vulkan/VulkanExtensions.h
+++ b/src/dawn/native/vulkan/VulkanExtensions.h
@@ -37,7 +37,7 @@
 
 // The list of known instance extensions. They must be in dependency order (this is checked
 // inside EnsureDependencies)
-enum class InstanceExt {
+enum class InstanceExt : uint32_t {
     // Surface extensions
     Surface,
     FuchsiaImagePipeSurface,
@@ -76,7 +76,7 @@
 // The list of known device extensions. They must be in dependency order (this is checked
 // inside EnsureDependencies). Remove extensions from this list once kRequiredVulkanVersion
 // is updated to the version they were promoted in.
-enum class DeviceExt {
+enum class DeviceExt : uint32_t {
     // Promoted to 1.2
     DriverProperties,
     ImageFormatList,
@@ -149,7 +149,7 @@
                                 uint32_t version);
 
 // The list of all known Vulkan layers.
-enum class VulkanLayer {
+enum class VulkanLayer : uint8_t {
     Validation,
     LunargVkTrace,
     RenderDocCapture,
diff --git a/src/dawn/native/webgpu_absl_format.h b/src/dawn/native/webgpu_absl_format.h
index 4a3bd99..ef1ee05 100644
--- a/src/dawn/native/webgpu_absl_format.h
+++ b/src/dawn/native/webgpu_absl_format.h
@@ -232,7 +232,7 @@
 absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
 AbslFormatConvert(SampleTypeBit value, const absl::FormatConversionSpec& spec, absl::FormatSink* s);
 
-enum class SingleShaderStage;
+enum class SingleShaderStage : uint8_t;
 absl::FormatConvertResult<absl::FormatConversionCharSet::kString> AbslFormatConvert(
     SingleShaderStage value,
     const absl::FormatConversionSpec& spec,
diff --git a/src/dawn/tests/unittests/EnumeratorTests.cpp b/src/dawn/tests/unittests/EnumeratorTests.cpp
index d7d8b85..ef1a493 100644
--- a/src/dawn/tests/unittests/EnumeratorTests.cpp
+++ b/src/dawn/tests/unittests/EnumeratorTests.cpp
@@ -30,6 +30,7 @@
 
 #include "dawn/common/Compiler.h"
 #include "dawn/common/Enumerator.h"
+#include "dawn/common/TypedInteger.h"
 #include "dawn/common/ityp_array.h"
 #include "dawn/common/ityp_span.h"
 #include "dawn/common/ityp_vector.h"
diff --git a/src/dawn/tests/unittests/ITypBitsetTests.cpp b/src/dawn/tests/unittests/ITypBitsetTests.cpp
index 11a76ce..1946878 100644
--- a/src/dawn/tests/unittests/ITypBitsetTests.cpp
+++ b/src/dawn/tests/unittests/ITypBitsetTests.cpp
@@ -380,7 +380,7 @@
 
 class EnumBitSetIteratorTest : public testing::Test {
   protected:
-    enum class TestEnum { A, B, C, D, E, F, G, H, I, J, EnumCount };
+    enum class TestEnum : uint8_t { A, B, C, D, E, F, G, H, I, J, EnumCount };
 
     static constexpr size_t kEnumCount = static_cast<size_t>(TestEnum::EnumCount);
     ityp::bitset<TestEnum, kEnumCount> mStateBits;