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 "namespaceDefinition.h"
17
18#include "checker/TSchecker.h"
19#include "checker/ETSchecker.h"
20#include "compiler/core/ETSGen.h"
21#include "compiler/core/pandagen.h"
22#include "ir/astDump.h"
23#include "ir/srcDump.h"
24#include "ir/base/classStaticBlock.h"
25#include "ir/base/methodDefinition.h"
26#include "ir/base/scriptFunction.h"
27#include "ir/expressions/identifier.h"
28#include "ir/ts/tsClassImplements.h"
29
30namespace ark::es2panda::ir {
31const FunctionExpression *NamespaceDefinition::Ctor() const
32{
33    return ctor_ != nullptr ? ctor_->Value()->AsFunctionExpression() : nullptr;
34}
35
36void NamespaceDefinition::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
37{
38    if (ident_ != nullptr) {
39        if (auto *transformedNode = cb(ident_); ident_ != transformedNode) {
40            ident_->SetTransformedNode(transformationName, transformedNode);
41            ident_ = transformedNode->AsIdentifier();
42        }
43    }
44
45    for (auto *&it : body_) {
46        if (auto *transformedNode = cb(it); it != transformedNode) {
47            it->SetTransformedNode(transformationName, transformedNode);
48            it = transformedNode;
49        }
50    }
51}
52
53void NamespaceDefinition::Iterate(const NodeTraverser &cb) const
54{
55    if (ident_ != nullptr) {
56        cb(ident_);
57    }
58
59    // NOLINTNEXTLINE(modernize-loop-convert)
60    for (size_t ix = 0; ix < body_.size(); ix++) {
61        cb(body_[ix]);
62    }
63}
64
65void NamespaceDefinition::SetIdent(ir::Identifier *ident) noexcept
66{
67    ident_ = ident;
68    if (ident_ != nullptr) {
69        ident_->SetParent(this);
70    }
71}
72
73void NamespaceDefinition::Dump(ir::AstDumper *dumper) const
74{
75    dumper->Add({{"id", AstDumper::Nullish(ident_)}, {"body", body_}});
76}
77
78void NamespaceDefinition::Dump([[maybe_unused]] ir::SrcDumper *dumper) const
79{
80    ASSERT(ident_ != nullptr);
81
82    if (IsExported()) {
83        dumper->Add("export ");
84    }
85
86    dumper->Add(" {");
87    if (!body_.empty()) {
88        dumper->IncrIndent();
89        dumper->Endl();
90        for (auto elem : body_) {
91            elem->Dump(dumper);
92            if (elem == body_.back()) {
93                dumper->DecrIndent();
94            }
95            dumper->Endl();
96        }
97    }
98    dumper->Add("}");
99    dumper->Endl();
100}
101
102void NamespaceDefinition::Compile(compiler::PandaGen *pg) const
103{
104    pg->GetAstCompiler()->Compile(this);
105}
106
107void NamespaceDefinition::Compile(compiler::ETSGen *etsg) const
108{
109    etsg->GetAstCompiler()->Compile(this);
110}
111
112checker::Type *NamespaceDefinition::Check(checker::TSChecker *checker)
113{
114    return checker->GetAnalyzer()->Check(this);
115}
116
117checker::Type *NamespaceDefinition::Check(checker::ETSChecker *checker)
118{
119    return checker->GetAnalyzer()->Check(this);
120}
121
122}  // namespace ark::es2panda::ir
123