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_PROPERTY_H
173af6ab5fSopenharmony_ci#define ES2PANDA_PARSER_INCLUDE_AST_PROPERTY_H
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ci#include "ir/expression.h"
203af6ab5fSopenharmony_ci#include "ir/validationInfo.h"
213af6ab5fSopenharmony_ci
223af6ab5fSopenharmony_cinamespace ark::es2panda::ir {
233af6ab5fSopenharmony_cienum class PropertyKind { INIT, GET, SET, PROTO };
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_ciclass Property : public Expression {
263af6ab5fSopenharmony_ciprivate:
273af6ab5fSopenharmony_ci    struct Tag {};
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_cipublic:
303af6ab5fSopenharmony_ci    Property() = delete;
313af6ab5fSopenharmony_ci    ~Property() override = default;
323af6ab5fSopenharmony_ci
333af6ab5fSopenharmony_ci    NO_COPY_OPERATOR(Property);
343af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(Property);
353af6ab5fSopenharmony_ci
363af6ab5fSopenharmony_ci    explicit Property(Expression *const key, Expression *const value)
373af6ab5fSopenharmony_ci        : Expression(AstNodeType::PROPERTY), kind_(PropertyKind::INIT), key_(key), value_(value)
383af6ab5fSopenharmony_ci    {
393af6ab5fSopenharmony_ci    }
403af6ab5fSopenharmony_ci
413af6ab5fSopenharmony_ci    explicit Property(PropertyKind const kind, Expression *const key, Expression *const value, bool const isMethod,
423af6ab5fSopenharmony_ci                      bool const isComputed)
433af6ab5fSopenharmony_ci        : Expression(AstNodeType::PROPERTY),
443af6ab5fSopenharmony_ci          kind_(kind),
453af6ab5fSopenharmony_ci          key_(key),
463af6ab5fSopenharmony_ci          value_(value),
473af6ab5fSopenharmony_ci          isMethod_(isMethod),
483af6ab5fSopenharmony_ci          isShorthand_(false),
493af6ab5fSopenharmony_ci          isComputed_(isComputed)
503af6ab5fSopenharmony_ci    {
513af6ab5fSopenharmony_ci    }
523af6ab5fSopenharmony_ci
533af6ab5fSopenharmony_ci    explicit Property(Tag tag, Property const &other, Expression *key, Expression *value);
543af6ab5fSopenharmony_ci
553af6ab5fSopenharmony_ci    [[nodiscard]] Expression *Key() noexcept
563af6ab5fSopenharmony_ci    {
573af6ab5fSopenharmony_ci        return key_;
583af6ab5fSopenharmony_ci    }
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_ci    [[nodiscard]] const Expression *Key() const noexcept
613af6ab5fSopenharmony_ci    {
623af6ab5fSopenharmony_ci        return key_;
633af6ab5fSopenharmony_ci    }
643af6ab5fSopenharmony_ci
653af6ab5fSopenharmony_ci    [[nodiscard]] const Expression *Value() const noexcept
663af6ab5fSopenharmony_ci    {
673af6ab5fSopenharmony_ci        return value_;
683af6ab5fSopenharmony_ci    }
693af6ab5fSopenharmony_ci
703af6ab5fSopenharmony_ci    [[nodiscard]] Expression *Value() noexcept
713af6ab5fSopenharmony_ci    {
723af6ab5fSopenharmony_ci        return value_;
733af6ab5fSopenharmony_ci    }
743af6ab5fSopenharmony_ci
753af6ab5fSopenharmony_ci    [[nodiscard]] PropertyKind Kind() const noexcept
763af6ab5fSopenharmony_ci    {
773af6ab5fSopenharmony_ci        return kind_;
783af6ab5fSopenharmony_ci    }
793af6ab5fSopenharmony_ci
803af6ab5fSopenharmony_ci    [[nodiscard]] bool IsMethod() const noexcept
813af6ab5fSopenharmony_ci    {
823af6ab5fSopenharmony_ci        return isMethod_;
833af6ab5fSopenharmony_ci    }
843af6ab5fSopenharmony_ci
853af6ab5fSopenharmony_ci    [[nodiscard]] bool IsShorthand() const noexcept
863af6ab5fSopenharmony_ci    {
873af6ab5fSopenharmony_ci        return isShorthand_;
883af6ab5fSopenharmony_ci    }
893af6ab5fSopenharmony_ci
903af6ab5fSopenharmony_ci    [[nodiscard]] bool IsComputed() const noexcept
913af6ab5fSopenharmony_ci    {
923af6ab5fSopenharmony_ci        return isComputed_;
933af6ab5fSopenharmony_ci    }
943af6ab5fSopenharmony_ci
953af6ab5fSopenharmony_ci    [[nodiscard]] bool IsAccessor() const noexcept
963af6ab5fSopenharmony_ci    {
973af6ab5fSopenharmony_ci        return IsAccessorKind(kind_);
983af6ab5fSopenharmony_ci    }
993af6ab5fSopenharmony_ci
1003af6ab5fSopenharmony_ci    [[nodiscard]] static bool IsAccessorKind(PropertyKind kind) noexcept
1013af6ab5fSopenharmony_ci    {
1023af6ab5fSopenharmony_ci        return kind == PropertyKind::GET || kind == PropertyKind::SET;
1033af6ab5fSopenharmony_ci    }
1043af6ab5fSopenharmony_ci
1053af6ab5fSopenharmony_ci    [[nodiscard]] Property *Clone(ArenaAllocator *allocator, AstNode *parent) override;
1063af6ab5fSopenharmony_ci
1073af6ab5fSopenharmony_ci    bool ConvertibleToPatternProperty();
1083af6ab5fSopenharmony_ci    ValidationInfo ValidateExpression();
1093af6ab5fSopenharmony_ci
1103af6ab5fSopenharmony_ci    void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
1113af6ab5fSopenharmony_ci    void Iterate(const NodeTraverser &cb) const override;
1123af6ab5fSopenharmony_ci    void Dump(ir::AstDumper *dumper) const override;
1133af6ab5fSopenharmony_ci    void Dump(ir::SrcDumper *dumper) const override;
1143af6ab5fSopenharmony_ci    void Compile(compiler::PandaGen *pg) const override;
1153af6ab5fSopenharmony_ci    void Compile(compiler::ETSGen *etsg) const override;
1163af6ab5fSopenharmony_ci    checker::Type *Check(checker::TSChecker *checker) override;
1173af6ab5fSopenharmony_ci    checker::Type *Check(checker::ETSChecker *checker) override;
1183af6ab5fSopenharmony_ci
1193af6ab5fSopenharmony_ci    void Accept(ASTVisitorT *v) override
1203af6ab5fSopenharmony_ci    {
1213af6ab5fSopenharmony_ci        v->Accept(this);
1223af6ab5fSopenharmony_ci    }
1233af6ab5fSopenharmony_ci
1243af6ab5fSopenharmony_ciprotected:
1253af6ab5fSopenharmony_ci    Property(Property const &other) : Expression(static_cast<Expression const &>(other))
1263af6ab5fSopenharmony_ci    {
1273af6ab5fSopenharmony_ci        kind_ = other.kind_;
1283af6ab5fSopenharmony_ci        isMethod_ = other.isMethod_;
1293af6ab5fSopenharmony_ci        isShorthand_ = other.isShorthand_;
1303af6ab5fSopenharmony_ci        isComputed_ = other.isComputed_;
1313af6ab5fSopenharmony_ci    }
1323af6ab5fSopenharmony_ci
1333af6ab5fSopenharmony_ciprivate:
1343af6ab5fSopenharmony_ci    PropertyKind kind_;
1353af6ab5fSopenharmony_ci    Expression *key_ = nullptr;
1363af6ab5fSopenharmony_ci    Expression *value_ = nullptr;
1373af6ab5fSopenharmony_ci    bool isMethod_ = false;
1383af6ab5fSopenharmony_ci    bool isShorthand_ = true;
1393af6ab5fSopenharmony_ci    bool isComputed_ = false;
1403af6ab5fSopenharmony_ci};
1413af6ab5fSopenharmony_ci}  // namespace ark::es2panda::ir
1423af6ab5fSopenharmony_ci
1433af6ab5fSopenharmony_ci#endif
144