• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
INHERITEDBlock22     : INHERITED(offset, kBlock_Kind)
23     , fSymbols(std::move(symbols))
24     , fStatements(std::move(statements)) {}
25 
isEmptyBlock26     bool isEmpty() const override {
27         for (const auto& s : fStatements) {
28             if (!s->isEmpty()) {
29                 return false;
30             }
31         }
32         return true;
33     }
34 
cloneBlock35     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 
descriptionBlock43     String description() const override {
44         String result("{");
45         for (size_t i = 0; i < fStatements.size(); i++) {
46             result += "\n";
47             result += fStatements[i]->description();
48         }
49         result += "\n}\n";
50         return result;
51     }
52 
53     // it's important to keep fStatements defined after (and thus destroyed before) fSymbols,
54     // because destroying statements can modify reference counts in symbols
55     const std::shared_ptr<SymbolTable> fSymbols;
56     std::vector<std::unique_ptr<Statement>> fStatements;
57 
58     typedef Statement INHERITED;
59 };
60 
61 } // namespace
62 
63 #endif
64