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
14namespace SkSL {
15
16/**
17 * A block of multiple statements functioning as a single statement.
18 */
19struct Block : public Statement {
20 Block(int offset, std::vector<std::unique_ptr<Statement>> statements,
21 const std::shared_ptr<SymbolTable> symbols = nullptr)
22 : INHERITED(offset, kBlock_Kind)
23 , fSymbols(std::move(symbols))
24 , fStatements(std::move(statements)) {}
25
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
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
43#ifdef SK_DEBUG
44 String description() const override {
45 String result("{");
46 for (size_t i = 0; i < fStatements.size(); i++) {
47 result += "\n";
48 result += fStatements[i]->description();
49 }
50 result += "\n}\n";
51 return result;
52 }
53#endif
54
55 // it's important to keep fStatements defined after (and thus destroyed before) fSymbols,
56 // because destroying statements can modify reference counts in symbols
57 const std::shared_ptr<SymbolTable> fSymbols;
58 std::vector<std::unique_ptr<Statement>> fStatements;
59
60 typedef Statement INHERITED;
61};
62
63} // namespace
64
65#endif
66