| 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 | |
| 11 | #include "src/sksl/ir/SkSLExpression.h" |
| 12 | #include "src/sksl/ir/SkSLStatement.h" |
| 13 | |
| 14 | namespace SkSL { |
| 15 | |
| 16 | /** |
| 17 | * A single case of a 'switch' statement. |
| 18 | */ |
| 19 | struct SwitchCase : public Statement { |
| 20 | SwitchCase(int offset, std::unique_ptr<Expression> value, |
| 21 | std::vector<std::unique_ptr<Statement>> statements) |
| 22 | : INHERITED(offset, kSwitch_Kind) |
| 23 | , fValue(std::move(value)) |
| 24 | , fStatements(std::move(statements)) {} |
| 25 | |
| 26 | std::unique_ptr<Statement> clone() const override { |
| 27 | std::vector<std::unique_ptr<Statement>> cloned; |
| 28 | for (const auto& s : fStatements) { |
| 29 | cloned.push_back(s->clone()); |
| 30 | } |
| 31 | return std::unique_ptr<Statement>(new SwitchCase(fOffset, |
| 32 | fValue ? fValue->clone() : nullptr, |
| 33 | std::move(cloned))); |
| 34 | } |
| 35 | |
| 36 | #ifdef SK_DEBUG |
| 37 | String description() const override { |
| 38 | String result; |
| 39 | if (fValue) { |
| 40 | result.appendf("case %s:\n", fValue->description().c_str()); |
| 41 | } else { |
| 42 | result += "default:\n"; |
| 43 | } |
| 44 | for (const auto& s : fStatements) { |
| 45 | result += s->description() + "\n"; |
| 46 | } |
| 47 | return result; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | // null value implies "default" case |
| 52 | std::unique_ptr<Expression> fValue; |
| 53 | std::vector<std::unique_ptr<Statement>> fStatements; |
| 54 | |
| 55 | typedef Statement INHERITED; |
| 56 | }; |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | #endif |
| 61 |