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 "etsNewArrayInstanceExpression.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 #include "ir/typeNode.h"
25
26 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)27 void ETSNewArrayInstanceExpression::TransformChildren(const NodeTransformer &cb)
28 {
29 typeReference_ = static_cast<TypeNode *>(cb(typeReference_));
30 dimension_ = cb(dimension_)->AsExpression();
31 }
32
Iterate(const NodeTraverser & cb) const33 void ETSNewArrayInstanceExpression::Iterate(const NodeTraverser &cb) const
34 {
35 cb(typeReference_);
36 cb(dimension_);
37 }
38
Dump(ir::AstDumper * dumper) const39 void ETSNewArrayInstanceExpression::Dump(ir::AstDumper *dumper) const
40 {
41 dumper->Add(
42 {{"type", "ETSNewArrayInstanceExpression"}, {"typeReference", typeReference_}, {"dimension", dimension_}});
43 }
44
Dump(ir::SrcDumper * dumper) const45 void ETSNewArrayInstanceExpression::Dump(ir::SrcDumper *dumper) const
46 {
47 dumper->Add("new ");
48 ASSERT(typeReference_);
49 typeReference_->Dump(dumper);
50 ASSERT(dimension_);
51 dumper->Add("[");
52 dimension_->Dump(dumper);
53 dumper->Add("]");
54 }
55
Compile(compiler::PandaGen * pg) const56 void ETSNewArrayInstanceExpression::Compile(compiler::PandaGen *pg) const
57 {
58 pg->GetAstCompiler()->Compile(this);
59 }
Compile(compiler::ETSGen * etsg) const60 void ETSNewArrayInstanceExpression::Compile(compiler::ETSGen *etsg) const
61 {
62 etsg->GetAstCompiler()->Compile(this);
63 }
64
Check(checker::TSChecker * checker)65 checker::Type *ETSNewArrayInstanceExpression::Check(checker::TSChecker *checker)
66 {
67 return checker->GetAnalyzer()->Check(this);
68 }
69
Check(checker::ETSChecker * checker)70 checker::Type *ETSNewArrayInstanceExpression::Check(checker::ETSChecker *checker)
71 {
72 return checker->GetAnalyzer()->Check(this);
73 }
74
75 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)76 ETSNewArrayInstanceExpression *ETSNewArrayInstanceExpression::Clone(ArenaAllocator *const allocator,
77 AstNode *const parent)
78 {
79 auto *const typeRef = typeReference_ != nullptr ? typeReference_->Clone(allocator) : nullptr;
80 auto *const dimension = dimension_ != nullptr ? dimension_->Clone(allocator)->AsExpression() : nullptr;
81
82 if (auto *const clone = allocator->New<ETSNewArrayInstanceExpression>(allocator, typeRef, dimension);
83 clone != nullptr) {
84 if (typeRef != nullptr) {
85 typeRef->SetParent(clone);
86 }
87 if (dimension != nullptr) {
88 dimension->SetParent(clone);
89 }
90 if (parent != nullptr) {
91 clone->SetParent(parent);
92 }
93 return clone;
94 }
95
96 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
97 }
98 } // namespace panda::es2panda::ir
99