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 | int nodeCount() const override { |
32 | int result = 1; |
33 | if (fInitializer) { |
34 | result += fInitializer->nodeCount(); |
35 | } |
36 | if (fTest) { |
37 | result += fTest->nodeCount(); |
38 | } |
39 | if (fNext) { |
40 | result += fNext->nodeCount(); |
41 | } |
42 | result += fStatement->nodeCount(); |
43 | return result; |
44 | } |
45 | |
46 | std::unique_ptr<Statement> clone() const override { |
47 | return std::unique_ptr<Statement>(new ForStatement(fOffset, |
48 | fInitializer ? fInitializer->clone() : nullptr, |
49 | fTest ? fTest->clone() : nullptr, |
50 | fNext ? fNext->clone() : nullptr, |
51 | fStatement->clone(), |
52 | fSymbols)); |
53 | } |
54 | |
55 | String description() const override { |
56 | String result("for (" ); |
57 | if (fInitializer) { |
58 | result += fInitializer->description(); |
59 | } else { |
60 | result += ";" ; |
61 | } |
62 | result += " " ; |
63 | if (fTest) { |
64 | result += fTest->description(); |
65 | } |
66 | result += "; " ; |
67 | if (fNext) { |
68 | result += fNext->description(); |
69 | } |
70 | result += ") " + fStatement->description(); |
71 | return result; |
72 | } |
73 | |
74 | // it's important to keep fSymbols defined first (and thus destroyed last) because destroying |
75 | // the other fields can update symbol reference counts |
76 | const std::shared_ptr<SymbolTable> fSymbols; |
77 | std::unique_ptr<Statement> fInitializer; |
78 | std::unique_ptr<Expression> fTest; |
79 | std::unique_ptr<Expression> fNext; |
80 | std::unique_ptr<Statement> fStatement; |
81 | |
82 | typedef Statement INHERITED; |
83 | }; |
84 | |
85 | } // namespace SkSL |
86 | |
87 | #endif |
88 | |