John Stiles | 52d3b01 | 2021-02-26 15:56:48 -0500 | [diff] [blame] | 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 | |
John Stiles | 2dda4b6 | 2021-09-16 13:18:10 -0400 | [diff] [blame] | 8 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 9 | |
| 10 | #include "include/sksl/SkSLErrorReporter.h" |
John Stiles | 52d3b01 | 2021-02-26 15:56:48 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLAnalysis.h" |
| 12 | #include "src/sksl/SkSLContext.h" |
John Stiles | 52d3b01 | 2021-02-26 15:56:48 -0500 | [diff] [blame] | 13 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 14 | |
| 15 | namespace SkSL { |
| 16 | |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 17 | std::unique_ptr<Expression> PostfixExpression::Convert(const Context& context, |
| 18 | std::unique_ptr<Expression> base, |
| 19 | Operator op) { |
John Stiles | 52d3b01 | 2021-02-26 15:56:48 -0500 | [diff] [blame] | 20 | const Type& baseType = base->type(); |
| 21 | if (!baseType.isNumber()) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 22 | context.fErrors->error(base->fLine, |
Ethan Nicholas | 8d11654 | 2021-08-11 13:27:16 -0400 | [diff] [blame] | 23 | "'" + String(op.operatorName()) + "' cannot operate on '" + |
| 24 | baseType.displayName() + "'"); |
John Stiles | 52d3b01 | 2021-02-26 15:56:48 -0500 | [diff] [blame] | 25 | return nullptr; |
| 26 | } |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 27 | if (!Analysis::UpdateVariableRefKind(base.get(), VariableRefKind::kReadWrite, |
| 28 | context.fErrors)) { |
John Stiles | 52d3b01 | 2021-02-26 15:56:48 -0500 | [diff] [blame] | 29 | return nullptr; |
| 30 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 31 | return PostfixExpression::Make(context, std::move(base), op); |
| 32 | } |
| 33 | |
| 34 | std::unique_ptr<Expression> PostfixExpression::Make(const Context&, |
| 35 | std::unique_ptr<Expression> base, |
| 36 | Operator op) { |
| 37 | SkASSERT(base->type().isNumber()); |
| 38 | SkASSERT(Analysis::IsAssignable(*base)); |
John Stiles | 52d3b01 | 2021-02-26 15:56:48 -0500 | [diff] [blame] | 39 | return std::make_unique<PostfixExpression>(std::move(base), op); |
| 40 | } |
| 41 | |
| 42 | } // namespace SkSL |