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 #include "blockExpression.h"
17 
18 #include "ir/astDump.h"
19 #include "ir/srcDump.h"
20 #include "compiler/core/ETSGen.h"
21 #include "checker/ETSchecker.h"
22 #include "ir/astNode.h"
23 
24 namespace ark::es2panda::ir {
25 
BlockExpression(ArenaVector<ir::Statement *> &&statements)26 BlockExpression::BlockExpression(ArenaVector<ir::Statement *> &&statements)
27     : Expression(AstNodeType::BLOCK_EXPRESSION), statements_(std::move(statements))
28 {
29     for (auto *const node : statements_) {
30         node->SetParent(this);
31     }
32 }
33 
BlockExpression([[maybe_unused]] Tag const tag, BlockExpression const &other, ArenaAllocator *const allocator)34 BlockExpression::BlockExpression([[maybe_unused]] Tag const tag, BlockExpression const &other,
35                                  ArenaAllocator *const allocator)
36     : Expression(static_cast<Expression const &>(other)), statements_(allocator->Adapter())
37 {
38     for (auto *const node : other.statements_) {
39         statements_.emplace_back(node->Clone(allocator, this)->AsStatement());
40     }
41 }
42 
Clone(ArenaAllocator *const allocator, AstNode *const parent)43 BlockExpression *BlockExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
44 {
45     if (auto *const clone = allocator->New<BlockExpression>(Tag {}, *this, allocator); clone != nullptr) {
46         if (parent != nullptr) {
47             clone->SetParent(parent);
48         }
49         return clone;
50     }
51     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
52 }
53 
TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)54 void BlockExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
55 {
56     for (auto *&node : statements_) {
57         if (auto *transformedNode = cb(node); node != transformedNode) {
58             node->SetTransformedNode(transformationName, transformedNode);
59             node = transformedNode->AsStatement();
60         }
61     }
62 }
63 
Iterate(const NodeTraverser &cb) const64 void BlockExpression::Iterate(const NodeTraverser &cb) const
65 {
66     for (auto *const node : statements_) {
67         cb(node);
68     }
69 }
70 
Dump(ir::AstDumper *dumper) const71 void BlockExpression::Dump(ir::AstDumper *dumper) const
72 {
73     dumper->Add({{"type", "BlockExpression"}, {"statements", statements_}});
74 }
75 
Dump(ir::SrcDumper *dumper) const76 void BlockExpression::Dump(ir::SrcDumper *dumper) const
77 {
78     dumper->Add("({");
79     for (auto *statement : statements_) {
80         statement->Dump(dumper);
81         if (statement != statements_.back()) {
82             dumper->Endl();
83         }
84     }
85     dumper->Add("})");
86 }
87 
Compile([[maybe_unused]] compiler::PandaGen *pg) const88 void BlockExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const
89 {
90     UNREACHABLE();
91 }
92 
Compile(compiler::ETSGen *etsg) const93 void BlockExpression::Compile(compiler::ETSGen *etsg) const
94 {
95     etsg->GetAstCompiler()->Compile(this);
96 }
97 
Check([[maybe_unused]] checker::TSChecker *checker)98 checker::Type *BlockExpression::Check([[maybe_unused]] checker::TSChecker *checker)
99 {
100     UNREACHABLE();
101 }
102 
Check(checker::ETSChecker *checker)103 checker::Type *BlockExpression::Check(checker::ETSChecker *checker)
104 {
105     return checker->GetAnalyzer()->Check(this);
106 }
107 }  // namespace ark::es2panda::ir
108