SkSLBlock.h revision 5b5f096a038259b8d9084834f877588a0db80250
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 "SkSLStatement.h"
12#include "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    String description() const override {
36        String result("{");
37        for (size_t i = 0; i < fStatements.size(); i++) {
38            result += "\n";
39            result += fStatements[i]->description();
40        }
41        result += "\n}\n";
42        return result;
43    }
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
50    typedef Statement INHERITED;
51};
52
53} // namespace
54
55#endif
56