blob: 8a4449a01c6c59d1489629124e237e0958f47850 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 Nicholas9e1138d2016-11-21 10:39:35 -05007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_BLOCK
9#define SKSL_BLOCK
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/sksl/ir/SkSLStatement.h"
12#include "src/sksl/ir/SkSLSymbolTable.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070013
14namespace SkSL {
15
16/**
17 * A block of multiple statements functioning as a single statement.
18 */
19struct Block : public Statement {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070020 Block(int offset, std::vector<std::unique_ptr<Statement>> statements,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040021 const std::shared_ptr<SymbolTable> symbols = nullptr)
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070022 : INHERITED(offset, kBlock_Kind)
Ethan Nicholas86a43402017-01-19 13:32:00 -050023 , fSymbols(std::move(symbols))
24 , fStatements(std::move(statements)) {}
ethannicholasb3058bd2016-07-01 08:22:01 -070025
Ethan Nicholascb670962017-04-20 19:31:52 -040026 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 Nicholas00543112018-07-31 09:44:36 -040035 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 Nicholas0df1b042017-03-31 13:56:23 -040043 String description() const override {
44 String result("{");
ethannicholasb3058bd2016-07-01 08:22:01 -070045 for (size_t i = 0; i < fStatements.size(); i++) {
46 result += "\n";
47 result += fStatements[i]->description();
48 }
49 result += "\n}\n";
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050050 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -070051 }
52
Ethan Nicholas86a43402017-01-19 13:32:00 -050053 // it's important to keep fStatements defined after (and thus destroyed before) fSymbols,
54 // because destroying statements can modify reference counts in symbols
Ethan Nicholas6415e0d2017-01-19 16:31:32 +000055 const std::shared_ptr<SymbolTable> fSymbols;
Ethan Nicholascb670962017-04-20 19:31:52 -040056 std::vector<std::unique_ptr<Statement>> fStatements;
ethannicholasb3058bd2016-07-01 08:22:01 -070057
58 typedef Statement INHERITED;
59};
60
61} // namespace
62
63#endif