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