ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 | */ |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_BLOCK |
| 9 | #define SKSL_BLOCK |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/sksl/ir/SkSLStatement.h" |
| 12 | #include "src/sksl/ir/SkSLSymbolTable.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | |
| 14 | namespace SkSL { |
| 15 | |
| 16 | /** |
| 17 | * A block of multiple statements functioning as a single statement. |
| 18 | */ |
| 19 | struct Block : public Statement { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 20 | Block(int offset, std::vector<std::unique_ptr<Statement>> statements, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 21 | const std::shared_ptr<SymbolTable> symbols = nullptr) |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 22 | : INHERITED(offset, kBlock_Kind) |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 23 | , fSymbols(std::move(symbols)) |
| 24 | , fStatements(std::move(statements)) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 25 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 26 | bool isEmpty() const override { |
| 27 | for (const auto& s : fStatements) { |
| 28 | if (!s->isEmpty()) { |
| 29 | return false; |
| 30 | } |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 35 | std::unique_ptr<Statement> clone() const override { |
| 36 | std::vector<std::unique_ptr<Statement>> cloned; |
| 37 | for (const auto& s : fStatements) { |
| 38 | cloned.push_back(s->clone()); |
| 39 | } |
| 40 | return std::unique_ptr<Statement>(new Block(fOffset, std::move(cloned), fSymbols)); |
| 41 | } |
| 42 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 43 | String description() const override { |
| 44 | String result("{"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 45 | for (size_t i = 0; i < fStatements.size(); i++) { |
| 46 | result += "\n"; |
| 47 | result += fStatements[i]->description(); |
| 48 | } |
| 49 | result += "\n}\n"; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 50 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 53 | // it's important to keep fStatements defined after (and thus destroyed before) fSymbols, |
| 54 | // because destroying statements can modify reference counts in symbols |
Ethan Nicholas | 6415e0d | 2017-01-19 16:31:32 +0000 | [diff] [blame] | 55 | const std::shared_ptr<SymbolTable> fSymbols; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 56 | std::vector<std::unique_ptr<Statement>> fStatements; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 57 | |
| 58 | typedef Statement INHERITED; |
| 59 | }; |
| 60 | |
| 61 | } // namespace |
| 62 | |
| 63 | #endif |