Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [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_SWITCHSTATEMENT |
| 9 | #define SKSL_SWITCHSTATEMENT |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/sksl/ir/SkSLStatement.h" |
| 12 | #include "src/sksl/ir/SkSLSwitchCase.h" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 13 | |
| 14 | namespace SkSL { |
| 15 | |
Hal Canary | 02eefbe | 2019-06-26 13:54:14 -0400 | [diff] [blame] | 16 | class SymbolTable; |
| 17 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 18 | /** |
| 19 | * A 'switch' statement. |
| 20 | */ |
| 21 | struct SwitchStatement : public Statement { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 22 | SwitchStatement(int offset, bool isStatic, std::unique_ptr<Expression> value, |
Ethan Nicholas | c432b0c | 2017-07-18 13:22:37 -0400 | [diff] [blame] | 23 | std::vector<std::unique_ptr<SwitchCase>> cases, |
| 24 | const std::shared_ptr<SymbolTable> symbols) |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 25 | : INHERITED(offset, kSwitch_Kind) |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 26 | , fIsStatic(isStatic) |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 27 | , fValue(std::move(value)) |
Ethan Nicholas | c432b0c | 2017-07-18 13:22:37 -0400 | [diff] [blame] | 28 | , fSymbols(std::move(symbols)) |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 29 | , fCases(std::move(cases)) {} |
| 30 | |
Ethan Nicholas | b65024b | 2020-05-19 09:31:38 -0400 | [diff] [blame^] | 31 | int nodeCount() const override { |
| 32 | int result = 1 + fValue->nodeCount(); |
| 33 | for (const auto& c : fCases) { |
| 34 | result += c->nodeCount(); |
| 35 | } |
| 36 | return result; |
| 37 | } |
| 38 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 39 | std::unique_ptr<Statement> clone() const override { |
| 40 | std::vector<std::unique_ptr<SwitchCase>> cloned; |
| 41 | for (const auto& s : fCases) { |
| 42 | cloned.push_back(std::unique_ptr<SwitchCase>((SwitchCase*) s->clone().release())); |
| 43 | } |
| 44 | return std::unique_ptr<Statement>(new SwitchStatement(fOffset, fIsStatic, fValue->clone(), |
| 45 | std::move(cloned), fSymbols)); |
| 46 | } |
| 47 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 48 | String description() const override { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 49 | String result; |
| 50 | if (fIsStatic) { |
| 51 | result += "@"; |
| 52 | } |
| 53 | result += String::printf("switch (%s) {\n", fValue->description().c_str()); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 54 | for (const auto& c : fCases) { |
| 55 | result += c->description(); |
| 56 | } |
| 57 | result += "}"; |
| 58 | return result; |
| 59 | } |
| 60 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 61 | bool fIsStatic; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 62 | std::unique_ptr<Expression> fValue; |
Ethan Nicholas | c432b0c | 2017-07-18 13:22:37 -0400 | [diff] [blame] | 63 | // it's important to keep fCases defined after (and thus destroyed before) fSymbols, because |
| 64 | // destroying statements can modify reference counts in symbols |
| 65 | const std::shared_ptr<SymbolTable> fSymbols; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 66 | std::vector<std::unique_ptr<SwitchCase>> fCases; |
| 67 | |
| 68 | typedef Statement INHERITED; |
| 69 | }; |
| 70 | |
| 71 | } // namespace |
| 72 | |
| 73 | #endif |