From 251e634792332de54ef6f7202875906aa6ba0333 Mon Sep 17 00:00:00 2001 From: Ethan Nicholas Date: Tue, 20 Jul 2021 13:16:19 -0400 Subject: [PATCH] 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 Reviewed-by: John Stiles --- src/sksl/SkSLIRGenerator.cpp | 24 +++++++----------------- src/sksl/dsl/priv/DSLWriter.cpp | 2 +- src/sksl/ir/SkSLIndexExpression.cpp | 11 +++++++++++ src/sksl/ir/SkSLIndexExpression.h | 1 + 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp index 4e584f3c6c..0c33596d8c 100644 --- a/src/sksl/SkSLIRGenerator.cpp +++ b/src/sksl/SkSLIRGenerator.cpp @@ -1421,30 +1421,20 @@ std::unique_ptr IRGenerator::convertIndexExpression(const ASTNode& i if (!base) { return nullptr; } - if (base->is()) { - // 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().value(); - int arraySize = this->convertArraySize(*type, index.fOffset, *iter); - if (!arraySize) { - return nullptr; - } - type = fSymbolTable->addArrayDimension(type, arraySize); - return std::make_unique(fContext, base->fOffset, type); - } - if (iter == index.end()) { - this->errorReporter().error(base->fOffset, "missing index in '[]'"); + if (base->is()) { + this->errorReporter().error(index.fOffset, "array must have a size"); + } else { + this->errorReporter().error(base->fOffset, "missing index in '[]'"); + } return nullptr; } std::unique_ptr 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 IRGenerator::convertCallExpression(const ASTNode& callNode) { diff --git a/src/sksl/dsl/priv/DSLWriter.cpp b/src/sksl/dsl/priv/DSLWriter.cpp index 81a2a4d565..a4eb878e46 100644 --- a/src/sksl/dsl/priv/DSLWriter.cpp +++ b/src/sksl/dsl/priv/DSLWriter.cpp @@ -177,7 +177,7 @@ std::unique_ptr DSLWriter::ConvertField(std::unique_ptr DSLWriter::ConvertIndex(std::unique_ptr base, std::unique_ptr 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 DSLWriter::ConvertPostfix(std::unique_ptr expr, diff --git a/src/sksl/ir/SkSLIndexExpression.cpp b/src/sksl/ir/SkSLIndexExpression.cpp index a95855dd6c..0df5400c1a 100644 --- a/src/sksl/ir/SkSLIndexExpression.cpp +++ b/src/sksl/ir/SkSLIndexExpression.cpp @@ -33,10 +33,21 @@ const Type& IndexExpression::IndexType(const Context& context, const Type& type) } std::unique_ptr IndexExpression::Convert(const Context& context, + SymbolTable& symbolTable, std::unique_ptr base, std::unique_ptr index) { // Convert an index expression with an expression inside of it: `arr[a * 3]`. const Type& baseType = base->type(); + if (base->is() && index->is()) { + const Type& baseType = base->as().value(); + if (baseType.isArray()) { + context.fErrors.error(base->fOffset, "multi-dimensional arrays are not supported"); + return nullptr; + } + return std::make_unique(context, /*offset=*/-1, + symbolTable.addArrayDimension(&baseType, + index->as().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 c1b0c9a33f..1814f3ff1b 100644 --- a/src/sksl/ir/SkSLIndexExpression.h +++ b/src/sksl/ir/SkSLIndexExpression.h @@ -28,6 +28,7 @@ struct IndexExpression final : public Expression { // Returns a simplified index-expression; reports errors via the ErrorReporter. static std::unique_ptr Convert(const Context& context, + SymbolTable& symbolTable, std::unique_ptr base, std::unique_ptr index);