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 "include/private/SkSLStatement.h"
12 #include "src/sksl/ir/SkSLExpression.h"
13 
14 #include <memory>
15 
16 namespace SkSL {
17 
18 /**
19  * An 'if' statement.
20  */
21 class IfStatement final : public Statement {
22 public:
23     static constexpr Kind kStatementKind = Kind::kIf;
24 
IfStatement(int offset,bool isStatic,std::unique_ptr<Expression> test,std::unique_ptr<Statement> ifTrue,std::unique_ptr<Statement> ifFalse)25     IfStatement(int offset, bool isStatic, std::unique_ptr<Expression> test,
26                 std::unique_ptr<Statement> ifTrue, std::unique_ptr<Statement> ifFalse)
27         : INHERITED(offset, kStatementKind)
28         , fTest(std::move(test))
29         , fIfTrue(std::move(ifTrue))
30         , fIfFalse(std::move(ifFalse))
31         , fIsStatic(isStatic) {}
32 
33     // Creates a potentially-simplified form of the if-statement. Typechecks and coerces the test
34     // expression; reports errors via ErrorReporter.
35     static std::unique_ptr<Statement> Convert(const Context& context, int offset, bool isStatic,
36                                               std::unique_ptr<Expression> test,
37                                               std::unique_ptr<Statement> ifTrue,
38                                               std::unique_ptr<Statement> ifFalse);
39 
40     // Creates a potentially-simplified form of the if-statement; reports errors via ASSERT.
41     static std::unique_ptr<Statement> Make(const Context& context, int offset, bool isStatic,
42                                            std::unique_ptr<Expression> test,
43                                            std::unique_ptr<Statement> ifTrue,
44                                            std::unique_ptr<Statement> ifFalse);
45 
isStatic()46     bool isStatic() const {
47         return fIsStatic;
48     }
49 
test()50     std::unique_ptr<Expression>& test() {
51         return fTest;
52     }
53 
test()54     const std::unique_ptr<Expression>& test() const {
55         return fTest;
56     }
57 
ifTrue()58     std::unique_ptr<Statement>& ifTrue() {
59         return fIfTrue;
60     }
61 
ifTrue()62     const std::unique_ptr<Statement>& ifTrue() const {
63         return fIfTrue;
64     }
65 
ifFalse()66     std::unique_ptr<Statement>& ifFalse() {
67         return fIfFalse;
68     }
69 
ifFalse()70     const std::unique_ptr<Statement>& ifFalse() const {
71         return fIfFalse;
72     }
73 
74     std::unique_ptr<Statement> clone() const override;
75 
76     String description() const override;
77 
78 private:
79     std::unique_ptr<Expression> fTest;
80     std::unique_ptr<Statement> fIfTrue;
81     std::unique_ptr<Statement> fIfFalse;
82     bool fIsStatic;
83 
84     using INHERITED = Statement;
85 };
86 
87 }  // namespace SkSL
88 
89 #endif
90