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_BLOCK_STATEMENT_H
17 #define ES2PANDA_IR_STATEMENT_BLOCK_STATEMENT_H
18 
19 #include "ir/statement.h"
20 
21 namespace ark::es2panda::checker {
22 class ETSAnalyzer;
23 }  // namespace ark::es2panda::checker
24 
25 namespace ark::es2panda::ir {
26 class BlockStatement : public Statement {
27 public:
BlockStatement(ArenaAllocator *allocator, ArenaVector<Statement *> &&statementList)28     explicit BlockStatement(ArenaAllocator *allocator, ArenaVector<Statement *> &&statementList)
29         : Statement(AstNodeType::BLOCK_STATEMENT),
30           statements_(std::move(statementList)),
31           trailingBlocks_(allocator->Adapter())
32     {
33     }
34 
35     // NOTE (somas): this friend relationship can be removed once there are getters for private fields
36     friend class checker::ETSAnalyzer;
37 
38     [[nodiscard]] bool IsScopeBearer() const noexcept override
39     {
40         return true;
41     }
42 
43     [[nodiscard]] varbinder::Scope *Scope() const noexcept override
44     {
45         return scope_;
46     }
47 
48     void SetScope(varbinder::Scope *scope) noexcept
49     {
50         scope_ = scope;
51     }
52 
53     void ClearScope() noexcept override
54     {
55         scope_ = nullptr;
56     }
57 
Statements() const58     const ArenaVector<Statement *> &Statements() const
59     {
60         return statements_;
61     }
62 
Statements()63     ArenaVector<Statement *> &Statements()
64     {
65         return statements_;
66     }
67 
AddTrailingBlock(AstNode *stmt, BlockStatement *trailingBlock)68     void AddTrailingBlock(AstNode *stmt, BlockStatement *trailingBlock)
69     {
70         trailingBlocks_.emplace(stmt, trailingBlock);
71     }
72 
73     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
74 
75     AstNode *Clone(ArenaAllocator *const allocator, AstNode *const parent) override;
76 
77     void Iterate(const NodeTraverser &cb) const override;
78     void Dump(ir::AstDumper *dumper) const override;
79     void Dump(ir::SrcDumper *dumper) const override;
80     void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
81     void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override;
82     checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override;
83     checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override;
84 
85     void Accept(ASTVisitorT *v) override
86     {
87         v->Accept(this);
88     }
89 
90 private:
91     varbinder::Scope *scope_ {};
92     ArenaVector<Statement *> statements_;
93     ArenaUnorderedMap<AstNode *, BlockStatement *> trailingBlocks_;
94 };
95 }  // namespace ark::es2panda::ir
96 
97 #endif
98