/src/skia/src/sksl/ir/SkSLSwitchStatement.h
Line | Count | Source |
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/SkSLDefines.h" |
12 | | #include "src/sksl/SkSLPosition.h" |
13 | | #include "src/sksl/ir/SkSLBlock.h" |
14 | | #include "src/sksl/ir/SkSLExpression.h" |
15 | | #include "src/sksl/ir/SkSLIRNode.h" |
16 | | #include "src/sksl/ir/SkSLStatement.h" |
17 | | |
18 | | #include <memory> |
19 | | #include <string> |
20 | | #include <utility> |
21 | | |
22 | | namespace SkSL { |
23 | | |
24 | | class Context; |
25 | | class SymbolTable; |
26 | | |
27 | | /** |
28 | | * A 'switch' statement. |
29 | | */ |
30 | | class SwitchStatement final : public Statement { |
31 | | public: |
32 | | inline static constexpr Kind kIRNodeKind = Kind::kSwitch; |
33 | | |
34 | | SwitchStatement(Position pos, |
35 | | std::unique_ptr<Expression> value, |
36 | | std::unique_ptr<Statement> caseBlock) |
37 | | : INHERITED(pos, kIRNodeKind) |
38 | | , fValue(std::move(value)) |
39 | 16 | , fCaseBlock(std::move(caseBlock)) {} |
40 | | |
41 | | // Create a `switch` statement with an array of case-values and case-statements. |
42 | | // Coerces case values to the proper type and reports an error if cases are duplicated. |
43 | | // Reports errors via the ErrorReporter. |
44 | | static std::unique_ptr<Statement> Convert(const Context& context, |
45 | | Position pos, |
46 | | std::unique_ptr<Expression> value, |
47 | | ExpressionArray caseValues, |
48 | | StatementArray caseStatements, |
49 | | std::unique_ptr<SymbolTable> symbolTable); |
50 | | |
51 | | // Create a `switch` statement with a Block containing only SwitchCases. The SwitchCase block |
52 | | // must already contain non-overlapping, correctly-typed case values. Reports errors via ASSERT. |
53 | | static std::unique_ptr<Statement> Make(const Context& context, |
54 | | Position pos, |
55 | | std::unique_ptr<Expression> value, |
56 | | std::unique_ptr<Statement> caseBlock); |
57 | | |
58 | 48 | std::unique_ptr<Expression>& value() { |
59 | 48 | return fValue; |
60 | 48 | } |
61 | | |
62 | 54 | const std::unique_ptr<Expression>& value() const { |
63 | 54 | return fValue; |
64 | 54 | } |
65 | | |
66 | 16 | std::unique_ptr<Statement>& caseBlock() { |
67 | 16 | return fCaseBlock; |
68 | 16 | } |
69 | | |
70 | 54 | const std::unique_ptr<Statement>& caseBlock() const { |
71 | 54 | return fCaseBlock; |
72 | 54 | } |
73 | | |
74 | 32 | StatementArray& cases() { |
75 | 32 | return fCaseBlock->as<Block>().children(); |
76 | 32 | } |
77 | | |
78 | 16 | const StatementArray& cases() const { |
79 | 16 | return fCaseBlock->as<Block>().children(); |
80 | 16 | } |
81 | | |
82 | | std::string description() const override; |
83 | | |
84 | | private: |
85 | | std::unique_ptr<Expression> fValue; |
86 | | std::unique_ptr<Statement> fCaseBlock; // must be a Block containing only SwitchCase statements |
87 | | |
88 | | using INHERITED = Statement; |
89 | | }; |
90 | | |
91 | | } // namespace SkSL |
92 | | |
93 | | #endif |