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_EXPRESSION_CONDITIONAL_EXPRESSION_H
17#define ES2PANDA_IR_EXPRESSION_CONDITIONAL_EXPRESSION_H
18
19#include "ir/expression.h"
20
21namespace ark::es2panda::checker {
22class TSAnalyzer;
23class ETSAnalyzer;
24}  // namespace ark::es2panda::checker
25
26namespace ark::es2panda::ir {
27class ConditionalExpression : public Expression {
28public:
29    ConditionalExpression() = delete;
30    ~ConditionalExpression() override = default;
31
32    NO_COPY_SEMANTIC(ConditionalExpression);
33    NO_MOVE_SEMANTIC(ConditionalExpression);
34
35    explicit ConditionalExpression(Expression *test, Expression *consequent, Expression *alternate)
36        : Expression(AstNodeType::CONDITIONAL_EXPRESSION), test_(test), consequent_(consequent), alternate_(alternate)
37    {
38    }
39
40    [[nodiscard]] const Expression *Test() const noexcept
41    {
42        return test_;
43    }
44
45    [[nodiscard]] Expression *Test() noexcept
46    {
47        return test_;
48    }
49
50    void SetTest(Expression *expr) noexcept
51    {
52        test_ = expr;
53        test_->SetParent(this);
54    }
55
56    [[nodiscard]] const Expression *Consequent() const noexcept
57    {
58        return consequent_;
59    }
60
61    [[nodiscard]] Expression *Consequent() noexcept
62    {
63        return consequent_;
64    }
65
66    void SetConsequent(Expression *expr) noexcept
67    {
68        consequent_ = expr;
69        consequent_->SetParent(this);
70    }
71
72    [[nodiscard]] const Expression *Alternate() const noexcept
73    {
74        return alternate_;
75    }
76
77    [[nodiscard]] Expression *Alternate() noexcept
78    {
79        return alternate_;
80    }
81
82    void SetAlternate(Expression *expr) noexcept
83    {
84        alternate_ = expr;
85        alternate_->SetParent(this);
86    }
87
88    [[nodiscard]] ConditionalExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override;
89
90    void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
91    void Iterate(const NodeTraverser &cb) const override;
92    void Dump(ir::AstDumper *dumper) const override;
93    void Dump(ir::SrcDumper *dumper) const override;
94    void Compile(compiler::PandaGen *pg) const override;
95    void Compile(compiler::ETSGen *etsg) const override;
96
97    checker::Type *Check(checker::TSChecker *checker) override;
98    checker::Type *Check(checker::ETSChecker *checker) override;
99
100    checker::Type *NumericConditionalCheck(checker::ETSChecker *checker, checker::Type *consequentType,
101                                           checker::Type *alternateType);
102
103    void Accept(ASTVisitorT *v) override
104    {
105        v->Accept(this);
106    }
107
108private:
109    Expression *test_;
110    Expression *consequent_;
111    Expression *alternate_;
112};
113}  // namespace ark::es2panda::ir
114
115#endif
116