skia2/src/sksl/SkSLConstantFolder.h
John Stiles 4599050369 Convert Operators namespace to first-class Operator object.
This lets us write `op.isAssignment()` instead of
`Operators::IsAssignment(op)`.

Change-Id: If35f2ac500b6ccabc364f9104faaad6e62564667
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/369958
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
2021-02-16 16:54:20 +00:00

58 lines
1.8 KiB
C++

/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SKSL_CONSTANT_FOLDER
#define SKSL_CONSTANT_FOLDER
#include <memory>
#include "src/sksl/SkSLDefines.h"
#include "src/sksl/SkSLOperators.h"
namespace SkSL {
class Context;
class ErrorReporter;
class Expression;
/**
* Performs constant folding on IR expressions. This simplifies expressions containing
* compile-time constants, such as replacing `IntLiteral(2) + IntLiteral(2)` with `IntLiteral(4)`.
*/
class ConstantFolder {
public:
/**
* If value is an int literal or const int variable with a known value, returns true and stores
* the value in out. Otherwise returns false.
*/
static bool GetConstantInt(const Expression& value, SKSL_INT* out);
/**
* If value is a float literal or const float variable with a known value, returns true and
* stores the value in out. Otherwise returns false.
*/
static bool GetConstantFloat(const Expression& value, SKSL_FLOAT* out);
/**
* Reports an error and returns true if op is a division / mod operator and right is zero or
* contains a zero element.
*/
static bool ErrorOnDivideByZero(const Context& context, int offset, Operator op,
const Expression& right);
/** Simplifies the binary expression `left OP right`. Returns null if it can't be simplified. */
static std::unique_ptr<Expression> Simplify(const Context& context,
int offset,
const Expression& left,
Operator op,
const Expression& right);
};
} // namespace SkSL
#endif // SKSL_CONSTANT_FOLDER