1 /**
2 * Copyright (c) 2023-2025 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 "compiler/core/ETSGen.h"
19 #include "checker/ETSchecker.h"
20
21 namespace ark::es2panda::ir {
22
BlockExpression(ArenaVector<ir::Statement * > && statements)23 BlockExpression::BlockExpression(ArenaVector<ir::Statement *> &&statements)
24 : Expression(AstNodeType::BLOCK_EXPRESSION), statements_(std::move(statements))
25 {
26 for (auto *const node : statements_) {
27 node->SetParent(this);
28 }
29 }
30
BlockExpression(Tag const tag,BlockExpression const & other,ArenaAllocator * const allocator)31 BlockExpression::BlockExpression([[maybe_unused]] Tag const tag, BlockExpression const &other,
32 ArenaAllocator *const allocator)
33 : Expression(static_cast<Expression const &>(other)), statements_(allocator->Adapter())
34 {
35 for (auto *const node : other.statements_) {
36 statements_.emplace_back(node->Clone(allocator, this)->AsStatement());
37 }
38 }
39
Clone(ArenaAllocator * const allocator,AstNode * const parent)40 BlockExpression *BlockExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
41 {
42 auto *const clone = allocator->New<BlockExpression>(Tag {}, *this, allocator);
43 ES2PANDA_ASSERT(clone);
44 if (parent != nullptr) {
45 clone->SetParent(parent);
46 }
47 clone->SetRange(Range());
48 return clone;
49 }
50
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)51 void BlockExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
52 {
53 for (auto *&node : statements_) {
54 if (auto *transformedNode = cb(node); node != transformedNode) {
55 node->SetTransformedNode(transformationName, transformedNode);
56 node = transformedNode->AsStatement();
57 }
58 }
59 }
60
Iterate(const NodeTraverser & cb) const61 void BlockExpression::Iterate(const NodeTraverser &cb) const
62 {
63 for (auto *const node : statements_) {
64 cb(node);
65 }
66 }
67
Dump(ir::AstDumper * dumper) const68 void BlockExpression::Dump(ir::AstDumper *dumper) const
69 {
70 dumper->Add({{"type", "BlockExpression"}, {"statements", statements_}});
71 }
72
Dump(ir::SrcDumper * dumper) const73 void BlockExpression::Dump(ir::SrcDumper *dumper) const
74 {
75 dumper->Add("({");
76 for (auto *statement : statements_) {
77 statement->Dump(dumper);
78 if (statement != statements_.back()) {
79 dumper->Endl();
80 }
81 }
82 dumper->Add("})");
83 }
84
Compile(compiler::PandaGen * pg) const85 void BlockExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const
86 {
87 ES2PANDA_UNREACHABLE();
88 }
89
Compile(compiler::ETSGen * etsg) const90 void BlockExpression::Compile(compiler::ETSGen *etsg) const
91 {
92 etsg->GetAstCompiler()->Compile(this);
93 }
94
Check(checker::TSChecker * checker)95 checker::Type *BlockExpression::Check([[maybe_unused]] checker::TSChecker *checker)
96 {
97 ES2PANDA_UNREACHABLE();
98 }
99
Check(checker::ETSChecker * checker)100 checker::VerifiedType BlockExpression::Check(checker::ETSChecker *checker)
101 {
102 return {this, checker->GetAnalyzer()->Check(this)};
103 }
104 } // namespace ark::es2panda::ir
105