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 "SkSLStatement.h" 12 #include "SkSLSwitchCase.h" 13 14 namespace SkSL { 15 16 /** 17 * A 'switch' statement. 18 */ 19 struct SwitchStatement : public Statement { SwitchStatementSwitchStatement20 SwitchStatement(int offset, bool isStatic, std::unique_ptr<Expression> value, 21 std::vector<std::unique_ptr<SwitchCase>> cases, 22 const std::shared_ptr<SymbolTable> symbols) 23 : INHERITED(offset, kSwitch_Kind) 24 , fIsStatic(isStatic) 25 , fValue(std::move(value)) 26 , fSymbols(std::move(symbols)) 27 , fCases(std::move(cases)) {} 28 cloneSwitchStatement29 std::unique_ptr<Statement> clone() const override { 30 std::vector<std::unique_ptr<SwitchCase>> cloned; 31 for (const auto& s : fCases) { 32 cloned.push_back(std::unique_ptr<SwitchCase>((SwitchCase*) s->clone().release())); 33 } 34 return std::unique_ptr<Statement>(new SwitchStatement(fOffset, fIsStatic, fValue->clone(), 35 std::move(cloned), fSymbols)); 36 } 37 descriptionSwitchStatement38 String description() const override { 39 String result; 40 if (fIsStatic) { 41 result += "@"; 42 } 43 result += String::printf("switch (%s) {\n", fValue->description().c_str()); 44 for (const auto& c : fCases) { 45 result += c->description(); 46 } 47 result += "}"; 48 return result; 49 } 50 51 bool fIsStatic; 52 std::unique_ptr<Expression> fValue; 53 // it's important to keep fCases defined after (and thus destroyed before) fSymbols, because 54 // destroying statements can modify reference counts in symbols 55 const std::shared_ptr<SymbolTable> fSymbols; 56 std::vector<std::unique_ptr<SwitchCase>> fCases; 57 58 typedef Statement INHERITED; 59 }; 60 61 } // namespace 62 63 #endif 64