1 /*
2 * Copyright 2021 Google LLC.
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 #include "include/sksl/DSLBlock.h"
9
10 #include "include/sksl/DSLStatement.h"
11 #include "src/sksl/ir/SkSLBlock.h"
12
13 namespace SkSL {
14
15 namespace dsl {
16
DSLBlock(SkSL::StatementArray statements,std::shared_ptr<SymbolTable> symbols)17 DSLBlock::DSLBlock(SkSL::StatementArray statements, std::shared_ptr<SymbolTable> symbols)
18 : fStatements(std::move(statements))
19 , fSymbols(std::move(symbols)) {}
20
DSLBlock(SkTArray<DSLStatement> statements,std::shared_ptr<SymbolTable> symbols)21 DSLBlock::DSLBlock(SkTArray<DSLStatement> statements, std::shared_ptr<SymbolTable> symbols)
22 : fSymbols(std::move(symbols)) {
23 fStatements.reserve_back(statements.count());
24 for (DSLStatement& s : statements) {
25 fStatements.push_back(s.release());
26 }
27 }
28
~DSLBlock()29 DSLBlock::~DSLBlock() {
30 if (!fStatements.empty()) {
31 // This will convert our Block into a DSLStatement, which is then immediately freed.
32 // If an FP is being generated, this will naturally incorporate the Block's Statement into
33 // our FP. If not, this will assert that unused code wasn't incorporated into the program.
34 DSLStatement(std::move(*this));
35 }
36 }
37
release()38 std::unique_ptr<SkSL::Block> DSLBlock::release() {
39 return std::make_unique<SkSL::Block>(/*line=*/-1, std::move(fStatements),
40 std::move(fSymbols));
41 }
42
append(DSLStatement stmt)43 void DSLBlock::append(DSLStatement stmt) {
44 fStatements.push_back(stmt.release());
45 }
46
47 } // namespace dsl
48
49 } // namespace SkSL
50