Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 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 | #ifndef SKSL_SWITCHCASE |
| 9 | #define SKSL_SWITCHCASE |
| 10 | |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 11 | #include "include/private/SkSLStatement.h" |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 12 | #include "src/sksl/ir/SkSLExpression.h" |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 13 | |
| 14 | #include <inttypes.h> |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 15 | |
| 16 | namespace SkSL { |
| 17 | |
| 18 | /** |
| 19 | * A single case of a 'switch' statement. |
| 20 | */ |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 21 | class SwitchCase final : public Statement { |
| 22 | public: |
| 23 | inline static constexpr Kind kStatementKind = Kind::kSwitchCase; |
| 24 | |
| 25 | static std::unique_ptr<SwitchCase> Make(int line, SKSL_INT value, |
| 26 | std::unique_ptr<Statement> statement) { |
| 27 | return std::unique_ptr<SwitchCase>(new SwitchCase(line, /*isDefault=*/false, value, |
| 28 | std::move(statement))); |
| 29 | } |
| 30 | |
| 31 | static std::unique_ptr<SwitchCase> MakeDefault(int line, std::unique_ptr<Statement> statement) { |
| 32 | return std::unique_ptr<SwitchCase>(new SwitchCase(line, /*isDefault=*/true, -1, |
| 33 | std::move(statement))); |
| 34 | } |
| 35 | |
| 36 | bool isDefault() const { |
| 37 | return fDefault; |
| 38 | } |
| 39 | |
| 40 | SKSL_INT value() const { |
| 41 | SkASSERT(!this->isDefault()); |
| 42 | return fValue; |
| 43 | } |
| 44 | |
| 45 | std::unique_ptr<Statement>& statement() { |
| 46 | return fStatement; |
| 47 | } |
| 48 | |
| 49 | const std::unique_ptr<Statement>& statement() const { |
| 50 | return fStatement; |
| 51 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 52 | |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 53 | std::unique_ptr<Statement> clone() const override { |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 54 | return fDefault ? SwitchCase::MakeDefault(fLine, this->statement()->clone()) |
| 55 | : SwitchCase::Make(fLine, this->value(), this->statement()->clone()); |
Xiaoming Shi | 73dfa20 | 2020-03-12 11:31:35 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 58 | std::string description() const override { |
| 59 | if (this->isDefault()) { |
| 60 | return String::printf("default:\n%s", fStatement->description().c_str()); |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 61 | } else { |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 62 | return String::printf("case %" PRId64 ":\n%s", |
| 63 | (int64_t) this->value(), |
| 64 | fStatement->description().c_str()); |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 65 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 68 | private: |
| 69 | SwitchCase(int line, bool isDefault, SKSL_INT value, std::unique_ptr<Statement> statement) |
| 70 | : INHERITED(line, kStatementKind) |
| 71 | , fDefault(isDefault) |
| 72 | , fValue(std::move(value)) |
| 73 | , fStatement(std::move(statement)) {} |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 74 | |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 75 | bool fDefault; |
| 76 | SKSL_INT fValue; |
| 77 | std::unique_ptr<Statement> fStatement; |
| 78 | |
| 79 | using INHERITED = Statement; |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
Kaido Kert | b108943 | 2024-03-18 19:46:49 -0700 | [diff] [blame] | 82 | } // namespace SkSL |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 83 | |
| 84 | #endif |