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_IFSTATEMENT |
9 | #define SKSL_IFSTATEMENT |
10 | |
11 | #include "src/sksl/ir/SkSLExpression.h" |
12 | #include "src/sksl/ir/SkSLStatement.h" |
13 | |
14 | namespace SkSL { |
15 | |
16 | /** |
17 | * An 'if' statement. |
18 | */ |
19 | struct IfStatement : public Statement { |
20 | IfStatement(int offset, bool isStatic, std::unique_ptr<Expression> test, |
21 | std::unique_ptr<Statement> ifTrue, std::unique_ptr<Statement> ifFalse) |
22 | : INHERITED(offset, kIf_Kind) |
23 | , fIsStatic(isStatic) |
24 | , fTest(std::move(test)) |
25 | , fIfTrue(std::move(ifTrue)) |
26 | , fIfFalse(std::move(ifFalse)) {} |
27 | |
28 | int nodeCount() const override { |
29 | return 1 + fTest->nodeCount() + fIfTrue->nodeCount() + |
30 | (fIfFalse ? fIfFalse->nodeCount() : 0); |
31 | } |
32 | |
33 | std::unique_ptr<Statement> clone() const override { |
34 | return std::unique_ptr<Statement>(new IfStatement(fOffset, fIsStatic, fTest->clone(), |
35 | fIfTrue->clone(), fIfFalse ? fIfFalse->clone() : nullptr)); |
36 | } |
37 | |
38 | String description() const override { |
39 | String result; |
40 | if (fIsStatic) { |
41 | result += "@" ; |
42 | } |
43 | result += "if (" + fTest->description() + ") " + fIfTrue->description(); |
44 | if (fIfFalse) { |
45 | result += " else " + fIfFalse->description(); |
46 | } |
47 | return result; |
48 | } |
49 | |
50 | bool fIsStatic; |
51 | std::unique_ptr<Expression> fTest; |
52 | std::unique_ptr<Statement> fIfTrue; |
53 | // may be null |
54 | std::unique_ptr<Statement> fIfFalse; |
55 | |
56 | typedef Statement INHERITED; |
57 | }; |
58 | |
59 | } // namespace SkSL |
60 | |
61 | #endif |
62 | |