1/**
2 * Copyright (c) 2021 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_TRY_STATEMENT_H
17#define ES2PANDA_IR_STATEMENT_TRY_STATEMENT_H
18
19#include <ir/statement.h>
20
21namespace panda::es2panda::compiler {
22class PandaGen;
23class TryLabelSet;
24class TryContext;
25}  // namespace panda::es2panda::compiler
26
27namespace panda::es2panda::checker {
28class Checker;
29class Type;
30}  // namespace panda::es2panda::checker
31
32namespace panda::es2panda::ir {
33
34class BlockStatement;
35class CatchClause;
36
37class TryStatement : public Statement {
38public:
39    explicit TryStatement(BlockStatement *block, CatchClause *catchClause, BlockStatement *finalizer)
40        : Statement(AstNodeType::TRY_STATEMENT), block_(block), catchClause_(catchClause), finalizer_(finalizer)
41    {
42    }
43
44    const BlockStatement *FinallyBlock() const
45    {
46        return finalizer_;
47    }
48
49    BlockStatement *Block() const
50    {
51        return block_;
52    }
53
54    CatchClause *GetCatchClause() const
55    {
56        return catchClause_;
57    }
58
59    void Iterate(const NodeTraverser &cb) const override;
60    void Dump(ir::AstDumper *dumper) const override;
61    void Compile(compiler::PandaGen *pg) const override;
62    checker::Type *Check(checker::Checker *checker) const override;
63    void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override;
64
65private:
66    void CompileTryCatch(compiler::PandaGen *pg) const;
67    void CompileTryFinally(compiler::PandaGen *pg) const;
68    void CompileTryCatchFinally(compiler::PandaGen *pg) const;
69    void CompileFinally(compiler::PandaGen *pg, compiler::TryContext *tryCtx,
70                        const compiler::TryLabelSet &labelSet) const;
71
72    BlockStatement *block_;
73    CatchClause *catchClause_;
74    BlockStatement *finalizer_;
75};
76
77}  // namespace panda::es2panda::ir
78
79#endif
80