13af6ab5fSopenharmony_ci/**
23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#ifndef ES2PANDA_PARSER_INCLUDE_AST_NAMESPACE_DEFINITION_H
173af6ab5fSopenharmony_ci#define ES2PANDA_PARSER_INCLUDE_AST_NAMESPACE_DEFINITION_H
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ci#include "varbinder/scope.h"
203af6ab5fSopenharmony_ci#include "varbinder/variable.h"
213af6ab5fSopenharmony_ci#include "ir/astNode.h"
223af6ab5fSopenharmony_ci#include "ir/expressions/identifier.h"
233af6ab5fSopenharmony_ci#include "util/language.h"
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_cinamespace ark::es2panda::ir {
263af6ab5fSopenharmony_ciclass ClassElement;
273af6ab5fSopenharmony_ciclass Identifier;
283af6ab5fSopenharmony_ciclass MethodDefinition;
293af6ab5fSopenharmony_ciclass TSTypeParameterDeclaration;
303af6ab5fSopenharmony_ciclass TSTypeParameterInstantiation;
313af6ab5fSopenharmony_ciclass TSClassImplements;
323af6ab5fSopenharmony_ciclass TSIndexSignature;
333af6ab5fSopenharmony_ci
343af6ab5fSopenharmony_ciclass NamespaceDefinition : public TypedAstNode {
353af6ab5fSopenharmony_cipublic:
363af6ab5fSopenharmony_ci    NamespaceDefinition() = delete;
373af6ab5fSopenharmony_ci    ~NamespaceDefinition() override = default;
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(NamespaceDefinition);
403af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(NamespaceDefinition);
413af6ab5fSopenharmony_ci
423af6ab5fSopenharmony_ci    explicit NamespaceDefinition(Identifier *ident, ArenaVector<AstNode *> &&body, MethodDefinition *ctor,
433af6ab5fSopenharmony_ci                                 ModifierFlags flags, Language lang)
443af6ab5fSopenharmony_ci        : TypedAstNode(AstNodeType::NAMESPACE_DEFINITION, flags),
453af6ab5fSopenharmony_ci
463af6ab5fSopenharmony_ci          ident_(ident),
473af6ab5fSopenharmony_ci          ctor_(ctor),
483af6ab5fSopenharmony_ci          body_(std::move(body)),
493af6ab5fSopenharmony_ci          lang_(lang)
503af6ab5fSopenharmony_ci    {
513af6ab5fSopenharmony_ci    }
523af6ab5fSopenharmony_ci
533af6ab5fSopenharmony_ci    [[nodiscard]] bool IsScopeBearer() const noexcept override
543af6ab5fSopenharmony_ci    {
553af6ab5fSopenharmony_ci        return true;
563af6ab5fSopenharmony_ci    }
573af6ab5fSopenharmony_ci
583af6ab5fSopenharmony_ci    [[nodiscard]] varbinder::LocalScope *Scope() const noexcept override
593af6ab5fSopenharmony_ci    {
603af6ab5fSopenharmony_ci        return scope_;
613af6ab5fSopenharmony_ci    }
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_ci    void SetScope(varbinder::LocalScope *scope)
643af6ab5fSopenharmony_ci    {
653af6ab5fSopenharmony_ci        ASSERT(scope_ == nullptr);
663af6ab5fSopenharmony_ci        scope_ = scope;
673af6ab5fSopenharmony_ci    }
683af6ab5fSopenharmony_ci
693af6ab5fSopenharmony_ci    void ClearScope() noexcept override
703af6ab5fSopenharmony_ci    {
713af6ab5fSopenharmony_ci        scope_ = nullptr;
723af6ab5fSopenharmony_ci    }
733af6ab5fSopenharmony_ci
743af6ab5fSopenharmony_ci    [[nodiscard]] const Identifier *Ident() const noexcept
753af6ab5fSopenharmony_ci    {
763af6ab5fSopenharmony_ci        return ident_;
773af6ab5fSopenharmony_ci    }
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_ci    [[nodiscard]] Identifier *Ident() noexcept
803af6ab5fSopenharmony_ci    {
813af6ab5fSopenharmony_ci        return ident_;
823af6ab5fSopenharmony_ci    }
833af6ab5fSopenharmony_ci
843af6ab5fSopenharmony_ci    void SetIdent(ir::Identifier *ident) noexcept;
853af6ab5fSopenharmony_ci
863af6ab5fSopenharmony_ci    [[nodiscard]] const util::StringView &PrivateId() const noexcept
873af6ab5fSopenharmony_ci    {
883af6ab5fSopenharmony_ci        return privateId_;
893af6ab5fSopenharmony_ci    }
903af6ab5fSopenharmony_ci
913af6ab5fSopenharmony_ci    [[nodiscard]] const util::StringView &InternalName() const noexcept
923af6ab5fSopenharmony_ci    {
933af6ab5fSopenharmony_ci        return privateId_;
943af6ab5fSopenharmony_ci    }
953af6ab5fSopenharmony_ci
963af6ab5fSopenharmony_ci    void SetInternalName(util::StringView internalName) noexcept
973af6ab5fSopenharmony_ci    {
983af6ab5fSopenharmony_ci        privateId_ = internalName;
993af6ab5fSopenharmony_ci    }
1003af6ab5fSopenharmony_ci
1013af6ab5fSopenharmony_ci    [[nodiscard]] es2panda::Language Language() const noexcept
1023af6ab5fSopenharmony_ci    {
1033af6ab5fSopenharmony_ci        return lang_;
1043af6ab5fSopenharmony_ci    }
1053af6ab5fSopenharmony_ci
1063af6ab5fSopenharmony_ci    [[nodiscard]] MethodDefinition *Ctor() noexcept
1073af6ab5fSopenharmony_ci    {
1083af6ab5fSopenharmony_ci        return ctor_;
1093af6ab5fSopenharmony_ci    }
1103af6ab5fSopenharmony_ci
1113af6ab5fSopenharmony_ci    void SetCtor(MethodDefinition *ctor)
1123af6ab5fSopenharmony_ci    {
1133af6ab5fSopenharmony_ci        ctor_ = ctor;
1143af6ab5fSopenharmony_ci    }
1153af6ab5fSopenharmony_ci
1163af6ab5fSopenharmony_ci    void AddProperties(ArenaVector<AstNode *> &&body)
1173af6ab5fSopenharmony_ci    {
1183af6ab5fSopenharmony_ci        for (auto *prop : body) {
1193af6ab5fSopenharmony_ci            prop->SetParent(this);
1203af6ab5fSopenharmony_ci        }
1213af6ab5fSopenharmony_ci
1223af6ab5fSopenharmony_ci        body_.insert(body_.end(), body.begin(), body.end());
1233af6ab5fSopenharmony_ci    }
1243af6ab5fSopenharmony_ci
1253af6ab5fSopenharmony_ci    [[nodiscard]] ArenaVector<AstNode *> &Body() noexcept
1263af6ab5fSopenharmony_ci    {
1273af6ab5fSopenharmony_ci        return body_;
1283af6ab5fSopenharmony_ci    }
1293af6ab5fSopenharmony_ci
1303af6ab5fSopenharmony_ci    [[nodiscard]] const ArenaVector<AstNode *> &Body() const noexcept
1313af6ab5fSopenharmony_ci    {
1323af6ab5fSopenharmony_ci        return body_;
1333af6ab5fSopenharmony_ci    }
1343af6ab5fSopenharmony_ci
1353af6ab5fSopenharmony_ci    const FunctionExpression *Ctor() const;
1363af6ab5fSopenharmony_ci    bool HasPrivateMethod() const;
1373af6ab5fSopenharmony_ci    bool HasComputedInstanceField() const;
1383af6ab5fSopenharmony_ci    bool HasMatchingPrivateKey(const util::StringView &name) const;
1393af6ab5fSopenharmony_ci
1403af6ab5fSopenharmony_ci    void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
1413af6ab5fSopenharmony_ci    void Iterate(const NodeTraverser &cb) const override;
1423af6ab5fSopenharmony_ci
1433af6ab5fSopenharmony_ci    void Dump(ir::AstDumper *dumper) const override;
1443af6ab5fSopenharmony_ci    void Dump(ir::SrcDumper *dumper) const override;
1453af6ab5fSopenharmony_ci    void Compile(compiler::PandaGen *pg) const override;
1463af6ab5fSopenharmony_ci    void Compile(compiler::ETSGen *etsg) const override;
1473af6ab5fSopenharmony_ci    checker::Type *Check(checker::TSChecker *checker) override;
1483af6ab5fSopenharmony_ci    checker::Type *Check(checker::ETSChecker *checker) override;
1493af6ab5fSopenharmony_ci
1503af6ab5fSopenharmony_ci    void Accept(ASTVisitorT *v) override
1513af6ab5fSopenharmony_ci    {
1523af6ab5fSopenharmony_ci        v->Accept(this);
1533af6ab5fSopenharmony_ci    }
1543af6ab5fSopenharmony_ci
1553af6ab5fSopenharmony_ciprivate:
1563af6ab5fSopenharmony_ci    varbinder::LocalScope *scope_ {nullptr};
1573af6ab5fSopenharmony_ci    util::StringView privateId_ {};
1583af6ab5fSopenharmony_ci    Identifier *ident_ {};
1593af6ab5fSopenharmony_ci    MethodDefinition *ctor_ {};
1603af6ab5fSopenharmony_ci    ArenaVector<AstNode *> body_;
1613af6ab5fSopenharmony_ci    es2panda::Language lang_;
1623af6ab5fSopenharmony_ci};
1633af6ab5fSopenharmony_ci}  // namespace ark::es2panda::ir
1643af6ab5fSopenharmony_ci
1653af6ab5fSopenharmony_ci#endif
166