1 /**
2  * Copyright (c) 2023-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_BLOCK_EXPRESSION_H
17 #define ES2PANDA_IR_EXPRESSION_BLOCK_EXPRESSION_H
18 
19 #include "ir/astNode.h"
20 #include "ir/expression.h"
21 #include "ir/statement.h"
22 
23 namespace ark::es2panda::ir {
24 
25 class BlockExpression : public Expression {
26 private:
27     struct Tag {};
28 
29 public:
30     BlockExpression() = delete;
31     ~BlockExpression() override = default;
32 
33     NO_COPY_SEMANTIC(BlockExpression);
34     NO_MOVE_SEMANTIC(BlockExpression);
35 
36     explicit BlockExpression(ArenaVector<ir::Statement *> &&statements);
37     explicit BlockExpression(Tag tag, BlockExpression const &other, ArenaAllocator *allocator);
38 
39     [[nodiscard]] ArenaVector<ir::Statement *> const &Statements() const noexcept
40     {
41         return statements_;
42     }
43 
44     [[nodiscard]] ArenaVector<ir::Statement *> &Statements() noexcept
45     {
46         return statements_;
47     }
48 
AddStatements(ArenaVector<ir::Statement *> const &statements)49     void AddStatements(ArenaVector<ir::Statement *> const &statements)
50     {
51         std::copy_if(statements.begin(), statements.end(), std::back_inserter(statements_),
52                      [this](ir::Statement *statement) {
53                          if (statement != nullptr) {
54                              statement->SetParent(this);
55                              return true;
56                          };
57                          return false;
58                      });
59     }
60 
AddStatement(ir::Statement *statement)61     void AddStatement(ir::Statement *statement)
62     {
63         if (statement != nullptr) {
64             statement->SetParent(this);
65             statements_.emplace_back(statement);
66         }
67     }
68 
69     [[nodiscard]] BlockExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override;
70 
71     [[nodiscard]] bool IsScopeBearer() const noexcept override
72     {
73         return true;
74     }
75 
76     [[nodiscard]] varbinder::Scope *Scope() const noexcept override
77     {
78         return scope_;
79     }
80 
SetScope(varbinder::Scope *scope)81     void SetScope(varbinder::Scope *scope)
82     {
83         ASSERT(scope_ == nullptr);
84         scope_ = scope;
85     }
86 
87     void ClearScope() noexcept override
88     {
89         scope_ = nullptr;
90     }
91 
92     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
93     void Iterate(const NodeTraverser &cb) const override;
94     void Dump(ir::AstDumper *dumper) const override;
95     void Dump(ir::SrcDumper *dumper) const override;
96     void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
97     void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override;
98     checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override;
99     checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override;
100 
101     void Accept(ASTVisitorT *v) override
102     {
103         v->Accept(this);
104     }
105 
106 private:
107     varbinder::Scope *scope_ = {};
108     ArenaVector<ir::Statement *> statements_;
109 };
110 }  // namespace ark::es2panda::ir
111 
112 #endif
113