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 "tsModuleDeclaration.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 "ir/base/decorator.h"
24
25 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)26 void TSModuleDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
27 {
28 for (auto *&it : decorators_) {
29 if (auto *transformedNode = cb(it); it != transformedNode) {
30 it->SetTransformedNode(transformationName, transformedNode);
31 it = transformedNode->AsDecorator();
32 }
33 }
34
35 if (auto *transformedNode = cb(name_); name_ != transformedNode) {
36 name_->SetTransformedNode(transformationName, transformedNode);
37 name_ = transformedNode->AsExpression();
38 }
39
40 if (body_ != nullptr) {
41 if (auto *transformedNode = cb(body_); body_ != transformedNode) {
42 body_->SetTransformedNode(transformationName, transformedNode);
43 body_ = transformedNode->AsStatement();
44 }
45 }
46 }
47
Iterate(const NodeTraverser & cb) const48 void TSModuleDeclaration::Iterate(const NodeTraverser &cb) const
49 {
50 for (auto *it : decorators_) {
51 cb(it);
52 }
53
54 cb(name_);
55
56 if (body_ != nullptr) {
57 cb(body_);
58 }
59 }
60
Dump(ir::AstDumper * dumper) const61 void TSModuleDeclaration::Dump(ir::AstDumper *dumper) const
62 {
63 dumper->Add({{"type", "TSModuleDeclaration"},
64 {"decorators", AstDumper::Optional(decorators_)},
65 {"id", name_},
66 {"body", AstDumper::Optional(body_)},
67 {"global", global_}});
68 }
69
Dump(ir::SrcDumper * dumper) const70 void TSModuleDeclaration::Dump(ir::SrcDumper *dumper) const
71 {
72 dumper->Add("TSModuleDeclaration");
73 }
74
Compile(compiler::PandaGen * pg) const75 void TSModuleDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const
76 {
77 pg->GetAstCompiler()->Compile(this);
78 }
79
Compile(compiler::ETSGen * etsg) const80 void TSModuleDeclaration::Compile(compiler::ETSGen *etsg) const
81 {
82 etsg->GetAstCompiler()->Compile(this);
83 }
84
Check(checker::TSChecker * checker)85 checker::Type *TSModuleDeclaration::Check([[maybe_unused]] checker::TSChecker *checker)
86 {
87 return checker->GetAnalyzer()->Check(this);
88 }
89
Check(checker::ETSChecker * checker)90 checker::Type *TSModuleDeclaration::Check([[maybe_unused]] checker::ETSChecker *checker)
91 {
92 return checker->GetAnalyzer()->Check(this);
93 }
94 } // namespace ark::es2panda::ir
95