Moved Array handling from convertIndexExpression into IndexExpression

This doesn't change anything for the current code path, but makes the
logic accessible to the DSL in upcoming changes.

Change-Id: I5199e4f86ef9dea51baa2658ea2e5a8cb6434e22
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/430416
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp
index 4e584f3..0c33596 100644
--- a/src/sksl/SkSLIRGenerator.cpp
+++ b/src/sksl/SkSLIRGenerator.cpp
@@ -1421,30 +1421,20 @@
     if (!base) {
         return nullptr;
     }
-    if (base->is<TypeReference>()) {
-        // Convert an index expression starting with a type name: `int[12]`
-        if (iter == index.end()) {
-            this->errorReporter().error(index.fOffset, "array must have a size");
-            return nullptr;
-        }
-        const Type* type = &base->as<TypeReference>().value();
-        int arraySize = this->convertArraySize(*type, index.fOffset, *iter);
-        if (!arraySize) {
-            return nullptr;
-        }
-        type = fSymbolTable->addArrayDimension(type, arraySize);
-        return std::make_unique<TypeReference>(fContext, base->fOffset, type);
-    }
-
     if (iter == index.end()) {
-        this->errorReporter().error(base->fOffset, "missing index in '[]'");
+        if (base->is<TypeReference>()) {
+            this->errorReporter().error(index.fOffset, "array must have a size");
+        } else {
+            this->errorReporter().error(base->fOffset, "missing index in '[]'");
+        }
         return nullptr;
     }
     std::unique_ptr<Expression> converted = this->convertExpression(*(iter++));
     if (!converted) {
         return nullptr;
     }
-    return IndexExpression::Convert(fContext, std::move(base), std::move(converted));
+    return IndexExpression::Convert(fContext, *fSymbolTable, std::move(base),
+                                    std::move(converted));
 }
 
 std::unique_ptr<Expression> IRGenerator::convertCallExpression(const ASTNode& callNode) {
diff --git a/src/sksl/dsl/priv/DSLWriter.cpp b/src/sksl/dsl/priv/DSLWriter.cpp
index 81a2a4d..a4eb878 100644
--- a/src/sksl/dsl/priv/DSLWriter.cpp
+++ b/src/sksl/dsl/priv/DSLWriter.cpp
@@ -177,7 +177,7 @@
 
 std::unique_ptr<SkSL::Expression> DSLWriter::ConvertIndex(std::unique_ptr<Expression> base,
                                                           std::unique_ptr<Expression> index) {
-    return IndexExpression::Convert(Context(), std::move(base), std::move(index));
+    return IndexExpression::Convert(Context(), *SymbolTable(), std::move(base), std::move(index));
 }
 
 std::unique_ptr<SkSL::Expression> DSLWriter::ConvertPostfix(std::unique_ptr<Expression> expr,
diff --git a/src/sksl/ir/SkSLIndexExpression.cpp b/src/sksl/ir/SkSLIndexExpression.cpp
index a95855d..0df5400 100644
--- a/src/sksl/ir/SkSLIndexExpression.cpp
+++ b/src/sksl/ir/SkSLIndexExpression.cpp
@@ -33,10 +33,21 @@
 }
 
 std::unique_ptr<Expression> IndexExpression::Convert(const Context& context,
+                                                     SymbolTable& symbolTable,
                                                      std::unique_ptr<Expression> base,
                                                      std::unique_ptr<Expression> index) {
     // Convert an index expression with an expression inside of it: `arr[a * 3]`.
     const Type& baseType = base->type();
+    if (base->is<TypeReference>() && index->is<IntLiteral>()) {
+        const Type& baseType = base->as<TypeReference>().value();
+        if (baseType.isArray()) {
+            context.fErrors.error(base->fOffset, "multi-dimensional arrays are not supported");
+            return nullptr;
+        }
+        return std::make_unique<TypeReference>(context, /*offset=*/-1,
+                symbolTable.addArrayDimension(&baseType,
+                                              index->as<IntLiteral>().value()));
+    }
     if (!baseType.isArray() && !baseType.isMatrix() && !baseType.isVector()) {
         context.fErrors.error(base->fOffset,
                               "expected array, but found '" + baseType.displayName() + "'");
diff --git a/src/sksl/ir/SkSLIndexExpression.h b/src/sksl/ir/SkSLIndexExpression.h
index c1b0c9a..1814f3f 100644
--- a/src/sksl/ir/SkSLIndexExpression.h
+++ b/src/sksl/ir/SkSLIndexExpression.h
@@ -28,6 +28,7 @@
 
     // Returns a simplified index-expression; reports errors via the ErrorReporter.
     static std::unique_ptr<Expression> Convert(const Context& context,
+                                               SymbolTable& symbolTable,
                                                std::unique_ptr<Expression> base,
                                                std::unique_ptr<Expression> index);