Add GetConstantValue, fix const-variable ES2 loop bounds
Prior to this fix, the new test cases would report that the various loop
terms needed to be constant expressions.
Bug: skia:12472
Change-Id: Ic377ed0c4598136ae38fb2b65c93b6d8609d54cb
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/452276
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
diff --git a/src/sksl/SkSLConstantFolder.cpp b/src/sksl/SkSLConstantFolder.cpp
index db184ea..c540cd1 100644
--- a/src/sksl/SkSLConstantFolder.cpp
+++ b/src/sksl/SkSLConstantFolder.cpp
@@ -145,6 +145,15 @@
return true;
}
+bool ConstantFolder::GetConstantValue(const Expression& value, double* out) {
+ const Expression* expr = GetConstantValueForVariable(value);
+ if (!expr->is<Literal>()) {
+ return false;
+ }
+ *out = expr->as<Literal>().value();
+ return true;
+}
+
static bool is_constant_scalar_value(const Expression& inExpr, double match) {
const Expression* expr = ConstantFolder::GetConstantValueForVariable(inExpr);
return (expr->is<Literal>() && expr->as<Literal>().value() == match);