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 "importDeclaration.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
24namespace ark::es2panda::ir {
25
26void ImportDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
27{
28    if (auto *transformedNode = cb(source_); source_ != transformedNode) {
29        source_->SetTransformedNode(transformationName, transformedNode);
30        source_ = transformedNode->AsStringLiteral();
31    }
32
33    for (auto *&it : specifiers_) {
34        if (auto *transformedNode = cb(it); it != transformedNode) {
35            it->SetTransformedNode(transformationName, transformedNode);
36            it = transformedNode;
37        }
38    }
39}
40
41void ImportDeclaration::Iterate(const NodeTraverser &cb) const
42{
43    cb(source_);
44
45    for (auto *it : specifiers_) {
46        cb(it);
47    }
48}
49
50void ImportDeclaration::Dump(ir::AstDumper *dumper) const
51{
52    dumper->Add({{"type", "ImportDeclaration"}, {"source", source_}, {"specifiers", specifiers_}});
53}
54
55void ImportDeclaration::Dump(ir::SrcDumper *dumper) const
56{
57    dumper->Add("import ");
58    if (specifiers_.size() == 1 && specifiers_[0]->IsImportNamespaceSpecifier()) {
59        specifiers_[0]->Dump(dumper);
60    } else {
61        dumper->Add("{ ");
62        for (auto specifier : specifiers_) {
63            specifier->Dump(dumper);
64            if (specifier != specifiers_.back()) {
65                dumper->Add(", ");
66            }
67        }
68        dumper->Add(" }");
69    }
70
71    dumper->Add(" from ");
72    source_->Dump(dumper);
73    dumper->Add(";");
74    dumper->Endl();
75}
76
77void ImportDeclaration::Compile(compiler::PandaGen *pg) const
78{
79    pg->GetAstCompiler()->Compile(this);
80}
81
82void ImportDeclaration::Compile(compiler::ETSGen *etsg) const
83{
84    etsg->GetAstCompiler()->Compile(this);
85}
86
87checker::Type *ImportDeclaration::Check(checker::TSChecker *checker)
88{
89    return checker->GetAnalyzer()->Check(this);
90}
91
92checker::Type *ImportDeclaration::Check(checker::ETSChecker *checker)
93{
94    return checker->GetAnalyzer()->Check(this);
95}
96}  // namespace ark::es2panda::ir
97