Revert "Remove operator= from DSL expression types."

This reverts commit b143520625.

Reason for revert: might have caused chromium:1326848

Original change's description:
> Remove operator= from DSL expression types.
>
> This means that in raw DSL code, `foo = bar` will need to be written as
> `foo.assign(bar)`. This is admittedly slightly less convenient. However,
> in practice, raw DSL code is very rare, and in fact we only had one
> use-case for operator= across all of our code. This change will allow us
> to simplify the DSL parser by eliminating the `DSLWrapper` helper class,
> which was only needed because we overloaded `operator=`.
>
> Change-Id: I4fbc39b2c1443fc26a761d424c52e3b9b59169eb
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/541062
> Commit-Queue: John Stiles <johnstiles@google.com>
> Auto-Submit: John Stiles <johnstiles@google.com>
> Reviewed-by: Brian Osman <brianosman@google.com>

Change-Id: I7c7e032c465aba004ba78fd69650c8037e019861
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/541737
Auto-Submit: John Stiles <johnstiles@google.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
This commit is contained in:
John Stiles 2022-05-18 13:21:22 +00:00 committed by SkCQ
parent 37c03c8d73
commit 4b354dfb5b
7 changed files with 97 additions and 86 deletions

View File

@ -106,7 +106,7 @@ public:
/**
* Overloads the '=' operator to create an SkSL assignment statement.
*/
DSLPossibleExpression assign(DSLExpression other);
DSLPossibleExpression operator=(DSLExpression other);
DSLExpression x(Position pos = {});
@ -293,13 +293,13 @@ public:
DSLExpression field(std::string_view name, Position pos = {});
DSLPossibleExpression assign(DSLExpression expr);
DSLPossibleExpression operator=(DSLExpression expr);
DSLPossibleExpression assign(int expr);
DSLPossibleExpression operator=(int expr);
DSLPossibleExpression assign(float expr);
DSLPossibleExpression operator=(float expr);
DSLPossibleExpression assign(double expr);
DSLPossibleExpression operator=(double expr);
DSLPossibleExpression operator[](DSLExpression index);

View File

@ -157,9 +157,6 @@ protected:
* A local variable.
*/
class DSLVar : public DSLVarBase {
private:
using INHERITED = DSLVarBase;
public:
DSLVar() = default;
@ -189,25 +186,25 @@ public:
void swap(DSLVar& other);
using INHERITED::assign;
DSLPossibleExpression operator=(DSLExpression expr);
DSLPossibleExpression assign(DSLVar& param) {
return this->assign(DSLExpression(param));
DSLPossibleExpression operator=(DSLVar& param) {
return this->operator=(DSLExpression(param));
}
template<class Param>
DSLPossibleExpression assign(Param& param) {
return this->assign(DSLExpression(param));
DSLPossibleExpression operator=(Param& param) {
return this->operator=(DSLExpression(param));
}
private:
using INHERITED = DSLVarBase;
};
/**
* A global variable.
*/
class DSLGlobalVar : public DSLVarBase {
private:
using INHERITED = DSLVarBase;
public:
DSLGlobalVar() = default;
@ -240,15 +237,15 @@ public:
void swap(DSLGlobalVar& other);
using INHERITED::assign;
DSLPossibleExpression operator=(DSLExpression expr);
DSLPossibleExpression assign(DSLGlobalVar& param) {
return this->assign(DSLExpression(param));
DSLPossibleExpression operator=(DSLGlobalVar& param) {
return this->operator=(DSLExpression(param));
}
template<class Param>
DSLPossibleExpression assign(Param& param) {
return this->assign(DSLExpression(param));
DSLPossibleExpression operator=(Param& param) {
return this->operator=(DSLExpression(param));
}
/**
@ -269,15 +266,14 @@ private:
DSLExpression eval(ExpressionArray args, Position pos);
std::unique_ptr<SkSL::Expression> methodCall(std::string_view methodName, Position pos);
using INHERITED = DSLVarBase;
};
/**
* A function parameter.
*/
class DSLParameter : public DSLVarBase {
private:
using INHERITED = DSLVarBase;
public:
DSLParameter() = default;
@ -302,16 +298,19 @@ public:
void swap(DSLParameter& other);
using INHERITED::assign;
DSLPossibleExpression operator=(DSLExpression expr);
DSLPossibleExpression assign(DSLParameter& param) {
return this->assign(DSLExpression(param));
DSLPossibleExpression operator=(DSLParameter& param) {
return this->operator=(DSLExpression(param));
}
template<class Param>
DSLPossibleExpression assign(Param& param) {
return this->assign(DSLExpression(param));
DSLPossibleExpression operator=(Param& param) {
return this->operator=(DSLExpression(param));
}
private:
using INHERITED = DSLVarBase;
};
} // namespace dsl

View File

@ -32,7 +32,7 @@ DSLCase::DSLCase(DSLCase&& other)
DSLCase::~DSLCase() {}
DSLCase& DSLCase::operator=(DSLCase&& other) {
fValue.assign(std::move(other.fValue));
fValue = std::move(other.fValue);
fStatements = std::move(other.fStatements);
return *this;
}

View File

@ -192,7 +192,7 @@ DSLExpression DSLExpression::field(std::string_view name, Position pos) {
*ThreadContext::SymbolTable(), this->release(), name), pos);
}
DSLPossibleExpression DSLExpression::assign(DSLExpression right) {
DSLPossibleExpression DSLExpression::operator=(DSLExpression right) {
Position pos = this->position().rangeThrough(right.position());
return BinaryExpression::Convert(ThreadContext::Context(), pos, this->release(),
SkSL::Operator::Kind::EQ, right.release());
@ -395,20 +395,20 @@ DSLExpression DSLPossibleExpression::field(std::string_view name, Position pos)
return DSLExpression(this->release()).field(name, pos);
}
DSLPossibleExpression DSLPossibleExpression::assign(DSLExpression expr) {
return DSLExpression(this->release()).assign(std::move(expr));
DSLPossibleExpression DSLPossibleExpression::operator=(DSLExpression expr) {
return DSLExpression(this->release()) = std::move(expr);
}
DSLPossibleExpression DSLPossibleExpression::assign(int expr) {
return this->assign(DSLExpression(expr));
DSLPossibleExpression DSLPossibleExpression::operator=(int expr) {
return this->operator=(DSLExpression(expr));
}
DSLPossibleExpression DSLPossibleExpression::assign(float expr) {
return this->assign(DSLExpression(expr));
DSLPossibleExpression DSLPossibleExpression::operator=(float expr) {
return this->operator=(DSLExpression(expr));
}
DSLPossibleExpression DSLPossibleExpression::assign(double expr) {
return this->assign(DSLExpression(expr));
DSLPossibleExpression DSLPossibleExpression::operator=(double expr) {
return this->operator=(DSLExpression(expr));
}
DSLPossibleExpression DSLPossibleExpression::operator[](DSLExpression index) {

View File

@ -123,6 +123,18 @@ DSLPossibleExpression DSLVarBase::assign(DSLExpression expr) {
expr.release());
}
DSLPossibleExpression DSLVar::operator=(DSLExpression expr) {
return this->assign(std::move(expr));
}
DSLPossibleExpression DSLGlobalVar::operator=(DSLExpression expr) {
return this->assign(std::move(expr));
}
DSLPossibleExpression DSLParameter::operator=(DSLExpression expr) {
return this->assign(std::move(expr));
}
std::unique_ptr<SkSL::Expression> DSLGlobalVar::methodCall(std::string_view methodName,
Position pos) {
if (!this->fType.isEffectChild()) {

View File

@ -60,10 +60,10 @@ static void append_rtadjust_fixup_to_vertex_main(const Context& context,
};
auto fixupStmt = DSLStatement(
Pos().assign(Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
0,
Pos().w()))
Pos() = Float4(Swizzle(Pos(), X, Y) * Swizzle(Adjust(), X, Z) +
Swizzle(Pos(), W, W) * Swizzle(Adjust(), Y, W),
0,
Pos().w())
);
body.children().push_back(fixupStmt.release());

View File

@ -201,7 +201,7 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFlags, r, ctxInfo) {
SkSL::ProgramKind::kFragment);
Var x(kHalf_Type, "x");
Var y(kFloat_Type, "y");
EXPECT_EQUAL(x.assign(y), "(x = half(y))");
EXPECT_EQUAL(x = y, "(x = half(y))");
}
{
@ -241,12 +241,12 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
"float4(0.0, 1.0, 2.0, 3.0)");
DSLVar x(kFloat_Type, "x");
EXPECT_EQUAL(x.assign(1.0), "(x = 1.0)");
EXPECT_EQUAL(x.assign(1.0f), "(x = 1.0)");
EXPECT_EQUAL(x = 1.0, "(x = 1.0)");
EXPECT_EQUAL(x = 1.0f, "(x = 1.0)");
DSLVar y(kFloat2_Type, "y");
EXPECT_EQUAL(y.x().assign(1.0), "(y.x = 1.0)");
EXPECT_EQUAL(y.x().assign(1.0f), "(y.x = 1.0)");
EXPECT_EQUAL(y.x() = 1.0, "(y.x = 1.0)");
EXPECT_EQUAL(y.x() = 1.0f, "(y.x = 1.0)");
{
ExpectError error(r, "floating point value is infinite");
@ -617,56 +617,56 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLType, r, ctxInfo) {
DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Var f22(kFloat2x2_Type, "f22");
EXPECT_EQUAL(f22.assign(Float2x2(1)), "(f22 = float2x2(1.0))");
EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))");
Var f32(kFloat3x2_Type, "f32");
EXPECT_EQUAL(f32.assign(Float3x2(1, 2, 3, 4, 5, 6)),
EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6),
"(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
Var f42(kFloat4x2_Type, "f42");
EXPECT_EQUAL(f42.assign(Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8)),
EXPECT_EQUAL(f42 = Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8),
"(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
Var f23(kFloat2x3_Type, "f23");
EXPECT_EQUAL(f23.assign(Float2x3(1, Float2(2, 3), 4, Float2(5, 6))),
EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)),
"(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
Var f33(kFloat3x3_Type, "f33");
EXPECT_EQUAL(f33.assign(Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9)),
EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9),
"(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
Var f43(kFloat4x3_Type, "f43");
EXPECT_EQUAL(f43.assign(Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11,12))),
EXPECT_EQUAL(f43 = Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11, 12)),
"(f43 = float4x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0))");
Var f24(kFloat2x4_Type, "f24");
EXPECT_EQUAL(f24.assign(Float2x4(1, 2, 3, 4, 5, 6, 7, 8)),
EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8),
"(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
Var f34(kFloat3x4_Type, "f34");
EXPECT_EQUAL(f34.assign(Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12))),
EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12)),
"(f34 = float3x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0))");
Var f44(kFloat4x4_Type, "f44");
EXPECT_EQUAL(f44.assign(Float4x4(1)), "(f44 = float4x4(1.0))");
EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))");
Var h22(kHalf2x2_Type, "h22");
EXPECT_EQUAL(h22.assign(Half2x2(1)), "(h22 = half2x2(1.0))");
EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))");
Var h32(kHalf3x2_Type, "h32");
EXPECT_EQUAL(h32.assign(Half3x2(1, 2, 3, 4, 5, 6)),
EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6),
"(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
Var h42(kHalf4x2_Type, "h42");
EXPECT_EQUAL(h42.assign(Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8)),
EXPECT_EQUAL(h42 = Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8),
"(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
Var h23(kHalf2x3_Type, "h23");
EXPECT_EQUAL(h23.assign(Half2x3(1, Half2(2, 3), 4, Half2(5, 6))),
EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)),
"(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
Var h33(kHalf3x3_Type, "h33");
EXPECT_EQUAL(h33.assign(Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9)),
EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9),
"(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
Var h43(kHalf4x3_Type, "h43");
EXPECT_EQUAL(h43.assign(Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12))),
EXPECT_EQUAL(h43 = Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12)),
"(h43 = half4x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0))");
Var h24(kHalf2x4_Type, "h24");
EXPECT_EQUAL(h24.assign(Half2x4(1, 2, 3, 4, 5, 6, 7, 8)),
EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8),
"(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
Var h34(kHalf3x4_Type, "h34");
EXPECT_EQUAL(h34.assign(Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12))),
EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12)),
"(h34 = half3x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0))");
Var h44(kHalf4x4_Type, "h44");
EXPECT_EQUAL(h44.assign(Half4x4(1)), "(h44 = half4x4(1.0))");
EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))");
EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)");
EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))");
@ -694,12 +694,12 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
{
ExpectError error(r, "type mismatch: '=' cannot operate on 'float4x3', 'float3x3'");
DSLExpression(f43.assign(f33)).release();
DSLExpression(f43 = f33).release();
}
{
ExpectError error(r, "type mismatch: '=' cannot operate on 'half2x2', 'float2x2'");
DSLExpression(h22.assign(f22)).release();
DSLExpression(h22 = f22).release();
}
{
@ -1312,12 +1312,12 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
EXPECT_EQUAL(Block(), "{ }");
Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
EXPECT_EQUAL(Block(Declare(a), Declare(b), a.assign(b)), "{ int a = 1; int b = 2; (a = b); }");
EXPECT_EQUAL(Block(Declare(a), Declare(b), a = b), "{ int a = 1; int b = 2; (a = b); }");
EXPECT_EQUAL((If(a > 0, --a), ++b), "if ((a > 0)) --a; ++b;");
SkTArray<DSLStatement> statements;
statements.push_back(a.assign(0));
statements.push_back(a = 0);
statements.push_back(++a);
EXPECT_EQUAL(Block(std::move(statements)), "{ (a = 0); ++a; }");
}
@ -1477,12 +1477,12 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
{
ExpectError error(r, "expected 'bool', but found 'int'");
For(i.assign(0), i + 10, ++i, i += 5).release();
For(i = 0, i + 10, ++i, i += 5).release();
}
{
ExpectError error(r, "invalid for loop initializer");
For(If(i == 0, i.assign(1)), i < 10, ++i, i += 5).release();
For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
}
}
@ -1490,7 +1490,7 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Parameter coords(kFloat2_Type, "coords");
DSLFunction(kVoid_Type, "main", coords).define(
sk_FragColor().assign(Half4(coords, 0, 1))
sk_FragColor() = Half4(coords, 0, 1)
);
REPORTER_ASSERT(r, SkSL::ThreadContext::ProgramElements().size() == 1);
EXPECT_EQUAL(*SkSL::ThreadContext::ProgramElements()[0],
@ -1667,12 +1667,12 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
Var a(kFloat_Type, "a"), b(kInt_Type, "b");
SkTArray<DSLStatement> caseStatements;
caseStatements.push_back(a.assign(1));
caseStatements.push_back(a = 1);
caseStatements.push_back(Continue());
Statement x = Switch(b,
Case(0, a.assign(0), Break()),
Case(0, a = 0, Break()),
Case(1, std::move(caseStatements)),
Case(2, a.assign(2) /*Fallthrough*/),
Case(2, a = 2 /*Fallthrough*/),
Default(Discard())
);
EXPECT_EQUAL(x, R"(
@ -1685,9 +1685,9 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
)");
Statement y = StaticSwitch(b,
Case(0, a.assign(0), Break()),
Case(1, a.assign(1), Continue()),
Case(2, a.assign(2) /*Fallthrough*/),
Case(0, a = 0, Break()),
Case(1, a = 1, Continue()),
Case(2, a = 2 /*Fallthrough*/),
Default(Discard())
);
EXPECT_EQUAL(y, R"(
@ -1712,7 +1712,7 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
{
ExpectError error(r, "duplicate default case");
DSLStatement(Switch(0, Default(a.assign(0)), Default(a.assign(1)))).release();
DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
}
{
@ -1765,7 +1765,7 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
Var(kInt_Type, "a").swap(a);
}
EXPECT_EQUAL(Statement(Block(Declare(a), a.assign(123))),
EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)),
"{ int a; (a = 123); }");
}
@ -2019,9 +2019,9 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
DSLVar result(simpleStruct, "result");
DSLFunction(simpleStruct, "returnStruct").define(
Declare(result),
result.field("x").assign(123),
result.field("b").assign(result.field("x") > 0),
result.field("a")[0].assign(result.field("x")),
result.field("x") = 123,
result.field("b") = result.field("x") > 0,
result.field("a")[0] = result.field("x"),
Return(result)
);
REPORTER_ASSERT(r, SkSL::ThreadContext::ProgramElements().size() == 2);
@ -2060,7 +2060,7 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLRTAdjust, r, ctxInfo) {
DSLGlobalVar rtAdjust(kUniform_Modifier, kFloat4_Type, "sk_RTAdjust");
Declare(rtAdjust);
DSLFunction(kVoid_Type, "main").define(
sk_Position().assign(Half4(0))
sk_Position() = Half4(0)
);
REPORTER_ASSERT(r, SkSL::ThreadContext::ProgramElements().size() == 2);
EXPECT_EQUAL(*SkSL::ThreadContext::ProgramElements()[1],
@ -2110,7 +2110,7 @@ DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInlining, r, ctxInfo) {
Return(x * x)
);
DSLFunction(kVoid_Type, "main").define(
sk_FragColor().assign((sqr(2), Half4(sqr(3))))
sk_FragColor() = (sqr(2), Half4(sqr(3)))
);
const char* source = "source test";
std::unique_ptr<SkSL::Program> program = ReleaseProgram(std::make_unique<std::string>(source));