1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ES2PANDA_IR_STATEMENT_IF_STATEMENT_H
17 #define ES2PANDA_IR_STATEMENT_IF_STATEMENT_H
18 
19 #include "ir/statement.h"
20 
21 namespace ark::es2panda::checker {
22 class TSAnalyzer;
23 class ETSAnalyzer;
24 }  // namespace ark::es2panda::checker
25 
26 namespace ark::es2panda::ir {
27 class Expression;
28 
29 class IfStatement final : public Statement {
30 public:
31     IfStatement() = delete;
32     ~IfStatement() override = default;
33 
34     NO_COPY_SEMANTIC(IfStatement);
35     NO_MOVE_SEMANTIC(IfStatement);
36 
IfStatement(Expression *test, Statement *consequent, Statement *alternate)37     explicit IfStatement(Expression *test, Statement *consequent, Statement *alternate)
38         : Statement(AstNodeType::IF_STATEMENT), test_(test), consequent_(consequent), alternate_(alternate)
39     {
40     }
41 
42     [[nodiscard]] const Expression *Test() const noexcept
43     {
44         return test_;
45     }
46 
47     [[nodiscard]] Expression *Test() noexcept
48     {
49         return test_;
50     }
51 
52     [[nodiscard]] const Statement *Consequent() const noexcept
53     {
54         return consequent_;
55     }
56 
57     [[nodiscard]] Statement *Consequent() noexcept
58     {
59         return consequent_;
60     }
61 
62     [[nodiscard]] Statement *Alternate() noexcept
63     {
64         return alternate_;
65     }
66 
67     [[nodiscard]] const Statement *Alternate() const noexcept
68     {
69         return alternate_;
70     }
71 
72     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
73 
74     void Iterate(const NodeTraverser &cb) const override;
75     void Dump(ir::AstDumper *dumper) const override;
76     void Dump(ir::SrcDumper *dumper) const override;
77     void Compile(compiler::PandaGen *pg) const override;
78     void Compile(compiler::ETSGen *etsg) const override;
79     checker::Type *Check(checker::TSChecker *checker) override;
80     checker::Type *Check(checker::ETSChecker *checker) override;
81 
82     void Accept(ASTVisitorT *v) override
83     {
84         v->Accept(this);
85     }
86 
87     [[nodiscard]] IfStatement *Clone(ArenaAllocator *allocator, AstNode *parent) override;
88 
89 private:
90     Expression *test_;
91     Statement *consequent_;
92     Statement *alternate_;
93 };
94 }  // namespace ark::es2panda::ir
95 
96 #endif
97