13af6ab5fSopenharmony_ci/**
23af6ab5fSopenharmony_ci * Copyright (c) 2021-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_METHOD_DEFINITION_H
173af6ab5fSopenharmony_ci#define ES2PANDA_PARSER_INCLUDE_AST_METHOD_DEFINITION_H
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ci#include "ir/base/classElement.h"
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_cinamespace ark::es2panda::checker {
223af6ab5fSopenharmony_ciclass ETSAnalyzer;
233af6ab5fSopenharmony_ci}  // namespace ark::es2panda::checker
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_cinamespace ark::es2panda::ir {
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_ciclass Expression;
283af6ab5fSopenharmony_ciclass ScriptFunction;
293af6ab5fSopenharmony_ci
303af6ab5fSopenharmony_cienum class MethodDefinitionKind { NONE, CONSTRUCTOR, METHOD, EXTENSION_METHOD, GET, SET };
313af6ab5fSopenharmony_ci
323af6ab5fSopenharmony_ciclass MethodDefinition : public ClassElement {
333af6ab5fSopenharmony_cipublic:
343af6ab5fSopenharmony_ci    MethodDefinition() = delete;
353af6ab5fSopenharmony_ci    ~MethodDefinition() override = default;
363af6ab5fSopenharmony_ci
373af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(MethodDefinition);
383af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(MethodDefinition);
393af6ab5fSopenharmony_ci
403af6ab5fSopenharmony_ci    using OverloadsT = ArenaVector<MethodDefinition *>;
413af6ab5fSopenharmony_ci
423af6ab5fSopenharmony_ci    explicit MethodDefinition(MethodDefinitionKind const kind, Expression *const key, Expression *const value,
433af6ab5fSopenharmony_ci                              ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const isComputed)
443af6ab5fSopenharmony_ci        : ClassElement(AstNodeType::METHOD_DEFINITION, key, value, modifiers, allocator, isComputed),
453af6ab5fSopenharmony_ci          kind_(kind),
463af6ab5fSopenharmony_ci          overloads_(allocator->Adapter()),
473af6ab5fSopenharmony_ci          baseOverloadMethod_(nullptr),
483af6ab5fSopenharmony_ci          asyncPairMethod_(nullptr)
493af6ab5fSopenharmony_ci    {
503af6ab5fSopenharmony_ci        ASSERT(key_ != nullptr);
513af6ab5fSopenharmony_ci        ASSERT(value != nullptr);
523af6ab5fSopenharmony_ci    }
533af6ab5fSopenharmony_ci
543af6ab5fSopenharmony_ci    // NOTE (csabahurton): these friend relationships can be removed once there are getters for private fields
553af6ab5fSopenharmony_ci    friend class checker::ETSAnalyzer;
563af6ab5fSopenharmony_ci
573af6ab5fSopenharmony_ci    MethodDefinitionKind Kind() const
583af6ab5fSopenharmony_ci    {
593af6ab5fSopenharmony_ci        return kind_;
603af6ab5fSopenharmony_ci    }
613af6ab5fSopenharmony_ci
623af6ab5fSopenharmony_ci    [[nodiscard]] bool IsConstructor() const noexcept
633af6ab5fSopenharmony_ci    {
643af6ab5fSopenharmony_ci        return kind_ == MethodDefinitionKind::CONSTRUCTOR;
653af6ab5fSopenharmony_ci    }
663af6ab5fSopenharmony_ci
673af6ab5fSopenharmony_ci    [[nodiscard]] bool IsExtensionMethod() const noexcept
683af6ab5fSopenharmony_ci    {
693af6ab5fSopenharmony_ci        return kind_ == MethodDefinitionKind::EXTENSION_METHOD;
703af6ab5fSopenharmony_ci    }
713af6ab5fSopenharmony_ci
723af6ab5fSopenharmony_ci    [[nodiscard]] const OverloadsT &Overloads() const noexcept
733af6ab5fSopenharmony_ci    {
743af6ab5fSopenharmony_ci        return overloads_;
753af6ab5fSopenharmony_ci    }
763af6ab5fSopenharmony_ci
773af6ab5fSopenharmony_ci    [[nodiscard]] const MethodDefinition *BaseOverloadMethod() const noexcept
783af6ab5fSopenharmony_ci    {
793af6ab5fSopenharmony_ci        return baseOverloadMethod_;
803af6ab5fSopenharmony_ci    }
813af6ab5fSopenharmony_ci
823af6ab5fSopenharmony_ci    [[nodiscard]] MethodDefinition *BaseOverloadMethod() noexcept
833af6ab5fSopenharmony_ci    {
843af6ab5fSopenharmony_ci        return baseOverloadMethod_;
853af6ab5fSopenharmony_ci    }
863af6ab5fSopenharmony_ci
873af6ab5fSopenharmony_ci    [[nodiscard]] const MethodDefinition *AsyncPairMethod() const noexcept
883af6ab5fSopenharmony_ci    {
893af6ab5fSopenharmony_ci        return asyncPairMethod_;
903af6ab5fSopenharmony_ci    }
913af6ab5fSopenharmony_ci
923af6ab5fSopenharmony_ci    [[nodiscard]] MethodDefinition *AsyncPairMethod() noexcept
933af6ab5fSopenharmony_ci    {
943af6ab5fSopenharmony_ci        return asyncPairMethod_;
953af6ab5fSopenharmony_ci    }
963af6ab5fSopenharmony_ci
973af6ab5fSopenharmony_ci    void SetOverloads(OverloadsT &&overloads)
983af6ab5fSopenharmony_ci    {
993af6ab5fSopenharmony_ci        overloads_ = std::move(overloads);
1003af6ab5fSopenharmony_ci    }
1013af6ab5fSopenharmony_ci
1023af6ab5fSopenharmony_ci    void ClearOverloads()
1033af6ab5fSopenharmony_ci    {
1043af6ab5fSopenharmony_ci        overloads_.clear();
1053af6ab5fSopenharmony_ci    }
1063af6ab5fSopenharmony_ci
1073af6ab5fSopenharmony_ci    void AddOverload(MethodDefinition *const overload)
1083af6ab5fSopenharmony_ci    {
1093af6ab5fSopenharmony_ci        overloads_.emplace_back(overload);
1103af6ab5fSopenharmony_ci        overload->SetBaseOverloadMethod(this);
1113af6ab5fSopenharmony_ci    }
1123af6ab5fSopenharmony_ci
1133af6ab5fSopenharmony_ci    void SetBaseOverloadMethod(MethodDefinition *const baseOverloadMethod)
1143af6ab5fSopenharmony_ci    {
1153af6ab5fSopenharmony_ci        baseOverloadMethod_ = baseOverloadMethod;
1163af6ab5fSopenharmony_ci    }
1173af6ab5fSopenharmony_ci
1183af6ab5fSopenharmony_ci    void SetAsyncPairMethod(MethodDefinition *const method)
1193af6ab5fSopenharmony_ci    {
1203af6ab5fSopenharmony_ci        asyncPairMethod_ = method;
1213af6ab5fSopenharmony_ci    }
1223af6ab5fSopenharmony_ci
1233af6ab5fSopenharmony_ci    [[nodiscard]] bool HasOverload(MethodDefinition *overload) noexcept
1243af6ab5fSopenharmony_ci    {
1253af6ab5fSopenharmony_ci        return std::find(overloads_.begin(), overloads_.end(), overload) != overloads_.end();
1263af6ab5fSopenharmony_ci    }
1273af6ab5fSopenharmony_ci
1283af6ab5fSopenharmony_ci    ScriptFunction *Function();
1293af6ab5fSopenharmony_ci    const ScriptFunction *Function() const;
1303af6ab5fSopenharmony_ci    PrivateFieldKind ToPrivateFieldKind(bool isStatic) const override;
1313af6ab5fSopenharmony_ci
1323af6ab5fSopenharmony_ci    [[nodiscard]] MethodDefinition *Clone(ArenaAllocator *allocator, AstNode *parent) override;
1333af6ab5fSopenharmony_ci
1343af6ab5fSopenharmony_ci    void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
1353af6ab5fSopenharmony_ci    void Iterate(const NodeTraverser &cb) const override;
1363af6ab5fSopenharmony_ci
1373af6ab5fSopenharmony_ci    void ResolveReferences(const NodeTraverser &cb) const;
1383af6ab5fSopenharmony_ci
1393af6ab5fSopenharmony_ci    void Dump(ir::AstDumper *dumper) const override;
1403af6ab5fSopenharmony_ci    void Dump(ir::SrcDumper *dumper) const override;
1413af6ab5fSopenharmony_ci    void Compile(compiler::PandaGen *pg) const override;
1423af6ab5fSopenharmony_ci    void Compile(compiler::ETSGen *etsg) const override;
1433af6ab5fSopenharmony_ci    checker::Type *Check(checker::TSChecker *checker) override;
1443af6ab5fSopenharmony_ci    checker::Type *Check(checker::ETSChecker *checker) override;
1453af6ab5fSopenharmony_ci
1463af6ab5fSopenharmony_ci    void Accept(ASTVisitorT *v) override
1473af6ab5fSopenharmony_ci    {
1483af6ab5fSopenharmony_ci        v->Accept(this);
1493af6ab5fSopenharmony_ci    }
1503af6ab5fSopenharmony_ci
1513af6ab5fSopenharmony_ciprivate:
1523af6ab5fSopenharmony_ci    void DumpPrefix(ir::SrcDumper *dumper) const;
1533af6ab5fSopenharmony_ci
1543af6ab5fSopenharmony_ci    MethodDefinitionKind kind_;
1553af6ab5fSopenharmony_ci    // Overloads are stored like in an 1:N fashion.
1563af6ab5fSopenharmony_ci    // The very firstly processed method becomes the base(1) and the others tied into it as overloads(N).
1573af6ab5fSopenharmony_ci    OverloadsT overloads_;
1583af6ab5fSopenharmony_ci    // Base overload method points at the first overload of the overloads.
1593af6ab5fSopenharmony_ci    MethodDefinition *baseOverloadMethod_;
1603af6ab5fSopenharmony_ci    // Pair method points at the original async method in case of an implement method and vice versa an implement
1613af6ab5fSopenharmony_ci    // method's point at the async method
1623af6ab5fSopenharmony_ci    MethodDefinition *asyncPairMethod_;
1633af6ab5fSopenharmony_ci};
1643af6ab5fSopenharmony_ci}  // namespace ark::es2panda::ir
1653af6ab5fSopenharmony_ci
1663af6ab5fSopenharmony_ci#endif
167