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