SkSLSwitchStatement.h 1.9 KB

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