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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SKSL_OPERATORS
|
|
|
|
#define SKSL_OPERATORS
|
|
|
|
|
2022-01-24 16:06:00 +00:00
|
|
|
#include "include/core/SkStringView.h"
|
2021-03-04 19:30:25 +00:00
|
|
|
#include "include/private/SkSLDefines.h"
|
2021-02-04 21:07:11 +00:00
|
|
|
#include "src/sksl/SkSLLexer.h"
|
|
|
|
|
|
|
|
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:
|
|
|
|
using Kind = Token::Kind;
|
|
|
|
|
|
|
|
// Allow implicit conversion from Token::Kind, since this is just a utility wrapper on top.
|
|
|
|
Operator(Token::Kind t) : fKind(t) {
|
2021-06-25 15:05:20 +00:00
|
|
|
SkASSERTF(this->isOperator(), "token-kind %d is not an operator", (int)fKind);
|
2021-02-16 15:55:27 +00:00
|
|
|
}
|
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
|
|
|
|
};
|
|
|
|
|
2021-02-16 15:55:27 +00:00
|
|
|
Token::Kind kind() const { return fKind; }
|
|
|
|
|
2021-12-09 20:02:44 +00:00
|
|
|
bool isEquality() const {
|
|
|
|
return fKind == Token::Kind::TK_EQEQ || fKind == Token::Kind::TK_NEQ;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
skstd::string_view tightOperatorName() const;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the set of logical (comparison) operators:
|
|
|
|
* < <= > >=
|
|
|
|
*/
|
|
|
|
bool isLogical() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
const Type** outResultType);
|
|
|
|
|
2021-02-16 15:55:27 +00:00
|
|
|
private:
|
|
|
|
bool isOperator() const;
|
2021-02-27 01:13:07 +00:00
|
|
|
bool isMatrixMultiply(const Type& left, const Type& right);
|
2021-02-16 15:55:27 +00:00
|
|
|
|
|
|
|
Kind fKind;
|
|
|
|
};
|
2021-02-04 21:07:11 +00:00
|
|
|
|
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
#endif
|