Simplify addArrayDimensions by removing multi-dimensional array support.

There's no need to pass in an array of multiple dimensions when only one
dimension is supported by the language.

Change-Id: Id170e96e1c0e8f83a79a85e4a737792677044150
Bug: skia:11026
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340659
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp
index f30145e..69870a5 100644
--- a/src/sksl/SkSLIRGenerator.cpp
+++ b/src/sksl/SkSLIRGenerator.cpp
@@ -402,7 +402,7 @@
                 } else {
                     arraySize = Type::kUnsizedArray;
                 }
-                type = fSymbolTable->addArrayDimensions(type, {arraySize});
+                type = fSymbolTable->addArrayDimension(type, arraySize);
             }
         }
         auto var = std::make_unique<Variable>(varDecl.fOffset, fModifiers->addToPool(modifiers),
@@ -903,7 +903,7 @@
         }
         if (pd.fIsArray) {
             int arraySize = (paramIter++)->getInt();
-            type = fSymbolTable->addArrayDimensions(type, {arraySize});
+            type = fSymbolTable->addArrayDimension(type, arraySize);
         }
         // Only the (builtin) declarations of 'sample' are allowed to have FP parameters
         if ((type->nonnullable() == *fContext.fFragmentProcessor_Type && !fIsBuiltinCode) ||
@@ -1164,7 +1164,7 @@
         } else {
             arraySize = Type::kUnsizedArray;
         }
-        type = symbols->addArrayDimensions(type, {arraySize});
+        type = symbols->addArrayDimension(type, arraySize);
     }
     const Variable* var = old->takeOwnershipOfSymbol(
             std::make_unique<Variable>(intf.fOffset,
@@ -1319,7 +1319,7 @@
     if (isArray) {
         auto iter = type.begin();
         int arraySize = *iter ? iter->getInt() : Type::kUnsizedArray;
-        result = fSymbolTable->addArrayDimensions(result, {arraySize});
+        result = fSymbolTable->addArrayDimension(result, arraySize);
     }
     return result;
 }
@@ -2459,10 +2459,9 @@
                                                       const ASTNode& index) {
     if (base->kind() == Expression::Kind::kTypeReference) {
         if (index.fKind == ASTNode::Kind::kInt) {
-            const Type& oldType = base->as<TypeReference>().value();
-            SkSTArray<1, int> dimension = {index.getInt()};
-            const Type* newType = fSymbolTable->addArrayDimensions(&oldType, dimension);
-            return std::make_unique<TypeReference>(fContext, base->fOffset, newType);
+            const Type* type = &base->as<TypeReference>().value();
+            type = fSymbolTable->addArrayDimension(type, index.getInt());
+            return std::make_unique<TypeReference>(fContext, base->fOffset, type);
 
         } else {
             fErrors.error(base->fOffset, "array size must be a constant");
diff --git a/src/sksl/SkSLParser.cpp b/src/sksl/SkSLParser.cpp
index 6c5c97b..db91a4a 100644
--- a/src/sksl/SkSLParser.cpp
+++ b/src/sksl/SkSLParser.cpp
@@ -556,7 +556,7 @@
                 }
                 // Add the array dimensions to our type.
                 int arraySize = size.getInt();
-                type = fSymbols.addArrayDimensions(type, {arraySize});
+                type = fSymbols.addArrayDimension(type, arraySize);
             }
 
             fields.push_back(Type::Field(modifiers, vd.fName, type));
diff --git a/src/sksl/ir/SkSLSymbolTable.cpp b/src/sksl/ir/SkSLSymbolTable.cpp
index a2ec139..fb102fa 100644
--- a/src/sksl/ir/SkSLSymbolTable.cpp
+++ b/src/sksl/ir/SkSLSymbolTable.cpp
@@ -121,18 +121,17 @@
     }
 }
 
-const Type* SymbolTable::addArrayDimensions(const Type* type, const SkTArray<int>& dimensions) {
-    StringFragment baseName = type->name();
-    String arrayDims;
-    for (int index = dimensions.count(); index--; ) {
-        int dim = dimensions[index];
-        arrayDims = (dim != Type::kUnsizedArray)
-                            ? String::printf("[%d]%s", dim, arrayDims.c_str())
-                            : String::printf("[]%s", arrayDims.c_str());
-
-        type = this->takeOwnershipOfSymbol(std::make_unique<Type>(baseName + arrayDims,
+const Type* SymbolTable::addArrayDimension(const Type* type, int arraySize) {
+    if (arraySize != 0) {
+        // SkSL doesn't support multi-dimensional arrays.
+        SkASSERT(type->typeKind() != Type::TypeKind::kArray);
+        String baseName = type->name();
+        String arrayName = (arraySize != Type::kUnsizedArray)
+                                   ? String::printf("%s[%d]", baseName.c_str(), arraySize)
+                                   : String::printf("%s[]", baseName.c_str());
+        type = this->takeOwnershipOfSymbol(std::make_unique<Type>(std::move(arrayName),
                                                                   Type::TypeKind::kArray,
-                                                                  *type, dim));
+                                                                  *type, arraySize));
     }
     return type;
 }
diff --git a/src/sksl/ir/SkSLSymbolTable.h b/src/sksl/ir/SkSLSymbolTable.h
index 1dea68e..3ea5ba1 100644
--- a/src/sksl/ir/SkSLSymbolTable.h
+++ b/src/sksl/ir/SkSLSymbolTable.h
@@ -74,11 +74,11 @@
     }
 
     /**
-     * Given baseType = `float` and dimensions = {1, 2, 3}, creates the full chain of required
-     * array types in the symbol table: `float[3]`, `float[2][3]`, and `float[1][2][3]`. The
-     * fully-dimensioned array type is returned. `kUnsizedArray` can be passed as a `[]` dimension.
+     * Given type = `float` and arraySize = 5, creates the array type `float[5]` in the symbol
+     * table. The created array type is returned. `kUnsizedArray` can be passed as a `[]` dimension.
+     * If zero is passed, the base type is returned unchanged.
      */
-    const Type* addArrayDimensions(const Type* type, const SkTArray<int>& dimensions);
+    const Type* addArrayDimension(const Type* type, int arraySize);
 
     // Call fn for every symbol in the table.  You may not mutate anything.
     template <typename Fn>