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_BASE_CATCH_CLAUSE_H
17#define ES2PANDA_IR_BASE_CATCH_CLAUSE_H
18
19#include "varbinder/scope.h"
20#include "ir/statement.h"
21
22namespace ark::es2panda::ir {
23class BlockStatement;
24class Expression;
25
26class CatchClause : public TypedStatement {
27public:
28    explicit CatchClause(Expression *param, BlockStatement *body)
29        : TypedStatement(AstNodeType::CATCH_CLAUSE), param_(param), body_(body)
30    {
31    }
32
33    Expression *Param()
34    {
35        return param_;
36    }
37
38    const Expression *Param() const
39    {
40        return param_;
41    }
42
43    BlockStatement *Body()
44    {
45        return body_;
46    }
47
48    const BlockStatement *Body() const
49    {
50        return body_;
51    }
52
53    [[nodiscard]] bool IsScopeBearer() const noexcept override
54    {
55        return true;
56    }
57
58    [[nodiscard]] varbinder::CatchScope *Scope() const noexcept override
59    {
60        return scope_;
61    }
62
63    void SetScope(varbinder::CatchScope *scope)
64    {
65        ASSERT(scope_ == nullptr);
66        scope_ = scope;
67    }
68
69    void ClearScope() noexcept override
70    {
71        scope_ = nullptr;
72    }
73
74    bool IsDefaultCatchClause() const;
75    void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
76    void Iterate(const NodeTraverser &cb) const override;
77    void Dump(ir::AstDumper *dumper) const override;
78    void Dump(ir::SrcDumper *dumper) const override;
79    void Compile(compiler::PandaGen *pg) const override;
80    void Compile(compiler::ETSGen *etsg) const override;
81    checker::Type *Check(checker::TSChecker *checker) override;
82    checker::Type *Check(checker::ETSChecker *checker) override;
83
84    void Accept(ASTVisitorT *v) override
85    {
86        v->Accept(this);
87    }
88
89private:
90    varbinder::CatchScope *scope_ {nullptr};
91    Expression *param_;
92    BlockStatement *body_;
93};
94}  // namespace ark::es2panda::ir
95
96#endif
97