Migrate type_is_or_contains_array into Type class.

This will be used in the Make functions for BinaryExpression,
TernaryExpression and Function.

Change-Id: Ifde9ca83ab415a00c4712eaa43fd93c695b915cb
Bug: skia:11342
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/377097
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
John Stiles 2021-02-26 20:42:07 -05:00
parent 0b750f2ef5
commit 31381af899
3 changed files with 19 additions and 16 deletions

View File

@ -1008,19 +1008,6 @@ void IRGenerator::finalizeFunction(FunctionDefinition& f) {
Finalizer(this, &f.declaration()).visitStatement(*f.body());
}
static bool type_is_or_contains_array(const Type* type) {
if (type->isStruct()) {
for (const auto& f : type->fields()) {
if (type_is_or_contains_array(f.fType)) {
return true;
}
}
return false;
}
return type->isArray();
}
void IRGenerator::convertFunction(const ASTNode& f) {
SkASSERT(fReferencedIntrinsics.empty());
SK_AT_SCOPE_EXIT(fReferencedIntrinsics.clear());
@ -1035,7 +1022,7 @@ void IRGenerator::convertFunction(const ASTNode& f) {
f.fOffset, "functions may not return type '" + returnType->displayName() + "'");
return;
}
if (this->strictES2Mode() && type_is_or_contains_array(returnType)) {
if (this->strictES2Mode() && returnType->isOrContainsArray()) {
this->errorReporter().error(f.fOffset,
"functions may not return structs containing arrays");
return;
@ -1675,7 +1662,7 @@ std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(
this->errorReporter().error(offset, "assignments to opaque type '" +
left->type().displayName() + "' are not permitted");
}
if (this->strictES2Mode() && type_is_or_contains_array(leftType)) {
if (this->strictES2Mode() && leftType->isOrContainsArray()) {
// Most operators are already rejected on arrays, but GLSL ES 1.0 is very explicit that the
// *only* operator allowed on arrays is subscripting (and the rules against assignment,
// comparison, and even sequence apply to structs containing arrays as well).
@ -1727,7 +1714,7 @@ std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(
trueType->displayName() + "' not allowed");
return nullptr;
}
if (this->strictES2Mode() && type_is_or_contains_array(trueType)) {
if (this->strictES2Mode() && trueType->isOrContainsArray()) {
this->errorReporter().error(offset, "ternary operator result may not be an array (or "
"struct containing an array)");
return nullptr;

View File

@ -275,4 +275,18 @@ std::unique_ptr<Expression> Type::coerceExpression(std::unique_ptr<Expression> e
return Constructor::Make(context, offset, this->scalarTypeForLiteral(), std::move(args));
}
bool Type::isOrContainsArray() const {
if (this->isStruct()) {
for (const Field& f : this->fields()) {
if (f.fType->isOrContainsArray()) {
return true;
}
}
return false;
}
return this->isArray();
}
} // namespace SkSL

View File

@ -358,6 +358,8 @@ public:
return fHighPrecision;
}
bool isOrContainsArray() const;
/**
* Returns the corresponding vector or matrix type with the specified number of columns and
* rows.