spirv-reader: only generate aliases for array types

Refactoring only.

Change-Id: Ia931870a337cf93a54c5f2154754ef549c8559d7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/64680
Auto-Submit: David Neto <dneto@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: David Neto <dneto@google.com>
diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc
index 588cba3..4160354 100644
--- a/src/reader/spirv/parser_impl.cc
+++ b/src/reader/spirv/parser_impl.cc
@@ -343,36 +343,27 @@
     return nullptr;
   }
 
-  auto maybe_generate_alias = [this, type_id,
-                               spirv_type](const Type* type) -> const Type* {
-    if (type != nullptr) {
-      return MaybeGenerateAlias(type_id, spirv_type, type);
-    }
-    return type;
-  };
-
   switch (spirv_type->kind()) {
     case spvtools::opt::analysis::Type::kVoid:
-      return maybe_generate_alias(ty_.Void());
+      return ty_.Void();
     case spvtools::opt::analysis::Type::kBool:
-      return maybe_generate_alias(ty_.Bool());
+      return ty_.Bool();
     case spvtools::opt::analysis::Type::kInteger:
-      return maybe_generate_alias(ConvertType(spirv_type->AsInteger()));
+      return ConvertType(spirv_type->AsInteger());
     case spvtools::opt::analysis::Type::kFloat:
-      return maybe_generate_alias(ConvertType(spirv_type->AsFloat()));
+      return ConvertType(spirv_type->AsFloat());
     case spvtools::opt::analysis::Type::kVector:
-      return maybe_generate_alias(ConvertType(spirv_type->AsVector()));
+      return ConvertType(spirv_type->AsVector());
     case spvtools::opt::analysis::Type::kMatrix:
-      return maybe_generate_alias(ConvertType(spirv_type->AsMatrix()));
+      return ConvertType(spirv_type->AsMatrix());
     case spvtools::opt::analysis::Type::kRuntimeArray:
-      return maybe_generate_alias(ConvertType(spirv_type->AsRuntimeArray()));
+      return ConvertType(type_id, spirv_type->AsRuntimeArray());
     case spvtools::opt::analysis::Type::kArray:
-      return maybe_generate_alias(ConvertType(spirv_type->AsArray()));
+      return ConvertType(type_id, spirv_type->AsArray());
     case spvtools::opt::analysis::Type::kStruct:
-      return maybe_generate_alias(ConvertType(type_id, spirv_type->AsStruct()));
+      return ConvertType(type_id, spirv_type->AsStruct());
     case spvtools::opt::analysis::Type::kPointer:
-      return maybe_generate_alias(
-          ConvertType(type_id, ptr_as, spirv_type->AsPointer()));
+      return ConvertType(type_id, ptr_as, spirv_type->AsPointer());
     case spvtools::opt::analysis::Type::kFunction:
       // Tint doesn't have a Function type.
       // We need to convert the result type and parameter types.
@@ -384,7 +375,7 @@
     case spvtools::opt::analysis::Type::kImage:
       // Fake it for sampler and texture types.  These are handled in an
       // entirely different way.
-      return maybe_generate_alias(ty_.Void());
+      return ty_.Void();
     default:
       break;
   }
@@ -978,6 +969,7 @@
 }
 
 const Type* ParserImpl::ConvertType(
+    uint32_t type_id,
     const spvtools::opt::analysis::RuntimeArray* rtarr_ty) {
   auto* ast_elem_ty = ConvertType(type_mgr_->GetId(rtarr_ty->element_type()));
   if (ast_elem_ty == nullptr) {
@@ -987,10 +979,12 @@
   if (!ParseArrayDecorations(rtarr_ty, &array_stride)) {
     return nullptr;
   }
-  return ty_.Array(ast_elem_ty, 0, array_stride);
+  const Type* result = ty_.Array(ast_elem_ty, 0, array_stride);
+  return MaybeGenerateAlias(type_id, rtarr_ty, result);
 }
 
 const Type* ParserImpl::ConvertType(
+    uint32_t type_id,
     const spvtools::opt::analysis::Array* arr_ty) {
   const auto elem_type_id = type_mgr_->GetId(arr_ty->element_type());
   auto* ast_elem_ty = ConvertType(elem_type_id);
@@ -1031,13 +1025,16 @@
   if (remap_buffer_block_type_.count(elem_type_id)) {
     remap_buffer_block_type_.insert(type_mgr_->GetId(arr_ty));
   }
-  return ty_.Array(ast_elem_ty, static_cast<uint32_t>(num_elem), array_stride);
+  const Type* result =
+      ty_.Array(ast_elem_ty, static_cast<uint32_t>(num_elem), array_stride);
+  return MaybeGenerateAlias(type_id, arr_ty, result);
 }
 
 bool ParserImpl::ParseArrayDecorations(
     const spvtools::opt::analysis::Type* spv_type,
     uint32_t* array_stride) {
   bool has_array_stride = false;
+  *array_stride = 0;  // Implicit stride case.
   const auto type_id = type_mgr_->GetId(spv_type);
   for (auto& decoration : this->GetDecorationsFor(type_id)) {
     if (decoration.size() == 2 && decoration[0] == SpvDecorationArrayStride) {
diff --git a/src/reader/spirv/parser_impl.h b/src/reader/spirv/parser_impl.h
index e5ad320..7940c6f 100644
--- a/src/reader/spirv/parser_impl.h
+++ b/src/reader/spirv/parser_impl.h
@@ -193,12 +193,9 @@
   /// @returns a Tint type, or nullptr
   const Type* ConvertType(uint32_t type_id, PtrAs ptr_as = PtrAs::Ptr);
 
-  /// Emits an alias type declaration for the given type, if necessary, and
-  /// also updates the mapping of the SPIR-V type ID to the alias type.
-  /// Do so for the types requiring user-specified names:
-  /// - struct types
-  /// - decorated arrays and runtime arrays
-  /// TODO(dneto): I expect images and samplers to require names as well.
+  /// Emits an alias type declaration for array or runtime-sized array type,
+  /// when needed to distinguish between differently-decorated underlying types.
+  /// Updates the mapping of the SPIR-V type ID to the alias type.
   /// This is a no-op if the parser has already failed.
   /// @param type_id the SPIR-V ID for the type
   /// @param type the type that might get an alias
@@ -698,12 +695,16 @@
   /// Converts a specific SPIR-V type to a Tint type. Matrix case
   const Type* ConvertType(const spvtools::opt::analysis::Matrix* mat_ty);
   /// Converts a specific SPIR-V type to a Tint type. RuntimeArray case
+  /// Distinct SPIR-V array types map to distinct Tint array types.
   /// @param rtarr_ty the Tint type
   const Type* ConvertType(
+      uint32_t type_id,
       const spvtools::opt::analysis::RuntimeArray* rtarr_ty);
   /// Converts a specific SPIR-V type to a Tint type. Array case
+  /// Distinct SPIR-V array types map to distinct Tint array types.
   /// @param arr_ty the Tint type
-  const Type* ConvertType(const spvtools::opt::analysis::Array* arr_ty);
+  const Type* ConvertType(uint32_t type_id,
+                          const spvtools::opt::analysis::Array* arr_ty);
   /// Converts a specific SPIR-V type to a Tint type. Struct case.
   /// SPIR-V allows distinct struct type definitions for two OpTypeStruct
   /// that otherwise have the same set of members (and struct and member
@@ -740,7 +741,8 @@
   /// @returns the signed type
   const Type* SignedTypeFor(const Type* type);
 
-  /// Parses the array or runtime-array decorations.
+  /// Parses the array or runtime-array decorations. Sets 0 if no explicit
+  /// stride was found, and therefore the implicit stride should be used.
   /// @param spv_type the SPIR-V array or runtime-array type.
   /// @param array_stride pointer to the array stride
   /// @returns true on success.
diff --git a/src/reader/spirv/parser_type.h b/src/reader/spirv/parser_type.h
index e82a0df..fad7161 100644
--- a/src/reader/spirv/parser_type.h
+++ b/src/reader/spirv/parser_type.h
@@ -266,7 +266,7 @@
   /// @param el the element type
   /// @param sz the number of elements in the array. 0 represents runtime-sized
   /// array.
-  /// @param st the byte stride of the array
+  /// @param st the byte stride of the array. 0 means use implicit stride.
   Array(const Type* el, uint32_t sz, uint32_t st);
 
   /// Copy constructor