1/**
2 * Copyright (c) 2021 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_PROPERTY_H
17#define ES2PANDA_PARSER_INCLUDE_AST_CLASS_PROPERTY_H
18
19#include <ir/statement.h>
20#include <ir/expression.h>
21
22namespace panda::es2panda::compiler {
23class PandaGen;
24}  // namespace panda::es2panda::compiler
25
26namespace panda::es2panda::checker {
27class Checker;
28class Type;
29}  // namespace panda::es2panda::checker
30
31namespace panda::es2panda::ir {
32
33class Expression;
34
35class ClassProperty : public Statement {
36public:
37    explicit ClassProperty(Expression *key, Expression *value, Expression *typeAnnotation, ModifierFlags modifiers,
38                           ArenaVector<Decorator *> &&decorators, bool isComputed, bool definite)
39        : Statement(AstNodeType::CLASS_PROPERTY),
40          key_(key),
41          value_(value),
42          typeAnnotation_(typeAnnotation),
43          modifiers_(modifiers),
44          decorators_(std::move(decorators)),
45          isComputed_(isComputed),
46          definite_(definite)
47    {
48    }
49
50    const Expression *Key() const
51    {
52        return key_;
53    }
54
55    Expression *Key()
56    {
57        return key_;
58    }
59
60    void SetKey(Expression *key)
61    {
62        key_ = key;
63    }
64
65    const Expression *Value() const
66    {
67        return value_;
68    }
69
70    Expression *Value()
71    {
72        return value_;
73    }
74
75    const Expression *TypeAnnotation() const
76    {
77        return typeAnnotation_;
78    }
79
80    ModifierFlags Modifiers() const
81    {
82        return modifiers_;
83    }
84
85    bool IsStatic() const
86    {
87        return (modifiers_ & ModifierFlags::STATIC) != 0;
88    }
89
90    const ArenaVector<Decorator *> &Decorators() const
91    {
92        return decorators_;
93    }
94
95    bool HasDecorators() const
96    {
97        return !decorators_.empty();
98    }
99
100    bool IsComputed() const
101    {
102        return isComputed_;
103    }
104
105    bool IsPrivate() const
106    {
107        return key_->IsPrivateIdentifier();
108    }
109
110    void SetComputed(bool computed)
111    {
112        isComputed_ = computed;
113    }
114
115    bool IsAutoAccessor() const
116    {
117        return (modifiers_ & ModifierFlags::ACCESSOR) != 0;
118    }
119
120    bool Definite() const
121    {
122        return definite_;
123    }
124
125    bool NeedCompileKey() const
126    {
127        return !key_->IsLiteral();
128    }
129
130    void RemoveValue()
131    {
132        value_ = nullptr;
133    }
134
135    void Iterate(const NodeTraverser &cb) const override;
136    void Dump(ir::AstDumper *dumper) const override;
137    void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
138    checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override;
139    void UpdateChildNodes(const NodeUpdater &cb);
140    void UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) override;
141
142private:
143    Expression *key_;
144    Expression *value_;
145    Expression *typeAnnotation_;
146    ModifierFlags modifiers_;
147    ArenaVector<Decorator *> decorators_;
148    bool isComputed_;
149    bool definite_;
150};
151
152}  // namespace panda::es2panda::ir
153
154#endif
155