• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
SwitchStatementSwitchStatement22     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 
cloneSwitchStatement31     std::unique_ptr<Statement> clone() const override {
32         std::vector<std::unique_ptr<SwitchCase>> cloned;
33         for (const auto& s : fCases) {
34             cloned.push_back(std::unique_ptr<SwitchCase>((SwitchCase*) s->clone().release()));
35         }
36         return std::unique_ptr<Statement>(new SwitchStatement(fOffset, fIsStatic, fValue->clone(),
37                                                               std::move(cloned), fSymbols));
38     }
39 
descriptionSwitchStatement40     String description() const override {
41         String result;
42         if (fIsStatic) {
43             result += "@";
44         }
45         result += String::printf("switch (%s) {\n", fValue->description().c_str());
46         for (const auto& c : fCases) {
47             result += c->description();
48         }
49         result += "}";
50         return result;
51     }
52 
53     bool fIsStatic;
54     std::unique_ptr<Expression> fValue;
55     // it's important to keep fCases defined after (and thus destroyed before) fSymbols, because
56     // destroying statements can modify reference counts in symbols
57     const std::shared_ptr<SymbolTable> fSymbols;
58     std::vector<std::unique_ptr<SwitchCase>> fCases;
59 
60     typedef Statement INHERITED;
61 };
62 
63 } // namespace
64 
65 #endif
66