1 /**
2 * Copyright (c) 2021-2023 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 panda::es2panda::ir {
SequenceExpression(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
35 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)36 SequenceExpression *SequenceExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
37 {
38 if (auto *const clone = allocator->New<SequenceExpression>(Tag {}, *this, allocator); clone != nullptr) {
39 if (parent != nullptr) {
40 clone->SetParent(parent);
41 }
42 return clone;
43 }
44 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
45 }
46
TransformChildren(const NodeTransformer & cb)47 void SequenceExpression::TransformChildren(const NodeTransformer &cb)
48 {
49 for (auto *&it : sequence_) {
50 it = cb(it)->AsExpression();
51 }
52 }
53
Iterate(const NodeTraverser & cb) const54 void SequenceExpression::Iterate(const NodeTraverser &cb) const
55 {
56 for (auto *it : sequence_) {
57 cb(it);
58 }
59 }
60
Dump(ir::AstDumper * dumper) const61 void SequenceExpression::Dump(ir::AstDumper *dumper) const
62 {
63 dumper->Add({{"type", "SequenceExpression"}, {"expressions", sequence_}});
64 }
65
Dump(ir::SrcDumper * dumper) const66 void SequenceExpression::Dump(ir::SrcDumper *dumper) const
67 {
68 for (auto *expr : sequence_) {
69 expr->Dump(dumper);
70 if (expr != sequence_.back()) {
71 dumper->Add(", ");
72 }
73 }
74 }
75
Compile(compiler::PandaGen * pg) const76 void SequenceExpression::Compile(compiler::PandaGen *pg) const
77 {
78 pg->GetAstCompiler()->Compile(this);
79 }
80
Compile(compiler::ETSGen * etsg) const81 void SequenceExpression::Compile(compiler::ETSGen *etsg) const
82 {
83 etsg->GetAstCompiler()->Compile(this);
84 }
85
Check(checker::TSChecker * checker)86 checker::Type *SequenceExpression::Check(checker::TSChecker *checker)
87 {
88 return checker->GetAnalyzer()->Check(this);
89 }
90
Check(checker::ETSChecker * checker)91 checker::Type *SequenceExpression::Check(checker::ETSChecker *checker)
92 {
93 return checker->GetAnalyzer()->Check(this);
94 }
95 } // namespace panda::es2panda::ir
96