[hlsl-writer] Simplify logical and/or code.

This CL unifies the logical and/or HLSL code as it was mostly duplicate
code.

Bug: tint:192
Change-Id: Ied050afb0b054a9ccda1c8896edabf78deaa3709
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/27921
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-09-02 15:09:55 +00:00 committed by Commit Bot service account
parent 593824c9d3
commit 2b5c9b23af
1 changed files with 7 additions and 48 deletions

View File

@ -302,7 +302,8 @@ bool GeneratorImpl::EmitAssign(std::ostream& out,
bool GeneratorImpl::EmitBinary(std::ostream& pre, bool GeneratorImpl::EmitBinary(std::ostream& pre,
std::ostream& out, std::ostream& out,
ast::BinaryExpression* expr) { ast::BinaryExpression* expr) {
if (expr->op() == ast::BinaryOp::kLogicalAnd) { if (expr->op() == ast::BinaryOp::kLogicalAnd ||
expr->op() == ast::BinaryOp::kLogicalOr) {
std::ostringstream lhs_out; std::ostringstream lhs_out;
if (!EmitExpression(pre, lhs_out, expr->lhs())) { if (!EmitExpression(pre, lhs_out, expr->lhs())) {
return false; return false;
@ -316,55 +317,15 @@ bool GeneratorImpl::EmitBinary(std::ostream& pre,
{ {
increment_indent(); increment_indent();
std::ostringstream rhs_out; if (expr->op() == ast::BinaryOp::kLogicalOr) {
if (!EmitExpression(pre, rhs_out, expr->rhs())) {
return false;
}
make_indent(pre);
pre << "if (" << rhs_out.str() << ") {" << std::endl;
{
increment_indent();
make_indent(pre); make_indent(pre);
pre << name << " = true;" << std::endl; pre << name << " = true;" << std::endl;
decrement_indent(); decrement_indent();
make_indent(pre);
pre << "} else {" << std::endl;
increment_indent();
} }
make_indent(pre);
pre << "}" << std::endl;
decrement_indent();
}
make_indent(pre);
pre << "}" << std::endl;
out << "(" << name << ")";
return true;
}
if (expr->op() == ast::BinaryOp::kLogicalOr) {
std::ostringstream lhs_out;
if (!EmitExpression(pre, lhs_out, expr->lhs())) {
return false;
}
auto name = generate_name(kTempNamePrefix);
make_indent(pre);
pre << "bool " << name << " = false;" << std::endl;
make_indent(pre);
pre << "if (" << lhs_out.str() << ") {" << std::endl;
{
increment_indent();
make_indent(pre);
pre << name << " = true;" << std::endl;
decrement_indent();
}
make_indent(pre);
pre << "} else {" << std::endl;
{
increment_indent();
std::ostringstream rhs_out; std::ostringstream rhs_out;
if (!EmitExpression(pre, rhs_out, expr->rhs())) { if (!EmitExpression(pre, rhs_out, expr->rhs())) {
@ -374,15 +335,13 @@ bool GeneratorImpl::EmitBinary(std::ostream& pre,
pre << "if (" << rhs_out.str() << ") {" << std::endl; pre << "if (" << rhs_out.str() << ") {" << std::endl;
{ {
increment_indent(); increment_indent();
make_indent(pre); make_indent(pre);
pre << name << " = true;" << std::endl; pre << name << " = true;" << std::endl;
decrement_indent(); decrement_indent();
} }
make_indent(pre); make_indent(pre);
pre << "}" << std::endl; pre << "}" << std::endl;
decrement_indent(); decrement_indent();
} }
make_indent(pre); make_indent(pre);