2021-02-04 21:07:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2022-03-30 19:48:52 +00:00
|
|
|
#ifndef SKSL_OPERATOR
|
|
|
|
#define SKSL_OPERATOR
|
2021-02-04 21:07:11 +00:00
|
|
|
|
2022-02-01 20:31:57 +00:00
|
|
|
#include <string_view>
|
|
|
|
|
2021-02-04 21:07:11 +00:00
|
|
|
namespace SkSL {
|
2021-02-16 15:55:27 +00:00
|
|
|
|
2021-02-27 01:13:07 +00:00
|
|
|
class Context;
|
|
|
|
class Type;
|
|
|
|
|
2021-02-16 15:55:27 +00:00
|
|
|
class Operator {
|
|
|
|
public:
|
2022-03-30 19:48:52 +00:00
|
|
|
enum class Kind {
|
|
|
|
PLUS,
|
|
|
|
MINUS,
|
|
|
|
STAR,
|
|
|
|
SLASH,
|
|
|
|
PERCENT,
|
|
|
|
SHL,
|
|
|
|
SHR,
|
|
|
|
LOGICALNOT,
|
|
|
|
LOGICALAND,
|
|
|
|
LOGICALOR,
|
|
|
|
LOGICALXOR,
|
|
|
|
BITWISENOT,
|
|
|
|
BITWISEAND,
|
|
|
|
BITWISEOR,
|
|
|
|
BITWISEXOR,
|
|
|
|
EQ,
|
|
|
|
EQEQ,
|
|
|
|
NEQ,
|
|
|
|
LT,
|
|
|
|
GT,
|
|
|
|
LTEQ,
|
|
|
|
GTEQ,
|
|
|
|
PLUSEQ,
|
|
|
|
MINUSEQ,
|
|
|
|
STAREQ,
|
|
|
|
SLASHEQ,
|
|
|
|
PERCENTEQ,
|
|
|
|
SHLEQ,
|
|
|
|
SHREQ,
|
|
|
|
BITWISEANDEQ,
|
|
|
|
BITWISEOREQ,
|
|
|
|
BITWISEXOREQ,
|
|
|
|
PLUSPLUS,
|
|
|
|
MINUSMINUS,
|
|
|
|
COMMA
|
|
|
|
};
|
2021-02-04 21:07:11 +00:00
|
|
|
|
|
|
|
enum class Precedence {
|
|
|
|
kParentheses = 1,
|
|
|
|
kPostfix = 2,
|
|
|
|
kPrefix = 3,
|
|
|
|
kMultiplicative = 4,
|
|
|
|
kAdditive = 5,
|
|
|
|
kShift = 6,
|
|
|
|
kRelational = 7,
|
|
|
|
kEquality = 8,
|
|
|
|
kBitwiseAnd = 9,
|
|
|
|
kBitwiseXor = 10,
|
|
|
|
kBitwiseOr = 11,
|
|
|
|
kLogicalAnd = 12,
|
|
|
|
kLogicalXor = 13,
|
|
|
|
kLogicalOr = 14,
|
|
|
|
kTernary = 15,
|
|
|
|
kAssignment = 16,
|
|
|
|
kSequence = 17,
|
|
|
|
kTopLevel = kSequence
|
|
|
|
};
|
|
|
|
|
2022-03-30 19:48:52 +00:00
|
|
|
Operator(Kind op) : fKind(op) {}
|
|
|
|
|
|
|
|
Kind kind() const { return fKind; }
|
2021-02-16 15:55:27 +00:00
|
|
|
|
2021-12-09 20:02:44 +00:00
|
|
|
bool isEquality() const {
|
2022-03-30 19:48:52 +00:00
|
|
|
return fKind == Kind::EQEQ || fKind == Kind::NEQ;
|
2021-12-09 20:02:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 15:55:27 +00:00
|
|
|
Precedence getBinaryPrecedence() const;
|
2021-02-04 21:07:11 +00:00
|
|
|
|
2022-01-24 16:06:00 +00:00
|
|
|
// Returns the operator name surrounded by the expected whitespace for a tidy binary expression.
|
2021-02-16 15:55:27 +00:00
|
|
|
const char* operatorName() const;
|
2021-02-04 21:07:11 +00:00
|
|
|
|
2022-01-24 16:06:00 +00:00
|
|
|
// Returns the operator name without any surrounding whitespace.
|
2022-02-01 20:31:57 +00:00
|
|
|
std::string_view tightOperatorName() const;
|
2022-01-24 16:06:00 +00:00
|
|
|
|
2021-02-04 21:07:11 +00:00
|
|
|
// Returns true if op is '=' or any compound assignment operator ('+=', '-=', etc.)
|
2021-02-16 15:55:27 +00:00
|
|
|
bool isAssignment() const;
|
2021-02-04 21:07:11 +00:00
|
|
|
|
|
|
|
// Given a compound assignment operator, returns the non-assignment version of the operator
|
|
|
|
// (e.g. '+=' becomes '+')
|
2021-02-16 15:55:27 +00:00
|
|
|
Operator removeAssignment() const;
|
|
|
|
|
|
|
|
/**
|
2022-03-30 19:48:52 +00:00
|
|
|
* Defines the set of relational (comparison) operators:
|
2021-02-16 15:55:27 +00:00
|
|
|
* < <= > >=
|
|
|
|
*/
|
2022-03-30 19:48:52 +00:00
|
|
|
bool isRelational() const;
|
2021-02-16 15:55:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the set of operators which are only valid on integral types:
|
|
|
|
* << <<= >> >>= & &= | |= ^ ^= % %=
|
|
|
|
*/
|
|
|
|
bool isOnlyValidForIntegralTypes() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the set of operators which perform vector/matrix math.
|
|
|
|
* + += - -= * *= / /= % %= << <<= >> >>= & &= | |= ^ ^=
|
|
|
|
*/
|
|
|
|
bool isValidForMatrixOrVector() const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Defines the set of operators allowed by The OpenGL ES Shading Language 1.00, Section 5.1.
|
|
|
|
* The set of illegal (reserved) operators are the ones that only make sense with integral
|
|
|
|
* types. This is not a coincidence: It's because ES2 doesn't require 'int' to be anything but
|
|
|
|
* syntactic sugar for floats with truncation after each operation.
|
|
|
|
*/
|
|
|
|
bool isAllowedInStrictES2Mode() const {
|
|
|
|
return !this->isOnlyValidForIntegralTypes();
|
|
|
|
}
|
|
|
|
|
2021-02-27 01:13:07 +00:00
|
|
|
/**
|
|
|
|
* Determines the operand and result types of a binary expression. Returns true if the
|
|
|
|
* expression is legal, false otherwise. If false, the values of the out parameters are
|
|
|
|
* undefined.
|
|
|
|
*/
|
|
|
|
bool determineBinaryType(const Context& context,
|
|
|
|
const Type& left,
|
|
|
|
const Type& right,
|
|
|
|
const Type** outLeftType,
|
|
|
|
const Type** outRightType,
|
2022-02-07 19:13:02 +00:00
|
|
|
const Type** outResultType) const;
|
2021-02-27 01:13:07 +00:00
|
|
|
|
2021-02-16 15:55:27 +00:00
|
|
|
private:
|
|
|
|
bool isOperator() const;
|
2022-02-07 19:13:02 +00:00
|
|
|
bool isMatrixMultiply(const Type& left, const Type& right) const;
|
2021-02-16 15:55:27 +00:00
|
|
|
|
|
|
|
Kind fKind;
|
|
|
|
};
|
2021-02-04 21:07:11 +00:00
|
|
|
|
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
#endif
|