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_PROPERTY_H
17#define ES2PANDA_PARSER_INCLUDE_AST_PROPERTY_H
18
19#include <ir/expression.h>
20#include <ir/validationInfo.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
33enum class PropertyKind { INIT, GET, SET, PROTO };
34
35class Property : public Expression {
36public:
37    explicit Property(Expression *key, Expression *value)
38        : Expression(AstNodeType::PROPERTY),
39          kind_(PropertyKind::INIT),
40          key_(key),
41          value_(value),
42          isMethod_(false),
43          isShorthand_(true),
44          isComputed_(false)
45    {
46    }
47
48    explicit Property(PropertyKind kind, Expression *key, Expression *value, bool isMethod, bool isComputed)
49        : Expression(AstNodeType::PROPERTY),
50          kind_(kind),
51          key_(key),
52          value_(value),
53          isMethod_(isMethod),
54          isShorthand_(false),
55          isComputed_(isComputed)
56    {
57    }
58
59    Expression *Key()
60    {
61        return key_;
62    }
63
64    const Expression *Key() const
65    {
66        return key_;
67    }
68
69    const Expression *Value() const
70    {
71        return value_;
72    }
73
74    Expression *Value()
75    {
76        return value_;
77    }
78
79    PropertyKind Kind() const
80    {
81        return kind_;
82    }
83
84    bool IsMethod() const
85    {
86        return isMethod_;
87    }
88
89    bool IsShorthand() const
90    {
91        return isShorthand_;
92    }
93
94    bool IsComputed() const
95    {
96        return isComputed_;
97    }
98
99    bool IsAccessor() const
100    {
101        return IsAccessorKind(kind_);
102    }
103
104    static bool IsAccessorKind(PropertyKind kind)
105    {
106        return kind == PropertyKind::GET || kind == PropertyKind::SET;
107    }
108
109    bool ConventibleToPatternProperty();
110    ValidationInfo ValidateExpression();
111    void Iterate(const NodeTraverser &cb) const override;
112    void Dump(ir::AstDumper *dumper) const override;
113    void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
114    checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override;
115    void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override;
116
117private:
118    PropertyKind kind_;
119    Expression *key_;
120    Expression *value_;
121    bool isMethod_;
122    bool isShorthand_;
123    bool isComputed_;
124};
125
126}  // namespace panda::es2panda::ir
127
128#endif
129