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 "etsNewMultiDimArrayInstanceExpression.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23
24 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)25 void ETSNewMultiDimArrayInstanceExpression::TransformChildren(const NodeTransformer &cb,
26 std::string_view const transformationName)
27 {
28 if (auto *transformedNode = cb(typeReference_); typeReference_ != transformedNode) {
29 typeReference_->SetTransformedNode(transformationName, transformedNode);
30 typeReference_ = static_cast<TypeNode *>(transformedNode);
31 }
32
33 for (auto *&dim : dimensions_) {
34 if (auto *transformedNode = cb(dim); dim != transformedNode) {
35 dim->SetTransformedNode(transformationName, transformedNode);
36 dim = transformedNode->AsExpression();
37 }
38 }
39 }
40
Iterate(const NodeTraverser & cb) const41 void ETSNewMultiDimArrayInstanceExpression::Iterate(const NodeTraverser &cb) const
42 {
43 cb(typeReference_);
44 for (auto *dim : dimensions_) {
45 cb(dim);
46 }
47 }
48
Dump(ir::AstDumper * dumper) const49 void ETSNewMultiDimArrayInstanceExpression::Dump(ir::AstDumper *dumper) const
50 {
51 dumper->Add({{"type", "ETSNewMultiDimArrayInstanceExpression"},
52 {"typeReference", typeReference_},
53 {"dimensions", dimensions_}});
54 }
55
Dump(ir::SrcDumper * dumper) const56 void ETSNewMultiDimArrayInstanceExpression::Dump(ir::SrcDumper *dumper) const
57 {
58 dumper->Add("new ");
59 ASSERT(typeReference_);
60 typeReference_->Dump(dumper);
61 for (auto dim : dimensions_) {
62 dumper->Add("[");
63 dim->Dump(dumper);
64 dumper->Add("]");
65 }
66 }
67
Compile(compiler::PandaGen * pg) const68 void ETSNewMultiDimArrayInstanceExpression::Compile(compiler::PandaGen *pg) const
69 {
70 pg->GetAstCompiler()->Compile(this);
71 }
Compile(compiler::ETSGen * etsg) const72 void ETSNewMultiDimArrayInstanceExpression::Compile(compiler::ETSGen *etsg) const
73 {
74 etsg->GetAstCompiler()->Compile(this);
75 }
76
Check(checker::TSChecker * checker)77 checker::Type *ETSNewMultiDimArrayInstanceExpression::Check(checker::TSChecker *checker)
78 {
79 return checker->GetAnalyzer()->Check(this);
80 }
81
Check(checker::ETSChecker * checker)82 checker::Type *ETSNewMultiDimArrayInstanceExpression::Check(checker::ETSChecker *checker)
83 {
84 return checker->GetAnalyzer()->Check(this);
85 }
86
ETSNewMultiDimArrayInstanceExpression(ETSNewMultiDimArrayInstanceExpression const & other,ArenaAllocator * const allocator)87 ETSNewMultiDimArrayInstanceExpression::ETSNewMultiDimArrayInstanceExpression(
88 ETSNewMultiDimArrayInstanceExpression const &other, ArenaAllocator *const allocator)
89 : Expression(static_cast<Expression const &>(other)),
90 dimensions_(allocator->Adapter()),
91 signature_(other.signature_)
92 {
93 typeReference_ = other.typeReference_->Clone(allocator, this);
94
95 for (auto *const dimension : other.dimensions_) {
96 dimensions_.emplace_back(dimension->Clone(allocator, this)->AsExpression());
97 }
98 }
99
Clone(ArenaAllocator * const allocator,AstNode * const parent)100 ETSNewMultiDimArrayInstanceExpression *ETSNewMultiDimArrayInstanceExpression::Clone(ArenaAllocator *const allocator,
101 AstNode *const parent)
102 {
103 if (auto *const clone = allocator->New<ETSNewMultiDimArrayInstanceExpression>(*this, allocator); clone != nullptr) {
104 if (parent != nullptr) {
105 clone->SetParent(parent);
106 }
107
108 clone->SetRange(Range());
109 return clone;
110 }
111
112 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
113 }
114 } // namespace ark::es2panda::ir
115