1 /**
2 * Copyright (c) 2021-2025 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 #include "utils/arena_containers.h"
24
25 namespace ark::es2panda::ir {
26
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)27 void ImportDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
28 {
29 if (auto *transformedNode = cb(source_); source_ != transformedNode) {
30 source_->SetTransformedNode(transformationName, transformedNode);
31 source_ = transformedNode->AsStringLiteral();
32 }
33
34 for (auto *&it : VectorIterationGuard(specifiers_)) {
35 if (auto *transformedNode = cb(it); it != transformedNode) {
36 it->SetTransformedNode(transformationName, transformedNode);
37 it = transformedNode;
38 }
39 }
40 }
41
Iterate(const NodeTraverser & cb) const42 void ImportDeclaration::Iterate(const NodeTraverser &cb) const
43 {
44 cb(source_);
45
46 for (auto *it : VectorIterationGuard(specifiers_)) {
47 cb(it);
48 }
49 }
50
Dump(ir::AstDumper * dumper) const51 void ImportDeclaration::Dump(ir::AstDumper *dumper) const
52 {
53 dumper->Add({{"type", "ImportDeclaration"}, {"source", source_}, {"specifiers", specifiers_}});
54 }
55
Dump(ir::SrcDumper * dumper) const56 void ImportDeclaration::Dump(ir::SrcDumper *dumper) const
57 {
58 dumper->Add("import ");
59 if (specifiers_.size() == 1 &&
60 (specifiers_[0]->IsImportNamespaceSpecifier() || specifiers_[0]->IsImportDefaultSpecifier())) {
61 specifiers_[0]->Dump(dumper);
62 } else {
63 dumper->Add("{ ");
64 for (auto specifier : specifiers_) {
65 specifier->Dump(dumper);
66 if (specifier != specifiers_.back()) {
67 dumper->Add(", ");
68 }
69 }
70 dumper->Add(" }");
71 }
72
73 dumper->Add(" from ");
74 source_->Dump(dumper);
75 dumper->Add(";");
76 dumper->Endl();
77 }
78
Compile(compiler::PandaGen * pg) const79 void ImportDeclaration::Compile(compiler::PandaGen *pg) const
80 {
81 pg->GetAstCompiler()->Compile(this);
82 }
83
Compile(compiler::ETSGen * etsg) const84 void ImportDeclaration::Compile(compiler::ETSGen *etsg) const
85 {
86 etsg->GetAstCompiler()->Compile(this);
87 }
88
Check(checker::TSChecker * checker)89 checker::Type *ImportDeclaration::Check(checker::TSChecker *checker)
90 {
91 return checker->GetAnalyzer()->Check(this);
92 }
93
Check(checker::ETSChecker * checker)94 checker::VerifiedType ImportDeclaration::Check(checker::ETSChecker *checker)
95 {
96 return {this, checker->GetAnalyzer()->Check(this)};
97 }
98
Construct(ArenaAllocator * allocator)99 ImportDeclaration *ImportDeclaration::Construct(ArenaAllocator *allocator)
100 {
101 ArenaVector<AstNode *> specifiers(allocator->Adapter());
102 return allocator->New<ImportDeclaration>(nullptr, std::move(specifiers));
103 }
104
CopyTo(AstNode * other) const105 void ImportDeclaration::CopyTo(AstNode *other) const
106 {
107 auto otherImpl = other->AsImportDeclaration();
108
109 otherImpl->source_ = source_;
110 otherImpl->specifiers_ = specifiers_;
111 otherImpl->importKinds_ = importKinds_;
112
113 Statement::CopyTo(other);
114 }
115
116 } // namespace ark::es2panda::ir
117