SkSLSwitchCase.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_SWITCHCASE
  8. #define SKSL_SWITCHCASE
  9. #include "src/sksl/ir/SkSLExpression.h"
  10. #include "src/sksl/ir/SkSLStatement.h"
  11. namespace SkSL {
  12. /**
  13. * A single case of a 'switch' statement.
  14. */
  15. struct SwitchCase : public Statement {
  16. SwitchCase(int offset, std::unique_ptr<Expression> value,
  17. std::vector<std::unique_ptr<Statement>> statements)
  18. : INHERITED(offset, kSwitch_Kind)
  19. , fValue(std::move(value))
  20. , fStatements(std::move(statements)) {}
  21. std::unique_ptr<Statement> clone() const override {
  22. std::vector<std::unique_ptr<Statement>> cloned;
  23. for (const auto& s : fStatements) {
  24. cloned.push_back(s->clone());
  25. }
  26. return std::unique_ptr<Statement>(new SwitchCase(fOffset,
  27. fValue ? fValue->clone() : nullptr,
  28. std::move(cloned)));
  29. }
  30. String description() const override {
  31. String result;
  32. if (fValue) {
  33. result.appendf("case %s:\n", fValue->description().c_str());
  34. } else {
  35. result += "default:\n";
  36. }
  37. for (const auto& s : fStatements) {
  38. result += s->description() + "\n";
  39. }
  40. return result;
  41. }
  42. // null value implies "default" case
  43. std::unique_ptr<Expression> fValue;
  44. std::vector<std::unique_ptr<Statement>> fStatements;
  45. typedef Statement INHERITED;
  46. };
  47. } // namespace
  48. #endif