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-03-04 19:30:25 +00:00
|
|
|
#include "include/sksl/DSLErrorHandling.h"
|
2021-01-07 15:57:27 +00:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
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-01-11 20:42:44 +00:00
|
|
|
class DSLVar;
|
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.
|
|
|
|
*/
|
|
|
|
DSLExpression(float value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an expression representing a literal float.
|
|
|
|
*/
|
|
|
|
DSLExpression(double value)
|
|
|
|
: DSLExpression((float) value) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an expression representing a literal int.
|
|
|
|
*/
|
|
|
|
DSLExpression(int value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an expression representing a literal bool.
|
|
|
|
*/
|
|
|
|
DSLExpression(bool value);
|
|
|
|
|
2021-01-11 20:42:44 +00:00
|
|
|
/**
|
|
|
|
* Creates an expression representing a variable reference.
|
|
|
|
*/
|
|
|
|
DSLExpression(const DSLVar& var);
|
|
|
|
|
2021-02-25 14:45:49 +00:00
|
|
|
DSLExpression(DSLPossibleExpression expr, PositionInfo pos = PositionInfo());
|
|
|
|
|
2021-01-07 15:57:27 +00:00
|
|
|
~DSLExpression();
|
|
|
|
|
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-02-26 01:50:32 +00:00
|
|
|
DSLExpression x(PositionInfo pos = PositionInfo());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLExpression y(PositionInfo pos = PositionInfo());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLExpression z(PositionInfo pos = PositionInfo());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLExpression w(PositionInfo pos = PositionInfo());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLExpression r(PositionInfo pos = PositionInfo());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLExpression g(PositionInfo pos = PositionInfo());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLExpression b(PositionInfo pos = PositionInfo());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLExpression a(PositionInfo pos = PositionInfo());
|
2021-01-26 19:31:29 +00:00
|
|
|
|
2021-02-11 20:18:31 +00:00
|
|
|
/**
|
|
|
|
* Creates an SkSL struct field access expression.
|
|
|
|
*/
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLExpression field(const char* name, PositionInfo pos = PositionInfo());
|
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-01-07 15:57:27 +00:00
|
|
|
/**
|
|
|
|
* Invalidates this object and returns the SkSL expression it represents.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<SkSL::Expression> release();
|
|
|
|
|
|
|
|
private:
|
|
|
|
DSLExpression(std::unique_ptr<SkSL::Expression> expression);
|
|
|
|
|
2021-01-13 15:38:59 +00:00
|
|
|
/**
|
|
|
|
* Invalidates this object and returns the SkSL expression it represents coerced to the
|
|
|
|
* specified type. If the expression cannot be coerced, reports an error and returns null.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<SkSL::Expression> coerceAndRelease(const SkSL::Type& type);
|
|
|
|
|
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-01-13 15:38:59 +00:00
|
|
|
friend class DSLVar;
|
2021-01-07 15:57:27 +00:00
|
|
|
friend class DSLWriter;
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
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 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();
|
|
|
|
|
|
|
|
DSLExpression x(PositionInfo pos = PositionInfo());
|
|
|
|
|
|
|
|
DSLExpression y(PositionInfo pos = PositionInfo());
|
|
|
|
|
|
|
|
DSLExpression z(PositionInfo pos = PositionInfo());
|
|
|
|
|
|
|
|
DSLExpression w(PositionInfo pos = PositionInfo());
|
|
|
|
|
|
|
|
DSLExpression r(PositionInfo pos = PositionInfo());
|
|
|
|
|
|
|
|
DSLExpression g(PositionInfo pos = PositionInfo());
|
|
|
|
|
|
|
|
DSLExpression b(PositionInfo pos = PositionInfo());
|
|
|
|
|
|
|
|
DSLExpression a(PositionInfo pos = PositionInfo());
|
|
|
|
|
|
|
|
DSLExpression field(const char* name, PositionInfo pos = PositionInfo());
|
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator=(const DSLVar& var);
|
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-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator[](DSLExpression index);
|
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-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
|
|
|
|
|
|
|
std::unique_ptr<SkSL::Expression> release();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<SkSL::Expression> fExpression;
|
|
|
|
|
|
|
|
friend class DSLExpression;
|
|
|
|
};
|
|
|
|
|
2021-01-07 15:57:27 +00:00
|
|
|
} // namespace dsl
|
|
|
|
|
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
#endif
|