2021-01-07 15:57:27 +00:00
|
|
|
/*
|
|
|
|
* 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_DSL_EXPRESSION
|
|
|
|
#define SKSL_DSL_EXPRESSION
|
|
|
|
|
|
|
|
#include "include/core/SkTypes.h"
|
2021-05-06 14:47:06 +00:00
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "include/sksl/DSLWrapper.h"
|
2021-08-13 21:29:51 +00:00
|
|
|
#include "include/sksl/SkSLErrorReporter.h"
|
2021-01-07 15:57:27 +00:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
2022-02-01 20:31:57 +00:00
|
|
|
#include <string_view>
|
2021-01-07 15:57:27 +00:00
|
|
|
|
2021-09-16 14:08:20 +00:00
|
|
|
#if defined(__has_cpp_attribute) && __has_cpp_attribute(clang::reinitializes)
|
|
|
|
#define SK_CLANG_REINITIALIZES [[clang::reinitializes]]
|
|
|
|
#else
|
|
|
|
#define SK_CLANG_REINITIALIZES
|
|
|
|
#endif
|
|
|
|
|
2021-01-07 15:57:27 +00:00
|
|
|
namespace SkSL {
|
|
|
|
|
|
|
|
class Expression;
|
2021-03-04 19:30:25 +00:00
|
|
|
class Type;
|
2021-01-07 15:57:27 +00:00
|
|
|
|
|
|
|
namespace dsl {
|
|
|
|
|
2021-02-25 14:45:49 +00:00
|
|
|
class DSLPossibleExpression;
|
2021-02-09 20:22:57 +00:00
|
|
|
class DSLStatement;
|
2021-05-06 14:47:06 +00:00
|
|
|
class DSLType;
|
Broke DSLVar into separate subclasses
Previously, DSLVar represented local, global, and parameter variables.
This splits it into three separate subclasses.
In addition to just being a cleaner API in general, this also addresses
an issue we ran into with the upcoming DSLParser: previously, a global
DSLVar's storage was not set correctly until DeclareGlobal was called,
so an AddToSymbolTable call prior to DeclareGlobal would create the
SkSL variable with the wrong storage, causing spurious errors on
global-only modifiers. But holding off on the AddToSymbolTable tends to
break constructs like "int x = 0, y = x", so improving the API seemed
like the best way to address it.
Now that we have greater type safety around variables, we can
potentially avoid having to call AddToSymbolTable for DSLVar and
DSLGlobalVar altogether, since we know they are both supposed to end up
in the symbol table, but that isn't something I want to change in this
CL.
Change-Id: I5f390a7384ce0af6a2131d84f97fc5e5b318063f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/428576
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
2021-07-15 14:35:54 +00:00
|
|
|
class DSLVarBase;
|
2021-01-07 15:57:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents an expression such as 'cos(x)' or 'a + b'.
|
|
|
|
*/
|
|
|
|
class DSLExpression {
|
|
|
|
public:
|
|
|
|
DSLExpression(const DSLExpression&) = delete;
|
|
|
|
|
2021-03-04 19:30:25 +00:00
|
|
|
DSLExpression(DSLExpression&&);
|
2021-01-07 15:57:27 +00:00
|
|
|
|
|
|
|
DSLExpression();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an expression representing a literal float.
|
|
|
|
*/
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLExpression(float value, PositionInfo pos = PositionInfo::Capture());
|
2021-01-07 15:57:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an expression representing a literal float.
|
|
|
|
*/
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLExpression(double value, PositionInfo pos = PositionInfo::Capture())
|
2021-01-07 15:57:27 +00:00
|
|
|
: DSLExpression((float) value) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an expression representing a literal int.
|
|
|
|
*/
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLExpression(int value, PositionInfo pos = PositionInfo::Capture());
|
2021-01-07 15:57:27 +00:00
|
|
|
|
2021-07-20 19:23:04 +00:00
|
|
|
/**
|
|
|
|
* Creates an expression representing a literal int.
|
|
|
|
*/
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLExpression(int64_t value, PositionInfo pos = PositionInfo::Capture());
|
2021-07-20 19:23:04 +00:00
|
|
|
|
2021-05-03 18:25:35 +00:00
|
|
|
/**
|
|
|
|
* Creates an expression representing a literal uint.
|
|
|
|
*/
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLExpression(unsigned int value, PositionInfo pos = PositionInfo::Capture());
|
2021-05-03 18:25:35 +00:00
|
|
|
|
2021-01-07 15:57:27 +00:00
|
|
|
/**
|
|
|
|
* Creates an expression representing a literal bool.
|
|
|
|
*/
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLExpression(bool value, PositionInfo pos = PositionInfo::Capture());
|
2021-01-07 15:57:27 +00:00
|
|
|
|
2021-01-11 20:42:44 +00:00
|
|
|
/**
|
|
|
|
* Creates an expression representing a variable reference.
|
|
|
|
*/
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLExpression(DSLVarBase& var, PositionInfo pos = PositionInfo::Capture());
|
2021-03-25 21:49:08 +00:00
|
|
|
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLExpression(DSLVarBase&& var, PositionInfo pos = PositionInfo::Capture());
|
2021-01-11 20:42:44 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression(DSLPossibleExpression expr, PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-06-23 14:27:09 +00:00
|
|
|
explicit DSLExpression(std::unique_ptr<SkSL::Expression> expression);
|
2021-05-06 14:47:06 +00:00
|
|
|
|
2021-08-31 11:40:24 +00:00
|
|
|
static DSLExpression Poison(PositionInfo pos = PositionInfo::Capture());
|
|
|
|
|
2021-01-07 15:57:27 +00:00
|
|
|
~DSLExpression();
|
|
|
|
|
2021-05-06 14:47:06 +00:00
|
|
|
DSLType type();
|
|
|
|
|
2021-01-13 15:38:59 +00:00
|
|
|
/**
|
|
|
|
* Overloads the '=' operator to create an SkSL assignment statement.
|
|
|
|
*/
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator=(DSLExpression other);
|
2021-01-13 15:38:59 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression x(PositionInfo pos = PositionInfo::Capture());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression y(PositionInfo pos = PositionInfo::Capture());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression z(PositionInfo pos = PositionInfo::Capture());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression w(PositionInfo pos = PositionInfo::Capture());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression r(PositionInfo pos = PositionInfo::Capture());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression g(PositionInfo pos = PositionInfo::Capture());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression b(PositionInfo pos = PositionInfo::Capture());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression a(PositionInfo pos = PositionInfo::Capture());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-02-11 20:18:31 +00:00
|
|
|
/**
|
|
|
|
* Creates an SkSL struct field access expression.
|
|
|
|
*/
|
2022-02-01 20:31:57 +00:00
|
|
|
DSLExpression field(std::string_view name, PositionInfo pos = PositionInfo::Capture());
|
2021-02-11 20:18:31 +00:00
|
|
|
|
2021-01-26 15:07:01 +00:00
|
|
|
/**
|
|
|
|
* Creates an SkSL array index expression.
|
|
|
|
*/
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator[](DSLExpression index);
|
2021-01-26 15:07:01 +00:00
|
|
|
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLPossibleExpression operator()(SkTArray<DSLWrapper<DSLExpression>> args,
|
|
|
|
PositionInfo pos = PositionInfo::Capture());
|
2021-05-06 14:47:06 +00:00
|
|
|
|
2021-09-09 19:03:22 +00:00
|
|
|
DSLPossibleExpression operator()(ExpressionArray args,
|
|
|
|
PositionInfo pos = PositionInfo::Capture());
|
|
|
|
|
2021-06-25 16:31:44 +00:00
|
|
|
/**
|
|
|
|
* Returns true if this object contains an expression. DSLExpressions which were created with
|
2021-08-31 20:12:40 +00:00
|
|
|
* the empty constructor or which have already been release()ed do not have a value.
|
|
|
|
* DSLExpressions created with errors are still considered to have a value (but contain poison).
|
2021-06-25 16:31:44 +00:00
|
|
|
*/
|
2021-08-31 20:12:40 +00:00
|
|
|
bool hasValue() const {
|
2021-06-23 14:27:09 +00:00
|
|
|
return fExpression != nullptr;
|
|
|
|
}
|
|
|
|
|
2021-08-31 20:12:40 +00:00
|
|
|
/**
|
|
|
|
* Returns true if this object contains an expression which is not poison.
|
|
|
|
*/
|
|
|
|
bool isValid() const;
|
|
|
|
|
2021-09-16 14:08:20 +00:00
|
|
|
SK_CLANG_REINITIALIZES void swap(DSLExpression& other);
|
2021-07-20 19:23:04 +00:00
|
|
|
|
2021-01-07 15:57:27 +00:00
|
|
|
/**
|
2021-06-25 16:31:44 +00:00
|
|
|
* Invalidates this object and returns the SkSL expression it represents. It is an error to call
|
|
|
|
* this on an invalid DSLExpression.
|
2021-01-07 15:57:27 +00:00
|
|
|
*/
|
|
|
|
std::unique_ptr<SkSL::Expression> release();
|
|
|
|
|
|
|
|
private:
|
2021-06-25 16:31:44 +00:00
|
|
|
/**
|
2021-08-31 20:12:40 +00:00
|
|
|
* Calls release if this expression has a value, otherwise returns null.
|
2021-06-25 16:31:44 +00:00
|
|
|
*/
|
2021-08-31 20:12:40 +00:00
|
|
|
std::unique_ptr<SkSL::Expression> releaseIfPossible();
|
2021-06-25 16:31:44 +00:00
|
|
|
|
2021-01-07 15:57:27 +00:00
|
|
|
std::unique_ptr<SkSL::Expression> fExpression;
|
|
|
|
|
2021-02-16 18:02:57 +00:00
|
|
|
friend DSLExpression SampleChild(int index, DSLExpression coords);
|
|
|
|
|
2021-01-22 20:18:25 +00:00
|
|
|
friend class DSLCore;
|
2021-02-23 17:05:49 +00:00
|
|
|
friend class DSLFunction;
|
2021-02-25 14:45:49 +00:00
|
|
|
friend class DSLPossibleExpression;
|
2021-10-04 16:43:11 +00:00
|
|
|
friend class DSLType;
|
Broke DSLVar into separate subclasses
Previously, DSLVar represented local, global, and parameter variables.
This splits it into three separate subclasses.
In addition to just being a cleaner API in general, this also addresses
an issue we ran into with the upcoming DSLParser: previously, a global
DSLVar's storage was not set correctly until DeclareGlobal was called,
so an AddToSymbolTable call prior to DeclareGlobal would create the
SkSL variable with the wrong storage, causing spurious errors on
global-only modifiers. But holding off on the AddToSymbolTable tends to
break constructs like "int x = 0, y = x", so improving the API seemed
like the best way to address it.
Now that we have greater type safety around variables, we can
potentially avoid having to call AddToSymbolTable for DSLVar and
DSLGlobalVar altogether, since we know they are both supposed to end up
in the symbol table, but that isn't something I want to change in this
CL.
Change-Id: I5f390a7384ce0af6a2131d84f97fc5e5b318063f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/428576
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
2021-07-15 14:35:54 +00:00
|
|
|
friend class DSLVarBase;
|
2021-01-07 15:57:27 +00:00
|
|
|
friend class DSLWriter;
|
2021-05-04 16:22:02 +00:00
|
|
|
template<typename T> friend class DSLWrapper;
|
2021-01-07 15:57:27 +00:00
|
|
|
};
|
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator+(DSLExpression left, DSLExpression right);
|
2021-03-08 22:07:58 +00:00
|
|
|
DSLPossibleExpression operator+(DSLExpression expr);
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator+=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator-(DSLExpression left, DSLExpression right);
|
2021-03-08 22:07:58 +00:00
|
|
|
DSLPossibleExpression operator-(DSLExpression expr);
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator-=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator*(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator*=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator/(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator/=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator%(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator%=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator<<(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator<<=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator>>(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator>>=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator&&(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator||(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator&(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator&=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator|(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator|=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator^(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator^=(DSLExpression left, DSLExpression right);
|
2021-07-19 16:30:37 +00:00
|
|
|
DSLPossibleExpression LogicalXor(DSLExpression left, DSLExpression right);
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator,(DSLExpression left, DSLExpression right);
|
2021-04-19 14:55:18 +00:00
|
|
|
DSLPossibleExpression operator,(DSLPossibleExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator,(DSLExpression left, DSLPossibleExpression right);
|
|
|
|
DSLPossibleExpression operator,(DSLPossibleExpression left, DSLPossibleExpression right);
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator==(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator!=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator>(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator<(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator>=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator<=(DSLExpression left, DSLExpression right);
|
|
|
|
DSLPossibleExpression operator!(DSLExpression expr);
|
|
|
|
DSLPossibleExpression operator~(DSLExpression expr);
|
|
|
|
DSLPossibleExpression operator++(DSLExpression expr);
|
|
|
|
DSLPossibleExpression operator++(DSLExpression expr, int);
|
|
|
|
DSLPossibleExpression operator--(DSLExpression expr);
|
|
|
|
DSLPossibleExpression operator--(DSLExpression expr, int);
|
2021-01-13 15:38:59 +00:00
|
|
|
|
2021-02-25 14:45:49 +00:00
|
|
|
/**
|
|
|
|
* Represents an Expression which may have failed and/or have pending errors to report. Converting a
|
|
|
|
* PossibleExpression into an Expression requires PositionInfo so that any pending errors can be
|
|
|
|
* reported at the correct position.
|
|
|
|
*
|
|
|
|
* PossibleExpression is used instead of Expression in situations where it is not possible to
|
|
|
|
* capture the PositionInfo at the time of Expression construction (notably in operator overloads,
|
|
|
|
* where we cannot add default parameters).
|
|
|
|
*/
|
|
|
|
class DSLPossibleExpression {
|
|
|
|
public:
|
|
|
|
DSLPossibleExpression(std::unique_ptr<SkSL::Expression> expression);
|
|
|
|
|
2021-03-04 19:30:25 +00:00
|
|
|
DSLPossibleExpression(DSLPossibleExpression&& other);
|
2021-02-25 14:45:49 +00:00
|
|
|
|
|
|
|
~DSLPossibleExpression();
|
|
|
|
|
2021-06-23 14:27:09 +00:00
|
|
|
bool valid() const {
|
|
|
|
return fExpression != nullptr;
|
|
|
|
}
|
|
|
|
|
2021-07-20 19:23:04 +00:00
|
|
|
/**
|
|
|
|
* Reports any pending errors at the specified position.
|
|
|
|
*/
|
|
|
|
void reportErrors(PositionInfo pos);
|
|
|
|
|
2021-05-06 14:47:06 +00:00
|
|
|
DSLType type();
|
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression x(PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression y(PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression z(PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression w(PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression r(PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression g(PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression b(PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
DSLExpression a(PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2022-02-01 20:31:57 +00:00
|
|
|
DSLExpression field(std::string_view name, PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator=(DSLExpression expr);
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator=(int expr);
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator=(float expr);
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-04-23 20:15:11 +00:00
|
|
|
DSLPossibleExpression operator=(double expr);
|
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator[](DSLExpression index);
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-08-31 11:40:24 +00:00
|
|
|
DSLPossibleExpression operator()(SkTArray<DSLWrapper<DSLExpression>> args,
|
|
|
|
PositionInfo pos = PositionInfo::Capture());
|
2021-05-06 14:47:06 +00:00
|
|
|
|
2021-09-09 19:03:22 +00:00
|
|
|
DSLPossibleExpression operator()(ExpressionArray args,
|
|
|
|
PositionInfo pos = PositionInfo::Capture());
|
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator++();
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator++(int);
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator--();
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator--(int);
|
2021-02-25 14:45:49 +00:00
|
|
|
|
2021-08-12 15:41:59 +00:00
|
|
|
std::unique_ptr<SkSL::Expression> release(PositionInfo pos = PositionInfo::Capture());
|
2021-02-25 14:45:49 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<SkSL::Expression> fExpression;
|
|
|
|
|
|
|
|
friend class DSLExpression;
|
|
|
|
};
|
|
|
|
|
2021-01-07 15:57:27 +00:00
|
|
|
} // namespace dsl
|
|
|
|
|
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
#endif
|