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/SkSLNodeArrayWrapper.h"
12 #include "src/sksl/ir/SkSLStatement.h"
13 #include "src/sksl/ir/SkSLSwitchCase.h"
14 
15 namespace SkSL {
16 
17 class SymbolTable;
18 
19 /**
20  * A 'switch' statement.
21  */
22 class SwitchStatement final : public Statement {
23 public:
24     static constexpr Kind kStatementKind = Kind::kSwitch;
25 
SwitchStatement(int offset,bool isStatic,std::unique_ptr<Expression> value,std::vector<std::unique_ptr<SwitchCase>> cases,const std::shared_ptr<SymbolTable> symbols)26     SwitchStatement(int offset, bool isStatic, std::unique_ptr<Expression> value,
27                     std::vector<std::unique_ptr<SwitchCase>> cases,
28                     const std::shared_ptr<SymbolTable> symbols)
29         : INHERITED(offset, kStatementKind)
30         , fIsStatic(isStatic)
31         , fValue(std::move(value))
32         , fCases(std::move(cases))
33         , fSymbols(std::move(symbols)) {}
34 
value()35     std::unique_ptr<Expression>& value() {
36         return fValue;
37     }
38 
value()39     const std::unique_ptr<Expression>& value() const {
40         return fValue;
41     }
42 
cases()43     std::vector<std::unique_ptr<SwitchCase>>& cases() {
44         return fCases;
45     }
46 
cases()47     const std::vector<std::unique_ptr<SwitchCase>>& cases() const {
48         return fCases;
49     }
50 
isStatic()51     bool isStatic() const {
52         return fIsStatic;
53     }
54 
symbols()55     const std::shared_ptr<SymbolTable>& symbols() const {
56         return fSymbols;
57     }
58 
clone()59     std::unique_ptr<Statement> clone() const override {
60         std::vector<std::unique_ptr<SwitchCase>> cloned;
61         for (const std::unique_ptr<SwitchCase>& sc : this->cases()) {
62             cloned.emplace_back(&sc->clone().release()->as<SwitchCase>());
63         }
64         return std::unique_ptr<Statement>(new SwitchStatement(
65                                                       fOffset,
66                                                       this->isStatic(),
67                                                       this->value()->clone(),
68                                                       std::move(cloned),
69                                                       SymbolTable::WrapIfBuiltin(this->symbols())));
70     }
71 
description()72     String description() const override {
73         String result;
74         if (this->isStatic()) {
75             result += "@";
76         }
77         result += String::printf("switch (%s) {\n", this->value()->description().c_str());
78         for (const auto& c : this->cases()) {
79             result += c->description();
80         }
81         result += "}";
82         return result;
83     }
84 
85 private:
86     bool fIsStatic;
87     std::unique_ptr<Expression> fValue;
88     std::vector<std::unique_ptr<SwitchCase>> fCases;
89     std::shared_ptr<SymbolTable> fSymbols;
90 
91     using INHERITED = Statement;
92 };
93 
94 }  // namespace SkSL
95 
96 #endif
97