| 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 | |
| 8 | #ifndef SKSL_BLOCK |
| 9 | #define SKSL_BLOCK |
| 10 | |
| 11 | #include "src/sksl/ir/SkSLStatement.h" |
| 12 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 13 | |
| 14 | namespace SkSL { |
| 15 | |
| 16 | /** |
| 17 | * A block of multiple statements functioning as a single statement. |
| 18 | */ |
| 19 | struct Block : public Statement { |
| 20 | Block(int offset, std::vector<std::unique_ptr<Statement>> statements, |
| 21 | const std::shared_ptr<SymbolTable> symbols = nullptr, bool isScope = true) |
| 22 | : INHERITED(offset, kBlock_Kind) |
| 23 | , fSymbols(std::move(symbols)) |
| 24 | , fStatements(std::move(statements)) |
| 25 | , fIsScope(isScope) {} |
| 26 | |
| 27 | bool isEmpty() const override { |
| 28 | for (const auto& s : fStatements) { |
| 29 | if (!s->isEmpty()) { |
| 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | int nodeCount() const override { |
| 37 | int result = 1; |
| 38 | for (const auto& s : fStatements) { |
| 39 | result += s->nodeCount(); |
| 40 | } |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | std::unique_ptr<Statement> clone() const override { |
| 45 | std::vector<std::unique_ptr<Statement>> cloned; |
| 46 | for (const auto& s : fStatements) { |
| 47 | cloned.push_back(s->clone()); |
| 48 | } |
| 49 | return std::unique_ptr<Statement>(new Block(fOffset, std::move(cloned), fSymbols, |
| 50 | fIsScope)); |
| 51 | } |
| 52 | |
| 53 | String description() const override { |
| 54 | String result("{" ); |
| 55 | for (size_t i = 0; i < fStatements.size(); i++) { |
| 56 | result += "\n" ; |
| 57 | result += fStatements[i]->description(); |
| 58 | } |
| 59 | result += "\n}\n" ; |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | // it's important to keep fStatements defined after (and thus destroyed before) fSymbols, |
| 64 | // because destroying statements can modify reference counts in symbols |
| 65 | const std::shared_ptr<SymbolTable> fSymbols; |
| 66 | std::vector<std::unique_ptr<Statement>> fStatements; |
| 67 | // if isScope is false, this is just a group of statements rather than an actual language-level |
| 68 | // block. This allows us to pass around multiple statements as if they were a single unit, with |
| 69 | // no semantic impact. |
| 70 | bool fIsScope; |
| 71 | |
| 72 | typedef Statement INHERITED; |
| 73 | }; |
| 74 | |
| 75 | } // namespace SkSL |
| 76 | |
| 77 | #endif |
| 78 | |