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 40 #ifdef SK_DEBUG descriptionSwitchStatement41 String description() const override { 42 String result; 43 if (fIsStatic) { 44 result += "@"; 45 } 46 result += String::printf("switch (%s) {\n", fValue->description().c_str()); 47 for (const auto& c : fCases) { 48 result += c->description(); 49 } 50 result += "}"; 51 return result; 52 } 53 #endif 54 55 bool fIsStatic; 56 std::unique_ptr<Expression> fValue; 57 // it's important to keep fCases defined after (and thus destroyed before) fSymbols, because 58 // destroying statements can modify reference counts in symbols 59 const std::shared_ptr<SymbolTable> fSymbols; 60 std::vector<std::unique_ptr<SwitchCase>> fCases; 61 62 typedef Statement INHERITED; 63 }; 64 65 } // namespace 66 67 #endif 68