blob: 28f229369b1f197a335cde66ae45525274d474f6 [file] [log] [blame]
Andrew Top200ce4b2018-01-29 13:43:50 -08001/*
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 Kertb1089432024-03-18 19:46:49 -070011#include "include/private/SkSLStatement.h"
Xiaoming Shi73dfa202020-03-12 11:31:35 -070012#include "src/sksl/ir/SkSLExpression.h"
Kaido Kertb1089432024-03-18 19:46:49 -070013
14#include <inttypes.h>
Andrew Top200ce4b2018-01-29 13:43:50 -080015
16namespace SkSL {
17
18/**
19 * A single case of a 'switch' statement.
20 */
Kaido Kertb1089432024-03-18 19:46:49 -070021class SwitchCase final : public Statement {
22public:
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 Top200ce4b2018-01-29 13:43:50 -080052
Xiaoming Shi73dfa202020-03-12 11:31:35 -070053 std::unique_ptr<Statement> clone() const override {
Kaido Kertb1089432024-03-18 19:46:49 -070054 return fDefault ? SwitchCase::MakeDefault(fLine, this->statement()->clone())
55 : SwitchCase::Make(fLine, this->value(), this->statement()->clone());
Xiaoming Shi73dfa202020-03-12 11:31:35 -070056 }
57
Kaido Kertb1089432024-03-18 19:46:49 -070058 std::string description() const override {
59 if (this->isDefault()) {
60 return String::printf("default:\n%s", fStatement->description().c_str());
Andrew Top200ce4b2018-01-29 13:43:50 -080061 } else {
Kaido Kertb1089432024-03-18 19:46:49 -070062 return String::printf("case %" PRId64 ":\n%s",
63 (int64_t) this->value(),
64 fStatement->description().c_str());
Andrew Top200ce4b2018-01-29 13:43:50 -080065 }
Andrew Top200ce4b2018-01-29 13:43:50 -080066 }
67
Kaido Kertb1089432024-03-18 19:46:49 -070068private:
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 Top200ce4b2018-01-29 13:43:50 -080074
Kaido Kertb1089432024-03-18 19:46:49 -070075 bool fDefault;
76 SKSL_INT fValue;
77 std::unique_ptr<Statement> fStatement;
78
79 using INHERITED = Statement;
Andrew Top200ce4b2018-01-29 13:43:50 -080080};
81
Kaido Kertb1089432024-03-18 19:46:49 -070082} // namespace SkSL
Andrew Top200ce4b2018-01-29 13:43:50 -080083
84#endif