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#ifndef ES2PANDA_PARSER_INCLUDE_AST_CLASS_ELEMENT_H
17#define ES2PANDA_PARSER_INCLUDE_AST_CLASS_ELEMENT_H
18
19#include "ir/statement.h"
20#include "ir/typed.h"
21
22namespace ark::es2panda::ir {
23class Expression;
24
25class ClassElement : public TypedStatement {
26public:
27    ClassElement() = delete;
28    ~ClassElement() override = default;
29
30    NO_COPY_SEMANTIC(ClassElement);
31    NO_MOVE_SEMANTIC(ClassElement);
32
33    explicit ClassElement(AstNodeType const elementType, Expression *const key, Expression *const value,
34                          ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const isComputed)
35        : TypedStatement(elementType, modifiers),
36          key_(key),
37          value_(value),
38          decorators_(allocator->Adapter()),
39          isComputed_(isComputed)
40    {
41    }
42
43    [[nodiscard]] Identifier *Id() noexcept;
44
45    [[nodiscard]] const Identifier *Id() const noexcept;
46
47    [[nodiscard]] Expression *Key() noexcept
48    {
49        return key_;
50    }
51
52    [[nodiscard]] const Expression *Key() const noexcept
53    {
54        return key_;
55    }
56
57    [[nodiscard]] Expression *Value() noexcept
58    {
59        return value_;
60    }
61
62    void SetValue(Expression *value) noexcept;
63
64    [[nodiscard]] const Expression *Value() const noexcept
65    {
66        return value_;
67    }
68
69    [[nodiscard]] bool IsPrivateElement() const noexcept;
70
71    [[nodiscard]] const ArenaVector<Decorator *> &Decorators() const noexcept
72    {
73        return decorators_;
74    }
75
76    const ArenaVector<Decorator *> *DecoratorsPtr() const override
77    {
78        return &Decorators();
79    }
80
81    [[nodiscard]] bool IsComputed() const noexcept
82    {
83        return isComputed_;
84    }
85
86    void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override
87    {
88        decorators_ = std::move(decorators);
89    }
90
91    void AddDecorator(ir::Decorator *const decorator)
92    {
93        if (decorator != nullptr) {
94            decorators_.emplace_back(decorator);
95        }
96    }
97
98    bool CanHaveDecorator([[maybe_unused]] bool inTs) const override
99    {
100        return true;
101    }
102
103    [[nodiscard]] virtual PrivateFieldKind ToPrivateFieldKind(bool isStatic) const = 0;
104
105protected:
106    // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
107    Expression *key_;
108    Expression *value_;
109    ArenaVector<Decorator *> decorators_;
110    bool isComputed_;
111    // NOLINTEND(misc-non-private-member-variables-in-classes)
112};
113}  // namespace ark::es2panda::ir
114
115#endif
116