| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/sksl/ir/SkSLExpressionStatement.h" |
| 9 |
|
| 10 |
#include "include/core/SkTypes.h" |
| 11 |
#include "src/sksl/SkSLAnalysis.h" |
| 12 |
#include "src/sksl/SkSLContext.h" |
| 13 |
#include "src/sksl/SkSLOperator.h" |
| 14 |
#include "src/sksl/SkSLProgramSettings.h" |
| 15 |
#include "src/sksl/ir/SkSLBinaryExpression.h" |
| 16 |
#include "src/sksl/ir/SkSLNop.h" |
| 17 |
#include "src/sksl/ir/SkSLVariableReference.h" |
| 18 |
|
| 19 |
namespace SkSL { |
| 20 |
|
| 21 |
std::unique_ptr<Statement> ExpressionStatement::Convert(const Context& context, |
| 22 |
std::unique_ptr<Expression> expr) { |
| 23 |
// Expression-statements need to represent a complete expression. |
| 24 |
// Report an error on intermediate expressions, like FunctionReference or TypeReference. |
| 25 |
if (expr->isIncomplete(context)) { |
| 26 |
return nullptr; |
| 27 |
} |
| 28 |
return ExpressionStatement::Make(context, std::move(expr)); |
| 29 |
} |
| 30 |
|
| 31 |
std::unique_ptr<Statement> ExpressionStatement::Make(const Context& context, |
| 32 |
std::unique_ptr<Expression> expr) { |
| 33 |
SkASSERT(!expr->isIncomplete(context)); |
| 34 |
|
| 35 |
if (context.fConfig->fSettings.fOptimize) { |
| 36 |
// Expression-statements without any side effect can be replaced with a Nop. |
| 37 |
if (!Analysis::HasSideEffects(*expr)) { |
| 38 |
return Nop::Make(); |
| 39 |
} |
| 40 |
|
| 41 |
// If this is an assignment statement like `a += b;`, the ref-kind of `a` will be set as |
| 42 |
// read-write; `a` is written-to by the +=, and read-from by the consumer of the expression. |
| 43 |
// We can demote the ref-kind to "write" safely, because the result of the expression is |
| 44 |
// discarded; that is, `a` is never actually read-from. |
| 45 |
if (expr->is<BinaryExpression>()) { |
| 46 |
BinaryExpression& binary = expr->as<BinaryExpression>(); |
| 47 |
if (VariableReference* assignedVar = binary.isAssignmentIntoVariable()) { |
| 48 |
if (assignedVar->refKind() == VariableRefKind::kReadWrite) { |
| 49 |
assignedVar->setRefKind(VariableRefKind::kWrite); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return std::make_unique<ExpressionStatement>(std::move(expr)); |
| 56 |
} |
| 57 |
|
| 58 |
std::string ExpressionStatement::description() const { |
| 59 |
return this->expression()->description(OperatorPrecedence::kStatement) + ";"; |
| 60 |
} |
| 61 |
|
| 62 |
} // namespace SkSL |