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
14namespace SkSL {
15
16/**
17 * A single case of a 'switch' statement.
18 */
19struct 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 int nodeCount() const override {
27 int result = 1;
28 if (fValue) {
29 result += fValue->nodeCount();
30 }
31 for (const auto& s : fStatements) {
32 result += s->nodeCount();
33 }
34 return result;
35 }
36
37 std::unique_ptr<Statement> clone() const override {
38 std::vector<std::unique_ptr<Statement>> cloned;
39 for (const auto& s : fStatements) {
40 cloned.push_back(s->clone());
41 }
42 return std::unique_ptr<Statement>(new SwitchCase(fOffset,
43 fValue ? fValue->clone() : nullptr,
44 std::move(cloned)));
45 }
46
47 String description() const override {
48 String result;
49 if (fValue) {
50 result.appendf("case %s:\n", fValue->description().c_str());
51 } else {
52 result += "default:\n";
53 }
54 for (const auto& s : fStatements) {
55 result += s->description() + "\n";
56 }
57 return result;
58 }
59
60 // null value implies "default" case
61 std::unique_ptr<Expression> fValue;
62 std::vector<std::unique_ptr<Statement>> fStatements;
63
64 typedef Statement INHERITED;
65};
66
67} // namespace SkSL
68
69#endif
70