blob: a85420a6959fae4ada5561094a87c16e5ccf0dea [file] [log] [blame]
Ethan Nicholasaf197692017-02-27 13:26:45 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/sksl/ir/SkSLStatement.h"
12#include "src/sksl/ir/SkSLSwitchCase.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050013
14namespace SkSL {
15
Hal Canary02eefbe2019-06-26 13:54:14 -040016class SymbolTable;
17
Ethan Nicholasaf197692017-02-27 13:26:45 -050018/**
19 * A 'switch' statement.
20 */
21struct SwitchStatement : public Statement {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070022 SwitchStatement(int offset, bool isStatic, std::unique_ptr<Expression> value,
Ethan Nicholasc432b0c2017-07-18 13:22:37 -040023 std::vector<std::unique_ptr<SwitchCase>> cases,
24 const std::shared_ptr<SymbolTable> symbols)
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070025 : INHERITED(offset, kSwitch_Kind)
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040026 , fIsStatic(isStatic)
Ethan Nicholasaf197692017-02-27 13:26:45 -050027 , fValue(std::move(value))
Ethan Nicholasc432b0c2017-07-18 13:22:37 -040028 , fSymbols(std::move(symbols))
Ethan Nicholasaf197692017-02-27 13:26:45 -050029 , fCases(std::move(cases)) {}
30
Ethan Nicholasb65024b2020-05-19 09:31:38 -040031 int nodeCount() const override {
32 int result = 1 + fValue->nodeCount();
33 for (const auto& c : fCases) {
34 result += c->nodeCount();
35 }
36 return result;
37 }
38
Ethan Nicholas00543112018-07-31 09:44:36 -040039 std::unique_ptr<Statement> clone() const override {
40 std::vector<std::unique_ptr<SwitchCase>> cloned;
41 for (const auto& s : fCases) {
42 cloned.push_back(std::unique_ptr<SwitchCase>((SwitchCase*) s->clone().release()));
43 }
44 return std::unique_ptr<Statement>(new SwitchStatement(fOffset, fIsStatic, fValue->clone(),
45 std::move(cloned), fSymbols));
46 }
47
Ethan Nicholas0df1b042017-03-31 13:56:23 -040048 String description() const override {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040049 String result;
50 if (fIsStatic) {
51 result += "@";
52 }
53 result += String::printf("switch (%s) {\n", fValue->description().c_str());
Ethan Nicholasaf197692017-02-27 13:26:45 -050054 for (const auto& c : fCases) {
55 result += c->description();
56 }
57 result += "}";
58 return result;
59 }
60
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040061 bool fIsStatic;
Ethan Nicholasaf197692017-02-27 13:26:45 -050062 std::unique_ptr<Expression> fValue;
Ethan Nicholasc432b0c2017-07-18 13:22:37 -040063 // it's important to keep fCases defined after (and thus destroyed before) fSymbols, because
64 // destroying statements can modify reference counts in symbols
65 const std::shared_ptr<SymbolTable> fSymbols;
Ethan Nicholasaf197692017-02-27 13:26:45 -050066 std::vector<std::unique_ptr<SwitchCase>> fCases;
67
68 typedef Statement INHERITED;
69};
70
71} // namespace
72
73#endif