1/**
2 * Copyright (c) 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#ifndef ES2PANDA_PARSER_INCLUDE_AST_NAMESPACE_DEFINITION_H
17#define ES2PANDA_PARSER_INCLUDE_AST_NAMESPACE_DEFINITION_H
18
19#include "varbinder/scope.h"
20#include "varbinder/variable.h"
21#include "ir/astNode.h"
22#include "ir/expressions/identifier.h"
23#include "util/language.h"
24
25namespace ark::es2panda::ir {
26class ClassElement;
27class Identifier;
28class MethodDefinition;
29class TSTypeParameterDeclaration;
30class TSTypeParameterInstantiation;
31class TSClassImplements;
32class TSIndexSignature;
33
34class NamespaceDefinition : public TypedAstNode {
35public:
36    NamespaceDefinition() = delete;
37    ~NamespaceDefinition() override = default;
38
39    NO_COPY_SEMANTIC(NamespaceDefinition);
40    NO_MOVE_SEMANTIC(NamespaceDefinition);
41
42    explicit NamespaceDefinition(Identifier *ident, ArenaVector<AstNode *> &&body, MethodDefinition *ctor,
43                                 ModifierFlags flags, Language lang)
44        : TypedAstNode(AstNodeType::NAMESPACE_DEFINITION, flags),
45
46          ident_(ident),
47          ctor_(ctor),
48          body_(std::move(body)),
49          lang_(lang)
50    {
51    }
52
53    [[nodiscard]] bool IsScopeBearer() const noexcept override
54    {
55        return true;
56    }
57
58    [[nodiscard]] varbinder::LocalScope *Scope() const noexcept override
59    {
60        return scope_;
61    }
62
63    void SetScope(varbinder::LocalScope *scope)
64    {
65        ASSERT(scope_ == nullptr);
66        scope_ = scope;
67    }
68
69    void ClearScope() noexcept override
70    {
71        scope_ = nullptr;
72    }
73
74    [[nodiscard]] const Identifier *Ident() const noexcept
75    {
76        return ident_;
77    }
78
79    [[nodiscard]] Identifier *Ident() noexcept
80    {
81        return ident_;
82    }
83
84    void SetIdent(ir::Identifier *ident) noexcept;
85
86    [[nodiscard]] const util::StringView &PrivateId() const noexcept
87    {
88        return privateId_;
89    }
90
91    [[nodiscard]] const util::StringView &InternalName() const noexcept
92    {
93        return privateId_;
94    }
95
96    void SetInternalName(util::StringView internalName) noexcept
97    {
98        privateId_ = internalName;
99    }
100
101    [[nodiscard]] es2panda::Language Language() const noexcept
102    {
103        return lang_;
104    }
105
106    [[nodiscard]] MethodDefinition *Ctor() noexcept
107    {
108        return ctor_;
109    }
110
111    void SetCtor(MethodDefinition *ctor)
112    {
113        ctor_ = ctor;
114    }
115
116    void AddProperties(ArenaVector<AstNode *> &&body)
117    {
118        for (auto *prop : body) {
119            prop->SetParent(this);
120        }
121
122        body_.insert(body_.end(), body.begin(), body.end());
123    }
124
125    [[nodiscard]] ArenaVector<AstNode *> &Body() noexcept
126    {
127        return body_;
128    }
129
130    [[nodiscard]] const ArenaVector<AstNode *> &Body() const noexcept
131    {
132        return body_;
133    }
134
135    const FunctionExpression *Ctor() const;
136    bool HasPrivateMethod() const;
137    bool HasComputedInstanceField() const;
138    bool HasMatchingPrivateKey(const util::StringView &name) const;
139
140    void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
141    void Iterate(const NodeTraverser &cb) const override;
142
143    void Dump(ir::AstDumper *dumper) const override;
144    void Dump(ir::SrcDumper *dumper) const override;
145    void Compile(compiler::PandaGen *pg) const override;
146    void Compile(compiler::ETSGen *etsg) const override;
147    checker::Type *Check(checker::TSChecker *checker) override;
148    checker::Type *Check(checker::ETSChecker *checker) override;
149
150    void Accept(ASTVisitorT *v) override
151    {
152        v->Accept(this);
153    }
154
155private:
156    varbinder::LocalScope *scope_ {nullptr};
157    util::StringView privateId_ {};
158    Identifier *ident_ {};
159    MethodDefinition *ctor_ {};
160    ArenaVector<AstNode *> body_;
161    es2panda::Language lang_;
162};
163}  // namespace ark::es2panda::ir
164
165#endif
166