blob: 4906e192a6929457613b0641b3a37989453e217e [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 Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_FORSTATEMENT
9#define SKSL_FORSTATEMENT
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/sksl/ir/SkSLExpression.h"
12#include "src/sksl/ir/SkSLStatement.h"
13#include "src/sksl/ir/SkSLSymbolTable.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070014
15namespace SkSL {
16
17/**
18 * A 'for' statement.
19 */
20struct ForStatement : public Statement {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070021 ForStatement(int offset, std::unique_ptr<Statement> initializer,
Ethan Nicholas0df1b042017-03-31 13:56:23 -040022 std::unique_ptr<Expression> test, std::unique_ptr<Expression> next,
ethannicholasd598f792016-07-25 10:08:54 -070023 std::unique_ptr<Statement> statement, std::shared_ptr<SymbolTable> symbols)
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070024 : INHERITED(offset, kFor_Kind)
Ethan Nicholas86a43402017-01-19 13:32:00 -050025 , fSymbols(symbols)
ethannicholasb3058bd2016-07-01 08:22:01 -070026 , fInitializer(std::move(initializer))
27 , fTest(std::move(test))
28 , fNext(std::move(next))
Ethan Nicholas86a43402017-01-19 13:32:00 -050029 , fStatement(std::move(statement)) {}
ethannicholasb3058bd2016-07-01 08:22:01 -070030
Ethan Nicholas00543112018-07-31 09:44:36 -040031 std::unique_ptr<Statement> clone() const override {
32 return std::unique_ptr<Statement>(new ForStatement(fOffset, fInitializer->clone(),
33 fTest->clone(), fNext->clone(),
34 fStatement->clone(), fSymbols));
35 }
36
Ethan Nicholas0df1b042017-03-31 13:56:23 -040037 String description() const override {
38 String result("for (");
ethannicholasb3058bd2016-07-01 08:22:01 -070039 if (fInitializer) {
40 result += fInitializer->description();
Ethan Nicholas0df1b042017-03-31 13:56:23 -040041 }
ethannicholasb3058bd2016-07-01 08:22:01 -070042 result += " ";
43 if (fTest) {
44 result += fTest->description();
Ethan Nicholas0df1b042017-03-31 13:56:23 -040045 }
ethannicholasb3058bd2016-07-01 08:22:01 -070046 result += "; ";
47 if (fNext) {
48 result += fNext->description();
49 }
50 result += ") " + fStatement->description();
51 return result;
52 }
53
Ethan Nicholas86a43402017-01-19 13:32:00 -050054 // it's important to keep fSymbols defined first (and thus destroyed last) because destroying
55 // the other fields can update symbol reference counts
ethannicholasd598f792016-07-25 10:08:54 -070056 const std::shared_ptr<SymbolTable> fSymbols;
Ethan Nicholascb670962017-04-20 19:31:52 -040057 std::unique_ptr<Statement> fInitializer;
Ethan Nicholas86a43402017-01-19 13:32:00 -050058 std::unique_ptr<Expression> fTest;
59 std::unique_ptr<Expression> fNext;
Ethan Nicholascb670962017-04-20 19:31:52 -040060 std::unique_ptr<Statement> fStatement;
ethannicholasb3058bd2016-07-01 08:22:01 -070061
62 typedef Statement INHERITED;
63};
64
65} // namespace
66
67#endif