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>
This commit is contained in:
Ethan Nicholas 2021-07-20 13:16:19 -04:00 committed by Skia Commit-Bot
parent 88dd356bf1
commit 251e634792
4 changed files with 20 additions and 18 deletions

View File

@ -1421,30 +1421,20 @@ std::unique_ptr<Expression> IRGenerator::convertIndexExpression(const ASTNode& i
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) {

View File

@ -177,7 +177,7 @@ std::unique_ptr<SkSL::Expression> DSLWriter::ConvertField(std::unique_ptr<Expres
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,

View File

@ -33,10 +33,21 @@ const Type& IndexExpression::IndexType(const Context& context, const Type& type)
}
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() + "'");

View File

@ -28,6 +28,7 @@ struct IndexExpression final : public Expression {
// 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);