SkSLBlock.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2016 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_BLOCK
  8. #define SKSL_BLOCK
  9. #include "src/sksl/ir/SkSLStatement.h"
  10. #include "src/sksl/ir/SkSLSymbolTable.h"
  11. namespace SkSL {
  12. /**
  13. * A block of multiple statements functioning as a single statement.
  14. */
  15. struct Block : public Statement {
  16. Block(int offset, std::vector<std::unique_ptr<Statement>> statements,
  17. const std::shared_ptr<SymbolTable> symbols = nullptr)
  18. : INHERITED(offset, kBlock_Kind)
  19. , fSymbols(std::move(symbols))
  20. , fStatements(std::move(statements)) {}
  21. bool isEmpty() const override {
  22. for (const auto& s : fStatements) {
  23. if (!s->isEmpty()) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29. std::unique_ptr<Statement> clone() const override {
  30. std::vector<std::unique_ptr<Statement>> cloned;
  31. for (const auto& s : fStatements) {
  32. cloned.push_back(s->clone());
  33. }
  34. return std::unique_ptr<Statement>(new Block(fOffset, std::move(cloned), fSymbols));
  35. }
  36. String description() const override {
  37. String result("{");
  38. for (size_t i = 0; i < fStatements.size(); i++) {
  39. result += "\n";
  40. result += fStatements[i]->description();
  41. }
  42. result += "\n}\n";
  43. return result;
  44. }
  45. // it's important to keep fStatements defined after (and thus destroyed before) fSymbols,
  46. // because destroying statements can modify reference counts in symbols
  47. const std::shared_ptr<SymbolTable> fSymbols;
  48. std::vector<std::unique_ptr<Statement>> fStatements;
  49. typedef Statement INHERITED;
  50. };
  51. } // namespace
  52. #endif