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 | |
11 | #include "src/sksl/ir/SkSLStatement.h" |
12 | #include "src/sksl/ir/SkSLSwitchCase.h" |
13 | |
14 | namespace SkSL { |
15 | |
16 | class SymbolTable; |
17 | |
18 | /** |
19 | * A 'switch' statement. |
20 | */ |
21 | struct SwitchStatement : public Statement { |
22 | SwitchStatement(int offset, bool isStatic, std::unique_ptr<Expression> value, |
23 | std::vector<std::unique_ptr<SwitchCase>> cases, |
24 | const std::shared_ptr<SymbolTable> symbols) |
25 | : INHERITED(offset, kSwitch_Kind) |
26 | , fIsStatic(isStatic) |
27 | , fValue(std::move(value)) |
28 | , fSymbols(std::move(symbols)) |
29 | , fCases(std::move(cases)) {} |
30 | |
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 | |
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 | |
48 | String description() const override { |
49 | String result; |
50 | if (fIsStatic) { |
51 | result += "@" ; |
52 | } |
53 | result += String::printf("switch (%s) {\n" , fValue->description().c_str()); |
54 | for (const auto& c : fCases) { |
55 | result += c->description(); |
56 | } |
57 | result += "}" ; |
58 | return result; |
59 | } |
60 | |
61 | bool fIsStatic; |
62 | std::unique_ptr<Expression> fValue; |
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; |
66 | std::vector<std::unique_ptr<SwitchCase>> fCases; |
67 | |
68 | typedef Statement INHERITED; |
69 | }; |
70 | |
71 | } // namespace SkSL |
72 | |
73 | #endif |
74 | |