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 "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
26namespace ark::es2panda::ir {
27void ETSNewArrayInstanceExpression::TransformChildren(const NodeTransformer &cb,
28                                                      std::string_view const transformationName)
29{
30    if (auto *transformedNode = cb(typeReference_); typeReference_ != transformedNode) {
31        typeReference_->SetTransformedNode(transformationName, transformedNode);
32        typeReference_ = static_cast<TypeNode *>(transformedNode);
33    }
34
35    if (auto *transformedNode = cb(dimension_); dimension_ != transformedNode) {
36        dimension_->SetTransformedNode(transformationName, transformedNode);
37        dimension_ = transformedNode->AsExpression();
38    }
39}
40
41void ETSNewArrayInstanceExpression::Iterate(const NodeTraverser &cb) const
42{
43    cb(typeReference_);
44    cb(dimension_);
45}
46
47void ETSNewArrayInstanceExpression::Dump(ir::AstDumper *dumper) const
48{
49    dumper->Add(
50        {{"type", "ETSNewArrayInstanceExpression"}, {"typeReference", typeReference_}, {"dimension", dimension_}});
51}
52
53void ETSNewArrayInstanceExpression::Dump(ir::SrcDumper *dumper) const
54{
55    dumper->Add("new ");
56    ASSERT(typeReference_);
57    typeReference_->Dump(dumper);
58    ASSERT(dimension_);
59    dumper->Add("[");
60    dimension_->Dump(dumper);
61    dumper->Add("]");
62}
63
64void ETSNewArrayInstanceExpression::Compile(compiler::PandaGen *pg) const
65{
66    pg->GetAstCompiler()->Compile(this);
67}
68void ETSNewArrayInstanceExpression::Compile(compiler::ETSGen *etsg) const
69{
70    etsg->GetAstCompiler()->Compile(this);
71}
72
73checker::Type *ETSNewArrayInstanceExpression::Check(checker::TSChecker *checker)
74{
75    return checker->GetAnalyzer()->Check(this);
76}
77
78checker::Type *ETSNewArrayInstanceExpression::Check(checker::ETSChecker *checker)
79{
80    return checker->GetAnalyzer()->Check(this);
81}
82
83ETSNewArrayInstanceExpression *ETSNewArrayInstanceExpression::Clone(ArenaAllocator *const allocator,
84                                                                    AstNode *const parent)
85{
86    auto *const typeRef = typeReference_ != nullptr ? typeReference_->Clone(allocator, nullptr) : nullptr;
87    auto *const dimension = dimension_ != nullptr ? dimension_->Clone(allocator, nullptr)->AsExpression() : nullptr;
88
89    if (auto *const clone = allocator->New<ETSNewArrayInstanceExpression>(typeRef, dimension); clone != nullptr) {
90        if (typeRef != nullptr) {
91            typeRef->SetParent(clone);
92        }
93
94        if (dimension != nullptr) {
95            dimension->SetParent(clone);
96        }
97
98        if (parent != nullptr) {
99            clone->SetParent(parent);
100        }
101
102        clone->defaultConstructorSignature_ = defaultConstructorSignature_;
103        clone->SetRange(Range());
104
105        return clone;
106    }
107
108    throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
109}
110}  // namespace ark::es2panda::ir
111