Flutter Engine
The Flutter Engine
SkSLExpressionStatement.cpp
Go to the documentation of this file.
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
9
16#include "src/sksl/ir/SkSLNop.h"
18
19namespace SkSL {
20
21std::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
31std::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>()) {
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
59 return this->expression()->description(OperatorPrecedence::kStatement) + ";";
60}
61
62} // namespace SkSL
#define SkASSERT(cond)
Definition: SkAssert.h:116
ProgramConfig * fConfig
Definition: SkSLContext.h:33
std::string description() const override
static std::unique_ptr< Statement > Convert(const Context &context, std::unique_ptr< Expression > expr)
static std::unique_ptr< Statement > Make(const Context &context, std::unique_ptr< Expression > expr)
const std::unique_ptr< Expression > & expression() const
static std::unique_ptr< Statement > Make()
Definition: SkSLNop.h:26
bool HasSideEffects(const Expression &expr)
ProgramSettings fSettings