| 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_FORSTATEMENT |
| 9 | #define SKSL_FORSTATEMENT |
| 10 | |
| 11 | #include "src/sksl/ir/SkSLExpression.h" |
| 12 | #include "src/sksl/ir/SkSLStatement.h" |
| 13 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 14 | |
| 15 | namespace SkSL { |
| 16 | |
| 17 | /** |
| 18 | * A 'for' statement. |
| 19 | */ |
| 20 | struct ForStatement : public Statement { |
| 21 | ForStatement(int offset, std::unique_ptr<Statement> initializer, |
| 22 | std::unique_ptr<Expression> test, std::unique_ptr<Expression> next, |
| 23 | std::unique_ptr<Statement> statement, std::shared_ptr<SymbolTable> symbols) |
| 24 | : INHERITED(offset, kFor_Kind) |
| 25 | , fSymbols(symbols) |
| 26 | , fInitializer(std::move(initializer)) |
| 27 | , fTest(std::move(test)) |
| 28 | , fNext(std::move(next)) |
| 29 | , fStatement(std::move(statement)) {} |
| 30 | |
| 31 | 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 | |
| 37 | #ifdef SK_DEBUG |
| 38 | String description() const override { |
| 39 | String result("for (" ); |
| 40 | if (fInitializer) { |
| 41 | result += fInitializer->description(); |
| 42 | } |
| 43 | result += " " ; |
| 44 | if (fTest) { |
| 45 | result += fTest->description(); |
| 46 | } |
| 47 | result += "; " ; |
| 48 | if (fNext) { |
| 49 | result += fNext->description(); |
| 50 | } |
| 51 | result += ") " + fStatement->description(); |
| 52 | return result; |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | // it's important to keep fSymbols defined first (and thus destroyed last) because destroying |
| 57 | // the other fields can update symbol reference counts |
| 58 | const std::shared_ptr<SymbolTable> fSymbols; |
| 59 | std::unique_ptr<Statement> fInitializer; |
| 60 | std::unique_ptr<Expression> fTest; |
| 61 | std::unique_ptr<Expression> fNext; |
| 62 | std::unique_ptr<Statement> fStatement; |
| 63 | |
| 64 | typedef Statement INHERITED; |
| 65 | }; |
| 66 | |
| 67 | } // namespace |
| 68 | |
| 69 | #endif |
| 70 | |