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