1/*
2 * Copyright (c) 2021 - 2023 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_COMPILER_BASE_CATCH_TABLE_H
17#define ES2PANDA_COMPILER_BASE_CATCH_TABLE_H
18
19#include "ir/irnode.h"
20#include "compiler/core/labelPair.h"
21
22namespace ark::es2panda::compiler {
23class CodeGen;
24
25class TryLabelSet {
26public:
27    explicit TryLabelSet(CodeGen *cg);
28    explicit TryLabelSet(CodeGen *cg, LabelPair tryLabelPair);
29
30    ~TryLabelSet() = default;
31    DEFAULT_COPY_SEMANTIC(TryLabelSet);
32    DEFAULT_MOVE_SEMANTIC(TryLabelSet);
33
34    const LabelPair &TryLabelPair() const
35    {
36        return try_;
37    }
38
39    const LabelPair &CatchLabelPair() const
40    {
41        return catch_;
42    }
43
44    Label *TryBegin() const
45    {
46        return try_.Begin();
47    }
48
49    Label *TryEnd() const
50    {
51        return try_.End();
52    }
53
54    Label *CatchBegin() const
55    {
56        return catch_.Begin();
57    }
58
59    Label *CatchEnd() const
60    {
61        return catch_.End();
62    }
63
64private:
65    LabelPair try_;
66    LabelPair catch_;
67};
68
69class CatchTable {
70public:
71    CatchTable(CodeGen *cg, uint32_t depth, util::StringView exceptionType)
72        : labelSet_(cg), depth_(depth), exceptionType_(exceptionType)
73    {
74    }
75    CatchTable(CodeGen *cg, uint32_t depth, LabelPair tryLabelPair, util::StringView exceptionType)
76        : labelSet_(cg, tryLabelPair), depth_(depth), exceptionType_(exceptionType)
77    {
78    }
79
80    ~CatchTable() = default;
81    NO_COPY_SEMANTIC(CatchTable);
82    NO_MOVE_SEMANTIC(CatchTable);
83
84    const TryLabelSet &LabelSet() const
85    {
86        return labelSet_;
87    }
88
89    uint32_t Depth() const
90    {
91        return depth_;
92    }
93
94    std::string Exception() const
95    {
96        return exceptionType_.Mutf8();
97    }
98
99private:
100    TryLabelSet labelSet_;
101    uint32_t depth_;
102    util::StringView exceptionType_ {};
103};
104}  // namespace ark::es2panda::compiler
105
106#endif
107