1 /**
2 * Copyright (c) 2021-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 "blockStatement.h"
17
18 #include "compiler/core/pandagen.h"
19 #include "compiler/core/regScope.h"
20 #include "compiler/core/ETSGen.h"
21 #include "checker/TSchecker.h"
22 #include "checker/ETSchecker.h"
23 #include "ir/astDump.h"
24 #include "ir/srcDump.h"
25 #include "utils/arena_containers.h"
26
27 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)28 void BlockStatement::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
29 {
30 // This will survive pushing element to the back of statements_ in the process
31 // NOLINTNEXTLINE(modernize-loop-convert)
32 for (size_t ix = 0; ix < statements_.size(); ix++) {
33 if (auto *transformedNode = cb(statements_[ix]); statements_[ix] != transformedNode) {
34 statements_[ix]->SetTransformedNode(transformationName, transformedNode);
35 statements_[ix] = transformedNode->AsStatement();
36 }
37 }
38 }
39
Clone(ArenaAllocator * const allocator,AstNode * const parent)40 AstNode *BlockStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent)
41 {
42 ArenaVector<Statement *> statements(allocator->Adapter());
43
44 for (auto *statement : this->statements_) {
45 statements.push_back(statement->Clone(allocator, parent)->AsStatement());
46 }
47
48 auto retVal = util::NodeAllocator::ForceSetParent<ir::BlockStatement>(allocator, allocator, std::move(statements));
49 ES2PANDA_ASSERT(retVal != nullptr);
50 retVal->SetParent(parent);
51
52 return retVal;
53 }
54
Iterate(const NodeTraverser & cb) const55 void BlockStatement::Iterate(const NodeTraverser &cb) const
56 {
57 // This will survive pushing element to the back of statements_ in the process
58 // NOLINTNEXTLINE(modernize-loop-convert)
59 for (size_t ix = 0; ix < statements_.size(); ix++) {
60 cb(statements_[ix]);
61 }
62 }
63
Dump(ir::AstDumper * dumper) const64 void BlockStatement::Dump(ir::AstDumper *dumper) const
65 {
66 dumper->Add({{"type", IsProgram() ? "Program" : "BlockStatement"}, {"statements", statements_}});
67 }
68
Dump(ir::SrcDumper * dumper) const69 void BlockStatement::Dump(ir::SrcDumper *dumper) const
70 {
71 // NOTE(nsizov): trailing blocks
72 if (Parent() != nullptr && (Parent()->IsBlockStatement() || Parent()->IsCallExpression())) {
73 dumper->Add("{");
74 if (!statements_.empty()) {
75 dumper->IncrIndent();
76 dumper->Endl();
77 }
78 }
79 for (auto statement : statements_) {
80 statement->Dump(dumper);
81 if (statement != statements_.back()) {
82 dumper->Endl();
83 }
84 }
85 if (Parent() != nullptr && (Parent()->IsBlockStatement() || Parent()->IsCallExpression())) {
86 if (!statements_.empty()) {
87 dumper->DecrIndent();
88 dumper->Endl();
89 }
90 dumper->Add("}");
91 }
92 }
93
Compile(compiler::PandaGen * pg) const94 void BlockStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const
95 {
96 pg->GetAstCompiler()->Compile(this);
97 }
98
Compile(compiler::ETSGen * etsg) const99 void BlockStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
100 {
101 etsg->GetAstCompiler()->Compile(this);
102 }
103
Check(checker::TSChecker * checker)104 checker::Type *BlockStatement::Check([[maybe_unused]] checker::TSChecker *checker)
105 {
106 return checker->GetAnalyzer()->Check(this);
107 }
108
Check(checker::ETSChecker * checker)109 checker::VerifiedType BlockStatement::Check([[maybe_unused]] checker::ETSChecker *checker)
110 {
111 return {this, checker->GetAnalyzer()->Check(this)};
112 }
113
Construct(ArenaAllocator * allocator)114 BlockStatement *BlockStatement::Construct(ArenaAllocator *allocator)
115 {
116 ArenaVector<Statement *> statementList(allocator->Adapter());
117 return allocator->New<BlockStatement>(allocator, std::move(statementList));
118 }
119
CopyTo(AstNode * other) const120 void BlockStatement::CopyTo(AstNode *other) const
121 {
122 auto otherImpl = other->AsBlockStatement();
123
124 otherImpl->scope_ = scope_;
125 otherImpl->statements_ = statements_;
126 otherImpl->trailingBlocks_ = trailingBlocks_;
127
128 Statement::CopyTo(other);
129 }
130
131 } // namespace ark::es2panda::ir
132