| 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/SkSLPostfixExpression.h" |
| 9 |
|
| 10 |
#include "include/core/SkTypes.h" |
| 11 |
#include "src/sksl/SkSLAnalysis.h" |
| 12 |
#include "src/sksl/SkSLContext.h" |
| 13 |
#include "src/sksl/SkSLErrorReporter.h" |
| 14 |
#include "src/sksl/SkSLOperator.h" |
| 15 |
#include "src/sksl/ir/SkSLType.h" |
| 16 |
#include "src/sksl/ir/SkSLVariableReference.h" |
| 17 |
|
| 18 |
namespace SkSL { |
| 19 |
|
| 20 |
std::unique_ptr<Expression> PostfixExpression::Convert(const Context& context, |
| 21 |
Position pos, |
| 22 |
std::unique_ptr<Expression> base, |
| 23 |
Operator op) { |
| 24 |
if (base->type().isArray() || !base->type().componentType().isNumber()) { |
| 25 |
context.fErrors->error(pos, "'" + std::string(op.tightOperatorName()) + |
| 26 |
"' cannot operate on '" + base->type().displayName() + "'"); |
| 27 |
return nullptr; |
| 28 |
} |
| 29 |
if (!Analysis::UpdateVariableRefKind(base.get(), VariableRefKind::kReadWrite, |
| 30 |
context.fErrors)) { |
| 31 |
return nullptr; |
| 32 |
} |
| 33 |
return PostfixExpression::Make(context, pos, std::move(base), op); |
| 34 |
} |
| 35 |
|
| 36 |
std::unique_ptr<Expression> PostfixExpression::Make(const Context& context, |
| 37 |
Position pos, |
| 38 |
std::unique_ptr<Expression> base, |
| 39 |
Operator op) { |
| 40 |
SkASSERT(!base->type().isArray() && base->type().componentType().isNumber()); |
| 41 |
SkASSERT(Analysis::IsAssignable(*base)); |
| 42 |
return std::make_unique<PostfixExpression>(pos, std::move(base), op); |
| 43 |
} |
| 44 |
|
| 45 |
std::string PostfixExpression::description(OperatorPrecedence parentPrecedence) const { |
| 46 |
bool needsParens = (OperatorPrecedence::kPostfix >= parentPrecedence); |
| 47 |
return std::string(needsParens ? "(" : "") + |
| 48 |
this->operand()->description(OperatorPrecedence::kPostfix) + |
| 49 |
std::string(this->getOperator().tightOperatorName()) + |
| 50 |
std::string(needsParens ? ")" : ""); |
| 51 |
} |
| 52 |
|
| 53 |
} // namespace SkSL |