Removed a number of utility methods from DSLWriter

DSLWriter is about to be renamed to something non-DSL-specific, and
(especially since we're in the middle of trying to break up some of our
big classes anyway) it didn't feel right to leave a pile of extremely
DSL-specific utility methods in it. And since it turns out that none of
these methods really do much of anything anymore, it seemed best to
just kill them all.

Change-Id: I5e257f87108a7a6db7fc1c01bab4e6c1544d60b0
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/455158
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
This commit is contained in:
Ethan Nicholas 2021-10-04 15:35:59 -04:00 committed by SkCQ
parent 9602a4fad0
commit df93db9d32
12 changed files with 94 additions and 176 deletions

View File

@ -159,12 +159,6 @@ private:
*/
std::unique_ptr<SkSL::Expression> releaseIfPossible();
/**
* 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);
std::unique_ptr<SkSL::Expression> fExpression;
friend DSLExpression SampleChild(int index, DSLExpression coords);

View File

@ -82,9 +82,10 @@ public:
/**
* Invokes the function with the given arguments.
*/
DSLExpression call(SkTArray<DSLWrapper<DSLExpression>> args);
DSLExpression call(SkTArray<DSLWrapper<DSLExpression>> args,
PositionInfo pos = PositionInfo::Capture());
DSLExpression call(ExpressionArray args);
DSLExpression call(ExpressionArray args, PositionInfo pos = PositionInfo::Capture());
private:
void collectArgs(ExpressionArray& args) {}

View File

@ -153,18 +153,18 @@ public:
bool isEffectChild() const;
template<typename... Args>
static DSLExpression Construct(DSLType type, DSLVarBase& var, Args&&... args) {
static DSLPossibleExpression Construct(DSLType type, DSLVarBase& var, Args&&... args) {
DSLExpression argArray[] = {var, args...};
return Construct(type, SkMakeSpan(argArray), PositionInfo());
return Construct(type, SkMakeSpan(argArray));
}
template<typename... Args>
static DSLExpression Construct(DSLType type, DSLExpression expr, Args&&... args) {
static DSLPossibleExpression Construct(DSLType type, DSLExpression expr, Args&&... args) {
DSLExpression argArray[] = {std::move(expr), std::move(args)...};
return Construct(type, SkMakeSpan(argArray), PositionInfo());
return Construct(type, SkMakeSpan(argArray));
}
static DSLExpression Construct(DSLType type, SkSpan<DSLExpression> argArray, PositionInfo pos);
static DSLPossibleExpression Construct(DSLType type, SkSpan<DSLExpression> argArray);
private:
const SkSL::Type& skslType() const;

View File

@ -242,16 +242,18 @@ public:
* half4 shader::eval(float2 coords);
* half4 colorFilter::eval(half4 input);
*/
DSLPossibleExpression eval(DSLExpression x, PositionInfo pos = PositionInfo::Capture());
DSLExpression eval(DSLExpression x, PositionInfo pos = PositionInfo::Capture());
/**
* Implements the following method call:
* half4 blender::eval(half4 src, half4 dst);
*/
DSLPossibleExpression eval(DSLExpression x, DSLExpression y,
PositionInfo pos = PositionInfo::Capture());
DSLExpression eval(DSLExpression x, DSLExpression y,
PositionInfo pos = PositionInfo::Capture());
private:
DSLExpression eval(ExpressionArray args, PositionInfo pos);
std::unique_ptr<SkSL::Expression> methodCall(skstd::string_view methodName, PositionInfo pos);
using INHERITED = DSLVarBase;

View File

@ -31,7 +31,9 @@ namespace SkSL {
namespace dsl {
class DSLCore;
class DSLExpression;
class DSLFunction;
class DSLGlobalVar;
class DSLVar;
class DSLWriter;
}
@ -170,7 +172,9 @@ private:
friend class Compiler;
friend class DSLParser;
friend class dsl::DSLCore;
friend class dsl::DSLExpression;
friend class dsl::DSLFunction;
friend class dsl::DSLGlobalVar;
friend class dsl::DSLVar;
friend class dsl::DSLWriter;
};

View File

@ -22,6 +22,7 @@
#include "src/sksl/ir/SkSLIfStatement.h"
#include "src/sksl/ir/SkSLReturnStatement.h"
#include "src/sksl/ir/SkSLStructDefinition.h"
#include "src/sksl/ir/SkSLSwitchStatement.h"
#include "src/sksl/ir/SkSLSwizzle.h"
#include "src/sksl/ir/SkSLTernaryExpression.h"
#include "src/sksl/transform/SkSLTransform.h"
@ -315,14 +316,16 @@ public:
bool isStatic) {
ExpressionArray values;
values.reserve_back(cases.count());
SkTArray<StatementArray> statements;
statements.reserve_back(cases.count());
StatementArray caseBlocks;
caseBlocks.reserve_back(cases.count());
for (DSLCase& c : cases) {
values.push_back(c.fValue.releaseIfPossible());
statements.push_back(std::move(c.fStatements));
caseBlocks.push_back(SkSL::Block::Make(/*line=*/-1,
std::move(c.fStatements), /*symbols=*/nullptr, /*isScope=*/false));
}
return DSLWriter::ConvertSwitch(value.release(), std::move(values), std::move(statements),
isStatic);
return SwitchStatement::Convert(DSLWriter::Context(), /*line=*/-1, isStatic,
value.release(), std::move(values), std::move(caseBlocks),
DSLWriter::SymbolTable());
}
static DSLPossibleStatement While(DSLExpression test, DSLStatement stmt) {

View File

@ -13,8 +13,12 @@
#include "src/sksl/SkSLIRGenerator.h"
#include "src/sksl/dsl/priv/DSLWriter.h"
#include "src/sksl/ir/SkSLBinaryExpression.h"
#include "src/sksl/ir/SkSLFieldAccess.h"
#include "src/sksl/ir/SkSLIndexExpression.h"
#include "src/sksl/ir/SkSLLiteral.h"
#include "src/sksl/ir/SkSLPoison.h"
#include "src/sksl/ir/SkSLPostfixExpression.h"
#include "src/sksl/ir/SkSLPrefixExpression.h"
#include "math.h"
@ -160,15 +164,18 @@ DSLExpression DSLExpression::a(PositionInfo pos) {
}
DSLExpression DSLExpression::field(skstd::string_view name, PositionInfo pos) {
return DSLExpression(DSLWriter::ConvertField(this->release(), name), pos);
return DSLExpression(FieldAccess::Convert(DSLWriter::Context(), *DSLWriter::SymbolTable(),
this->release(), name), pos);
}
DSLPossibleExpression DSLExpression::operator=(DSLExpression right) {
return DSLWriter::ConvertBinary(this->release(), SkSL::Token::Kind::TK_EQ, right.release());
return BinaryExpression::Convert(DSLWriter::Context(), this->release(),
SkSL::Token::Kind::TK_EQ, right.release());
}
DSLPossibleExpression DSLExpression::operator[](DSLExpression right) {
return DSLWriter::ConvertIndex(this->release(), right.release());
return IndexExpression::Convert(DSLWriter::Context(), *DSLWriter::SymbolTable(),
this->release(), right.release());
}
DSLPossibleExpression DSLExpression::operator()(SkTArray<DSLWrapper<DSLExpression>> args,
@ -178,26 +185,31 @@ DSLPossibleExpression DSLExpression::operator()(SkTArray<DSLWrapper<DSLExpressio
for (DSLWrapper<DSLExpression>& arg : args) {
converted.push_back(arg->release());
}
return DSLWriter::Call(this->release(), std::move(converted), pos);
return (*this)(std::move(converted), pos);
}
DSLPossibleExpression DSLExpression::operator()(ExpressionArray args, PositionInfo pos) {
return DSLWriter::Call(this->release(), std::move(args), pos);
// We can't call FunctionCall::Convert directly here, because intrinsic management is handled in
// IRGenerator::call. skbug.com/12500
return DSLWriter::IRGenerator().call(pos.line(), this->release(), std::move(args));
}
#define OP(op, token) \
DSLPossibleExpression operator op(DSLExpression left, DSLExpression right) { \
return DSLWriter::ConvertBinary(left.release(), SkSL::Token::Kind::token, right.release()); \
return BinaryExpression::Convert(DSLWriter::Context(), left.release(), \
SkSL::Token::Kind::token, right.release()); \
}
#define PREFIXOP(op, token) \
DSLPossibleExpression operator op(DSLExpression expr) { \
return DSLWriter::ConvertPrefix(SkSL::Token::Kind::token, expr.release()); \
return PrefixExpression::Convert(DSLWriter::Context(), SkSL::Token::Kind::token, \
expr.release()); \
}
#define POSTFIXOP(op, token) \
DSLPossibleExpression operator op(DSLExpression expr, int) { \
return DSLWriter::ConvertPostfix(expr.release(), SkSL::Token::Kind::token); \
return PostfixExpression::Convert(DSLWriter::Context(), expr.release(), \
SkSL::Token::Kind::token); \
}
OP(+, TK_PLUS)
@ -223,8 +235,8 @@ OP(|=, TK_BITWISEOREQ)
OP(^, TK_BITWISEXOR)
OP(^=, TK_BITWISEXOREQ)
DSLPossibleExpression LogicalXor(DSLExpression left, DSLExpression right) {
return DSLWriter::ConvertBinary(left.release(), SkSL::Token::Kind::TK_LOGICALXOR,
right.release());
return BinaryExpression::Convert(DSLWriter::Context(), left.release(),
SkSL::Token::Kind::TK_LOGICALXOR, right.release());
}
OP(==, TK_EQEQ)
OP(!=, TK_NEQ)
@ -243,28 +255,23 @@ PREFIXOP(--, TK_MINUSMINUS)
POSTFIXOP(--, TK_MINUSMINUS)
DSLPossibleExpression operator,(DSLExpression left, DSLExpression right) {
return DSLWriter::ConvertBinary(left.release(), SkSL::Token::Kind::TK_COMMA,
right.release());
return BinaryExpression::Convert(DSLWriter::Context(), left.release(),
SkSL::Token::Kind::TK_COMMA, right.release());
}
DSLPossibleExpression operator,(DSLPossibleExpression left, DSLExpression right) {
return DSLWriter::ConvertBinary(DSLExpression(std::move(left)).release(),
SkSL::Token::Kind::TK_COMMA, right.release());
return BinaryExpression::Convert(DSLWriter::Context(), DSLExpression(std::move(left)).release(),
SkSL::Token::Kind::TK_COMMA, right.release());
}
DSLPossibleExpression operator,(DSLExpression left, DSLPossibleExpression right) {
return DSLWriter::ConvertBinary(left.release(), SkSL::Token::Kind::TK_COMMA,
DSLExpression(std::move(right)).release());
return BinaryExpression::Convert(DSLWriter::Context(), left.release(),
SkSL::Token::Kind::TK_COMMA, DSLExpression(std::move(right)).release());
}
DSLPossibleExpression operator,(DSLPossibleExpression left, DSLPossibleExpression right) {
return DSLWriter::ConvertBinary(DSLExpression(std::move(left)).release(),
SkSL::Token::Kind::TK_COMMA,
DSLExpression(std::move(right)).release());
}
std::unique_ptr<SkSL::Expression> DSLExpression::coerceAndRelease(const SkSL::Type& type) {
return DSLWriter::Coerce(this->release(), type).release();
return BinaryExpression::Convert(DSLWriter::Context(), DSLExpression(std::move(left)).release(),
SkSL::Token::Kind::TK_COMMA, DSLExpression(std::move(right)).release());
}
DSLPossibleExpression::DSLPossibleExpression(std::unique_ptr<SkSL::Expression> expr)

View File

@ -106,7 +106,7 @@ void DSLFunction::define(DSLBlock block, PositionInfo pos) {
DSLWriter::ProgramElements().push_back(std::move(function));
}
DSLExpression DSLFunction::call(SkTArray<DSLWrapper<DSLExpression>> args) {
DSLExpression DSLFunction::call(SkTArray<DSLWrapper<DSLExpression>> args, PositionInfo pos) {
ExpressionArray released;
released.reserve_back(args.size());
for (DSLWrapper<DSLExpression>& arg : args) {
@ -115,9 +115,12 @@ DSLExpression DSLFunction::call(SkTArray<DSLWrapper<DSLExpression>> args) {
return this->call(std::move(released));
}
DSLExpression DSLFunction::call(ExpressionArray args) {
std::unique_ptr<SkSL::Expression> result = DSLWriter::Call(*fDecl, std::move(args));
return result ? DSLExpression(std::move(result)) : DSLExpression();
DSLExpression DSLFunction::call(ExpressionArray args, PositionInfo pos) {
// We can't call FunctionCall::Convert directly here, because intrinsic management is handled in
// IRGenerator::call. skbug.com/12500
std::unique_ptr<SkSL::Expression> result = DSLWriter::IRGenerator().call(pos.line(), *fDecl,
std::move(args));
return DSLExpression(std::move(result), pos);
}
} // namespace dsl

View File

@ -220,15 +220,24 @@ bool DSLType::reportIllegalTypes(PositionInfo pos) const {
return false;
}
DSLExpression DSLType::Construct(DSLType type, SkSpan<DSLExpression> argArray, PositionInfo pos) {
const SkSL::Type& skslType = type.skslType();
if (type.reportIllegalTypes(pos)) {
DSLPossibleExpression DSLType::Construct(DSLType type, SkSpan<DSLExpression> argArray) {
SkSL::ExpressionArray skslArgs;
skslArgs.reserve_back(argArray.size());
if (type.reportIllegalTypes(PositionInfo())) {
for (DSLExpression& arg : argArray) {
arg.releaseIfPossible();
}
return DSLPossibleExpression(nullptr);
}
return DSLExpression(DSLWriter::Construct(skslType, std::move(argArray)), pos);
for (DSLExpression& arg : argArray) {
if (!arg.hasValue()) {
return DSLPossibleExpression(nullptr);
}
skslArgs.push_back(arg.release());
}
return SkSL::Constructor::Convert(DSLWriter::Context(), /*line=*/-1, type.skslType(),
std::move(skslArgs));
}
DSLType Array(const DSLType& base, int count, PositionInfo pos) {

View File

@ -163,8 +163,9 @@ DSLPossibleExpression DSLVarBase::operator[](DSLExpression&& index) {
}
DSLPossibleExpression DSLVarBase::assign(DSLExpression expr) {
return DSLWriter::ConvertBinary(DSLExpression(*this, PositionInfo()).release(),
SkSL::Token::Kind::TK_EQ, expr.release());
return BinaryExpression::Convert(DSLWriter::Context(),
DSLExpression(*this, PositionInfo()).release(), SkSL::Token::Kind::TK_EQ,
expr.release());
}
DSLPossibleExpression DSLVar::operator=(DSLExpression expr) {
@ -185,26 +186,31 @@ std::unique_ptr<SkSL::Expression> DSLGlobalVar::methodCall(skstd::string_view me
DSLWriter::ReportError("type does not support method calls", pos);
return nullptr;
}
return DSLWriter::ConvertField(DSLExpression(*this, PositionInfo()).release(), methodName);
return FieldAccess::Convert(DSLWriter::Context(), *DSLWriter::SymbolTable(),
DSLExpression(*this, PositionInfo()).release(), methodName);
}
DSLPossibleExpression DSLGlobalVar::eval(DSLExpression x, PositionInfo pos) {
DSLExpression DSLGlobalVar::eval(ExpressionArray args, PositionInfo pos) {
auto method = this->methodCall("eval", pos);
// We can't call FunctionCall::Convert directly here, because intrinsic management is handled in
// IRGenerator::call. skbug.com/12500
return DSLExpression(
method ? DSLWriter::IRGenerator().call(pos.line(), std::move(method), std::move(args))
: nullptr,
pos);
}
DSLExpression DSLGlobalVar::eval(DSLExpression x, PositionInfo pos) {
ExpressionArray converted;
converted.push_back(x.release());
auto method = this->methodCall("eval", pos);
return DSLPossibleExpression(
method ? DSLWriter::Call(std::move(method), std::move(converted), pos) : nullptr);
return this->eval(std::move(converted), pos);
}
DSLPossibleExpression DSLGlobalVar::eval(DSLExpression x, DSLExpression y, PositionInfo pos) {
DSLExpression DSLGlobalVar::eval(DSLExpression x, DSLExpression y, PositionInfo pos) {
ExpressionArray converted;
converted.push_back(x.release());
converted.push_back(y.release());
auto method = this->methodCall("eval", pos);
return DSLPossibleExpression(
method ? DSLWriter::Call(std::move(method), std::move(converted), pos) : nullptr);
return this->eval(std::move(converted), pos);
}
} // namespace dsl

View File

@ -155,83 +155,6 @@ void DSLWriter::AddVarDeclaration(DSLStatement& existing, DSLVar& additional) {
}
}
std::unique_ptr<SkSL::Expression> DSLWriter::Call(const FunctionDeclaration& function,
ExpressionArray arguments,
PositionInfo pos) {
// We can't call FunctionCall::Convert directly here, because intrinsic management is handled in
// IRGenerator::call.
return IRGenerator().call(pos.line(), function, std::move(arguments));
}
std::unique_ptr<SkSL::Expression> DSLWriter::Call(std::unique_ptr<SkSL::Expression> expr,
ExpressionArray arguments,
PositionInfo pos) {
// We can't call FunctionCall::Convert directly here, because intrinsic management is handled in
// IRGenerator::call.
return IRGenerator().call(pos.line(), std::move(expr), std::move(arguments));
}
DSLPossibleExpression DSLWriter::Coerce(std::unique_ptr<Expression> expr, const SkSL::Type& type) {
return type.coerceExpression(std::move(expr), Context());
}
DSLPossibleExpression DSLWriter::Construct(const SkSL::Type& type, SkSpan<DSLExpression> rawArgs) {
SkSL::ExpressionArray args;
args.reserve_back(rawArgs.size());
for (DSLExpression& arg : rawArgs) {
if (!arg.hasValue()) {
return DSLPossibleExpression(nullptr);
}
args.push_back(arg.release());
}
return SkSL::Constructor::Convert(Context(), /*line=*/-1, type, std::move(args));
}
std::unique_ptr<SkSL::Expression> DSLWriter::ConvertBinary(std::unique_ptr<Expression> left,
Operator op,
std::unique_ptr<Expression> right) {
return BinaryExpression::Convert(Context(), std::move(left), op, std::move(right));
}
std::unique_ptr<SkSL::Expression> DSLWriter::ConvertField(std::unique_ptr<Expression> base,
skstd::string_view name) {
return FieldAccess::Convert(Context(), *SymbolTable(), std::move(base), name);
}
std::unique_ptr<SkSL::Expression> DSLWriter::ConvertIndex(std::unique_ptr<Expression> base,
std::unique_ptr<Expression> index) {
return IndexExpression::Convert(Context(), *SymbolTable(), std::move(base), std::move(index));
}
std::unique_ptr<SkSL::Expression> DSLWriter::ConvertPostfix(std::unique_ptr<Expression> expr,
Operator op) {
return PostfixExpression::Convert(Context(), std::move(expr), op);
}
std::unique_ptr<SkSL::Expression> DSLWriter::ConvertPrefix(Operator op,
std::unique_ptr<Expression> expr) {
return PrefixExpression::Convert(Context(), op, std::move(expr));
}
DSLPossibleStatement DSLWriter::ConvertSwitch(std::unique_ptr<Expression> value,
ExpressionArray caseValues,
SkTArray<SkSL::StatementArray> caseStatements,
bool isStatic) {
StatementArray caseBlocks;
caseBlocks.resize(caseStatements.count());
for (int index = 0; index < caseStatements.count(); ++index) {
caseBlocks[index] = std::make_unique<SkSL::Block>(/*line=*/-1,
std::move(caseStatements[index]),
/*symbols=*/nullptr,
/*isScope=*/false);
}
return SwitchStatement::Convert(Context(), /*line=*/-1, isStatic, std::move(value),
std::move(caseValues), std::move(caseBlocks),
IRGenerator().fSymbolTable);
}
void DSLWriter::SetErrorReporter(ErrorReporter* errorReporter) {
SkASSERT(errorReporter);
Context().fErrors = errorReporter;

View File

@ -195,40 +195,6 @@ public:
*/
static void AddVarDeclaration(DSLStatement& existing, DSLVar& additional);
static std::unique_ptr<SkSL::Expression> Call(const FunctionDeclaration& function,
ExpressionArray arguments,
PositionInfo pos = PositionInfo::Capture());
/**
* Invokes expr(arguments), where expr is a function or type reference.
*/
static std::unique_ptr<SkSL::Expression> Call(std::unique_ptr<SkSL::Expression> expr,
ExpressionArray arguments,
PositionInfo pos = PositionInfo::Capture());
static DSLPossibleExpression Coerce(std::unique_ptr<Expression> expr, const SkSL::Type& type);
static DSLPossibleExpression Construct(const SkSL::Type& type, SkSpan<DSLExpression> rawArgs);
static std::unique_ptr<Expression> ConvertBinary(std::unique_ptr<Expression> left, Operator op,
std::unique_ptr<Expression> right);
static std::unique_ptr<SkSL::Expression> ConvertField(std::unique_ptr<Expression> base,
skstd::string_view name);
static std::unique_ptr<Expression> ConvertIndex(std::unique_ptr<Expression> base,
std::unique_ptr<Expression> index);
static std::unique_ptr<Expression> ConvertPostfix(std::unique_ptr<Expression> expr,
Operator op);
static std::unique_ptr<Expression> ConvertPrefix(Operator op, std::unique_ptr<Expression> expr);
static DSLPossibleStatement ConvertSwitch(std::unique_ptr<Expression> value,
ExpressionArray caseValues,
SkTArray<SkSL::StatementArray> caseStatements,
bool isStatic);
/**
* Returns the ErrorReporter associated with the current thread. This object will be notified
* when any DSL errors occur.