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 #include "sequenceExpression.h"
17 
18 #include "checker/ETSchecker.h"
19 #include "checker/TSchecker.h"
20 #include "compiler/core/ETSGen.h"
21 #include "compiler/core/pandagen.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 
25 namespace ark::es2panda::ir {
SequenceExpression([[maybe_unused]] Tag const tag, SequenceExpression const &other, ArenaAllocator *const allocator)26 SequenceExpression::SequenceExpression([[maybe_unused]] Tag const tag, SequenceExpression const &other,
27                                        ArenaAllocator *const allocator)
28     : Expression(static_cast<Expression const &>(other)), sequence_(allocator->Adapter())
29 {
30     for (auto *sequence : other.sequence_) {
31         sequence_.emplace_back(sequence->Clone(allocator, this)->AsExpression());
32     }
33 }
34 
Clone(ArenaAllocator *const allocator, AstNode *const parent)35 SequenceExpression *SequenceExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
36 {
37     if (auto *const clone = allocator->New<SequenceExpression>(Tag {}, *this, allocator); clone != nullptr) {
38         if (parent != nullptr) {
39             clone->SetParent(parent);
40         }
41         return clone;
42     }
43     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
44 }
45 
TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)46 void SequenceExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
47 {
48     for (auto *&it : sequence_) {
49         if (auto *transformedNode = cb(it); it != transformedNode) {
50             it->SetTransformedNode(transformationName, transformedNode);
51             it = transformedNode->AsExpression();
52         }
53     }
54 }
55 
Iterate(const NodeTraverser &cb) const56 void SequenceExpression::Iterate(const NodeTraverser &cb) const
57 {
58     for (auto *it : sequence_) {
59         cb(it);
60     }
61 }
62 
Dump(ir::AstDumper *dumper) const63 void SequenceExpression::Dump(ir::AstDumper *dumper) const
64 {
65     dumper->Add({{"type", "SequenceExpression"}, {"expressions", sequence_}});
66 }
67 
Dump(ir::SrcDumper *dumper) const68 void SequenceExpression::Dump(ir::SrcDumper *dumper) const
69 {
70     for (auto *expr : sequence_) {
71         expr->Dump(dumper);
72         if (expr != sequence_.back()) {
73             dumper->Add(", ");
74         }
75     }
76 }
77 
Compile(compiler::PandaGen *pg) const78 void SequenceExpression::Compile(compiler::PandaGen *pg) const
79 {
80     pg->GetAstCompiler()->Compile(this);
81 }
82 
Compile(compiler::ETSGen *etsg) const83 void SequenceExpression::Compile(compiler::ETSGen *etsg) const
84 {
85     etsg->GetAstCompiler()->Compile(this);
86 }
87 
Check(checker::TSChecker *checker)88 checker::Type *SequenceExpression::Check(checker::TSChecker *checker)
89 {
90     return checker->GetAnalyzer()->Check(this);
91 }
92 
Check(checker::ETSChecker *checker)93 checker::Type *SequenceExpression::Check(checker::ETSChecker *checker)
94 {
95     return checker->GetAnalyzer()->Check(this);
96 }
97 }  // namespace ark::es2panda::ir
98