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
24 namespace ark::es2panda::ir {
25
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)26 void 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
Iterate(const NodeTraverser & cb) const41 void ImportDeclaration::Iterate(const NodeTraverser &cb) const
42 {
43 cb(source_);
44
45 for (auto *it : specifiers_) {
46 cb(it);
47 }
48 }
49
Dump(ir::AstDumper * dumper) const50 void ImportDeclaration::Dump(ir::AstDumper *dumper) const
51 {
52 dumper->Add({{"type", "ImportDeclaration"}, {"source", source_}, {"specifiers", specifiers_}});
53 }
54
Dump(ir::SrcDumper * dumper) const55 void ImportDeclaration::Dump(ir::SrcDumper *dumper) const
56 {
57 dumper->Add("import ");
58 for (auto specifier : specifiers_) {
59 specifier->Dump(dumper);
60 if (specifier != specifiers_.back()) {
61 dumper->Add(", ");
62 } else {
63 dumper->Add(" ");
64 }
65 }
66 dumper->Add("from ");
67 source_->Dump(dumper);
68 dumper->Add(";");
69 dumper->Endl();
70 }
71
Compile(compiler::PandaGen * pg) const72 void ImportDeclaration::Compile(compiler::PandaGen *pg) const
73 {
74 pg->GetAstCompiler()->Compile(this);
75 }
76
Compile(compiler::ETSGen * etsg) const77 void ImportDeclaration::Compile(compiler::ETSGen *etsg) const
78 {
79 etsg->GetAstCompiler()->Compile(this);
80 }
81
Check(checker::TSChecker * checker)82 checker::Type *ImportDeclaration::Check(checker::TSChecker *checker)
83 {
84 return checker->GetAnalyzer()->Check(this);
85 }
86
Check(checker::ETSChecker * checker)87 checker::Type *ImportDeclaration::Check(checker::ETSChecker *checker)
88 {
89 return checker->GetAnalyzer()->Check(this);
90 }
91 } // namespace ark::es2panda::ir
92